@waku/interfaces 0.0.21 → 0.0.23-070b625.0

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.
@@ -1,6 +1,6 @@
1
1
  import type { Libp2p } from "@libp2p/interface";
2
- import type { PeerId } from "@libp2p/interface/peer-id";
3
- import type { Peer, PeerStore } from "@libp2p/interface/peer-store";
2
+ import type { PeerId } from "@libp2p/interface";
3
+ import type { Peer, PeerStore } from "@libp2p/interface";
4
4
  import type { ShardInfo } from "./enr.js";
5
5
  import type { CreateLibp2pOptions } from "./libp2p.js";
6
6
  import type { IDecodedMessage } from "./message.js";
@@ -11,13 +11,18 @@ export declare enum Protocols {
11
11
  LightPush = "lightpush",
12
12
  Filter = "filter"
13
13
  }
14
- export interface IBaseProtocol {
14
+ export type IBaseProtocolCore = {
15
+ shardInfo?: ShardInfo;
15
16
  multicodec: string;
16
17
  peerStore: PeerStore;
17
- peers: () => Promise<Peer[]>;
18
+ allPeers: () => Promise<Peer[]>;
19
+ connectedPeers: () => Promise<Peer[]>;
18
20
  addLibp2pEventListener: Libp2p["addEventListener"];
19
21
  removeLibp2pEventListener: Libp2p["removeEventListener"];
20
- }
22
+ };
23
+ export type IBaseProtocolSDK = {
24
+ numPeers: number;
25
+ };
21
26
  export type ContentTopicInfo = {
22
27
  clusterId: number;
23
28
  contentTopics: string[];
@@ -51,15 +56,23 @@ export type ProtocolCreateOptions = {
51
56
  * See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
52
57
  *
53
58
  */
54
- shardInfo?: ShardingParams;
59
+ shardInfo?: Partial<ShardingParams>;
55
60
  /**
56
- * You can pass options to the `Libp2p` instance used by {@link @waku/core!WakuNode} using the `libp2p` property.
61
+ * You can pass options to the `Libp2p` instance used by {@link @waku/sdk!WakuNode} using the `libp2p` property.
57
62
  * This property is the same type as the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
58
63
  * apart that we made the `modules` property optional and partial,
59
64
  * allowing its omission and letting Waku set good defaults.
60
- * Notes that some values are overridden by {@link @waku/core!WakuNode} to ensure it implements the Waku protocol.
65
+ * Notes that some values are overridden by {@link @waku/sdk!WakuNode} to ensure it implements the Waku protocol.
61
66
  */
62
67
  libp2p?: Partial<CreateLibp2pOptions>;
68
+ /**
69
+ * Number of peers to connect to, for the usage of the protocol.
70
+ * This is used by:
71
+ * - Light Push to send messages,
72
+ * - Filter to retrieve messages.
73
+ * Defaults to 3.
74
+ */
75
+ numPeersToUse?: number;
63
76
  /**
64
77
  * Byte array used as key for the noise protocol used for connection encryption
65
78
  * by [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
@@ -70,9 +83,22 @@ export type ProtocolCreateOptions = {
70
83
  * Use recommended bootstrap method to discovery and connect to new nodes.
71
84
  */
72
85
  defaultBootstrap?: boolean;
86
+ /**
87
+ * List of peers to use to bootstrap the node. Ignored if defaultBootstrap is set to true.
88
+ */
89
+ bootstrapPeers?: string[];
73
90
  };
74
91
  export type Callback<T extends IDecodedMessage> = (msg: T) => void | Promise<void>;
75
- export declare enum SendError {
92
+ export type ProtocolResult<SK extends string, SV, EK extends string = "error", EV = ProtocolError> = ({
93
+ [key in SK]: SV;
94
+ } & {
95
+ [key in EK]: null;
96
+ }) | ({
97
+ [key in SK]: null;
98
+ } & {
99
+ [key in EK]: EV;
100
+ });
101
+ export declare enum ProtocolError {
76
102
  /** Could not determine the origin of the fault. Best to check connectivity and try again */
77
103
  GENERIC_FAIL = "Generic error",
78
104
  /**
@@ -117,9 +143,18 @@ export declare enum SendError {
117
143
  * is logged. Review message validity, or mitigation for `NO_PEER_AVAILABLE`
118
144
  * or `DECODE_FAILED` can be used.
119
145
  */
120
- REMOTE_PEER_REJECTED = "Remote peer rejected"
146
+ REMOTE_PEER_REJECTED = "Remote peer rejected",
147
+ /**
148
+ * The protocol request timed out without a response. This may be due to a connection issue.
149
+ * Mitigation can be: retrying after a given time period
150
+ */
151
+ REQUEST_TIMEOUT = "Request timeout"
152
+ }
153
+ export interface Failure {
154
+ error: ProtocolError;
155
+ peerId?: PeerId;
121
156
  }
122
157
  export interface SendResult {
123
- errors?: SendError[];
124
- recipients: PeerId[];
158
+ failures?: Failure[];
159
+ successes: PeerId[];
125
160
  }
package/dist/protocols.js CHANGED
@@ -5,52 +5,57 @@ export var Protocols;
5
5
  Protocols["LightPush"] = "lightpush";
6
6
  Protocols["Filter"] = "filter";
7
7
  })(Protocols || (Protocols = {}));
