@waku/core 0.0.36-f911bf8.0 → 0.0.36
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/CHANGELOG.md +39 -0
- package/bundle/index.js +1143 -610
- package/bundle/lib/message/version_0.js +1 -2
- package/bundle/{version_0-CiYGrPc2.js → version_0-9DPFjcJG.js} +1586 -10
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/connection_manager/connection_manager.d.ts +2 -1
- package/dist/lib/connection_manager/connection_manager.js +16 -8
- package/dist/lib/connection_manager/connection_manager.js.map +1 -1
- package/dist/lib/filter/filter.d.ts +8 -5
- package/dist/lib/filter/filter.js +30 -10
- package/dist/lib/filter/filter.js.map +1 -1
- package/dist/lib/light_push/light_push.d.ts +4 -3
- package/dist/lib/light_push/light_push.js +6 -4
- package/dist/lib/light_push/light_push.js.map +1 -1
- package/dist/lib/message/version_0.d.ts +3 -4
- package/dist/lib/message/version_0.js +1 -4
- package/dist/lib/message/version_0.js.map +1 -1
- package/dist/lib/message_hash/index.d.ts +1 -0
- package/dist/lib/message_hash/index.js +2 -0
- package/dist/lib/message_hash/index.js.map +1 -0
- package/dist/lib/message_hash/message_hash.d.ts +52 -0
- package/dist/lib/message_hash/message_hash.js +84 -0
- package/dist/lib/message_hash/message_hash.js.map +1 -0
- package/dist/lib/metadata/metadata.js +6 -4
- package/dist/lib/metadata/metadata.js.map +1 -1
- package/dist/lib/store/rpc.js +16 -10
- package/dist/lib/store/rpc.js.map +1 -1
- package/dist/lib/store/store.d.ts +4 -3
- package/dist/lib/store/store.js +18 -6
- package/dist/lib/store/store.js.map +1 -1
- package/dist/lib/stream_manager/stream_manager.d.ts +3 -4
- package/dist/lib/stream_manager/stream_manager.js +6 -8
- package/dist/lib/stream_manager/stream_manager.js.map +1 -1
- package/package.json +125 -1
- package/src/index.ts +2 -0
- package/src/lib/connection_manager/connection_manager.ts +24 -16
- package/src/lib/filter/filter.ts +46 -14
- package/src/lib/light_push/light_push.ts +8 -5
- package/src/lib/message/version_0.ts +3 -7
- package/src/lib/message_hash/index.ts +1 -0
- package/src/lib/message_hash/message_hash.ts +106 -0
- package/src/lib/metadata/metadata.ts +8 -5
- package/src/lib/store/rpc.ts +23 -19
- package/src/lib/store/store.ts +21 -6
- package/src/lib/stream_manager/stream_manager.ts +8 -6
- package/bundle/base_protocol-DvQrudwy.js +0 -152
- package/bundle/index-CTo1my9M.js +0 -1543
- package/bundle/lib/base_protocol.js +0 -2
- package/dist/lib/base_protocol.d.ts +0 -18
- package/dist/lib/base_protocol.js +0 -25
- package/dist/lib/base_protocol.js.map +0 -1
- package/src/lib/base_protocol.ts +0 -44
@@ -1,13 +1,12 @@
|
|
1
1
|
import type { PeerId, Stream } from "@libp2p/interface";
|
2
|
-
import type {
|
2
|
+
import type { Libp2pComponents } from "@waku/interfaces";
|
3
3
|
export declare class StreamManager {
|
4
4
|
private multicodec;
|
5
|
-
private
|
6
|
-
private addEventListener;
|
5
|
+
private readonly libp2p;
|
7
6
|
private readonly log;
|
8
7
|
private ongoingCreation;
|
9
8
|
private streamPool;
|
10
|
-
constructor(multicodec: string,
|
9
|
+
constructor(multicodec: string, libp2p: Libp2pComponents);
|
11
10
|
getStream(peerId: PeerId): Promise<Stream>;
|
12
11
|
private createStream;
|
13
12
|
private createStreamWithLock;
|
@@ -3,17 +3,15 @@ import { selectOpenConnection } from "./utils.js";
|
|
3
3
|
const STREAM_LOCK_KEY = "consumed";
|
4
4
|
export class StreamManager {
|
5
5
|
multicodec;
|
6
|
-
|
7
|
-
addEventListener;
|
6
|
+
libp2p;
|
8
7
|
log;
|
9
8
|
ongoingCreation = new Set();
|
10
9
|
streamPool = new Map();
|
11
|
-
constructor(multicodec,
|
10
|
+
constructor(multicodec, libp2p) {
|
12
11
|
this.multicodec = multicodec;
|
13
|
-
this.
|
14
|
-
this.addEventListener = addEventListener;
|
12
|
+
this.libp2p = libp2p;
|
15
13
|
this.log = new Logger(`stream-manager:${multicodec}`);
|
16
|
-
this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
|
14
|
+
this.libp2p.events.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
|
17
15
|
}
|
18
16
|
async getStream(peerId) {
|
19
17
|
const peerIdStr = peerId.toString();
|
@@ -33,7 +31,7 @@ export class StreamManager {
|
|
33
31
|
return stream;
|
34
32
|
}
|
35
33
|
async createStream(peerId, retries = 0) {
|
36
|
-
const connections = this.getConnections(peerId);
|
34
|
+
const connections = this.libp2p.connectionManager.getConnections(peerId);
|
37
35
|
const connection = selectOpenConnection(connections);
|
38
36
|
if (!connection) {
|
39
37
|
throw new Error(`Failed to get a connection to the peer peerId=${peerId.toString()} multicodec=${this.multicodec}`);
|
@@ -94,7 +92,7 @@ export class StreamManager {
|
|
94
92
|
this.streamPool.set(peer.id.toString(), this.createStreamWithLock(peer));
|
95
93
|
}
|
96
94
|
getOpenStreamForCodec(peerId) {
|
97
|
-
const connections = this.getConnections(peerId);
|
95
|
+
const connections = this.libp2p.connectionManager.getConnections(peerId);
|
98
96
|
const connection = selectOpenConnection(connections);
|
99
97
|
if (!connection) {
|
100
98
|
return;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"stream_manager.js","sourceRoot":"","sources":["../../../src/lib/stream_manager/stream_manager.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAElD,MAAM,eAAe,GAAG,UAAU,CAAC;AAEnC,MAAM,OAAO,aAAa;IAOd;
|
1
|
+
{"version":3,"file":"stream_manager.js","sourceRoot":"","sources":["../../../src/lib/stream_manager/stream_manager.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AA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AC,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAC;IAC5C,CAAC;CACF"}
|
package/package.json
CHANGED
@@ -1 +1,125 @@
|
|
1
|
-
{
|
1
|
+
{
|
2
|
+
"name": "@waku/core",
|
3
|
+
"version": "0.0.36",
|
4
|
+
"description": "TypeScript implementation of the Waku v2 protocol",
|
5
|
+
"types": "./dist/index.d.ts",
|
6
|
+
"module": "./dist/index.js",
|
7
|
+
"exports": {
|
8
|
+
".": {
|
9
|
+
"types": "./dist/index.d.ts",
|
10
|
+
"import": "./dist/index.js"
|
11
|
+
},
|
12
|
+
"./lib/message/version_0": {
|
13
|
+
"types": "./dist/lib/message/version_0.d.ts",
|
14
|
+
"import": "./dist/lib/message/version_0.js"
|
15
|
+
}
|
16
|
+
},
|
17
|
+
"typesVersions": {
|
18
|
+
"*": {
|
19
|
+
"lib/*": [
|
20
|
+
"dist/lib/*"
|
21
|
+
],
|
22
|
+
"constants/*": [
|
23
|
+
"dist/constants/*"
|
24
|
+
]
|
25
|
+
}
|
26
|
+
},
|
27
|
+
"type": "module",
|
28
|
+
"homepage": "https://github.com/waku-org/js-waku/tree/master/packages/core#readme",
|
29
|
+
"repository": {
|
30
|
+
"type": "git",
|
31
|
+
"url": "https://github.com/waku-org/js-waku.git"
|
32
|
+
},
|
33
|
+
"bugs": {
|
34
|
+
"url": "https://github.com/waku-org/js-waku/issues"
|
35
|
+
},
|
36
|
+
"license": "MIT OR Apache-2.0",
|
37
|
+
"keywords": [
|
38
|
+
"waku",
|
39
|
+
"decentralised",
|
40
|
+
"communication",
|
41
|
+
"web3",
|
42
|
+
"ethereum",
|
43
|
+
"dapps"
|
44
|
+
],
|
45
|
+
"scripts": {
|
46
|
+
"build": "run-s build:**",
|
47
|
+
"build:esm": "tsc",
|
48
|
+
"build:bundle": "rollup --config rollup.config.js",
|
49
|
+
"fix": "run-s fix:*",
|
50
|
+
"fix:lint": "eslint src *.js --fix",
|
51
|
+
"check": "run-s check:*",
|
52
|
+
"check:tsc": "tsc -p tsconfig.dev.json",
|
53
|
+
"check:lint": "eslint src *.js",
|
54
|
+
"check:spelling": "cspell \"{README.md,src/**/*.ts}\"",
|
55
|
+
"test": "NODE_ENV=test run-s test:*",
|
56
|
+
"test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha",
|
57
|
+
"test:browser": "NODE_ENV=test karma start karma.conf.cjs",
|
58
|
+
"watch:build": "tsc -p tsconfig.json -w",
|
59
|
+
"watch:test": "mocha --watch",
|
60
|
+
"prepublish": "npm run build",
|
61
|
+
"reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build"
|
62
|
+
},
|
63
|
+
"engines": {
|
64
|
+
"node": ">=22"
|
65
|
+
},
|
66
|
+
"dependencies": {
|
67
|
+
"@waku/enr": "^0.0.30",
|
68
|
+
"@waku/interfaces": "0.0.31",
|
69
|
+
"@libp2p/ping": "2.0.35",
|
70
|
+
"@waku/proto": "0.0.11",
|
71
|
+
"@waku/utils": "0.0.24",
|
72
|
+
"debug": "^4.3.4",
|
73
|
+
"@noble/hashes": "^1.3.2",
|
74
|
+
"it-all": "^3.0.4",
|
75
|
+
"it-length-prefixed": "^9.0.4",
|
76
|
+
"it-pipe": "^3.0.1",
|
77
|
+
"uint8arraylist": "^2.4.3",
|
78
|
+
"uuid": "^9.0.0"
|
79
|
+
},
|
80
|
+
"devDependencies": {
|
81
|
+
"@libp2p/peer-id": "5.1.7",
|
82
|
+
"@libp2p/interface": "2.10.4",
|
83
|
+
"@multiformats/multiaddr": "^12.0.0",
|
84
|
+
"@rollup/plugin-commonjs": "^25.0.7",
|
85
|
+
"@rollup/plugin-json": "^6.0.0",
|
86
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
87
|
+
"@types/chai": "^4.3.11",
|
88
|
+
"@types/debug": "^4.1.12",
|
89
|
+
"@types/mocha": "^10.0.6",
|
90
|
+
"@types/uuid": "^9.0.8",
|
91
|
+
"@waku/build-utils": "*",
|
92
|
+
"chai": "^4.3.10",
|
93
|
+
"sinon": "^18.0.0",
|
94
|
+
"cspell": "^8.6.1",
|
95
|
+
"fast-check": "^3.19.0",
|
96
|
+
"ignore-loader": "^0.1.2",
|
97
|
+
"isomorphic-fetch": "^3.0.0",
|
98
|
+
"mocha": "^10.3.0",
|
99
|
+
"npm-run-all": "^4.1.5",
|
100
|
+
"process": "^0.11.10",
|
101
|
+
"rollup": "^4.12.0"
|
102
|
+
},
|
103
|
+
"peerDependencies": {
|
104
|
+
"@multiformats/multiaddr": "^12.0.0",
|
105
|
+
"libp2p": "2.8.11"
|
106
|
+
},
|
107
|
+
"peerDependenciesMeta": {
|
108
|
+
"@multiformats/multiaddr": {
|
109
|
+
"optional": true
|
110
|
+
},
|
111
|
+
"libp2p": {
|
112
|
+
"optional": true
|
113
|
+
}
|
114
|
+
},
|
115
|
+
"files": [
|
116
|
+
"dist",
|
117
|
+
"bundle",
|
118
|
+
"src/**/*.ts",
|
119
|
+
"!**/*.spec.*",
|
120
|
+
"!**/*.json",
|
121
|
+
"CHANGELOG.md",
|
122
|
+
"LICENSE",
|
123
|
+
"README.md"
|
124
|
+
]
|
125
|
+
}
|
package/src/index.ts
CHANGED
@@ -20,3 +20,5 @@ export { ConnectionManager } from "./lib/connection_manager/index.js";
|
|
20
20
|
export { StreamManager } from "./lib/stream_manager/index.js";
|
21
21
|
|
22
22
|
export { MetadataCodec, wakuMetadata } from "./lib/metadata/index.js";
|
23
|
+
|
24
|
+
export { messageHash, messageHashStr } from "./lib/message_hash/index.js";
|
@@ -4,7 +4,6 @@ import {
|
|
4
4
|
type Peer,
|
5
5
|
type PeerId,
|
6
6
|
type PeerInfo,
|
7
|
-
type PeerStore,
|
8
7
|
type Stream,
|
9
8
|
TypedEventEmitter
|
10
9
|
} from "@libp2p/interface";
|
@@ -574,12 +573,9 @@ export class ConnectionManager
|
|
574
573
|
return false;
|
575
574
|
}
|
576
575
|
|
577
|
-
const isSameShard = await this.
|
576
|
+
const isSameShard = await this.isPeerOnSameShard(peerId);
|
578
577
|
if (!isSameShard) {
|
579
|
-
const shardInfo = await this.getPeerShardInfo(
|
580
|
-
peerId,
|
581
|
-
this.libp2p.peerStore
|
582
|
-
);
|
578
|
+
const shardInfo = await this.getPeerShardInfo(peerId);
|
583
579
|
|
584
580
|
log.warn(
|
585
581
|
`Discovered peer ${peerId.toString()} with ShardInfo ${shardInfo} is not part of any of the configured pubsub topics (${
|
@@ -666,28 +662,40 @@ export class ConnectionManager
|
|
666
662
|
}
|
667
663
|
}
|
668
664
|
|
669
|
-
|
670
|
-
const shardInfo = await this.getPeerShardInfo(
|
671
|
-
peerId,
|
672
|
-
this.libp2p.peerStore
|
673
|
-
);
|
665
|
+
public async isPeerOnSameShard(peerId: PeerId): Promise<boolean> {
|
666
|
+
const shardInfo = await this.getPeerShardInfo(peerId);
|
674
667
|
|
675
|
-
|
676
|
-
|
668
|
+
if (!shardInfo) {
|
669
|
+
return true;
|
670
|
+
}
|
677
671
|
|
678
672
|
const pubsubTopics = shardInfoToPubsubTopics(shardInfo);
|
679
673
|
|
680
674
|
const isTopicConfigured = pubsubTopics.some((topic) =>
|
681
675
|
this.pubsubTopics.includes(topic)
|
682
676
|
);
|
677
|
+
|
683
678
|
return isTopicConfigured;
|
684
679
|
}
|
685
680
|
|
686
|
-
|
681
|
+
public async isPeerOnPubsubTopic(
|
687
682
|
peerId: PeerId,
|
688
|
-
|
683
|
+
pubsubTopic: string
|
684
|
+
): Promise<boolean> {
|
685
|
+
const shardInfo = await this.getPeerShardInfo(peerId);
|
686
|
+
|
687
|
+
if (!shardInfo) {
|
688
|
+
return true;
|
689
|
+
}
|
690
|
+
|
691
|
+
const pubsubTopics = shardInfoToPubsubTopics(shardInfo);
|
692
|
+
return pubsubTopics.some((t) => t === pubsubTopic);
|
693
|
+
}
|
694
|
+
|
695
|
+
private async getPeerShardInfo(
|
696
|
+
peerId: PeerId
|
689
697
|
): Promise<ShardInfo | undefined> {
|
690
|
-
const peer = await peerStore.get(peerId);
|
698
|
+
const peer = await this.libp2p.peerStore.get(peerId);
|
691
699
|
const shardInfoBytes = peer.metadata.get("shardInfo");
|
692
700
|
if (!shardInfoBytes) return undefined;
|
693
701
|
return decodeRelayShard(shardInfoBytes);
|
package/src/lib/filter/filter.ts
CHANGED
@@ -3,7 +3,6 @@ import type { IncomingStreamData } from "@libp2p/interface-internal";
|
|
3
3
|
import {
|
4
4
|
type ContentTopic,
|
5
5
|
type CoreProtocolResult,
|
6
|
-
type IBaseProtocolCore,
|
7
6
|
type Libp2p,
|
8
7
|
ProtocolError,
|
9
8
|
type PubsubTopic
|
@@ -15,7 +14,7 @@ import * as lp from "it-length-prefixed";
|
|
15
14
|
import { pipe } from "it-pipe";
|
16
15
|
import { Uint8ArrayList } from "uint8arraylist";
|
17
16
|
|
18
|
-
import {
|
17
|
+
import { StreamManager } from "../stream_manager/index.js";
|
19
18
|
|
20
19
|
import {
|
21
20
|
FilterPushRpc,
|
@@ -30,17 +29,50 @@ export const FilterCodecs = {
|
|
30
29
|
PUSH: "/vac/waku/filter-push/2.0.0-beta1"
|
31
30
|
};
|
32
31
|
|
33
|
-
|
32
|
+
type IncomingMessageHandler = (
|
33
|
+
pubsubTopic: PubsubTopic,
|
34
|
+
wakuMessage: WakuMessage,
|
35
|
+
peerIdStr: string
|
36
|
+
) => Promise<void>;
|
37
|
+
|
38
|
+
export class FilterCore {
|
39
|
+
private streamManager: StreamManager;
|
40
|
+
private static handleIncomingMessage?: IncomingMessageHandler;
|
41
|
+
|
42
|
+
public readonly multicodec = FilterCodecs.SUBSCRIBE;
|
43
|
+
|
34
44
|
public constructor(
|
35
|
-
|
36
|
-
pubsubTopic: PubsubTopic,
|
37
|
-
wakuMessage: WakuMessage,
|
38
|
-
peerIdStr: string
|
39
|
-
) => Promise<void>,
|
45
|
+
handleIncomingMessage: IncomingMessageHandler,
|
40
46
|
public readonly pubsubTopics: PubsubTopic[],
|
41
47
|
libp2p: Libp2p
|
42
48
|
) {
|
43
|
-
|
49
|
+
this.streamManager = new StreamManager(
|
50
|
+
FilterCodecs.SUBSCRIBE,
|
51
|
+
libp2p.components
|
52
|
+
);
|
53
|
+
|
54
|
+
// TODO(weboko): remove when @waku/sdk 0.0.33 is released
|
55
|
+
const prevHandler = FilterCore.handleIncomingMessage;
|
56
|
+
FilterCore.handleIncomingMessage = !prevHandler
|
57
|
+
? handleIncomingMessage
|
58
|
+
: async (pubsubTopic, message, peerIdStr): Promise<void> => {
|
59
|
+
try {
|
60
|
+
await prevHandler(pubsubTopic, message, peerIdStr);
|
61
|
+
} catch (e) {
|
62
|
+
log.error(
|
63
|
+
"Previous FilterCore incoming message handler failed ",
|
64
|
+
e
|
65
|
+
);
|
66
|
+
}
|
67
|
+
|
68
|
+
try {
|
69
|
+
await handleIncomingMessage(pubsubTopic, message, peerIdStr);
|
70
|
+
} catch (e) {
|
71
|
+
log.error("Present FilterCore incoming message handler failed ", e);
|
72
|
+
}
|
73
|
+
|
74
|
+
return;
|
75
|
+
};
|
44
76
|
|
45
77
|
libp2p
|
46
78
|
.handle(FilterCodecs.PUSH, this.onRequest.bind(this), {
|
@@ -56,7 +88,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
56
88
|
peerId: PeerId,
|
57
89
|
contentTopics: ContentTopic[]
|
58
90
|
): Promise<CoreProtocolResult> {
|
59
|
-
const stream = await this.getStream(peerId);
|
91
|
+
const stream = await this.streamManager.getStream(peerId);
|
60
92
|
|
61
93
|
const request = FilterSubscribeRpc.createSubscribeRequest(
|
62
94
|
pubsubTopic,
|
@@ -112,7 +144,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
112
144
|
): Promise<CoreProtocolResult> {
|
113
145
|
let stream: Stream | undefined;
|
114
146
|
try {
|
115
|
-
stream = await this.getStream(peerId);
|
147
|
+
stream = await this.streamManager.getStream(peerId);
|
116
148
|
} catch (error) {
|
117
149
|
log.error(
|
118
150
|
`Failed to get a stream for remote peer${peerId.toString()}`,
|
@@ -155,7 +187,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
155
187
|
pubsubTopic: PubsubTopic,
|
156
188
|
peerId: PeerId
|
157
189
|
): Promise<CoreProtocolResult> {
|
158
|
-
const stream = await this.getStream(peerId);
|
190
|
+
const stream = await this.streamManager.getStream(peerId);
|
159
191
|
|
160
192
|
const request = FilterSubscribeRpc.createUnsubscribeAllRequest(pubsubTopic);
|
161
193
|
|
@@ -202,7 +234,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
202
234
|
public async ping(peerId: PeerId): Promise<CoreProtocolResult> {
|
203
235
|
let stream: Stream | undefined;
|
204
236
|
try {
|
205
|
-
stream = await this.getStream(peerId);
|
237
|
+
stream = await this.streamManager.getStream(peerId);
|
206
238
|
} catch (error) {
|
207
239
|
log.error(
|
208
240
|
`Failed to get a stream for remote peer${peerId.toString()}`,
|
@@ -291,7 +323,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
291
323
|
return;
|
292
324
|
}
|
293
325
|
|
294
|
-
await
|
326
|
+
await FilterCore.handleIncomingMessage?.(
|
295
327
|
pubsubTopic,
|
296
328
|
wakuMessage,
|
297
329
|
connection.remotePeer.toString()
|
@@ -1,7 +1,6 @@
|
|
1
1
|
import type { PeerId, Stream } from "@libp2p/interface";
|
2
2
|
import {
|
3
3
|
type CoreProtocolResult,
|
4
|
-
type IBaseProtocolCore,
|
5
4
|
type IEncoder,
|
6
5
|
type IMessage,
|
7
6
|
type Libp2p,
|
@@ -17,7 +16,7 @@ import * as lp from "it-length-prefixed";
|
|
17
16
|
import { pipe } from "it-pipe";
|
18
17
|
import { Uint8ArrayList } from "uint8arraylist";
|
19
18
|
|
20
|
-
import {
|
19
|
+
import { StreamManager } from "../stream_manager/index.js";
|
21
20
|
|
22
21
|
import { PushRpc } from "./push_rpc.js";
|
23
22
|
import { isRLNResponseError } from "./utils.js";
|
@@ -32,12 +31,16 @@ type PreparePushMessageResult = ThisOrThat<"query", PushRpc>;
|
|
32
31
|
/**
|
33
32
|
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
34
33
|
*/
|
35
|
-
export class LightPushCore
|
34
|
+
export class LightPushCore {
|
35
|
+
private readonly streamManager: StreamManager;
|
36
|
+
|
37
|
+
public readonly multicodec = LightPushCodec;
|
38
|
+
|
36
39
|
public constructor(
|
37
40
|
public readonly pubsubTopics: PubsubTopic[],
|
38
41
|
libp2p: Libp2p
|
39
42
|
) {
|
40
|
-
|
43
|
+
this.streamManager = new StreamManager(LightPushCodec, libp2p.components);
|
41
44
|
}
|
42
45
|
|
43
46
|
private async preparePushMessage(
|
@@ -98,7 +101,7 @@ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
|
98
101
|
|
99
102
|
let stream: Stream;
|
100
103
|
try {
|
101
|
-
stream = await this.getStream(peerId);
|
104
|
+
stream = await this.streamManager.getStream(peerId);
|
102
105
|
} catch (error) {
|
103
106
|
log.error("Failed to get stream", error);
|
104
107
|
return {
|
@@ -22,7 +22,7 @@ export { proto };
|
|
22
22
|
export class DecodedMessage implements IDecodedMessage {
|
23
23
|
public constructor(
|
24
24
|
public pubsubTopic: string,
|
25
|
-
|
25
|
+
private proto: proto.WakuMessage
|
26
26
|
) {}
|
27
27
|
|
28
28
|
public get ephemeral(): boolean {
|
@@ -37,10 +37,6 @@ export class DecodedMessage implements IDecodedMessage {
|
|
37
37
|
return this.proto.contentTopic;
|
38
38
|
}
|
39
39
|
|
40
|
-
public get _rawTimestamp(): bigint | undefined {
|
41
|
-
return this.proto.timestamp;
|
42
|
-
}
|
43
|
-
|
44
40
|
public get timestamp(): Date | undefined {
|
45
41
|
// In the case we receive a value that is bigger than JS's max number,
|
46
42
|
// we catch the error and return undefined.
|
@@ -63,7 +59,7 @@ export class DecodedMessage implements IDecodedMessage {
|
|
63
59
|
public get version(): number {
|
64
60
|
// https://rfc.vac.dev/spec/14/
|
65
61
|
// > If omitted, the value SHOULD be interpreted as version 0.
|
66
|
-
return this.proto.version ??
|
62
|
+
return this.proto.version ?? Version;
|
67
63
|
}
|
68
64
|
|
69
65
|
public get rateLimitProof(): IRateLimitProof | undefined {
|
@@ -160,7 +156,7 @@ export class Decoder implements IDecoder<IDecodedMessage> {
|
|
160
156
|
public async fromProtoObj(
|
161
157
|
pubsubTopic: string,
|
162
158
|
proto: IProtoMessage
|
163
|
-
): Promise<
|
159
|
+
): Promise<IDecodedMessage | undefined> {
|
164
160
|
// https://rfc.vac.dev/spec/14/
|
165
161
|
// > If omitted, the value SHOULD be interpreted as version 0.
|
166
162
|
if (proto.version ?? 0 !== Version) {
|
@@ -0,0 +1 @@
|
|
1
|
+
export { messageHash, messageHashStr } from "./message_hash.js";
|
@@ -0,0 +1,106 @@
|
|
1
|
+
import { sha256 } from "@noble/hashes/sha256";
|
2
|
+
import type { IDecodedMessage, IProtoMessage } from "@waku/interfaces";
|
3
|
+
import { isDefined } from "@waku/utils";
|
4
|
+
import {
|
5
|
+
bytesToHex,
|
6
|
+
concat,
|
7
|
+
numberToBytes,
|
8
|
+
utf8ToBytes
|
9
|
+
} from "@waku/utils/bytes";
|
10
|
+
|
11
|
+
/**
|
12
|
+
* Deterministic Message Hashing as defined in
|
13
|
+
* [14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/#deterministic-message-hashing)
|
14
|
+
*
|
15
|
+
* Computes a SHA-256 hash of the concatenation of pubsub topic, payload, content topic, meta, and timestamp.
|
16
|
+
*
|
17
|
+
* @param pubsubTopic - The pubsub topic string
|
18
|
+
* @param message - The message to be hashed
|
19
|
+
* @returns A Uint8Array containing the SHA-256 hash
|
20
|
+
*
|
21
|
+
* @example
|
22
|
+
* ```typescript
|
23
|
+
* import { messageHash } from "@waku/core";
|
24
|
+
*
|
25
|
+
* const pubsubTopic = "/waku/2/default-waku/proto";
|
26
|
+
* const message = {
|
27
|
+
* payload: new Uint8Array([1, 2, 3, 4]),
|
28
|
+
* contentTopic: "/waku/2/default-content/proto",
|
29
|
+
* meta: new Uint8Array([5, 6, 7, 8]),
|
30
|
+
* timestamp: new Date()
|
31
|
+
* };
|
32
|
+
*
|
33
|
+
* const hash = messageHash(pubsubTopic, message);
|
34
|
+
* ```
|
35
|
+
*/
|
36
|
+
export function messageHash(
|
37
|
+
pubsubTopic: string,
|
38
|
+
message: IProtoMessage | IDecodedMessage
|
39
|
+
): Uint8Array {
|
40
|
+
const pubsubTopicBytes = utf8ToBytes(pubsubTopic);
|
41
|
+
const contentTopicBytes = utf8ToBytes(message.contentTopic);
|
42
|
+
const timestampBytes = tryConvertTimestampToBytes(message.timestamp);
|
43
|
+
|
44
|
+
const bytes = concat(
|
45
|
+
[
|
46
|
+
pubsubTopicBytes,
|
47
|
+
message.payload,
|
48
|
+
contentTopicBytes,
|
49
|
+
message.meta,
|
50
|
+
timestampBytes
|
51
|
+
].filter(isDefined)
|
52
|
+
);
|
53
|
+
|
54
|
+
return sha256(bytes);
|
55
|
+
}
|
56
|
+
|
57
|
+
function tryConvertTimestampToBytes(
|
58
|
+
timestamp: Date | number | bigint | undefined
|
59
|
+
): undefined | Uint8Array {
|
60
|
+
if (!timestamp) {
|
61
|
+
return;
|
62
|
+
}
|
63
|
+
|
64
|
+
let bigIntTimestamp: bigint;
|
65
|
+
|
66
|
+
if (typeof timestamp === "bigint") {
|
67
|
+
bigIntTimestamp = timestamp;
|
68
|
+
} else {
|
69
|
+
bigIntTimestamp = BigInt(timestamp.valueOf()) * 1000000n;
|
70
|
+
}
|
71
|
+
|
72
|
+
return numberToBytes(bigIntTimestamp);
|
73
|
+
}
|
74
|
+
|
75
|
+
/**
|
76
|
+
* Computes a deterministic message hash and returns it as a hexadecimal string.
|
77
|
+
* This is a convenience wrapper around messageHash that converts the result to a hex string.
|
78
|
+
*
|
79
|
+
* @param pubsubTopic - The pubsub topic string
|
80
|
+
* @param message - The message to be hashed
|
81
|
+
* @returns A string containing the hex representation of the SHA-256 hash
|
82
|
+
*
|
83
|
+
* @example
|
84
|
+
* ```typescript
|
85
|
+
* import { messageHashStr } from "@waku/core";
|
86
|
+
*
|
87
|
+
* const pubsubTopic = "/waku/2/default-waku/proto";
|
88
|
+
* const message = {
|
89
|
+
* payload: new Uint8Array([1, 2, 3, 4]),
|
90
|
+
* contentTopic: "/waku/2/default-content/proto",
|
91
|
+
* meta: new Uint8Array([5, 6, 7, 8]),
|
92
|
+
* timestamp: new Date()
|
93
|
+
* };
|
94
|
+
*
|
95
|
+
* const hashString = messageHashStr(pubsubTopic, message);
|
96
|
+
* console.log(hashString); // e.g. "a1b2c3d4..."
|
97
|
+
* ```
|
98
|
+
*/
|
99
|
+
export function messageHashStr(
|
100
|
+
pubsubTopic: string,
|
101
|
+
message: IProtoMessage | IDecodedMessage
|
102
|
+
): string {
|
103
|
+
const hash = messageHash(pubsubTopic, message);
|
104
|
+
const hashStr = bytesToHex(hash);
|
105
|
+
return hashStr;
|
106
|
+
}
|
@@ -16,21 +16,24 @@ import * as lp from "it-length-prefixed";
|
|
16
16
|
import { pipe } from "it-pipe";
|
17
17
|
import { Uint8ArrayList } from "uint8arraylist";
|
18
18
|
|
19
|
-
import {
|
19
|
+
import { StreamManager } from "../stream_manager/index.js";
|
20
20
|
|
21
21
|
const log = new Logger("metadata");
|
22
22
|
|
23
23
|
export const MetadataCodec = "/vac/waku/metadata/1.0.0";
|
24
24
|
|
25
|
-
class Metadata
|
26
|
-
private
|
25
|
+
class Metadata implements IMetadata {
|
26
|
+
private readonly streamManager: StreamManager;
|
27
|
+
private readonly libp2pComponents: Libp2pComponents;
|
27
28
|
protected handshakesConfirmed: Map<PeerIdStr, ShardInfo> = new Map();
|
28
29
|
|
30
|
+
public readonly multicodec = MetadataCodec;
|
31
|
+
|
29
32
|
public constructor(
|
30
33
|
public pubsubTopics: PubsubTopic[],
|
31
34
|
libp2p: Libp2pComponents
|
32
35
|
) {
|
33
|
-
|
36
|
+
this.streamManager = new StreamManager(MetadataCodec, libp2p);
|
34
37
|
this.libp2pComponents = libp2p;
|
35
38
|
void libp2p.registrar.handle(MetadataCodec, (streamData) => {
|
36
39
|
void this.onRequest(streamData);
|
@@ -55,7 +58,7 @@ class Metadata extends BaseProtocol implements IMetadata {
|
|
55
58
|
|
56
59
|
let stream;
|
57
60
|
try {
|
58
|
-
stream = await this.getStream(peerId);
|
61
|
+
stream = await this.streamManager.getStream(peerId);
|
59
62
|
} catch (error) {
|
60
63
|
log.error("Failed to get stream", error);
|
61
64
|
return {
|