@leofcoin/peernet 0.11.2 → 0.11.5
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/dist/browser/326.peernet.js +1 -29
- package/dist/browser/peernet.js +2 -88552
- package/dist/browser/peernet.js.LICENSE.txt +40 -0
- package/dist/commonjs/{http-4bc6caeb.js → http-ee1b2071.js} +1 -1
- package/dist/commonjs/peernet.js +35 -29
- package/dist/commonjs/response.js +1 -1
- package/dist/module/peernet.js +35 -29
- package/index.html +1 -1
- package/package.json +5 -4
- package/src/handlers/message.js +2 -2
- package/src/peernet.js +27 -20
- package/src/proto/response.proto.js +1 -1
- package/test.js +1 -1
- package/webpack.config.js +3 -3
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Determine if an object is a Buffer
|
|
3
|
+
*
|
|
4
|
+
* @author Feross Aboukhadijeh <https://feross.org>
|
|
5
|
+
* @license MIT
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/*!
|
|
9
|
+
* The buffer module from node.js, for the browser.
|
|
10
|
+
*
|
|
11
|
+
* @author Feross Aboukhadijeh <https://feross.org>
|
|
12
|
+
* @license MIT
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/*!
|
|
16
|
+
* assert.js - assertions for javascript
|
|
17
|
+
* Copyright (c) 2018, Christopher Jeffrey (MIT License).
|
|
18
|
+
* https://github.com/chjj/bsert
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/*!
|
|
22
|
+
* base32.js - base32 for bcrypto
|
|
23
|
+
* Copyright (c) 2014-2019, Christopher Jeffrey (MIT License).
|
|
24
|
+
* https://github.com/bcoin-org/bcrypto
|
|
25
|
+
*
|
|
26
|
+
* Parts of this software are based on bitcoin/bitcoin:
|
|
27
|
+
* Copyright (c) 2009-2019, The Bitcoin Core Developers (MIT License).
|
|
28
|
+
* Copyright (c) 2009-2019, The Bitcoin Developers (MIT License).
|
|
29
|
+
* https://github.com/bitcoin/bitcoin
|
|
30
|
+
*
|
|
31
|
+
* Resources:
|
|
32
|
+
* https://tools.ietf.org/html/rfc4648
|
|
33
|
+
* https://github.com/bitcoin/bitcoin/blob/11d486d/src/utilstrencodings.cpp#L230
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/*! For license information please see browser.js.LICENSE.txt */
|
|
37
|
+
|
|
38
|
+
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
|
39
|
+
|
|
40
|
+
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
|
@@ -10,7 +10,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
10
10
|
var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
|
|
11
11
|
var Koa__default = /*#__PURE__*/_interopDefaultLegacy(Koa);
|
|
12
12
|
|
|
13
|
-
var version = "0.11.
|
|
13
|
+
var version = "0.11.4";
|
|
14
14
|
|
|
15
15
|
var api$1 = {
|
|
16
16
|
version: ({send}) => send({client: '@peernet/api/http', version}),
|
package/dist/commonjs/peernet.js
CHANGED
|
@@ -1221,10 +1221,10 @@ class HDWallet {
|
|
|
1221
1221
|
return network;
|
|
1222
1222
|
}
|
|
1223
1223
|
|
|
1224
|
-
async generate(network) {
|
|
1224
|
+
async generate(password, network) {
|
|
1225
1225
|
network = this.validateNetwork(network);
|
|
1226
|
-
const mnemonic = bip39.generateMnemonic();
|
|
1227
|
-
const seed = await bip39.mnemonicToSeed(mnemonic);
|
|
1226
|
+
const mnemonic = bip39.generateMnemonic(256);
|
|
1227
|
+
const seed = await bip39.mnemonicToSeed(mnemonic, password);
|
|
1228
1228
|
this.defineHDNode(bip32__namespace.fromSeed(seed, network));
|
|
1229
1229
|
return mnemonic; // userpw
|
|
1230
1230
|
}
|
|
@@ -1232,8 +1232,8 @@ class HDWallet {
|
|
|
1232
1232
|
/**
|
|
1233
1233
|
* recover using mnemonic (recovery word list)
|
|
1234
1234
|
*/
|
|
1235
|
-
async recover(mnemonic, network) {
|
|
1236
|
-
network = this.validateNetwork(network);
|
|
1235
|
+
async recover(mnemonic, password, network) {
|
|
1236
|
+
network = this.validateNetwork(network, password);
|
|
1237
1237
|
const seed = await bip39.mnemonicToSeed(mnemonic);
|
|
1238
1238
|
this.defineHDNode(bip32__namespace.fromSeed(seed, network));
|
|
1239
1239
|
}
|
|
@@ -1433,8 +1433,8 @@ class MessageHandler {
|
|
|
1433
1433
|
*/
|
|
1434
1434
|
async hashAndSignMessage(message) {
|
|
1435
1435
|
const hasher = new hash(message, {name: 'peernet-message'});
|
|
1436
|
-
|
|
1437
|
-
|
|
1436
|
+
let identity = await walletStore.get('identity');
|
|
1437
|
+
identity = JSON.parse(new TextDecoder().decode(identity));
|
|
1438
1438
|
const wallet = new MultiWallet(this.network);
|
|
1439
1439
|
wallet.import(identity.multiWIF);
|
|
1440
1440
|
return wallet.sign(hasher.hash.slice(0, 32))
|
|
@@ -1563,7 +1563,15 @@ class Peernet {
|
|
|
1563
1563
|
* @return {Array} peerId
|
|
1564
1564
|
*/
|
|
1565
1565
|
get peers() {
|
|
1566
|
-
return
|
|
1566
|
+
return Object.keys(this.client.connections)
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
get connections() {
|
|
1570
|
+
return Object.values(this.client.connections)
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
get peerEntries() {
|
|
1574
|
+
return Object.entries(this.client.connections)
|
|
1567
1575
|
}
|
|
1568
1576
|
|
|
1569
1577
|
/**
|
|
@@ -1628,7 +1636,7 @@ class Peernet {
|
|
|
1628
1636
|
protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
|
|
1629
1637
|
});
|
|
1630
1638
|
} else {
|
|
1631
|
-
const http = await Promise.resolve().then(function () { return require('./http-
|
|
1639
|
+
const http = await Promise.resolve().then(function () { return require('./http-ee1b2071.js'); });
|
|
1632
1640
|
if (environment !== 'browser') http.default(options);
|
|
1633
1641
|
}
|
|
1634
1642
|
|
|
@@ -1638,19 +1646,17 @@ class Peernet {
|
|
|
1638
1646
|
|
|
1639
1647
|
try {
|
|
1640
1648
|
const pub = await accountStore.get('public');
|
|
1641
|
-
this.id = pub.walletId;
|
|
1649
|
+
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
|
|
1642
1650
|
} catch (e) {
|
|
1643
1651
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
1644
|
-
const wallet = {};
|
|
1645
1652
|
const {identity, accounts, config} = await generateAccount__default["default"](this.network);
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
await accountStore.put('
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
this.id = wallet.identity.walletId;
|
|
1653
|
+
walletStore.put('version', new TextEncoder().encode(1));
|
|
1654
|
+
walletStore.put('accounts', new TextEncoder().encode(accounts));
|
|
1655
|
+
walletStore.put('identity', new TextEncoder().encode(JSON.stringify(identity)));
|
|
1656
|
+
await accountStore.put('config', new TextEncoder().encode(JSON.stringify(config)));
|
|
1657
|
+
await accountStore.put('public', new TextEncoder().encode(JSON.stringify({walletId: identity.walletId})));
|
|
1658
|
+
|
|
1659
|
+
this.id = identity.walletId;
|
|
1654
1660
|
} else {
|
|
1655
1661
|
throw e
|
|
1656
1662
|
}
|
|
@@ -1779,7 +1785,7 @@ class Peernet {
|
|
|
1779
1785
|
if (!hash) throw new Error('hash expected, received undefined')
|
|
1780
1786
|
const data = new dht({hash});
|
|
1781
1787
|
this.client.id;
|
|
1782
|
-
for (const peer of this.
|
|
1788
|
+
for (const peer of this.connections) {
|
|
1783
1789
|
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
1784
1790
|
|
|
1785
1791
|
const result = await peer.request(node.encoded);
|
|
@@ -1877,9 +1883,9 @@ class Peernet {
|
|
|
1877
1883
|
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store.name ? store.name : store)
|
|
1878
1884
|
|
|
1879
1885
|
const id = closestPeer.id.toString();
|
|
1880
|
-
if (this.
|
|
1881
|
-
let closest = this.
|
|
1882
|
-
if (
|
|
1886
|
+
if (this.connections) {
|
|
1887
|
+
let closest = this.connections.filter((peer) => {
|
|
1888
|
+
if (peer.id === id) return peer
|
|
1883
1889
|
});
|
|
1884
1890
|
|
|
1885
1891
|
let data = new DataMessage({hash, store: store.name ? store.name : store});
|
|
@@ -1887,8 +1893,8 @@ class Peernet {
|
|
|
1887
1893
|
const node = await this.prepareMessage(id, data.encoded);
|
|
1888
1894
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
1889
1895
|
else {
|
|
1890
|
-
closest = this.
|
|
1891
|
-
if (peer.id
|
|
1896
|
+
closest = this.connections.filter((peer) => {
|
|
1897
|
+
if (peer.id === id) return peer
|
|
1892
1898
|
});
|
|
1893
1899
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
1894
1900
|
}
|
|
@@ -2029,9 +2035,9 @@ class Peernet {
|
|
|
2029
2035
|
if (data instanceof Uint8Array === false) data = new TextEncoder().encode(JSON.stringify(data));
|
|
2030
2036
|
const id = Math.random().toString(36).slice(-12);
|
|
2031
2037
|
data = new PsMessage({data, topic});
|
|
2032
|
-
for (const peer of this.
|
|
2033
|
-
if (peer.
|
|
2034
|
-
if (peer.id
|
|
2038
|
+
for (const peer of this.connections) {
|
|
2039
|
+
if (peer.connected) {
|
|
2040
|
+
if (peer.id !== this.peerId) {
|
|
2035
2041
|
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
2036
2042
|
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
2037
2043
|
}
|
|
@@ -2057,7 +2063,7 @@ class Peernet {
|
|
|
2057
2063
|
}
|
|
2058
2064
|
|
|
2059
2065
|
async removePeer(peer) {
|
|
2060
|
-
connections
|
|
2066
|
+
delete this.client.connections[peer.id];
|
|
2061
2067
|
}
|
|
2062
2068
|
|
|
2063
2069
|
get Buffer() {
|
package/dist/module/peernet.js
CHANGED
|
@@ -1281,7 +1281,7 @@ class RequestMessage extends FormatInterface {
|
|
|
1281
1281
|
var proto$3 = `
|
|
1282
1282
|
// PeernetResponseMessage
|
|
1283
1283
|
message PeernetResponseMessage {
|
|
1284
|
-
required
|
|
1284
|
+
required bytes response = 1;
|
|
1285
1285
|
}
|
|
1286
1286
|
`;
|
|
1287
1287
|
|
|
@@ -1799,10 +1799,10 @@ class HDWallet {
|
|
|
1799
1799
|
return network;
|
|
1800
1800
|
}
|
|
1801
1801
|
|
|
1802
|
-
async generate(network) {
|
|
1802
|
+
async generate(password, network) {
|
|
1803
1803
|
network = this.validateNetwork(network);
|
|
1804
|
-
const mnemonic = generateMnemonic();
|
|
1805
|
-
const seed = await mnemonicToSeed(mnemonic);
|
|
1804
|
+
const mnemonic = generateMnemonic(256);
|
|
1805
|
+
const seed = await mnemonicToSeed(mnemonic, password);
|
|
1806
1806
|
this.defineHDNode(bip32.fromSeed(seed, network));
|
|
1807
1807
|
return mnemonic; // userpw
|
|
1808
1808
|
}
|
|
@@ -1810,8 +1810,8 @@ class HDWallet {
|
|
|
1810
1810
|
/**
|
|
1811
1811
|
* recover using mnemonic (recovery word list)
|
|
1812
1812
|
*/
|
|
1813
|
-
async recover(mnemonic, network) {
|
|
1814
|
-
network = this.validateNetwork(network);
|
|
1813
|
+
async recover(mnemonic, password, network) {
|
|
1814
|
+
network = this.validateNetwork(network, password);
|
|
1815
1815
|
const seed = await mnemonicToSeed(mnemonic);
|
|
1816
1816
|
this.defineHDNode(bip32.fromSeed(seed, network));
|
|
1817
1817
|
}
|
|
@@ -2011,8 +2011,8 @@ class MessageHandler {
|
|
|
2011
2011
|
*/
|
|
2012
2012
|
async hashAndSignMessage(message) {
|
|
2013
2013
|
const hasher = new PeernetHash(message, {name: 'peernet-message'});
|
|
2014
|
-
|
|
2015
|
-
|
|
2014
|
+
let identity = await walletStore.get('identity');
|
|
2015
|
+
identity = JSON.parse(new TextDecoder().decode(identity));
|
|
2016
2016
|
const wallet = new MultiWallet(this.network);
|
|
2017
2017
|
wallet.import(identity.multiWIF);
|
|
2018
2018
|
return wallet.sign(hasher.hash.slice(0, 32))
|
|
@@ -2141,7 +2141,15 @@ class Peernet {
|
|
|
2141
2141
|
* @return {Array} peerId
|
|
2142
2142
|
*/
|
|
2143
2143
|
get peers() {
|
|
2144
|
-
return
|
|
2144
|
+
return Object.keys(this.client.connections)
|
|
2145
|
+
}
|
|
2146
|
+
|
|
2147
|
+
get connections() {
|
|
2148
|
+
return Object.values(this.client.connections)
|
|
2149
|
+
}
|
|
2150
|
+
|
|
2151
|
+
get peerEntries() {
|
|
2152
|
+
return Object.entries(this.client.connections)
|
|
2145
2153
|
}
|
|
2146
2154
|
|
|
2147
2155
|
/**
|
|
@@ -2208,19 +2216,17 @@ class Peernet {
|
|
|
2208
2216
|
|
|
2209
2217
|
try {
|
|
2210
2218
|
const pub = await accountStore.get('public');
|
|
2211
|
-
this.id = pub.walletId;
|
|
2219
|
+
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
|
|
2212
2220
|
} catch (e) {
|
|
2213
2221
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
2214
|
-
const wallet = {};
|
|
2215
2222
|
const {identity, accounts, config} = await generateAccount(this.network);
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
await accountStore.put('
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
this.id = wallet.identity.walletId;
|
|
2223
|
+
walletStore.put('version', new TextEncoder().encode(1));
|
|
2224
|
+
walletStore.put('accounts', new TextEncoder().encode(accounts));
|
|
2225
|
+
walletStore.put('identity', new TextEncoder().encode(JSON.stringify(identity)));
|
|
2226
|
+
await accountStore.put('config', new TextEncoder().encode(JSON.stringify(config)));
|
|
2227
|
+
await accountStore.put('public', new TextEncoder().encode(JSON.stringify({walletId: identity.walletId})));
|
|
2228
|
+
|
|
2229
|
+
this.id = identity.walletId;
|
|
2224
2230
|
} else {
|
|
2225
2231
|
throw e
|
|
2226
2232
|
}
|
|
@@ -2349,7 +2355,7 @@ class Peernet {
|
|
|
2349
2355
|
if (!hash) throw new Error('hash expected, received undefined')
|
|
2350
2356
|
const data = new DHTMessage({hash});
|
|
2351
2357
|
this.client.id;
|
|
2352
|
-
for (const peer of this.
|
|
2358
|
+
for (const peer of this.connections) {
|
|
2353
2359
|
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
2354
2360
|
|
|
2355
2361
|
const result = await peer.request(node.encoded);
|
|
@@ -2447,9 +2453,9 @@ class Peernet {
|
|
|
2447
2453
|
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store.name ? store.name : store)
|
|
2448
2454
|
|
|
2449
2455
|
const id = closestPeer.id.toString();
|
|
2450
|
-
if (this.
|
|
2451
|
-
let closest = this.
|
|
2452
|
-
if (
|
|
2456
|
+
if (this.connections) {
|
|
2457
|
+
let closest = this.connections.filter((peer) => {
|
|
2458
|
+
if (peer.id === id) return peer
|
|
2453
2459
|
});
|
|
2454
2460
|
|
|
2455
2461
|
let data = new DataMessage({hash, store: store.name ? store.name : store});
|
|
@@ -2457,8 +2463,8 @@ class Peernet {
|
|
|
2457
2463
|
const node = await this.prepareMessage(id, data.encoded);
|
|
2458
2464
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
2459
2465
|
else {
|
|
2460
|
-
closest = this.
|
|
2461
|
-
if (peer.id
|
|
2466
|
+
closest = this.connections.filter((peer) => {
|
|
2467
|
+
if (peer.id === id) return peer
|
|
2462
2468
|
});
|
|
2463
2469
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
2464
2470
|
}
|
|
@@ -2599,9 +2605,9 @@ class Peernet {
|
|
|
2599
2605
|
if (data instanceof Uint8Array === false) data = new TextEncoder().encode(JSON.stringify(data));
|
|
2600
2606
|
const id = Math.random().toString(36).slice(-12);
|
|
2601
2607
|
data = new PsMessage({data, topic});
|
|
2602
|
-
for (const peer of this.
|
|
2603
|
-
if (peer.
|
|
2604
|
-
if (peer.id
|
|
2608
|
+
for (const peer of this.connections) {
|
|
2609
|
+
if (peer.connected) {
|
|
2610
|
+
if (peer.id !== this.peerId) {
|
|
2605
2611
|
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
2606
2612
|
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
2607
2613
|
}
|
|
@@ -2627,7 +2633,7 @@ class Peernet {
|
|
|
2627
2633
|
}
|
|
2628
2634
|
|
|
2629
2635
|
async removePeer(peer) {
|
|
2630
|
-
connections
|
|
2636
|
+
delete this.client.connections[peer.id];
|
|
2631
2637
|
}
|
|
2632
2638
|
|
|
2633
2639
|
get Buffer() {
|
package/index.html
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
(async () => {
|
|
14
14
|
const peernet = await new Peernet.default()
|
|
15
|
-
peernet.addRequestHandler('lastBlock', () => new peernet.protos['peernet-response']({response: 100}))
|
|
15
|
+
peernet.addRequestHandler('lastBlock', () => new peernet.protos['peernet-response']({response: new TextEncoder().encode(100)}))
|
|
16
16
|
})()
|
|
17
17
|
</script>
|
|
18
18
|
</body>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/commonjs/peernet.js",
|
|
6
6
|
"module": "dist/module/peernet.js",
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"browserslist": "> 0.5%, last 2 versions, not dead",
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@leofcoin/generate-account": "^1.0.2",
|
|
27
|
-
"@leofcoin/multi-wallet": "^2.1.
|
|
27
|
+
"@leofcoin/multi-wallet": "^2.1.2",
|
|
28
28
|
"@leofcoin/peernet-swarm": "^0.1.9",
|
|
29
|
-
"@leofcoin/storage": "^2.
|
|
29
|
+
"@leofcoin/storage": "^2.3.0",
|
|
30
30
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
31
31
|
"@vandeurenglenn/base58": "^1.1.0",
|
|
32
32
|
"@vandeurenglenn/is-hex": "^1.0.0",
|
|
@@ -62,6 +62,7 @@
|
|
|
62
62
|
"rollup-plugin-node-polyfills": "^0.2.1",
|
|
63
63
|
"tape": "^5.2.2",
|
|
64
64
|
"webpack": "^5.72.0",
|
|
65
|
-
"webpack-cli": "^4.9.2"
|
|
65
|
+
"webpack-cli": "^4.9.2",
|
|
66
|
+
"webpack-node-externals": "^3.0.0"
|
|
66
67
|
}
|
|
67
68
|
}
|
package/src/handlers/message.js
CHANGED
|
@@ -18,8 +18,8 @@ export default class MessageHandler {
|
|
|
18
18
|
*/
|
|
19
19
|
async hashAndSignMessage(message) {
|
|
20
20
|
const hasher = new Hash(message, {name: 'peernet-message'})
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
let identity = await walletStore.get('identity')
|
|
22
|
+
identity = JSON.parse(new TextDecoder().decode(identity))
|
|
23
23
|
const wallet = new MultiWallet(this.network)
|
|
24
24
|
wallet.import(identity.multiWIF)
|
|
25
25
|
return wallet.sign(hasher.hash.slice(0, 32))
|
package/src/peernet.js
CHANGED
|
@@ -108,7 +108,15 @@ export default class Peernet {
|
|
|
108
108
|
* @return {Array} peerId
|
|
109
109
|
*/
|
|
110
110
|
get peers() {
|
|
111
|
-
return
|
|
111
|
+
return Object.keys(this.client.connections)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get connections() {
|
|
115
|
+
return Object.values(this.client.connections)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
get peerEntries() {
|
|
119
|
+
return Object.entries(this.client.connections)
|
|
112
120
|
}
|
|
113
121
|
|
|
114
122
|
/**
|
|
@@ -175,19 +183,18 @@ export default class Peernet {
|
|
|
175
183
|
|
|
176
184
|
try {
|
|
177
185
|
const pub = await accountStore.get('public')
|
|
178
|
-
this.id = pub.walletId
|
|
186
|
+
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId
|
|
179
187
|
} catch (e) {
|
|
180
188
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
181
189
|
const wallet = {}
|
|
182
190
|
const {identity, accounts, config} = await generateAccount(this.network)
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
await accountStore.put('
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
this.id = wallet.identity.walletId
|
|
191
|
+
walletStore.put('version', new TextEncoder().encode(1))
|
|
192
|
+
walletStore.put('accounts', new TextEncoder().encode(accounts))
|
|
193
|
+
walletStore.put('identity', new TextEncoder().encode(JSON.stringify(identity)))
|
|
194
|
+
await accountStore.put('config', new TextEncoder().encode(JSON.stringify(config)));
|
|
195
|
+
await accountStore.put('public', new TextEncoder().encode(JSON.stringify({walletId: identity.walletId})));
|
|
196
|
+
|
|
197
|
+
this.id = identity.walletId
|
|
191
198
|
} else {
|
|
192
199
|
throw e
|
|
193
200
|
}
|
|
@@ -318,7 +325,7 @@ export default class Peernet {
|
|
|
318
325
|
if (!hash) throw new Error('hash expected, received undefined')
|
|
319
326
|
const data = new DHTMessage({hash})
|
|
320
327
|
const clientId = this.client.id
|
|
321
|
-
for (const peer of this.
|
|
328
|
+
for (const peer of this.connections) {
|
|
322
329
|
const node = await this.prepareMessage(peer.id, data.encoded)
|
|
323
330
|
|
|
324
331
|
const result = await peer.request(node.encoded)
|
|
@@ -416,9 +423,9 @@ export default class Peernet {
|
|
|
416
423
|
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store.name ? store.name : store)
|
|
417
424
|
|
|
418
425
|
const id = closestPeer.id.toString()
|
|
419
|
-
if (this.
|
|
420
|
-
let closest = this.
|
|
421
|
-
if (
|
|
426
|
+
if (this.connections) {
|
|
427
|
+
let closest = this.connections.filter((peer) => {
|
|
428
|
+
if (peer.id === id) return peer
|
|
422
429
|
})
|
|
423
430
|
|
|
424
431
|
let data = new DataMessage({hash, store: store.name ? store.name : store});
|
|
@@ -426,8 +433,8 @@ export default class Peernet {
|
|
|
426
433
|
const node = await this.prepareMessage(id, data.encoded)
|
|
427
434
|
if (closest[0]) data = await closest[0].request(node.encoded)
|
|
428
435
|
else {
|
|
429
|
-
closest = this.
|
|
430
|
-
if (peer.id
|
|
436
|
+
closest = this.connections.filter((peer) => {
|
|
437
|
+
if (peer.id === id) return peer
|
|
431
438
|
})
|
|
432
439
|
if (closest[0]) data = await closest[0].request(node.encoded)
|
|
433
440
|
}
|
|
@@ -569,9 +576,9 @@ export default class Peernet {
|
|
|
569
576
|
if (data instanceof Uint8Array === false) data = new TextEncoder().encode(JSON.stringify(data))
|
|
570
577
|
const id = Math.random().toString(36).slice(-12)
|
|
571
578
|
data = new PsMessage({data, topic})
|
|
572
|
-
for (const peer of this.
|
|
573
|
-
if (peer.
|
|
574
|
-
if (peer.id
|
|
579
|
+
for (const peer of this.connections) {
|
|
580
|
+
if (peer.connected) {
|
|
581
|
+
if (peer.id !== this.peerId) {
|
|
575
582
|
const node = await this.prepareMessage(peer.id, data.encoded)
|
|
576
583
|
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})))
|
|
577
584
|
}
|
|
@@ -597,7 +604,7 @@ export default class Peernet {
|
|
|
597
604
|
}
|
|
598
605
|
|
|
599
606
|
async removePeer(peer) {
|
|
600
|
-
connections
|
|
607
|
+
delete this.client.connections[peer.id]
|
|
601
608
|
}
|
|
602
609
|
|
|
603
610
|
get Buffer() {
|
package/test.js
CHANGED
|
@@ -23,6 +23,6 @@ pubsub.subscribe('peer:connected', async peer => {
|
|
|
23
23
|
response = new globalThis.peernet.protos['peernet-response'](proto.decoded.data)
|
|
24
24
|
console.log({response});
|
|
25
25
|
|
|
26
|
-
const block =
|
|
26
|
+
const block = new TextDecoder().decode(response.decoded.response)
|
|
27
27
|
console.log(block);
|
|
28
28
|
})
|
package/webpack.config.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
2
|
const webpack = require('webpack');
|
|
3
|
-
module.exports = {
|
|
3
|
+
module.exports = [{
|
|
4
4
|
entry: './src/peernet.js',
|
|
5
5
|
plugins: [
|
|
6
6
|
// Work around for Buffer is undefined:
|
|
@@ -17,7 +17,7 @@ module.exports = {
|
|
|
17
17
|
})
|
|
18
18
|
],
|
|
19
19
|
optimization: {
|
|
20
|
-
minimize:
|
|
20
|
+
minimize: true
|
|
21
21
|
},
|
|
22
22
|
resolve: {
|
|
23
23
|
extensions: [ '.ts', '.js' ],
|
|
@@ -38,4 +38,4 @@ resolve: {
|
|
|
38
38
|
filename: 'peernet.js',
|
|
39
39
|
path: path.resolve(__dirname, 'dist', 'browser'),
|
|
40
40
|
},
|
|
41
|
-
};
|
|
41
|
+
}];
|