@leofcoin/peernet 0.11.13 → 0.11.16
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/peernet.js +41043 -76302
- package/dist/commonjs/{client-1a1f75e6.js → client-5633ba04.js} +1 -4
- package/dist/commonjs/codec-format-interface.js +7 -7
- package/dist/commonjs/{http-2bb2eb4b.js → http-20a16cac.js} +1 -1
- package/dist/commonjs/peernet.js +222 -66
- package/dist/module/peernet.js +224 -69
- package/index.html +1 -1
- package/package.json +4 -11
- package/rollup.config.js +10 -20
- package/src/codec/codec-format-interface.js +7 -7
- package/src/handlers/message.js +1 -1
- package/src/peernet.js +20 -20
- package/webpack.config.js +12 -3
- package/dist/browser/326.peernet.js +0 -29
package/dist/module/peernet.js
CHANGED
|
@@ -6,20 +6,16 @@ import bs58 from '@vandeurenglenn/base58';
|
|
|
6
6
|
import isHex from '@vandeurenglenn/is-hex';
|
|
7
7
|
import varint from 'varint';
|
|
8
8
|
import createKeccakHash from 'keccak';
|
|
9
|
-
import
|
|
10
|
-
import generateAccount from '@leofcoin/generate-account';
|
|
9
|
+
import MultiWallet$1 from '@leofcoin/multi-wallet';
|
|
11
10
|
import * as bs58check from 'bs58check';
|
|
12
11
|
import bs58check__default from 'bs58check';
|
|
13
|
-
import { generateMnemonic, mnemonicToSeed } from 'bip39';
|
|
14
12
|
import * as bip32 from 'bip32';
|
|
15
|
-
import { publicKeyConvert } from 'secp256k1';
|
|
16
13
|
import ecc from 'tiny-secp256k1';
|
|
14
|
+
import Mnemonic from '@leofcoin/mnemonic';
|
|
17
15
|
import MultiSignature from 'multi-signature';
|
|
18
|
-
import
|
|
19
|
-
import 'crypto-js/sha512.js';
|
|
20
|
-
import ENC from 'crypto-js/enc-utf8.js';
|
|
16
|
+
import randombytes from 'randombytes';
|
|
21
17
|
|
|
22
|
-
/* socket-request-client version 1.6.
|
|
18
|
+
/* socket-request-client version 1.6.3 */
|
|
23
19
|
|
|
24
20
|
class LittlePubSub {
|
|
25
21
|
constructor(verbose = true) {
|
|
@@ -206,9 +202,6 @@ const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry
|
|
|
206
202
|
peernet: api.peernet(client),
|
|
207
203
|
server: api.server(client),
|
|
208
204
|
close: exit => {
|
|
209
|
-
client.onclose = message => {
|
|
210
|
-
if (exit) process.exit();
|
|
211
|
-
};
|
|
212
205
|
client.close();
|
|
213
206
|
}
|
|
214
207
|
}
|
|
@@ -275,6 +268,10 @@ class Peer {
|
|
|
275
268
|
return this.#connected
|
|
276
269
|
}
|
|
277
270
|
|
|
271
|
+
get readyState() {
|
|
272
|
+
return this.channel?.readyState
|
|
273
|
+
}
|
|
274
|
+
|
|
278
275
|
/**
|
|
279
276
|
* @params {Object} options
|
|
280
277
|
* @params {string} options.channelName - this peerid : otherpeer id
|
|
@@ -314,8 +311,19 @@ constructor(options = {}) {
|
|
|
314
311
|
}
|
|
315
312
|
|
|
316
313
|
send(message) {
|
|
317
|
-
this.
|
|
318
|
-
|
|
314
|
+
switch (this.channel?.readyState) {
|
|
315
|
+
case 'open':
|
|
316
|
+
this.bw.up += message.length || message.byteLength;
|
|
317
|
+
this.channel.send(message);
|
|
318
|
+
break;
|
|
319
|
+
case 'closed':
|
|
320
|
+
case 'closing':
|
|
321
|
+
debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
|
|
322
|
+
break;
|
|
323
|
+
case undefined:
|
|
324
|
+
debug(`trying to send before a channel is created`);
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
319
327
|
}
|
|
320
328
|
|
|
321
329
|
request(data) {
|
|
@@ -388,7 +396,6 @@ constructor(options = {}) {
|
|
|
388
396
|
this.channel.onmessage = (message) => {
|
|
389
397
|
pubsub.publish('peer:data', message);
|
|
390
398
|
debug(`incoming message from ${this.peerId}`);
|
|
391
|
-
debug(message);
|
|
392
399
|
this.bw.down += message.length || message.byteLength;
|
|
393
400
|
};
|
|
394
401
|
|
|
@@ -448,6 +455,7 @@ constructor(options = {}) {
|
|
|
448
455
|
|
|
449
456
|
close() {
|
|
450
457
|
debug(`closing ${this.peerId}`);
|
|
458
|
+
this.#connected = false;
|
|
451
459
|
this.channel?.close();
|
|
452
460
|
this.#connection?.close();
|
|
453
461
|
|
|
@@ -464,6 +472,10 @@ class Client {
|
|
|
464
472
|
return { ...this.#connections }
|
|
465
473
|
}
|
|
466
474
|
|
|
475
|
+
get peers() {
|
|
476
|
+
return Object.entries(this.#connections)
|
|
477
|
+
}
|
|
478
|
+
|
|
467
479
|
constructor(id, identifiers = ['peernet-v0.1.0'], stars = []) {
|
|
468
480
|
this.id = id || Math.random().toString(36).slice(-12);
|
|
469
481
|
if (!Array.isArray(identifiers)) identifiers = [identifiers];
|
|
@@ -578,6 +590,15 @@ class Client {
|
|
|
578
590
|
debug(`peer ${id} joined`);
|
|
579
591
|
}
|
|
580
592
|
|
|
593
|
+
removePeer(peer) {
|
|
594
|
+
const id = peer.peerId || peer;
|
|
595
|
+
if (this.#connections[id]) {
|
|
596
|
+
this.#connections[id].connected && this.#connections[id].close();
|
|
597
|
+
delete this.#connections[id];
|
|
598
|
+
}
|
|
599
|
+
debug(`peer ${id} removed`);
|
|
600
|
+
}
|
|
601
|
+
|
|
581
602
|
|
|
582
603
|
}
|
|
583
604
|
|
|
@@ -984,14 +1005,14 @@ class FormatInterface {
|
|
|
984
1005
|
this.protoDecode = proto.decode;
|
|
985
1006
|
this.hashFormat = options.hashFormat || 'bs32';
|
|
986
1007
|
if (options.name) this.name = options.name;
|
|
987
|
-
if (buffer instanceof Uint8Array)
|
|
988
|
-
else if (buffer instanceof ArrayBuffer)
|
|
1008
|
+
if (buffer instanceof Uint8Array) this.fromUint8Array(buffer);
|
|
1009
|
+
else if (buffer instanceof ArrayBuffer) this.fromArrayBuffer(buffer);
|
|
989
1010
|
else if (buffer.name === options.name) return buffer
|
|
990
|
-
else if (
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
1011
|
+
else if (buffer instanceof String) {
|
|
1012
|
+
if (isHex(buffer)) this.fromHex(buffer);
|
|
1013
|
+
else if (bs32.isBase32(buffer)) this.fromBs32(buffer);
|
|
1014
|
+
else if (bs58.isBase58(buffer)) this.fromBs58(buffer);
|
|
1015
|
+
else throw new Error(`unsupported string ${buffer}`)
|
|
995
1016
|
} else {
|
|
996
1017
|
this.create(buffer);
|
|
997
1018
|
}
|
|
@@ -1624,6 +1645,43 @@ class DhtEarth {
|
|
|
1624
1645
|
}
|
|
1625
1646
|
}
|
|
1626
1647
|
|
|
1648
|
+
/**
|
|
1649
|
+
* @params {String} network
|
|
1650
|
+
* @return {object} { identity, accounts, config }
|
|
1651
|
+
*/
|
|
1652
|
+
var generateAccount = async network => {
|
|
1653
|
+
let wallet = new MultiWallet$1(network);
|
|
1654
|
+
/**
|
|
1655
|
+
* @type {string}
|
|
1656
|
+
*/
|
|
1657
|
+
const mnemonic = await wallet.generate();
|
|
1658
|
+
|
|
1659
|
+
wallet = new MultiWallet$1(network);
|
|
1660
|
+
await wallet.recover(mnemonic, network);
|
|
1661
|
+
/**
|
|
1662
|
+
* @type {object}
|
|
1663
|
+
*/
|
|
1664
|
+
const account = wallet.account(0);
|
|
1665
|
+
/**
|
|
1666
|
+
* @type {object}
|
|
1667
|
+
*/
|
|
1668
|
+
const external = account.external(0);
|
|
1669
|
+
const internal = account.internal(0);
|
|
1670
|
+
|
|
1671
|
+
return {
|
|
1672
|
+
identity: {
|
|
1673
|
+
mnemonic,
|
|
1674
|
+
// multiWIF: wallet.export(),
|
|
1675
|
+
publicKey: external.publicKey,
|
|
1676
|
+
privateKey: external.privateKey,
|
|
1677
|
+
walletId: external.id
|
|
1678
|
+
},
|
|
1679
|
+
accounts: [['main account', external.address, internal.address]]
|
|
1680
|
+
// config: {
|
|
1681
|
+
// }
|
|
1682
|
+
}
|
|
1683
|
+
};
|
|
1684
|
+
|
|
1627
1685
|
var testnets = {
|
|
1628
1686
|
'leofcoin:olivia': {
|
|
1629
1687
|
messagePrefix: '\u0019Leofcoin Signed Message:',
|
|
@@ -1677,6 +1735,7 @@ const bitcoin = {
|
|
|
1677
1735
|
messagePrefix: '\x18Bitcoin Signed Message:\n',
|
|
1678
1736
|
bech32: 'bc',
|
|
1679
1737
|
pubKeyHash: 0x00,
|
|
1738
|
+
multiCodec: 0x00,
|
|
1680
1739
|
scriptHash: 0x05,
|
|
1681
1740
|
wif: 0x80,
|
|
1682
1741
|
coin_type: 0,
|
|
@@ -1725,12 +1784,15 @@ const fromNetworkString = network => {
|
|
|
1725
1784
|
network = networks[parts[0]];
|
|
1726
1785
|
if (parts[1]) {
|
|
1727
1786
|
if (network[parts[1]]) network = network[parts[1]];
|
|
1728
|
-
|
|
1787
|
+
|
|
1729
1788
|
network.coin_type = 1;
|
|
1730
1789
|
}
|
|
1731
1790
|
return network;
|
|
1732
1791
|
};
|
|
1733
1792
|
|
|
1793
|
+
// import { createHash } from 'crypto'
|
|
1794
|
+
// import { createHash as _createHash } from './hash'
|
|
1795
|
+
|
|
1734
1796
|
const { encode: encode$1, decode: decode$1 } = bs58check__default;
|
|
1735
1797
|
class HDWallet {
|
|
1736
1798
|
|
|
@@ -1758,20 +1820,48 @@ class HDWallet {
|
|
|
1758
1820
|
return this.ifNotLocked(() => this.publicKeyBuffer.toString('hex'))
|
|
1759
1821
|
}
|
|
1760
1822
|
|
|
1823
|
+
get ethereumAddress() {
|
|
1824
|
+
const buffer = ecc.pointFromScalar(this.hdnode.__D, false);
|
|
1825
|
+
let hash = createKeccakHash('keccak256').update(buffer.slice(1)).digest();
|
|
1826
|
+
return `0x${hash.slice(-20).toString('hex')}`
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
// async bitcoinAddress() {
|
|
1830
|
+
// const chainCode = this.privateKeyBuffer
|
|
1831
|
+
//
|
|
1832
|
+
// const node = bip32.fromPrivateKey(this.privateKeyBuffer, chainCode, networks['bitcoin'])
|
|
1833
|
+
// let buffer = await _createHash(node.publicKey, 'SHA-256')
|
|
1834
|
+
// buffer = createHash('ripemd160').update(buffer).digest()
|
|
1835
|
+
// // buffer = Buffer.from(`0x00${buffer.toString('hex')}`, 'hex')
|
|
1836
|
+
// // buffer = createHash('sha256').update(buffer).digest()
|
|
1837
|
+
// // const mainHash = buffer
|
|
1838
|
+
// // buffer = createHash('sha256').update(buffer).digest()
|
|
1839
|
+
// // const checksum = buffer.toString('hex').substring(0, 8)
|
|
1840
|
+
// // return base58.encode(Buffer.concat([mainHash, Buffer.from(checksum, 'hex')]))
|
|
1841
|
+
// const payload = Buffer.allocUnsafe(21)
|
|
1842
|
+
// payload.writeUInt8(networks['bitcoin'].pubKeyHash, 0)
|
|
1843
|
+
// buffer.copy(payload, 1)
|
|
1844
|
+
//
|
|
1845
|
+
// return encode(payload)
|
|
1846
|
+
// }
|
|
1847
|
+
|
|
1848
|
+
get leofcoinAddress() {
|
|
1849
|
+
return encode$1(this.neutered.publicKeyBuffer)
|
|
1850
|
+
}
|
|
1851
|
+
|
|
1761
1852
|
get address() {
|
|
1762
|
-
|
|
1763
|
-
|
|
1853
|
+
return this.getAddressForCoin()
|
|
1854
|
+
}
|
|
1855
|
+
|
|
1856
|
+
getAddressForCoin(coin_type) {
|
|
1857
|
+
if (!coin_type) coin_type = this.hdnode.network.coin_type;
|
|
1764
1858
|
if (coin_type === 1) {
|
|
1765
1859
|
if (this.networkName?.split(':')[0] === 'ethereum') coin_type = 60;
|
|
1766
1860
|
if (this.networkName?.split(':')[0] === 'leofcoin') coin_type = 640;
|
|
1767
1861
|
}
|
|
1768
|
-
if (coin_type ===
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
let hash = createKeccakHash('keccak256').update(buffer).digest();
|
|
1772
|
-
return hash.slice(-20).toString('hex')
|
|
1773
|
-
}
|
|
1774
|
-
return encode$1(this.neutered.publicKeyBuffer)
|
|
1862
|
+
// if (coin_type === 0) return this.bitcoinAddress
|
|
1863
|
+
if (coin_type === 60) return this.ethereumAddress
|
|
1864
|
+
if (coin_type === 640) return this.leofcoinAddress
|
|
1775
1865
|
}
|
|
1776
1866
|
|
|
1777
1867
|
get accountAddress() {
|
|
@@ -1818,10 +1908,10 @@ class HDWallet {
|
|
|
1818
1908
|
|
|
1819
1909
|
async generate(password, network) {
|
|
1820
1910
|
network = this.validateNetwork(network);
|
|
1821
|
-
const mnemonic =
|
|
1822
|
-
const seed =
|
|
1911
|
+
const mnemonic = new Mnemonic().generate();
|
|
1912
|
+
const seed = new Mnemonic().seedFromMnemonic(mnemonic, password);
|
|
1823
1913
|
this.defineHDNode(bip32.fromSeed(seed, network));
|
|
1824
|
-
return mnemonic;
|
|
1914
|
+
return mnemonic;
|
|
1825
1915
|
}
|
|
1826
1916
|
|
|
1827
1917
|
/**
|
|
@@ -1829,7 +1919,7 @@ class HDWallet {
|
|
|
1829
1919
|
*/
|
|
1830
1920
|
async recover(mnemonic, password, network) {
|
|
1831
1921
|
network = this.validateNetwork(network, password);
|
|
1832
|
-
const seed =
|
|
1922
|
+
const seed = new Mnemonic().seedFromMnemonic(mnemonic, password);
|
|
1833
1923
|
this.defineHDNode(bip32.fromSeed(seed, network));
|
|
1834
1924
|
}
|
|
1835
1925
|
|
|
@@ -1862,6 +1952,71 @@ class HDWallet {
|
|
|
1862
1952
|
}
|
|
1863
1953
|
}
|
|
1864
1954
|
|
|
1955
|
+
const { subtle } = crypto;
|
|
1956
|
+
|
|
1957
|
+
const generateAesKey = async (length = 256) => {
|
|
1958
|
+
const key = await subtle.generateKey({
|
|
1959
|
+
name: 'AES-CBC',
|
|
1960
|
+
length
|
|
1961
|
+
}, true, ['encrypt', 'decrypt']);
|
|
1962
|
+
|
|
1963
|
+
return key;
|
|
1964
|
+
};
|
|
1965
|
+
|
|
1966
|
+
const importAesKey = async (exported, format = 'raw', length = 256) => {
|
|
1967
|
+
return await subtle.importKey(format, exported, {
|
|
1968
|
+
name: 'AES-CBC',
|
|
1969
|
+
length
|
|
1970
|
+
}, true, ['encrypt', 'decrypt'])
|
|
1971
|
+
};
|
|
1972
|
+
|
|
1973
|
+
const exportAesKey = async (key, format = 'raw') => {
|
|
1974
|
+
return await subtle.exportKey(format, key)
|
|
1975
|
+
};
|
|
1976
|
+
|
|
1977
|
+
const encryptAes = async (uint8Array, key, iv) => subtle.encrypt({
|
|
1978
|
+
name: 'AES-CBC',
|
|
1979
|
+
iv,
|
|
1980
|
+
}, key, uint8Array);
|
|
1981
|
+
|
|
1982
|
+
const uint8ArrayToHex = uint8Array =>
|
|
1983
|
+
[...uint8Array].map(x => x.toString(16).padStart(2, '0')).join('');
|
|
1984
|
+
|
|
1985
|
+
const arrayBufferToHex = arrayBuffer =>
|
|
1986
|
+
uint8ArrayToHex(new Uint8Array(arrayBuffer));
|
|
1987
|
+
|
|
1988
|
+
const hexToUint8Array = hex =>
|
|
1989
|
+
new Uint8Array(hex.match(/[\da-f]{2}/gi).map(x => parseInt(x, 16)));
|
|
1990
|
+
|
|
1991
|
+
const encrypt = async string => {
|
|
1992
|
+
const ec = new TextEncoder();
|
|
1993
|
+
const key = await generateAesKey();
|
|
1994
|
+
const iv = await randombytes(16);
|
|
1995
|
+
|
|
1996
|
+
const ciphertext = await encryptAes(ec.encode(string), key, iv);
|
|
1997
|
+
const exported = await exportAesKey(key);
|
|
1998
|
+
|
|
1999
|
+
return {
|
|
2000
|
+
key: arrayBufferToHex(exported),
|
|
2001
|
+
iv: iv.toString('hex'),
|
|
2002
|
+
cipher: arrayBufferToHex(ciphertext)
|
|
2003
|
+
}
|
|
2004
|
+
};
|
|
2005
|
+
|
|
2006
|
+
const decrypt = async (cipher, key, iv) => {
|
|
2007
|
+
if (!key.type) key = await importAesKey(hexToUint8Array(key));
|
|
2008
|
+
cipher = new Uint8Array(hexToUint8Array(cipher));
|
|
2009
|
+
iv = new Uint8Array(hexToUint8Array(iv));
|
|
2010
|
+
|
|
2011
|
+
const dec = new TextDecoder();
|
|
2012
|
+
const plaintext = await subtle.decrypt({
|
|
2013
|
+
name: 'AES-CBC',
|
|
2014
|
+
iv,
|
|
2015
|
+
}, key, cipher);
|
|
2016
|
+
|
|
2017
|
+
return dec.decode(plaintext);
|
|
2018
|
+
};
|
|
2019
|
+
|
|
1865
2020
|
const { encode, decode } = bs58check;
|
|
1866
2021
|
|
|
1867
2022
|
// TODO: multihash addresses
|
|
@@ -1892,9 +2047,7 @@ class HDAccount {
|
|
|
1892
2047
|
|
|
1893
2048
|
class MultiWallet extends HDWallet {
|
|
1894
2049
|
constructor(network, hdnode) {
|
|
1895
|
-
const networkName = network;
|
|
1896
2050
|
super(network, hdnode);
|
|
1897
|
-
if (typeof networkName === 'string') this.networkName = networkName;
|
|
1898
2051
|
this.multiCodec = this.network.multiCodec;
|
|
1899
2052
|
this.version = 0x00;
|
|
1900
2053
|
}
|
|
@@ -1912,7 +2065,7 @@ class MultiWallet extends HDWallet {
|
|
|
1912
2065
|
}
|
|
1913
2066
|
|
|
1914
2067
|
get neutered() {
|
|
1915
|
-
const neutered = this.ifNotLocked(() => new MultiWallet(this.
|
|
2068
|
+
const neutered = this.ifNotLocked(() => new MultiWallet(this.networkName, this.hdnode.neutered()));
|
|
1916
2069
|
if (neutered) this._neutered = neutered;
|
|
1917
2070
|
return this._neutered
|
|
1918
2071
|
}
|
|
@@ -1921,18 +2074,19 @@ class MultiWallet extends HDWallet {
|
|
|
1921
2074
|
let buffer = decode(id);
|
|
1922
2075
|
varint.decode(buffer);
|
|
1923
2076
|
buffer = buffer.slice(varint.decode.bytes);
|
|
1924
|
-
this.fromPublicKey(buffer, null, this.
|
|
2077
|
+
this.fromPublicKey(buffer, null, this.networkName);
|
|
1925
2078
|
}
|
|
1926
2079
|
|
|
1927
|
-
lock(
|
|
2080
|
+
async lock(multiWIF) {
|
|
1928
2081
|
if (!multiWIF) multiWIF = this.multiWIF;
|
|
1929
|
-
this.encrypted =
|
|
2082
|
+
this.encrypted = await encrypt(multiWIF.toString('hex'));
|
|
1930
2083
|
this.locked = true;
|
|
2084
|
+
return this.encrypted
|
|
1931
2085
|
}
|
|
1932
2086
|
|
|
1933
|
-
unlock(key,
|
|
1934
|
-
|
|
1935
|
-
this.import(
|
|
2087
|
+
async unlock({key, iv, cipher}) {
|
|
2088
|
+
const decrypted = await decrypt(cipher, key, iv);
|
|
2089
|
+
this.import(decrypted);
|
|
1936
2090
|
this.locked = false;
|
|
1937
2091
|
}
|
|
1938
2092
|
|
|
@@ -1952,7 +2106,7 @@ class MultiWallet extends HDWallet {
|
|
|
1952
2106
|
else if (c.testnet && c.testnet.multiCodec === multiCodec) return c.testnet
|
|
1953
2107
|
else return p
|
|
1954
2108
|
}, networks['leofcoin']);
|
|
1955
|
-
this.load(bs58, this.
|
|
2109
|
+
this.load(bs58, this.networkName);
|
|
1956
2110
|
}
|
|
1957
2111
|
|
|
1958
2112
|
/**
|
|
@@ -1995,7 +2149,7 @@ class MultiWallet extends HDWallet {
|
|
|
1995
2149
|
* @return { internal(addressIndex), external(addressIndex) }
|
|
1996
2150
|
*/
|
|
1997
2151
|
account(index) {
|
|
1998
|
-
return new HDAccount(new MultiWallet(this.
|
|
2152
|
+
return new HDAccount(new MultiWallet(this.networkName, this.hdnode), index);
|
|
1999
2153
|
}
|
|
2000
2154
|
|
|
2001
2155
|
/**
|
|
@@ -2004,11 +2158,11 @@ class MultiWallet extends HDWallet {
|
|
|
2004
2158
|
* see https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
|
|
2005
2159
|
*/
|
|
2006
2160
|
derivePath(path) {
|
|
2007
|
-
return new MultiWallet(this.
|
|
2161
|
+
return new MultiWallet(this.networkName, this.hdnode.derivePath(path))
|
|
2008
2162
|
}
|
|
2009
2163
|
|
|
2010
2164
|
derive(index) {
|
|
2011
|
-
return new MultiWallet(this.
|
|
2165
|
+
return new MultiWallet(this.networkName, this.hdnode.derive(index));
|
|
2012
2166
|
}
|
|
2013
2167
|
}
|
|
2014
2168
|
|
|
@@ -2031,7 +2185,8 @@ class MessageHandler {
|
|
|
2031
2185
|
let identity = await walletStore.get('identity');
|
|
2032
2186
|
identity = JSON.parse(new TextDecoder().decode(identity));
|
|
2033
2187
|
const wallet = new MultiWallet(this.network);
|
|
2034
|
-
|
|
2188
|
+
console.log(identity);
|
|
2189
|
+
wallet.recover(identity.mnemonic);
|
|
2035
2190
|
return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
|
|
2036
2191
|
}
|
|
2037
2192
|
|
|
@@ -2138,7 +2293,7 @@ class Peernet {
|
|
|
2138
2293
|
Storage = globalThis.LeofcoinStorage?.default ? globalThis.LeofcoinStorage.default : LeofcoinStorage;
|
|
2139
2294
|
}
|
|
2140
2295
|
globalThis[`${name}Store`] = globalThis[`${name}Store`] ||
|
|
2141
|
-
await new Storage(
|
|
2296
|
+
await new Storage(name, root);
|
|
2142
2297
|
|
|
2143
2298
|
globalThis[`${name}Store`].private = isPrivate;
|
|
2144
2299
|
if (!isPrivate) this.stores.push(name);
|
|
@@ -2231,7 +2386,7 @@ class Peernet {
|
|
|
2231
2386
|
const {daemon, environment} = await target();
|
|
2232
2387
|
this.hasDaemon = daemon;
|
|
2233
2388
|
|
|
2234
|
-
|
|
2389
|
+
|
|
2235
2390
|
|
|
2236
2391
|
for (const store of this.defaultStores) {
|
|
2237
2392
|
await this.addStore(store, options.storePrefix, options.root);
|
|
@@ -2297,6 +2452,16 @@ class Peernet {
|
|
|
2297
2452
|
this.requestProtos[name] = method;
|
|
2298
2453
|
}
|
|
2299
2454
|
|
|
2455
|
+
sendMessage(peer, id, data) {
|
|
2456
|
+
if (peer.readyState === 'open') {
|
|
2457
|
+
peer.send(new TextEncoder().encode(JSON.stringify({id, data})));
|
|
2458
|
+
this.bw.up += data.length;
|
|
2459
|
+
} else if (peer.readyState === 'closed') {
|
|
2460
|
+
this.removePeer(peer);
|
|
2461
|
+
}
|
|
2462
|
+
|
|
2463
|
+
}
|
|
2464
|
+
|
|
2300
2465
|
/**
|
|
2301
2466
|
* @private
|
|
2302
2467
|
*
|
|
@@ -2321,8 +2486,7 @@ class Peernet {
|
|
|
2321
2486
|
const data = new DHTMessageResponse({hash, has});
|
|
2322
2487
|
const node = await this.prepareMessage(from, data.encoded);
|
|
2323
2488
|
|
|
2324
|
-
|
|
2325
|
-
this.bw.up += node.encoded.length;
|
|
2489
|
+
this.sendMessage(peer, id, node.encoded);
|
|
2326
2490
|
} else if (proto.name === 'peernet-data') {
|
|
2327
2491
|
let { hash, store } = proto.decoded;
|
|
2328
2492
|
let data;
|
|
@@ -2335,24 +2499,19 @@ class Peernet {
|
|
|
2335
2499
|
data = await store.get(hash);
|
|
2336
2500
|
|
|
2337
2501
|
if (data) {
|
|
2338
|
-
data = new DataMessageResponse({hash, data
|
|
2502
|
+
data = new DataMessageResponse({hash, data});
|
|
2339
2503
|
|
|
2340
2504
|
const node = await this.prepareMessage(from, data.encoded);
|
|
2341
|
-
|
|
2342
|
-
this.bw.up += node.encoded.length;
|
|
2505
|
+
this.sendMessage(peer, id, node.encoded);
|
|
2343
2506
|
}
|
|
2344
2507
|
}
|
|
2345
2508
|
|
|
2346
2509
|
} else if (proto.name === 'peernet-request') {
|
|
2347
|
-
// TODO: make dynamic
|
|
2348
|
-
// exposeddevapi[proto.decoded.request](proto.decoded.params)
|
|
2349
2510
|
const method = this.requestProtos[proto.decoded.request];
|
|
2350
2511
|
if (method) {
|
|
2351
2512
|
const data = await method();
|
|
2352
2513
|
const node = await this.prepareMessage(from, data.encoded);
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
this.bw.up += node.encoded.length;
|
|
2514
|
+
this.sendMessage(peer, id, node.encoded);
|
|
2356
2515
|
}
|
|
2357
2516
|
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
2358
2517
|
globalSub.publish(new TextDecoder().decode(proto.decoded.topic), proto.decoded.data);
|
|
@@ -2622,13 +2781,9 @@ class Peernet {
|
|
|
2622
2781
|
const id = Math.random().toString(36).slice(-12);
|
|
2623
2782
|
data = new PsMessage({data, topic});
|
|
2624
2783
|
for (const peer of this.connections) {
|
|
2625
|
-
if (peer.
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
2629
|
-
}
|
|
2630
|
-
} else {
|
|
2631
|
-
this.removePeer(peer);
|
|
2784
|
+
if (peer.peerId !== this.peerId) {
|
|
2785
|
+
const node = await this.prepareMessage(peer.peerId, data.encoded);
|
|
2786
|
+
this.sendMessage(peer, id, node.encoded);
|
|
2632
2787
|
}
|
|
2633
2788
|
// TODO: if peer subscribed
|
|
2634
2789
|
}
|
|
@@ -2649,7 +2804,7 @@ class Peernet {
|
|
|
2649
2804
|
}
|
|
2650
2805
|
|
|
2651
2806
|
async removePeer(peer) {
|
|
2652
|
-
|
|
2807
|
+
return this.client.removePeer(peer)
|
|
2653
2808
|
}
|
|
2654
2809
|
|
|
2655
2810
|
get Buffer() {
|
package/index.html
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
<script>
|
|
12
12
|
|
|
13
13
|
(async () => {
|
|
14
|
-
const peernet = await new Peernet
|
|
14
|
+
const peernet = await new Peernet()
|
|
15
15
|
peernet.addRequestHandler('lastBlock', () => new peernet.protos['peernet-response']({response: new TextEncoder().encode(100)}))
|
|
16
16
|
})()
|
|
17
17
|
</script>
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.16",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/commonjs/peernet.js",
|
|
6
6
|
"module": "dist/module/peernet.js",
|
|
7
7
|
"browser": "dist/browser/peernet.js",
|
|
8
8
|
"scripts": {
|
|
9
|
-
"build": "webpack",
|
|
9
|
+
"build": "npm run c && webpack",
|
|
10
10
|
"test": "node test/index.js",
|
|
11
11
|
"server": "discovery-swarm-webrtc --port=4000",
|
|
12
12
|
"demo": "jsproject --serve ./ --port 6868",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@leofcoin/generate-account": "^1.0.2",
|
|
27
27
|
"@leofcoin/multi-wallet": "^2.1.2",
|
|
28
|
-
"@leofcoin/peernet-swarm": "^0.1.
|
|
28
|
+
"@leofcoin/peernet-swarm": "^0.1.17",
|
|
29
29
|
"@leofcoin/storage": "^2.3.0",
|
|
30
30
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
31
31
|
"@vandeurenglenn/base58": "^1.1.0",
|
|
@@ -42,15 +42,10 @@
|
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@babel/plugin-proposal-private-methods": "^7.16.11",
|
|
45
|
-
"@rollup/plugin-commonjs": "^19.0.1",
|
|
46
45
|
"@rollup/plugin-eslint": "^8.0.1",
|
|
47
46
|
"@rollup/plugin-json": "^4.1.0",
|
|
48
|
-
"@rollup/plugin-node-resolve": "^13.0.4",
|
|
49
47
|
"@vandeurenglenn/debug": "^1.0.0",
|
|
50
48
|
"coveralls": "^3.1.1",
|
|
51
|
-
"esdoc": "^1.1.0",
|
|
52
|
-
"esdoc-ecmascript-proposal-plugin": "^1.0.0",
|
|
53
|
-
"esdoc-standard-plugin": "^1.0.0",
|
|
54
49
|
"eslint": "^7.31.0",
|
|
55
50
|
"eslint-config-google": "^0.14.0",
|
|
56
51
|
"nyc": "^15.1.0",
|
|
@@ -58,10 +53,8 @@
|
|
|
58
53
|
"path-browserify": "^1.0.1",
|
|
59
54
|
"rollup": "^2.70.2",
|
|
60
55
|
"rollup-plugin-modify": "^3.0.0",
|
|
61
|
-
"rollup-plugin-natives": "^0.7.5",
|
|
62
|
-
"rollup-plugin-node-globals": "^1.4.0",
|
|
63
|
-
"rollup-plugin-node-polyfills": "^0.2.1",
|
|
64
56
|
"tape": "^5.2.2",
|
|
57
|
+
"vm-browserify": "^1.1.2",
|
|
65
58
|
"webpack": "^5.72.0",
|
|
66
59
|
"webpack-cli": "^4.9.2",
|
|
67
60
|
"webpack-node-externals": "^3.0.0"
|
package/rollup.config.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import { execSync } from 'child_process';
|
|
2
2
|
import lint from '@rollup/plugin-eslint'
|
|
3
|
-
import resolve from '@rollup/plugin-node-resolve'
|
|
4
|
-
import cjs from '@rollup/plugin-commonjs'
|
|
5
3
|
import json from '@rollup/plugin-json'
|
|
6
4
|
import modify from 'rollup-plugin-modify'
|
|
7
|
-
import nativePlugin from 'rollup-plugin-natives';
|
|
8
|
-
import polyfills from 'rollup-plugin-node-polyfills'
|
|
9
5
|
|
|
10
6
|
try {
|
|
11
7
|
execSync('rm dist -r')
|
|
@@ -31,7 +27,8 @@ export default [{
|
|
|
31
27
|
} else {
|
|
32
28
|
const http = await import('./http/http.js')
|
|
33
29
|
if (environment !== 'browser') http.default(options)
|
|
34
|
-
}
|
|
30
|
+
}`,
|
|
31
|
+
SUBTLE_IMPORT: `const { subtle } = require('crypto').webcrypto`
|
|
35
32
|
})
|
|
36
33
|
]
|
|
37
34
|
}, {
|
|
@@ -44,25 +41,13 @@ export default [{
|
|
|
44
41
|
json(),
|
|
45
42
|
modify({
|
|
46
43
|
"import fetch from 'node-fetch'": '',
|
|
47
|
-
HTTP_IMPORT:
|
|
44
|
+
HTTP_IMPORT: ``,
|
|
45
|
+
SUBTLE_IMPORT: `const { subtle } = crypto`
|
|
48
46
|
}),
|
|
49
|
-
// nativePlugin(),
|
|
50
|
-
// polyfills(),
|
|
51
|
-
// resolve({
|
|
52
|
-
// preferBuiltins: true,
|
|
53
|
-
// mainFields: ["browser", "module", "main"],
|
|
54
|
-
// extensions: ['.mjs', '.js', '.json']
|
|
55
|
-
// }),
|
|
56
|
-
// cjs({
|
|
57
|
-
// exclude: ['*.node'],
|
|
58
|
-
// extensions: ['.js']
|
|
59
|
-
// }),
|
|
60
47
|
// lint({
|
|
61
48
|
// fix: true,
|
|
62
49
|
// exclude: ['package.json', "package-lock.json"]
|
|
63
50
|
// })
|
|
64
|
-
|
|
65
|
-
|
|
66
51
|
]
|
|
67
52
|
}, {
|
|
68
53
|
input: 'src/peernet.js',
|
|
@@ -71,6 +56,11 @@ export default [{
|
|
|
71
56
|
format: 'es'
|
|
72
57
|
},
|
|
73
58
|
plugins: [
|
|
74
|
-
json()
|
|
59
|
+
json(),
|
|
60
|
+
modify({
|
|
61
|
+
"import fetch from 'node-fetch'": '',
|
|
62
|
+
HTTP_IMPORT: ``,
|
|
63
|
+
SUBTLE_IMPORT: `const { subtle } = crypto`
|
|
64
|
+
}),
|
|
75
65
|
]
|
|
76
66
|
}]
|
|
@@ -15,14 +15,14 @@ export default class FormatInterface {
|
|
|
15
15
|
this.protoDecode = proto.decode
|
|
16
16
|
this.hashFormat = options.hashFormat || 'bs32'
|
|
17
17
|
if (options.name) this.name = options.name
|
|
18
|
-
if (buffer instanceof Uint8Array)
|
|
19
|
-
else if (buffer instanceof ArrayBuffer)
|
|
18
|
+
if (buffer instanceof Uint8Array) this.fromUint8Array(buffer)
|
|
19
|
+
else if (buffer instanceof ArrayBuffer) this.fromArrayBuffer(buffer)
|
|
20
20
|
else if (buffer.name === options.name) return buffer
|
|
21
|
-
else if (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
else if (buffer instanceof String) {
|
|
22
|
+
if (isHex(buffer)) this.fromHex(buffer)
|
|
23
|
+
else if (bs32.isBase32(buffer)) this.fromBs32(buffer)
|
|
24
|
+
else if (bs58.isBase58(buffer)) this.fromBs58(buffer)
|
|
25
|
+
else throw new Error(`unsupported string ${buffer}`)
|
|
26
26
|
} else {
|
|
27
27
|
this.create(buffer)
|
|
28
28
|
}
|
package/src/handlers/message.js
CHANGED
|
@@ -21,7 +21,7 @@ export default class MessageHandler {
|
|
|
21
21
|
let identity = await walletStore.get('identity')
|
|
22
22
|
identity = JSON.parse(new TextDecoder().decode(identity))
|
|
23
23
|
const wallet = new MultiWallet(this.network)
|
|
24
|
-
wallet.
|
|
24
|
+
wallet.recover(identity.mnemonic)
|
|
25
25
|
return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
|
|
26
26
|
}
|
|
27
27
|
|