peerbit 1.1.7 → 1.3.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.
- package/lib/esm/bootstrap.d.ts +1 -0
- package/lib/esm/bootstrap.js +7 -0
- package/lib/esm/bootstrap.js.map +1 -0
- package/lib/esm/peer.d.ts +10 -4
- package/lib/esm/peer.js +24 -7
- package/lib/esm/peer.js.map +1 -1
- package/package.json +7 -7
- package/src/bootstrap.ts +12 -0
- package/src/peer.ts +32 -11
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const resolveBootstrapAddresses: () => Promise<string[]>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const resolveBootstrapAddresses = async () => {
|
|
2
|
+
// Bootstrap addresses for network
|
|
3
|
+
return (await (await fetch("https://raw.githubusercontent.com/dao-xyz/peerbit-bootstrap/master/bootstrap.env")).text())
|
|
4
|
+
.split(/\r?\n/)
|
|
5
|
+
.filter((x) => x.length > 0);
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=bootstrap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bootstrap.js","sourceRoot":"","sources":["../../src/bootstrap.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,yBAAyB,GAAG,KAAK,IAAuB,EAAE;IACtE,kCAAkC;IAClC,OAAO,CACN,MAAM,CACL,MAAM,KAAK,CACV,kFAAkF,CAClF,CACD,CAAC,IAAI,EAAE,CACR;SACC,KAAK,CAAC,OAAO,CAAC;SACd,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AAC/B,CAAC,CAAC"}
|
package/lib/esm/peer.d.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import LazyLevel from "@peerbit/lazy-level";
|
|
2
2
|
import { Multiaddr } from "@multiformats/multiaddr";
|
|
3
3
|
import { Ed25519Keypair, Libp2pKeychain } from "@peerbit/crypto";
|
|
4
|
-
import { Program, Address, ProgramClient } from "@peerbit/program";
|
|
4
|
+
import { Program, Address, ProgramClient, ProgramHandler } from "@peerbit/program";
|
|
5
5
|
import "@libp2p/peer-id";
|
|
6
6
|
import { Libp2pExtended, Libp2pCreateOptions as ClientCreateOptions } from "./libp2p.js";
|
|
7
7
|
import { OpenOptions } from "@peerbit/program";
|
|
8
8
|
export declare const logger: import("pino").Logger<import("pino").LoggerOptions | import("pino").DestinationStream>;
|
|
9
9
|
export type OptionalCreateOptions = {
|
|
10
10
|
limitSigning?: boolean;
|
|
11
|
-
minReplicas?: number;
|
|
12
|
-
refreshIntreval?: number;
|
|
13
11
|
libp2pExternal?: boolean;
|
|
14
12
|
};
|
|
15
13
|
export type CreateOptions = {
|
|
@@ -18,8 +16,13 @@ export type CreateOptions = {
|
|
|
18
16
|
identity: Ed25519Keypair;
|
|
19
17
|
keychain: Libp2pKeychain;
|
|
20
18
|
} & OptionalCreateOptions;
|
|
21
|
-
|
|
19
|
+
type Libp2pOptions = {
|
|
22
20
|
libp2p?: Libp2pExtended | ClientCreateOptions;
|
|
21
|
+
};
|
|
22
|
+
type SimpleLibp2pOptions = {
|
|
23
|
+
relay?: boolean;
|
|
24
|
+
};
|
|
25
|
+
export type CreateInstanceOptions = (SimpleLibp2pOptions | Libp2pOptions) & {
|
|
23
26
|
directory?: string;
|
|
24
27
|
cache?: LazyLevel;
|
|
25
28
|
} & OptionalCreateOptions;
|
|
@@ -42,6 +45,7 @@ export declare class Peerbit implements ProgramClient {
|
|
|
42
45
|
identify: any;
|
|
43
46
|
} & import("./libp2p.js").Libp2pExtendServices;
|
|
44
47
|
get keychain(): Libp2pKeychain;
|
|
48
|
+
get handler(): ProgramHandler;
|
|
45
49
|
getMultiaddrs(): Multiaddr[];
|
|
46
50
|
/**
|
|
47
51
|
* Dial a peer with an Ed25519 peerId
|
|
@@ -49,6 +53,7 @@ export declare class Peerbit implements ProgramClient {
|
|
|
49
53
|
dial(address: string | Multiaddr | Multiaddr[] | ProgramClient): Promise<boolean>;
|
|
50
54
|
start(): Promise<void>;
|
|
51
55
|
stop(): Promise<void>;
|
|
56
|
+
bootstrap(): Promise<boolean[]>;
|
|
52
57
|
/**
|
|
53
58
|
* Default behaviour of a store is only to accept heads that are forks (new roots) with some probability
|
|
54
59
|
* and to replicate heads (and updates) which is requested by another peer
|
|
@@ -59,3 +64,4 @@ export declare class Peerbit implements ProgramClient {
|
|
|
59
64
|
open<S extends Program<Args>, Args = any>(storeOrAddress: S | Address | string, options?: OpenOptions<Args, S>): Promise<S>;
|
|
60
65
|
get memory(): LazyLevel;
|
|
61
66
|
}
|
|
67
|
+
export {};
|
package/lib/esm/peer.js
CHANGED
|
@@ -15,6 +15,7 @@ import { DirectBlock } from "@peerbit/blocks";
|
|
|
15
15
|
import { LevelDatastore } from "datastore-level";
|
|
16
16
|
import { BinaryWriter } from "@dao-xyz/borsh";
|
|
17
17
|
import { logger as loggerFn } from "@peerbit/logger";
|
|
18
|
+
import { resolveBootstrapAddresses } from "./bootstrap.js";
|
|
18
19
|
export const logger = loggerFn({ module: "client" });
|
|
19
20
|
const isLibp2pInstance = (libp2p) => !!libp2p.getMultiaddrs;
|
|
20
21
|
const createLevel = (path) => {
|
|
@@ -63,7 +64,9 @@ export class Peerbit {
|
|
|
63
64
|
}
|
|
64
65
|
static async create(options = {}) {
|
|
65
66
|
await sodium.ready; // Some of the modules depends on sodium to be readyy
|
|
66
|
-
let libp2pExtended = options
|
|
67
|
+
let libp2pExtended = options
|
|
68
|
+
.libp2p;
|
|
69
|
+
const asRelay = options.relay;
|
|
67
70
|
const blocksDirectory = options.directory != null
|
|
68
71
|
? path.join(options.directory, "/blocks").toString()
|
|
69
72
|
: undefined;
|
|
@@ -77,8 +80,11 @@ export class Peerbit {
|
|
|
77
80
|
if (!libp2pExtended) {
|
|
78
81
|
libp2pExtended = await createLibp2pExtended({
|
|
79
82
|
services: {
|
|
80
|
-
blocks: (c) => new DirectBlock(c, {
|
|
81
|
-
|
|
83
|
+
blocks: (c) => new DirectBlock(c, {
|
|
84
|
+
canRelayMessage: asRelay,
|
|
85
|
+
directory: blocksDirectory,
|
|
86
|
+
}),
|
|
87
|
+
pubsub: (c) => new DirectSub(c, { canRelayMessage: asRelay }),
|
|
82
88
|
},
|
|
83
89
|
// If directory is passed, we store keys within that directory, else we will use memory datastore (which is the default behaviour)
|
|
84
90
|
datastore,
|
|
@@ -93,8 +99,11 @@ export class Peerbit {
|
|
|
93
99
|
libp2pExtended = await createLibp2pExtended({
|
|
94
100
|
...extendedOptions,
|
|
95
101
|
services: {
|
|
96
|
-
blocks: (c) => new DirectBlock(c, {
|
|
97
|
-
|
|
102
|
+
blocks: (c) => new DirectBlock(c, {
|
|
103
|
+
canRelayMessage: asRelay,
|
|
104
|
+
directory: blocksDirectory,
|
|
105
|
+
}),
|
|
106
|
+
pubsub: (c) => new DirectSub(c, { canRelayMessage: asRelay }),
|
|
98
107
|
...extendedOptions?.services,
|
|
99
108
|
},
|
|
100
109
|
datastore,
|
|
@@ -140,8 +149,6 @@ export class Peerbit {
|
|
|
140
149
|
cache,
|
|
141
150
|
libp2pExternal,
|
|
142
151
|
limitSigning: options.limitSigning,
|
|
143
|
-
minReplicas: options.minReplicas,
|
|
144
|
-
refreshIntreval: options.refreshIntreval,
|
|
145
152
|
identity,
|
|
146
153
|
keychain,
|
|
147
154
|
});
|
|
@@ -162,6 +169,9 @@ export class Peerbit {
|
|
|
162
169
|
get keychain() {
|
|
163
170
|
return this._keychain;
|
|
164
171
|
}
|
|
172
|
+
get handler() {
|
|
173
|
+
return this._handler;
|
|
174
|
+
}
|
|
165
175
|
getMultiaddrs() {
|
|
166
176
|
return this.libp2p.getMultiaddrs();
|
|
167
177
|
}
|
|
@@ -195,6 +205,13 @@ export class Peerbit {
|
|
|
195
205
|
await this.libp2p.stop();
|
|
196
206
|
}
|
|
197
207
|
}
|
|
208
|
+
async bootstrap() {
|
|
209
|
+
const addresses = await resolveBootstrapAddresses();
|
|
210
|
+
if (addresses.length === 0) {
|
|
211
|
+
throw new Error("Failed to find any addresses to dial");
|
|
212
|
+
}
|
|
213
|
+
return Promise.all(addresses.map((x) => this.dial(x)));
|
|
214
|
+
}
|
|
198
215
|
/**
|
|
199
216
|
* Default behaviour of a store is only to accept heads that are forks (new roots) with some probability
|
|
200
217
|
* and to replicate heads (and updates) which is requested by another peer
|
package/lib/esm/peer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"peer.js","sourceRoot":"","sources":["../../src/peer.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,qBAAqB,CAAC;AAE5C,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAC9B,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAa,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAE5E,OAAO,EACN,cAAc,EACd,gBAAgB,EAChB,cAAc,GACd,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAIN,cAAc,GACd,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,IAAI,MAAM,iBAAiB,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,iBAAiB,CAAC;AACzB,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEvC,OAAO,EACN,oBAAoB,GAGpB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,MAAM,IAAI,QAAQ,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+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": "dao.xyz",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"url": "https://github.com/dao-xyz/peerbit"
|
|
11
11
|
},
|
|
12
12
|
"engines": {
|
|
13
|
-
"node": ">=
|
|
13
|
+
"node": ">=18"
|
|
14
14
|
},
|
|
15
15
|
"files": [
|
|
16
16
|
"lib",
|
|
@@ -40,12 +40,12 @@
|
|
|
40
40
|
"@libp2p/tcp": "^7.0.3",
|
|
41
41
|
"@libp2p/webrtc": "^2.0.11",
|
|
42
42
|
"@libp2p/websockets": "^6.0.3",
|
|
43
|
-
"@peerbit/blocks": "^1.0.
|
|
44
|
-
"@peerbit/crypto": "1.0.
|
|
43
|
+
"@peerbit/blocks": "^1.0.8",
|
|
44
|
+
"@peerbit/crypto": "1.0.5",
|
|
45
45
|
"@peerbit/lazy-level": "1.0.2",
|
|
46
46
|
"@peerbit/logger": "1.0.1",
|
|
47
|
-
"@peerbit/program": "2.
|
|
48
|
-
"@peerbit/pubsub": "^1.0
|
|
47
|
+
"@peerbit/program": "2.2.0",
|
|
48
|
+
"@peerbit/pubsub": "^1.1.0",
|
|
49
49
|
"@peerbit/uint8arrays": "3.0.1",
|
|
50
50
|
"datastore-level": "^10.1.2",
|
|
51
51
|
"level": "^8.0.0",
|
|
@@ -73,5 +73,5 @@
|
|
|
73
73
|
"p2p",
|
|
74
74
|
"peer-to-peer"
|
|
75
75
|
],
|
|
76
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "464e807d679e24b897b7811ac99d6f85fbd756f9"
|
|
77
77
|
}
|
package/src/bootstrap.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const resolveBootstrapAddresses = async (): Promise<string[]> => {
|
|
2
|
+
// Bootstrap addresses for network
|
|
3
|
+
return (
|
|
4
|
+
await (
|
|
5
|
+
await fetch(
|
|
6
|
+
"https://raw.githubusercontent.com/dao-xyz/peerbit-bootstrap/master/bootstrap.env"
|
|
7
|
+
)
|
|
8
|
+
).text()
|
|
9
|
+
)
|
|
10
|
+
.split(/\r?\n/)
|
|
11
|
+
.filter((x) => x.length > 0);
|
|
12
|
+
};
|
package/src/peer.ts
CHANGED
|
@@ -32,13 +32,12 @@ import { LevelDatastore } from "datastore-level";
|
|
|
32
32
|
import { BinaryWriter } from "@dao-xyz/borsh";
|
|
33
33
|
import { logger as loggerFn } from "@peerbit/logger";
|
|
34
34
|
import { OpenOptions } from "@peerbit/program";
|
|
35
|
+
import { resolveBootstrapAddresses } from "./bootstrap.js";
|
|
35
36
|
|
|
36
37
|
export const logger = loggerFn({ module: "client" });
|
|
37
38
|
|
|
38
39
|
export type OptionalCreateOptions = {
|
|
39
40
|
limitSigning?: boolean;
|
|
40
|
-
minReplicas?: number;
|
|
41
|
-
refreshIntreval?: number;
|
|
42
41
|
libp2pExternal?: boolean;
|
|
43
42
|
};
|
|
44
43
|
export type CreateOptions = {
|
|
@@ -48,8 +47,9 @@ export type CreateOptions = {
|
|
|
48
47
|
keychain: Libp2pKeychain;
|
|
49
48
|
} & OptionalCreateOptions;
|
|
50
49
|
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
type Libp2pOptions = { libp2p?: Libp2pExtended | ClientCreateOptions };
|
|
51
|
+
type SimpleLibp2pOptions = { relay?: boolean };
|
|
52
|
+
export type CreateInstanceOptions = (SimpleLibp2pOptions | Libp2pOptions) & {
|
|
53
53
|
directory?: string;
|
|
54
54
|
cache?: LazyLevel;
|
|
55
55
|
} & OptionalCreateOptions;
|
|
@@ -121,7 +121,10 @@ export class Peerbit implements ProgramClient {
|
|
|
121
121
|
static async create(options: CreateInstanceOptions = {}): Promise<Peerbit> {
|
|
122
122
|
await sodium.ready; // Some of the modules depends on sodium to be readyy
|
|
123
123
|
|
|
124
|
-
let libp2pExtended: Libp2pExtended = options
|
|
124
|
+
let libp2pExtended: Libp2pExtended | undefined = (options as Libp2pOptions)
|
|
125
|
+
.libp2p as Libp2pExtended;
|
|
126
|
+
const asRelay = (options as SimpleLibp2pOptions).relay;
|
|
127
|
+
|
|
125
128
|
const blocksDirectory =
|
|
126
129
|
options.directory != null
|
|
127
130
|
? path.join(options.directory, "/blocks").toString()
|
|
@@ -139,8 +142,12 @@ export class Peerbit implements ProgramClient {
|
|
|
139
142
|
if (!libp2pExtended) {
|
|
140
143
|
libp2pExtended = await createLibp2pExtended({
|
|
141
144
|
services: {
|
|
142
|
-
blocks: (c) =>
|
|
143
|
-
|
|
145
|
+
blocks: (c) =>
|
|
146
|
+
new DirectBlock(c, {
|
|
147
|
+
canRelayMessage: asRelay,
|
|
148
|
+
directory: blocksDirectory,
|
|
149
|
+
}),
|
|
150
|
+
pubsub: (c) => new DirectSub(c, { canRelayMessage: asRelay }),
|
|
144
151
|
},
|
|
145
152
|
// If directory is passed, we store keys within that directory, else we will use memory datastore (which is the default behaviour)
|
|
146
153
|
datastore,
|
|
@@ -153,8 +160,12 @@ export class Peerbit implements ProgramClient {
|
|
|
153
160
|
libp2pExtended = await createLibp2pExtended({
|
|
154
161
|
...extendedOptions,
|
|
155
162
|
services: {
|
|
156
|
-
blocks: (c) =>
|
|
157
|
-
|
|
163
|
+
blocks: (c) =>
|
|
164
|
+
new DirectBlock(c, {
|
|
165
|
+
canRelayMessage: asRelay,
|
|
166
|
+
directory: blocksDirectory,
|
|
167
|
+
}),
|
|
168
|
+
pubsub: (c) => new DirectSub(c, { canRelayMessage: asRelay }),
|
|
158
169
|
...extendedOptions?.services,
|
|
159
170
|
},
|
|
160
171
|
datastore,
|
|
@@ -209,8 +220,6 @@ export class Peerbit implements ProgramClient {
|
|
|
209
220
|
cache,
|
|
210
221
|
libp2pExternal,
|
|
211
222
|
limitSigning: options.limitSigning,
|
|
212
|
-
minReplicas: options.minReplicas,
|
|
213
|
-
refreshIntreval: options.refreshIntreval,
|
|
214
223
|
identity,
|
|
215
224
|
keychain,
|
|
216
225
|
});
|
|
@@ -236,6 +245,10 @@ export class Peerbit implements ProgramClient {
|
|
|
236
245
|
return this._keychain;
|
|
237
246
|
}
|
|
238
247
|
|
|
248
|
+
get handler(): ProgramHandler {
|
|
249
|
+
return this._handler;
|
|
250
|
+
}
|
|
251
|
+
|
|
239
252
|
getMultiaddrs(): Multiaddr[] {
|
|
240
253
|
return this.libp2p.getMultiaddrs();
|
|
241
254
|
}
|
|
@@ -281,6 +294,14 @@ export class Peerbit implements ProgramClient {
|
|
|
281
294
|
}
|
|
282
295
|
}
|
|
283
296
|
|
|
297
|
+
async bootstrap() {
|
|
298
|
+
const addresses = await resolveBootstrapAddresses();
|
|
299
|
+
if (addresses.length === 0) {
|
|
300
|
+
throw new Error("Failed to find any addresses to dial");
|
|
301
|
+
}
|
|
302
|
+
return Promise.all(addresses.map((x) => this.dial(x)));
|
|
303
|
+
}
|
|
304
|
+
|
|
284
305
|
/**
|
|
285
306
|
* Default behaviour of a store is only to accept heads that are forks (new roots) with some probability
|
|
286
307
|
* and to replicate heads (and updates) which is requested by another peer
|