8
- export var SendError;
9
- (function (SendError) {
8
+ export var ProtocolError;
9
+ (function (ProtocolError) {
10
10
  /** Could not determine the origin of the fault. Best to check connectivity and try again */
11
- SendError["GENERIC_FAIL"] = "Generic error";
11
+ ProtocolError["GENERIC_FAIL"] = "Generic error";
12
12
  /**
13
13
  * Failure to protobuf encode the message. This is not recoverable and needs
14
14
  * further investigation.
15
15
  */
16
- SendError["ENCODE_FAILED"] = "Failed to encode";
16
+ ProtocolError["ENCODE_FAILED"] = "Failed to encode";
17
17
  /**
18
18
  * Failure to protobuf decode the message. May be due to a remote peer issue,
19
19
  * ensuring that messages are sent via several peer enable mitigation of this error.
20
20
  */
21
- SendError["DECODE_FAILED"] = "Failed to decode";
21
+ ProtocolError["DECODE_FAILED"] = "Failed to decode";
22
22
  /**
23
23
  * The message payload is empty, making the message invalid. Ensure that a non-empty
24
24
  * payload is set on the outgoing message.
25
25
  */
26
- SendError["EMPTY_PAYLOAD"] = "Payload is empty";
26
+ ProtocolError["EMPTY_PAYLOAD"] = "Payload is empty";
27
27
  /**
28
28
  * The message size is above the maximum message size allowed on the Waku Network.
29
29
  * Compressing the message or using an alternative strategy for large messages is recommended.
30
30
  */
31
- SendError["SIZE_TOO_BIG"] = "Size is too big";
31
+ ProtocolError["SIZE_TOO_BIG"] = "Size is too big";
32
32
  /**
33
33
  * The PubsubTopic passed to the send function is not configured on the Waku node.
34
34
  * Please ensure that the PubsubTopic is used when initializing the Waku node.
35
35
  */
36
- SendError["TOPIC_NOT_CONFIGURED"] = "Topic not configured";
36
+ ProtocolError["TOPIC_NOT_CONFIGURED"] = "Topic not configured";
37
37
  /**
38
38
  * Failure to find a peer with suitable protocols. This may due to a connection issue.
39
39
  * Mitigation can be: retrying after a given time period, display connectivity issue
40
40
  * to user or listening for `peer:connected:bootstrap` or `peer:connected:peer-exchange`
41
41
  * on the connection manager before retrying.
42
42
  */
43
- SendError["NO_PEER_AVAILABLE"] = "No peer available";
43
+ ProtocolError["NO_PEER_AVAILABLE"] = "No peer available";
44
44
  /**
45
45
  * The remote peer did not behave as expected. Mitigation for `NO_PEER_AVAILABLE`
46
46
  * or `DECODE_FAILED` can be used.
47
47
  */
48
- SendError["REMOTE_PEER_FAULT"] = "Remote peer fault";
48
+ ProtocolError["REMOTE_PEER_FAULT"] = "Remote peer fault";
49
49
  /**
50
50
  * The remote peer rejected the message. Information provided by the remote peer
51
51
  * is logged. Review message validity, or mitigation for `NO_PEER_AVAILABLE`
52
52
  * or `DECODE_FAILED` can be used.
53
53
  */
54
- SendError["REMOTE_PEER_REJECTED"] = "Remote peer rejected";
55
- })(SendError || (SendError = {}));
54
+ ProtocolError["REMOTE_PEER_REJECTED"] = "Remote peer rejected";
55
+ /**
56
+ * The protocol request timed out without a response. This may be due to a connection issue.
57
+ * Mitigation can be: retrying after a given time period
58
+ */
59
+ ProtocolError["REQUEST_TIMEOUT"] = "Request timeout";
60
+ })(ProtocolError || (ProtocolError = {}));
56
61
  //# sourceMappingURL=protocols.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"protocols.js","sourceRoot":"","sources":["../src/protocols.ts"],"names":[],"mappings":"AASA,MAAM,CAAN,IAAY,SAKX;AALD,WAAY,SAAS;IACnB,4BAAe,CAAA;IACf,4BAAe,CAAA;IACf,oCAAuB,CAAA;IACvB,8BAAiB,CAAA;AACnB,CAAC,EALW,SAAS,KAAT,SAAS,QAKpB;AAuED,MAAM,CAAN,IAAY,SA8CX;AA9CD,WAAY,SAAS;IACnB,4FAA4F;IAC5F,2CAA8B,CAAA;IAC9B;;;OAGG;IACH,+CAAkC,CAAA;IAClC;;;OAGG;IACH,+CAAkC,CAAA;IAClC;;;OAGG;IACH,+CAAkC,CAAA;IAClC;;;OAGG;IACH,6CAAgC,CAAA;IAChC;;;OAGG;IACH,0DAA6C,CAAA;IAC7C;;;;;OAKG;IACH,oDAAuC,CAAA;IACvC;;;OAGG;IACH,oDAAuC,CAAA;IACvC;;;;OAIG;IACH,0DAA6C,CAAA;AAC/C,CAAC,EA9CW,SAAS,KAAT,SAAS,QA8CpB"}
