@waku/enr 0.0.16 → 0.0.18

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/raw_enr.ts CHANGED
@@ -1,13 +1,20 @@
1
1
  import type { Multiaddr } from "@multiformats/multiaddr";
2
2
  import {
3
3
  convertToBytes,
4
- convertToString,
4
+ convertToString
5
5
  } from "@multiformats/multiaddr/convert";
6
- import type { ENRKey, ENRValue, SequenceNumber, Waku2 } from "@waku/interfaces";
6
+ import type {
7
+ ENRKey,
8
+ ENRValue,
9
+ SequenceNumber,
10
+ ShardInfo,
11
+ Waku2
12
+ } from "@waku/interfaces";
7
13
  import { bytesToUtf8 } from "@waku/utils/bytes";
8
14
 
9
15
  import { ERR_INVALID_ID } from "./constants.js";
10
16
  import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec.js";
17
+ import { decodeRelayShard } from "./relay_shard_codec.js";
11
18
  import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js";
12
19
 
13
20
  export class RawEnr extends Map<ENRKey, ENRValue> {
@@ -45,6 +52,18 @@ export class RawEnr extends Map<ENRKey, ENRValue> {
45
52
  }
46
53
  }
47
54
 
55
+ get rs(): ShardInfo | undefined {
56
+ const rs = this.get("rs");
57
+ if (!rs) return undefined;
58
+ return decodeRelayShard(rs);
59
+ }
60
+
61
+ get rsv(): ShardInfo | undefined {
62
+ const rsv = this.get("rsv");
63
+ if (!rsv) return undefined;
64
+ return decodeRelayShard(rsv);
65
+ }
66
+
48
67
  get ip(): string | undefined {
49
68
  return getStringValue(this, "ip", "ip4");
50
69
  }
@@ -0,0 +1,60 @@
1
+ import type { ShardInfo } from "@waku/interfaces";
2
+
3
+ export const decodeRelayShard = (bytes: Uint8Array): ShardInfo => {
4
+ // explicitly converting to Uint8Array to avoid Buffer
5
+ // https://github.com/libp2p/js-libp2p/issues/2146
6
+ bytes = new Uint8Array(bytes);
7
+
8
+ if (bytes.length < 3) throw new Error("Insufficient data");
9
+
10
+ const view = new DataView(bytes.buffer);
11
+ const cluster = view.getUint16(0);
12
+
13
+ const indexList = [];
14
+
15
+ if (bytes.length === 130) {
16
+ // rsv format (Bit Vector)
17
+ for (let i = 0; i < 1024; i++) {
18
+ const byteIndex = Math.floor(i / 8) + 2; // Adjusted for the 2-byte cluster field
19
+ const bitIndex = 7 - (i % 8);
20
+ if (view.getUint8(byteIndex) & (1 << bitIndex)) {
21
+ indexList.push(i);
22
+ }
23
+ }
24
+ } else {
25
+ // rs format (Index List)
26
+ const numIndices = view.getUint8(2);
27
+ for (let i = 0, offset = 3; i < numIndices; i++, offset += 2) {
28
+ if (offset + 1 >= bytes.length) throw new Error("Unexpected end of data");
29
+ indexList.push(view.getUint16(offset));
30
+ }
31
+ }
32
+
33
+ return { cluster, indexList };
34
+ };
35
+
36
+ export const encodeRelayShard = (shardInfo: ShardInfo): Uint8Array => {
37
+ const { cluster, indexList } = shardInfo;
38
+ const totalLength = indexList.length >= 64 ? 130 : 3 + 2 * indexList.length;
39
+ const buffer = new ArrayBuffer(totalLength);
40
+ const view = new DataView(buffer);
41
+
42
+ view.setUint16(0, cluster);
43
+
44
+ if (indexList.length >= 64) {
45
+ // rsv format (Bit Vector)
46
+ for (const index of indexList) {
47
+ const byteIndex = Math.floor(index / 8) + 2; // Adjusted for the 2-byte cluster field
48
+ const bitIndex = 7 - (index % 8);
49
+ view.setUint8(byteIndex, view.getUint8(byteIndex) | (1 << bitIndex));
50
+ }
51
+ } else {
52
+ // rs format (Index List)
53
+ view.setUint8(2, indexList.length);
54
+ for (let i = 0, offset = 3; i < indexList.length; i++, offset += 2) {
55
+ view.setUint16(offset, indexList[i]);
56
+ }
57
+ }
58
+
59
+ return new Uint8Array(buffer);
60
+ };
package/src/v4.ts CHANGED
@@ -8,7 +8,7 @@ export async function sign(
8
8
  msg: Uint8Array
9
9
  ): Promise<Uint8Array> {
10
10
  return secp.sign(keccak256(msg), privKey, {
11
- der: false,
11
+ der: false
12
12
  });
13
13
  }
14
14
 
@@ -19,7 +19,7 @@ export function decodeWaku2(byte: number): Waku2 {
19
19
  relay: false,
20
20
  store: false,
21
21
  filter: false,
22
- lightPush: false,
22
+ lightPush: false
23
23
  };
24
24
 
25
25
  if (byte % 2) waku2.relay = true;