peerbit 2.0.15 → 3.0.1
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/libp2p.d.ts +3 -1
- package/lib/esm/libp2p.js +9 -7
- package/lib/esm/libp2p.js.map +1 -1
- package/lib/esm/peer.d.ts +10 -13
- package/lib/esm/peer.js +43 -65
- package/lib/esm/peer.js.map +1 -1
- package/lib/esm/transports-browser.d.ts +1 -1
- package/lib/esm/transports-browser.js +1 -1
- package/lib/esm/transports-browser.js.map +1 -1
- package/lib/esm/transports.d.ts +2 -2
- package/lib/esm/transports.js +2 -2
- package/lib/esm/transports.js.map +1 -1
- package/package.json +17 -15
- package/src/libp2p.ts +11 -8
- package/src/peer.ts +68 -91
- package/src/transports-browser.ts +1 -1
- package/src/transports.ts +2 -2
package/lib/esm/libp2p.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { Libp2p, Libp2pOptions, ServiceFactoryMap } from "libp2p";
|
|
2
2
|
import { DirectSub } from "@peerbit/pubsub";
|
|
3
3
|
import { DirectBlock } from "@peerbit/blocks";
|
|
4
|
-
import { CircuitRelayService } from "libp2p/
|
|
4
|
+
import { CircuitRelayService } from "@libp2p/circuit-relay-v2";
|
|
5
|
+
import { Keychain } from "@peerbit/keychain";
|
|
5
6
|
export type Libp2pExtendServices = {
|
|
6
7
|
pubsub: DirectSub;
|
|
7
8
|
blocks: DirectBlock;
|
|
9
|
+
keychain: Keychain;
|
|
8
10
|
};
|
|
9
11
|
export type Libp2pExtended = Libp2p<{
|
|
10
12
|
relay: CircuitRelayService;
|
package/lib/esm/libp2p.js
CHANGED
|
@@ -4,17 +4,19 @@ import { DirectBlock } from "@peerbit/blocks";
|
|
|
4
4
|
import { noise } from "@dao-xyz/libp2p-noise";
|
|
5
5
|
import { mplex } from "@libp2p/mplex";
|
|
6
6
|
import { transports, relay, listen } from "./transports.js";
|
|
7
|
-
import {
|
|
7
|
+
import { identify } from "@libp2p/identify";
|
|
8
8
|
import { yamux } from "@chainsafe/libp2p-yamux";
|
|
9
|
+
import { DefaultKeychain } from "@peerbit/keychain";
|
|
9
10
|
export const createLibp2pExtended = (opts = {
|
|
10
11
|
services: {
|
|
11
12
|
blocks: (c) => new DirectBlock(c),
|
|
12
|
-
pubsub: (c) => new DirectSub(c)
|
|
13
|
+
pubsub: (c) => new DirectSub(c),
|
|
14
|
+
keychain: (c) => new DefaultKeychain()
|
|
13
15
|
}
|
|
14
16
|
}) => {
|
|
15
17
|
const relayIdentify = {
|
|
16
18
|
relay: relay(),
|
|
17
|
-
identify:
|
|
19
|
+
identify: identify()
|
|
18
20
|
};
|
|
19
21
|
// https://github.com/libp2p/js-libp2p/issues/1757
|
|
20
22
|
Object.keys(relayIdentify).forEach((key) => {
|
|
@@ -40,12 +42,12 @@ export const createLibp2pExtended = (opts = {
|
|
|
40
42
|
pubsub: opts.services?.pubsub ||
|
|
41
43
|
((c) => new DirectSub(c, {
|
|
42
44
|
canRelayMessage: true,
|
|
43
|
-
signaturePolicy: "StrictNoSign"
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
45
|
+
signaturePolicy: "StrictNoSign"
|
|
46
|
+
// auto dial true
|
|
47
|
+
// auto prune true
|
|
47
48
|
})),
|
|
48
49
|
blocks: opts.services?.blocks || ((c) => new DirectBlock(c)),
|
|
50
|
+
keychain: opts.services?.keychain || ((c) => new DefaultKeychain()),
|
|
49
51
|
...opts.services
|
|
50
52
|
}
|
|
51
53
|
});
|
package/lib/esm/libp2p.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"libp2p.js","sourceRoot":"","sources":["../../src/libp2p.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA4C,MAAM,QAAQ,CAAC;AAChF,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,O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-
import
|
|
1
|
+
import { AnyStore } from "../../../utils/any-store/lib/esm/index.js";
|
|
2
2
|
import { Multiaddr } from "@multiformats/multiaddr";
|
|
3
|
-
import { Ed25519Keypair
|
|
3
|
+
import { Ed25519Keypair } from "@peerbit/crypto";
|
|
4
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
|
-
export declare const logger: import("pino").Logger<
|
|
8
|
+
export declare const logger: import("pino").Logger<never>;
|
|
9
9
|
export type OptionalCreateOptions = {
|
|
10
10
|
libp2pExternal?: boolean;
|
|
11
11
|
};
|
|
12
12
|
export type CreateOptions = {
|
|
13
13
|
directory?: string;
|
|
14
|
-
|
|
14
|
+
memory: AnyStore;
|
|
15
15
|
identity: Ed25519Keypair;
|
|
16
|
-
keychain: Libp2pKeychain;
|
|
17
16
|
} & OptionalCreateOptions;
|
|
17
|
+
type ExtractArgs<T> = T extends Program<infer Args> ? Args : never;
|
|
18
18
|
type Libp2pOptions = {
|
|
19
19
|
libp2p?: Libp2pExtended | ClientCreateOptions;
|
|
20
20
|
};
|
|
@@ -23,26 +23,23 @@ type SimpleLibp2pOptions = {
|
|
|
23
23
|
};
|
|
24
24
|
export type CreateInstanceOptions = (SimpleLibp2pOptions | Libp2pOptions) & {
|
|
25
25
|
directory?: string;
|
|
26
|
-
cache?: LazyLevel;
|
|
27
26
|
} & OptionalCreateOptions;
|
|
28
27
|
export declare class Peerbit implements ProgramClient {
|
|
29
28
|
_libp2p: Libp2pExtended;
|
|
30
29
|
directory?: string;
|
|
31
|
-
private
|
|
30
|
+
private _memory;
|
|
32
31
|
private _libp2pExternal?;
|
|
33
32
|
private _identity;
|
|
34
|
-
private _keychain;
|
|
35
33
|
private _handler;
|
|
36
34
|
constructor(libp2p: Libp2pExtended, options: CreateOptions);
|
|
37
35
|
static create(options?: CreateInstanceOptions): Promise<Peerbit>;
|
|
38
36
|
get libp2p(): Libp2pExtended;
|
|
39
37
|
get identity(): Ed25519Keypair;
|
|
40
|
-
get peerId(): import("@libp2p/interface
|
|
38
|
+
get peerId(): import("@libp2p/interface").PeerId;
|
|
41
39
|
get services(): {
|
|
42
|
-
relay: import("libp2p/
|
|
40
|
+
relay: import("@libp2p/circuit-relay-v2").CircuitRelayService;
|
|
43
41
|
identify: any;
|
|
44
42
|
} & import("./libp2p.js").Libp2pExtendServices;
|
|
45
|
-
get keychain(): Libp2pKeychain;
|
|
46
43
|
get handler(): ProgramHandler;
|
|
47
44
|
getMultiaddrs(): Multiaddr[];
|
|
48
45
|
/**
|
|
@@ -59,7 +56,7 @@ export declare class Peerbit implements ProgramClient {
|
|
|
59
56
|
* @param options
|
|
60
57
|
* @returns
|
|
61
58
|
*/
|
|
62
|
-
open<S extends Program<
|
|
63
|
-
get memory():
|
|
59
|
+
open<S extends Program<ExtractArgs<S>>>(storeOrAddress: S | Address | string, options?: OpenOptions<ExtractArgs<S>, S>): Promise<S>;
|
|
60
|
+
get memory(): AnyStore;
|
|
64
61
|
}
|
|
65
62
|
export {};
|
package/lib/esm/peer.js
CHANGED
|
@@ -1,46 +1,38 @@
|
|
|
1
|
-
import LazyLevel from "@peerbit/lazy-level";
|
|
2
|
-
import { Level } from "level";
|
|
3
|
-
import { MemoryLevel } from "memory-level";
|
|
4
1
|
import { multiaddr, isMultiaddr } from "@multiformats/multiaddr";
|
|
5
|
-
import { Ed25519Keypair, Ed25519PublicKey
|
|
2
|
+
import { Ed25519Keypair, Ed25519PublicKey } from "@peerbit/crypto";
|
|
6
3
|
import { ProgramHandler } from "@peerbit/program";
|
|
7
4
|
import { DirectSub } from "@peerbit/pubsub";
|
|
8
5
|
import sodium from "libsodium-wrappers";
|
|
9
6
|
import path from "path-browserify";
|
|
10
7
|
import { waitFor } from "@peerbit/time";
|
|
11
8
|
import "@libp2p/peer-id";
|
|
12
|
-
import { Cache } from "@peerbit/cache";
|
|
13
9
|
import { createLibp2pExtended } from "./libp2p.js";
|
|
14
10
|
import { DirectBlock } from "@peerbit/blocks";
|
|
15
11
|
import { LevelDatastore } from "datastore-level";
|
|
16
|
-
import { BinaryWriter } from "@dao-xyz/borsh";
|
|
17
12
|
import { logger as loggerFn } from "@peerbit/logger";
|
|
18
13
|
import { resolveBootstrapAddresses } from "./bootstrap.js";
|
|
14
|
+
import { createStore } from "@peerbit/any-store";
|
|
15
|
+
import { DefaultKeychain } from "@peerbit/keychain";
|
|
19
16
|
export const logger = loggerFn({ module: "client" });
|
|
20
17
|
const isLibp2pInstance = (libp2p) => !!libp2p.getMultiaddrs;
|
|
21
|
-
const createLevel = (path) => {
|
|
22
|
-
return path
|
|
23
|
-
? new Level(path, { valueEncoding: "view" })
|
|
24
|
-
: new MemoryLevel({ valueEncoding: "view" });
|
|
25
|
-
};
|
|
26
18
|
const createCache = async (directory, options) => {
|
|
27
|
-
const cache =
|
|
19
|
+
const cache = createStore(directory);
|
|
28
20
|
// "Wake up" the caches if they need it
|
|
29
21
|
if (cache)
|
|
30
22
|
await cache.open();
|
|
31
23
|
if (options?.reset) {
|
|
32
|
-
await cache
|
|
24
|
+
await cache.clear();
|
|
33
25
|
}
|
|
34
26
|
return cache;
|
|
35
27
|
};
|
|
28
|
+
const SELF_IDENTITY_KEY_ID = new TextEncoder().encode("__self__");
|
|
36
29
|
export class Peerbit {
|
|
37
30
|
_libp2p;
|
|
38
31
|
directory;
|
|
39
|
-
|
|
32
|
+
_memory;
|
|
40
33
|
_libp2pExternal = false;
|
|
41
34
|
// Libp2p peerid in Identity form
|
|
42
35
|
_identity;
|
|
43
|
-
_keychain; // Keychain + Caching + X25519 keys
|
|
44
36
|
_handler;
|
|
45
37
|
constructor(libp2p, options) {
|
|
46
38
|
if (libp2p == null) {
|
|
@@ -55,9 +47,8 @@ export class Peerbit {
|
|
|
55
47
|
throw new Error("Only Ed25519 peerIds are supported");
|
|
56
48
|
}
|
|
57
49
|
this._identity = options.identity;
|
|
58
|
-
this._keychain = options.keychain;
|
|
59
50
|
this.directory = options.directory;
|
|
60
|
-
this.
|
|
51
|
+
this._memory = options.memory;
|
|
61
52
|
this._libp2pExternal = options.libp2pExternal;
|
|
62
53
|
}
|
|
63
54
|
static async create(options = {}) {
|
|
@@ -65,49 +56,44 @@ export class Peerbit {
|
|
|
65
56
|
let libp2pExtended = options
|
|
66
57
|
.libp2p;
|
|
67
58
|
const asRelay = options.relay;
|
|
68
|
-
const
|
|
69
|
-
|
|
59
|
+
const directory = options.directory;
|
|
60
|
+
const hasDir = directory != null;
|
|
61
|
+
const memory = await createCache(directory != null ? path.join(directory, "/cache") : undefined);
|
|
62
|
+
const blocksDirectory = hasDir
|
|
63
|
+
? path.join(options["directory"], "/blocks").toString()
|
|
64
|
+
: undefined;
|
|
65
|
+
const keychainDirectory = hasDir
|
|
66
|
+
? path.join(options["directory"], "/keychain").toString()
|
|
70
67
|
: undefined;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
? new LevelDatastore(path.join(options.directory, "/libp2p").toString())
|
|
68
|
+
const datastore = hasDir
|
|
69
|
+
? new LevelDatastore(path.join(options["directory"], "/libp2p").toString())
|
|
74
70
|
: undefined;
|
|
75
71
|
if (datastore) {
|
|
76
72
|
await datastore.open();
|
|
77
73
|
}
|
|
78
|
-
|
|
74
|
+
const libp2pExternal = libp2pExtended && isLibp2pInstance(libp2pExtended);
|
|
75
|
+
if (!libp2pExternal) {
|
|
76
|
+
const extendedOptions = libp2pExtended;
|
|
77
|
+
const keychain = new DefaultKeychain({
|
|
78
|
+
store: createStore(keychainDirectory)
|
|
79
|
+
});
|
|
80
|
+
const peerId = extendedOptions?.peerId ||
|
|
81
|
+
(await (await keychain.exportById(SELF_IDENTITY_KEY_ID, Ed25519Keypair))?.toPeerId());
|
|
79
82
|
libp2pExtended = await createLibp2pExtended({
|
|
83
|
+
...extendedOptions,
|
|
84
|
+
peerId,
|
|
80
85
|
services: {
|
|
86
|
+
keychain: (c) => keychain,
|
|
81
87
|
blocks: (c) => new DirectBlock(c, {
|
|
82
88
|
canRelayMessage: asRelay,
|
|
83
89
|
directory: blocksDirectory
|
|
84
90
|
}),
|
|
85
|
-
pubsub: (c) => new DirectSub(c, { canRelayMessage: asRelay })
|
|
91
|
+
pubsub: (c) => new DirectSub(c, { canRelayMessage: asRelay }),
|
|
92
|
+
...extendedOptions?.services
|
|
86
93
|
},
|
|
87
|
-
// If directory is passed, we store keys within that directory, else we will use memory datastore (which is the default behaviour)
|
|
88
94
|
datastore
|
|
89
95
|
});
|
|
90
96
|
}
|
|
91
|
-
else {
|
|
92
|
-
if (isLibp2pInstance(libp2pExtended)) {
|
|
93
|
-
libp2pExternal = true; // libp2p was created outside
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
const extendedOptions = libp2pExtended;
|
|
97
|
-
libp2pExtended = await createLibp2pExtended({
|
|
98
|
-
...extendedOptions,
|
|
99
|
-
services: {
|
|
100
|
-
blocks: (c) => new DirectBlock(c, {
|
|
101
|
-
canRelayMessage: asRelay,
|
|
102
|
-
directory: blocksDirectory
|
|
103
|
-
}),
|
|
104
|
-
pubsub: (c) => new DirectSub(c, { canRelayMessage: asRelay }),
|
|
105
|
-
...extendedOptions?.services
|
|
106
|
-
},
|
|
107
|
-
datastore
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
97
|
if (datastore) {
|
|
112
98
|
const stopFn = libp2pExtended.stop.bind(libp2pExtended);
|
|
113
99
|
libp2pExtended.stop = async () => {
|
|
@@ -115,24 +101,20 @@ export class Peerbit {
|
|
|
115
101
|
await datastore?.close();
|
|
116
102
|
};
|
|
117
103
|
}
|
|
118
|
-
if (
|
|
104
|
+
if (libp2pExtended.status === "stopped" ||
|
|
105
|
+
libp2pExtended.status === "stopping") {
|
|
119
106
|
await libp2pExtended.start();
|
|
120
107
|
}
|
|
121
108
|
if (libp2pExtended.peerId.type !== "Ed25519") {
|
|
122
109
|
throw new Error("Unsupported id type, expecting Ed25519 but got " +
|
|
123
110
|
libp2pExtended.peerId.type);
|
|
124
111
|
}
|
|
125
|
-
const directory = options.directory;
|
|
126
|
-
const cache = options.cache ||
|
|
127
|
-
(await createCache(directory ? path.join(directory, "/cache") : undefined));
|
|
128
112
|
const identity = Ed25519Keypair.fromPeerId(libp2pExtended.peerId);
|
|
129
|
-
const keychain = new Libp2pKeychain(libp2pExtended.keychain, {
|
|
130
|
-
cache: new Cache({ max: 1000 })
|
|
131
|
-
});
|
|
132
113
|
try {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
114
|
+
await libp2pExtended.services.keychain.import({
|
|
115
|
+
keypair: identity,
|
|
116
|
+
id: SELF_IDENTITY_KEY_ID
|
|
117
|
+
});
|
|
136
118
|
}
|
|
137
119
|
catch (error) {
|
|
138
120
|
if (error.code == "ERR_KEY_ALREADY_EXISTS") {
|
|
@@ -144,10 +126,9 @@ export class Peerbit {
|
|
|
144
126
|
}
|
|
145
127
|
const peer = new Peerbit(libp2pExtended, {
|
|
146
128
|
directory,
|
|
147
|
-
|
|
129
|
+
memory: memory,
|
|
148
130
|
libp2pExternal,
|
|
149
|
-
identity
|
|
150
|
-
keychain
|
|
131
|
+
identity
|
|
151
132
|
});
|
|
152
133
|
return peer;
|
|
153
134
|
}
|
|
@@ -163,9 +144,6 @@ export class Peerbit {
|
|
|
163
144
|
get services() {
|
|
164
145
|
return this.libp2p.services;
|
|
165
146
|
}
|
|
166
|
-
get keychain() {
|
|
167
|
-
return this._keychain;
|
|
168
|
-
}
|
|
169
147
|
get handler() {
|
|
170
148
|
return this._handler;
|
|
171
149
|
}
|
|
@@ -188,15 +166,15 @@ export class Peerbit {
|
|
|
188
166
|
this.libp2p.services.blocks.peers.has(publicKey.hashcode()))) || false);
|
|
189
167
|
}
|
|
190
168
|
async start() {
|
|
191
|
-
await this.
|
|
192
|
-
if (
|
|
169
|
+
await this._memory.open();
|
|
170
|
+
if (this.libp2p.status === "stopped" || this.libp2p.status === "stopping") {
|
|
193
171
|
this._libp2pExternal = false; // this means we will also close libp2p client on close
|
|
194
172
|
return this.libp2p.start();
|
|
195
173
|
}
|
|
196
174
|
}
|
|
197
175
|
async stop() {
|
|
198
176
|
await this._handler?.stop();
|
|
199
|
-
await this.
|
|
177
|
+
await this._memory.close();
|
|
200
178
|
// Close libp2p (after above)
|
|
201
179
|
if (!this._libp2pExternal) {
|
|
202
180
|
// only close it if we created it
|
|
@@ -221,7 +199,7 @@ export class Peerbit {
|
|
|
221
199
|
return (this._handler || (this._handler = new ProgramHandler({ client: this }))).open(storeOrAddress, options);
|
|
222
200
|
}
|
|
223
201
|
get memory() {
|
|
224
|
-
return this.
|
|
202
|
+
return this._memory;
|
|
225
203
|
}
|
|
226
204
|
}
|
|
227
205
|
//# sourceMappingURL=peer.js.map
|
package/lib/esm/peer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"peer.js","sourceRoot":"","sources":["../../src/peer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"peer.js","sourceRoot":"","sources":["../../src/peer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAa,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAE5E,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACnE,OAAO,EAIN,cAAc,EACd,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;AAEzB,OAAO,EACN,oBAAoB,EAGpB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,c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AAC;IACrB,CAAC;CACD"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare const transports: () => ((components: import("libp2p/dist/src/
|
|
1
|
+
export declare const transports: () => (((components: import("@libp2p/circuit-relay-v2/dist/src/transport").CircuitRelayTransportComponents) => import("@libp2p/interface/dist/src").Transport) | ((components: import("@libp2p/webrtc/dist/src/private-to-private/transport").WebRTCTransportComponents) => import("@libp2p/interface/dist/src").Transport))[];
|
|
2
2
|
export declare const relay: () => undefined;
|
|
3
3
|
export declare const listen: () => string[] | undefined;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { webSockets } from "@libp2p/websockets";
|
|
2
|
-
import { circuitRelayTransport } from "libp2p/circuit-relay";
|
|
2
|
+
import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
|
|
3
3
|
import { webRTC } from "@libp2p/webrtc";
|
|
4
4
|
import { all } from "@libp2p/websockets/filters";
|
|
5
5
|
export const transports = () => [
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transports-browser.js","sourceRoot":"","sources":["../../src/transports-browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,qBAAqB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"transports-browser.js","sourceRoot":"","sources":["../../src/transports-browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAE,GAAG,EAAE,MAAM,4BAA4B,CAAC;AAEjD,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,EAAE,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;IAC3B,qBAAqB,CAAC;QACrB,cAAc,EAAE,CAAC;KACjB,CAAC;IACF,MAAM,CAAC,EAAE,CAAC;CACV,CAAC;AAEF,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,EAAE,CAAC,SAAS,CAAC;AAErC,MAAM,CAAC,MAAM,MAAM,GAA+B,GAAG,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC"}
|
package/lib/esm/transports.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare const transports: () => (((components: import("libp2p/dist/src/
|
|
2
|
-
export declare const relay: () => (components: import("libp2p/dist/src/
|
|
1
|
+
export declare const transports: () => (((components: import("@libp2p/circuit-relay-v2/dist/src/transport").CircuitRelayTransportComponents) => import("@libp2p/interface/dist/src").Transport) | ((components: import("@libp2p/tcp").TCPComponents) => import("@libp2p/interface/dist/src").Transport))[];
|
|
2
|
+
export declare const relay: () => (components: import("@libp2p/circuit-relay-v2/dist/src/server").CircuitRelayServerComponents) => import("@libp2p/circuit-relay-v2").CircuitRelayService;
|
|
3
3
|
export declare const listen: () => string[] | undefined;
|
package/lib/esm/transports.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { webSockets } from "@libp2p/websockets";
|
|
2
|
-
import { circuitRelayTransport } from "libp2p/circuit-relay";
|
|
2
|
+
import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
|
|
3
3
|
import { tcp } from "@libp2p/tcp";
|
|
4
|
-
import { circuitRelayServer } from "libp2p/circuit-relay";
|
|
4
|
+
import { circuitRelayServer } from "@libp2p/circuit-relay-v2";
|
|
5
5
|
import { all } from "@libp2p/websockets/filters";
|
|
6
6
|
export const transports = () => [
|
|
7
7
|
webSockets({ filter: all }),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transports.js","sourceRoot":"","sources":["../../src/transports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,qBAAqB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"transports.js","sourceRoot":"","sources":["../../src/transports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,GAAG,EAAE,MAAM,4BAA4B,CAAC;AACjD,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,EAAE,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;IAC3B,qBAAqB,EAAE;IACvB,GAAG,EAAE;CACL,CAAC;AACF,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,EAAE,CACzB,kBAAkB,CAAC,EAAE,YAAY,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;AAEjE,MAAM,CAAC,MAAM,MAAM,GAA+B,GAAG,EAAE,CAAC;IACvD,sBAAsB;IACtB,yBAAyB;CACzB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "peerbit",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "Peerbit client",
|
|
5
5
|
"author": "dao.xyz",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,21 +34,23 @@
|
|
|
34
34
|
"./transports.js": "./lib/esm/transports-browser.js"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@chainsafe/libp2p-yamux": "^
|
|
37
|
+
"@chainsafe/libp2p-yamux": "^6.0.1",
|
|
38
38
|
"@dao-xyz/borsh": "^5.1.8",
|
|
39
|
-
"@dao-xyz/libp2p-noise": "^
|
|
40
|
-
"@libp2p/
|
|
41
|
-
"@libp2p/
|
|
42
|
-
"@libp2p/
|
|
43
|
-
"@libp2p/
|
|
44
|
-
"@
|
|
45
|
-
"@
|
|
46
|
-
"@peerbit/
|
|
47
|
-
"@peerbit/
|
|
48
|
-
"@peerbit/
|
|
49
|
-
"@peerbit/
|
|
39
|
+
"@dao-xyz/libp2p-noise": "^14.1.2",
|
|
40
|
+
"@libp2p/circuit-relay-v2": "^1.0.9",
|
|
41
|
+
"@libp2p/identify": "^1.0.8",
|
|
42
|
+
"@libp2p/mplex": "^10.0.9",
|
|
43
|
+
"@libp2p/tcp": "^9.0.9",
|
|
44
|
+
"@libp2p/webrtc": "^4.0.13",
|
|
45
|
+
"@libp2p/websockets": "^8.0.9",
|
|
46
|
+
"@peerbit/any-store": "^1.0.1",
|
|
47
|
+
"@peerbit/blocks": "^2.0.1",
|
|
48
|
+
"@peerbit/crypto": "2.0.0",
|
|
49
|
+
"@peerbit/logger": "1.0.2",
|
|
50
|
+
"@peerbit/program": "3.0.1",
|
|
51
|
+
"@peerbit/pubsub": "^2.0.1",
|
|
50
52
|
"@peerbit/uint8arrays": "3.0.1",
|
|
51
|
-
"datastore-level": "^10.1.
|
|
53
|
+
"datastore-level": "^10.1.5",
|
|
52
54
|
"level": "^8.0.0",
|
|
53
55
|
"memory-level": "^1.0.0",
|
|
54
56
|
"path-browserify": "^1.0.1"
|
|
@@ -74,5 +76,5 @@
|
|
|
74
76
|
"p2p",
|
|
75
77
|
"peer-to-peer"
|
|
76
78
|
],
|
|
77
|
-
"gitHead": "
|
|
79
|
+
"gitHead": "c48cb37d237a25b0bcc849482b43f6941d53e3d5"
|
|
78
80
|
}
|
package/src/libp2p.ts
CHANGED
|
@@ -4,13 +4,15 @@ import { DirectBlock } from "@peerbit/blocks";
|
|
|
4
4
|
import { noise } from "@dao-xyz/libp2p-noise";
|
|
5
5
|
import { mplex } from "@libp2p/mplex";
|
|
6
6
|
import { transports, relay, listen } from "./transports.js";
|
|
7
|
-
import {
|
|
8
|
-
import { CircuitRelayService } from "libp2p/
|
|
7
|
+
import { identify } from "@libp2p/identify";
|
|
8
|
+
import { CircuitRelayService } from "@libp2p/circuit-relay-v2";
|
|
9
9
|
import { yamux } from "@chainsafe/libp2p-yamux";
|
|
10
|
+
import { DefaultKeychain, Keychain } from "@peerbit/keychain";
|
|
10
11
|
|
|
11
12
|
export type Libp2pExtendServices = {
|
|
12
13
|
pubsub: DirectSub;
|
|
13
14
|
blocks: DirectBlock;
|
|
15
|
+
keychain: Keychain;
|
|
14
16
|
};
|
|
15
17
|
export type Libp2pExtended = Libp2p<
|
|
16
18
|
{ relay: CircuitRelayService; identify: any } & Libp2pExtendServices
|
|
@@ -28,13 +30,14 @@ export const createLibp2pExtended = (
|
|
|
28
30
|
opts: Libp2pCreateOptions = {
|
|
29
31
|
services: {
|
|
30
32
|
blocks: (c) => new DirectBlock(c),
|
|
31
|
-
pubsub: (c) => new DirectSub(c)
|
|
33
|
+
pubsub: (c) => new DirectSub(c),
|
|
34
|
+
keychain: (c) => new DefaultKeychain()
|
|
32
35
|
}
|
|
33
36
|
}
|
|
34
37
|
): Promise<Libp2pExtended> => {
|
|
35
38
|
const relayIdentify = {
|
|
36
39
|
relay: relay(),
|
|
37
|
-
identify:
|
|
40
|
+
identify: identify()
|
|
38
41
|
};
|
|
39
42
|
|
|
40
43
|
// https://github.com/libp2p/js-libp2p/issues/1757
|
|
@@ -64,12 +67,12 @@ export const createLibp2pExtended = (
|
|
|
64
67
|
((c) =>
|
|
65
68
|
new DirectSub(c, {
|
|
66
69
|
canRelayMessage: true,
|
|
67
|
-
signaturePolicy: "StrictNoSign"
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
70
|
+
signaturePolicy: "StrictNoSign"
|
|
71
|
+
// auto dial true
|
|
72
|
+
// auto prune true
|
|
71
73
|
})),
|
|
72
74
|
blocks: opts.services?.blocks || ((c) => new DirectBlock(c)),
|
|
75
|
+
keychain: opts.services?.keychain || ((c) => new DefaultKeychain()),
|
|
73
76
|
...opts.services
|
|
74
77
|
}
|
|
75
78
|
});
|
package/src/peer.ts
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { AbstractLevel } from "abstract-level";
|
|
3
|
-
import { Level } from "level";
|
|
4
|
-
import { MemoryLevel } from "memory-level";
|
|
1
|
+
import { AnyStore } from "../../../utils/any-store/lib/esm/index.js";
|
|
5
2
|
import { multiaddr, Multiaddr, isMultiaddr } from "@multiformats/multiaddr";
|
|
6
3
|
import type { Libp2p } from "libp2p";
|
|
7
|
-
import {
|
|
8
|
-
Ed25519Keypair,
|
|
9
|
-
Ed25519PublicKey,
|
|
10
|
-
Libp2pKeychain
|
|
11
|
-
} from "@peerbit/crypto";
|
|
4
|
+
import { Ed25519Keypair, Ed25519PublicKey } from "@peerbit/crypto";
|
|
12
5
|
import {
|
|
13
6
|
Program,
|
|
14
7
|
Address,
|
|
@@ -20,7 +13,6 @@ import sodium from "libsodium-wrappers";
|
|
|
20
13
|
import path from "path-browserify";
|
|
21
14
|
import { waitFor } from "@peerbit/time";
|
|
22
15
|
import "@libp2p/peer-id";
|
|
23
|
-
import { Cache } from "@peerbit/cache";
|
|
24
16
|
|
|
25
17
|
import {
|
|
26
18
|
createLibp2pExtended,
|
|
@@ -29,10 +21,11 @@ import {
|
|
|
29
21
|
} from "./libp2p.js";
|
|
30
22
|
import { DirectBlock } from "@peerbit/blocks";
|
|
31
23
|
import { LevelDatastore } from "datastore-level";
|
|
32
|
-
import { BinaryWriter } from "@dao-xyz/borsh";
|
|
33
24
|
import { logger as loggerFn } from "@peerbit/logger";
|
|
34
25
|
import { OpenOptions } from "@peerbit/program";
|
|
35
26
|
import { resolveBootstrapAddresses } from "./bootstrap.js";
|
|
27
|
+
import { createStore } from "@peerbit/any-store";
|
|
28
|
+
import { DefaultKeychain } from "@peerbit/keychain";
|
|
36
29
|
|
|
37
30
|
export const logger = loggerFn({ module: "client" });
|
|
38
31
|
|
|
@@ -41,55 +34,47 @@ export type OptionalCreateOptions = {
|
|
|
41
34
|
};
|
|
42
35
|
export type CreateOptions = {
|
|
43
36
|
directory?: string;
|
|
44
|
-
|
|
37
|
+
memory: AnyStore;
|
|
45
38
|
identity: Ed25519Keypair;
|
|
46
|
-
keychain: Libp2pKeychain;
|
|
47
39
|
} & OptionalCreateOptions;
|
|
48
|
-
|
|
40
|
+
type ExtractArgs<T> = T extends Program<infer Args> ? Args : never;
|
|
49
41
|
type Libp2pOptions = { libp2p?: Libp2pExtended | ClientCreateOptions };
|
|
50
42
|
type SimpleLibp2pOptions = { relay?: boolean };
|
|
51
43
|
export type CreateInstanceOptions = (SimpleLibp2pOptions | Libp2pOptions) & {
|
|
52
44
|
directory?: string;
|
|
53
|
-
cache?: LazyLevel;
|
|
54
45
|
} & OptionalCreateOptions;
|
|
55
46
|
|
|
56
47
|
const isLibp2pInstance = (libp2p: Libp2pExtended | ClientCreateOptions) =>
|
|
57
48
|
!!(libp2p as Libp2p).getMultiaddrs;
|
|
58
49
|
|
|
59
|
-
const createLevel = (path?: string): AbstractLevel<any, string, Uint8Array> => {
|
|
60
|
-
return path
|
|
61
|
-
? new Level(path, { valueEncoding: "view" })
|
|
62
|
-
: new MemoryLevel({ valueEncoding: "view" });
|
|
63
|
-
};
|
|
64
|
-
|
|
65
50
|
const createCache = async (
|
|
66
51
|
directory: string | undefined,
|
|
67
52
|
options?: { reset?: boolean }
|
|
68
53
|
) => {
|
|
69
|
-
const cache =
|
|
54
|
+
const cache = createStore(directory);
|
|
70
55
|
|
|
71
56
|
// "Wake up" the caches if they need it
|
|
72
57
|
if (cache) await cache.open();
|
|
73
58
|
if (options?.reset) {
|
|
74
|
-
await cache
|
|
59
|
+
await cache.clear();
|
|
75
60
|
}
|
|
76
|
-
|
|
77
61
|
return cache;
|
|
78
62
|
};
|
|
79
63
|
|
|
64
|
+
const SELF_IDENTITY_KEY_ID = new TextEncoder().encode("__self__");
|
|
65
|
+
|
|
80
66
|
export class Peerbit implements ProgramClient {
|
|
81
67
|
_libp2p: Libp2pExtended;
|
|
82
68
|
|
|
83
69
|
directory?: string;
|
|
84
70
|
|
|
85
|
-
private
|
|
71
|
+
private _memory: AnyStore;
|
|
86
72
|
private _libp2pExternal?: boolean = false;
|
|
87
73
|
|
|
88
74
|
// Libp2p peerid in Identity form
|
|
89
75
|
private _identity: Ed25519Keypair;
|
|
90
|
-
|
|
91
|
-
private _keychain: Libp2pKeychain; // Keychain + Caching + X25519 keys
|
|
92
76
|
private _handler: ProgramHandler;
|
|
77
|
+
|
|
93
78
|
constructor(libp2p: Libp2pExtended, options: CreateOptions) {
|
|
94
79
|
if (libp2p == null) {
|
|
95
80
|
throw new Error("Libp2p required");
|
|
@@ -107,10 +92,8 @@ export class Peerbit implements ProgramClient {
|
|
|
107
92
|
}
|
|
108
93
|
|
|
109
94
|
this._identity = options.identity;
|
|
110
|
-
this._keychain = options.keychain;
|
|
111
|
-
|
|
112
95
|
this.directory = options.directory;
|
|
113
|
-
this.
|
|
96
|
+
this._memory = options.memory;
|
|
114
97
|
this._libp2pExternal = options.libp2pExternal;
|
|
115
98
|
}
|
|
116
99
|
|
|
@@ -121,52 +104,58 @@ export class Peerbit implements ProgramClient {
|
|
|
121
104
|
.libp2p as Libp2pExtended;
|
|
122
105
|
const asRelay = (options as SimpleLibp2pOptions).relay;
|
|
123
106
|
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
107
|
+
const directory = options.directory;
|
|
108
|
+
const hasDir = directory != null;
|
|
109
|
+
|
|
110
|
+
const memory = await createCache(
|
|
111
|
+
directory != null ? path.join(directory, "/cache") : undefined
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
const blocksDirectory = hasDir
|
|
115
|
+
? path.join(options["directory"], "/blocks").toString()
|
|
116
|
+
: undefined;
|
|
117
|
+
|
|
118
|
+
const keychainDirectory = hasDir
|
|
119
|
+
? path.join(options["directory"], "/keychain").toString()
|
|
120
|
+
: undefined;
|
|
121
|
+
|
|
122
|
+
const datastore = hasDir
|
|
123
|
+
? new LevelDatastore(
|
|
124
|
+
path.join(options["directory"], "/libp2p").toString()
|
|
125
|
+
)
|
|
126
|
+
: undefined;
|
|
129
127
|
|
|
130
|
-
const datastore =
|
|
131
|
-
options.directory != null
|
|
132
|
-
? new LevelDatastore(path.join(options.directory, "/libp2p").toString())
|
|
133
|
-
: undefined;
|
|
134
128
|
if (datastore) {
|
|
135
129
|
await datastore.open();
|
|
136
130
|
}
|
|
137
131
|
|
|
138
|
-
|
|
132
|
+
const libp2pExternal = libp2pExtended && isLibp2pInstance(libp2pExtended);
|
|
133
|
+
if (!libp2pExternal) {
|
|
134
|
+
const extendedOptions: ClientCreateOptions | undefined =
|
|
135
|
+
libp2pExtended as any as ClientCreateOptions;
|
|
136
|
+
const keychain = new DefaultKeychain({
|
|
137
|
+
store: createStore(keychainDirectory)
|
|
138
|
+
});
|
|
139
|
+
const peerId =
|
|
140
|
+
extendedOptions?.peerId ||
|
|
141
|
+
(await (
|
|
142
|
+
await keychain.exportById(SELF_IDENTITY_KEY_ID, Ed25519Keypair)
|
|
143
|
+
)?.toPeerId());
|
|
139
144
|
libp2pExtended = await createLibp2pExtended({
|
|
145
|
+
...extendedOptions,
|
|
146
|
+
peerId,
|
|
140
147
|
services: {
|
|
148
|
+
keychain: (c) => keychain,
|
|
141
149
|
blocks: (c) =>
|
|
142
150
|
new DirectBlock(c, {
|
|
143
151
|
canRelayMessage: asRelay,
|
|
144
152
|
directory: blocksDirectory
|
|
145
153
|
}),
|
|
146
|
-
pubsub: (c) => new DirectSub(c, { canRelayMessage: asRelay })
|
|
154
|
+
pubsub: (c) => new DirectSub(c, { canRelayMessage: asRelay }),
|
|
155
|
+
...extendedOptions?.services
|
|
147
156
|
},
|
|
148
|
-
// If directory is passed, we store keys within that directory, else we will use memory datastore (which is the default behaviour)
|
|
149
157
|
datastore
|
|
150
158
|
});
|
|
151
|
-
} else {
|
|
152
|
-
if (isLibp2pInstance(libp2pExtended)) {
|
|
153
|
-
libp2pExternal = true; // libp2p was created outside
|
|
154
|
-
} else {
|
|
155
|
-
const extendedOptions = libp2pExtended as any as ClientCreateOptions;
|
|
156
|
-
libp2pExtended = await createLibp2pExtended({
|
|
157
|
-
...extendedOptions,
|
|
158
|
-
services: {
|
|
159
|
-
blocks: (c) =>
|
|
160
|
-
new DirectBlock(c, {
|
|
161
|
-
canRelayMessage: asRelay,
|
|
162
|
-
directory: blocksDirectory
|
|
163
|
-
}),
|
|
164
|
-
pubsub: (c) => new DirectSub(c, { canRelayMessage: asRelay }),
|
|
165
|
-
...extendedOptions?.services
|
|
166
|
-
},
|
|
167
|
-
datastore
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
159
|
}
|
|
171
160
|
if (datastore) {
|
|
172
161
|
const stopFn = libp2pExtended.stop.bind(libp2pExtended);
|
|
@@ -176,7 +165,10 @@ export class Peerbit implements ProgramClient {
|
|
|
176
165
|
};
|
|
177
166
|
}
|
|
178
167
|
|
|
179
|
-
if (
|
|
168
|
+
if (
|
|
169
|
+
libp2pExtended.status === "stopped" ||
|
|
170
|
+
libp2pExtended.status === "stopping"
|
|
171
|
+
) {
|
|
180
172
|
await libp2pExtended.start();
|
|
181
173
|
}
|
|
182
174
|
|
|
@@ -187,22 +179,12 @@ export class Peerbit implements ProgramClient {
|
|
|
187
179
|
);
|
|
188
180
|
}
|
|
189
181
|
|
|
190
|
-
const directory = options.directory;
|
|
191
|
-
const cache =
|
|
192
|
-
options.cache ||
|
|
193
|
-
(await createCache(
|
|
194
|
-
directory ? path.join(directory, "/cache") : undefined
|
|
195
|
-
));
|
|
196
|
-
|
|
197
182
|
const identity = Ed25519Keypair.fromPeerId(libp2pExtended.peerId);
|
|
198
|
-
const keychain = new Libp2pKeychain(libp2pExtended.keychain, {
|
|
199
|
-
cache: new Cache({ max: 1000 })
|
|
200
|
-
});
|
|
201
|
-
|
|
202
183
|
try {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
184
|
+
await libp2pExtended.services.keychain.import({
|
|
185
|
+
keypair: identity,
|
|
186
|
+
id: SELF_IDENTITY_KEY_ID
|
|
187
|
+
});
|
|
206
188
|
} catch (error: any) {
|
|
207
189
|
if (error.code == "ERR_KEY_ALREADY_EXISTS") {
|
|
208
190
|
// Do nothing
|
|
@@ -213,10 +195,9 @@ export class Peerbit implements ProgramClient {
|
|
|
213
195
|
|
|
214
196
|
const peer = new Peerbit(libp2pExtended, {
|
|
215
197
|
directory,
|
|
216
|
-
|
|
198
|
+
memory: memory,
|
|
217
199
|
libp2pExternal,
|
|
218
|
-
identity
|
|
219
|
-
keychain
|
|
200
|
+
identity
|
|
220
201
|
});
|
|
221
202
|
return peer;
|
|
222
203
|
}
|
|
@@ -236,10 +217,6 @@ export class Peerbit implements ProgramClient {
|
|
|
236
217
|
return this.libp2p.services;
|
|
237
218
|
}
|
|
238
219
|
|
|
239
|
-
get keychain(): Libp2pKeychain {
|
|
240
|
-
return this._keychain;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
220
|
get handler(): ProgramHandler {
|
|
244
221
|
return this._handler;
|
|
245
222
|
}
|
|
@@ -257,8 +234,8 @@ export class Peerbit implements ProgramClient {
|
|
|
257
234
|
typeof address == "string"
|
|
258
235
|
? multiaddr(address)
|
|
259
236
|
: isMultiaddr(address) || Array.isArray(address)
|
|
260
|
-
|
|
261
|
-
|
|
237
|
+
? address
|
|
238
|
+
: address.getMultiaddrs();
|
|
262
239
|
const connection = await this.libp2p.dial(maddress);
|
|
263
240
|
const publicKey = Ed25519PublicKey.fromPeerId(connection.remotePeer);
|
|
264
241
|
|
|
@@ -273,16 +250,16 @@ export class Peerbit implements ProgramClient {
|
|
|
273
250
|
}
|
|
274
251
|
|
|
275
252
|
async start() {
|
|
276
|
-
await this.
|
|
253
|
+
await this._memory.open();
|
|
277
254
|
|
|
278
|
-
if (
|
|
255
|
+
if (this.libp2p.status === "stopped" || this.libp2p.status === "stopping") {
|
|
279
256
|
this._libp2pExternal = false; // this means we will also close libp2p client on close
|
|
280
257
|
return this.libp2p.start();
|
|
281
258
|
}
|
|
282
259
|
}
|
|
283
260
|
async stop() {
|
|
284
261
|
await this._handler?.stop();
|
|
285
|
-
await this.
|
|
262
|
+
await this._memory.close();
|
|
286
263
|
|
|
287
264
|
// Close libp2p (after above)
|
|
288
265
|
if (!this._libp2pExternal) {
|
|
@@ -307,9 +284,9 @@ export class Peerbit implements ProgramClient {
|
|
|
307
284
|
* @returns
|
|
308
285
|
*/
|
|
309
286
|
|
|
310
|
-
async open<S extends Program<
|
|
287
|
+
async open<S extends Program<ExtractArgs<S>>>(
|
|
311
288
|
storeOrAddress: S | Address | string,
|
|
312
|
-
options: OpenOptions<
|
|
289
|
+
options: OpenOptions<ExtractArgs<S>, S> = {}
|
|
313
290
|
): Promise<S> {
|
|
314
291
|
return (
|
|
315
292
|
this._handler || (this._handler = new ProgramHandler({ client: this }))
|
|
@@ -317,6 +294,6 @@ export class Peerbit implements ProgramClient {
|
|
|
317
294
|
}
|
|
318
295
|
|
|
319
296
|
get memory() {
|
|
320
|
-
return this.
|
|
297
|
+
return this._memory;
|
|
321
298
|
}
|
|
322
299
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { webSockets } from "@libp2p/websockets";
|
|
2
|
-
import { circuitRelayTransport } from "libp2p/circuit-relay";
|
|
2
|
+
import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
|
|
3
3
|
import { webRTC } from "@libp2p/webrtc";
|
|
4
4
|
import { all } from "@libp2p/websockets/filters";
|
|
5
5
|
|
package/src/transports.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { webSockets } from "@libp2p/websockets";
|
|
2
|
-
import { circuitRelayTransport } from "libp2p/circuit-relay";
|
|
2
|
+
import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
|
|
3
3
|
import { tcp } from "@libp2p/tcp";
|
|
4
|
-
import { circuitRelayServer } from "libp2p/circuit-relay";
|
|
4
|
+
import { circuitRelayServer } from "@libp2p/circuit-relay-v2";
|
|
5
5
|
import { all } from "@libp2p/websockets/filters";
|
|
6
6
|
export const transports = () => [
|
|
7
7
|
webSockets({ filter: all }),
|