1
+ {"version":3,"file":"protocols.js","sourceRoot":"","sources":["../src/protocols.ts"],"names":[],"mappings":"AASA,MAAM,CAAN,IAAY,SAKX;AALD,WAAY,SAAS;IACnB,4BAAe,CAAA;IACf,4BAAe,CAAA;IACf,oCAAuB,CAAA;IACvB,8BAAiB,CAAA;AACnB,CAAC,EALW,SAAS,KAAT,SAAS,QAKpB;AA8GD,MAAM,CAAN,IAAY,aAmDX;AAnDD,WAAY,aAAa;IACvB,4FAA4F;IAC5F,+CAA8B,CAAA;IAC9B;;;OAGG;IACH,mDAAkC,CAAA;IAClC;;;OAGG;IACH,mDAAkC,CAAA;IAClC;;;OAGG;IACH,mDAAkC,CAAA;IAClC;;;OAGG;IACH,iDAAgC,CAAA;IAChC;;;OAGG;IACH,8DAA6C,CAAA;IAC7C;;;;;OAKG;IACH,wDAAuC,CAAA;IACvC;;;OAGG;IACH,wDAAuC,CAAA;IACvC;;;;OAIG;IACH,8DAA6C,CAAA;IAC7C;;;OAGG;IACH,oDAAmC,CAAA;AACrC,CAAC,EAnDW,aAAa,KAAb,aAAa,QAmDxB"}
package/dist/store.d.ts CHANGED
@@ -1,5 +1,6 @@
1
+ import { proto_store as proto } from "@waku/proto";
1
2
  import type { IDecodedMessage, IDecoder } from "./message.js";
