@leofcoin/peernet 0.11.1 → 0.11.4
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 -0
- package/dist/browser/peernet.js +2 -108833
- package/dist/browser/peernet.js.LICENSE.txt +40 -0
- package/dist/commonjs/{http-7bbac90a.js → http-da1c8d33.js} +1 -1
- package/dist/commonjs/peernet-message.js +2 -2
- package/dist/commonjs/peernet.js +29 -31
- package/dist/commonjs/response.js +1 -1
- package/dist/module/peernet.js +31 -33
- package/index.html +2 -2
- package/package.json +5 -4
- package/src/handlers/message.js +2 -4
- package/src/peernet.js +21 -20
- package/src/proto/peernet.proto.js +2 -2
- package/src/proto/response.proto.js +1 -1
- package/test.js +1 -1
- package/webpack.config.js +10 -4
|
@@ -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.3";
|
|
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))
|
|
@@ -1447,8 +1447,6 @@ class MessageHandler {
|
|
|
1447
1447
|
* or the messageNode itself
|
|
1448
1448
|
*/
|
|
1449
1449
|
async prepareMessage(from, to, data, id) {
|
|
1450
|
-
if (!Buffer.isBuffer(from)) from = new Buffer.from(from);
|
|
1451
|
-
if (!Buffer.isBuffer(to)) to = new Buffer.from(to);
|
|
1452
1450
|
if (data.encoded) data = data.encoded;
|
|
1453
1451
|
|
|
1454
1452
|
const message = {
|
|
@@ -1542,7 +1540,7 @@ class Peernet {
|
|
|
1542
1540
|
if (this.hasDaemon) {
|
|
1543
1541
|
Storage = LeofcoinStorageClient;
|
|
1544
1542
|
} else {
|
|
1545
|
-
Storage = LeofcoinStorage__default["default"];
|
|
1543
|
+
Storage = globalThis.LeofcoinStorage?.default ? globalThis.LeofcoinStorage.default : LeofcoinStorage__default["default"];
|
|
1546
1544
|
}
|
|
1547
1545
|
globalThis[`${name}Store`] = globalThis[`${name}Store`] ||
|
|
1548
1546
|
await new Storage(`${prefix}-${name}`, root);
|
|
@@ -1565,7 +1563,7 @@ class Peernet {
|
|
|
1565
1563
|
* @return {Array} peerId
|
|
1566
1564
|
*/
|
|
1567
1565
|
get peers() {
|
|
1568
|
-
return
|
|
1566
|
+
return this.client.connections
|
|
1569
1567
|
}
|
|
1570
1568
|
|
|
1571
1569
|
/**
|
|
@@ -1630,7 +1628,7 @@ class Peernet {
|
|
|
1630
1628
|
protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
|
|
1631
1629
|
});
|
|
1632
1630
|
} else {
|
|
1633
|
-
const http = await Promise.resolve().then(function () { return require('./http-
|
|
1631
|
+
const http = await Promise.resolve().then(function () { return require('./http-da1c8d33.js'); });
|
|
1634
1632
|
if (environment !== 'browser') http.default(options);
|
|
1635
1633
|
}
|
|
1636
1634
|
|
|
@@ -1640,19 +1638,17 @@ class Peernet {
|
|
|
1640
1638
|
|
|
1641
1639
|
try {
|
|
1642
1640
|
const pub = await accountStore.get('public');
|
|
1643
|
-
this.id = pub.walletId;
|
|
1641
|
+
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
|
|
1644
1642
|
} catch (e) {
|
|
1645
1643
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
1646
|
-
const wallet = {};
|
|
1647
1644
|
const {identity, accounts, config} = await generateAccount__default["default"](this.network);
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
await accountStore.put('
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
this.id = wallet.identity.walletId;
|
|
1645
|
+
walletStore.put('version', new TextEncoder().encode(1));
|
|
1646
|
+
walletStore.put('accounts', new TextEncoder().encode(accounts));
|
|
1647
|
+
walletStore.put('identity', new TextEncoder().encode(JSON.stringify(identity)));
|
|
1648
|
+
await accountStore.put('config', new TextEncoder().encode(JSON.stringify(config)));
|
|
1649
|
+
await accountStore.put('public', new TextEncoder().encode(JSON.stringify({walletId: identity.walletId})));
|
|
1650
|
+
|
|
1651
|
+
this.id = identity.walletId;
|
|
1656
1652
|
} else {
|
|
1657
1653
|
throw e
|
|
1658
1654
|
}
|
|
@@ -1681,7 +1677,7 @@ class Peernet {
|
|
|
1681
1677
|
message = new peernetMessage(uint8Array);
|
|
1682
1678
|
const proto = protoFor(message.decoded.data);
|
|
1683
1679
|
|
|
1684
|
-
const from =
|
|
1680
|
+
const from = message.decoded.from;
|
|
1685
1681
|
this._protoHandler({id, proto}, this.client.connections[from], from);
|
|
1686
1682
|
});
|
|
1687
1683
|
|
|
@@ -1732,7 +1728,7 @@ class Peernet {
|
|
|
1732
1728
|
const data = new dhtResponse({hash, has});
|
|
1733
1729
|
const node = await this.prepareMessage(from, data.encoded);
|
|
1734
1730
|
|
|
1735
|
-
peer.send(
|
|
1731
|
+
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
1736
1732
|
this.bw.up += node.encoded.length;
|
|
1737
1733
|
} else if (proto.name === 'peernet-data') {
|
|
1738
1734
|
let { hash, store } = proto.decoded;
|
|
@@ -1746,10 +1742,10 @@ class Peernet {
|
|
|
1746
1742
|
data = await store.get(hash);
|
|
1747
1743
|
|
|
1748
1744
|
if (data) {
|
|
1749
|
-
data = new DataMessageResponse({hash, data: data.decoded ?
|
|
1745
|
+
data = new DataMessageResponse({hash, data: data.decoded ? new TextEncoder().encode(JSON.stringify(data.decoded)) : data});
|
|
1750
1746
|
|
|
1751
1747
|
const node = await this.prepareMessage(from, data.encoded);
|
|
1752
|
-
peer.send(
|
|
1748
|
+
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
1753
1749
|
this.bw.up += node.encoded.length;
|
|
1754
1750
|
}
|
|
1755
1751
|
}
|
|
@@ -1762,6 +1758,7 @@ class Peernet {
|
|
|
1762
1758
|
const data = await method();
|
|
1763
1759
|
const node = await this.prepareMessage(from, data.encoded);
|
|
1764
1760
|
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
1761
|
+
|
|
1765
1762
|
this.bw.up += node.encoded.length;
|
|
1766
1763
|
}
|
|
1767
1764
|
} else if (proto.name === 'peernet-ps' &&
|
|
@@ -1894,7 +1891,8 @@ class Peernet {
|
|
|
1894
1891
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
1895
1892
|
}
|
|
1896
1893
|
if (data.data) {
|
|
1897
|
-
|
|
1894
|
+
console.log(data.data);
|
|
1895
|
+
let proto = protoFor(data.data);
|
|
1898
1896
|
proto = protoFor(proto.decoded.data);
|
|
1899
1897
|
return proto.decoded.data
|
|
1900
1898
|
}
|
|
@@ -2025,15 +2023,15 @@ class Peernet {
|
|
|
2025
2023
|
*/
|
|
2026
2024
|
async publish(topic, data) {
|
|
2027
2025
|
// globalSub.publish(topic, data)
|
|
2028
|
-
if (
|
|
2029
|
-
if (
|
|
2026
|
+
if (topic instanceof Uint8Array === false) topic = new TextEncoder().encode(topic);
|
|
2027
|
+
if (data instanceof Uint8Array === false) data = new TextEncoder().encode(JSON.stringify(data));
|
|
2030
2028
|
const id = Math.random().toString(36).slice(-12);
|
|
2031
2029
|
data = new PsMessage({data, topic});
|
|
2032
2030
|
for (const peer of this.peers) {
|
|
2033
2031
|
if (peer.connection._connected) {
|
|
2034
2032
|
if (peer.id.toString() !== this.peerId.toString()) {
|
|
2035
2033
|
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
2036
|
-
peer.send(
|
|
2034
|
+
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
2037
2035
|
}
|
|
2038
2036
|
} else {
|
|
2039
2037
|
this.removePeer(peer);
|
|
@@ -2057,7 +2055,7 @@ class Peernet {
|
|
|
2057
2055
|
}
|
|
2058
2056
|
|
|
2059
2057
|
async removePeer(peer) {
|
|
2060
|
-
connections
|
|
2058
|
+
delete this.client.connections[peer.id];
|
|
2061
2059
|
}
|
|
2062
2060
|
|
|
2063
2061
|
get Buffer() {
|
package/dist/module/peernet.js
CHANGED
|
@@ -621,8 +621,8 @@ var proto$a = `
|
|
|
621
621
|
message PeernetMessage {
|
|
622
622
|
required bytes data = 1;
|
|
623
623
|
required bytes signature = 2;
|
|
624
|
-
optional
|
|
625
|
-
optional
|
|
624
|
+
optional string from = 3;
|
|
625
|
+
optional string to = 4;
|
|
626
626
|
optional string id = 5;
|
|
627
627
|
}`;
|
|
628
628
|
|
|
@@ -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))
|
|
@@ -2025,8 +2025,6 @@ class MessageHandler {
|
|
|
2025
2025
|
* or the messageNode itself
|
|
2026
2026
|
*/
|
|
2027
2027
|
async prepareMessage(from, to, data, id) {
|
|
2028
|
-
if (!Buffer.isBuffer(from)) from = new Buffer.from(from);
|
|
2029
|
-
if (!Buffer.isBuffer(to)) to = new Buffer.from(to);
|
|
2030
2028
|
if (data.encoded) data = data.encoded;
|
|
2031
2029
|
|
|
2032
2030
|
const message = {
|
|
@@ -2120,7 +2118,7 @@ class Peernet {
|
|
|
2120
2118
|
if (this.hasDaemon) {
|
|
2121
2119
|
Storage = LeofcoinStorageClient;
|
|
2122
2120
|
} else {
|
|
2123
|
-
Storage = LeofcoinStorage;
|
|
2121
|
+
Storage = globalThis.LeofcoinStorage?.default ? globalThis.LeofcoinStorage.default : LeofcoinStorage;
|
|
2124
2122
|
}
|
|
2125
2123
|
globalThis[`${name}Store`] = globalThis[`${name}Store`] ||
|
|
2126
2124
|
await new Storage(`${prefix}-${name}`, root);
|
|
@@ -2143,7 +2141,7 @@ class Peernet {
|
|
|
2143
2141
|
* @return {Array} peerId
|
|
2144
2142
|
*/
|
|
2145
2143
|
get peers() {
|
|
2146
|
-
return
|
|
2144
|
+
return this.client.connections
|
|
2147
2145
|
}
|
|
2148
2146
|
|
|
2149
2147
|
/**
|
|
@@ -2210,19 +2208,17 @@ class Peernet {
|
|
|
2210
2208
|
|
|
2211
2209
|
try {
|
|
2212
2210
|
const pub = await accountStore.get('public');
|
|
2213
|
-
this.id = pub.walletId;
|
|
2211
|
+
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
|
|
2214
2212
|
} catch (e) {
|
|
2215
2213
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
2216
|
-
const wallet = {};
|
|
2217
2214
|
const {identity, accounts, config} = await generateAccount(this.network);
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
await accountStore.put('
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
this.id = wallet.identity.walletId;
|
|
2215
|
+
walletStore.put('version', new TextEncoder().encode(1));
|
|
2216
|
+
walletStore.put('accounts', new TextEncoder().encode(accounts));
|
|
2217
|
+
walletStore.put('identity', new TextEncoder().encode(JSON.stringify(identity)));
|
|
2218
|
+
await accountStore.put('config', new TextEncoder().encode(JSON.stringify(config)));
|
|
2219
|
+
await accountStore.put('public', new TextEncoder().encode(JSON.stringify({walletId: identity.walletId})));
|
|
2220
|
+
|
|
2221
|
+
this.id = identity.walletId;
|
|
2226
2222
|
} else {
|
|
2227
2223
|
throw e
|
|
2228
2224
|
}
|
|
@@ -2251,7 +2247,7 @@ class Peernet {
|
|
|
2251
2247
|
message = new PeernetMessage(uint8Array);
|
|
2252
2248
|
const proto = protoFor(message.decoded.data);
|
|
2253
2249
|
|
|
2254
|
-
const from =
|
|
2250
|
+
const from = message.decoded.from;
|
|
2255
2251
|
this._protoHandler({id, proto}, this.client.connections[from], from);
|
|
2256
2252
|
});
|
|
2257
2253
|
|
|
@@ -2302,7 +2298,7 @@ class Peernet {
|
|
|
2302
2298
|
const data = new DHTMessageResponse({hash, has});
|
|
2303
2299
|
const node = await this.prepareMessage(from, data.encoded);
|
|
2304
2300
|
|
|
2305
|
-
peer.send(
|
|
2301
|
+
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
2306
2302
|
this.bw.up += node.encoded.length;
|
|
2307
2303
|
} else if (proto.name === 'peernet-data') {
|
|
2308
2304
|
let { hash, store } = proto.decoded;
|
|
@@ -2316,10 +2312,10 @@ class Peernet {
|
|
|
2316
2312
|
data = await store.get(hash);
|
|
2317
2313
|
|
|
2318
2314
|
if (data) {
|
|
2319
|
-
data = new DataMessageResponse({hash, data: data.decoded ?
|
|
2315
|
+
data = new DataMessageResponse({hash, data: data.decoded ? new TextEncoder().encode(JSON.stringify(data.decoded)) : data});
|
|
2320
2316
|
|
|
2321
2317
|
const node = await this.prepareMessage(from, data.encoded);
|
|
2322
|
-
peer.send(
|
|
2318
|
+
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
2323
2319
|
this.bw.up += node.encoded.length;
|
|
2324
2320
|
}
|
|
2325
2321
|
}
|
|
@@ -2332,6 +2328,7 @@ class Peernet {
|
|
|
2332
2328
|
const data = await method();
|
|
2333
2329
|
const node = await this.prepareMessage(from, data.encoded);
|
|
2334
2330
|
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
2331
|
+
|
|
2335
2332
|
this.bw.up += node.encoded.length;
|
|
2336
2333
|
}
|
|
2337
2334
|
} else if (proto.name === 'peernet-ps' &&
|
|
@@ -2464,7 +2461,8 @@ class Peernet {
|
|
|
2464
2461
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
2465
2462
|
}
|
|
2466
2463
|
if (data.data) {
|
|
2467
|
-
|
|
2464
|
+
console.log(data.data);
|
|
2465
|
+
let proto = protoFor(data.data);
|
|
2468
2466
|
proto = protoFor(proto.decoded.data);
|
|
2469
2467
|
return proto.decoded.data
|
|
2470
2468
|
}
|
|
@@ -2595,15 +2593,15 @@ class Peernet {
|
|
|
2595
2593
|
*/
|
|
2596
2594
|
async publish(topic, data) {
|
|
2597
2595
|
// globalSub.publish(topic, data)
|
|
2598
|
-
if (
|
|
2599
|
-
if (
|
|
2596
|
+
if (topic instanceof Uint8Array === false) topic = new TextEncoder().encode(topic);
|
|
2597
|
+
if (data instanceof Uint8Array === false) data = new TextEncoder().encode(JSON.stringify(data));
|
|
2600
2598
|
const id = Math.random().toString(36).slice(-12);
|
|
2601
2599
|
data = new PsMessage({data, topic});
|
|
2602
2600
|
for (const peer of this.peers) {
|
|
2603
2601
|
if (peer.connection._connected) {
|
|
2604
2602
|
if (peer.id.toString() !== this.peerId.toString()) {
|
|
2605
2603
|
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
2606
|
-
peer.send(
|
|
2604
|
+
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
2607
2605
|
}
|
|
2608
2606
|
} else {
|
|
2609
2607
|
this.removePeer(peer);
|
|
@@ -2627,7 +2625,7 @@ class Peernet {
|
|
|
2627
2625
|
}
|
|
2628
2626
|
|
|
2629
2627
|
async removePeer(peer) {
|
|
2630
|
-
connections
|
|
2628
|
+
delete this.client.connections[peer.id];
|
|
2631
2629
|
}
|
|
2632
2630
|
|
|
2633
2631
|
get Buffer() {
|
package/index.html
CHANGED
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
<script>
|
|
12
12
|
|
|
13
13
|
(async () => {
|
|
14
|
-
const peernet = await new Peernet()
|
|
15
|
-
peernet.addRequestHandler('lastBlock', () => new peernet.protos['peernet-response']({response: 100}))
|
|
14
|
+
const peernet = await new Peernet.default()
|
|
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.4",
|
|
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))
|
|
@@ -32,8 +32,6 @@ export default class MessageHandler {
|
|
|
32
32
|
* or the messageNode itself
|
|
33
33
|
*/
|
|
34
34
|
async prepareMessage(from, to, data, id) {
|
|
35
|
-
if (!Buffer.isBuffer(from)) from = new Buffer.from(from)
|
|
36
|
-
if (!Buffer.isBuffer(to)) to = new Buffer.from(to)
|
|
37
35
|
if (data.encoded) data = data.encoded
|
|
38
36
|
|
|
39
37
|
const message = {
|
package/src/peernet.js
CHANGED
|
@@ -85,7 +85,7 @@ export default class Peernet {
|
|
|
85
85
|
if (this.hasDaemon) {
|
|
86
86
|
Storage = LeofcoinStorageClient
|
|
87
87
|
} else {
|
|
88
|
-
Storage = LeofcoinStorage
|
|
88
|
+
Storage = globalThis.LeofcoinStorage?.default ? globalThis.LeofcoinStorage.default : LeofcoinStorage
|
|
89
89
|
}
|
|
90
90
|
globalThis[`${name}Store`] = globalThis[`${name}Store`] ||
|
|
91
91
|
await new Storage(`${prefix}-${name}`, root)
|
|
@@ -108,7 +108,7 @@ export default class Peernet {
|
|
|
108
108
|
* @return {Array} peerId
|
|
109
109
|
*/
|
|
110
110
|
get peers() {
|
|
111
|
-
return
|
|
111
|
+
return this.client.connections
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
/**
|
|
@@ -175,19 +175,18 @@ export default class Peernet {
|
|
|
175
175
|
|
|
176
176
|
try {
|
|
177
177
|
const pub = await accountStore.get('public')
|
|
178
|
-
this.id = pub.walletId
|
|
178
|
+
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId
|
|
179
179
|
} catch (e) {
|
|
180
180
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
181
181
|
const wallet = {}
|
|
182
182
|
const {identity, accounts, config} = await generateAccount(this.network)
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
await accountStore.put('
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
this.id = wallet.identity.walletId
|
|
183
|
+
walletStore.put('version', new TextEncoder().encode(1))
|
|
184
|
+
walletStore.put('accounts', new TextEncoder().encode(accounts))
|
|
185
|
+
walletStore.put('identity', new TextEncoder().encode(JSON.stringify(identity)))
|
|
186
|
+
await accountStore.put('config', new TextEncoder().encode(JSON.stringify(config)));
|
|
187
|
+
await accountStore.put('public', new TextEncoder().encode(JSON.stringify({walletId: identity.walletId})));
|
|
188
|
+
|
|
189
|
+
this.id = identity.walletId
|
|
191
190
|
} else {
|
|
192
191
|
throw e
|
|
193
192
|
}
|
|
@@ -216,7 +215,7 @@ export default class Peernet {
|
|
|
216
215
|
message = new PeernetMessage(uint8Array)
|
|
217
216
|
const proto = protoFor(message.decoded.data)
|
|
218
217
|
|
|
219
|
-
const from =
|
|
218
|
+
const from = message.decoded.from
|
|
220
219
|
this._protoHandler({id, proto}, this.client.connections[from], from)
|
|
221
220
|
})
|
|
222
221
|
|
|
@@ -267,7 +266,7 @@ export default class Peernet {
|
|
|
267
266
|
const data = new DHTMessageResponse({hash, has})
|
|
268
267
|
const node = await this.prepareMessage(from, data.encoded)
|
|
269
268
|
|
|
270
|
-
peer.send(
|
|
269
|
+
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})))
|
|
271
270
|
this.bw.up += node.encoded.length
|
|
272
271
|
} else if (proto.name === 'peernet-data') {
|
|
273
272
|
let { hash, store } = proto.decoded
|
|
@@ -281,10 +280,10 @@ export default class Peernet {
|
|
|
281
280
|
data = await store.get(hash)
|
|
282
281
|
|
|
283
282
|
if (data) {
|
|
284
|
-
data = new DataMessageResponse({hash, data: data.decoded ?
|
|
283
|
+
data = new DataMessageResponse({hash, data: data.decoded ? new TextEncoder().encode(JSON.stringify(data.decoded)) : data});
|
|
285
284
|
|
|
286
285
|
const node = await this.prepareMessage(from, data.encoded)
|
|
287
|
-
peer.send(
|
|
286
|
+
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})))
|
|
288
287
|
this.bw.up += node.encoded.length
|
|
289
288
|
}
|
|
290
289
|
} else {
|
|
@@ -299,6 +298,7 @@ export default class Peernet {
|
|
|
299
298
|
const data = await method()
|
|
300
299
|
const node = await this.prepareMessage(from, data.encoded)
|
|
301
300
|
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})))
|
|
301
|
+
|
|
302
302
|
this.bw.up += node.encoded.length
|
|
303
303
|
}
|
|
304
304
|
} else if (proto.name === 'peernet-ps' &&
|
|
@@ -431,7 +431,8 @@ export default class Peernet {
|
|
|
431
431
|
if (closest[0]) data = await closest[0].request(node.encoded)
|
|
432
432
|
}
|
|
433
433
|
if (data.data) {
|
|
434
|
-
|
|
434
|
+
console.log(data.data);
|
|
435
|
+
let proto = protoFor(data.data)
|
|
435
436
|
proto = protoFor(proto.decoded.data)
|
|
436
437
|
return proto.decoded.data
|
|
437
438
|
}
|
|
@@ -563,15 +564,15 @@ export default class Peernet {
|
|
|
563
564
|
*/
|
|
564
565
|
async publish(topic, data) {
|
|
565
566
|
// globalSub.publish(topic, data)
|
|
566
|
-
if (
|
|
567
|
-
if (
|
|
567
|
+
if (topic instanceof Uint8Array === false) topic = new TextEncoder().encode(topic)
|
|
568
|
+
if (data instanceof Uint8Array === false) data = new TextEncoder().encode(JSON.stringify(data))
|
|
568
569
|
const id = Math.random().toString(36).slice(-12)
|
|
569
570
|
data = new PsMessage({data, topic})
|
|
570
571
|
for (const peer of this.peers) {
|
|
571
572
|
if (peer.connection._connected) {
|
|
572
573
|
if (peer.id.toString() !== this.peerId.toString()) {
|
|
573
574
|
const node = await this.prepareMessage(peer.id, data.encoded)
|
|
574
|
-
peer.send(
|
|
575
|
+
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})))
|
|
575
576
|
}
|
|
576
577
|
} else {
|
|
577
578
|
this.removePeer(peer)
|
|
@@ -595,7 +596,7 @@ export default class Peernet {
|
|
|
595
596
|
}
|
|
596
597
|
|
|
597
598
|
async removePeer(peer) {
|
|
598
|
-
connections
|
|
599
|
+
delete this.client.connections[peer.id]
|
|
599
600
|
}
|
|
600
601
|
|
|
601
602
|
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
|
})
|