@waku/enr 0.0.25-ce62600.0 → 0.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/encoder.ts CHANGED
@@ -7,7 +7,7 @@ import { ERR_NO_SIGNATURE, MAX_RECORD_SIZE } from "./constants.js";
7
7
  import { ENR } from "./enr.js";
8
8
 
9
9
  export class EnrEncoder {
10
- static async toValues(
10
+ public static async toValues(
11
11
  enr: ENR,
12
12
  privateKey?: Uint8Array
13
13
  ): Promise<(ENRKey | ENRValue | number[])[]> {
@@ -31,7 +31,10 @@ export class EnrEncoder {
31
31
  return content;
32
32
  }
33
33
 
34
- static async toBytes(enr: ENR, privateKey?: Uint8Array): Promise<Uint8Array> {
34
+ public static async toBytes(
35
+ enr: ENR,
36
+ privateKey?: Uint8Array
37
+ ): Promise<Uint8Array> {
35
38
  const encoded = hexToBytes(
36
39
  RLP.encode(await EnrEncoder.toValues(enr, privateKey))
37
40
  );
@@ -41,7 +44,10 @@ export class EnrEncoder {
41
44
  return encoded;
42
45
  }
43
46
 
44
- static async toString(enr: ENR, privateKey?: Uint8Array): Promise<string> {
47
+ public static async toString(
48
+ enr: ENR,
49
+ privateKey?: Uint8Array
50
+ ): Promise<string> {
45
51
  return (
46
52
  ENR.RECORD_PREFIX +
47
53
  toString(await EnrEncoder.toBytes(enr, privateKey), "base64url")
package/src/enr.ts CHANGED
@@ -34,7 +34,7 @@ export class ENR extends RawEnr implements IEnr {
34
34
  public static readonly RECORD_PREFIX = "enr:";
35
35
  public peerId?: PeerId;
36
36
 
37
- static async create(
37
+ public static async create(
38
38
  kvs: Record<ENRKey, ENRValue> = {},
39
39
  seq: SequenceNumber = BigInt(1),
40
40
  signature?: Uint8Array
@@ -52,7 +52,7 @@ export class ENR extends RawEnr implements IEnr {
52
52
  return enr;
53
53
  }
54
54
 
55
- get nodeId(): NodeId | undefined {
55
+ public get nodeId(): NodeId | undefined {
56
56
  switch (this.id) {
57
57
  case "v4":
58
58
  return this.publicKey ? v4.nodeId(this.publicKey) : undefined;
@@ -60,18 +60,18 @@ export class ENR extends RawEnr implements IEnr {
60
60
  throw new Error(ERR_INVALID_ID);
61
61
  }
62
62
  }
63
- getLocationMultiaddr: (
63
+ public getLocationMultiaddr: (
64
64
  protocol: TransportProtocol | TransportProtocolPerIpVersion
65
65
  ) => Multiaddr | undefined = locationMultiaddrFromEnrFields.bind({}, this);
66
66
 
67
- get shardInfo(): ShardInfo | undefined {
67
+ public get shardInfo(): ShardInfo | undefined {
68
68
  if (this.rs && this.rsv) {
69
69
  log.warn("ENR contains both `rs` and `rsv` fields.");
70
70
  }
71
71
  return this.rs || this.rsv;
72
72
  }
73
73
 
74
- setLocationMultiaddr(multiaddr: Multiaddr): void {
74
+ public setLocationMultiaddr(multiaddr: Multiaddr): void {
75
75
  const protoNames = multiaddr.protoNames();
76
76
  if (
77
77
  protoNames.length !== 2 &&
@@ -95,7 +95,7 @@ export class ENR extends RawEnr implements IEnr {
95
95
  }
96
96
  }
97
97
 
98
- getAllLocationMultiaddrs(): Multiaddr[] {
98
+ public getAllLocationMultiaddrs(): Multiaddr[] {
99
99
  const multiaddrs = [];
100
100
 
101
101
  for (const protocol of Object.values(TransportProtocolPerIpVersion)) {
@@ -115,7 +115,7 @@ export class ENR extends RawEnr implements IEnr {
115
115
  });
116
116
  }
117
117
 
118
- get peerInfo(): PeerInfo | undefined {
118
+ public get peerInfo(): PeerInfo | undefined {
119
119
  const id = this.peerId;
120
120
  if (!id) return;
121
121
  return {
@@ -132,7 +132,7 @@ export class ENR extends RawEnr implements IEnr {
132
132
  *
133
133
  * @param protocol
134
134
  */
135
- getFullMultiaddr(
135
+ public getFullMultiaddr(
136
136
  protocol: TransportProtocol | TransportProtocolPerIpVersion
137
137
  ): Multiaddr | undefined {
138
138
  if (this.peerId) {
@@ -147,7 +147,7 @@ export class ENR extends RawEnr implements IEnr {
147
147
  /**
148
148
  * Returns the full multiaddrs from the `multiaddrs` ENR field.
149
149
  */
150
- getFullMultiaddrs(): Multiaddr[] {
150
+ public getFullMultiaddrs(): Multiaddr[] {
151
151
  if (this.peerId && this.multiaddrs) {
152
152
  const peerId = this.peerId;
153
153
  return this.multiaddrs.map((ma) => {
@@ -157,7 +157,7 @@ export class ENR extends RawEnr implements IEnr {
157
157
  return [];
158
158
  }
159
159
 
160
- verify(data: Uint8Array, signature: Uint8Array): boolean {
160
+ public verify(data: Uint8Array, signature: Uint8Array): boolean {
161
161
  if (!this.get("id") || this.id !== "v4") {
162
162
  throw new Error(ERR_INVALID_ID);
163
163
  }
@@ -167,7 +167,10 @@ export class ENR extends RawEnr implements IEnr {
167
167
  return verifySignature(signature, keccak256(data), this.publicKey);
168
168
  }
169
169
 
170
- async sign(data: Uint8Array, privateKey: Uint8Array): Promise<Uint8Array> {
170
+ public async sign(
171
+ data: Uint8Array,
172
+ privateKey: Uint8Array
173
+ ): Promise<Uint8Array> {
171
174
  switch (this.id) {
172
175
  case "v4":
173
176
  this.signature = await v4.sign(privateKey, data);
package/src/peer_id.ts CHANGED
@@ -15,6 +15,10 @@ export function getPublicKeyFromPeerId(peerId: PeerId): Uint8Array {
15
15
  throw new Error("Unsupported peer id type");
16
16
  }
17
17
 
18
+ if (!peerId.publicKey) {
19
+ throw new Error("Public key not present on peer id");
20
+ }
21
+
18
22
  return unmarshalPublicKey(peerId.publicKey).marshal();
19
23
  }
20
24
 
package/src/raw_enr.ts CHANGED
@@ -31,19 +31,19 @@ export class RawEnr extends Map<ENRKey, ENRValue> {
31
31
  this.signature = signature;
32
32
  }
33
33
 
34
- set(k: ENRKey, v: ENRValue): this {
34
+ public set(k: ENRKey, v: ENRValue): this {
35
35
  this.signature = undefined;
36
36
  this.seq++;
37
37
  return super.set(k, v);
38
38
  }
39
39
 
40
- get id(): string {
40
+ public get id(): string {
41
41
  const id = this.get("id");
42
42
  if (!id) throw new Error("id not found.");
43
43
  return bytesToUtf8(id);
44
44
  }
45
45
 
46
- get publicKey(): Uint8Array | undefined {
46
+ public get publicKey(): Uint8Array | undefined {
47
47
  switch (this.id) {
48
48
  case "v4":
49
49
  return this.get("secp256k1");
@@ -52,63 +52,63 @@ export class RawEnr extends Map<ENRKey, ENRValue> {
52
52
  }
53
53
  }
54
54
 
55
- get rs(): ShardInfo | undefined {
55
+ public get rs(): ShardInfo | undefined {
56
56
  const rs = this.get("rs");
57
57
  if (!rs) return undefined;
58
58
  return decodeRelayShard(rs);
59
59
  }
60
60
 
61
- get rsv(): ShardInfo | undefined {
61
+ public get rsv(): ShardInfo | undefined {
62
62
  const rsv = this.get("rsv");
63
63
  if (!rsv) return undefined;
64
64
  return decodeRelayShard(rsv);
65
65
  }
66
66
 
67
- get ip(): string | undefined {
67
+ public get ip(): string | undefined {
68
68
  return getStringValue(this, "ip", "ip4");
69
69
  }
70
70
 
71
- set ip(ip: string | undefined) {
71
+ public set ip(ip: string | undefined) {
72
72
  setStringValue(this, "ip", "ip4", ip);
73
73
  }
74
74
 
75
- get tcp(): number | undefined {
75
+ public get tcp(): number | undefined {
76
76
  return getNumberAsStringValue(this, "tcp", "tcp");
77
77
  }
78
78
 
79
- set tcp(port: number | undefined) {
79
+ public set tcp(port: number | undefined) {
80
80
  setNumberAsStringValue(this, "tcp", "tcp", port);
81
81
  }
82
82
 
83
- get udp(): number | undefined {
83
+ public get udp(): number | undefined {
84
84
  return getNumberAsStringValue(this, "udp", "udp");
85
85
  }
86
86
 
87
- set udp(port: number | undefined) {
87
+ public set udp(port: number | undefined) {
88
88
  setNumberAsStringValue(this, "udp", "udp", port);
89
89
  }
90
90
 
91
- get ip6(): string | undefined {
91
+ public get ip6(): string | undefined {
92
92
  return getStringValue(this, "ip6", "ip6");
93
93
  }
94
94
 
95
- set ip6(ip: string | undefined) {
95
+ public set ip6(ip: string | undefined) {
96
96
  setStringValue(this, "ip6", "ip6", ip);
97
97
  }
98
98
 
99
- get tcp6(): number | undefined {
99
+ public get tcp6(): number | undefined {
100
100
  return getNumberAsStringValue(this, "tcp6", "tcp");
101
101
  }
102
102
 
103
- set tcp6(port: number | undefined) {
103
+ public set tcp6(port: number | undefined) {
104
104
  setNumberAsStringValue(this, "tcp6", "tcp", port);
105
105
  }
106
106
 
107
- get udp6(): number | undefined {
107
+ public get udp6(): number | undefined {
108
108
  return getNumberAsStringValue(this, "udp6", "udp");
109
109
  }
110
110
 
111
- set udp6(port: number | undefined) {
111
+ public set udp6(port: number | undefined) {
112
112
  setNumberAsStringValue(this, "udp6", "udp", port);
113
113
  }
114
114
 
@@ -124,7 +124,7 @@ export class RawEnr extends Map<ENRKey, ENRValue> {
124
124
  *
125
125
  * The multiaddresses stored in this field are expected to be location multiaddresses, ie, peer id less.
126
126
  */
127
- get multiaddrs(): Multiaddr[] | undefined {
127
+ public get multiaddrs(): Multiaddr[] | undefined {
128
128
  const raw = this.get("multiaddrs");
129
129
 
130
130
  if (raw) return decodeMultiaddrs(raw);
@@ -144,14 +144,14 @@ export class RawEnr extends Map<ENRKey, ENRValue> {
144
144
  * The multiaddresses stored in this field must be location multiaddresses,
145
145
  * ie, without a peer id.
146
146
  */
147
- set multiaddrs(multiaddrs: Multiaddr[] | undefined) {
147
+ public set multiaddrs(multiaddrs: Multiaddr[] | undefined) {
148
148
  deleteUndefined(this, "multiaddrs", multiaddrs, encodeMultiaddrs);
149
149
  }
150
150
 
151
151
  /**
152
152
  * Get the `waku2` field from ENR.
153
153
  */
154
- get waku2(): Waku2 | undefined {
154
+ public get waku2(): Waku2 | undefined {
155
155
  const raw = this.get("waku2");
156
156
  if (raw) return decodeWaku2(raw[0]);
157
157
 
@@ -161,7 +161,7 @@ export class RawEnr extends Map<ENRKey, ENRValue> {
161
161
  /**
162
162
  * Set the `waku2` field on the ENR.
163
163
  */
164
- set waku2(waku2: Waku2 | undefined) {
164
+ public set waku2(waku2: Waku2 | undefined) {
165
165
  deleteUndefined(
166
166
  this,
167
167
  "waku2",