2
- import type { IBaseProtocol } from "./protocols.js";
3
+ import type { IBaseProtocolCore, IBaseProtocolSDK } from "./protocols.js";
3
4
  export declare enum PageDirection {
4
5
  BACKWARD = "backward",
5
6
  FORWARD = "forward"
@@ -38,10 +39,13 @@ export type StoreQueryOptions = {
38
39
  * Cursor as an index to start a query from. Must be generated from a Waku
39
40
  * Message.
40
41
  */
41
- cursor?: Cursor;
42
+ cursor?: proto.Index;
42
43
  };
43
- export interface IStore extends IBaseProtocol {
44
+ export type IStoreCore = IBaseProtocolCore;
45
+ export type IStoreSDK = IBaseProtocolSDK & {
46
+ protocol: IBaseProtocolCore;
47
+ createCursor(message: IDecodedMessage): Cursor;
48
+ queryGenerator: <T extends IDecodedMessage>(decoders: IDecoder<T>[], options?: StoreQueryOptions) => AsyncGenerator<Promise<T | undefined>[]>;
44
49
  queryWithOrderedCallback: <T extends IDecodedMessage>(decoders: IDecoder<T>[], callback: (message: T) => Promise<void | boolean> | boolean | void, options?: StoreQueryOptions) => Promise<void>;
45
50
  queryWithPromiseCallback: <T extends IDecodedMessage>(decoders: IDecoder<T>[], callback: (message: Promise<T | undefined>) => Promise<void | boolean> | boolean | void, options?: StoreQueryOptions) => Promise<void>;
46
- queryGenerator: <T extends IDecodedMessage>(decoders: IDecoder<T>[], options?: StoreQueryOptions) => AsyncGenerator<Promise<T | undefined>[]>;
47
- }
51
+ };
package/dist/store.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAGA,MAAM,CAAN,IAAY,aAGX;AAHD,WAAY,aAAa;IACvB,sCAAqB,CAAA;IACrB,oCAAmB,CAAA;AACrB,CAAC,EAHW,aAAa,KAAb,aAAa,QAGxB"}
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAKA,MAAM,CAAN,IAAY,aAGX;AAHD,WAAY,aAAa;IACvB,sCAAqB,CAAA;IACrB,oCAAmB,CAAA;AACrB,CAAC,EAHW,aAAa,KAAb,aAAa,QAGxB"}
package/dist/waku.d.ts CHANGED
@@ -1,19 +1,18 @@
1
- import type { Stream } from "@libp2p/interface/connection";
2
- import type { PeerId } from "@libp2p/interface/peer-id";
1
+ import type { PeerId, Stream } from "@libp2p/interface";
3
2
  import type { Multiaddr } from "@multiformats/multiaddr";
4
3
  import { IConnectionManager } from "./connection_manager.js";
5
4
  import type { IFilter } from "./filter.js";
6
5
  import type { Libp2p } from "./libp2p.js";
7
- import type { ILightPush } from "./light_push.js";
6
+ import type { ILightPushSDK } from "./light_push.js";
8
7
  import { Protocols } from "./protocols.js";
9
8
  import type { IRelay } from "./relay.js";
10
- import type { IStore } from "./store.js";
9
+ import type { IStoreSDK } from "./store.js";
11
10
  export interface Waku {
12
11
  libp2p: Libp2p;
13
12
  relay?: IRelay;
14
- store?: IStore;
13
+ store?: IStoreSDK;
15
14
  filter?: IFilter;
16
- lightPush?: ILightPush;
15
+ lightPush?: ILightPushSDK;
17
16
  connectionManager: IConnectionManager;
18
17
  dial(peer: PeerId | Multiaddr, protocols?: Protocols[]): Promise<Stream>;
19
18
  start(): Promise<void>;
@@ -23,9 +22,9 @@ export interface Waku {
23
22
  }
24
23
  export interface LightNode extends Waku {
25
24
  relay: undefined;
26
- store: IStore;
25
+ store: IStoreSDK;
27
26
  filter: IFilter;
28
- lightPush: ILightPush;
27
+ lightPush: ILightPushSDK;
29
28
  }
30
29
  export interface RelayNode extends Waku {
31
30
  relay: IRelay;
@@ -35,7 +34,7 @@ export interface RelayNode extends Waku {
35
34
  }
36
35
  export interface FullNode extends Waku {
37
36
  relay: IRelay;
38
- store: IStore;
37
+ store: IStoreSDK;
39
38
  filter: IFilter;
40
- lightPush: ILightPush;
39
+ lightPush: ILightPushSDK;
41
40
  }
package/package.json CHANGED
@@ -1,66 +1 @@
1
- {
2
- "name": "@waku/interfaces",
3
- "version": "0.0.21",
4
- "description": "Definition of Waku interfaces",
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
- },
13
- "type": "module",
14
- "author": "Waku Team",
15
- "homepage": "https://github.com/waku-org/js-waku/tree/master/packages/interfaces#readme",
16
- "repository": {
17
- "type": "git",
18
- "url": "https://github.com/waku-org/js-waku.git"
19
- },
20
- "bugs": {
21
- "url": "https://github.com/waku-org/js-waku/issues"
22
- },
23
- "license": "MIT OR Apache-2.0",
24
- "keywords": [
25
- "waku",
26
- "decentralized",
27
- "secure",
28
- "communication",
29
- "web3",
30
- "ethereum",
31
- "dapps",
32
- "privacy"
33
- ],
34
- "scripts": {
35
- "build": "run-s build:**",
36
- "build:esm": "tsc",
37
- "fix": "run-s fix:*",
38
- "fix:lint": "eslint src --fix",
39
- "check": "run-s check:*",
40
- "check:lint": "eslint src",
41
- "check:spelling": "cspell \"{README.md,src/**/*.ts}\"",
42
- "check:tsc": "tsc -p tsconfig.dev.json",
43
- "prepublish": "npm run build",
44
- "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build"
45
- },
46
- "engines": {
47
- "node": ">=18"
48
- },
49
- "devDependencies": {
50
- "@chainsafe/libp2p-gossipsub": "^10.1.1",
51
- "@multiformats/multiaddr": "^12.0.0",
52
- "cspell": "^7.3.2",
53
- "npm-run-all": "^4.1.5",
54
- "libp2p": "^0.46.14"
55
- },
56
- "files": [
57
- "dist",
58
- "bundle",
59
- "src/**/*.ts",
60
- "!**/*.spec.*",
61
- "!**/*.json",
62
- "CHANGELOG.md",
63
- "LICENSE",
64
- "README.md"
65
- ]
66
- }
1
+ {"name":"@waku/interfaces","version":"0.0.23-070b625.0","description":"Definition of Waku interfaces","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"}},"type":"module","author":"Waku Team","homepage":"https://github.com/waku-org/js-waku/tree/master/packages/interfaces#readme","repository":{"type":"git","url":"https://github.com/waku-org/js-waku.git"},"bugs":{"url":"https://github.com/waku-org/js-waku/issues"},"license":"MIT OR Apache-2.0","keywords":["waku","decentralized","secure","communication","web3","ethereum","dapps","privacy"],"scripts":{"build":"run-s build:**","build:esm":"tsc","fix":"run-s fix:*","fix:lint":"eslint src --fix","check":"run-s check:*","check:lint":"eslint src","check:spelling":"cspell \"{README.md,src/**/*.ts}\"","check:tsc":"tsc -p tsconfig.dev.json","prepublish":"npm run build","reset-hard":"git clean -dfx -e .idea && git reset --hard && npm i && npm run build"},"engines":{"node":">=18"},"devDependencies":{"@chainsafe/libp2p-gossipsub":"^12.0.0","@multiformats/multiaddr":"^12.0.0","cspell":"^8.6.1","npm-run-all":"^4.1.5","libp2p":"^1.1.2"},"files":["dist","bundle","src/**/*.ts","!**/*.spec.*","!**/*.json","CHANGELOG.md","LICENSE","README.md"],"dependencies":{"@waku/proto":"0.0.7-070b625.0"}}
@@ -1,10 +1,9 @@
1
- import type { PeerId } from "@libp2p/interface/peer-id";
2
- import type { Peer } from "@libp2p/interface/peer-store";
3
- import type { EventEmitter } from "@libp2p/interfaces/events";
1
+ import type { Peer, PeerId, TypedEventEmitter } from "@libp2p/interface";
4
2
 
