@leofcoin/peernet 0.9.6 → 0.9.9

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.
@@ -93,7 +93,7 @@ class PeernetClient {
93
93
  this.topic = Buffer.from(sha256__default['default'](`${options.networkName}-${options.networkVersion}`).toString());
94
94
 
95
95
  const trackers = [
96
- 'wss://star.leofcoin.org:7575',
96
+ 'wss://star.leofcoin.org',
97
97
  'wss://tracker.openwebtorrent.com',
98
98
  // 'wss://tracker.sloppyta.co:443/announce',
99
99
  ];
@@ -261,7 +261,7 @@ class LeofcoinStorage$1 {
261
261
 
262
262
  }
263
263
 
264
- var version = "0.9.4";
264
+ var version = "0.9.7";
265
265
 
266
266
  var api$1 = {
267
267
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -1818,17 +1818,13 @@ class DhtEarth {
1818
1818
  const peerLoc = await this.getCoordinates(address);
1819
1819
 
1820
1820
  for (const provider of providers) {
1821
- all.push(this.getDistance(peerLoc, provider));
1821
+ if (provider.address === '127.0.0.1') all.push({provider, distance: 0});
1822
+ else all.push(this.getDistance(peerLoc, provider));
1822
1823
  }
1823
1824
 
1824
1825
  all = await Promise.all(all);
1825
-
1826
- const closestPeer = all.reduce((p, c) => {
1827
- if (!c.distance || c.distance === NaN) c.distance = 0;
1828
- if (c.distance < p || p === 0) return c.provider;
1829
- }, 0);
1830
-
1831
- return closestPeer;
1826
+ all = all.sort((previous, current) => previous.distance - current.distance);
1827
+ return all[0].provider;
1832
1828
  }
1833
1829
 
1834
1830
  /**
@@ -1956,656 +1952,656 @@ const nothingFoundError = (hash) => {
1956
1952
  return new Error(`nothing found for ${hash}`)
1957
1953
  };
1958
1954
 
1959
- globalThis.leofcoin = globalThis.leofcoin || {};
1960
- globalThis.globalSub = globalThis.globalSub || new PubSub__default['default']({verbose: true});
1961
-
1962
- /**
1963
- * @access public
1964
- * @example
1965
- * const peernet = new Peernet();
1966
- */
1967
- class Peernet {
1968
- /**
1969
- * @access public
1970
- * @param {Object} options
1971
- * @param {String} options.network - desired network
1972
- * @param {String} options.root - path to root directory
1973
- * @param {String} options.storePrefix - prefix for datatores (lfc)
1974
- *
1975
- * @return {Promise} instance of Peernet
1976
- *
1977
- * @example
1978
- * const peernet = new Peernet({network: 'leofcoin', root: '.leofcoin'});
1979
- */
1980
- constructor(options = {}) {
1981
- this._discovered = [];
1982
- /**
1983
- * @property {String} network - current network
1984
- */
1985
- this.network = options.network || 'leofcoin';
1986
- const parts = this.network.split(':');
1987
-
1988
- if (!options.storePrefix) options.storePrefix = 'lfc';
1989
- if (!options.port) options.port = 2000;
1990
- if (!options.root) {
1991
- if (parts[1]) options.root = `.${parts[0]}/peernet/${parts[1]}`;
1992
- else options.root = `.${this.network}/peernet`;
1993
- }
1994
- globalThis.peernet = this;
1995
- return this._init(options)
1996
- }
1997
-
1998
- get defaultStores() {
1999
- return ['account', 'wallet', 'block', 'transaction', 'chain', 'data', 'message']
2000
- }
2001
-
2002
- addProto(name, proto) {
2003
- if (!this.protos[name]) this.protos[name] = proto;
2004
- }
2005
-
2006
- addCodec(name, codec) {
2007
- if (!this.codecs[name]) this.codecs[name] = codec;
2008
- }
2009
-
2010
- async addStore(name, prefix, root, isPrivate = true) {
2011
- if (name === 'block' || name === 'transaction' || name === 'chain' ||
2012
- name === 'data' || name === 'message') isPrivate = false;
2013
-
2014
- let Storage;
2015
- if (this.hasDaemon) {
2016
- Storage = LeofcoinStorageClient;
2017
- } else {
2018
- Storage = LeofcoinStorage$1;
2019
- }
2020
- globalThis[`${name}Store`] = globalThis[`${name}Store`] ||
2021
- await new Storage(`${prefix}-${name}`, root);
2022
-
2023
- globalThis[`${name}Store`].private = isPrivate;
2024
- if (!isPrivate) this.stores.push(name);
2025
- }
2026
-
2027
-
2028
- /**
2029
- * @see MessageHandler
2030
- */
2031
- prepareMessage(to, data) {
2032
- return this._messageHandler.prepareMessage(this.id, to, data)
2033
- }
2034
-
2035
- /**
2036
- * @access public
2037
- *
2038
- * @return {Array} peerId
2039
- */
2040
- get peers() {
2041
- return [...connections.values()]
2042
- }
2043
-
2044
- /**
2045
- * @private
2046
- *
2047
- * @param {Object} options
2048
- * @param {String} options.root - path to root directory
2049
- *
2050
- * @return {Promise} instance of Peernet
2051
- */
2052
- async _init(options) {
2053
- // peernetDHT aka closesPeer by coordinates
2054
- /**
2055
- * @type {Object}
2056
- * @property {Object} peer Instance of Peer
2057
- */
2058
- this.dht = new DhtEarth();
2059
- /**
2060
- * @type {Map}
2061
- * @property {Object} peer Instance of Peer
2062
- */
2063
- this.peerMap = new Map();
2064
- this.stores = [];
2065
- this.requestProtos = {};
2066
- this.storePrefix = options.storePrefix;
2067
- this.root = options.root;
2068
-
2069
- /**
2070
- * proto Object containing protos
2071
- * @type {Object}
2072
- * @property {PeernetMessage} protos[peernet-message] messageNode
2073
- * @property {DHTMessage} protos[peernet-dht] messageNode
2074
- * @property {DHTMessageResponse} protos[peernet-dht-response] messageNode
2075
- * @property {DataMessage} protos[peernet-data] messageNode
2076
- * @property {DataMessageResponse} protos[peernet-data-response] messageNode
2077
- */
2078
- globalThis.peernet.protos = {
2079
- 'peernet-request': RequestMessage,
2080
- 'peernet-response': ResponseMessage,
2081
- 'peernet-peer': PeerMessage,
2082
- 'peernet-peer-response': PeerMessageResponse,
2083
- 'peernet-message': PeernetMessage,
2084
- 'peernet-dht': DHTMessage,
2085
- 'peernet-dht-response': DHTMessageResponse,
2086
- 'peernet-data': DataMessage,
2087
- 'peernet-data-response': DataMessageResponse,
2088
- 'peernet-ps': PsMessage,
2089
- 'chat-message': ChatMessage,
2090
- };
2091
-
2092
- this.protos = globalThis.peernet.protos;
2093
- this.codecs = codecs;
2094
-
2095
- this._messageHandler = new MessageHandler(this.network);
2096
-
2097
- const {daemon, environment} = await target();
2098
- this.hasDaemon = daemon;
2099
-
2100
- if (this.hasDaemon) {
2101
- globalThis.peernet.client = await httpClient({
2102
- protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port,
2103
- });
2104
- } else {
2105
- if (environment !== 'browser') http(options);
2106
- }
2107
-
2108
- for (const store of this.defaultStores) {
2109
- await this.addStore(store, options.storePrefix, options.root);
2110
- }
2111
-
2112
- try {
2113
- const pub = await accountStore.get('public');
2114
- this.id = pub.walletId;
2115
- } catch (e) {
2116
- if (e.code === 'ERR_NOT_FOUND') {
2117
- const wallet = {};
2118
- const {identity, accounts, config} = await generateAccount(this.network);
2119
- wallet.identity = identity;
2120
- wallet.accounts = accounts;
2121
- wallet.version = 1;
2122
- walletStore.put(wallet);
2123
- await accountStore.put('config', config);
2124
- await accountStore.put('public', {walletId: wallet.identity.walletId});
2125
-
2126
- this.id = wallet.identity.walletId;
2127
- } else {
2128
- throw e
2129
- }
2130
- }
2131
- this._peerHandler = new PeerDiscovery(this.id);
2132
- // peernet id
2133
- const id = Buffer.from(this.id.slice(0, 32));
2134
- this.peerId = id;
2135
-
2136
- pubsub.subscribe('peer:discovered', async (peer) => {
2137
- this._peerHandler.discover(peer);
2138
- peer.on('peernet.data', async (message) => {
2139
- const id = message.id;
2140
- message = new PeernetMessage(Buffer.from(message.data.data));
2141
- const proto = protoFor(message.decoded.data);
2142
- await this._protoHandler({id, proto}, peer);
2143
- const fulldId = this._getPeerId(peer.id);
2144
- if (fulldId && this._discovered.indexOf(peer.id) === -1) {
2145
- this._discovered.push(peer.id);
2146
- pubsub.publish('peer:connected', peer);
2147
- }
2148
- });
2149
- });
2150
- pubsub.subscribe('peer:disconnected', async (peer) => {
2151
- let index = this._discovered.indexOf(peer.id);
2152
- if (index !== -1) this._discovered.splice(index, 1);
2153
- const id = this._getPeerId(peer.id);
2154
- let peerIds = this.peerMap.get(id);
2155
-
2156
- if (peerIds) {
2157
- index = peerIds.indexOf(peer.id);
2158
- if (index !== -1) peerIds.splice(index, 1);
2159
- } else {
2160
- peerIds = [];
2161
- }
2162
-
2163
- if (peerIds.length === 0) this.peerMap.delete(id);
2164
- else this.peerMap.set(id, peerIds);
2165
- });
2166
- pubsub.subscribe('peer:connected', async (peer) => {
2167
- console.log({connected: peer.id, as: this._getPeerId(peer.id) });
2168
- // peer.on('peernet.data', async (message) => {
2169
- // const id = message.id
2170
- // message = new PeernetMessage(Buffer.from(message.data.data))
2171
- // const proto = protoFor(message.decoded.data)
2172
- // this._protoHandler({id, proto}, peer)
2173
- // })
2174
- });
2175
-
2176
- /**
2177
- * @access public
2178
- * @type {PeernetClient}
2179
- */
2180
- this.client = new PeernetClient({...options, id});
2181
- return this
2182
- }
2183
-
2184
- _getPeerId(id) {
2185
- for (const entry of [...this.peerMap.entries()]) {
2186
- for (const _id of entry[1]) {
2187
- if (_id === id) return entry[0]
2188
- }
2189
- }
2190
- }
2191
-
2192
- addRequestHandler(name, method) {
2193
- this.requestProtos[name] = method;
2194
- }
2195
-
2196
- /**
2197
- * @private
2198
- *
2199
- * @param {Buffer} message - peernet message
2200
- * @param {PeernetPeer} peer - peernet peer
2201
- */
2202
- async _protoHandler(message, peer) {
2203
- const {id, proto} = message;
2204
- if (proto.name === 'peernet-peer') {
2205
- const from = proto.decoded.id;
2206
- if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2207
- else {
2208
- const connections = this.peerMap.get(from);
2209
- if (connections.indexOf(peer.id) === -1) {
2210
- connections.push(peer.id);
2211
- this.peerMap.set(from, connections);
2212
- }
2213
- }
2214
- const data = new PeerMessageResponse({id: this.id});
2215
- const node = await this.prepareMessage(from, data.encoded);
2216
-
2217
- peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2218
- } else if (proto.name === 'peernet-peer-response') {
2219
- const from = proto.decoded.id;
2220
- if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2221
- else {
2222
- const connections = this.peerMap.get(from);
2223
- if (connections.indexOf(peer.id) === -1) {
2224
- connections.push(peer.id);
2225
- this.peerMap.set(from, connections);
2226
- }
2227
- }
2228
- } else {
2229
- let from = this._getPeerId(peer.id);
2230
- if (!from) {
2231
- const data = new PeerMessage({id: this.id});
2232
- const node = await this.prepareMessage(peer.id, data.encoded);
2233
-
2234
- let response = await peer.request(node.encoded);
2235
- response = protoFor(response);
2236
- response = new PeerMessageResponse(response.decoded.data);
2237
-
2238
- from = response.decoded.id;
2239
- if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2240
- else {
2241
- const connections = this.peerMap.get(from);
2242
- if (connections.indexOf(peer.id) === -1) {
2243
- connections.push(peer.id);
2244
- this.peerMap.set(from, connections);
2245
- }
2246
- }
2247
- }
2248
- if (proto.name === 'peernet-dht') {
2249
- let { hash, store } = proto.decoded;
2250
- let has;
2251
-
2252
- if (!store) {
2253
- has = await this.has(hash);
2254
- } else {
2255
- store = globalThis[`${store}Store`];
2256
- if (store.private) has = false;
2257
- else has = await store.has(hash);
2258
- }
2259
- const data = new DHTMessageResponse({hash, has});
2260
- const node = await this.prepareMessage(from, data.encoded);
2261
-
2262
- peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2263
- } else if (proto.name === 'peernet-data') {
2264
- let { hash, store } = proto.decoded;
2265
- let data;
2266
-
2267
- if (!store) {
2268
- data = await this.get(hash);
2269
- } else {
2270
- store = globalThis[`${store}Store`];
2271
- if (store.private) {
2272
- // TODO: ban
2273
- return
2274
- } else data = await store.get(hash);
2275
- }
2276
-
2277
- if (data) {
2278
- data = new DataMessageResponse({hash, data: Buffer.from(data)});
2279
-
2280
- const node = await this.prepareMessage(from, data.encoded);
2281
- peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2282
- }
2283
- } else if (proto.name === 'peernet-peer') {
2284
- const from = proto.decoded.id;
2285
- if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2286
- else {
2287
- const connections = this.peerMap.get(from);
2288
- connections.push(peer.id);
2289
- this.peerMap.set(from, connections);
2290
- }
2291
- const data = new PeerMessage({id: this.id});
2292
- const node = await this.prepareMessage(from, data.encoded);
2293
-
2294
- peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2295
- } else if (proto.name === 'peernet-request') {
2296
- // TODO: make dynamic
2297
- // exposeddevapi[proto.decoded.request](proto.decoded.params)
2298
- const method = this.requestProtos[proto.decoded.request];
2299
- if (method) {
2300
- const data = await method();
2301
- const node = await this.prepareMessage(from, data.encoded);
2302
- peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2303
- }
2304
- } else if (proto.name === 'peernet-ps' &&
2305
- this._getPeerId(peer.id) !== this.id.toString()) {
2306
- globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString());
2307
- }
2308
- }
2309
- }
2310
-
2311
- /**
2312
- * performs a walk and resolves first encounter
2313
- *
2314
- * @param {String} hash
2315
- */
2316
- async walk(hash) {
2317
- if (!hash) throw new Error('hash expected, received undefined')
2318
- const data = new DHTMessage({hash});
2319
- this.client.id;
2320
- for (const peer of this.peers) {
2321
- const node = await this.prepareMessage(peer.id, data.encoded);
2322
-
2323
- const result = await peer.request(node.encoded);
2324
-
2325
- let proto = protoFor(result.data);
2326
-
2327
- if (proto.name !== 'peernet-message') throw encapsulatedError()
2328
- const from = proto.decoded.from;
2329
- proto = protoFor(proto.decoded.data);
2330
-
2331
- if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
2332
-
2333
- // TODO: give ip and port (just used for location)
2334
- if (!peer.connection.remoteAddress || !peer.connection.localAddress) {
2335
- peer.connection.remoteFamily = 'ipv4';
2336
- peer.connection.remoteAddress = '127.0.0.1';
2337
- peer.connection.remotePort = '0000';
2338
- }
2339
-
2340
- const peerInfo = {
2341
- family: peer.connection.remoteFamily || peer.connection.localFamily,
2342
- address: peer.connection.remoteAddress || peer.connection.localAddress,
2343
- port: peer.connection.remotePort || peer.connection.localPort,
2344
- id: from,
2345
- };
2346
-
2347
- if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
2348
- }
2349
- return
2350
- }
2351
-
2352
- /**
2353
- * Override DHT behavior, try's finding the content three times
2354
- *
2355
- * @param {String} hash
2356
- */
2357
- async providersFor(hash) {
2358
- let providers = await this.dht.providersFor(hash);
2359
- // walk the network to find a provider
2360
- if (!providers || providers.length === 0) {
2361
- await this.walk(hash);
2362
- providers = await this.dht.providersFor(hash);
2363
- // second walk the network to find a provider
2364
- if (!providers || providers.length === 0) {
2365
- await this.walk(hash);
2366
- providers = await this.dht.providersFor(hash);
2367
- }
2368
- // last walk
2369
- if (!providers || providers.length === 0) {
2370
- await this.walk(hash);
2371
- providers = await this.dht.providersFor(hash);
2372
- }
2373
- }
2374
- // undefined if no providers given
2375
- return providers
2376
- }
2377
-
2378
- get block() {
2379
- return {
2380
- get: async (hash) => {
2381
- const data = await blockStore.has(hash);
2382
- if (data) return await blockStore.get(hash)
2383
- return this.requestData(hash)
2384
- },
2385
- put: async (hash, data) => {
2386
- if (await blockStore.has(hash)) return
2387
- return await blockStore.put(hash, data)
2388
- },
2389
- has: async (hash) => await blockStore.has(hash, 'block'),
2390
- }
2391
- }
2392
-
2393
- get transaction() {
2394
- return {
2395
- get: async (hash) => {
2396
- const data = await transactionStore.has(hash);
2397
- if (data) return await transactionStore.get(hash)
2398
- return this.requestData(hash, 'transaction')
2399
- },
2400
- put: async (hash, data) => {
2401
- if (await transactionStore.has(hash)) return
2402
- return await transactionStore.put(hash, data)
2403
- },
2404
- has: async (hash) => await transactionStore.has(hash),
2405
- }
2406
- }
2407
-
2408
- async requestData(hash, store) {
2409
- const providers = await this.providersFor(hash);
2410
- if (!providers || providers.size === 0) throw nothingFoundError(hash)
2411
- debug(`found ${providers.size} provider(s) for ${hash}`);
2412
- // get closest peer on earth
2413
- const closestPeer = await this.dht.closestPeer(providers);
2414
- // get peer instance by id
2415
- if (!closestPeer || !closestPeer.id) return this.requestData(hash, store)
2416
-
2417
- const id = closestPeer.id.toString();
2418
- if (this.peers) {
2419
- let closest = this.peers.filter((peer) => {
2420
- if (this._getPeerId(peer.id) === id) return peer
2421
- });
2422
-
2423
- let data = new DataMessage({hash, store});
2424
-
2425
- const node = await this.prepareMessage(id, data.encoded);
2426
- if (closest[0]) data = await closest[0].request(node.encoded);
2427
- else {
2428
- closest = this.peers.filter((peer) => {
2429
- if (peer.id.toString() === id) return peer
2430
- });
2431
- if (closest[0]) data = await closest[0].request(node.encoded);
2432
- }
2433
- if (data.data) {
2434
- let proto = protoFor(Buffer.from(data.data));
2435
- proto = protoFor(proto.decoded.data);
2436
- return proto.decoded.data
2437
- }
2438
-
2439
- // this.put(hash, proto.decoded.data)
2440
- }
2441
- return null
2442
- }
2443
-
2444
-
2445
- get message() {
2446
- return {
2447
- /**
2448
- * Get content for given message hash
2449
- *
2450
- * @param {String} hash
2451
- */
2452
- get: async (hash) => {
2453
- debug(`get message ${hash}`);
2454
- const message = await messageStore.has(hash);
2455
- if (message) return await messageStore.get(hash)
2456
- return this.requestData(hash, 'message')
2457
- },
2458
- /**
2459
- * put message content
2460
- *
2461
- * @param {String} hash
2462
- * @param {Buffer} message
2463
- */
2464
- put: async (hash, message) => await messageStore.put(hash, message),
2465
- /**
2466
- * @param {String} hash
2467
- * @return {Boolean}
2468
- */
2469
- has: async (hash) => await messageStore.has(hash),
2470
- }
2471
- }
2472
-
2473
- get data() {
2474
- return {
2475
- /**
2476
- * Get content for given data hash
2477
- *
2478
- * @param {String} hash
2479
- */
2480
- get: async (hash) => {
2481
- debug(`get data ${hash}`);
2482
- const data = await dataStore.has(hash);
2483
- if (data) return await dataStore.get(hash)
2484
- return this.requestData(hash, 'data')
2485
- },
2486
- /**
2487
- * put data content
2488
- *
2489
- * @param {String} hash
2490
- * @param {Buffer} data
2491
- */
2492
- put: async (hash, data) => await dataStore.put(hash, data),
2493
- /**
2494
- * @param {String} hash
2495
- * @return {Boolean}
2496
- */
2497
- has: async (hash) => await dataStore.has(hash),
2498
- }
2499
- }
2500
-
2501
- /**
2502
- * goes trough given stores and tries to find data for given hash
2503
- * @param {Array} stores
2504
- * @param {string} hash
2505
- */
2506
- async whichStore(stores, hash) {
2507
- let store = stores.pop();
2508
- store = globalThis[`${store}Store`];
2509
- if (store) {
2510
- const has = await store.has(hash);
2511
- if (has) return store
2512
- if (stores.length > 0) return this.whichStore(stores, hash)
2513
- } else return null
2514
- }
2515
-
2516
- /**
2517
- * Get content for given hash
2518
- *
2519
- * @param {String} hash
2520
- */
2521
- async get(hash, store) {
2522
- debug(`get ${hash}`);
2523
- let data;
2524
- if (store) store = globalThis[`${store}Store`];
2525
- if (!store) store = await this.whichStore([...this.stores], hash);
2526
- if (store && await store.has(hash)) data = await store.get(hash);
2527
- if (data) return data
2528
-
2529
- return this.requestData(hash, 'data')
2530
- }
2531
-
2532
- /**
2533
- * put content
2534
- *
2535
- * @param {String} hash
2536
- * @param {Buffer} data
2537
- */
2538
- async put(hash, data, store = 'data') {
2539
- store = globalThis[`${store}Store`];
2540
- return await store.put(hash, data)
2541
- }
2542
-
2543
- /**
2544
- * @param {String} hash
2545
- * @return {Boolean}
2546
- */
2547
- async has(hash) {
2548
- const store = await this.whichStore([...this.stores], hash);
2549
- if (store) {
2550
- if (store.private) return false
2551
- else return true
2552
- }
2553
- return false
2554
- }
2555
-
2556
- /**
2557
- *
2558
- * @param {String} topic
2559
- * @param {String|Object|Array|Boolean|Buffer} data
2560
- */
2561
- async publish(topic, data) {
2562
- // globalSub.publish(topic, data)
2563
- if (!Buffer.isBuffer(topic)) topic = Buffer.from(topic);
2564
- if (!Buffer.isBuffer(data)) data = Buffer.from(data);
2565
- const id = Math.random().toString(36).slice(-12);
2566
- data = new PsMessage({data, topic});
2567
- for (const peer of this.peers) {
2568
- if (peer.connection._connected) {
2569
- if (peer.id.toString() !== this.peerId.toString()) {
2570
- const node = await this.prepareMessage(peer.id, data.encoded);
2571
- peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2572
- }
2573
- } else {
2574
- this.removePeer(peer);
2575
- }
2576
- // TODO: if peer subscribed
2577
- }
2578
- }
2579
-
2580
- createHash(data, name) {
2581
- return new PeernetHash(data, {name})
2582
- }
2583
-
2584
- /**
2585
- *
2586
- * @param {String} topic
2587
- * @param {Method} cb
2588
- */
2589
- async subscribe(topic, cb) {
2590
- // TODO: if peer subscribed
2591
- globalSub.subscribe(topic, cb);
2592
- }
2593
-
2594
- async removePeer(peer) {
2595
- connections.delete(peer.id);
2596
- }
2597
-
2598
- get Buffer() {
2599
- return Buffer
2600
- }
2601
- // async block(index) {
2602
- // const _values = []
2603
- // for (const peer of this.peers) {
2604
- // const value = await peer.request({type: 'block', index})
2605
- // console.log(value);
2606
- // }
2607
- //
2608
- // }
1955
+ globalThis.leofcoin = globalThis.leofcoin || {};
1956
+ globalThis.globalSub = globalThis.globalSub || new PubSub__default['default']({verbose: true});
1957
+
1958
+ /**
1959
+ * @access public
1960
+ * @example
1961
+ * const peernet = new Peernet();
1962
+ */
1963
+ class Peernet {
1964
+ /**
1965
+ * @access public
1966
+ * @param {Object} options
1967
+ * @param {String} options.network - desired network
1968
+ * @param {String} options.root - path to root directory
1969
+ * @param {String} options.storePrefix - prefix for datatores (lfc)
1970
+ *
1971
+ * @return {Promise} instance of Peernet
1972
+ *
1973
+ * @example
1974
+ * const peernet = new Peernet({network: 'leofcoin', root: '.leofcoin'});
1975
+ */
1976
+ constructor(options = {}) {
1977
+ this._discovered = [];
1978
+ /**
1979
+ * @property {String} network - current network
1980
+ */
1981
+ this.network = options.network || 'leofcoin';
1982
+ const parts = this.network.split(':');
1983
+
1984
+ if (!options.storePrefix) options.storePrefix = 'lfc';
1985
+ if (!options.port) options.port = 2000;
1986
+ if (!options.root) {
1987
+ if (parts[1]) options.root = `.${parts[0]}/peernet/${parts[1]}`;
1988
+ else options.root = `.${this.network}/peernet`;
1989
+ }
1990
+ globalThis.peernet = this;
1991
+ return this._init(options)
1992
+ }
1993
+
1994
+ get defaultStores() {
1995
+ return ['account', 'wallet', 'block', 'transaction', 'chain', 'data', 'message']
1996
+ }
1997
+
1998
+ addProto(name, proto) {
1999
+ if (!this.protos[name]) this.protos[name] = proto;
2000
+ }
2001
+
2002
+ addCodec(name, codec) {
2003
+ if (!this.codecs[name]) this.codecs[name] = codec;
2004
+ }
2005
+
2006
+ async addStore(name, prefix, root, isPrivate = true) {
2007
+ if (name === 'block' || name === 'transaction' || name === 'chain' ||
2008
+ name === 'data' || name === 'message') isPrivate = false;
2009
+
2010
+ let Storage;
2011
+ if (this.hasDaemon) {
2012
+ Storage = LeofcoinStorageClient;
2013
+ } else {
2014
+ Storage = LeofcoinStorage$1;
2015
+ }
2016
+ globalThis[`${name}Store`] = globalThis[`${name}Store`] ||
2017
+ await new Storage(`${prefix}-${name}`, root);
2018
+
2019
+ globalThis[`${name}Store`].private = isPrivate;
2020
+ if (!isPrivate) this.stores.push(name);
2021
+ }
2022
+
2023
+
2024
+ /**
2025
+ * @see MessageHandler
2026
+ */
2027
+ prepareMessage(to, data) {
2028
+ return this._messageHandler.prepareMessage(this.id, to, data)
2029
+ }
2030
+
2031
+ /**
2032
+ * @access public
2033
+ *
2034
+ * @return {Array} peerId
2035
+ */
2036
+ get peers() {
2037
+ return [...connections.values()]
2038
+ }
2039
+
2040
+ /**
2041
+ * @private
2042
+ *
2043
+ * @param {Object} options
2044
+ * @param {String} options.root - path to root directory
2045
+ *
2046
+ * @return {Promise} instance of Peernet
2047
+ */
2048
+ async _init(options) {
2049
+ // peernetDHT aka closesPeer by coordinates
2050
+ /**
2051
+ * @type {Object}
2052
+ * @property {Object} peer Instance of Peer
2053
+ */
2054
+ this.dht = new DhtEarth();
2055
+ /**
2056
+ * @type {Map}
2057
+ * @property {Object} peer Instance of Peer
2058
+ */
2059
+ this.peerMap = new Map();
2060
+ this.stores = [];
2061
+ this.requestProtos = {};
2062
+ this.storePrefix = options.storePrefix;
2063
+ this.root = options.root;
2064
+
2065
+ /**
2066
+ * proto Object containing protos
2067
+ * @type {Object}
2068
+ * @property {PeernetMessage} protos[peernet-message] messageNode
2069
+ * @property {DHTMessage} protos[peernet-dht] messageNode
2070
+ * @property {DHTMessageResponse} protos[peernet-dht-response] messageNode
2071
+ * @property {DataMessage} protos[peernet-data] messageNode
2072
+ * @property {DataMessageResponse} protos[peernet-data-response] messageNode
2073
+ */
2074
+ globalThis.peernet.protos = {
2075
+ 'peernet-request': RequestMessage,
2076
+ 'peernet-response': ResponseMessage,
2077
+ 'peernet-peer': PeerMessage,
2078
+ 'peernet-peer-response': PeerMessageResponse,
2079
+ 'peernet-message': PeernetMessage,
2080
+ 'peernet-dht': DHTMessage,
2081
+ 'peernet-dht-response': DHTMessageResponse,
2082
+ 'peernet-data': DataMessage,
2083
+ 'peernet-data-response': DataMessageResponse,
2084
+ 'peernet-ps': PsMessage,
2085
+ 'chat-message': ChatMessage,
2086
+ };
2087
+
2088
+ this.protos = globalThis.peernet.protos;
2089
+ this.codecs = codecs;
2090
+
2091
+ this._messageHandler = new MessageHandler(this.network);
2092
+
2093
+ const {daemon, environment} = await target();
2094
+ this.hasDaemon = daemon;
2095
+
2096
+ if (this.hasDaemon) {
2097
+ globalThis.peernet.client = await httpClient({
2098
+ protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port,
2099
+ });
2100
+ } else {
2101
+ if (environment !== 'browser') http(options);
2102
+ }
2103
+
2104
+ for (const store of this.defaultStores) {
2105
+ await this.addStore(store, options.storePrefix, options.root);
2106
+ }
2107
+
2108
+ try {
2109
+ const pub = await accountStore.get('public');
2110
+ this.id = pub.walletId;
2111
+ } catch (e) {
2112
+ if (e.code === 'ERR_NOT_FOUND') {
2113
+ const wallet = {};
2114
+ const {identity, accounts, config} = await generateAccount(this.network);
2115
+ wallet.identity = identity;
2116
+ wallet.accounts = accounts;
2117
+ wallet.version = 1;
2118
+ walletStore.put(wallet);
2119
+ await accountStore.put('config', config);
2120
+ await accountStore.put('public', {walletId: wallet.identity.walletId});
2121
+
2122
+ this.id = wallet.identity.walletId;
2123
+ } else {
2124
+ throw e
2125
+ }
2126
+ }
2127
+ this._peerHandler = new PeerDiscovery(this.id);
2128
+ // peernet id
2129
+ const id = Buffer.from(this.id.slice(0, 32));
2130
+ this.peerId = id;
2131
+
2132
+ pubsub.subscribe('peer:discovered', async (peer) => {
2133
+ this._peerHandler.discover(peer);
2134
+ peer.on('peernet.data', async (message) => {
2135
+ const id = message.id;
2136
+ message = new PeernetMessage(Buffer.from(message.data.data));
2137
+ const proto = protoFor(message.decoded.data);
2138
+ await this._protoHandler({id, proto}, peer);
2139
+ const fulldId = this._getPeerId(peer.id);
2140
+ if (fulldId && this._discovered.indexOf(peer.id) === -1) {
2141
+ this._discovered.push(peer.id);
2142
+ pubsub.publish('peer:connected', peer);
2143
+ }
2144
+ });
2145
+ });
2146
+ pubsub.subscribe('peer:disconnected', async (peer) => {
2147
+ let index = this._discovered.indexOf(peer.id);
2148
+ if (index !== -1) this._discovered.splice(index, 1);
2149
+ const id = this._getPeerId(peer.id);
2150
+ let peerIds = this.peerMap.get(id);
2151
+
2152
+ if (peerIds) {
2153
+ index = peerIds.indexOf(peer.id);
2154
+ if (index !== -1) peerIds.splice(index, 1);
2155
+ } else {
2156
+ peerIds = [];
2157
+ }
2158
+
2159
+ if (peerIds.length === 0) this.peerMap.delete(id);
2160
+ else this.peerMap.set(id, peerIds);
2161
+ });
2162
+ pubsub.subscribe('peer:connected', async (peer) => {
2163
+ console.log({connected: peer.id, as: this._getPeerId(peer.id) });
2164
+ // peer.on('peernet.data', async (message) => {
2165
+ // const id = message.id
2166
+ // message = new PeernetMessage(Buffer.from(message.data.data))
2167
+ // const proto = protoFor(message.decoded.data)
2168
+ // this._protoHandler({id, proto}, peer)
2169
+ // })
2170
+ });
2171
+
2172
+ /**
2173
+ * @access public
2174
+ * @type {PeernetClient}
2175
+ */
2176
+ this.client = new PeernetClient({...options, id});
2177
+ return this
2178
+ }
2179
+
2180
+ _getPeerId(id) {
2181
+ for (const entry of [...this.peerMap.entries()]) {
2182
+ for (const _id of entry[1]) {
2183
+ if (_id === id) return entry[0]
2184
+ }
2185
+ }
2186
+ }
2187
+
2188
+ addRequestHandler(name, method) {
2189
+ this.requestProtos[name] = method;
2190
+ }
2191
+
2192
+ /**
2193
+ * @private
2194
+ *
2195
+ * @param {Buffer} message - peernet message
2196
+ * @param {PeernetPeer} peer - peernet peer
2197
+ */
2198
+ async _protoHandler(message, peer) {
2199
+ const {id, proto} = message;
2200
+ if (proto.name === 'peernet-peer') {
2201
+ const from = proto.decoded.id;
2202
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2203
+ else {
2204
+ const connections = this.peerMap.get(from);
2205
+ if (connections.indexOf(peer.id) === -1) {
2206
+ connections.push(peer.id);
2207
+ this.peerMap.set(from, connections);
2208
+ }
2209
+ }
2210
+ const data = new PeerMessageResponse({id: this.id});
2211
+ const node = await this.prepareMessage(from, data.encoded);
2212
+
2213
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2214
+ } else if (proto.name === 'peernet-peer-response') {
2215
+ const from = proto.decoded.id;
2216
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2217
+ else {
2218
+ const connections = this.peerMap.get(from);
2219
+ if (connections.indexOf(peer.id) === -1) {
2220
+ connections.push(peer.id);
2221
+ this.peerMap.set(from, connections);
2222
+ }
2223
+ }
2224
+ } else {
2225
+ let from = this._getPeerId(peer.id);
2226
+ if (!from) {
2227
+ const data = new PeerMessage({id: this.id});
2228
+ const node = await this.prepareMessage(peer.id, data.encoded);
2229
+
2230
+ let response = await peer.request(node.encoded);
2231
+ response = protoFor(response);
2232
+ response = new PeerMessageResponse(response.decoded.data);
2233
+
2234
+ from = response.decoded.id;
2235
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2236
+ else {
2237
+ const connections = this.peerMap.get(from);
2238
+ if (connections.indexOf(peer.id) === -1) {
2239
+ connections.push(peer.id);
2240
+ this.peerMap.set(from, connections);
2241
+ }
2242
+ }
2243
+ }
2244
+ if (proto.name === 'peernet-dht') {
2245
+ let { hash, store } = proto.decoded;
2246
+ let has;
2247
+
2248
+ if (!store) {
2249
+ has = await this.has(hash);
2250
+ } else {
2251
+ store = globalThis[`${store}Store`];
2252
+ if (store.private) has = false;
2253
+ else has = await store.has(hash);
2254
+ }
2255
+ const data = new DHTMessageResponse({hash, has});
2256
+ const node = await this.prepareMessage(from, data.encoded);
2257
+
2258
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2259
+ } else if (proto.name === 'peernet-data') {
2260
+ let { hash, store } = proto.decoded;
2261
+ let data;
2262
+
2263
+ if (!store) {
2264
+ data = await this.get(hash);
2265
+ } else {
2266
+ store = globalThis[`${store}Store`];
2267
+ if (store.private) {
2268
+ // TODO: ban
2269
+ return
2270
+ } else data = await store.get(hash);
2271
+ }
2272
+
2273
+ if (data) {
2274
+ data = new DataMessageResponse({hash, data: Buffer.from(data)});
2275
+
2276
+ const node = await this.prepareMessage(from, data.encoded);
2277
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2278
+ }
2279
+ } else if (proto.name === 'peernet-peer') {
2280
+ const from = proto.decoded.id;
2281
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2282
+ else {
2283
+ const connections = this.peerMap.get(from);
2284
+ connections.push(peer.id);
2285
+ this.peerMap.set(from, connections);
2286
+ }
2287
+ const data = new PeerMessage({id: this.id});
2288
+ const node = await this.prepareMessage(from, data.encoded);
2289
+
2290
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2291
+ } else if (proto.name === 'peernet-request') {
2292
+ // TODO: make dynamic
2293
+ // exposeddevapi[proto.decoded.request](proto.decoded.params)
2294
+ const method = this.requestProtos[proto.decoded.request];
2295
+ if (method) {
2296
+ const data = await method();
2297
+ const node = await this.prepareMessage(from, data.encoded);
2298
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2299
+ }
2300
+ } else if (proto.name === 'peernet-ps' &&
2301
+ this._getPeerId(peer.id) !== this.id.toString()) {
2302
+ globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString());
2303
+ }
2304
+ }
2305
+ }
2306
+
2307
+ /**
2308
+ * performs a walk and resolves first encounter
2309
+ *
2310
+ * @param {String} hash
2311
+ */
2312
+ async walk(hash) {
2313
+ if (!hash) throw new Error('hash expected, received undefined')
2314
+ const data = new DHTMessage({hash});
2315
+ this.client.id;
2316
+ for (const peer of this.peers) {
2317
+ const node = await this.prepareMessage(peer.id, data.encoded);
2318
+
2319
+ const result = await peer.request(node.encoded);
2320
+
2321
+ let proto = protoFor(result.data);
2322
+
2323
+ if (proto.name !== 'peernet-message') throw encapsulatedError()
2324
+ const from = proto.decoded.from;
2325
+ proto = protoFor(proto.decoded.data);
2326
+
2327
+ if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
2328
+
2329
+ // TODO: give ip and port (just used for location)
2330
+ if (!peer.connection.remoteAddress || !peer.connection.localAddress) {
2331
+ peer.connection.remoteFamily = 'ipv4';
2332
+ peer.connection.remoteAddress = '127.0.0.1';
2333
+ peer.connection.remotePort = '0000';
2334
+ }
2335
+
2336
+ const peerInfo = {
2337
+ family: peer.connection.remoteFamily || peer.connection.localFamily,
2338
+ address: peer.connection.remoteAddress || peer.connection.localAddress,
2339
+ port: peer.connection.remotePort || peer.connection.localPort,
2340
+ id: from,
2341
+ };
2342
+
2343
+ if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
2344
+ }
2345
+ return
2346
+ }
2347
+
2348
+ /**
2349
+ * Override DHT behavior, try's finding the content three times
2350
+ *
2351
+ * @param {String} hash
2352
+ */
2353
+ async providersFor(hash) {
2354
+ let providers = await this.dht.providersFor(hash);
2355
+ // walk the network to find a provider
2356
+ if (!providers || providers.length === 0) {
2357
+ await this.walk(hash);
2358
+ providers = await this.dht.providersFor(hash);
2359
+ // second walk the network to find a provider
2360
+ if (!providers || providers.length === 0) {
2361
+ await this.walk(hash);
2362
+ providers = await this.dht.providersFor(hash);
2363
+ }
2364
+ // last walk
2365
+ if (!providers || providers.length === 0) {
2366
+ await this.walk(hash);
2367
+ providers = await this.dht.providersFor(hash);
2368
+ }
2369
+ }
2370
+ // undefined if no providers given
2371
+ return providers
2372
+ }
2373
+
2374
+ get block() {
2375
+ return {
2376
+ get: async (hash) => {
2377
+ const data = await blockStore.has(hash);
2378
+ if (data) return await blockStore.get(hash)
2379
+ return this.requestData(hash)
2380
+ },
2381
+ put: async (hash, data) => {
2382
+ if (await blockStore.has(hash)) return
2383
+ return await blockStore.put(hash, data)
2384
+ },
2385
+ has: async (hash) => await blockStore.has(hash, 'block'),
2386
+ }
2387
+ }
2388
+
2389
+ get transaction() {
2390
+ return {
2391
+ get: async (hash) => {
2392
+ const data = await transactionStore.has(hash);
2393
+ if (data) return await transactionStore.get(hash)
2394
+ return this.requestData(hash, 'transaction')
2395
+ },
2396
+ put: async (hash, data) => {
2397
+ if (await transactionStore.has(hash)) return
2398
+ return await transactionStore.put(hash, data)
2399
+ },
2400
+ has: async (hash) => await transactionStore.has(hash),
2401
+ }
2402
+ }
2403
+
2404
+ async requestData(hash, store) {
2405
+ const providers = await this.providersFor(hash);
2406
+ if (!providers || providers.size === 0) throw nothingFoundError(hash)
2407
+ debug(`found ${providers.size} provider(s) for ${hash}`);
2408
+ // get closest peer on earth
2409
+ const closestPeer = await this.dht.closestPeer(providers);
2410
+ // get peer instance by id
2411
+ if (!closestPeer || !closestPeer.id) return this.requestData(hash, store)
2412
+
2413
+ const id = closestPeer.id.toString();
2414
+ if (this.peers) {
2415
+ let closest = this.peers.filter((peer) => {
2416
+ if (this._getPeerId(peer.id) === id) return peer
2417
+ });
2418
+
2419
+ let data = new DataMessage({hash, store});
2420
+
2421
+ const node = await this.prepareMessage(id, data.encoded);
2422
+ if (closest[0]) data = await closest[0].request(node.encoded);
2423
+ else {
2424
+ closest = this.peers.filter((peer) => {
2425
+ if (peer.id.toString() === id) return peer
2426
+ });
2427
+ if (closest[0]) data = await closest[0].request(node.encoded);
2428
+ }
2429
+ if (data.data) {
2430
+ let proto = protoFor(Buffer.from(data.data));
2431
+ proto = protoFor(proto.decoded.data);
2432
+ return proto.decoded.data
2433
+ }
2434
+
2435
+ // this.put(hash, proto.decoded.data)
2436
+ }
2437
+ return null
2438
+ }
2439
+
2440
+
2441
+ get message() {
2442
+ return {
2443
+ /**
2444
+ * Get content for given message hash
2445
+ *
2446
+ * @param {String} hash
2447
+ */
2448
+ get: async (hash) => {
2449
+ debug(`get message ${hash}`);
2450
+ const message = await messageStore.has(hash);
2451
+ if (message) return await messageStore.get(hash)
2452
+ return this.requestData(hash, 'message')
2453
+ },
2454
+ /**
2455
+ * put message content
2456
+ *
2457
+ * @param {String} hash
2458
+ * @param {Buffer} message
2459
+ */
2460
+ put: async (hash, message) => await messageStore.put(hash, message),
2461
+ /**
2462
+ * @param {String} hash
2463
+ * @return {Boolean}
2464
+ */
2465
+ has: async (hash) => await messageStore.has(hash),
2466
+ }
2467
+ }
2468
+
2469
+ get data() {
2470
+ return {
2471
+ /**
2472
+ * Get content for given data hash
2473
+ *
2474
+ * @param {String} hash
2475
+ */
2476
+ get: async (hash) => {
2477
+ debug(`get data ${hash}`);
2478
+ const data = await dataStore.has(hash);
2479
+ if (data) return await dataStore.get(hash)
2480
+ return this.requestData(hash, 'data')
2481
+ },
2482
+ /**
2483
+ * put data content
2484
+ *
2485
+ * @param {String} hash
2486
+ * @param {Buffer} data
2487
+ */
2488
+ put: async (hash, data) => await dataStore.put(hash, data),
2489
+ /**
2490
+ * @param {String} hash
2491
+ * @return {Boolean}
2492
+ */
2493
+ has: async (hash) => await dataStore.has(hash),
2494
+ }
2495
+ }
2496
+
2497
+ /**
2498
+ * goes trough given stores and tries to find data for given hash
2499
+ * @param {Array} stores
2500
+ * @param {string} hash
2501
+ */
2502
+ async whichStore(stores, hash) {
2503
+ let store = stores.pop();
2504
+ store = globalThis[`${store}Store`];
2505
+ if (store) {
2506
+ const has = await store.has(hash);
2507
+ if (has) return store
2508
+ if (stores.length > 0) return this.whichStore(stores, hash)
2509
+ } else return null
2510
+ }
2511
+
2512
+ /**
2513
+ * Get content for given hash
2514
+ *
2515
+ * @param {String} hash
2516
+ */
2517
+ async get(hash, store) {
2518
+ debug(`get ${hash}`);
2519
+ let data;
2520
+ if (store) store = globalThis[`${store}Store`];
2521
+ if (!store) store = await this.whichStore([...this.stores], hash);
2522
+ if (store && await store.has(hash)) data = await store.get(hash);
2523
+ if (data) return data
2524
+
2525
+ return this.requestData(hash, 'data')
2526
+ }
2527
+
2528
+ /**
2529
+ * put content
2530
+ *
2531
+ * @param {String} hash
2532
+ * @param {Buffer} data
2533
+ */
2534
+ async put(hash, data, store = 'data') {
2535
+ store = globalThis[`${store}Store`];
2536
+ return await store.put(hash, data)
2537
+ }
2538
+
2539
+ /**
2540
+ * @param {String} hash
2541
+ * @return {Boolean}
2542
+ */
2543
+ async has(hash) {
2544
+ const store = await this.whichStore([...this.stores], hash);
2545
+ if (store) {
2546
+ if (store.private) return false
2547
+ else return true
2548
+ }
2549
+ return false
2550
+ }
2551
+
2552
+ /**
2553
+ *
2554
+ * @param {String} topic
2555
+ * @param {String|Object|Array|Boolean|Buffer} data
2556
+ */
2557
+ async publish(topic, data) {
2558
+ // globalSub.publish(topic, data)
2559
+ if (!Buffer.isBuffer(topic)) topic = Buffer.from(topic);
2560
+ if (!Buffer.isBuffer(data)) data = Buffer.from(data);
2561
+ const id = Math.random().toString(36).slice(-12);
2562
+ data = new PsMessage({data, topic});
2563
+ for (const peer of this.peers) {
2564
+ if (peer.connection._connected) {
2565
+ if (peer.id.toString() !== this.peerId.toString()) {
2566
+ const node = await this.prepareMessage(peer.id, data.encoded);
2567
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2568
+ }
2569
+ } else {
2570
+ this.removePeer(peer);
2571
+ }
2572
+ // TODO: if peer subscribed
2573
+ }
2574
+ }
2575
+
2576
+ createHash(data, name) {
2577
+ return new PeernetHash(data, {name})
2578
+ }
2579
+
2580
+ /**
2581
+ *
2582
+ * @param {String} topic
2583
+ * @param {Method} cb
2584
+ */
2585
+ async subscribe(topic, cb) {
2586
+ // TODO: if peer subscribed
2587
+ globalSub.subscribe(topic, cb);
2588
+ }
2589
+
2590
+ async removePeer(peer) {
2591
+ connections.delete(peer.id);
2592
+ }
2593
+
2594
+ get Buffer() {
2595
+ return Buffer
2596
+ }
2597
+ // async block(index) {
2598
+ // const _values = []
2599
+ // for (const peer of this.peers) {
2600
+ // const value = await peer.request({type: 'block', index})
2601
+ // console.log(value);
2602
+ // }
2603
+ //
2604
+ // }
2609
2605
  }
2610
2606
 
2611
2607
  module.exports = Peernet;