@waku/enr 0.0.17 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waku/enr",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "description": "ENR (EIP-778) for Waku",
5
5
  "types": "./dist/index.d.ts",
6
6
  "module": "./dist/index.js",
@@ -48,7 +48,7 @@
48
48
  "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build"
49
49
  },
50
50
  "engines": {
51
- "node": ">=16"
51
+ "node": ">=18"
52
52
  },
53
53
  "dependencies": {
54
54
  "@ethersproject/rlp": "^5.7.0",
@@ -56,9 +56,9 @@
56
56
  "@libp2p/peer-id": "^3.0.2",
57
57
  "@multiformats/multiaddr": "^12.0.0",
58
58
  "@noble/secp256k1": "^1.7.1",
59
- "@waku/utils": "0.0.11",
59
+ "@waku/utils": "0.0.12",
60
60
  "debug": "^4.3.4",
61
- "js-sha3": "^0.8.0"
61
+ "js-sha3": "^0.9.2"
62
62
  },
63
63
  "devDependencies": {
64
64
  "@libp2p/peer-id-factory": "^3.0.3",
@@ -68,25 +68,16 @@
68
68
  "@types/chai": "^4.3.5",
69
69
  "@types/mocha": "^10.0.1",
70
70
  "@waku/build-utils": "*",
71
- "@waku/interfaces": "0.0.18",
71
+ "@waku/interfaces": "0.0.19",
72
72
  "chai": "^4.3.7",
73
73
  "cspell": "^7.3.2",
74
- "karma": "^6.4.1",
75
- "karma-chrome-launcher": "^3.2.0",
76
- "karma-mocha": "^2.0.1",
77
- "karma-webpack": "^5.0.0",
74
+ "fast-check": "^3.13.1",
78
75
  "mocha": "^10.2.0",
79
76
  "npm-run-all": "^4.1.5",
80
77
  "process": "^0.11.10",
81
- "puppeteer": "^21.1.1",
82
- "rollup": "^3.29.0",
83
- "ts-loader": "^9.4.2",
84
- "typescript": "^5.0.4",
78
+ "rollup": "^3.29.2",
85
79
  "uint8arrays": "^4.0.4"
86
80
  },
87
- "typedoc": {
88
- "entryPoint": "./src/index.ts"
89
- },
90
81
  "files": [
91
82
  "dist",
92
83
  "bundle",
package/src/enr.ts CHANGED
@@ -6,7 +6,8 @@ import type {
6
6
  ENRValue,
7
7
  IEnr,
8
8
  NodeId,
9
- SequenceNumber
9
+ SequenceNumber,
10
+ ShardInfo
10
11
  } from "@waku/interfaces";
11
12
  import debug from "debug";
12
13
 
@@ -64,6 +65,13 @@ export class ENR extends RawEnr implements IEnr {
64
65
  protocol: TransportProtocol | TransportProtocolPerIpVersion
65
66
  ) => Multiaddr | undefined = locationMultiaddrFromEnrFields.bind({}, this);
66
67
 
68
+ get shardInfo(): ShardInfo | undefined {
69
+ if (this.rs && this.rsv) {
70
+ log("Warning: ENR contains both `rs` and `rsv` fields.");
71
+ }
72
+ return this.rs || this.rsv;
73
+ }
74
+
67
75
  setLocationMultiaddr(multiaddr: Multiaddr): void {
68
76
  const protoNames = multiaddr.protoNames();
69
77
  if (
package/src/index.ts CHANGED
@@ -5,3 +5,4 @@ export * from "./enr.js";
5
5
  export * from "./peer_id.js";
6
6
  export * from "./waku2_codec.js";
7
7
  export * from "./crypto.js";
8
+ export * from "./relay_shard_codec.js";
package/src/raw_enr.ts CHANGED
@@ -3,11 +3,18 @@ import {
3
3
  convertToBytes,
4
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
+ };