5
3
  export enum Tags {
6
4
  BOOTSTRAP = "bootstrap",
7
- PEER_EXCHANGE = "peer-exchange"
5
+ PEER_EXCHANGE = "peer-exchange",
6
+ LOCAL = "local-peer-cache"
8
7
  }
9
8
 
10
9
  export interface ConnectionManagerOptions {
@@ -42,10 +41,12 @@ export interface PeersByDiscoveryResult {
42
41
  DISCOVERED: {
43
42
  [Tags.BOOTSTRAP]: Peer[];
44
43
  [Tags.PEER_EXCHANGE]: Peer[];
44
+ [Tags.LOCAL]: Peer[];
45
45
  };
46
46
  CONNECTED: {
47
47
  [Tags.BOOTSTRAP]: Peer[];
48
48
  [Tags.PEER_EXCHANGE]: Peer[];
49
+ [Tags.LOCAL]: Peer[];
49
50
  };
50
51
  }
51
52
 
@@ -59,7 +60,7 @@ export interface IConnectionStateEvents {
59
60
  }
60
61
 
61
62
  export interface IConnectionManager
62
- extends EventEmitter<IPeersByDiscoveryEvents & IConnectionStateEvents> {
63
+ extends TypedEventEmitter<IPeersByDiscoveryEvents & IConnectionStateEvents> {
63
64
  getPeersByDiscovery(): Promise<PeersByDiscoveryResult>;
64
65
  stop(): void;
65
66
  }
package/src/constants.ts CHANGED
@@ -2,3 +2,8 @@
2
2
  * DefaultPubsubTopic is the default gossipsub topic to use for Waku.
3
3
  */
4
4
  export const DefaultPubsubTopic = "/waku/2/default-waku/proto";
5
+
6
+ /**
7
+ * The default cluster ID for The Waku Network
8
+ */
9
+ export const DEFAULT_CLUSTER_ID = 1;
@@ -1,4 +1,4 @@
1
- import { PeerStore } from "@libp2p/interface/peer-store";
1
+ import { PeerStore } from "@libp2p/interface";
2
2
 
3
3
  export type SearchContext = {
4
4
  domain: string;
package/src/enr.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { PeerId } from "@libp2p/interface/peer-id";
2
- import type { PeerInfo } from "@libp2p/interface/peer-info";
1
+ import type { PeerId } from "@libp2p/interface";
2
+ import type { PeerInfo } from "@libp2p/interface";
3
3
  import type { Multiaddr } from "@multiformats/multiaddr";
4
4
 
5
5
  export type ENRKey = string;
package/src/filter.ts CHANGED
@@ -1,8 +1,8 @@
1
- import type { PeerId } from "@libp2p/interface/peer-id";
1
+ import type { PeerId } from "@libp2p/interface";
2
2
 
3
3
  import type { IDecodedMessage, IDecoder, SingleShardInfo } from "./message.js";
4
4
  import type { ContentTopic, PubsubTopic } from "./misc.js";
5
- import type { Callback, IBaseProtocol } from "./protocols.js";
5
+ import type { Callback, IBaseProtocolCore } from "./protocols.js";
6
6
  import type { IReceiver } from "./receiver.js";
7
7
 
8
8
  export type ContentFilter = {
@@ -23,7 +23,7 @@ export interface IFilterSubscription {
23
23
  }
24
24
 
25
25
  export type IFilter = IReceiver &
26
- IBaseProtocol & {
26
+ IBaseProtocolCore & {
27
27
  createSubscription(
28
28
  pubsubTopicShardInfo?: SingleShardInfo | PubsubTopic,
29
29
  peerId?: PeerId
package/src/index.ts CHANGED
@@ -16,3 +16,4 @@ export * from "./keep_alive_manager.js";
16
16
  export * from "./dns_discovery.js";
17
17
  export * from "./metadata.js";
18
18
  export * from "./constants.js";
19
+ export * from "./local_storage.js";
package/src/libp2p.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import type { GossipSub } from "@chainsafe/libp2p-gossipsub";
2
+ import type { identify } from "@libp2p/identify";
2
3
  import type { Libp2p as BaseLibp2p } from "@libp2p/interface";
4
+ import type { PingService } from "@libp2p/ping";
3
5
  import type { Libp2pInit, Libp2pOptions } from "libp2p";
4
- import type { identifyService } from "libp2p/identify";
5
- import type { PingService } from "libp2p/ping";
6
6
 
7
7
  import { IMetadata } from "./metadata";
8
8
 
@@ -10,7 +10,7 @@ export type Libp2pServices = {
10
10
  ping: PingService;
11
11
  metadata?: IMetadata;
12
12
  pubsub?: GossipSub;
13
- identify: ReturnType<ReturnType<typeof identifyService>>;
13
+ identify: ReturnType<ReturnType<typeof identify>>;
14
14
  };
15
15
 
16
16
  // TODO: Get libp2p to export this.
package/src/light_push.ts CHANGED
@@ -1,4 +1,5 @@
1
- import type { IBaseProtocol } from "./protocols.js";
1
+ import { IBaseProtocolCore, IBaseProtocolSDK } from "./protocols.js";
2
2
  import type { ISender } from "./sender.js";
3
3
 
4
- export type ILightPush = ISender & IBaseProtocol;
4
+ export type ILightPushSDK = ISender &
5
+ IBaseProtocolSDK & { protocol: IBaseProtocolCore };
@@ -0,0 +1,4 @@
1
+ export type LocalStoragePeerInfo = {
2
+ id: string;
3
+ address: string;
4
+ };
package/src/message.ts CHANGED
@@ -5,7 +5,7 @@ export interface SingleShardInfo {
5
5
  /**
6
6
  * Specifying this field indicates to the encoder/decoder that static sharding must be used.
7
7
  */
8
- shard?: number;
8
+ shard: number;
9
9
  }
10
10
 
11
11
  export interface IRateLimitProof {
package/src/metadata.ts CHANGED
@@ -1,8 +1,17 @@
1
- import type { PeerId } from "@libp2p/interface/peer-id";
1
+ import type { PeerId } from "@libp2p/interface";
2
2
 
3
- import type { ShardInfo } from "./enr.js";
4
- import type { IBaseProtocol } from "./protocols.js";
3
+ import { type ShardInfo } from "./enr.js";
4
+ import type {
5
+ IBaseProtocolCore,
6
+ ProtocolResult,
7
+ ShardingParams
8
+ } from "./protocols.js";
5
9
 
6
- export interface IMetadata extends IBaseProtocol {
7
- query(peerId: PeerId): Promise<ShardInfo | undefined>;
10
+ export type QueryResult = ProtocolResult<"shardInfo", ShardInfo>;
11
+
12
+ // IMetadata always has shardInfo defined while it is optionally undefined in IBaseProtocol
13
+ export interface IMetadata extends Omit<IBaseProtocolCore, "shardInfo"> {
14
+ shardInfo: ShardingParams;
15
+ confirmOrAttemptHandshake(peerId: PeerId): Promise<QueryResult>;
16
+ query(peerId: PeerId): Promise<QueryResult>;
8
17
  }
@@ -1,17 +1,19 @@
1
- import type { PeerId } from "@libp2p/interface/peer-id";
2
- import type { PeerStore } from "@libp2p/interface/peer-store";
3
- import type { ConnectionManager } from "@libp2p/interface-internal/connection-manager";
1
+ import type { PeerId } from "@libp2p/interface";
2
+ import type { PeerStore } from "@libp2p/interface";
3
+ import type { ConnectionManager } from "@libp2p/interface-internal";
4
4
 
5
5
  import { IEnr } from "./enr.js";
6
- import { IBaseProtocol } from "./protocols.js";
6
+ import { IBaseProtocolCore, ProtocolResult } from "./protocols.js";
7
7
 
8
- export interface IPeerExchange extends IBaseProtocol {
9
- query(params: PeerExchangeQueryParams): Promise<PeerInfo[] | undefined>;
8
+ export interface IPeerExchange extends IBaseProtocolCore {
9
+ query(params: PeerExchangeQueryParams): Promise<PeerExchangeResult>;
10
10
  }
11
11
 
12
+ export type PeerExchangeResult = ProtocolResult<"peerInfos", PeerInfo[]>;
13
+
12
14
  export interface PeerExchangeQueryParams {
13
15
  numPeers: number;
14
- peerId?: PeerId;
16
+ peerId: PeerId;
15
17
  }
16
18
 
17
19
  export interface PeerExchangeResponse {
package/src/protocols.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Libp2p } from "@libp2p/interface";
2
- import type { PeerId } from "@libp2p/interface/peer-id";
3
- import type { Peer, PeerStore } from "@libp2p/interface/peer-store";
2
+ import type { PeerId } from "@libp2p/interface";
3
+ import type { Peer, PeerStore } from "@libp2p/interface";
4
4
 
5
5
  import type { ShardInfo } from "./enr.js";
6
6
  import type { CreateLibp2pOptions } from "./libp2p.js";
@@ -14,13 +14,19 @@ export enum Protocols {
14
14
  Filter = "filter"
15
15
  }
16
16
 
17
- export interface IBaseProtocol {
17
+ export type IBaseProtocolCore = {
18
+ shardInfo?: ShardInfo;
18
19
  multicodec: string;
19
20
  peerStore: PeerStore;
20
- peers: () => Promise<Peer[]>;
21
+ allPeers: () => Promise<Peer[]>;
22
+ connectedPeers: () => Promise<Peer[]>;
21
23
  addLibp2pEventListener: Libp2p["addEventListener"];
22
24
  removeLibp2pEventListener: Libp2p["removeEventListener"];
23
- }
25
+ };
26
+
27
+ export type IBaseProtocolSDK = {
28
+ numPeers: number;
29
+ };
24
30
 
25
31
  export type ContentTopicInfo = {
26
32
  clusterId: number;
@@ -58,15 +64,23 @@ export type ProtocolCreateOptions = {
58
64
  * See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
59
65
  *
60
66
  */
61
- shardInfo?: ShardingParams;
67
+ shardInfo?: Partial<ShardingParams>;
62
68
  /**
63
- * You can pass options to the `Libp2p` instance used by {@link @waku/core!WakuNode} using the `libp2p` property.
69
+ * You can pass options to the `Libp2p` instance used by {@link @waku/sdk!WakuNode} using the `libp2p` property.
64
70
  * This property is the same type as the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
65
71
  * apart that we made the `modules` property optional and partial,
66
72
  * allowing its omission and letting Waku set good defaults.
67
- * Notes that some values are overridden by {@link @waku/core!WakuNode} to ensure it implements the Waku protocol.
73
+ * Notes that some values are overridden by {@link @waku/sdk!WakuNode} to ensure it implements the Waku protocol.
68
74
  */
69
75
  libp2p?: Partial<CreateLibp2pOptions>;
76
+ /**
77
+ * Number of peers to connect to, for the usage of the protocol.
78
+ * This is used by:
79
+ * - Light Push to send messages,
80
+ * - Filter to retrieve messages.
81
+ * Defaults to 3.
82
+ */
83
+ numPeersToUse?: number;
70
84
  /**
71
85
  * Byte array used as key for the noise protocol used for connection encryption
72
86
  * by [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
@@ -77,13 +91,38 @@ export type ProtocolCreateOptions = {
77
91
  * Use recommended bootstrap method to discovery and connect to new nodes.
78
92
  */
79
93
  defaultBootstrap?: boolean;
94
+ /**
95
+ * List of peers to use to bootstrap the node. Ignored if defaultBootstrap is set to true.
96
+ */
97
+ bootstrapPeers?: string[];
80
98
  };
81
99
 
82
100
  export type Callback<T extends IDecodedMessage> = (
83
101
  msg: T
84
102
  ) => void | Promise<void>;
85
103
 
86
- export enum SendError {
104
+ // SK = success key name
105
+ // SV = success value type
106
+ // EK = error key name (default: "error")
107
+ // EV = error value type (default: ProtocolError)
108
+ export type ProtocolResult<
109
+ SK extends string,
110
+ SV,
111
+ EK extends string = "error",
112
+ EV = ProtocolError
113
+ > =
114
+ | ({
115
+ [key in SK]: SV;
116
+ } & {
117
+ [key in EK]: null;
118
+ })
119
+ | ({
120
+ [key in SK]: null;
121
+ } & {
122
+ [key in EK]: EV;
123
+ });
124
+
125
+ export enum ProtocolError {
87
126
  /** Could not determine the origin of the fault. Best to check connectivity and try again */
88
127
  GENERIC_FAIL = "Generic error",
89
128
  /**
@@ -128,10 +167,20 @@ export enum SendError {
128
167
  * is logged. Review message validity, or mitigation for `NO_PEER_AVAILABLE`
129
168
  * or `DECODE_FAILED` can be used.
130
169
  */
131
- REMOTE_PEER_REJECTED = "Remote peer rejected"
170
+ REMOTE_PEER_REJECTED = "Remote peer rejected",
171
+ /**
172
+ * The protocol request timed out without a response. This may be due to a connection issue.
173
+ * Mitigation can be: retrying after a given time period
174
+ */
175
+ REQUEST_TIMEOUT = "Request timeout"
176
+ }
177
+
178
+ export interface Failure {
179
+ error: ProtocolError;
180
+ peerId?: PeerId;
132
181
  }
133
182
 
134
183
  export interface SendResult {
135
- errors?: SendError[];
136
- recipients: PeerId[];
184
+ failures?: Failure[];
185
+ successes: PeerId[];
137
186
  }