@leofcoin/peernet 0.18.8 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/exports/browser/browser-store.js +141 -141
- package/exports/browser/{client-ae251a9c.js → client-95423644.js} +2 -2
- package/exports/browser/{index-72471f52.js → index-3699fac4.js} +7 -9
- package/exports/browser/{messages-1f20bad9.js → messages-2214fcbb.js} +2 -2
- package/exports/browser/{index-769b10c7.js → peernet-e74b2091.js} +3388 -314
- package/exports/browser/peernet.js +2 -2
- package/exports/browser/prompts/password/browser.d.ts +2 -0
- package/exports/browser/prompts/password/node.d.ts +2 -0
- package/exports/browser/prompts/password.js +3 -0
- package/exports/browser/{value-40634404.js → value-157ab062.js} +59 -59
- package/exports/peernet.js +84 -42
- package/exports/prompts/password/browser.d.ts +2 -0
- package/exports/prompts/password/node.d.ts +2 -0
- package/exports/prompts/password.js +8 -0
- package/exports/store.js +210 -212
- package/package.json +3 -2
- package/rollup.config.js +18 -0
- package/src/handlers/message.js +2 -9
- package/src/identity.ts +81 -0
- package/src/peernet.ts +17 -34
- package/src/prompts/password/browser.js +1 -0
- package/src/prompts/password/node.js +6 -0
- package/exports/browser/peernet-34ea081d.js +0 -2999
|
@@ -1,5 +1,2371 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { K as KeyValue } from './value-157ab062.js';
|
|
2
|
+
|
|
3
|
+
if (!globalThis.DEBUG) {
|
|
4
|
+
if (globalThis.localStorage) globalThis.DEBUG = Boolean(globalThis.localStorage.getItem('DEBUG') === 'true');
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
globalThis.debug = text => {
|
|
8
|
+
if (globalThis.DEBUG) console.log('\x1b[34m\x1b[1m%s', text, '\x1b[0m'); // bright blue
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
class LittlePubSub {
|
|
12
|
+
subscribers = {};
|
|
13
|
+
verbose;
|
|
14
|
+
constructor(verbose = false) {
|
|
15
|
+
this.verbose = verbose;
|
|
16
|
+
}
|
|
17
|
+
#handleContext(handler, context) {
|
|
18
|
+
if (typeof context === 'undefined') {
|
|
19
|
+
context = handler;
|
|
20
|
+
}
|
|
21
|
+
return context;
|
|
22
|
+
}
|
|
23
|
+
hasSubscribers(event) {
|
|
24
|
+
return this.subscribers[event] ? true : false;
|
|
25
|
+
}
|
|
26
|
+
subscribe(event, handler, context) {
|
|
27
|
+
if (!this.hasSubscribers(event))
|
|
28
|
+
this.subscribers[event] = { handlers: [], value: undefined };
|
|
29
|
+
context = this.#handleContext(handler, context);
|
|
30
|
+
this.subscribers[event].handlers.push(handler.bind(context));
|
|
31
|
+
}
|
|
32
|
+
unsubscribe(event, handler, context) {
|
|
33
|
+
if (!this.hasSubscribers(event))
|
|
34
|
+
return;
|
|
35
|
+
context = this.#handleContext(handler, context);
|
|
36
|
+
const index = this.subscribers[event].handlers.indexOf(handler.bind(context));
|
|
37
|
+
this.subscribers[event].handlers.splice(index);
|
|
38
|
+
if (this.subscribers[event].handlers.length === 0)
|
|
39
|
+
delete this.subscribers[event];
|
|
40
|
+
}
|
|
41
|
+
publish(event, change) {
|
|
42
|
+
if (!this.hasSubscribers(event))
|
|
43
|
+
return;
|
|
44
|
+
if (this.verbose || this.subscribers[event].value !== change) {
|
|
45
|
+
this.subscribers[event].value = change;
|
|
46
|
+
this.subscribers[event].handlers.forEach((handler) => {
|
|
47
|
+
handler(change, this.subscribers[event].value);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
once(event) {
|
|
52
|
+
return new Promise((resolve) => {
|
|
53
|
+
const cb = (value) => {
|
|
54
|
+
this.unsubscribe(event, cb);
|
|
55
|
+
resolve(value);
|
|
56
|
+
};
|
|
57
|
+
this.subscribe(event, cb);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// base-x encoding / decoding
|
|
63
|
+
// Copyright (c) 2018 base-x contributors
|
|
64
|
+
// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
|
|
65
|
+
// Distributed under the MIT software license, see the accompanying
|
|
66
|
+
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
|
67
|
+
const base$1 = (ALPHABET) => {
|
|
68
|
+
if (ALPHABET.length >= 255) {
|
|
69
|
+
throw new TypeError('Alphabet too long');
|
|
70
|
+
}
|
|
71
|
+
const BASE_MAP = new Uint8Array(256);
|
|
72
|
+
for (let j = 0; j < BASE_MAP.length; j++) {
|
|
73
|
+
BASE_MAP[j] = 255;
|
|
74
|
+
}
|
|
75
|
+
for (let i = 0; i < ALPHABET.length; i++) {
|
|
76
|
+
const x = ALPHABET.charAt(i);
|
|
77
|
+
const xc = x.charCodeAt(0);
|
|
78
|
+
if (BASE_MAP[xc] !== 255) {
|
|
79
|
+
throw new TypeError(x + ' is ambiguous');
|
|
80
|
+
}
|
|
81
|
+
BASE_MAP[xc] = i;
|
|
82
|
+
}
|
|
83
|
+
const BASE = ALPHABET.length;
|
|
84
|
+
const LEADER = ALPHABET.charAt(0);
|
|
85
|
+
const FACTOR = Math.log(BASE) / Math.log(256); // log(BASE) / log(256), rounded up
|
|
86
|
+
const iFACTOR = Math.log(256) / Math.log(BASE); // log(256) / log(BASE), rounded up
|
|
87
|
+
const encode = (source) => {
|
|
88
|
+
if (source instanceof Uint8Array) ;
|
|
89
|
+
else if (ArrayBuffer.isView(source)) {
|
|
90
|
+
source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
91
|
+
}
|
|
92
|
+
else if (Array.isArray(source)) {
|
|
93
|
+
source = Uint8Array.from(source);
|
|
94
|
+
}
|
|
95
|
+
if (!(source instanceof Uint8Array)) {
|
|
96
|
+
throw new TypeError('Expected Uint8Array');
|
|
97
|
+
}
|
|
98
|
+
if (source.length === 0) {
|
|
99
|
+
return '';
|
|
100
|
+
}
|
|
101
|
+
// Skip & count leading zeroes.
|
|
102
|
+
let zeroes = 0;
|
|
103
|
+
let length = 0;
|
|
104
|
+
let pbegin = 0;
|
|
105
|
+
const pend = source.length;
|
|
106
|
+
while (pbegin !== pend && source[pbegin] === 0) {
|
|
107
|
+
pbegin++;
|
|
108
|
+
zeroes++;
|
|
109
|
+
}
|
|
110
|
+
// Allocate enough space in big-endian base58 representation.
|
|
111
|
+
const size = ((pend - pbegin) * iFACTOR + 1) >>> 0;
|
|
112
|
+
const b58 = new Uint8Array(size);
|
|
113
|
+
// Process the bytes.
|
|
114
|
+
while (pbegin !== pend) {
|
|
115
|
+
let carry = source[pbegin];
|
|
116
|
+
// Apply "b58 = b58 * 256 + ch".
|
|
117
|
+
let i = 0;
|
|
118
|
+
for (let it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
|
|
119
|
+
carry += (256 * b58[it1]) >>> 0;
|
|
120
|
+
b58[it1] = (carry % BASE) >>> 0;
|
|
121
|
+
carry = (carry / BASE) >>> 0;
|
|
122
|
+
}
|
|
123
|
+
if (carry !== 0) {
|
|
124
|
+
throw new Error('Non-zero carry');
|
|
125
|
+
}
|
|
126
|
+
length = i;
|
|
127
|
+
pbegin++;
|
|
128
|
+
}
|
|
129
|
+
// Skip leading zeroes in base58 result.
|
|
130
|
+
let it2 = size - length;
|
|
131
|
+
while (it2 !== size && b58[it2] === 0) {
|
|
132
|
+
it2++;
|
|
133
|
+
}
|
|
134
|
+
// Translate the result into a string.
|
|
135
|
+
let str = LEADER.repeat(zeroes);
|
|
136
|
+
for (; it2 < size; ++it2) {
|
|
137
|
+
str += ALPHABET.charAt(b58[it2]);
|
|
138
|
+
}
|
|
139
|
+
return str;
|
|
140
|
+
};
|
|
141
|
+
const decodeUnsafe = (source) => {
|
|
142
|
+
if (typeof source !== 'string') {
|
|
143
|
+
throw new TypeError('Expected String');
|
|
144
|
+
}
|
|
145
|
+
if (source.length === 0) {
|
|
146
|
+
return new Uint8Array();
|
|
147
|
+
}
|
|
148
|
+
let psz = 0;
|
|
149
|
+
// Skip and count leading '1's.
|
|
150
|
+
let zeroes = 0;
|
|
151
|
+
let length = 0;
|
|
152
|
+
while (source[psz] === LEADER) {
|
|
153
|
+
zeroes++;
|
|
154
|
+
psz++;
|
|
155
|
+
}
|
|
156
|
+
// Allocate enough space in big-endian base256 representation.
|
|
157
|
+
const size = (((source.length - psz) * FACTOR) + 1) >>> 0; // log(58) / log(256), rounded up.
|
|
158
|
+
let b256 = new Uint8Array(size);
|
|
159
|
+
// Process the characters.
|
|
160
|
+
while (source[psz]) {
|
|
161
|
+
// Decode character
|
|
162
|
+
let carry = BASE_MAP[source.charCodeAt(psz)];
|
|
163
|
+
// Invalid character
|
|
164
|
+
if (carry === 255) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
let i = 0;
|
|
168
|
+
for (let it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
|
|
169
|
+
carry += (BASE * b256[it3]) >>> 0;
|
|
170
|
+
b256[it3] = (carry % 256) >>> 0;
|
|
171
|
+
carry = (carry / 256) >>> 0;
|
|
172
|
+
}
|
|
173
|
+
if (carry !== 0) {
|
|
174
|
+
throw new Error('Non-zero carry');
|
|
175
|
+
}
|
|
176
|
+
length = i;
|
|
177
|
+
psz++;
|
|
178
|
+
}
|
|
179
|
+
// Skip leading zeroes in b256.
|
|
180
|
+
let it4 = size - length;
|
|
181
|
+
while (it4 !== size && b256[it4] === 0) {
|
|
182
|
+
it4++;
|
|
183
|
+
}
|
|
184
|
+
let vch = new Uint8Array(zeroes + (size - it4));
|
|
185
|
+
let j = zeroes;
|
|
186
|
+
while (it4 !== size) {
|
|
187
|
+
vch[j++] = b256[it4++];
|
|
188
|
+
}
|
|
189
|
+
return vch;
|
|
190
|
+
};
|
|
191
|
+
const decode = (string) => {
|
|
192
|
+
const buffer = decodeUnsafe(string);
|
|
193
|
+
if (buffer) {
|
|
194
|
+
return buffer;
|
|
195
|
+
}
|
|
196
|
+
throw new Error('Non-base' + BASE + ' character');
|
|
197
|
+
};
|
|
198
|
+
return {
|
|
199
|
+
encode,
|
|
200
|
+
decodeUnsafe,
|
|
201
|
+
decode
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const ALPHABET$3 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
|
|
206
|
+
const ALPHABET_HEX$1 = '0123456789ABCDEFGHIJKLMNOPQRSTUV';
|
|
207
|
+
const base32 = base$1(ALPHABET$3);
|
|
208
|
+
const base32Hex = base$1(ALPHABET_HEX$1);
|
|
209
|
+
const decode$9 = base32.decode;
|
|
210
|
+
const decodeHex$2 = base32Hex.decode;
|
|
211
|
+
const encode$8 = base32.encode;
|
|
212
|
+
const encodeHex$2 = base32Hex.encode;
|
|
213
|
+
const isBase32 = (string, hex = false) => {
|
|
214
|
+
try {
|
|
215
|
+
if (hex)
|
|
216
|
+
decodeHex$2(string);
|
|
217
|
+
else
|
|
218
|
+
decode$9(string);
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
catch (e) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
const isBase32Hex = (string) => {
|
|
226
|
+
return isBase32(string, true);
|
|
227
|
+
};
|
|
228
|
+
var index$8 = {
|
|
229
|
+
encode: encode$8,
|
|
230
|
+
decode: decode$9,
|
|
231
|
+
encodeHex: encodeHex$2,
|
|
232
|
+
decodeHex: decodeHex$2,
|
|
233
|
+
isBase32,
|
|
234
|
+
isBase32Hex
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
var ALPHABET$2 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
238
|
+
var ALPHABET_HEX = '0123456789ABCDEFGHJKLMNPQRSTUVabcdefghijklmnopqrstuv';
|
|
239
|
+
var base58 = base$1(ALPHABET$2);
|
|
240
|
+
var base58Hex = base$1(ALPHABET_HEX);
|
|
241
|
+
var encode$7 = base58.encode;
|
|
242
|
+
var decode$8 = base58.decode;
|
|
243
|
+
var encodeHex$1 = base58Hex.encode;
|
|
244
|
+
var decodeHex$1 = base58Hex.decode;
|
|
245
|
+
var isBase58 = function (string) {
|
|
246
|
+
try {
|
|
247
|
+
decode$8(string);
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
catch (e) {
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
var isBase58Hex = function (string) {
|
|
255
|
+
try {
|
|
256
|
+
decodeHex$1(string);
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
catch (e) {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
var whatType = function (string) {
|
|
264
|
+
try {
|
|
265
|
+
decode$8(string);
|
|
266
|
+
return 'base58';
|
|
267
|
+
}
|
|
268
|
+
catch (e) {
|
|
269
|
+
try {
|
|
270
|
+
decodeHex$1(string);
|
|
271
|
+
return 'base58Hex';
|
|
272
|
+
}
|
|
273
|
+
catch (_a) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
var base58$1 = { encode: encode$7, decode: decode$8, isBase58: isBase58, isBase58Hex: isBase58Hex, encodeHex: encodeHex$1, decodeHex: decodeHex$1, whatType: whatType };
|
|
279
|
+
|
|
280
|
+
const MSB$1 = 0x80;
|
|
281
|
+
const REST$1 = 0x7F;
|
|
282
|
+
const MSBALL = ~REST$1;
|
|
283
|
+
const INT = Math.pow(2, 31);
|
|
284
|
+
const encode$6 = (num, out, offset) => {
|
|
285
|
+
if (Number.MAX_SAFE_INTEGER && num > Number.MAX_SAFE_INTEGER) {
|
|
286
|
+
encode$6.bytes = 0;
|
|
287
|
+
throw new RangeError('Could not encode varint');
|
|
288
|
+
}
|
|
289
|
+
out = out || [];
|
|
290
|
+
offset = offset || 0;
|
|
291
|
+
const oldOffset = offset;
|
|
292
|
+
while (num >= INT) {
|
|
293
|
+
out[offset++] = (num & 0xFF) | MSB$1;
|
|
294
|
+
num /= 128;
|
|
295
|
+
}
|
|
296
|
+
while (num & MSBALL) {
|
|
297
|
+
out[offset++] = (num & 0xFF) | MSB$1;
|
|
298
|
+
num >>>= 7;
|
|
299
|
+
}
|
|
300
|
+
out[offset] = num | 0;
|
|
301
|
+
encode$6.bytes = offset - oldOffset + 1;
|
|
302
|
+
return out;
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const MSB = 0x80;
|
|
306
|
+
const REST = 0x7F;
|
|
307
|
+
const decode$7 = (buf, offset) => {
|
|
308
|
+
offset = offset || 0;
|
|
309
|
+
const l = buf.length;
|
|
310
|
+
let counter = offset;
|
|
311
|
+
let result = 0;
|
|
312
|
+
let shift = 0;
|
|
313
|
+
let b;
|
|
314
|
+
do {
|
|
315
|
+
if (counter >= l || shift > 49) {
|
|
316
|
+
decode$7.bytes = 0;
|
|
317
|
+
throw new RangeError('Could not decode varint');
|
|
318
|
+
}
|
|
319
|
+
b = buf[counter++];
|
|
320
|
+
result += shift < 28
|
|
321
|
+
? (b & REST) << shift
|
|
322
|
+
: (b & REST) * Math.pow(2, shift);
|
|
323
|
+
shift += 7;
|
|
324
|
+
} while (b >= MSB);
|
|
325
|
+
decode$7.bytes = counter - offset;
|
|
326
|
+
return result;
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
const N1 = Math.pow(2, 7);
|
|
330
|
+
const N2 = Math.pow(2, 14);
|
|
331
|
+
const N3 = Math.pow(2, 21);
|
|
332
|
+
const N4 = Math.pow(2, 28);
|
|
333
|
+
const N5 = Math.pow(2, 35);
|
|
334
|
+
const N6 = Math.pow(2, 42);
|
|
335
|
+
const N7 = Math.pow(2, 49);
|
|
336
|
+
const N8 = Math.pow(2, 56);
|
|
337
|
+
const N9 = Math.pow(2, 63);
|
|
338
|
+
var encodingLength = (value) => (value < N1 ? 1
|
|
339
|
+
: value < N2 ? 2
|
|
340
|
+
: value < N3 ? 3
|
|
341
|
+
: value < N4 ? 4
|
|
342
|
+
: value < N5 ? 5
|
|
343
|
+
: value < N6 ? 6
|
|
344
|
+
: value < N7 ? 7
|
|
345
|
+
: value < N8 ? 8
|
|
346
|
+
: value < N9 ? 9
|
|
347
|
+
: 10);
|
|
348
|
+
|
|
349
|
+
var index$7 = {
|
|
350
|
+
encode: encode$6,
|
|
351
|
+
decode: decode$7,
|
|
352
|
+
encodingLength
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
var index$6 = (input, prefix) => {
|
|
356
|
+
const encodedArray = [];
|
|
357
|
+
const length = input.reduce((total, current) => {
|
|
358
|
+
const encoded = index$7.encode(current.length);
|
|
359
|
+
encodedArray.push(encoded);
|
|
360
|
+
total += current.length + encoded.length;
|
|
361
|
+
return total;
|
|
362
|
+
}, 0);
|
|
363
|
+
const typedArray = new Uint8Array(prefix ? prefix.length + length : length);
|
|
364
|
+
let currentIndex = 0;
|
|
365
|
+
let index = 0;
|
|
366
|
+
if (prefix) {
|
|
367
|
+
typedArray.set(prefix);
|
|
368
|
+
currentIndex += prefix.length;
|
|
369
|
+
}
|
|
370
|
+
for (const source of input) {
|
|
371
|
+
typedArray.set(encodedArray[index], currentIndex);
|
|
372
|
+
currentIndex += encodedArray[index].length;
|
|
373
|
+
typedArray.set(source, currentIndex);
|
|
374
|
+
currentIndex += source.length;
|
|
375
|
+
index += 1;
|
|
376
|
+
}
|
|
377
|
+
return typedArray;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
var index$5 = (typedArray, prefix) => {
|
|
381
|
+
const set = [];
|
|
382
|
+
if (prefix)
|
|
383
|
+
typedArray = typedArray.subarray(prefix.length);
|
|
384
|
+
const varintAndSub = (typedArray) => {
|
|
385
|
+
const length = index$7.decode(typedArray);
|
|
386
|
+
// remove length
|
|
387
|
+
typedArray = typedArray.subarray(index$7.decode.bytes);
|
|
388
|
+
// push value
|
|
389
|
+
set.push(typedArray.subarray(0, length));
|
|
390
|
+
// remove value
|
|
391
|
+
typedArray = typedArray.subarray(length);
|
|
392
|
+
if (typedArray.length !== 0)
|
|
393
|
+
return varintAndSub(typedArray);
|
|
394
|
+
return set;
|
|
395
|
+
};
|
|
396
|
+
return varintAndSub(typedArray);
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
const ALPHABET$1 = '0123456789ABCDEF';
|
|
400
|
+
const base16 = base$1(ALPHABET$1);
|
|
401
|
+
const decode$6 = base16.decode;
|
|
402
|
+
const encode$5 = base16.encode;
|
|
403
|
+
const isBase16 = (string) => {
|
|
404
|
+
try {
|
|
405
|
+
decode$6(string);
|
|
406
|
+
return true;
|
|
407
|
+
}
|
|
408
|
+
catch (e) {
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
var index$4 = {
|
|
413
|
+
encode: encode$5,
|
|
414
|
+
decode: decode$6,
|
|
415
|
+
isBase16
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
419
|
+
const base64 = base$1(ALPHABET);
|
|
420
|
+
const decode$5 = base64.decode;
|
|
421
|
+
const encode$4 = base64.encode;
|
|
422
|
+
const isBase64 = (string) => {
|
|
423
|
+
try {
|
|
424
|
+
decode$5(string);
|
|
425
|
+
return true;
|
|
426
|
+
}
|
|
427
|
+
catch (e) {
|
|
428
|
+
return false;
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
var index$3 = {
|
|
432
|
+
encode: encode$4,
|
|
433
|
+
decode: decode$5,
|
|
434
|
+
isBase64
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
const isTypedArrayCompatible = (possibleUint8Array) => {
|
|
438
|
+
if (typeof possibleUint8Array === 'string') {
|
|
439
|
+
possibleUint8Array = possibleUint8Array.split(',').map(number => Number(number));
|
|
440
|
+
for (const number of possibleUint8Array) {
|
|
441
|
+
if (isNaN(number))
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
for (const number of possibleUint8Array) {
|
|
446
|
+
if (isNaN(number))
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
return true;
|
|
450
|
+
};
|
|
451
|
+
/**
|
|
452
|
+
* Returns a String as Uint8Array
|
|
453
|
+
* @param string string to encode to Uint8Array
|
|
454
|
+
* @returns Uint8Array
|
|
455
|
+
*/
|
|
456
|
+
const fromString$1 = (string) => new TextEncoder().encode(string);
|
|
457
|
+
/**
|
|
458
|
+
* Returns a Uint8Array as String
|
|
459
|
+
* @param uint8Array Uint8Array to encode to String
|
|
460
|
+
* @returns String
|
|
461
|
+
*/
|
|
462
|
+
const toString$1 = (uint8Array) => new TextDecoder().decode(uint8Array);
|
|
463
|
+
/**
|
|
464
|
+
* Returns a String as Uint8Array
|
|
465
|
+
* @param string string to encode to Uint8Array
|
|
466
|
+
* @returns Uint8Array
|
|
467
|
+
*/
|
|
468
|
+
const fromUintArrayString = (string) => Uint8Array.from(string.split(',').map(string => Number(string)));
|
|
469
|
+
/**
|
|
470
|
+
* Returns a Uint8Array as String
|
|
471
|
+
* @param uint8Array Uint8Array to encode to String
|
|
472
|
+
* @returns String
|
|
473
|
+
*/
|
|
474
|
+
const toUintArrayString = (uint8Array) => uint8Array.toString();
|
|
475
|
+
/**
|
|
476
|
+
* hexString -> uint8Array
|
|
477
|
+
* @param string hex encoded string
|
|
478
|
+
* @returns UintArray
|
|
479
|
+
*/
|
|
480
|
+
const fromHex = (string) => Uint8Array.from(string.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
|
481
|
+
/**
|
|
482
|
+
* uint8Array -> hexString
|
|
483
|
+
* @param bytes number[]
|
|
484
|
+
* @returns hexString
|
|
485
|
+
*/
|
|
486
|
+
const toHex$1 = (bytes) => bytes.reduce((string, byte) => string + byte.toString(16).padStart(2, '0'), '');
|
|
487
|
+
/**
|
|
488
|
+
* number[] -> Uint8Array
|
|
489
|
+
* @param array number[]
|
|
490
|
+
* @returns Uint8Array
|
|
491
|
+
*/
|
|
492
|
+
const fromArrayLike = (array) => Uint8Array.from(array);
|
|
493
|
+
/**
|
|
494
|
+
* Uint8Array -> number[]
|
|
495
|
+
* @param uint8Array Uint8Array
|
|
496
|
+
* @returns Uint8Array
|
|
497
|
+
*/
|
|
498
|
+
const toArrayLike = (uint8Array) => [...uint8Array.values()];
|
|
499
|
+
const toBase64 = (uint8Array) => index$3.encode(uint8Array);
|
|
500
|
+
const fromBase64 = (string) => index$3.decode(string);
|
|
501
|
+
const toBase58 = (uint8Array) => base58$1.encode(uint8Array);
|
|
502
|
+
const fromBase58 = (string) => base58$1.decode(string);
|
|
503
|
+
const toBase32 = (uint8Array) => index$8.encode(uint8Array);
|
|
504
|
+
const fromBase32 = (string) => index$8.decode(string);
|
|
505
|
+
const toBase16 = (uint8Array) => index$4.encode(uint8Array);
|
|
506
|
+
const fromBase16 = (string) => index$4.decode(string);
|
|
507
|
+
let FormatInterface$2 = class FormatInterface {
|
|
508
|
+
encoded;
|
|
509
|
+
constructor(input) {
|
|
510
|
+
if (input) {
|
|
511
|
+
if (index$4.isBase16(input))
|
|
512
|
+
this.encoded = this.fromBase16(input);
|
|
513
|
+
else if (index$8.isBase32(input))
|
|
514
|
+
this.encoded = this.fromBase32(input);
|
|
515
|
+
else if (base58$1.isBase58(input))
|
|
516
|
+
this.encoded = this.fromBase58(input);
|
|
517
|
+
else if (index$3.isBase64(input))
|
|
518
|
+
this.encoded = this.fromBase64(input);
|
|
519
|
+
else if (typeof input === 'string') {
|
|
520
|
+
let isCompatible = isTypedArrayCompatible(input);
|
|
521
|
+
if (isCompatible)
|
|
522
|
+
this.encoded = fromUintArrayString(input);
|
|
523
|
+
else
|
|
524
|
+
this.encoded = this.fromString(input); // normal string
|
|
525
|
+
}
|
|
526
|
+
else if (typeof input === 'object')
|
|
527
|
+
this.encoded = this.fromObject(input);
|
|
528
|
+
else if (input instanceof Uint8Array)
|
|
529
|
+
this.encoded = input;
|
|
530
|
+
else if (Array.isArray(input) && isTypedArrayCompatible(input))
|
|
531
|
+
this.encoded = this.fromArrayLike(input);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Returns a String as Uint8Array
|
|
536
|
+
* @param string string to encode to Uint8Array
|
|
537
|
+
* @returns Uint8Array
|
|
538
|
+
*/
|
|
539
|
+
fromString(string) {
|
|
540
|
+
return new TextEncoder().encode(string);
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Returns a Uint8Array as String
|
|
544
|
+
* @param uint8Array Uint8Array to encode to String
|
|
545
|
+
* @returns String
|
|
546
|
+
*/
|
|
547
|
+
toString(uint8Array) {
|
|
548
|
+
return new TextDecoder().decode(uint8Array);
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Returns a String as Uint8Array
|
|
552
|
+
* @param string string to encode to Uint8Array
|
|
553
|
+
* @returns Uint8Array
|
|
554
|
+
*/
|
|
555
|
+
fromUintArrayString(string) {
|
|
556
|
+
return Uint8Array.from(string.split(',').map(string => Number(string)));
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Returns a Uint8Array as String
|
|
560
|
+
* @param uint8Array Uint8Array to encode to String
|
|
561
|
+
* @returns String
|
|
562
|
+
*/
|
|
563
|
+
toUintArrayString(uint8Array) {
|
|
564
|
+
return uint8Array.toString();
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* hexString -> uint8Array
|
|
568
|
+
* @param string hex encoded string
|
|
569
|
+
* @returns UintArray
|
|
570
|
+
*/
|
|
571
|
+
fromHex(string) {
|
|
572
|
+
return Uint8Array.from(string.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* uint8Array -> hexString
|
|
576
|
+
* @param bytes number[]
|
|
577
|
+
* @returns hexString
|
|
578
|
+
*/
|
|
579
|
+
toHex(bytes) {
|
|
580
|
+
return bytes.reduce((string, byte) => string + byte.toString(16).padStart(2, '0'), '');
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* number[] -> Uint8Array
|
|
584
|
+
* @param array number[]
|
|
585
|
+
* @returns Uint8Array
|
|
586
|
+
*/
|
|
587
|
+
fromArrayLike(array) {
|
|
588
|
+
return Uint8Array.from(array);
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Uint8Array -> number[]
|
|
592
|
+
* @param uint8Array Uint8Array
|
|
593
|
+
* @returns Uint8Array
|
|
594
|
+
*/
|
|
595
|
+
toArrayLike(uint8Array) {
|
|
596
|
+
return [...uint8Array.values()];
|
|
597
|
+
}
|
|
598
|
+
fromObject(object) {
|
|
599
|
+
return new TextEncoder().encode(JSON.stringify(object));
|
|
600
|
+
}
|
|
601
|
+
toObject(uint8Array) {
|
|
602
|
+
return JSON.parse(new TextDecoder().decode(uint8Array));
|
|
603
|
+
}
|
|
604
|
+
toBase64(uint8Array) {
|
|
605
|
+
return index$3.encode(uint8Array);
|
|
606
|
+
}
|
|
607
|
+
fromBase64(string) {
|
|
608
|
+
return index$3.decode(string);
|
|
609
|
+
}
|
|
610
|
+
toBase58(uint8Array) {
|
|
611
|
+
return base58$1.encode(uint8Array);
|
|
612
|
+
}
|
|
613
|
+
fromBase58(string) {
|
|
614
|
+
return base58$1.decode(string);
|
|
615
|
+
}
|
|
616
|
+
toBase32(uint8Array) {
|
|
617
|
+
return index$8.encode(uint8Array);
|
|
618
|
+
}
|
|
619
|
+
fromBase32(string) {
|
|
620
|
+
return index$8.decode(string);
|
|
621
|
+
}
|
|
622
|
+
toBase16(uint8Array) {
|
|
623
|
+
return index$4.encode(uint8Array);
|
|
624
|
+
}
|
|
625
|
+
fromBase16(string) {
|
|
626
|
+
return index$4.decode(string);
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
var index$2 = {
|
|
630
|
+
fromString: fromString$1,
|
|
631
|
+
toString: toString$1,
|
|
632
|
+
fromHex,
|
|
633
|
+
toHex: toHex$1,
|
|
634
|
+
fromArrayLike,
|
|
635
|
+
toArrayLike,
|
|
636
|
+
fromUintArrayString,
|
|
637
|
+
toUintArrayString,
|
|
638
|
+
toBase64,
|
|
639
|
+
fromBase64,
|
|
640
|
+
toBase58,
|
|
641
|
+
fromBase58,
|
|
642
|
+
toBase32,
|
|
643
|
+
fromBase32,
|
|
644
|
+
toBase16,
|
|
645
|
+
fromBase16,
|
|
646
|
+
FormatInterface: FormatInterface$2
|
|
647
|
+
};
|
|
648
|
+
|
|
649
|
+
const { fromString, toString } = index$2;
|
|
650
|
+
const isJson = (type) => type === 'object' || 'array';
|
|
651
|
+
const isString = (type) => type === 'string';
|
|
652
|
+
const isNumber = (type) => type === 'number';
|
|
653
|
+
const isBoolean = (type) => type === 'boolean';
|
|
654
|
+
const isUint8Array$1 = (type) => type === 'uint8Array';
|
|
655
|
+
const tokenize = (key, value) => {
|
|
656
|
+
const optional = key.endsWith('?');
|
|
657
|
+
let type = value;
|
|
658
|
+
type = Array.isArray(type) ? 'array' : typeof type;
|
|
659
|
+
if (value instanceof Uint8Array)
|
|
660
|
+
type = 'uint8Array';
|
|
661
|
+
const parts = key.split('?');
|
|
662
|
+
const minimumLength = parts[2]?.includes('min') ? parts[2].split['min:'][1] : 0;
|
|
663
|
+
return { type, optional, key: parts[0], minimumLength };
|
|
664
|
+
};
|
|
665
|
+
const toType = (data) => {
|
|
666
|
+
// always return uint8Arrays as they are
|
|
667
|
+
if (data instanceof Uint8Array)
|
|
668
|
+
return data;
|
|
669
|
+
// returns the ArrayBuffer as a UintArray
|
|
670
|
+
if (data instanceof ArrayBuffer)
|
|
671
|
+
return new Uint8Array(data);
|
|
672
|
+
// returns the string as a UintArray
|
|
673
|
+
if (typeof data === 'string')
|
|
674
|
+
return new TextEncoder().encode(data);
|
|
675
|
+
// returns the object as a UintArray
|
|
676
|
+
if (typeof data === 'object')
|
|
677
|
+
return new TextEncoder().encode(JSON.stringify(data));
|
|
678
|
+
// returns the number as a UintArray
|
|
679
|
+
if (typeof data === 'number' || typeof data === 'boolean')
|
|
680
|
+
return new TextEncoder().encode(data.toString());
|
|
681
|
+
throw new Error(`unsuported type ${typeof data || data}`);
|
|
682
|
+
};
|
|
683
|
+
const encode$3 = (proto, input) => {
|
|
684
|
+
const keys = Object.keys(proto);
|
|
685
|
+
const values = Object.values(proto);
|
|
686
|
+
const set = [];
|
|
687
|
+
for (let i = 0; i < keys.length; i++) {
|
|
688
|
+
const token = tokenize(keys[i], values[i]);
|
|
689
|
+
const data = input[token.key];
|
|
690
|
+
if (!token.optional && data === undefined)
|
|
691
|
+
throw new Error(`missing required property: ${token.key}`);
|
|
692
|
+
if (token.type === 'array' && token.minimumLength > data.length || token.type === 'object' && token.minimumLength > Object.keys(data).length)
|
|
693
|
+
throw new Error(`minimumLength for ${token.key} is set to ${token.minimumLength} but got ${data.length}`);
|
|
694
|
+
// always push data to the set.
|
|
695
|
+
// when data is undefined push the default value of the proto
|
|
696
|
+
set.push(toType(data || values[i]));
|
|
697
|
+
}
|
|
698
|
+
return index$6(set);
|
|
699
|
+
};
|
|
700
|
+
const decode$4 = (proto, uint8Array) => {
|
|
701
|
+
let deconcated = index$5(uint8Array);
|
|
702
|
+
const output = {};
|
|
703
|
+
const keys = Object.keys(proto);
|
|
704
|
+
const values = Object.values(proto);
|
|
705
|
+
if (keys.length !== deconcated.length)
|
|
706
|
+
console.warn(`length mismatch: expected ${keys.length} got ${uint8Array.length}`);
|
|
707
|
+
for (let i = 0; i < keys.length; i++) {
|
|
708
|
+
const token = tokenize(keys[i], values[i]);
|
|
709
|
+
if (isUint8Array$1(token.type))
|
|
710
|
+
output[token.key] = deconcated[i];
|
|
711
|
+
else if (isString(token.type))
|
|
712
|
+
output[token.key] = toString(deconcated[i]);
|
|
713
|
+
else if (isBoolean(token.type))
|
|
714
|
+
output[token.key] = Boolean(new TextDecoder().decode(deconcated[i]));
|
|
715
|
+
else if (isNumber(token.type))
|
|
716
|
+
output[token.key] = Number(new TextDecoder().decode(deconcated[i]));
|
|
717
|
+
else if (isJson(token.type))
|
|
718
|
+
output[token.key] = JSON.parse(new TextDecoder().decode(deconcated[i]));
|
|
719
|
+
if (token.optional) {
|
|
720
|
+
if (!output[token.key] || output[token.key].length === 0)
|
|
721
|
+
delete output[token.key];
|
|
722
|
+
}
|
|
723
|
+
if (!token.optional && output[token.key] === undefined)
|
|
724
|
+
throw new Error(`missing required property: ${token.key}`);
|
|
725
|
+
}
|
|
726
|
+
return output;
|
|
727
|
+
};
|
|
728
|
+
var index$1 = {
|
|
729
|
+
encode: encode$3,
|
|
730
|
+
decode: decode$4
|
|
731
|
+
};
|
|
732
|
+
|
|
733
|
+
/*!
|
|
734
|
+
* hash-wasm (https://www.npmjs.com/package/hash-wasm)
|
|
735
|
+
* (c) Dani Biro
|
|
736
|
+
* @license MIT
|
|
737
|
+
*/
|
|
738
|
+
|
|
739
|
+
/*! *****************************************************************************
|
|
740
|
+
Copyright (c) Microsoft Corporation.
|
|
741
|
+
|
|
742
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
743
|
+
purpose with or without fee is hereby granted.
|
|
744
|
+
|
|
745
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
746
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
747
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
748
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
749
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
750
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
751
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
752
|
+
***************************************************************************** */
|
|
753
|
+
|
|
754
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
755
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
756
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
757
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
758
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
759
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
760
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
class Mutex {
|
|
765
|
+
constructor() {
|
|
766
|
+
this.mutex = Promise.resolve();
|
|
767
|
+
}
|
|
768
|
+
lock() {
|
|
769
|
+
let begin = () => { };
|
|
770
|
+
this.mutex = this.mutex.then(() => new Promise(begin));
|
|
771
|
+
return new Promise((res) => {
|
|
772
|
+
begin = res;
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
dispatch(fn) {
|
|
776
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
777
|
+
const unlock = yield this.lock();
|
|
778
|
+
try {
|
|
779
|
+
return yield Promise.resolve(fn());
|
|
780
|
+
}
|
|
781
|
+
finally {
|
|
782
|
+
unlock();
|
|
783
|
+
}
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/* eslint-disable import/prefer-default-export */
|
|
789
|
+
/* eslint-disable no-bitwise */
|
|
790
|
+
var _a;
|
|
791
|
+
function getGlobal() {
|
|
792
|
+
if (typeof globalThis !== 'undefined')
|
|
793
|
+
return globalThis;
|
|
794
|
+
// eslint-disable-next-line no-restricted-globals
|
|
795
|
+
if (typeof self !== 'undefined')
|
|
796
|
+
return self;
|
|
797
|
+
if (typeof window !== 'undefined')
|
|
798
|
+
return window;
|
|
799
|
+
return global;
|
|
800
|
+
}
|
|
801
|
+
const globalObject = getGlobal();
|
|
802
|
+
const nodeBuffer = (_a = globalObject.Buffer) !== null && _a !== void 0 ? _a : null;
|
|
803
|
+
const textEncoder = globalObject.TextEncoder ? new globalObject.TextEncoder() : null;
|
|
804
|
+
function hexCharCodesToInt(a, b) {
|
|
805
|
+
return (((a & 0xF) + ((a >> 6) | ((a >> 3) & 0x8))) << 4) | ((b & 0xF) + ((b >> 6) | ((b >> 3) & 0x8)));
|
|
806
|
+
}
|
|
807
|
+
function writeHexToUInt8(buf, str) {
|
|
808
|
+
const size = str.length >> 1;
|
|
809
|
+
for (let i = 0; i < size; i++) {
|
|
810
|
+
const index = i << 1;
|
|
811
|
+
buf[i] = hexCharCodesToInt(str.charCodeAt(index), str.charCodeAt(index + 1));
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
function hexStringEqualsUInt8(str, buf) {
|
|
815
|
+
if (str.length !== buf.length * 2) {
|
|
816
|
+
return false;
|
|
817
|
+
}
|
|
818
|
+
for (let i = 0; i < buf.length; i++) {
|
|
819
|
+
const strIndex = i << 1;
|
|
820
|
+
if (buf[i] !== hexCharCodesToInt(str.charCodeAt(strIndex), str.charCodeAt(strIndex + 1))) {
|
|
821
|
+
return false;
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
return true;
|
|
825
|
+
}
|
|
826
|
+
const alpha = 'a'.charCodeAt(0) - 10;
|
|
827
|
+
const digit = '0'.charCodeAt(0);
|
|
828
|
+
function getDigestHex(tmpBuffer, input, hashLength) {
|
|
829
|
+
let p = 0;
|
|
830
|
+
/* eslint-disable no-plusplus */
|
|
831
|
+
for (let i = 0; i < hashLength; i++) {
|
|
832
|
+
let nibble = input[i] >>> 4;
|
|
833
|
+
tmpBuffer[p++] = nibble > 9 ? nibble + alpha : nibble + digit;
|
|
834
|
+
nibble = input[i] & 0xF;
|
|
835
|
+
tmpBuffer[p++] = nibble > 9 ? nibble + alpha : nibble + digit;
|
|
836
|
+
}
|
|
837
|
+
/* eslint-enable no-plusplus */
|
|
838
|
+
return String.fromCharCode.apply(null, tmpBuffer);
|
|
839
|
+
}
|
|
840
|
+
const getUInt8Buffer = nodeBuffer !== null
|
|
841
|
+
? (data) => {
|
|
842
|
+
if (typeof data === 'string') {
|
|
843
|
+
const buf = nodeBuffer.from(data, 'utf8');
|
|
844
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.length);
|
|
845
|
+
}
|
|
846
|
+
if (nodeBuffer.isBuffer(data)) {
|
|
847
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.length);
|
|
848
|
+
}
|
|
849
|
+
if (ArrayBuffer.isView(data)) {
|
|
850
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
851
|
+
}
|
|
852
|
+
throw new Error('Invalid data type!');
|
|
853
|
+
}
|
|
854
|
+
: (data) => {
|
|
855
|
+
if (typeof data === 'string') {
|
|
856
|
+
return textEncoder.encode(data);
|
|
857
|
+
}
|
|
858
|
+
if (ArrayBuffer.isView(data)) {
|
|
859
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
860
|
+
}
|
|
861
|
+
throw new Error('Invalid data type!');
|
|
862
|
+
};
|
|
863
|
+
const base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
864
|
+
const base64Lookup = new Uint8Array(256);
|
|
865
|
+
for (let i = 0; i < base64Chars.length; i++) {
|
|
866
|
+
base64Lookup[base64Chars.charCodeAt(i)] = i;
|
|
867
|
+
}
|
|
868
|
+
function getDecodeBase64Length(data) {
|
|
869
|
+
let bufferLength = Math.floor(data.length * 0.75);
|
|
870
|
+
const len = data.length;
|
|
871
|
+
if (data[len - 1] === '=') {
|
|
872
|
+
bufferLength -= 1;
|
|
873
|
+
if (data[len - 2] === '=') {
|
|
874
|
+
bufferLength -= 1;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
return bufferLength;
|
|
878
|
+
}
|
|
879
|
+
function decodeBase64(data) {
|
|
880
|
+
const bufferLength = getDecodeBase64Length(data);
|
|
881
|
+
const len = data.length;
|
|
882
|
+
const bytes = new Uint8Array(bufferLength);
|
|
883
|
+
let p = 0;
|
|
884
|
+
for (let i = 0; i < len; i += 4) {
|
|
885
|
+
const encoded1 = base64Lookup[data.charCodeAt(i)];
|
|
886
|
+
const encoded2 = base64Lookup[data.charCodeAt(i + 1)];
|
|
887
|
+
const encoded3 = base64Lookup[data.charCodeAt(i + 2)];
|
|
888
|
+
const encoded4 = base64Lookup[data.charCodeAt(i + 3)];
|
|
889
|
+
bytes[p] = (encoded1 << 2) | (encoded2 >> 4);
|
|
890
|
+
p += 1;
|
|
891
|
+
bytes[p] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
|
|
892
|
+
p += 1;
|
|
893
|
+
bytes[p] = ((encoded3 & 3) << 6) | (encoded4 & 63);
|
|
894
|
+
p += 1;
|
|
895
|
+
}
|
|
896
|
+
return bytes;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
const MAX_HEAP = 16 * 1024;
|
|
900
|
+
const WASM_FUNC_HASH_LENGTH = 4;
|
|
901
|
+
const wasmMutex = new Mutex();
|
|
902
|
+
const wasmModuleCache = new Map();
|
|
903
|
+
function WASMInterface(binary, hashLength) {
|
|
904
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
905
|
+
let wasmInstance = null;
|
|
906
|
+
let memoryView = null;
|
|
907
|
+
let initialized = false;
|
|
908
|
+
if (typeof WebAssembly === 'undefined') {
|
|
909
|
+
throw new Error('WebAssembly is not supported in this environment!');
|
|
910
|
+
}
|
|
911
|
+
const writeMemory = (data, offset = 0) => {
|
|
912
|
+
memoryView.set(data, offset);
|
|
913
|
+
};
|
|
914
|
+
const getMemory = () => memoryView;
|
|
915
|
+
const getExports = () => wasmInstance.exports;
|
|
916
|
+
const setMemorySize = (totalSize) => {
|
|
917
|
+
wasmInstance.exports.Hash_SetMemorySize(totalSize);
|
|
918
|
+
const arrayOffset = wasmInstance.exports.Hash_GetBuffer();
|
|
919
|
+
const memoryBuffer = wasmInstance.exports.memory.buffer;
|
|
920
|
+
memoryView = new Uint8Array(memoryBuffer, arrayOffset, totalSize);
|
|
921
|
+
};
|
|
922
|
+
const getStateSize = () => {
|
|
923
|
+
const view = new DataView(wasmInstance.exports.memory.buffer);
|
|
924
|
+
const stateSize = view.getUint32(wasmInstance.exports.STATE_SIZE, true);
|
|
925
|
+
return stateSize;
|
|
926
|
+
};
|
|
927
|
+
const loadWASMPromise = wasmMutex.dispatch(() => __awaiter(this, void 0, void 0, function* () {
|
|
928
|
+
if (!wasmModuleCache.has(binary.name)) {
|
|
929
|
+
const asm = decodeBase64(binary.data);
|
|
930
|
+
const promise = WebAssembly.compile(asm);
|
|
931
|
+
wasmModuleCache.set(binary.name, promise);
|
|
932
|
+
}
|
|
933
|
+
const module = yield wasmModuleCache.get(binary.name);
|
|
934
|
+
wasmInstance = yield WebAssembly.instantiate(module, {
|
|
935
|
+
// env: {
|
|
936
|
+
// emscripten_memcpy_big: (dest, src, num) => {
|
|
937
|
+
// const memoryBuffer = wasmInstance.exports.memory.buffer;
|
|
938
|
+
// const memView = new Uint8Array(memoryBuffer, 0);
|
|
939
|
+
// memView.set(memView.subarray(src, src + num), dest);
|
|
940
|
+
// },
|
|
941
|
+
// print_memory: (offset, len) => {
|
|
942
|
+
// const memoryBuffer = wasmInstance.exports.memory.buffer;
|
|
943
|
+
// const memView = new Uint8Array(memoryBuffer, 0);
|
|
944
|
+
// console.log('print_int32', memView.subarray(offset, offset + len));
|
|
945
|
+
// },
|
|
946
|
+
// },
|
|
947
|
+
});
|
|
948
|
+
// wasmInstance.exports._start();
|
|
949
|
+
}));
|
|
950
|
+
const setupInterface = () => __awaiter(this, void 0, void 0, function* () {
|
|
951
|
+
if (!wasmInstance) {
|
|
952
|
+
yield loadWASMPromise;
|
|
953
|
+
}
|
|
954
|
+
const arrayOffset = wasmInstance.exports.Hash_GetBuffer();
|
|
955
|
+
const memoryBuffer = wasmInstance.exports.memory.buffer;
|
|
956
|
+
memoryView = new Uint8Array(memoryBuffer, arrayOffset, MAX_HEAP);
|
|
957
|
+
});
|
|
958
|
+
const init = (bits = null) => {
|
|
959
|
+
initialized = true;
|
|
960
|
+
wasmInstance.exports.Hash_Init(bits);
|
|
961
|
+
};
|
|
962
|
+
const updateUInt8Array = (data) => {
|
|
963
|
+
let read = 0;
|
|
964
|
+
while (read < data.length) {
|
|
965
|
+
const chunk = data.subarray(read, read + MAX_HEAP);
|
|
966
|
+
read += chunk.length;
|
|
967
|
+
memoryView.set(chunk);
|
|
968
|
+
wasmInstance.exports.Hash_Update(chunk.length);
|
|
969
|
+
}
|
|
970
|
+
};
|
|
971
|
+
const update = (data) => {
|
|
972
|
+
if (!initialized) {
|
|
973
|
+
throw new Error('update() called before init()');
|
|
974
|
+
}
|
|
975
|
+
const Uint8Buffer = getUInt8Buffer(data);
|
|
976
|
+
updateUInt8Array(Uint8Buffer);
|
|
977
|
+
};
|
|
978
|
+
const digestChars = new Uint8Array(hashLength * 2);
|
|
979
|
+
const digest = (outputType, padding = null) => {
|
|
980
|
+
if (!initialized) {
|
|
981
|
+
throw new Error('digest() called before init()');
|
|
982
|
+
}
|
|
983
|
+
initialized = false;
|
|
984
|
+
wasmInstance.exports.Hash_Final(padding);
|
|
985
|
+
if (outputType === 'binary') {
|
|
986
|
+
// the data is copied to allow GC of the original memory object
|
|
987
|
+
return memoryView.slice(0, hashLength);
|
|
988
|
+
}
|
|
989
|
+
return getDigestHex(digestChars, memoryView, hashLength);
|
|
990
|
+
};
|
|
991
|
+
const save = () => {
|
|
992
|
+
if (!initialized) {
|
|
993
|
+
throw new Error('save() can only be called after init() and before digest()');
|
|
994
|
+
}
|
|
995
|
+
const stateOffset = wasmInstance.exports.Hash_GetState();
|
|
996
|
+
const stateLength = getStateSize();
|
|
997
|
+
const memoryBuffer = wasmInstance.exports.memory.buffer;
|
|
998
|
+
const internalState = new Uint8Array(memoryBuffer, stateOffset, stateLength);
|
|
999
|
+
// prefix is 4 bytes from SHA1 hash of the WASM binary
|
|
1000
|
+
// it is used to detect incompatible internal states between different versions of hash-wasm
|
|
1001
|
+
const prefixedState = new Uint8Array(WASM_FUNC_HASH_LENGTH + stateLength);
|
|
1002
|
+
writeHexToUInt8(prefixedState, binary.hash);
|
|
1003
|
+
prefixedState.set(internalState, WASM_FUNC_HASH_LENGTH);
|
|
1004
|
+
return prefixedState;
|
|
1005
|
+
};
|
|
1006
|
+
const load = (state) => {
|
|
1007
|
+
if (!(state instanceof Uint8Array)) {
|
|
1008
|
+
throw new Error('load() expects an Uint8Array generated by save()');
|
|
1009
|
+
}
|
|
1010
|
+
const stateOffset = wasmInstance.exports.Hash_GetState();
|
|
1011
|
+
const stateLength = getStateSize();
|
|
1012
|
+
const overallLength = WASM_FUNC_HASH_LENGTH + stateLength;
|
|
1013
|
+
const memoryBuffer = wasmInstance.exports.memory.buffer;
|
|
1014
|
+
if (state.length !== overallLength) {
|
|
1015
|
+
throw new Error(`Bad state length (expected ${overallLength} bytes, got ${state.length})`);
|
|
1016
|
+
}
|
|
1017
|
+
if (!hexStringEqualsUInt8(binary.hash, state.subarray(0, WASM_FUNC_HASH_LENGTH))) {
|
|
1018
|
+
throw new Error('This state was written by an incompatible hash implementation');
|
|
1019
|
+
}
|
|
1020
|
+
const internalState = state.subarray(WASM_FUNC_HASH_LENGTH);
|
|
1021
|
+
new Uint8Array(memoryBuffer, stateOffset, stateLength).set(internalState);
|
|
1022
|
+
initialized = true;
|
|
1023
|
+
};
|
|
1024
|
+
const isDataShort = (data) => {
|
|
1025
|
+
if (typeof data === 'string') {
|
|
1026
|
+
// worst case is 4 bytes / char
|
|
1027
|
+
return data.length < MAX_HEAP / 4;
|
|
1028
|
+
}
|
|
1029
|
+
return data.byteLength < MAX_HEAP;
|
|
1030
|
+
};
|
|
1031
|
+
let canSimplify = isDataShort;
|
|
1032
|
+
switch (binary.name) {
|
|
1033
|
+
case 'argon2':
|
|
1034
|
+
case 'scrypt':
|
|
1035
|
+
canSimplify = () => true;
|
|
1036
|
+
break;
|
|
1037
|
+
case 'blake2b':
|
|
1038
|
+
case 'blake2s':
|
|
1039
|
+
// if there is a key at blake2 then cannot simplify
|
|
1040
|
+
canSimplify = (data, initParam) => initParam <= 512 && isDataShort(data);
|
|
1041
|
+
break;
|
|
1042
|
+
case 'blake3':
|
|
1043
|
+
// if there is a key at blake3 then cannot simplify
|
|
1044
|
+
canSimplify = (data, initParam) => initParam === 0 && isDataShort(data);
|
|
1045
|
+
break;
|
|
1046
|
+
case 'xxhash64': // cannot simplify
|
|
1047
|
+
case 'xxhash3':
|
|
1048
|
+
case 'xxhash128':
|
|
1049
|
+
canSimplify = () => false;
|
|
1050
|
+
break;
|
|
1051
|
+
}
|
|
1052
|
+
// shorthand for (init + update + digest) for better performance
|
|
1053
|
+
const calculate = (data, initParam = null, digestParam = null) => {
|
|
1054
|
+
if (!canSimplify(data, initParam)) {
|
|
1055
|
+
init(initParam);
|
|
1056
|
+
update(data);
|
|
1057
|
+
return digest('hex', digestParam);
|
|
1058
|
+
}
|
|
1059
|
+
const buffer = getUInt8Buffer(data);
|
|
1060
|
+
memoryView.set(buffer);
|
|
1061
|
+
wasmInstance.exports.Hash_Calculate(buffer.length, initParam, digestParam);
|
|
1062
|
+
return getDigestHex(digestChars, memoryView, hashLength);
|
|
1063
|
+
};
|
|
1064
|
+
yield setupInterface();
|
|
1065
|
+
return {
|
|
1066
|
+
getMemory,
|
|
1067
|
+
writeMemory,
|
|
1068
|
+
getExports,
|
|
1069
|
+
setMemorySize,
|
|
1070
|
+
init,
|
|
1071
|
+
update,
|
|
1072
|
+
digest,
|
|
1073
|
+
save,
|
|
1074
|
+
load,
|
|
1075
|
+
calculate,
|
|
1076
|
+
hashLength,
|
|
1077
|
+
};
|
|
1078
|
+
});
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
new Mutex();
|
|
1082
|
+
|
|
1083
|
+
new Mutex();
|
|
1084
|
+
|
|
1085
|
+
new Mutex();
|
|
1086
|
+
|
|
1087
|
+
new Mutex();
|
|
1088
|
+
|
|
1089
|
+
new Mutex();
|
|
1090
|
+
|
|
1091
|
+
new Mutex();
|
|
1092
|
+
|
|
1093
|
+
new Mutex();
|
|
1094
|
+
|
|
1095
|
+
new Mutex();
|
|
1096
|
+
|
|
1097
|
+
new Mutex();
|
|
1098
|
+
|
|
1099
|
+
var name$b = "sha3";
|
|
1100
|
+
var data$b = "AGFzbQEAAAABDwNgAAF/YAF/AGADf39/AAMIBwABAQIBAAIEBQFwAQEBBQQBAQICBg4CfwFBkI0FC38AQcAJCwdwCAZtZW1vcnkCAA5IYXNoX0dldEJ1ZmZlcgAACUhhc2hfSW5pdAABC0hhc2hfVXBkYXRlAAIKSGFzaF9GaW5hbAAEDUhhc2hfR2V0U3RhdGUABQ5IYXNoX0NhbGN1bGF0ZQAGClNUQVRFX1NJWkUDAQrLFwcFAEGACgvXAwBBAEIANwOAjQFBAEIANwP4jAFBAEIANwPwjAFBAEIANwPojAFBAEIANwPgjAFBAEIANwPYjAFBAEIANwPQjAFBAEIANwPIjAFBAEIANwPAjAFBAEIANwO4jAFBAEIANwOwjAFBAEIANwOojAFBAEIANwOgjAFBAEIANwOYjAFBAEIANwOQjAFBAEIANwOIjAFBAEIANwOAjAFBAEIANwP4iwFBAEIANwPwiwFBAEIANwPoiwFBAEIANwPgiwFBAEIANwPYiwFBAEIANwPQiwFBAEIANwPIiwFBAEIANwPAiwFBAEIANwO4iwFBAEIANwOwiwFBAEIANwOoiwFBAEIANwOgiwFBAEIANwOYiwFBAEIANwOQiwFBAEIANwOIiwFBAEIANwOAiwFBAEIANwP4igFBAEIANwPwigFBAEIANwPoigFBAEIANwPgigFBAEIANwPYigFBAEIANwPQigFBAEIANwPIigFBAEIANwPAigFBAEIANwO4igFBAEIANwOwigFBAEIANwOoigFBAEIANwOgigFBAEIANwOYigFBAEIANwOQigFBAEIANwOIigFBAEIANwOAigFBAEHADCAAQQF0a0EDdjYCjI0BQQBBADYCiI0BC/8BAQZ/AkBBACgCiI0BIgFBAEgN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AhAANAIABBgApqIABBgIoBaigCADYCACAAQQRqIQAgAUF/aiIBDQALCwsLzAEBAEGACAvEAQEAAAAAAAAAgoAAAAAAAACKgAAAAAAAgACAAIAAAACAi4AAAAAAAAABAACAAAAAAIGAAIAAAACACYAAAAAAAICKAAAAAAAAAIgAAAAAAAAACYAAgAAAAAAKAACAAAAAAIuAAIAAAAAAiwAAAAAAAICJgAAAAAAAgAOAAAAAAACAAoAAAAAAAICAAAAAAAAAgAqAAAAAAAAACgAAgAAAAICBgACAAAAAgICAAAAAAACAAQAAgAAAAAAIgACAAAAAgJABAAA=";
|
|
1101
|
+
var hash$b = "ec266d91";
|
|
1102
|
+
var wasmJson$b = {
|
|
1103
|
+
name: name$b,
|
|
1104
|
+
data: data$b,
|
|
1105
|
+
hash: hash$b
|
|
1106
|
+
};
|
|
1107
|
+
|
|
1108
|
+
new Mutex();
|
|
1109
|
+
|
|
1110
|
+
new Mutex();
|
|
1111
|
+
function validateBits(bits) {
|
|
1112
|
+
if (![224, 256, 384, 512].includes(bits)) {
|
|
1113
|
+
return new Error('Invalid variant! Valid values: 224, 256, 384, 512');
|
|
1114
|
+
}
|
|
1115
|
+
return null;
|
|
1116
|
+
}
|
|
1117
|
+
/**
|
|
1118
|
+
* Creates a new Keccak hash instance
|
|
1119
|
+
* @param bits Number of output bits. Valid values: 224, 256, 384, 512
|
|
1120
|
+
*/
|
|
1121
|
+
function createKeccak(bits = 512) {
|
|
1122
|
+
if (validateBits(bits)) {
|
|
1123
|
+
return Promise.reject(validateBits(bits));
|
|
1124
|
+
}
|
|
1125
|
+
const outputSize = bits / 8;
|
|
1126
|
+
return WASMInterface(wasmJson$b, outputSize).then((wasm) => {
|
|
1127
|
+
wasm.init(bits);
|
|
1128
|
+
const obj = {
|
|
1129
|
+
init: () => { wasm.init(bits); return obj; },
|
|
1130
|
+
update: (data) => { wasm.update(data); return obj; },
|
|
1131
|
+
digest: (outputType) => wasm.digest(outputType, 0x01),
|
|
1132
|
+
save: () => wasm.save(),
|
|
1133
|
+
load: (data) => { wasm.load(data); return obj; },
|
|
1134
|
+
blockSize: 200 - 2 * outputSize,
|
|
1135
|
+
digestSize: outputSize,
|
|
1136
|
+
};
|
|
1137
|
+
return obj;
|
|
1138
|
+
});
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
new Mutex();
|
|
1142
|
+
|
|
1143
|
+
new Mutex();
|
|
1144
|
+
|
|
1145
|
+
var name$9 = "sha512";
|
|
1146
|
+
var data$9 = "AGFzbQEAAAABEQRgAAF/YAF/AGACf38AYAAAAwgHAAEBAgMAAgQFAXABAQEFBAEBAgIGDgJ/AUHQigULfwBBgAgLB3AIBm1lbW9yeQIADkhhc2hfR2V0QnVmZmVyAAAJSGFzaF9Jbml0AAELSGFzaF9VcGRhdGUAAgpIYXNoX0ZpbmFsAAQNSGFzaF9HZXRTdGF0ZQAFDkhhc2hfQ2FsY3VsYXRlAAYKU1RBVEVfU0laRQMBCvhnBwUAQYAJC5sCAEEAQgA3A4CKAUEAQTBBwAAgAEGAA0YiABs2AsiKAUEAQqSf6ffbg9LaxwBC+cL4m5Gjs/DbACAAGzcDwIoBQQBCp5/mp9bBi4ZbQuv6htq/tfbBHyAAGzcDuIoBQQBCkargwvbQktqOf0Kf2PnZwpHagpt/IAAbNwOwigFBAEKxloD+/8zJmecAQtGFmu/6z5SH0QAgABs3A6iKAUEAQrmyubiPm/uXFULx7fT4paf9p6V/IAAbNwOgigFBAEKXusODo6vArJF/Qqvw0/Sv7ry3PCAAGzcDmIoBQQBCh6rzs6Olis3iAEK7zqqm2NDrs7t/IAAbNwOQigFBAELYvZaI3Kvn3UtCiJLznf/M+YTqACAAGzcDiIoBC4MCAgF+Bn9BAEEAKQOAigEiASAArXw3A4CKAQJAAkACQCABp0H/AHEiAg0AQYAJIQIMAQsCQCAAQYABIAJrIgMgAyAASyIEGyIFRQ0AIAJBgIkBaiEGQQAhAkEAIQcDQCAGIAJqIAJBgAlqLQAAOgAAIAUgB0EBaiIHQ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A/v/MyZnnAELRhZrv+s+Uh9EAIAEbNwOoigFBAEK5srm4j5v7lxVC8e30+KWn/aelfyABGzcDoIoBQQBCl7rDg6OrwKyRf0Kr8NP0r+68tzwgARs3A5iKAUEAQoeq87OjpYrN4gBCu86qptjQ67O7fyABGzcDkIoBQQBC2L2WiNyr591LQoiS853/zPmE6gAgARs3A4iKASAAEAIQBAsLCwEAQYAICwTQAAAA";
|
|
1147
|
+
var hash$9 = "a5d1ca7c";
|
|
1148
|
+
var wasmJson$9 = {
|
|
1149
|
+
name: name$9,
|
|
1150
|
+
data: data$9,
|
|
1151
|
+
hash: hash$9
|
|
1152
|
+
};
|
|
1153
|
+
|
|
1154
|
+
new Mutex();
|
|
1155
|
+
|
|
1156
|
+
new Mutex();
|
|
1157
|
+
/**
|
|
1158
|
+
* Creates a new SHA-2 (SHA-512) hash instance
|
|
1159
|
+
*/
|
|
1160
|
+
function createSHA512() {
|
|
1161
|
+
return WASMInterface(wasmJson$9, 64).then((wasm) => {
|
|
1162
|
+
wasm.init(512);
|
|
1163
|
+
const obj = {
|
|
1164
|
+
init: () => { wasm.init(512); return obj; },
|
|
1165
|
+
update: (data) => { wasm.update(data); return obj; },
|
|
1166
|
+
digest: (outputType) => wasm.digest(outputType),
|
|
1167
|
+
save: () => wasm.save(),
|
|
1168
|
+
load: (data) => { wasm.load(data); return obj; },
|
|
1169
|
+
blockSize: 128,
|
|
1170
|
+
digestSize: 64,
|
|
1171
|
+
};
|
|
1172
|
+
return obj;
|
|
1173
|
+
});
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
new Mutex();
|
|
1177
|
+
|
|
1178
|
+
new Mutex();
|
|
1179
|
+
|
|
1180
|
+
new Mutex();
|
|
1181
|
+
|
|
1182
|
+
new Mutex();
|
|
1183
|
+
|
|
1184
|
+
var name$4 = "ripemd160";
|
|
1185
|
+
var data$4 = "AGFzbQEAAAABEQRgAAF/YAAAYAF/AGACf38AAwkIAAECAwIBAAIEBQFwAQEBBQQ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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABcAAAA";
|
|
1186
|
+
var hash$4 = "42f1de39";
|
|
1187
|
+
var wasmJson$4 = {
|
|
1188
|
+
name: name$4,
|
|
1189
|
+
data: data$4,
|
|
1190
|
+
hash: hash$4
|
|
1191
|
+
};
|
|
1192
|
+
|
|
1193
|
+
new Mutex();
|
|
1194
|
+
/**
|
|
1195
|
+
* Creates a new RIPEMD-160 hash instance
|
|
1196
|
+
*/
|
|
1197
|
+
function createRIPEMD160() {
|
|
1198
|
+
return WASMInterface(wasmJson$4, 20).then((wasm) => {
|
|
1199
|
+
wasm.init();
|
|
1200
|
+
const obj = {
|
|
1201
|
+
init: () => { wasm.init(); return obj; },
|
|
1202
|
+
update: (data) => { wasm.update(data); return obj; },
|
|
1203
|
+
digest: (outputType) => wasm.digest(outputType),
|
|
1204
|
+
save: () => wasm.save(),
|
|
1205
|
+
load: (data) => { wasm.load(data); return obj; },
|
|
1206
|
+
blockSize: 64,
|
|
1207
|
+
digestSize: 20,
|
|
1208
|
+
};
|
|
1209
|
+
return obj;
|
|
1210
|
+
});
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
function calculateKeyBuffer(hasher, key) {
|
|
1214
|
+
const { blockSize } = hasher;
|
|
1215
|
+
const buf = getUInt8Buffer(key);
|
|
1216
|
+
if (buf.length > blockSize) {
|
|
1217
|
+
hasher.update(buf);
|
|
1218
|
+
const uintArr = hasher.digest('binary');
|
|
1219
|
+
hasher.init();
|
|
1220
|
+
return uintArr;
|
|
1221
|
+
}
|
|
1222
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.length);
|
|
1223
|
+
}
|
|
1224
|
+
function calculateHmac(hasher, key) {
|
|
1225
|
+
hasher.init();
|
|
1226
|
+
const { blockSize } = hasher;
|
|
1227
|
+
const keyBuf = calculateKeyBuffer(hasher, key);
|
|
1228
|
+
const keyBuffer = new Uint8Array(blockSize);
|
|
1229
|
+
keyBuffer.set(keyBuf);
|
|
1230
|
+
const opad = new Uint8Array(blockSize);
|
|
1231
|
+
for (let i = 0; i < blockSize; i++) {
|
|
1232
|
+
const v = keyBuffer[i];
|
|
1233
|
+
opad[i] = v ^ 0x5C;
|
|
1234
|
+
keyBuffer[i] = v ^ 0x36;
|
|
1235
|
+
}
|
|
1236
|
+
hasher.update(keyBuffer);
|
|
1237
|
+
const obj = {
|
|
1238
|
+
init: () => {
|
|
1239
|
+
hasher.init();
|
|
1240
|
+
hasher.update(keyBuffer);
|
|
1241
|
+
return obj;
|
|
1242
|
+
},
|
|
1243
|
+
update: (data) => {
|
|
1244
|
+
hasher.update(data);
|
|
1245
|
+
return obj;
|
|
1246
|
+
},
|
|
1247
|
+
digest: ((outputType) => {
|
|
1248
|
+
const uintArr = hasher.digest('binary');
|
|
1249
|
+
hasher.init();
|
|
1250
|
+
hasher.update(opad);
|
|
1251
|
+
hasher.update(uintArr);
|
|
1252
|
+
return hasher.digest(outputType);
|
|
1253
|
+
}),
|
|
1254
|
+
save: () => {
|
|
1255
|
+
throw new Error('save() not supported');
|
|
1256
|
+
},
|
|
1257
|
+
load: () => {
|
|
1258
|
+
throw new Error('load() not supported');
|
|
1259
|
+
},
|
|
1260
|
+
blockSize: hasher.blockSize,
|
|
1261
|
+
digestSize: hasher.digestSize,
|
|
1262
|
+
};
|
|
1263
|
+
return obj;
|
|
1264
|
+
}
|
|
1265
|
+
/**
|
|
1266
|
+
* Calculates HMAC hash
|
|
1267
|
+
* @param hash Hash algorithm to use. It has to be the return value of a function like createSHA1()
|
|
1268
|
+
* @param key Key (string, Buffer or TypedArray)
|
|
1269
|
+
*/
|
|
1270
|
+
function createHMAC(hash, key) {
|
|
1271
|
+
if (!hash || !hash.then) {
|
|
1272
|
+
throw new Error('Invalid hash function is provided! Usage: createHMAC(createMD5(), "key").');
|
|
1273
|
+
}
|
|
1274
|
+
return hash.then((hasher) => calculateHmac(hasher, key));
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
new Mutex();
|
|
1278
|
+
|
|
1279
|
+
new Mutex();
|
|
1280
|
+
|
|
1281
|
+
const blockchainCodecs = [
|
|
1282
|
+
{
|
|
1283
|
+
name: 'leofcoin-block',
|
|
1284
|
+
codec: '0x6c62',
|
|
1285
|
+
hashAlg: 'dbl-keccak-512',
|
|
1286
|
+
},
|
|
1287
|
+
{
|
|
1288
|
+
name: 'leofcoin-tx',
|
|
1289
|
+
codec: '0x6c74',
|
|
1290
|
+
hashAlg: 'dbl-keccak-512',
|
|
1291
|
+
},
|
|
1292
|
+
{
|
|
1293
|
+
name: 'leofcoin-itx',
|
|
1294
|
+
codec: '0x6c69',
|
|
1295
|
+
hashAlg: 'keccak-512',
|
|
1296
|
+
},
|
|
1297
|
+
{
|
|
1298
|
+
name: 'leofcoin-pr',
|
|
1299
|
+
codec: '0x6c70',
|
|
1300
|
+
hashAlg: 'keccak-256',
|
|
1301
|
+
},
|
|
1302
|
+
{
|
|
1303
|
+
name: 'contract-message',
|
|
1304
|
+
codec: '0x63636d',
|
|
1305
|
+
hashAlg: 'keccak-256'
|
|
1306
|
+
},
|
|
1307
|
+
{
|
|
1308
|
+
name: 'transaction-message',
|
|
1309
|
+
codec: '0x746d',
|
|
1310
|
+
hashAlg: 'keccak-256'
|
|
1311
|
+
},
|
|
1312
|
+
{
|
|
1313
|
+
name: 'block-message',
|
|
1314
|
+
codec: '0x626d',
|
|
1315
|
+
hashAlg: 'keccak-256'
|
|
1316
|
+
},
|
|
1317
|
+
{
|
|
1318
|
+
name: 'bw-message',
|
|
1319
|
+
codec: '0x62776d',
|
|
1320
|
+
hashAlg: 'keccak-256'
|
|
1321
|
+
},
|
|
1322
|
+
{
|
|
1323
|
+
name: 'bw-request-message',
|
|
1324
|
+
codec: '0x6277726d',
|
|
1325
|
+
hashAlg: 'keccak-256'
|
|
1326
|
+
},
|
|
1327
|
+
{
|
|
1328
|
+
name: 'validator-message',
|
|
1329
|
+
codec: '0x766d',
|
|
1330
|
+
hashAlg: 'keccak-256'
|
|
1331
|
+
}
|
|
1332
|
+
];
|
|
1333
|
+
|
|
1334
|
+
const internalCodecs = [
|
|
1335
|
+
{
|
|
1336
|
+
name: 'disco-hash',
|
|
1337
|
+
codec: '0x30',
|
|
1338
|
+
hashAlg: 'dbl-keccak-256',
|
|
1339
|
+
},
|
|
1340
|
+
{
|
|
1341
|
+
name: 'peernet-peer-response',
|
|
1342
|
+
codec: '0x707072',
|
|
1343
|
+
hashAlg: 'keccak-256',
|
|
1344
|
+
},
|
|
1345
|
+
{
|
|
1346
|
+
name: 'peernet-peer',
|
|
1347
|
+
codec: '0x7070',
|
|
1348
|
+
hashAlg: 'keccak-256',
|
|
1349
|
+
},
|
|
1350
|
+
{
|
|
1351
|
+
name: 'peernet-dht',
|
|
1352
|
+
codec: '0x706468',
|
|
1353
|
+
hashAlg: 'keccak-256',
|
|
1354
|
+
},
|
|
1355
|
+
{
|
|
1356
|
+
name: 'peernet-dht-response',
|
|
1357
|
+
codec: '0x706472',
|
|
1358
|
+
hashAlg: 'keccak-256',
|
|
1359
|
+
},
|
|
1360
|
+
{
|
|
1361
|
+
name: 'peernet-data',
|
|
1362
|
+
codec: '0x706461',
|
|
1363
|
+
hashAlg: 'keccak-256',
|
|
1364
|
+
},
|
|
1365
|
+
{
|
|
1366
|
+
name: 'peernet-data-response',
|
|
1367
|
+
codec: '0x70646172',
|
|
1368
|
+
hashAlg: 'keccak-256',
|
|
1369
|
+
},
|
|
1370
|
+
{
|
|
1371
|
+
name: 'peernet-message',
|
|
1372
|
+
codec: '0x706d65',
|
|
1373
|
+
hashAlg: 'keccak-256',
|
|
1374
|
+
},
|
|
1375
|
+
{
|
|
1376
|
+
name: 'peernet-ps',
|
|
1377
|
+
codec: '707073',
|
|
1378
|
+
hashAlg: 'keccak-256',
|
|
1379
|
+
},
|
|
1380
|
+
{
|
|
1381
|
+
name: 'peernet-response',
|
|
1382
|
+
codec: '0x7072',
|
|
1383
|
+
hashAlg: 'keccak-256',
|
|
1384
|
+
},
|
|
1385
|
+
{
|
|
1386
|
+
name: 'peernet-request',
|
|
1387
|
+
codec: '0x707271',
|
|
1388
|
+
hashAlg: 'keccak-256',
|
|
1389
|
+
},
|
|
1390
|
+
{
|
|
1391
|
+
name: 'peernet-file',
|
|
1392
|
+
codec: '0x7066',
|
|
1393
|
+
hashAlg: 'keccak-256',
|
|
1394
|
+
},
|
|
1395
|
+
{
|
|
1396
|
+
name: 'peernet-file-response',
|
|
1397
|
+
codec: '0x706672',
|
|
1398
|
+
hashAlg: 'keccak-256',
|
|
1399
|
+
}
|
|
1400
|
+
];
|
|
1401
|
+
|
|
1402
|
+
const codecs = [
|
|
1403
|
+
...internalCodecs,
|
|
1404
|
+
...blockchainCodecs,
|
|
1405
|
+
{
|
|
1406
|
+
name: 'chat-message',
|
|
1407
|
+
codec: '0x70636d',
|
|
1408
|
+
hashAlg: 'dbl-keccak-256',
|
|
1409
|
+
}
|
|
1410
|
+
];
|
|
1411
|
+
|
|
1412
|
+
globalThis.peernetCodecs = globalThis.peernetCodecs || {};
|
|
1413
|
+
const addCodec = (codecInput) => {
|
|
1414
|
+
let { hashAlg, codec, name } = codecInput;
|
|
1415
|
+
if (!globalThis.peernetCodecs[name])
|
|
1416
|
+
globalThis.peernetCodecs[name] = {
|
|
1417
|
+
hashAlg,
|
|
1418
|
+
codec: typeof codec === 'string' ? parseInt(codec, 16) : codec
|
|
1419
|
+
};
|
|
1420
|
+
};
|
|
1421
|
+
const getCodec = (name) => {
|
|
1422
|
+
if (typeof name === 'number')
|
|
1423
|
+
return name;
|
|
1424
|
+
return getCodecByName(name).codec;
|
|
1425
|
+
};
|
|
1426
|
+
const getCodecName = (codec) => {
|
|
1427
|
+
return Object.keys(globalThis.peernetCodecs).reduce((p, c) => {
|
|
1428
|
+
const item = globalThis.peernetCodecs[c];
|
|
1429
|
+
if (item.codec === codec)
|
|
1430
|
+
return c;
|
|
1431
|
+
else
|
|
1432
|
+
return p;
|
|
1433
|
+
}, undefined);
|
|
1434
|
+
};
|
|
1435
|
+
const getCodecByName = (name) => globalThis.peernetCodecs[name];
|
|
1436
|
+
const getHashAlg = (name) => {
|
|
1437
|
+
if (typeof name === 'number')
|
|
1438
|
+
return getCodecByName(getCodecName(name)).hashAlg;
|
|
1439
|
+
return getCodecByName(name).hashAlg;
|
|
1440
|
+
};
|
|
1441
|
+
const isCodec = (codec) => {
|
|
1442
|
+
if (codec.codec !== undefined && codec.hashAlg)
|
|
1443
|
+
return true;
|
|
1444
|
+
return false;
|
|
1445
|
+
};
|
|
1446
|
+
const validateCodec = (codec) => {
|
|
1447
|
+
if (codec.codec === undefined ||
|
|
1448
|
+
codec.hashAlg === undefined ||
|
|
1449
|
+
codec.name === undefined)
|
|
1450
|
+
throw new Error(`invalid codecInput: ${codec}`);
|
|
1451
|
+
};
|
|
1452
|
+
for (const codec of codecs) {
|
|
1453
|
+
addCodec(codec);
|
|
1454
|
+
}
|
|
1455
|
+
var utils$1 = {
|
|
1456
|
+
isCodec,
|
|
1457
|
+
addCodec,
|
|
1458
|
+
getCodec,
|
|
1459
|
+
getHashAlg,
|
|
1460
|
+
getCodecName,
|
|
1461
|
+
validateCodec,
|
|
1462
|
+
codecs: globalThis.peernetCodecs
|
|
1463
|
+
};
|
|
1464
|
+
|
|
1465
|
+
/**
|
|
1466
|
+
* @param {string}
|
|
1467
|
+
*/
|
|
1468
|
+
var isHex = (function (string) { return /^[A-F0-9]+$/i.test(string); });
|
|
1469
|
+
|
|
1470
|
+
let BasicInterface$1 = class BasicInterface {
|
|
1471
|
+
encoded;
|
|
1472
|
+
decoded;
|
|
1473
|
+
keys;
|
|
1474
|
+
name;
|
|
1475
|
+
#proto;
|
|
1476
|
+
set proto(value) {
|
|
1477
|
+
this.#proto = value;
|
|
1478
|
+
this.keys = Object.keys(value);
|
|
1479
|
+
}
|
|
1480
|
+
get proto() {
|
|
1481
|
+
return this.#proto;
|
|
1482
|
+
}
|
|
1483
|
+
decode(encoded) {
|
|
1484
|
+
encoded = encoded || this.encoded;
|
|
1485
|
+
return new Object();
|
|
1486
|
+
}
|
|
1487
|
+
encode(decoded) {
|
|
1488
|
+
decoded = decoded || this.decoded;
|
|
1489
|
+
return new Uint8Array();
|
|
1490
|
+
}
|
|
1491
|
+
// get Codec(): Codec {}
|
|
1492
|
+
protoEncode(data) {
|
|
1493
|
+
// check schema
|
|
1494
|
+
return index$1.encode(this.proto, data);
|
|
1495
|
+
}
|
|
1496
|
+
protoDecode(data) {
|
|
1497
|
+
// check schema
|
|
1498
|
+
return index$1.decode(this.proto, data);
|
|
1499
|
+
}
|
|
1500
|
+
isHex(string) {
|
|
1501
|
+
return isHex(string);
|
|
1502
|
+
}
|
|
1503
|
+
isBase32(string) {
|
|
1504
|
+
return index$8.isBase32(string);
|
|
1505
|
+
}
|
|
1506
|
+
isBase58(string) {
|
|
1507
|
+
return base58$1.isBase58(string);
|
|
1508
|
+
}
|
|
1509
|
+
fromBs32(encoded) {
|
|
1510
|
+
return this.decode(index$8.decode(encoded));
|
|
1511
|
+
}
|
|
1512
|
+
fromBs58(encoded) {
|
|
1513
|
+
return this.decode(fromBase58(encoded));
|
|
1514
|
+
}
|
|
1515
|
+
async toArray() {
|
|
1516
|
+
const array = [];
|
|
1517
|
+
for await (const value of this.encoded.values()) {
|
|
1518
|
+
array.push(value);
|
|
1519
|
+
}
|
|
1520
|
+
return array;
|
|
1521
|
+
}
|
|
1522
|
+
fromString(string) {
|
|
1523
|
+
const array = string.split(',');
|
|
1524
|
+
const arrayLike = array.map(string => Number(string));
|
|
1525
|
+
return this.decode(Uint8Array.from(arrayLike));
|
|
1526
|
+
}
|
|
1527
|
+
fromHex(string) {
|
|
1528
|
+
return this.decode(fromHex(string));
|
|
1529
|
+
}
|
|
1530
|
+
fromArray(array) {
|
|
1531
|
+
return this.decode(Uint8Array.from([...array]));
|
|
1532
|
+
}
|
|
1533
|
+
fromEncoded(encoded) {
|
|
1534
|
+
return this.decode(encoded);
|
|
1535
|
+
}
|
|
1536
|
+
toString() {
|
|
1537
|
+
if (!this.encoded)
|
|
1538
|
+
this.encode();
|
|
1539
|
+
return this.encoded.toString();
|
|
1540
|
+
}
|
|
1541
|
+
toHex() {
|
|
1542
|
+
if (!this.encoded)
|
|
1543
|
+
this.encode();
|
|
1544
|
+
return toHex$1(this.encoded.toString().split(',').map(number => Number(number)));
|
|
1545
|
+
}
|
|
1546
|
+
/**
|
|
1547
|
+
* @return {String} encoded
|
|
1548
|
+
*/
|
|
1549
|
+
toBs32() {
|
|
1550
|
+
if (!this.encoded)
|
|
1551
|
+
this.encode();
|
|
1552
|
+
return toBase32(this.encoded);
|
|
1553
|
+
}
|
|
1554
|
+
/**
|
|
1555
|
+
* @return {String} encoded
|
|
1556
|
+
*/
|
|
1557
|
+
toBs58() {
|
|
1558
|
+
if (!this.encoded)
|
|
1559
|
+
this.encode();
|
|
1560
|
+
return toBase58(this.encoded);
|
|
1561
|
+
}
|
|
1562
|
+
};
|
|
1563
|
+
|
|
1564
|
+
let Codec$1 = class Codec extends BasicInterface$1 {
|
|
1565
|
+
codecBuffer;
|
|
1566
|
+
codec;
|
|
1567
|
+
hashAlg;
|
|
1568
|
+
constructor(buffer) {
|
|
1569
|
+
super();
|
|
1570
|
+
if (buffer) {
|
|
1571
|
+
if (buffer instanceof Uint8Array) {
|
|
1572
|
+
const codec = index$7.decode(buffer);
|
|
1573
|
+
const name = this.getCodecName(codec);
|
|
1574
|
+
if (name) {
|
|
1575
|
+
this.name = name;
|
|
1576
|
+
this.encoded = buffer;
|
|
1577
|
+
this.decode(buffer);
|
|
1578
|
+
}
|
|
1579
|
+
else {
|
|
1580
|
+
this.encode(Number(new TextDecoder().decode(buffer)));
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
else if (buffer instanceof ArrayBuffer) {
|
|
1584
|
+
const codec = index$7.decode(buffer);
|
|
1585
|
+
const name = this.getCodecName(codec);
|
|
1586
|
+
if (name) {
|
|
1587
|
+
this.name = name;
|
|
1588
|
+
this.decode(buffer);
|
|
1589
|
+
}
|
|
1590
|
+
else {
|
|
1591
|
+
this.encode(Number(new TextDecoder().decode(new Uint8Array(buffer))));
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
else if (typeof buffer === 'string') {
|
|
1595
|
+
if (utils$1.getCodec(buffer))
|
|
1596
|
+
this.fromName(buffer);
|
|
1597
|
+
else if (this.isHex(buffer))
|
|
1598
|
+
this.fromHex(buffer);
|
|
1599
|
+
else if (this.isBase32(buffer))
|
|
1600
|
+
this.fromBs32(buffer);
|
|
1601
|
+
else if (this.isBase58(buffer))
|
|
1602
|
+
this.fromBs58(buffer);
|
|
1603
|
+
else
|
|
1604
|
+
this.fromString(buffer);
|
|
1605
|
+
}
|
|
1606
|
+
if (!isNaN(buffer))
|
|
1607
|
+
if (utils$1.getCodec(buffer))
|
|
1608
|
+
this.fromCodec(buffer);
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
fromEncoded(encoded) {
|
|
1612
|
+
const codec = index$7.decode(encoded);
|
|
1613
|
+
const name = this.getCodecName(codec);
|
|
1614
|
+
this.name = name;
|
|
1615
|
+
this.encoded = encoded;
|
|
1616
|
+
return this.decode(encoded);
|
|
1617
|
+
}
|
|
1618
|
+
getCodec(name) {
|
|
1619
|
+
return utils$1.getCodec(name);
|
|
1620
|
+
}
|
|
1621
|
+
getCodecName(codec) {
|
|
1622
|
+
return utils$1.getCodecName(codec);
|
|
1623
|
+
}
|
|
1624
|
+
getHashAlg(name) {
|
|
1625
|
+
return utils$1.getHashAlg(name);
|
|
1626
|
+
}
|
|
1627
|
+
fromCodec(codec) {
|
|
1628
|
+
this.name = this.getCodecName(codec);
|
|
1629
|
+
this.hashAlg = this.getHashAlg(this.name);
|
|
1630
|
+
this.codec = this.getCodec(this.name);
|
|
1631
|
+
this.codecBuffer = index$7.encode(this.codec);
|
|
1632
|
+
}
|
|
1633
|
+
fromName(name) {
|
|
1634
|
+
const codec = this.getCodec(name);
|
|
1635
|
+
this.name = name;
|
|
1636
|
+
this.codec = codec;
|
|
1637
|
+
this.hashAlg = this.getHashAlg(name);
|
|
1638
|
+
this.codecBuffer = index$7.encode(this.codec);
|
|
1639
|
+
}
|
|
1640
|
+
decode(encoded) {
|
|
1641
|
+
encoded = encoded || this.encoded;
|
|
1642
|
+
const codec = index$7.decode(encoded);
|
|
1643
|
+
this.fromCodec(codec);
|
|
1644
|
+
return this.decoded;
|
|
1645
|
+
}
|
|
1646
|
+
encode(codec) {
|
|
1647
|
+
codec = codec || this.codec;
|
|
1648
|
+
this.encoded = index$7.encode(codec);
|
|
1649
|
+
return this.encoded;
|
|
1650
|
+
}
|
|
1651
|
+
};
|
|
1652
|
+
|
|
1653
|
+
let CodecHash$1 = class CodecHash extends BasicInterface$1 {
|
|
1654
|
+
constructor(buffer, options = {}) {
|
|
1655
|
+
super();
|
|
1656
|
+
if (options.name)
|
|
1657
|
+
this.name = options.name;
|
|
1658
|
+
else
|
|
1659
|
+
this.name = 'disco-hash';
|
|
1660
|
+
if (options.codecs)
|
|
1661
|
+
this.codecs = options.codecs;
|
|
1662
|
+
return this.init(buffer);
|
|
1663
|
+
}
|
|
1664
|
+
async init(uint8Array) {
|
|
1665
|
+
if (uint8Array) {
|
|
1666
|
+
if (uint8Array instanceof Uint8Array) {
|
|
1667
|
+
this.discoCodec = new Codec$1(uint8Array, this.codecs);
|
|
1668
|
+
const name = this.discoCodec.name;
|
|
1669
|
+
if (name) {
|
|
1670
|
+
this.name = name;
|
|
1671
|
+
this.decode(uint8Array);
|
|
1672
|
+
}
|
|
1673
|
+
else {
|
|
1674
|
+
await this.encode(uint8Array);
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
if (typeof uint8Array === 'string') {
|
|
1678
|
+
if (this.isHex(uint8Array))
|
|
1679
|
+
await this.fromHex(uint8Array);
|
|
1680
|
+
if (this.isBase32(uint8Array))
|
|
1681
|
+
await this.fromBs32(uint8Array);
|
|
1682
|
+
else if (this.isBase58(uint8Array))
|
|
1683
|
+
await this.fromBs58(uint8Array);
|
|
1684
|
+
else
|
|
1685
|
+
throw new Error(`unsupported string ${uint8Array}`);
|
|
1686
|
+
}
|
|
1687
|
+
else if (typeof uint8Array === 'object')
|
|
1688
|
+
await this.fromJSON(uint8Array);
|
|
1689
|
+
}
|
|
1690
|
+
return this;
|
|
1691
|
+
}
|
|
1692
|
+
get prefix() {
|
|
1693
|
+
const length = this.length;
|
|
1694
|
+
const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
|
|
1695
|
+
uint8Array.set(length);
|
|
1696
|
+
uint8Array.set(this.discoCodec.codecBuffer, length.length);
|
|
1697
|
+
return uint8Array;
|
|
1698
|
+
}
|
|
1699
|
+
get length() {
|
|
1700
|
+
return index$7.encode(this.size);
|
|
1701
|
+
}
|
|
1702
|
+
get buffer() {
|
|
1703
|
+
return this.encoded;
|
|
1704
|
+
}
|
|
1705
|
+
get hash() {
|
|
1706
|
+
return this.encoded;
|
|
1707
|
+
}
|
|
1708
|
+
fromJSON(json) {
|
|
1709
|
+
return this.encode(Buffer.from(JSON.stringify(json)));
|
|
1710
|
+
}
|
|
1711
|
+
async encode(buffer, name) {
|
|
1712
|
+
if (!this.name && name)
|
|
1713
|
+
this.name = name;
|
|
1714
|
+
if (!buffer)
|
|
1715
|
+
buffer = this.buffer;
|
|
1716
|
+
this.discoCodec = new Codec$1(this.name, this.codecs);
|
|
1717
|
+
this.discoCodec.fromName(this.name);
|
|
1718
|
+
let hashAlg = this.discoCodec.hashAlg;
|
|
1719
|
+
const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1]);
|
|
1720
|
+
if (hashAlg.includes('dbl')) {
|
|
1721
|
+
hashAlg = hashAlg.replace('dbl-', '');
|
|
1722
|
+
const hasher = await createKeccak(hashVariant);
|
|
1723
|
+
await hasher.init();
|
|
1724
|
+
hasher.update(buffer);
|
|
1725
|
+
buffer = hasher.digest('binary');
|
|
1726
|
+
}
|
|
1727
|
+
const hasher = await createKeccak(hashVariant);
|
|
1728
|
+
await hasher.init();
|
|
1729
|
+
hasher.update(buffer);
|
|
1730
|
+
this.digest = hasher.digest('binary');
|
|
1731
|
+
this.size = this.digest.length;
|
|
1732
|
+
this.codec = this.discoCodec.encode();
|
|
1733
|
+
this.codec = this.discoCodec.codecBuffer;
|
|
1734
|
+
const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
|
|
1735
|
+
uint8Array.set(this.prefix);
|
|
1736
|
+
uint8Array.set(this.digest, this.prefix.length);
|
|
1737
|
+
this.encoded = uint8Array;
|
|
1738
|
+
return this.encoded;
|
|
1739
|
+
}
|
|
1740
|
+
async validate(buffer) {
|
|
1741
|
+
if (Buffer.isBuffer(buffer)) {
|
|
1742
|
+
const codec = index$7.decode(buffer);
|
|
1743
|
+
if (this.codecs[codec]) {
|
|
1744
|
+
this.decode(buffer);
|
|
1745
|
+
}
|
|
1746
|
+
else {
|
|
1747
|
+
await this.encode(buffer);
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
if (typeof buffer === 'string') {
|
|
1751
|
+
if (this.isHex(buffer))
|
|
1752
|
+
this.fromHex(buffer);
|
|
1753
|
+
if (this.isBase32(buffer))
|
|
1754
|
+
this.fromBs32(buffer);
|
|
1755
|
+
}
|
|
1756
|
+
if (typeof buffer === 'object')
|
|
1757
|
+
this.fromJSON(buffer);
|
|
1758
|
+
}
|
|
1759
|
+
decode(buffer) {
|
|
1760
|
+
this.encoded = buffer;
|
|
1761
|
+
const codec = index$7.decode(buffer);
|
|
1762
|
+
this.discoCodec = new Codec$1(codec, this.codecs);
|
|
1763
|
+
// TODO: validate codec
|
|
1764
|
+
buffer = buffer.slice(index$7.decode.bytes);
|
|
1765
|
+
this.size = index$7.decode(buffer);
|
|
1766
|
+
this.digest = buffer.slice(index$7.decode.bytes);
|
|
1767
|
+
if (this.digest.length !== this.size) {
|
|
1768
|
+
throw new Error(`hash length inconsistent: 0x${this.encoded.toString('hex')}`);
|
|
1769
|
+
}
|
|
1770
|
+
// const discoCodec = new Codec(codec, this.codecs)
|
|
1771
|
+
this.name = this.discoCodec.name;
|
|
1772
|
+
this.size = this.digest.length;
|
|
1773
|
+
return {
|
|
1774
|
+
codec: this.codec,
|
|
1775
|
+
name: this.name,
|
|
1776
|
+
size: this.size,
|
|
1777
|
+
length: this.length,
|
|
1778
|
+
digest: this.digest,
|
|
1779
|
+
};
|
|
1780
|
+
}
|
|
1781
|
+
};
|
|
1782
|
+
|
|
1783
|
+
let FormatInterface$1 = class FormatInterface extends BasicInterface$1 {
|
|
1784
|
+
hashFormat;
|
|
1785
|
+
init(buffer) {
|
|
1786
|
+
if (buffer instanceof Uint8Array)
|
|
1787
|
+
this.fromUint8Array(buffer);
|
|
1788
|
+
else if (buffer instanceof ArrayBuffer)
|
|
1789
|
+
this.fromArrayBuffer(buffer);
|
|
1790
|
+
else if (buffer instanceof FormatInterface$1 && buffer?.name === this.name)
|
|
1791
|
+
return buffer;
|
|
1792
|
+
else if (typeof buffer === 'string') {
|
|
1793
|
+
if (this.isHex(buffer))
|
|
1794
|
+
this.fromHex(buffer);
|
|
1795
|
+
else if (this.isBase58(buffer))
|
|
1796
|
+
this.fromBs58(buffer);
|
|
1797
|
+
else if (this.isBase32(buffer))
|
|
1798
|
+
this.fromBs32(buffer);
|
|
1799
|
+
else
|
|
1800
|
+
this.fromString(buffer);
|
|
1801
|
+
}
|
|
1802
|
+
else {
|
|
1803
|
+
this.create(buffer);
|
|
1804
|
+
}
|
|
1805
|
+
return this;
|
|
1806
|
+
}
|
|
1807
|
+
hasCodec() {
|
|
1808
|
+
if (!this.encoded)
|
|
1809
|
+
return false;
|
|
1810
|
+
const codec = new Codec$1(this.encoded);
|
|
1811
|
+
if (codec.name)
|
|
1812
|
+
return true;
|
|
1813
|
+
}
|
|
1814
|
+
decode(encoded) {
|
|
1815
|
+
encoded = encoded || this.encoded;
|
|
1816
|
+
const codec = new Codec$1(this.encoded);
|
|
1817
|
+
if (codec.codecBuffer) {
|
|
1818
|
+
encoded = encoded.slice(codec.codecBuffer.length);
|
|
1819
|
+
this.name = codec.name;
|
|
1820
|
+
this.decoded = this.protoDecode(encoded);
|
|
1821
|
+
// try {
|
|
1822
|
+
// this.decoded = JSON.parse(this.decoded)
|
|
1823
|
+
// } catch {
|
|
1824
|
+
// }
|
|
1825
|
+
}
|
|
1826
|
+
else {
|
|
1827
|
+
throw new Error(`no codec found`);
|
|
1828
|
+
}
|
|
1829
|
+
return this.decoded;
|
|
1830
|
+
}
|
|
1831
|
+
encode(decoded) {
|
|
1832
|
+
let encoded;
|
|
1833
|
+
if (!decoded)
|
|
1834
|
+
decoded = this.decoded;
|
|
1835
|
+
const codec = new Codec$1(this.name);
|
|
1836
|
+
if (decoded instanceof Uint8Array)
|
|
1837
|
+
encoded = decoded;
|
|
1838
|
+
else
|
|
1839
|
+
encoded = this.protoEncode(decoded);
|
|
1840
|
+
if (codec.codecBuffer) {
|
|
1841
|
+
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
|
|
1842
|
+
uint8Array.set(codec.codecBuffer);
|
|
1843
|
+
uint8Array.set(encoded, codec.codecBuffer.length);
|
|
1844
|
+
this.encoded = uint8Array;
|
|
1845
|
+
}
|
|
1846
|
+
else {
|
|
1847
|
+
throw new Error(`invalid codec`);
|
|
1848
|
+
}
|
|
1849
|
+
return this.encoded;
|
|
1850
|
+
}
|
|
1851
|
+
/**
|
|
1852
|
+
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
1853
|
+
* @param {Object} proto - {protoObject}
|
|
1854
|
+
* @param {Object} options - {hashFormat, name}
|
|
1855
|
+
*/
|
|
1856
|
+
constructor(buffer, proto, options) {
|
|
1857
|
+
super();
|
|
1858
|
+
this.proto = proto;
|
|
1859
|
+
this.hashFormat = options?.hashFormat ? options.hashFormat : 'bs32';
|
|
1860
|
+
if (options?.name)
|
|
1861
|
+
this.name = options.name;
|
|
1862
|
+
this.init(buffer);
|
|
1863
|
+
}
|
|
1864
|
+
/**
|
|
1865
|
+
* @return {PeernetHash}
|
|
1866
|
+
*/
|
|
1867
|
+
get peernetHash() {
|
|
1868
|
+
return new CodecHash$1(this.decoded, { name: this.name });
|
|
1869
|
+
}
|
|
1870
|
+
/**
|
|
1871
|
+
* @return {peernetHash}
|
|
1872
|
+
*/
|
|
1873
|
+
async hash() {
|
|
1874
|
+
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
1875
|
+
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
1876
|
+
return (await this.peernetHash)[`to${format}`]();
|
|
1877
|
+
}
|
|
1878
|
+
fromUint8Array(buffer) {
|
|
1879
|
+
this.encoded = buffer;
|
|
1880
|
+
return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
1881
|
+
}
|
|
1882
|
+
fromArrayBuffer(buffer) {
|
|
1883
|
+
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
1884
|
+
return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
1885
|
+
}
|
|
1886
|
+
/**
|
|
1887
|
+
* @param {Object} data
|
|
1888
|
+
*/
|
|
1889
|
+
create(data) {
|
|
1890
|
+
const decoded = {};
|
|
1891
|
+
if (this.keys?.length > 0) {
|
|
1892
|
+
for (const key of this.keys) {
|
|
1893
|
+
Object.defineProperties(decoded, {
|
|
1894
|
+
[key]: {
|
|
1895
|
+
enumerable: true,
|
|
1896
|
+
configurable: true,
|
|
1897
|
+
set: (value) => data[key],
|
|
1898
|
+
get: () => data[key]
|
|
1899
|
+
}
|
|
1900
|
+
});
|
|
1901
|
+
}
|
|
1902
|
+
this.decoded = decoded;
|
|
1903
|
+
return this.encode(decoded);
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
};
|
|
1907
|
+
const FormatInterface = FormatInterface$1;
|
|
1908
|
+
const Codec = Codec$1;
|
|
1909
|
+
|
|
1910
|
+
const BufferToUint8Array = data => {
|
|
1911
|
+
if (data.type === 'Buffer') {
|
|
1912
|
+
data = new Uint8Array(data.data);
|
|
1913
|
+
}
|
|
1914
|
+
return data;
|
|
1915
|
+
};
|
|
1916
|
+
const protoFor = (message) => {
|
|
1917
|
+
const codec = new Codec(message);
|
|
1918
|
+
if (!codec.name)
|
|
1919
|
+
throw new Error('proto not found');
|
|
1920
|
+
const Proto = globalThis.peernet.protos[codec.name];
|
|
1921
|
+
if (!Proto)
|
|
1922
|
+
throw (new Error(`No proto defined for ${codec.name}`));
|
|
1923
|
+
return new Proto(message);
|
|
1924
|
+
};
|
|
1925
|
+
/**
|
|
1926
|
+
* wether or not a peernet daemon is active
|
|
1927
|
+
* @return {Boolean}
|
|
1928
|
+
*/
|
|
1929
|
+
const hasDaemon = async () => {
|
|
1930
|
+
try {
|
|
1931
|
+
let response = await fetch('http://127.0.0.1:1000/api/version');
|
|
1932
|
+
response = await response.json();
|
|
1933
|
+
return Boolean(response.client === '@peernet/api/http');
|
|
1934
|
+
}
|
|
1935
|
+
catch (e) {
|
|
1936
|
+
return false;
|
|
1937
|
+
}
|
|
1938
|
+
};
|
|
1939
|
+
const https = () => {
|
|
1940
|
+
if (!globalThis.location)
|
|
1941
|
+
return false;
|
|
1942
|
+
return Boolean(globalThis.location.protocol === 'https:');
|
|
1943
|
+
};
|
|
1944
|
+
/**
|
|
1945
|
+
* Get current environment
|
|
1946
|
+
* @return {String} current environment [node, electron, browser]
|
|
1947
|
+
*/
|
|
1948
|
+
const environment = () => {
|
|
1949
|
+
const _navigator = globalThis.navigator;
|
|
1950
|
+
if (!_navigator) {
|
|
1951
|
+
return 'node';
|
|
1952
|
+
}
|
|
1953
|
+
else if (_navigator && /electron/i.test(_navigator.userAgent)) {
|
|
1954
|
+
return 'electron';
|
|
1955
|
+
}
|
|
1956
|
+
else {
|
|
1957
|
+
return 'browser';
|
|
1958
|
+
}
|
|
1959
|
+
};
|
|
1960
|
+
/**
|
|
1961
|
+
* * Get current environment
|
|
1962
|
+
* @return {Object} result
|
|
1963
|
+
* @property {Boolean} reult.daemon whether or not daemon is running
|
|
1964
|
+
* @property {Boolean} reult.environment Current environment
|
|
1965
|
+
*/
|
|
1966
|
+
const target = async () => {
|
|
1967
|
+
let daemon = false;
|
|
1968
|
+
if (!https())
|
|
1969
|
+
daemon = await hasDaemon();
|
|
1970
|
+
return { daemon, environment: environment() };
|
|
1971
|
+
};
|
|
1972
|
+
|
|
1973
|
+
class PeerDiscovery {
|
|
1974
|
+
constructor(id) {
|
|
1975
|
+
this.id = id;
|
|
1976
|
+
}
|
|
1977
|
+
_getPeerId(id) {
|
|
1978
|
+
if (!peernet.peerMap || peernet.peerMap && peernet.peerMap.size === 0)
|
|
1979
|
+
return false;
|
|
1980
|
+
for (const entry of [...peernet.peerMap.entries()]) {
|
|
1981
|
+
for (const _id of entry[1]) {
|
|
1982
|
+
if (_id === id)
|
|
1983
|
+
return entry[0];
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1986
|
+
}
|
|
1987
|
+
async discover(peer) {
|
|
1988
|
+
let id = this._getPeerId(peer.id);
|
|
1989
|
+
if (id)
|
|
1990
|
+
return id;
|
|
1991
|
+
const data = await new peernet.protos['peernet-peer']({ id: this.id });
|
|
1992
|
+
const node = await peernet.prepareMessage(peer.id, data.encoded);
|
|
1993
|
+
let response = await peer.request(node.encoded);
|
|
1994
|
+
response = await protoFor(response);
|
|
1995
|
+
response = await new peernet.protos['peernet-peer-response'](response.decoded.data);
|
|
1996
|
+
id = response.decoded.id;
|
|
1997
|
+
if (id === this.id)
|
|
1998
|
+
return;
|
|
1999
|
+
if (!peernet.peerMap.has(id))
|
|
2000
|
+
peernet.peerMap.set(id, [peer.id]);
|
|
2001
|
+
else {
|
|
2002
|
+
const connections = peernet.peerMap.get(id);
|
|
2003
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
2004
|
+
connections.push(peer.id);
|
|
2005
|
+
peernet.peerMap.set(peer.id, connections);
|
|
2006
|
+
}
|
|
2007
|
+
}
|
|
2008
|
+
return id;
|
|
2009
|
+
}
|
|
2010
|
+
async discoverHandler(message, peer) {
|
|
2011
|
+
const { id, proto } = message;
|
|
2012
|
+
// if (typeof message.data === 'string') message.data = Buffer.from(message.data)
|
|
2013
|
+
if (proto.name === 'peernet-peer') {
|
|
2014
|
+
const from = proto.decoded.id;
|
|
2015
|
+
if (from === this.id)
|
|
2016
|
+
return;
|
|
2017
|
+
if (!peernet.peerMap.has(from))
|
|
2018
|
+
peernet.peerMap.set(from, [peer.id]);
|
|
2019
|
+
else {
|
|
2020
|
+
const connections = peernet.peerMap.get(from);
|
|
2021
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
2022
|
+
connections.push(peer.id);
|
|
2023
|
+
peernet.peerMap.set(from, connections);
|
|
2024
|
+
}
|
|
2025
|
+
}
|
|
2026
|
+
const data = await new peernet.protos['peernet-peer-response']({ id: this.id });
|
|
2027
|
+
const node = await peernet.prepareMessage(from, data.encoded);
|
|
2028
|
+
peer.write(Buffer.from(JSON.stringify({ id, data: node.encoded })));
|
|
2029
|
+
}
|
|
2030
|
+
else if (proto.name === 'peernet-peer-response') {
|
|
2031
|
+
const from = proto.decoded.id;
|
|
2032
|
+
if (from === this.id)
|
|
2033
|
+
return;
|
|
2034
|
+
if (!peernet.peerMap.has(from))
|
|
2035
|
+
peernet.peerMap.set(from, [peer.id]);
|
|
2036
|
+
else {
|
|
2037
|
+
const connections = peernet.peerMap.get(from);
|
|
2038
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
2039
|
+
connections.push(peer.id);
|
|
2040
|
+
peernet.peerMap.set(from, connections);
|
|
2041
|
+
}
|
|
2042
|
+
}
|
|
2043
|
+
}
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
2046
|
+
|
|
2047
|
+
/**
|
|
2048
|
+
* Keep history of fetched address and ptr
|
|
2049
|
+
* @property {Object} address
|
|
2050
|
+
* @property {Object} ptr
|
|
2051
|
+
*/
|
|
2052
|
+
const lastFetched = {
|
|
2053
|
+
address: {
|
|
2054
|
+
value: undefined,
|
|
2055
|
+
timestamp: 0,
|
|
2056
|
+
},
|
|
2057
|
+
ptr: {
|
|
2058
|
+
value: undefined,
|
|
2059
|
+
timestamp: 0,
|
|
2060
|
+
},
|
|
2061
|
+
};
|
|
2062
|
+
const getAddress = async () => {
|
|
2063
|
+
const { address } = lastFetched;
|
|
2064
|
+
const now = Math.round(new Date().getTime() / 1000);
|
|
2065
|
+
if (now - address.timestamp > 1200000) {
|
|
2066
|
+
address.value = await fetch('https://icanhazip.com/');
|
|
2067
|
+
address.value = await address.value.text();
|
|
2068
|
+
address.timestamp = Math.round(new Date().getTime() / 1000);
|
|
2069
|
+
lastFetched.address = address;
|
|
2070
|
+
}
|
|
2071
|
+
return address.value;
|
|
2072
|
+
};
|
|
2073
|
+
const degreesToRadians = (degrees) => {
|
|
2074
|
+
return degrees * Math.PI / 180;
|
|
2075
|
+
};
|
|
2076
|
+
const distanceInKmBetweenEarthCoordinates = (lat1, lon1, lat2, lon2) => {
|
|
2077
|
+
const earthRadiusKm = 6371;
|
|
2078
|
+
const dLat = degreesToRadians(lat2 - lat1);
|
|
2079
|
+
const dLon = degreesToRadians(lon2 - lon1);
|
|
2080
|
+
lat1 = degreesToRadians(lat1);
|
|
2081
|
+
lat2 = degreesToRadians(lat2);
|
|
2082
|
+
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
2083
|
+
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
|
|
2084
|
+
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
2085
|
+
return earthRadiusKm * c;
|
|
2086
|
+
};
|
|
2087
|
+
class DhtEarth {
|
|
2088
|
+
/**
|
|
2089
|
+
*
|
|
2090
|
+
*/
|
|
2091
|
+
constructor() {
|
|
2092
|
+
this.providerMap = new Map();
|
|
2093
|
+
}
|
|
2094
|
+
/**
|
|
2095
|
+
* @param {Object} address
|
|
2096
|
+
* @return {Object} {latitude: lat, longitude: lon}
|
|
2097
|
+
*/
|
|
2098
|
+
async getCoordinates(address) {
|
|
2099
|
+
// const {address} = parseAddress(provider)
|
|
2100
|
+
const request = `https://whereis.leofcoin.org/?ip=${address}`;
|
|
2101
|
+
let response = await fetch(request);
|
|
2102
|
+
response = await response.json();
|
|
2103
|
+
const { lat, lon } = response;
|
|
2104
|
+
return { latitude: lat, longitude: lon };
|
|
2105
|
+
}
|
|
2106
|
+
/**
|
|
2107
|
+
* @param {Object} peer
|
|
2108
|
+
* @param {Object} provider
|
|
2109
|
+
* @return {Object} {provider, distance}
|
|
2110
|
+
*/
|
|
2111
|
+
async getDistance(peer, provider) {
|
|
2112
|
+
const { latitude, longitude } = await this.getCoordinates(provider.address);
|
|
2113
|
+
return { provider, distance: distanceInKmBetweenEarthCoordinates(peer.latitude, peer.longitude, latitude, longitude) };
|
|
2114
|
+
}
|
|
2115
|
+
/**
|
|
2116
|
+
* @param {Array} providers
|
|
2117
|
+
* @return {Object} closestPeer
|
|
2118
|
+
*/
|
|
2119
|
+
async closestPeer(providers) {
|
|
2120
|
+
let all = [];
|
|
2121
|
+
const address = await getAddress();
|
|
2122
|
+
const peerLoc = await this.getCoordinates(address);
|
|
2123
|
+
for (const provider of providers) {
|
|
2124
|
+
if (provider.address === '127.0.0.1')
|
|
2125
|
+
all.push({ provider, distance: 0 });
|
|
2126
|
+
else
|
|
2127
|
+
all.push(this.getDistance(peerLoc, provider));
|
|
2128
|
+
}
|
|
2129
|
+
all = await Promise.all(all);
|
|
2130
|
+
all = all.sort((previous, current) => previous.distance - current.distance);
|
|
2131
|
+
return all[0].provider;
|
|
2132
|
+
}
|
|
2133
|
+
/**
|
|
2134
|
+
* @param {String} hash
|
|
2135
|
+
* @return {Array} providers
|
|
2136
|
+
*/
|
|
2137
|
+
providersFor(hash) {
|
|
2138
|
+
return this.providerMap.get(hash);
|
|
2139
|
+
}
|
|
2140
|
+
/**
|
|
2141
|
+
* @param {String} address
|
|
2142
|
+
* @param {String} hash
|
|
2143
|
+
* @return {Array} providers
|
|
2144
|
+
*/
|
|
2145
|
+
async addProvider(address, hash) {
|
|
2146
|
+
let providers = [];
|
|
2147
|
+
if (this.providerMap.has(hash))
|
|
2148
|
+
providers = this.providerMap.get(hash);
|
|
2149
|
+
providers = new Set([...providers, address]);
|
|
2150
|
+
this.providerMap.set(hash, providers);
|
|
2151
|
+
return providers;
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
class MessageHandler {
|
|
2156
|
+
constructor(network) {
|
|
2157
|
+
this.network = network;
|
|
2158
|
+
}
|
|
2159
|
+
/**
|
|
2160
|
+
* hash and sign message
|
|
2161
|
+
*
|
|
2162
|
+
* @param {object} message
|
|
2163
|
+
* @param {Buffer} message.from peer id
|
|
2164
|
+
* @param {Buffer} message.to peer id
|
|
2165
|
+
* @param {string} message.data Peernet message
|
|
2166
|
+
* (PeernetMessage excluded) encoded as a string
|
|
2167
|
+
* @return message
|
|
2168
|
+
*/
|
|
2169
|
+
async hashAndSignMessage(message) {
|
|
2170
|
+
const hash = await message.peernetHash;
|
|
2171
|
+
message.decoded.signature = globalThis.identity.sign(hash.buffer);
|
|
2172
|
+
return message;
|
|
2173
|
+
}
|
|
2174
|
+
/**
|
|
2175
|
+
* @param {String} from - peer id
|
|
2176
|
+
* @param {String} to - peer id
|
|
2177
|
+
* @param {String|PeernetMessage} data - data encoded message string
|
|
2178
|
+
* or the messageNode itself
|
|
2179
|
+
*/
|
|
2180
|
+
async prepareMessage(message) {
|
|
2181
|
+
if (message.keys.includes('signature')) {
|
|
2182
|
+
message = await this.hashAndSignMessage(message);
|
|
2183
|
+
}
|
|
2184
|
+
return message;
|
|
2185
|
+
}
|
|
2186
|
+
}
|
|
2187
|
+
|
|
2188
|
+
const dataHandler = async (message) => {
|
|
2189
|
+
if (!message)
|
|
2190
|
+
return;
|
|
2191
|
+
const { data, id, from } = message;
|
|
2192
|
+
const proto = await protoFor(data);
|
|
2193
|
+
peernet._protoHandler({ id, proto }, peernet.client.connections[from], from);
|
|
2194
|
+
};
|
|
2195
|
+
|
|
2196
|
+
const dhtError = (proto) => {
|
|
2197
|
+
const text = `Received proto ${proto.name} expected peernet-dht-response`;
|
|
2198
|
+
return new Error(`Routing error: ${text}`);
|
|
2199
|
+
};
|
|
2200
|
+
const nothingFoundError = (hash) => {
|
|
2201
|
+
return new Error(`nothing found for ${hash}`);
|
|
2202
|
+
};
|
|
2203
|
+
|
|
2204
|
+
// import base32 from '@vandeurenglenn/base32'
|
|
2205
|
+
// import base58 from '@vandeurenglenn/base58'
|
|
2206
|
+
|
|
2207
|
+
// export const encodings = {
|
|
2208
|
+
// base58,
|
|
2209
|
+
// base32
|
|
2210
|
+
// }
|
|
2211
|
+
|
|
2212
|
+
const encode$2 = (string, encoding = 'utf-8') => {
|
|
2213
|
+
if (typeof string === 'string') {
|
|
2214
|
+
let encoded;
|
|
2215
|
+
|
|
2216
|
+
// if (encodings[encoding]) encoded = encodings[encoding].encode(encoded)
|
|
2217
|
+
encoded = new TextEncoder().encode(string);
|
|
2218
|
+
return encoded
|
|
2219
|
+
}
|
|
2220
|
+
throw Error(`expected typeof String instead got ${string}`)
|
|
2221
|
+
};
|
|
2222
|
+
|
|
2223
|
+
const decode$3 = (uint8Array, encoding) => {
|
|
2224
|
+
if (uint8Array instanceof Uint8Array) {
|
|
2225
|
+
let decoded;
|
|
2226
|
+
// if (encodings[encoding]) decoded = encodings[encoding].decode(decoded)
|
|
2227
|
+
decoded = new TextDecoder().decode(uint8Array);
|
|
2228
|
+
|
|
2229
|
+
return decoded
|
|
2230
|
+
}
|
|
2231
|
+
throw Error(`expected typeof uint8Array instead got ${uint8Array}`)
|
|
2232
|
+
};
|
|
2233
|
+
|
|
2234
|
+
const pathSepS = '/';
|
|
2235
|
+
class KeyPath {
|
|
2236
|
+
|
|
2237
|
+
/**
|
|
2238
|
+
* @param {string | Uint8Array} input
|
|
2239
|
+
*/
|
|
2240
|
+
constructor(input) {
|
|
2241
|
+
if (typeof input === 'string') {
|
|
2242
|
+
this.uint8Array = encode$2(input);
|
|
2243
|
+
} else if (input instanceof Uint8Array) {
|
|
2244
|
+
this.uint8Array = input;
|
|
2245
|
+
} else if (input instanceof KeyPath) {
|
|
2246
|
+
this.uint8Array = input.uint8Array;
|
|
2247
|
+
} else {
|
|
2248
|
+
throw new Error('Invalid keyPath, should be a String, Uint8Array or KeyPath')
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
2251
|
+
|
|
2252
|
+
isKeyPath() {
|
|
2253
|
+
return true
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2256
|
+
/**
|
|
2257
|
+
* Convert to the string representation
|
|
2258
|
+
*
|
|
2259
|
+
* @param {import('uint8arrays/to-string').SupportedEncodings} [encoding='utf8'] - The encoding to use.
|
|
2260
|
+
* @returns {string}
|
|
2261
|
+
*/
|
|
2262
|
+
toString(encoding = 'hex') {
|
|
2263
|
+
return decode$3(this.uint8Array)
|
|
2264
|
+
}
|
|
2265
|
+
|
|
2266
|
+
/**
|
|
2267
|
+
* Returns the `list` representation of this path.
|
|
2268
|
+
*
|
|
2269
|
+
* @returns string[]
|
|
2270
|
+
*
|
|
2271
|
+
* @example
|
|
2272
|
+
* ```js
|
|
2273
|
+
* new Key('/Comedy/MontyPython/Actor:JohnCleese').list()
|
|
2274
|
+
* // => ['Comedy', 'MontyPythong', 'Actor:JohnCleese']
|
|
2275
|
+
* ```
|
|
2276
|
+
*/
|
|
2277
|
+
list() {
|
|
2278
|
+
return this.toString().split(pathSepS).slice(1)
|
|
2279
|
+
}
|
|
2280
|
+
|
|
2281
|
+
}
|
|
2282
|
+
|
|
2283
|
+
class LeofcoinStorage {
|
|
2284
|
+
|
|
2285
|
+
constructor(name = 'storage', root = '.leofcoin') {
|
|
2286
|
+
this.name = name;
|
|
2287
|
+
this.root = root;
|
|
2288
|
+
}
|
|
2289
|
+
|
|
2290
|
+
async init(name, root) {
|
|
2291
|
+
const importee = await import(globalThis.navigator ? './browser-store.js' : './store.js');
|
|
2292
|
+
const Store = importee.default;
|
|
2293
|
+
this.db = new Store(this.name, this.root);
|
|
2294
|
+
}
|
|
2295
|
+
|
|
2296
|
+
async get(key) {
|
|
2297
|
+
if (typeof key === 'object') return this.many('get', key);
|
|
2298
|
+
return this.db.get(new KeyPath(key))
|
|
2299
|
+
}
|
|
2300
|
+
|
|
2301
|
+
/**
|
|
2302
|
+
*
|
|
2303
|
+
* @param {*} key
|
|
2304
|
+
* @param {*} value
|
|
2305
|
+
* @returns Promise
|
|
2306
|
+
*/
|
|
2307
|
+
put(key, value) {
|
|
2308
|
+
if (typeof key === 'object') return this.many('put', key);
|
|
2309
|
+
return this.db.put(new KeyPath(key), new KeyValue(value));
|
|
2310
|
+
}
|
|
2311
|
+
|
|
2312
|
+
async has(key) {
|
|
2313
|
+
if (typeof key === 'object') return this.many('has', key);
|
|
2314
|
+
|
|
2315
|
+
try {
|
|
2316
|
+
const has = await this.db.get(new KeyPath(key));
|
|
2317
|
+
|
|
2318
|
+
return Boolean(has);
|
|
2319
|
+
} catch (e) {
|
|
2320
|
+
return false
|
|
2321
|
+
}
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
async delete(key) {
|
|
2325
|
+
return this.db.delete(new KeyPath(key))
|
|
2326
|
+
}
|
|
2327
|
+
|
|
2328
|
+
keys(limit = -1) {
|
|
2329
|
+
return this.db.keys(limit)
|
|
2330
|
+
}
|
|
2331
|
+
|
|
2332
|
+
async values(limit = -1) {
|
|
2333
|
+
return this.db.values(limit)
|
|
2334
|
+
}
|
|
2335
|
+
|
|
2336
|
+
async many(type, _value) {
|
|
2337
|
+
const jobs = [];
|
|
2338
|
+
|
|
2339
|
+
for (const key of Object.keys(_value)) {
|
|
2340
|
+
jobs.push(this[type](key, _value[key]));
|
|
2341
|
+
}
|
|
2342
|
+
|
|
2343
|
+
return Promise.all(jobs)
|
|
2344
|
+
}
|
|
2345
|
+
|
|
2346
|
+
async length() {
|
|
2347
|
+
const keys = await this.keys();
|
|
2348
|
+
return keys.length
|
|
2349
|
+
}
|
|
2350
|
+
|
|
2351
|
+
async size() {
|
|
2352
|
+
let size = 0;
|
|
2353
|
+
const query = await this.db.iterate();
|
|
2354
|
+
for await (const item of query) {
|
|
2355
|
+
size += item.value ? item.value.length : item[1].length;
|
|
2356
|
+
}
|
|
2357
|
+
return size
|
|
2358
|
+
}
|
|
2359
|
+
|
|
2360
|
+
async clear() {
|
|
2361
|
+
return this.db.clear()
|
|
2362
|
+
}
|
|
2363
|
+
|
|
2364
|
+
async iterate() {
|
|
2365
|
+
return this.db.iterate()
|
|
2366
|
+
}
|
|
2367
|
+
|
|
2368
|
+
}
|
|
3
2369
|
|
|
4
2370
|
const randombytes = strength => crypto.getRandomValues(new Uint8Array(strength));
|
|
5
2371
|
|
|
@@ -193,7 +2559,7 @@ var base58check = { encode: encode$1, decode: decode$2, encodeHex, decodeHex, is
|
|
|
193
2559
|
|
|
194
2560
|
const decode$1 = (multiWif, expectedVersion, expectedCodec) => {
|
|
195
2561
|
const decoded = base58$1.decode(multiWif);
|
|
196
|
-
let [version, codec, privateKey] = index$
|
|
2562
|
+
let [version, codec, privateKey] = index$5(decoded);
|
|
197
2563
|
version = Number(new TextDecoder().decode(version));
|
|
198
2564
|
codec = Number(new TextDecoder().decode(codec));
|
|
199
2565
|
if (expectedVersion && version !== expectedVersion)
|
|
@@ -204,7 +2570,7 @@ const decode$1 = (multiWif, expectedVersion, expectedCodec) => {
|
|
|
204
2570
|
};
|
|
205
2571
|
var index = {
|
|
206
2572
|
encode: (version, codec, privateKey) => {
|
|
207
|
-
return base58$1.encode(index$
|
|
2573
|
+
return base58$1.encode(index$6([
|
|
208
2574
|
new TextEncoder().encode(version.toString()),
|
|
209
2575
|
new TextEncoder().encode(codec.toString()),
|
|
210
2576
|
privateKey
|
|
@@ -9797,255 +12163,57 @@ var elliptic$1 = {
|
|
|
9797
12163
|
const sigs = new BN(sigObj.s);
|
|
9798
12164
|
if (sigr.cmp(ecparams.n) >= 0 || sigs.cmp(ecparams.n) >= 0) return 1
|
|
9799
12165
|
|
|
9800
|
-
if (sigr.isZero() || sigs.isZero()) return 2
|
|
9801
|
-
|
|
9802
|
-
// Can throw `throw new Error('Unable to find sencond key candinate');`
|
|
9803
|
-
let point;
|
|
9804
|
-
try {
|
|
9805
|
-
point = ec.recoverPubKey(msg32, sigObj, recid);
|
|
9806
|
-
} catch (err) {
|
|
9807
|
-
return 2
|
|
9808
|
-
}
|
|
9809
|
-
|
|
9810
|
-
savePublicKey(output, point);
|
|
9811
|
-
|
|
9812
|
-
return 0
|
|
9813
|
-
},
|
|
9814
|
-
|
|
9815
|
-
ecdh (output, pubkey, seckey, data, hashfn, xbuf, ybuf) {
|
|
9816
|
-
const pair = loadPublicKey(pubkey);
|
|
9817
|
-
if (pair === null) return 1
|
|
9818
|
-
|
|
9819
|
-
const scalar = new BN(seckey);
|
|
9820
|
-
if (scalar.cmp(ecparams.n) >= 0 || scalar.isZero()) return 2
|
|
9821
|
-
|
|
9822
|
-
const point = pair.getPublic().mul(scalar);
|
|
9823
|
-
|
|
9824
|
-
if (hashfn === undefined) {
|
|
9825
|
-
const data = point.encode(null, true);
|
|
9826
|
-
const sha256 = ec.hash().update(data).digest();
|
|
9827
|
-
for (let i = 0; i < 32; ++i) output[i] = sha256[i];
|
|
9828
|
-
} else {
|
|
9829
|
-
if (!xbuf) xbuf = new Uint8Array(32);
|
|
9830
|
-
const x = point.getX().toArray('be', 32);
|
|
9831
|
-
for (let i = 0; i < 32; ++i) xbuf[i] = x[i];
|
|
9832
|
-
|
|
9833
|
-
if (!ybuf) ybuf = new Uint8Array(32);
|
|
9834
|
-
const y = point.getY().toArray('be', 32);
|
|
9835
|
-
for (let i = 0; i < 32; ++i) ybuf[i] = y[i];
|
|
9836
|
-
|
|
9837
|
-
const hash = hashfn(xbuf, ybuf, data);
|
|
9838
|
-
|
|
9839
|
-
const isValid = hash instanceof Uint8Array && hash.length === output.length;
|
|
9840
|
-
if (!isValid) return 2
|
|
9841
|
-
|
|
9842
|
-
output.set(hash);
|
|
9843
|
-
}
|
|
9844
|
-
|
|
9845
|
-
return 0
|
|
9846
|
-
}
|
|
9847
|
-
};
|
|
9848
|
-
|
|
9849
|
-
var elliptic = lib(elliptic$1);
|
|
9850
|
-
|
|
9851
|
-
const encode = (version, privateKey, compressed) => {
|
|
9852
|
-
if (privateKey.length !== 32)
|
|
9853
|
-
throw new TypeError(`Invalid privateKey length: expected 32 got ${privateKey.length}`);
|
|
9854
|
-
const uint8Array = new Uint8Array(compressed ? 34 : 33);
|
|
9855
|
-
uint8Array.set([Number(version)]);
|
|
9856
|
-
uint8Array.set(privateKey, 1);
|
|
9857
|
-
if (compressed) {
|
|
9858
|
-
uint8Array.set([Number(version)], 33);
|
|
9859
|
-
}
|
|
9860
|
-
return base58check.encode(uint8Array);
|
|
9861
|
-
};
|
|
9862
|
-
const decode = async (wif, version) => {
|
|
9863
|
-
wif = (await base58check.decode(wif)).data;
|
|
9864
|
-
if (version && wif[0] !== version)
|
|
9865
|
-
throw new Error('Invalid network version');
|
|
9866
|
-
if (wif.length < 33 || wif.length > 34)
|
|
9867
|
-
throw new Error('Invalid WIF length');
|
|
9868
|
-
const comp = new TextEncoder().encode('1');
|
|
9869
|
-
if (wif.length === 34 && wif[33] !== comp[0])
|
|
9870
|
-
throw new Error('Invalid compression flag');
|
|
9871
|
-
return {
|
|
9872
|
-
version: wif[0],
|
|
9873
|
-
privateKey: wif.subarray(1, 33),
|
|
9874
|
-
compressed: wif.length === 33 ? false : true
|
|
9875
|
-
};
|
|
9876
|
-
};
|
|
9877
|
-
var wif = { encode, decode };
|
|
9878
|
-
|
|
9879
|
-
// see https://github.com/bitcoin/bips/blob/master/bip-0039/english.txt
|
|
9880
|
-
var wordlist = ["abandon", "ability", "able", "about", "above", "absent", "absorb", "abstract", "absurd", "abuse", "access", "accident", "account", "accuse", "achieve", "acid", "acoustic", "acquire", "across", "act", "action", "actor", "actress", "actual", "adapt", "add", "addict", "address", "adjust", "admit", "adult", "advance", "advice", "aerobic", "affair", "afford", "afraid", "again", "age", "agent", "agree", "ahead", "aim", "air", "airport", "aisle", "alarm", "album", "alcohol", "alert", "alien", "all", "alley", "allow", "almost", "alone", "alpha", "already", "also", "alter", "always", "amateur", "amazing", "among", "amount", "amused", "analyst", "anchor", "ancient", "anger", "angle", "angry", "animal", "ankle", "announce", "annual", "another", "answer", "antenna", "antique", "anxiety", "any", "apart", "apology", "appear", "apple", "approve", "april", "arch", "arctic", "area", "arena", "argue", "arm", "armed", "armor", "army", "around", "arrange", "arrest", "arrive", "arrow", "art", "artefact", "artist", "artwork", "ask", "aspect", "assault", "asset", "assist", "assume", "asthma", "athlete", "atom", "attack", "attend", "attitude", "attract", "auction", "audit", "august", "aunt", "author", "auto", "autumn", "average", "avocado", "avoid", "awake", "aware", "away", "awesome", "awful", "awkward", "axis", "baby", "bachelor", "bacon", "badge", "bag", "balance", "balcony", "ball", "bamboo", "banana", "banner", "bar", "barely", "bargain", "barrel", "base", "basic", "basket", "battle", "beach", "bean", "beauty", "because", "become", "beef", "before", "begin", "behave", "behind", "believe", "below", "belt", "bench", "benefit", "best", "betray", "better", "between", "beyond", "bicycle", "bid", "bike", "bind", "biology", "bird", "birth", "bitter", "black", "blade", "blame", "blanket", "blast", "bleak", "bless", "blind", "blood", "blossom", "blouse", "blue", "blur", "blush", "board", "boat", "body", "boil", "bomb", "bone", "bonus", "book", "boost", "border", "boring", "borrow", "boss", "bottom", "bounce", "box", "boy", "bracket", "brain", "brand", "brass", "brave", "bread", "breeze", "brick", "bridge", "brief", "bright", "bring", "brisk", "broccoli", "broken", "bronze", "broom", "brother", "brown", "brush", "bubble", "buddy", "budget", "buffalo", "build", "bulb", "bulk", "bullet", "bundle", "bunker", "burden", "burger", "burst", "bus", "business", "busy", "butter", "buyer", "buzz", "cabbage", "cabin", "cable", "cactus", "cage", "cake", "call", "calm", "camera", "camp", "can", "canal", "cancel", "candy", "cannon", "canoe", "canvas", "canyon", "capable", "capital", "captain", "car", "carbon", "card", "cargo", "carpet", "carry", "cart", "case", "cash", "casino", "castle", "casual", "cat", "catalog", "catch", "category", "cattle", "caught", "cause", "caution", "cave", "ceiling", "celery", "cement", "census", "century", "cereal", "certain", "chair", "chalk", "champion", "change", "chaos", "chapter", "charge", "chase", "chat", "cheap", "check", "cheese", "chef", "cherry", "chest", "chicken", "chief", "child", "chimney", "choice", "choose", "chronic", "chuckle", "chunk", "churn", "cigar", "cinnamon", "circle", "citizen", "city", "civil", "claim", "clap", "clarify", "claw", "clay", "clean", "clerk", "clever", "click", "client", "cliff", "climb", "clinic", "clip", "clock", "clog", "close", "cloth", "cloud", "clown", "club", "clump", "cluster", "clutch", "coach", "coast", "coconut", "code", "coffee", "coil", "coin", "collect", "color", "column", "combine", "come", "comfort", "comic", "common", "company", "concert", "conduct", "confirm", "congress", "connect", "consider", "control", "convince", "cook", "cool", "copper", "copy", "coral", "core", "corn", "correct", "cost", "cotton", "couch", "country", "couple", "course", "cousin", "cover", "coyote", "crack", "cradle", "craft", "cram", "crane", "crash", "crater", "crawl", "crazy", "cream", "credit", "creek", "crew", "cricket", "crime", "crisp", "critic", "crop", "cross", "crouch", "crowd", "crucial", "cruel", "cruise", "crumble", "crunch", "crush", "cry", "crystal", "cube", "culture", "cup", "cupboard", "curious", "current", "curtain", "curve", "cushion", "custom", "cute", "cycle", "dad", "damage", "damp", "dance", "danger", "daring", "dash", "daughter", "dawn", "day", "deal", "debate", "debris", "decade", "december", "decide", "decline", "decorate", "decrease", "deer", "defense", "define", "defy", "degree", "delay", "deliver", "demand", "demise", "denial", "dentist", "deny", "depart", "depend", "deposit", "depth", "deputy", "derive", "describe", "desert", "design", "desk", "despair", "destroy", "detail", "detect", "develop", "device", "devote", "diagram", "dial", "diamond", "diary", "dice", "diesel", "diet", "differ", "digital", "dignity", "dilemma", "dinner", "dinosaur", "direct", "dirt", "disagree", "discover", "disease", "dish", "dismiss", "disorder", "display", "distance", "divert", "divide", "divorce", "dizzy", "doctor", "document", "dog", "doll", "dolphin", "domain", "donate", "donkey", "donor", "door", "dose", "double", "dove", "draft", "dragon", "drama", "drastic", "draw", "dream", "dress", "drift", "drill", "drink", "drip", "drive", "drop", "drum", "dry", "duck", "dumb", "dune", "during", "dust", "dutch", "duty", "dwarf", "dynamic", "eager", "eagle", "early", "earn", "earth", "easily", "east", "easy", "echo", "ecology", "economy", "edge", "edit", "educate", "effort", "egg", "eight", "either", "elbow", "elder", "electric", "elegant", "element", "elephant", "elevator", "elite", "else", "embark", "embody", "embrace", "emerge", "emotion", "employ", "empower", "empty", "enable", "enact", "end", "endless", "endorse", "enemy", "energy", "enforce", "engage", "engine", "enhance", "enjoy", "enlist", "enough", "enrich", "enroll", "ensure", "enter", "entire", "entry", "envelope", "episode", "equal", "equip", "era", "erase", "erode", "erosion", "error", "erupt", "escape", "essay", "essence", "estate", "eternal", "ethics", "evidence", "evil", "evoke", "evolve", "exact", "example", "excess", "exchange", "excite", "exclude", "excuse", "execute", "exercise", "exhaust", "exhibit", "exile", "exist", "exit", "exotic", "expand", "expect", "expire", "explain", "expose", "express", "extend", "extra", "eye", "eyebrow", "fabric", "face", "faculty", "fade", "faint", "faith", "fall", "false", "fame", "family", "famous", "fan", "fancy", "fantasy", "farm", "fashion", "fat", "fatal", "father", "fatigue", "fault", "favorite", "feature", "february", "federal", "fee", "feed", "feel", "female", "fence", "festival", "fetch", "fever", "few", "fiber", "fiction", "field", "figure", "file", "film", "filter", "final", "find", "fine", "finger", "finish", "fire", "firm", "first", "fiscal", "fish", "fit", "fitness", "fix", "flag", "flame", "flash", "flat", "flavor", "flee", "flight", "flip", "float", "flock", "floor", "flower", "fluid", "flush", "fly", "foam", "focus", "fog", "foil", "fold", "follow", "food", "foot", "force", "forest", "forget", "fork", "fortune", "forum", "forward", "fossil", "foster", "found", "fox", "fragile", "frame", "frequent", "fresh", "friend", "fringe", "frog", "front", "frost", "frown", "frozen", "fruit", "fuel", "fun", "funny", "furnace", "fury", "future", "gadget", "gain", "galaxy", "gallery", "game", "gap", "garage", "garbage", "garden", "garlic", "garment", "gas", "gasp", "gate", "gather", "gauge", "gaze", "general", "genius", "genre", "gentle", "genuine", "gesture", "ghost", "giant", "gift", "giggle", "ginger", "giraffe", "girl", "give", "glad", "glance", "glare", "glass", "glide", "glimpse", "globe", "gloom", "glory", "glove", "glow", "glue", "goat", "goddess", "gold", "good", "goose", "gorilla", "gospel", "gossip", "govern", "gown", "grab", "grace", "grain", "grant", "grape", "grass", "gravity", "great", "green", "grid", "grief", "grit", "grocery", "group", "grow", "grunt", "guard", "guess", "guide", "guilt", "guitar", "gun", "gym", "habit", "hair", "half", "hammer", "hamster", "hand", "happy", "harbor", "hard", "harsh", "harvest", "hat", "have", "hawk", "hazard", "head", "health", "heart", "heavy", "hedgehog", "height", "hello", "helmet", "help", "hen", "hero", "hidden", "high", "hill", "hint", "hip", "hire", "history", "hobby", "hockey", "hold", "hole", "holiday", "hollow", "home", "honey", "hood", "hope", "horn", "horror", "horse", "hospital", "host", "hotel", "hour", "hover", "hub", "huge", "human", "humble", "humor", "hundred", "hungry", "hunt", "hurdle", "hurry", "hurt", "husband", "hybrid", "ice", "icon", "idea", "identify", "idle", "ignore", "ill", "illegal", "illness", "image", "imitate", "immense", "immune", "impact", "impose", "improve", "impulse", "inch", "include", "income", "increase", "index", "indicate", "indoor", "industry", "infant", "inflict", "inform", "inhale", "inherit", "initial", "inject", "injury", "inmate", "inner", "innocent", "input", "inquiry", "insane", "insect", "inside", "inspire", "install", "intact", "interest", "into", "invest", "invite", "involve", "iron", "island", "isolate", "issue", "item", "ivory", "jacket", "jaguar", "jar", "jazz", "jealous", "jeans", "jelly", "jewel", "job", "join", "joke", "journey", "joy", "judge", "juice", "jump", "jungle", "junior", "junk", "just", "kangaroo", "keen", "keep", "ketchup", "key", "kick", "kid", "kidney", "kind", "kingdom", "kiss", "kit", "kitchen", "kite", "kitten", "kiwi", "knee", "knife", "knock", "know", "lab", "label", "labor", "ladder", "lady", "lake", "lamp", "language", "laptop", "large", "later", "latin", "laugh", "laundry", "lava", "law", "lawn", "lawsuit", "layer", "lazy", "leader", "leaf", "learn", "leave", "lecture", "left", "leg", "legal", "legend", "leisure", "lemon", "lend", "length", "lens", "leopard", "lesson", "letter", "level", "liar", "liberty", "library", "license", "life", "lift", "light", "like", "limb", "limit", "link", "lion", "liquid", "list", "little", "live", "lizard", "load", "loan", "lobster", "local", "lock", "logic", "lonely", "long", "loop", "lottery", "loud", "lounge", "love", "loyal", "lucky", "luggage", "lumber", "lunar", "lunch", "luxury", "lyrics", "machine", "mad", "magic", "magnet", "maid", "mail", "main", "major", "make", "mammal", "man", "manage", "mandate", "mango", "mansion", "manual", "maple", "marble", "march", "margin", "marine", "market", "marriage", "mask", "mass", "master", "match", "material", "math", "matrix", "matter", "maximum", "maze", "meadow", "mean", "measure", "meat", "mechanic", "medal", "media", "melody", "melt", "member", "memory", "mention", "menu", "mercy", "merge", "merit", "merry", "mesh", "message", "metal", "method", "middle", "midnight", "milk", "million", "mimic", "mind", "minimum", "minor", "minute", "miracle", "mirror", "misery", "miss", "mistake", "mix", "mixed", "mixture", "mobile", "model", "modify", "mom", "moment", "monitor", "monkey", "monster", "month", "moon", "moral", "more", "morning", "mosquito", "mother", "motion", "motor", "mountain", "mouse", "move", "movie", "much", "muffin", "mule", "multiply", "muscle", "museum", "mushroom", "music", "must", "mutual", "myself", "mystery", "myth", "naive", "name", "napkin", "narrow", "nasty", "nation", "nature", "near", "neck", "need", "negative", "neglect", "neither", "nephew", "nerve", "nest", "net", "network", "neutral", "never", "news", "next", "nice", "night", "noble", "noise", "nominee", "noodle", "normal", "north", "nose", "notable", "note", "nothing", "notice", "novel", "now", "nuclear", "number", "nurse", "nut", "oak", "obey", "object", "oblige", "obscure", "observe", "obtain", "obvious", "occur", "ocean", "october", "odor", "off", "offer", "office", "often", "oil", "okay", "old", "olive", "olympic", "omit", "once", "one", "onion", "online", "only", "open", "opera", "opinion", "oppose", "option", "orange", "orbit", "orchard", "order", "ordinary", "organ", "orient", "original", "orphan", "ostrich", "other", "outdoor", "outer", "output", "outside", "oval", "oven", "over", "own", "owner", "oxygen", "oyster", "ozone", "pact", "paddle", "page", "pair", "palace", "palm", "panda", "panel", "panic", "panther", "paper", "parade", "parent", "park", "parrot", "party", "pass", "patch", "path", "patient", "patrol", "pattern", "pause", "pave", "payment", "peace", "peanut", "pear", "peasant", "pelican", "pen", "penalty", "pencil", "people", "pepper", "perfect", "permit", "person", "pet", "phone", "photo", "phrase", "physical", "piano", "picnic", "picture", "piece", "pig", "pigeon", "pill", "pilot", "pink", "pioneer", "pipe", "pistol", "pitch", "pizza", "place", "planet", "plastic", "plate", "play", "please", "pledge", "pluck", "plug", "plunge", "poem", "poet", "point", "polar", "pole", "police", "pond", "pony", "pool", "popular", "portion", "position", "possible", "post", "potato", "pottery", "poverty", "powder", "power", "practice", "praise", "predict", "prefer", "prepare", "present", "pretty", "prevent", "price", "pride", "primary", "print", "priority", "prison", "private", "prize", "problem", "process", "produce", "profit", "program", "project", "promote", "proof", "property", "prosper", "protect", "proud", "provide", "public", "pudding", "pull", "pulp", "pulse", "pumpkin", "punch", "pupil", "puppy", "purchase", "purity", "purpose", "purse", "push", "put", "puzzle", "pyramid", "quality", "quantum", "quarter", "question", "quick", "quit", "quiz", "quote", "rabbit", "raccoon", "race", "rack", "radar", "radio", "rail", "rain", "raise", "rally", "ramp", "ranch", "random", "range", "rapid", "rare", "rate", "rather", "raven", "raw", "razor", "ready", "real", "reason", "rebel", "rebuild", "recall", "receive", "recipe", "record", "recycle", "reduce", "reflect", "reform", "refuse", "region", "regret", "regular", "reject", "relax", "release", "relief", "rely", "remain", "remember", "remind", "remove", "render", "renew", "rent", "reopen", "repair", "repeat", "replace", "report", "require", "rescue", "resemble", "resist", "resource", "response", "result", "retire", "retreat", "return", "reunion", "reveal", "review", "reward", "rhythm", "rib", "ribbon", "rice", "rich", "ride", "ridge", "rifle", "right", "rigid", "ring", "riot", "ripple", "risk", "ritual", "rival", "river", "road", "roast", "robot", "robust", "rocket", "romance", "roof", "rookie", "room", "rose", "rotate", "rough", "round", "route", "royal", "rubber", "rude", "rug", "rule", "run", "runway", "rural", "sad", "saddle", "sadness", "safe", "sail", "salad", "salmon", "salon", "salt", "salute", "same", "sample", "sand", "satisfy", "satoshi", "sauce", "sausage", "save", "say", "scale", "scan", "scare", "scatter", "scene", "scheme", "school", "science", "scissors", "scorpion", "scout", "scrap", "screen", "script", "scrub", "sea", "search", "season", "seat", "second", "secret", "section", "security", "seed", "seek", "segment", "select", "sell", "seminar", "senior", "sense", "sentence", "series", "service", "session", "settle", "setup", "seven", "shadow", "shaft", "shallow", "share", "shed", "shell", "sheriff", "shield", "shift", "shine", "ship", "shiver", "shock", "shoe", "shoot", "shop", "short", "shoulder", "shove", "shrimp", "shrug", "shuffle", "shy", "sibling", "sick", "side", "siege", "sight", "sign", "silent", "silk", "silly", "silver", "similar", "simple", "since", "sing", "siren", "sister", "situate", "six", "size", "skate", "sketch", "ski", "skill", "skin", "skirt", "skull", "slab", "slam", "sleep", "slender", "slice", "slide", "slight", "slim", "slogan", "slot", "slow", "slush", "small", "smart", "smile", "smoke", "smooth", "snack", "snake", "snap", "sniff", "snow", "soap", "soccer", "social", "sock", "soda", "soft", "solar", "soldier", "solid", "solution", "solve", "someone", "song", "soon", "sorry", "sort", "soul", "sound", "soup", "source", "south", "space", "spare", "spatial", "spawn", "speak", "special", "speed", "spell", "spend", "sphere", "spice", "spider", "spike", "spin", "spirit", "split", "spoil", "sponsor", "spoon", "sport", "spot", "spray", "spread", "spring", "spy", "square", "squeeze", "squirrel", "stable", "stadium", "staff", "stage", "stairs", "stamp", "stand", "start", "state", "stay", "steak", "steel", "stem", "step", "stereo", "stick", "still", "sting", "stock", "stomach", "stone", "stool", "story", "stove", "strategy", "street", "strike", "strong", "struggle", "student", "stuff", "stumble", "style", "subject", "submit", "subway", "success", "such", "sudden", "suffer", "sugar", "suggest", "suit", "summer", "sun", "sunny", "sunset", "super", "supply", "supreme", "sure", "surface", "surge", "surprise", "surround", "survey", "suspect", "sustain", "swallow", "swamp", "swap", "swarm", "swear", "sweet", "swift", "swim", "swing", "switch", "sword", "symbol", "symptom", "syrup", "system", "table", "tackle", "tag", "tail", "talent", "talk", "tank", "tape", "target", "task", "taste", "tattoo", "taxi", "teach", "team", "tell", "ten", "tenant", "tennis", "tent", "term", "test", "text", "thank", "that", "theme", "then", "theory", "there", "they", "thing", "this", "thought", "three", "thrive", "throw", "thumb", "thunder", "ticket", "tide", "tiger", "tilt", "timber", "time", "tiny", "tip", "tired", "tissue", "title", "toast", "tobacco", "today", "toddler", "toe", "together", "toilet", "token", "tomato", "tomorrow", "tone", "tongue", "tonight", "tool", "tooth", "top", "topic", "topple", "torch", "tornado", "tortoise", "toss", "total", "tourist", "toward", "tower", "town", "toy", "track", "trade", "traffic", "tragic", "train", "transfer", "trap", "trash", "travel", "tray", "treat", "tree", "trend", "trial", "tribe", "trick", "trigger", "trim", "trip", "trophy", "trouble", "truck", "true", "truly", "trumpet", "trust", "truth", "try", "tube", "tuition", "tumble", "tuna", "tunnel", "turkey", "turn", "turtle", "twelve", "twenty", "twice", "twin", "twist", "two", "type", "typical", "ugly", "umbrella", "unable", "unaware", "uncle", "uncover", "under", "undo", "unfair", "unfold", "unhappy", "uniform", "unique", "unit", "universe", "unknown", "unlock", "until", "unusual", "unveil", "update", "upgrade", "uphold", "upon", "upper", "upset", "urban", "urge", "usage", "use", "used", "useful", "useless", "usual", "utility", "vacant", "vacuum", "vague", "valid", "valley", "valve", "van", "vanish", "vapor", "various", "vast", "vault", "vehicle", "velvet", "vendor", "venture", "venue", "verb", "verify", "version", "very", "vessel", "veteran", "viable", "vibrant", "vicious", "victory", "video", "view", "village", "vintage", "violin", "virtual", "virus", "visa", "visit", "visual", "vital", "vivid", "vocal", "voice", "void", "volcano", "volume", "vote", "voyage", "wage", "wagon", "wait", "walk", "wall", "walnut", "want", "warfare", "warm", "warrior", "wash", "wasp", "waste", "water", "wave", "way", "wealth", "weapon", "wear", "weasel", "weather", "web", "wedding", "weekend", "weird", "welcome", "west", "wet", "whale", "what", "wheat", "wheel", "when", "where", "whip", "whisper", "wide", "width", "wife", "wild", "will", "win", "window", "wine", "wing", "wink", "winner", "winter", "wire", "wisdom", "wise", "wish", "witness", "wolf", "woman", "wonder", "wood", "wool", "word", "work", "world", "worry", "worth", "wrap", "wreck", "wrestle", "wrist", "write", "wrong", "yard", "year", "yellow", "you", "young", "youth", "zebra", "zero", "zone", "zoo"];
|
|
9881
|
-
|
|
9882
|
-
class Mnemonic {
|
|
9883
|
-
wordlist;
|
|
9884
|
-
constructor(options) {
|
|
9885
|
-
// english always loaded, rest included by dev
|
|
9886
|
-
this.wordlist = options?.wordlist || wordlist;
|
|
9887
|
-
}
|
|
9888
|
-
lpad(string, padString, length) {
|
|
9889
|
-
while (string.length < length) {
|
|
9890
|
-
string = padString + string;
|
|
9891
|
-
}
|
|
9892
|
-
return string;
|
|
9893
|
-
}
|
|
9894
|
-
normalize(string) {
|
|
9895
|
-
return (string || '').normalize('NFKD');
|
|
9896
|
-
}
|
|
9897
|
-
bytesToBinary(bytes) {
|
|
9898
|
-
return bytes.map((byte) => this.lpad(byte.toString(2), '0', 8)).join('');
|
|
9899
|
-
}
|
|
9900
|
-
async deriveChecksumBits(entropyBuffer) {
|
|
9901
|
-
const entropy = entropyBuffer.length * 8;
|
|
9902
|
-
const cs = entropy / 32;
|
|
9903
|
-
const hash = await createHash(entropyBuffer, 'SHA-512');
|
|
9904
|
-
return this.bytesToBinary(Array.from(hash)).slice(0, cs);
|
|
9905
|
-
}
|
|
9906
|
-
async mnemonicFromEntropy(entropyBuffer) {
|
|
9907
|
-
let checksum = await this.deriveChecksumBits(entropyBuffer);
|
|
9908
|
-
const entropy = this.bytesToBinary(Array.from(entropyBuffer));
|
|
9909
|
-
let bits = entropy + checksum;
|
|
9910
|
-
return bits
|
|
9911
|
-
.match(/(.{1,11})/g)
|
|
9912
|
-
.map((binary) => {
|
|
9913
|
-
const index = parseInt(binary, 2);
|
|
9914
|
-
return this.wordlist[index];
|
|
9915
|
-
}).join(' ');
|
|
9916
|
-
}
|
|
9917
|
-
generate(strength = 256) {
|
|
9918
|
-
return this.mnemonicFromEntropy(randombytes(strength / 8));
|
|
9919
|
-
}
|
|
9920
|
-
salt(password) {
|
|
9921
|
-
return 'mnemonic' + this.normalize(password);
|
|
9922
|
-
}
|
|
9923
|
-
seedFromMnemonic(mnemonic, password, strength = 256, iterations = 2048) {
|
|
9924
|
-
const encoder = new TextEncoder();
|
|
9925
|
-
return pbkdf2(encoder.encode(this.salt(password)), encoder.encode(this.normalize(mnemonic)), iterations, strength, 'SHA-512');
|
|
9926
|
-
}
|
|
9927
|
-
}
|
|
9928
|
-
|
|
9929
|
-
class MultiSignature {
|
|
9930
|
-
multiCodec;
|
|
9931
|
-
version;
|
|
9932
|
-
decoded;
|
|
9933
|
-
encoded;
|
|
9934
|
-
#multiSignature;
|
|
9935
|
-
constructor(version, multiCodec) {
|
|
9936
|
-
if (version === undefined)
|
|
9937
|
-
throw ReferenceError('version undefined');
|
|
9938
|
-
if (multiCodec === undefined)
|
|
9939
|
-
throw ReferenceError('multicodec undefined');
|
|
9940
|
-
this.multiCodec = multiCodec;
|
|
9941
|
-
this.version = version;
|
|
9942
|
-
}
|
|
9943
|
-
get signature() {
|
|
9944
|
-
return this.decoded.signature;
|
|
9945
|
-
}
|
|
9946
|
-
get multiSignature() {
|
|
9947
|
-
return this.#multiSignature || this.encoded || this.encode(this.signature);
|
|
9948
|
-
}
|
|
9949
|
-
export() {
|
|
9950
|
-
return base58$1.encode(this.multiSignature);
|
|
9951
|
-
}
|
|
9952
|
-
import(encoded) {
|
|
9953
|
-
return base58$1.decode(encoded);
|
|
9954
|
-
}
|
|
9955
|
-
sign(hash, privateKey) {
|
|
9956
|
-
if (!hash || !privateKey)
|
|
9957
|
-
throw ReferenceError(`${hash ? 'privateKey' : 'hash'} undefined`);
|
|
9958
|
-
const { signature } = elliptic.ecdsaSign(hash, privateKey);
|
|
9959
|
-
this.decoded = {
|
|
9960
|
-
version: this.version,
|
|
9961
|
-
multiCodec: this.multiCodec,
|
|
9962
|
-
signature
|
|
9963
|
-
};
|
|
9964
|
-
return this.encode(signature);
|
|
9965
|
-
}
|
|
9966
|
-
/**
|
|
9967
|
-
* verify signature (multiSignature.signature)
|
|
9968
|
-
*/
|
|
9969
|
-
verifySignature(signature, hash, publicKey) {
|
|
9970
|
-
return elliptic.ecdsaVerify(signature, hash, publicKey);
|
|
9971
|
-
}
|
|
9972
|
-
/**
|
|
9973
|
-
* verify multiSignature
|
|
9974
|
-
*/
|
|
9975
|
-
verify(multiSignature, hash, publicKey) {
|
|
9976
|
-
multiSignature = this.decode(multiSignature);
|
|
9977
|
-
return elliptic.ecdsaVerify(multiSignature.signature, hash, publicKey);
|
|
9978
|
-
}
|
|
9979
|
-
encode(signature) {
|
|
9980
|
-
signature = signature || this.signature;
|
|
9981
|
-
if (!signature)
|
|
9982
|
-
throw ReferenceError('signature undefined');
|
|
9983
|
-
this.#multiSignature = typedArrayConcat([
|
|
9984
|
-
index$3.encode(this.version),
|
|
9985
|
-
index$3.encode(this.multiCodec),
|
|
9986
|
-
signature
|
|
9987
|
-
]);
|
|
9988
|
-
return this.multiSignature;
|
|
9989
|
-
}
|
|
9990
|
-
/**
|
|
9991
|
-
* decode exported multi signature to object
|
|
9992
|
-
* @param {multiSignature} multiSignature base58 encoded string
|
|
9993
|
-
* @return {decodedMultiSignature} { version, multiCodec, signature }
|
|
9994
|
-
*/
|
|
9995
|
-
decode(multiSignature) {
|
|
9996
|
-
if (multiSignature)
|
|
9997
|
-
this.#multiSignature = multiSignature;
|
|
9998
|
-
if (!this.multiSignature)
|
|
9999
|
-
throw ReferenceError('multiSignature undefined');
|
|
10000
|
-
let buffer = this.multiSignature;
|
|
10001
|
-
const version = index$3.decode(buffer);
|
|
10002
|
-
buffer = buffer.subarray(index$3.decode.bytes);
|
|
10003
|
-
const codec = index$3.decode(buffer);
|
|
10004
|
-
buffer = buffer.subarray(index$3.decode.bytes);
|
|
10005
|
-
const signature = buffer.subarray(0, buffer.length);
|
|
10006
|
-
if (version !== this.version)
|
|
10007
|
-
throw TypeError('Invalid version');
|
|
10008
|
-
if (this.multiCodec !== codec)
|
|
10009
|
-
throw TypeError('Invalid multiCodec');
|
|
10010
|
-
this.decoded = {
|
|
10011
|
-
version,
|
|
10012
|
-
multiCodec: codec,
|
|
10013
|
-
signature
|
|
10014
|
-
};
|
|
10015
|
-
return this.decoded;
|
|
10016
|
-
}
|
|
10017
|
-
toString() {
|
|
10018
|
-
return this.multiSignature.toString();
|
|
10019
|
-
}
|
|
10020
|
-
fromString(string) {
|
|
10021
|
-
return this.decode(new Uint8Array(string.split(',')));
|
|
10022
|
-
}
|
|
10023
|
-
toBs58() {
|
|
10024
|
-
return base58$1.encode(this.multiSignature);
|
|
10025
|
-
}
|
|
10026
|
-
fromBs58(string) {
|
|
10027
|
-
return this.decode(base58$1.decode(string));
|
|
10028
|
-
}
|
|
10029
|
-
toBs32() {
|
|
10030
|
-
return index$4.encode(this.multiSignature);
|
|
10031
|
-
}
|
|
10032
|
-
fromBs32(string) {
|
|
10033
|
-
return this.decode(index$4.decode(string));
|
|
10034
|
-
}
|
|
10035
|
-
toBs32Hex() {
|
|
10036
|
-
return index$4.encodeHex(this.multiSignature);
|
|
10037
|
-
}
|
|
10038
|
-
fromBs32Hex(string) {
|
|
10039
|
-
return this.decode(index$4.decodeHex(string));
|
|
10040
|
-
}
|
|
10041
|
-
toBs58Hex() {
|
|
10042
|
-
return base58$1.encodeHex(this.multiSignature);
|
|
10043
|
-
}
|
|
10044
|
-
fromBs58Hex(string) {
|
|
10045
|
-
return this.decode(base58$1.decodeHex(string));
|
|
10046
|
-
}
|
|
10047
|
-
}
|
|
10048
|
-
|
|
12166
|
+
if (sigr.isZero() || sigs.isZero()) return 2
|
|
12167
|
+
|
|
12168
|
+
// Can throw `throw new Error('Unable to find sencond key candinate');`
|
|
12169
|
+
let point;
|
|
12170
|
+
try {
|
|
12171
|
+
point = ec.recoverPubKey(msg32, sigObj, recid);
|
|
12172
|
+
} catch (err) {
|
|
12173
|
+
return 2
|
|
12174
|
+
}
|
|
12175
|
+
|
|
12176
|
+
savePublicKey(output, point);
|
|
12177
|
+
|
|
12178
|
+
return 0
|
|
12179
|
+
},
|
|
12180
|
+
|
|
12181
|
+
ecdh (output, pubkey, seckey, data, hashfn, xbuf, ybuf) {
|
|
12182
|
+
const pair = loadPublicKey(pubkey);
|
|
12183
|
+
if (pair === null) return 1
|
|
12184
|
+
|
|
12185
|
+
const scalar = new BN(seckey);
|
|
12186
|
+
if (scalar.cmp(ecparams.n) >= 0 || scalar.isZero()) return 2
|
|
12187
|
+
|
|
12188
|
+
const point = pair.getPublic().mul(scalar);
|
|
12189
|
+
|
|
12190
|
+
if (hashfn === undefined) {
|
|
12191
|
+
const data = point.encode(null, true);
|
|
12192
|
+
const sha256 = ec.hash().update(data).digest();
|
|
12193
|
+
for (let i = 0; i < 32; ++i) output[i] = sha256[i];
|
|
12194
|
+
} else {
|
|
12195
|
+
if (!xbuf) xbuf = new Uint8Array(32);
|
|
12196
|
+
const x = point.getX().toArray('be', 32);
|
|
12197
|
+
for (let i = 0; i < 32; ++i) xbuf[i] = x[i];
|
|
12198
|
+
|
|
12199
|
+
if (!ybuf) ybuf = new Uint8Array(32);
|
|
12200
|
+
const y = point.getY().toArray('be', 32);
|
|
12201
|
+
for (let i = 0; i < 32; ++i) ybuf[i] = y[i];
|
|
12202
|
+
|
|
12203
|
+
const hash = hashfn(xbuf, ybuf, data);
|
|
12204
|
+
|
|
12205
|
+
const isValid = hash instanceof Uint8Array && hash.length === output.length;
|
|
12206
|
+
if (!isValid) return 2
|
|
12207
|
+
|
|
12208
|
+
output.set(hash);
|
|
12209
|
+
}
|
|
12210
|
+
|
|
12211
|
+
return 0
|
|
12212
|
+
}
|
|
12213
|
+
};
|
|
12214
|
+
|
|
12215
|
+
var elliptic = lib(elliptic$1);
|
|
12216
|
+
|
|
10049
12217
|
const leofcoinOlivia = {
|
|
10050
12218
|
messagePrefix: '\u0019Leofcoin Signed Message:',
|
|
10051
12219
|
version: 1,
|
|
@@ -10149,6 +12317,34 @@ var networks = {
|
|
|
10149
12317
|
ethereum
|
|
10150
12318
|
};
|
|
10151
12319
|
|
|
12320
|
+
const encode = (version, privateKey, compressed) => {
|
|
12321
|
+
if (privateKey.length !== 32)
|
|
12322
|
+
throw new TypeError(`Invalid privateKey length: expected 32 got ${privateKey.length}`);
|
|
12323
|
+
const uint8Array = new Uint8Array(compressed ? 34 : 33);
|
|
12324
|
+
uint8Array.set([Number(version)]);
|
|
12325
|
+
uint8Array.set(privateKey, 1);
|
|
12326
|
+
if (compressed) {
|
|
12327
|
+
uint8Array.set([Number(version)], 33);
|
|
12328
|
+
}
|
|
12329
|
+
return base58check.encode(uint8Array);
|
|
12330
|
+
};
|
|
12331
|
+
const decode = async (wif, version) => {
|
|
12332
|
+
wif = (await base58check.decode(wif)).data;
|
|
12333
|
+
if (version && wif[0] !== version)
|
|
12334
|
+
throw new Error('Invalid network version');
|
|
12335
|
+
if (wif.length < 33 || wif.length > 34)
|
|
12336
|
+
throw new Error('Invalid WIF length');
|
|
12337
|
+
const comp = new TextEncoder().encode('1');
|
|
12338
|
+
if (wif.length === 34 && wif[33] !== comp[0])
|
|
12339
|
+
throw new Error('Invalid compression flag');
|
|
12340
|
+
return {
|
|
12341
|
+
version: wif[0],
|
|
12342
|
+
privateKey: wif.subarray(1, 33),
|
|
12343
|
+
compressed: wif.length === 33 ? false : true
|
|
12344
|
+
};
|
|
12345
|
+
};
|
|
12346
|
+
var wif = { encode, decode };
|
|
12347
|
+
|
|
10152
12348
|
const HIGHEST_BIT = 0x80000000;
|
|
10153
12349
|
const { publicKeyCreate, publicKeyVerify, privateKeyVerify, privateKeyTweakAdd, ecdh } = elliptic;
|
|
10154
12350
|
class HdNode {
|
|
@@ -10239,7 +12435,7 @@ class HdNode {
|
|
|
10239
12435
|
else {
|
|
10240
12436
|
set.push(new Uint8Array(this.publicKey));
|
|
10241
12437
|
}
|
|
10242
|
-
return base58check.encode(index$
|
|
12438
|
+
return base58check.encode(index$6(set));
|
|
10243
12439
|
}
|
|
10244
12440
|
toWIF() {
|
|
10245
12441
|
if (!this.#privateKey)
|
|
@@ -10255,14 +12451,14 @@ class HdNode {
|
|
|
10255
12451
|
if (this.isNeutered)
|
|
10256
12452
|
throw new TypeError('Missing private key for hardened child key');
|
|
10257
12453
|
// data = 0x00 || ser256(kpar) || ser32(index)
|
|
10258
|
-
data = index$
|
|
12454
|
+
data = index$6([
|
|
10259
12455
|
new TextEncoder().encode('0'),
|
|
10260
12456
|
this.privateKey,
|
|
10261
12457
|
new TextEncoder().encode(index.toString())
|
|
10262
12458
|
]);
|
|
10263
12459
|
}
|
|
10264
12460
|
else {
|
|
10265
|
-
data = index$
|
|
12461
|
+
data = index$6([
|
|
10266
12462
|
this.publicKey,
|
|
10267
12463
|
new TextEncoder().encode(index.toString())
|
|
10268
12464
|
]);
|
|
@@ -10326,7 +12522,7 @@ class HdNode {
|
|
|
10326
12522
|
let buffer = (await base58check.decode(string)).data;
|
|
10327
12523
|
network = network || networks.leofcoin;
|
|
10328
12524
|
// 4 bytes: version bytes
|
|
10329
|
-
let [version, depth, parentFingerprint, index, chainCode, k, privateKey] = index$
|
|
12525
|
+
let [version, depth, parentFingerprint, index, chainCode, k, privateKey] = index$5(buffer);
|
|
10330
12526
|
version = Number(new TextDecoder().decode(version));
|
|
10331
12527
|
depth = Number(new TextDecoder().decode(depth));
|
|
10332
12528
|
parentFingerprint = Number(new TextDecoder().decode(parentFingerprint));
|
|
@@ -10350,6 +12546,56 @@ class HdNode {
|
|
|
10350
12546
|
}
|
|
10351
12547
|
}
|
|
10352
12548
|
|
|
12549
|
+
// see https://github.com/bitcoin/bips/blob/master/bip-0039/english.txt
|
|
12550
|
+
var wordlist = ["abandon", "ability", "able", "about", "above", "absent", "absorb", "abstract", "absurd", "abuse", "access", "accident", "account", "accuse", "achieve", "acid", "acoustic", "acquire", "across", "act", "action", "actor", "actress", "actual", "adapt", "add", "addict", "address", "adjust", "admit", "adult", "advance", "advice", "aerobic", "affair", "afford", "afraid", "again", "age", "agent", "agree", "ahead", "aim", "air", "airport", "aisle", "alarm", "album", "alcohol", "alert", "alien", "all", "alley", "allow", "almost", "alone", "alpha", "already", "also", "alter", "always", "amateur", "amazing", "among", "amount", "amused", "analyst", "anchor", "ancient", "anger", "angle", "angry", "animal", "ankle", "announce", "annual", "another", "answer", "antenna", "antique", "anxiety", "any", "apart", "apology", "appear", "apple", "approve", "april", "arch", "arctic", "area", "arena", "argue", "arm", "armed", "armor", "army", "around", "arrange", "arrest", "arrive", "arrow", "art", "artefact", "artist", "artwork", "ask", "aspect", "assault", "asset", "assist", "assume", "asthma", "athlete", "atom", "attack", "attend", "attitude", "attract", "auction", "audit", "august", "aunt", "author", "auto", "autumn", "average", "avocado", "avoid", "awake", "aware", "away", "awesome", "awful", "awkward", "axis", "baby", "bachelor", "bacon", "badge", "bag", "balance", "balcony", "ball", "bamboo", "banana", "banner", "bar", "barely", "bargain", "barrel", "base", "basic", "basket", "battle", "beach", "bean", "beauty", "because", "become", "beef", "before", "begin", "behave", "behind", "believe", "below", "belt", "bench", "benefit", "best", "betray", "better", "between", "beyond", "bicycle", "bid", "bike", "bind", "biology", "bird", "birth", "bitter", "black", "blade", "blame", "blanket", "blast", "bleak", "bless", "blind", "blood", "blossom", "blouse", "blue", "blur", "blush", "board", "boat", "body", "boil", "bomb", "bone", "bonus", "book", "boost", "border", "boring", "borrow", "boss", "bottom", "bounce", "box", "boy", "bracket", "brain", "brand", "brass", "brave", "bread", "breeze", "brick", "bridge", "brief", "bright", "bring", "brisk", "broccoli", "broken", "bronze", "broom", "brother", "brown", "brush", "bubble", "buddy", "budget", "buffalo", "build", "bulb", "bulk", "bullet", "bundle", "bunker", "burden", "burger", "burst", "bus", "business", "busy", "butter", "buyer", "buzz", "cabbage", "cabin", "cable", "cactus", "cage", "cake", "call", "calm", "camera", "camp", "can", "canal", "cancel", "candy", "cannon", "canoe", "canvas", "canyon", "capable", "capital", "captain", "car", "carbon", "card", "cargo", "carpet", "carry", "cart", "case", "cash", "casino", "castle", "casual", "cat", "catalog", "catch", "category", "cattle", "caught", "cause", "caution", "cave", "ceiling", "celery", "cement", "census", "century", "cereal", "certain", "chair", "chalk", "champion", "change", "chaos", "chapter", "charge", "chase", "chat", "cheap", "check", "cheese", "chef", "cherry", "chest", "chicken", "chief", "child", "chimney", "choice", "choose", "chronic", "chuckle", "chunk", "churn", "cigar", "cinnamon", "circle", "citizen", "city", "civil", "claim", "clap", "clarify", "claw", "clay", "clean", "clerk", "clever", "click", "client", "cliff", "climb", "clinic", "clip", "clock", "clog", "close", "cloth", "cloud", "clown", "club", "clump", "cluster", "clutch", "coach", "coast", "coconut", "code", "coffee", "coil", "coin", "collect", "color", "column", "combine", "come", "comfort", "comic", "common", "company", "concert", "conduct", "confirm", "congress", "connect", "consider", "control", "convince", "cook", "cool", "copper", "copy", "coral", "core", "corn", "correct", "cost", "cotton", "couch", "country", "couple", "course", "cousin", "cover", "coyote", "crack", "cradle", "craft", "cram", "crane", "crash", "crater", "crawl", "crazy", "cream", "credit", "creek", "crew", "cricket", "crime", "crisp", "critic", "crop", "cross", "crouch", "crowd", "crucial", "cruel", "cruise", "crumble", "crunch", "crush", "cry", "crystal", "cube", "culture", "cup", "cupboard", "curious", "current", "curtain", "curve", "cushion", "custom", "cute", "cycle", "dad", "damage", "damp", "dance", "danger", "daring", "dash", "daughter", "dawn", "day", "deal", "debate", "debris", "decade", "december", "decide", "decline", "decorate", "decrease", "deer", "defense", "define", "defy", "degree", "delay", "deliver", "demand", "demise", "denial", "dentist", "deny", "depart", "depend", "deposit", "depth", "deputy", "derive", "describe", "desert", "design", "desk", "despair", "destroy", "detail", "detect", "develop", "device", "devote", "diagram", "dial", "diamond", "diary", "dice", "diesel", "diet", "differ", "digital", "dignity", "dilemma", "dinner", "dinosaur", "direct", "dirt", "disagree", "discover", "disease", "dish", "dismiss", "disorder", "display", "distance", "divert", "divide", "divorce", "dizzy", "doctor", "document", "dog", "doll", "dolphin", "domain", "donate", "donkey", "donor", "door", "dose", "double", "dove", "draft", "dragon", "drama", "drastic", "draw", "dream", "dress", "drift", "drill", "drink", "drip", "drive", "drop", "drum", "dry", "duck", "dumb", "dune", "during", "dust", "dutch", "duty", "dwarf", "dynamic", "eager", "eagle", "early", "earn", "earth", "easily", "east", "easy", "echo", "ecology", "economy", "edge", "edit", "educate", "effort", "egg", "eight", "either", "elbow", "elder", "electric", "elegant", "element", "elephant", "elevator", "elite", "else", "embark", "embody", "embrace", "emerge", "emotion", "employ", "empower", "empty", "enable", "enact", "end", "endless", "endorse", "enemy", "energy", "enforce", "engage", "engine", "enhance", "enjoy", "enlist", "enough", "enrich", "enroll", "ensure", "enter", "entire", "entry", "envelope", "episode", "equal", "equip", "era", "erase", "erode", "erosion", "error", "erupt", "escape", "essay", "essence", "estate", "eternal", "ethics", "evidence", "evil", "evoke", "evolve", "exact", "example", "excess", "exchange", "excite", "exclude", "excuse", "execute", "exercise", "exhaust", "exhibit", "exile", "exist", "exit", "exotic", "expand", "expect", "expire", "explain", "expose", "express", "extend", "extra", "eye", "eyebrow", "fabric", "face", "faculty", "fade", "faint", "faith", "fall", "false", "fame", "family", "famous", "fan", "fancy", "fantasy", "farm", "fashion", "fat", "fatal", "father", "fatigue", "fault", "favorite", "feature", "february", "federal", "fee", "feed", "feel", "female", "fence", "festival", "fetch", "fever", "few", "fiber", "fiction", "field", "figure", "file", "film", "filter", "final", "find", "fine", "finger", "finish", "fire", "firm", "first", "fiscal", "fish", "fit", "fitness", "fix", "flag", "flame", "flash", "flat", "flavor", "flee", "flight", "flip", "float", "flock", "floor", "flower", "fluid", "flush", "fly", "foam", "focus", "fog", "foil", "fold", "follow", "food", "foot", "force", "forest", "forget", "fork", "fortune", "forum", "forward", "fossil", "foster", "found", "fox", "fragile", "frame", "frequent", "fresh", "friend", "fringe", "frog", "front", "frost", "frown", "frozen", "fruit", "fuel", "fun", "funny", "furnace", "fury", "future", "gadget", "gain", "galaxy", "gallery", "game", "gap", "garage", "garbage", "garden", "garlic", "garment", "gas", "gasp", "gate", "gather", "gauge", "gaze", "general", "genius", "genre", "gentle", "genuine", "gesture", "ghost", "giant", "gift", "giggle", "ginger", "giraffe", "girl", "give", "glad", "glance", "glare", "glass", "glide", "glimpse", "globe", "gloom", "glory", "glove", "glow", "glue", "goat", "goddess", "gold", "good", "goose", "gorilla", "gospel", "gossip", "govern", "gown", "grab", "grace", "grain", "grant", "grape", "grass", "gravity", "great", "green", "grid", "grief", "grit", "grocery", "group", "grow", "grunt", "guard", "guess", "guide", "guilt", "guitar", "gun", "gym", "habit", "hair", "half", "hammer", "hamster", "hand", "happy", "harbor", "hard", "harsh", "harvest", "hat", "have", "hawk", "hazard", "head", "health", "heart", "heavy", "hedgehog", "height", "hello", "helmet", "help", "hen", "hero", "hidden", "high", "hill", "hint", "hip", "hire", "history", "hobby", "hockey", "hold", "hole", "holiday", "hollow", "home", "honey", "hood", "hope", "horn", "horror", "horse", "hospital", "host", "hotel", "hour", "hover", "hub", "huge", "human", "humble", "humor", "hundred", "hungry", "hunt", "hurdle", "hurry", "hurt", "husband", "hybrid", "ice", "icon", "idea", "identify", "idle", "ignore", "ill", "illegal", "illness", "image", "imitate", "immense", "immune", "impact", "impose", "improve", "impulse", "inch", "include", "income", "increase", "index", "indicate", "indoor", "industry", "infant", "inflict", "inform", "inhale", "inherit", "initial", "inject", "injury", "inmate", "inner", "innocent", "input", "inquiry", "insane", "insect", "inside", "inspire", "install", "intact", "interest", "into", "invest", "invite", "involve", "iron", "island", "isolate", "issue", "item", "ivory", "jacket", "jaguar", "jar", "jazz", "jealous", "jeans", "jelly", "jewel", "job", "join", "joke", "journey", "joy", "judge", "juice", "jump", "jungle", "junior", "junk", "just", "kangaroo", "keen", "keep", "ketchup", "key", "kick", "kid", "kidney", "kind", "kingdom", "kiss", "kit", "kitchen", "kite", "kitten", "kiwi", "knee", "knife", "knock", "know", "lab", "label", "labor", "ladder", "lady", "lake", "lamp", "language", "laptop", "large", "later", "latin", "laugh", "laundry", "lava", "law", "lawn", "lawsuit", "layer", "lazy", "leader", "leaf", "learn", "leave", "lecture", "left", "leg", "legal", "legend", "leisure", "lemon", "lend", "length", "lens", "leopard", "lesson", "letter", "level", "liar", "liberty", "library", "license", "life", "lift", "light", "like", "limb", "limit", "link", "lion", "liquid", "list", "little", "live", "lizard", "load", "loan", "lobster", "local", "lock", "logic", "lonely", "long", "loop", "lottery", "loud", "lounge", "love", "loyal", "lucky", "luggage", "lumber", "lunar", "lunch", "luxury", "lyrics", "machine", "mad", "magic", "magnet", "maid", "mail", "main", "major", "make", "mammal", "man", "manage", "mandate", "mango", "mansion", "manual", "maple", "marble", "march", "margin", "marine", "market", "marriage", "mask", "mass", "master", "match", "material", "math", "matrix", "matter", "maximum", "maze", "meadow", "mean", "measure", "meat", "mechanic", "medal", "media", "melody", "melt", "member", "memory", "mention", "menu", "mercy", "merge", "merit", "merry", "mesh", "message", "metal", "method", "middle", "midnight", "milk", "million", "mimic", "mind", "minimum", "minor", "minute", "miracle", "mirror", "misery", "miss", "mistake", "mix", "mixed", "mixture", "mobile", "model", "modify", "mom", "moment", "monitor", "monkey", "monster", "month", "moon", "moral", "more", "morning", "mosquito", "mother", "motion", "motor", "mountain", "mouse", "move", "movie", "much", "muffin", "mule", "multiply", "muscle", "museum", "mushroom", "music", "must", "mutual", "myself", "mystery", "myth", "naive", "name", "napkin", "narrow", "nasty", "nation", "nature", "near", "neck", "need", "negative", "neglect", "neither", "nephew", "nerve", "nest", "net", "network", "neutral", "never", "news", "next", "nice", "night", "noble", "noise", "nominee", "noodle", "normal", "north", "nose", "notable", "note", "nothing", "notice", "novel", "now", "nuclear", "number", "nurse", "nut", "oak", "obey", "object", "oblige", "obscure", "observe", "obtain", "obvious", "occur", "ocean", "october", "odor", "off", "offer", "office", "often", "oil", "okay", "old", "olive", "olympic", "omit", "once", "one", "onion", "online", "only", "open", "opera", "opinion", "oppose", "option", "orange", "orbit", "orchard", "order", "ordinary", "organ", "orient", "original", "orphan", "ostrich", "other", "outdoor", "outer", "output", "outside", "oval", "oven", "over", "own", "owner", "oxygen", "oyster", "ozone", "pact", "paddle", "page", "pair", "palace", "palm", "panda", "panel", "panic", "panther", "paper", "parade", "parent", "park", "parrot", "party", "pass", "patch", "path", "patient", "patrol", "pattern", "pause", "pave", "payment", "peace", "peanut", "pear", "peasant", "pelican", "pen", "penalty", "pencil", "people", "pepper", "perfect", "permit", "person", "pet", "phone", "photo", "phrase", "physical", "piano", "picnic", "picture", "piece", "pig", "pigeon", "pill", "pilot", "pink", "pioneer", "pipe", "pistol", "pitch", "pizza", "place", "planet", "plastic", "plate", "play", "please", "pledge", "pluck", "plug", "plunge", "poem", "poet", "point", "polar", "pole", "police", "pond", "pony", "pool", "popular", "portion", "position", "possible", "post", "potato", "pottery", "poverty", "powder", "power", "practice", "praise", "predict", "prefer", "prepare", "present", "pretty", "prevent", "price", "pride", "primary", "print", "priority", "prison", "private", "prize", "problem", "process", "produce", "profit", "program", "project", "promote", "proof", "property", "prosper", "protect", "proud", "provide", "public", "pudding", "pull", "pulp", "pulse", "pumpkin", "punch", "pupil", "puppy", "purchase", "purity", "purpose", "purse", "push", "put", "puzzle", "pyramid", "quality", "quantum", "quarter", "question", "quick", "quit", "quiz", "quote", "rabbit", "raccoon", "race", "rack", "radar", "radio", "rail", "rain", "raise", "rally", "ramp", "ranch", "random", "range", "rapid", "rare", "rate", "rather", "raven", "raw", "razor", "ready", "real", "reason", "rebel", "rebuild", "recall", "receive", "recipe", "record", "recycle", "reduce", "reflect", "reform", "refuse", "region", "regret", "regular", "reject", "relax", "release", "relief", "rely", "remain", "remember", "remind", "remove", "render", "renew", "rent", "reopen", "repair", "repeat", "replace", "report", "require", "rescue", "resemble", "resist", "resource", "response", "result", "retire", "retreat", "return", "reunion", "reveal", "review", "reward", "rhythm", "rib", "ribbon", "rice", "rich", "ride", "ridge", "rifle", "right", "rigid", "ring", "riot", "ripple", "risk", "ritual", "rival", "river", "road", "roast", "robot", "robust", "rocket", "romance", "roof", "rookie", "room", "rose", "rotate", "rough", "round", "route", "royal", "rubber", "rude", "rug", "rule", "run", "runway", "rural", "sad", "saddle", "sadness", "safe", "sail", "salad", "salmon", "salon", "salt", "salute", "same", "sample", "sand", "satisfy", "satoshi", "sauce", "sausage", "save", "say", "scale", "scan", "scare", "scatter", "scene", "scheme", "school", "science", "scissors", "scorpion", "scout", "scrap", "screen", "script", "scrub", "sea", "search", "season", "seat", "second", "secret", "section", "security", "seed", "seek", "segment", "select", "sell", "seminar", "senior", "sense", "sentence", "series", "service", "session", "settle", "setup", "seven", "shadow", "shaft", "shallow", "share", "shed", "shell", "sheriff", "shield", "shift", "shine", "ship", "shiver", "shock", "shoe", "shoot", "shop", "short", "shoulder", "shove", "shrimp", "shrug", "shuffle", "shy", "sibling", "sick", "side", "siege", "sight", "sign", "silent", "silk", "silly", "silver", "similar", "simple", "since", "sing", "siren", "sister", "situate", "six", "size", "skate", "sketch", "ski", "skill", "skin", "skirt", "skull", "slab", "slam", "sleep", "slender", "slice", "slide", "slight", "slim", "slogan", "slot", "slow", "slush", "small", "smart", "smile", "smoke", "smooth", "snack", "snake", "snap", "sniff", "snow", "soap", "soccer", "social", "sock", "soda", "soft", "solar", "soldier", "solid", "solution", "solve", "someone", "song", "soon", "sorry", "sort", "soul", "sound", "soup", "source", "south", "space", "spare", "spatial", "spawn", "speak", "special", "speed", "spell", "spend", "sphere", "spice", "spider", "spike", "spin", "spirit", "split", "spoil", "sponsor", "spoon", "sport", "spot", "spray", "spread", "spring", "spy", "square", "squeeze", "squirrel", "stable", "stadium", "staff", "stage", "stairs", "stamp", "stand", "start", "state", "stay", "steak", "steel", "stem", "step", "stereo", "stick", "still", "sting", "stock", "stomach", "stone", "stool", "story", "stove", "strategy", "street", "strike", "strong", "struggle", "student", "stuff", "stumble", "style", "subject", "submit", "subway", "success", "such", "sudden", "suffer", "sugar", "suggest", "suit", "summer", "sun", "sunny", "sunset", "super", "supply", "supreme", "sure", "surface", "surge", "surprise", "surround", "survey", "suspect", "sustain", "swallow", "swamp", "swap", "swarm", "swear", "sweet", "swift", "swim", "swing", "switch", "sword", "symbol", "symptom", "syrup", "system", "table", "tackle", "tag", "tail", "talent", "talk", "tank", "tape", "target", "task", "taste", "tattoo", "taxi", "teach", "team", "tell", "ten", "tenant", "tennis", "tent", "term", "test", "text", "thank", "that", "theme", "then", "theory", "there", "they", "thing", "this", "thought", "three", "thrive", "throw", "thumb", "thunder", "ticket", "tide", "tiger", "tilt", "timber", "time", "tiny", "tip", "tired", "tissue", "title", "toast", "tobacco", "today", "toddler", "toe", "together", "toilet", "token", "tomato", "tomorrow", "tone", "tongue", "tonight", "tool", "tooth", "top", "topic", "topple", "torch", "tornado", "tortoise", "toss", "total", "tourist", "toward", "tower", "town", "toy", "track", "trade", "traffic", "tragic", "train", "transfer", "trap", "trash", "travel", "tray", "treat", "tree", "trend", "trial", "tribe", "trick", "trigger", "trim", "trip", "trophy", "trouble", "truck", "true", "truly", "trumpet", "trust", "truth", "try", "tube", "tuition", "tumble", "tuna", "tunnel", "turkey", "turn", "turtle", "twelve", "twenty", "twice", "twin", "twist", "two", "type", "typical", "ugly", "umbrella", "unable", "unaware", "uncle", "uncover", "under", "undo", "unfair", "unfold", "unhappy", "uniform", "unique", "unit", "universe", "unknown", "unlock", "until", "unusual", "unveil", "update", "upgrade", "uphold", "upon", "upper", "upset", "urban", "urge", "usage", "use", "used", "useful", "useless", "usual", "utility", "vacant", "vacuum", "vague", "valid", "valley", "valve", "van", "vanish", "vapor", "various", "vast", "vault", "vehicle", "velvet", "vendor", "venture", "venue", "verb", "verify", "version", "very", "vessel", "veteran", "viable", "vibrant", "vicious", "victory", "video", "view", "village", "vintage", "violin", "virtual", "virus", "visa", "visit", "visual", "vital", "vivid", "vocal", "voice", "void", "volcano", "volume", "vote", "voyage", "wage", "wagon", "wait", "walk", "wall", "walnut", "want", "warfare", "warm", "warrior", "wash", "wasp", "waste", "water", "wave", "way", "wealth", "weapon", "wear", "weasel", "weather", "web", "wedding", "weekend", "weird", "welcome", "west", "wet", "whale", "what", "wheat", "wheel", "when", "where", "whip", "whisper", "wide", "width", "wife", "wild", "will", "win", "window", "wine", "wing", "wink", "winner", "winter", "wire", "wisdom", "wise", "wish", "witness", "wolf", "woman", "wonder", "wood", "wool", "word", "work", "world", "worry", "worth", "wrap", "wreck", "wrestle", "wrist", "write", "wrong", "yard", "year", "yellow", "you", "young", "youth", "zebra", "zero", "zone", "zoo"];
|
|
12551
|
+
|
|
12552
|
+
class Mnemonic {
|
|
12553
|
+
wordlist;
|
|
12554
|
+
constructor(options) {
|
|
12555
|
+
// english always loaded, rest included by dev
|
|
12556
|
+
this.wordlist = options?.wordlist || wordlist;
|
|
12557
|
+
}
|
|
12558
|
+
lpad(string, padString, length) {
|
|
12559
|
+
while (string.length < length) {
|
|
12560
|
+
string = padString + string;
|
|
12561
|
+
}
|
|
12562
|
+
return string;
|
|
12563
|
+
}
|
|
12564
|
+
normalize(string) {
|
|
12565
|
+
return (string || '').normalize('NFKD');
|
|
12566
|
+
}
|
|
12567
|
+
bytesToBinary(bytes) {
|
|
12568
|
+
return bytes.map((byte) => this.lpad(byte.toString(2), '0', 8)).join('');
|
|
12569
|
+
}
|
|
12570
|
+
async deriveChecksumBits(entropyBuffer) {
|
|
12571
|
+
const entropy = entropyBuffer.length * 8;
|
|
12572
|
+
const cs = entropy / 32;
|
|
12573
|
+
const hash = await createHash(entropyBuffer, 'SHA-512');
|
|
12574
|
+
return this.bytesToBinary(Array.from(hash)).slice(0, cs);
|
|
12575
|
+
}
|
|
12576
|
+
async mnemonicFromEntropy(entropyBuffer) {
|
|
12577
|
+
let checksum = await this.deriveChecksumBits(entropyBuffer);
|
|
12578
|
+
const entropy = this.bytesToBinary(Array.from(entropyBuffer));
|
|
12579
|
+
let bits = entropy + checksum;
|
|
12580
|
+
return bits
|
|
12581
|
+
.match(/(.{1,11})/g)
|
|
12582
|
+
.map((binary) => {
|
|
12583
|
+
const index = parseInt(binary, 2);
|
|
12584
|
+
return this.wordlist[index];
|
|
12585
|
+
}).join(' ');
|
|
12586
|
+
}
|
|
12587
|
+
generate(strength = 256) {
|
|
12588
|
+
return this.mnemonicFromEntropy(randombytes(strength / 8));
|
|
12589
|
+
}
|
|
12590
|
+
salt(password) {
|
|
12591
|
+
return 'mnemonic' + this.normalize(password);
|
|
12592
|
+
}
|
|
12593
|
+
seedFromMnemonic(mnemonic, password, strength = 256, iterations = 2048) {
|
|
12594
|
+
const encoder = new TextEncoder();
|
|
12595
|
+
return pbkdf2(encoder.encode(this.salt(password)), encoder.encode(this.normalize(mnemonic)), iterations, strength, 'SHA-512');
|
|
12596
|
+
}
|
|
12597
|
+
}
|
|
12598
|
+
|
|
10353
12599
|
const fromNetworkString = network => {
|
|
10354
12600
|
const parts = network.split(':');
|
|
10355
12601
|
network = networks[parts[0]];
|
|
@@ -10452,51 +12698,171 @@ class HDWallet {
|
|
|
10452
12698
|
await this.defineHDNode(await (new HdNode()).fromSeed(new Uint8Array(seed), network));
|
|
10453
12699
|
return mnemonic;
|
|
10454
12700
|
}
|
|
10455
|
-
/**
|
|
10456
|
-
* recover using mnemonic (recovery word list)
|
|
10457
|
-
*/
|
|
10458
|
-
async recover(mnemonic, password, network) {
|
|
10459
|
-
network = this.validateNetwork(network || password);
|
|
10460
|
-
const seed = await new Mnemonic().seedFromMnemonic(mnemonic, password, 512);
|
|
10461
|
-
let node = new HdNode();
|
|
10462
|
-
node = await node.fromSeed(new Uint8Array(seed), network);
|
|
10463
|
-
await this.defineHDNode(await node.fromSeed(new Uint8Array(seed), network));
|
|
12701
|
+
/**
|
|
12702
|
+
* recover using mnemonic (recovery word list)
|
|
12703
|
+
*/
|
|
12704
|
+
async recover(mnemonic, password, network) {
|
|
12705
|
+
network = this.validateNetwork(network || password);
|
|
12706
|
+
const seed = await new Mnemonic().seedFromMnemonic(mnemonic, password, 512);
|
|
12707
|
+
let node = new HdNode();
|
|
12708
|
+
node = await node.fromSeed(new Uint8Array(seed), network);
|
|
12709
|
+
await this.defineHDNode(await node.fromSeed(new Uint8Array(seed), network));
|
|
12710
|
+
}
|
|
12711
|
+
async load(base58, network) {
|
|
12712
|
+
network = this.validateNetwork(network);
|
|
12713
|
+
await this.defineHDNode(await (new HdNode()).fromBase58(base58, network));
|
|
12714
|
+
}
|
|
12715
|
+
save() {
|
|
12716
|
+
return this.hdnode.toBase58();
|
|
12717
|
+
}
|
|
12718
|
+
async fromAddress(address, chainCode, network) {
|
|
12719
|
+
network = this.validateNetwork(network);
|
|
12720
|
+
address = (await base58check.decode(address)).data;
|
|
12721
|
+
if (!chainCode || chainCode && !Buffer.isBuffer(chainCode))
|
|
12722
|
+
chainCode = address.slice(1);
|
|
12723
|
+
await this.defineHDNode(await (new HdNode()).fromPublicKey(address, chainCode, network));
|
|
12724
|
+
}
|
|
12725
|
+
async fromPublicKey(hex, chainCode, network) {
|
|
12726
|
+
network = this.validateNetwork(network);
|
|
12727
|
+
if (!chainCode || chainCode)
|
|
12728
|
+
chainCode = hex.slice(1);
|
|
12729
|
+
let node = new HdNode();
|
|
12730
|
+
node = await node.fromPublicKey(hex, chainCode, network);
|
|
12731
|
+
await this.defineHDNode(node);
|
|
12732
|
+
return this;
|
|
12733
|
+
}
|
|
12734
|
+
async fromPrivateKey(privateKey, chainCode, network) {
|
|
12735
|
+
await this.defineHDNode(await (new HdNode()).fromPrivateKey(privateKey, chainCode, network));
|
|
12736
|
+
}
|
|
12737
|
+
}
|
|
12738
|
+
|
|
12739
|
+
class MultiSignature {
|
|
12740
|
+
multiCodec;
|
|
12741
|
+
version;
|
|
12742
|
+
decoded;
|
|
12743
|
+
encoded;
|
|
12744
|
+
#multiSignature;
|
|
12745
|
+
constructor(version, multiCodec) {
|
|
12746
|
+
if (version === undefined)
|
|
12747
|
+
throw ReferenceError('version undefined');
|
|
12748
|
+
if (multiCodec === undefined)
|
|
12749
|
+
throw ReferenceError('multicodec undefined');
|
|
12750
|
+
this.multiCodec = multiCodec;
|
|
12751
|
+
this.version = version;
|
|
12752
|
+
}
|
|
12753
|
+
get signature() {
|
|
12754
|
+
return this.decoded.signature;
|
|
12755
|
+
}
|
|
12756
|
+
get multiSignature() {
|
|
12757
|
+
return this.#multiSignature || this.encoded || this.encode(this.signature);
|
|
12758
|
+
}
|
|
12759
|
+
export() {
|
|
12760
|
+
return base58$1.encode(this.multiSignature);
|
|
12761
|
+
}
|
|
12762
|
+
import(encoded) {
|
|
12763
|
+
return base58$1.decode(encoded);
|
|
12764
|
+
}
|
|
12765
|
+
sign(hash, privateKey) {
|
|
12766
|
+
if (!hash || !privateKey)
|
|
12767
|
+
throw ReferenceError(`${hash ? 'privateKey' : 'hash'} undefined`);
|
|
12768
|
+
const { signature } = elliptic.ecdsaSign(hash, privateKey);
|
|
12769
|
+
this.decoded = {
|
|
12770
|
+
version: this.version,
|
|
12771
|
+
multiCodec: this.multiCodec,
|
|
12772
|
+
signature
|
|
12773
|
+
};
|
|
12774
|
+
return this.encode(signature);
|
|
12775
|
+
}
|
|
12776
|
+
/**
|
|
12777
|
+
* verify signature (multiSignature.signature)
|
|
12778
|
+
*/
|
|
12779
|
+
verifySignature(signature, hash, publicKey) {
|
|
12780
|
+
return elliptic.ecdsaVerify(signature, hash, publicKey);
|
|
12781
|
+
}
|
|
12782
|
+
/**
|
|
12783
|
+
* verify multiSignature
|
|
12784
|
+
*/
|
|
12785
|
+
verify(multiSignature, hash, publicKey) {
|
|
12786
|
+
multiSignature = this.decode(multiSignature);
|
|
12787
|
+
return elliptic.ecdsaVerify(multiSignature.signature, hash, publicKey);
|
|
12788
|
+
}
|
|
12789
|
+
encode(signature) {
|
|
12790
|
+
signature = signature || this.signature;
|
|
12791
|
+
if (!signature)
|
|
12792
|
+
throw ReferenceError('signature undefined');
|
|
12793
|
+
this.#multiSignature = typedArrayConcat([
|
|
12794
|
+
index$7.encode(this.version),
|
|
12795
|
+
index$7.encode(this.multiCodec),
|
|
12796
|
+
signature
|
|
12797
|
+
]);
|
|
12798
|
+
return this.multiSignature;
|
|
12799
|
+
}
|
|
12800
|
+
/**
|
|
12801
|
+
* decode exported multi signature to object
|
|
12802
|
+
* @param {multiSignature} multiSignature base58 encoded string
|
|
12803
|
+
* @return {decodedMultiSignature} { version, multiCodec, signature }
|
|
12804
|
+
*/
|
|
12805
|
+
decode(multiSignature) {
|
|
12806
|
+
if (multiSignature)
|
|
12807
|
+
this.#multiSignature = multiSignature;
|
|
12808
|
+
if (!this.multiSignature)
|
|
12809
|
+
throw ReferenceError('multiSignature undefined');
|
|
12810
|
+
let buffer = this.multiSignature;
|
|
12811
|
+
const version = index$7.decode(buffer);
|
|
12812
|
+
buffer = buffer.subarray(index$7.decode.bytes);
|
|
12813
|
+
const codec = index$7.decode(buffer);
|
|
12814
|
+
buffer = buffer.subarray(index$7.decode.bytes);
|
|
12815
|
+
const signature = buffer.subarray(0, buffer.length);
|
|
12816
|
+
if (version !== this.version)
|
|
12817
|
+
throw TypeError('Invalid version');
|
|
12818
|
+
if (this.multiCodec !== codec)
|
|
12819
|
+
throw TypeError('Invalid multiCodec');
|
|
12820
|
+
this.decoded = {
|
|
12821
|
+
version,
|
|
12822
|
+
multiCodec: codec,
|
|
12823
|
+
signature
|
|
12824
|
+
};
|
|
12825
|
+
return this.decoded;
|
|
12826
|
+
}
|
|
12827
|
+
toString() {
|
|
12828
|
+
return this.multiSignature.toString();
|
|
12829
|
+
}
|
|
12830
|
+
fromString(string) {
|
|
12831
|
+
return this.decode(new Uint8Array(string.split(',')));
|
|
12832
|
+
}
|
|
12833
|
+
toBs58() {
|
|
12834
|
+
return base58$1.encode(this.multiSignature);
|
|
12835
|
+
}
|
|
12836
|
+
fromBs58(string) {
|
|
12837
|
+
return this.decode(base58$1.decode(string));
|
|
10464
12838
|
}
|
|
10465
|
-
|
|
10466
|
-
|
|
10467
|
-
await this.defineHDNode(await (new HdNode()).fromBase58(base58, network));
|
|
12839
|
+
toBs32() {
|
|
12840
|
+
return index$8.encode(this.multiSignature);
|
|
10468
12841
|
}
|
|
10469
|
-
|
|
10470
|
-
return this.
|
|
12842
|
+
fromBs32(string) {
|
|
12843
|
+
return this.decode(index$8.decode(string));
|
|
10471
12844
|
}
|
|
10472
|
-
|
|
10473
|
-
|
|
10474
|
-
address = (await base58check.decode(address)).data;
|
|
10475
|
-
if (!chainCode || chainCode && !Buffer.isBuffer(chainCode))
|
|
10476
|
-
chainCode = address.slice(1);
|
|
10477
|
-
await this.defineHDNode(await (new HdNode()).fromPublicKey(address, chainCode, network));
|
|
12845
|
+
toBs32Hex() {
|
|
12846
|
+
return index$8.encodeHex(this.multiSignature);
|
|
10478
12847
|
}
|
|
10479
|
-
|
|
10480
|
-
|
|
10481
|
-
if (!chainCode || chainCode)
|
|
10482
|
-
chainCode = hex.slice(1);
|
|
10483
|
-
let node = new HdNode();
|
|
10484
|
-
node = await node.fromPublicKey(hex, chainCode, network);
|
|
10485
|
-
await this.defineHDNode(node);
|
|
10486
|
-
return this;
|
|
12848
|
+
fromBs32Hex(string) {
|
|
12849
|
+
return this.decode(index$8.decodeHex(string));
|
|
10487
12850
|
}
|
|
10488
|
-
|
|
10489
|
-
|
|
12851
|
+
toBs58Hex() {
|
|
12852
|
+
return base58$1.encodeHex(this.multiSignature);
|
|
12853
|
+
}
|
|
12854
|
+
fromBs58Hex(string) {
|
|
12855
|
+
return this.decode(base58$1.decodeHex(string));
|
|
10490
12856
|
}
|
|
10491
12857
|
}
|
|
10492
12858
|
|
|
10493
|
-
class
|
|
12859
|
+
class MultiHDNode extends HDWallet {
|
|
10494
12860
|
#encrypted;
|
|
10495
12861
|
constructor(network, hdnode) {
|
|
10496
12862
|
super(network, hdnode);
|
|
10497
12863
|
}
|
|
10498
12864
|
get id() {
|
|
10499
|
-
return base58check.encode(index$
|
|
12865
|
+
return base58check.encode(index$6([
|
|
10500
12866
|
new TextEncoder().encode(this.version.toString()),
|
|
10501
12867
|
this.account(0).hdnode.neutered.publicKey
|
|
10502
12868
|
]));
|
|
@@ -10504,13 +12870,10 @@ class MultiWallet extends HDWallet {
|
|
|
10504
12870
|
get multiWIF() {
|
|
10505
12871
|
return this.toMultiWif();
|
|
10506
12872
|
}
|
|
10507
|
-
get neutered() {
|
|
10508
|
-
return new HDAccount(this.networkName, this, this.hdnode.depth);
|
|
10509
|
-
}
|
|
10510
12873
|
async fromId(id) {
|
|
10511
12874
|
let buffer = (await base58check.decode(id)).data;
|
|
10512
|
-
index$
|
|
10513
|
-
buffer = buffer.slice(index$
|
|
12875
|
+
index$7.decode(buffer);
|
|
12876
|
+
buffer = buffer.slice(index$7.decode.bytes);
|
|
10514
12877
|
this.fromPublicKey(buffer, null, this.networkName);
|
|
10515
12878
|
}
|
|
10516
12879
|
async lock(multiWIF) {
|
|
@@ -10550,6 +12913,51 @@ class MultiWallet extends HDWallet {
|
|
|
10550
12913
|
return new MultiSignature(this.version, this.network.multiCodec)
|
|
10551
12914
|
.verify(multiSignature, hash, this.publicKey);
|
|
10552
12915
|
}
|
|
12916
|
+
}
|
|
12917
|
+
|
|
12918
|
+
class HDAccount extends MultiHDNode {
|
|
12919
|
+
/**
|
|
12920
|
+
* @param {number} depth - acount depth
|
|
12921
|
+
*/
|
|
12922
|
+
constructor(network, hdnode, depth = 0) {
|
|
12923
|
+
super(network, hdnode);
|
|
12924
|
+
this.hdnode = hdnode;
|
|
12925
|
+
this.depth = depth;
|
|
12926
|
+
this._prefix = `m/44'/${hdnode.network.coin_type}'/${depth}'/`;
|
|
12927
|
+
}
|
|
12928
|
+
get neutered() {
|
|
12929
|
+
return this.hdnode.neutered;
|
|
12930
|
+
}
|
|
12931
|
+
/**
|
|
12932
|
+
* @param {number} index - address index
|
|
12933
|
+
*/
|
|
12934
|
+
async internal(index = 0) {
|
|
12935
|
+
return this.hdnode.derivePath(`${this._prefix}1/${index}`);
|
|
12936
|
+
}
|
|
12937
|
+
/**
|
|
12938
|
+
* @param {number} index - address index
|
|
12939
|
+
*/
|
|
12940
|
+
async external(index = 0) {
|
|
12941
|
+
return this.hdnode.derivePath(`${this._prefix}0/${index}`);
|
|
12942
|
+
}
|
|
12943
|
+
}
|
|
12944
|
+
|
|
12945
|
+
class MultiWallet extends MultiHDNode {
|
|
12946
|
+
constructor(network, hdnode) {
|
|
12947
|
+
super(network, hdnode);
|
|
12948
|
+
}
|
|
12949
|
+
get id() {
|
|
12950
|
+
return base58check.encode(index$6([
|
|
12951
|
+
new TextEncoder().encode(this.version.toString()),
|
|
12952
|
+
this.account(0).hdnode.neutered.publicKey
|
|
12953
|
+
]));
|
|
12954
|
+
}
|
|
12955
|
+
get multiWIF() {
|
|
12956
|
+
return this.toMultiWif();
|
|
12957
|
+
}
|
|
12958
|
+
get neutered() {
|
|
12959
|
+
return new HDAccount(this.networkName, this, this.hdnode.depth);
|
|
12960
|
+
}
|
|
10553
12961
|
/**
|
|
10554
12962
|
* @param {number} account - account to return chain for
|
|
10555
12963
|
* @return internal(addressIndex), external(addressIndex)
|
|
@@ -10557,6 +12965,10 @@ class MultiWallet extends HDWallet {
|
|
|
10557
12965
|
account(index) {
|
|
10558
12966
|
return new HDAccount(this.networkName, this, index);
|
|
10559
12967
|
}
|
|
12968
|
+
async fromAccount(privateKey, depth, network) {
|
|
12969
|
+
const node = await new MultiWallet(network).fromPrivateKey(privateKey);
|
|
12970
|
+
return new HDAccount(node, depth);
|
|
12971
|
+
}
|
|
10560
12972
|
/**
|
|
10561
12973
|
* m / purpose' / coin_type' / account' / change / aadress_index
|
|
10562
12974
|
*
|
|
@@ -10568,34 +12980,696 @@ class MultiWallet extends HDWallet {
|
|
|
10568
12980
|
async derive(index) {
|
|
10569
12981
|
return new MultiWallet(this.networkName, await this.hdnode.derive(index));
|
|
10570
12982
|
}
|
|
10571
|
-
|
|
10572
|
-
|
|
10573
|
-
|
|
12983
|
+
}
|
|
12984
|
+
|
|
12985
|
+
class Identity {
|
|
12986
|
+
#wallet;
|
|
12987
|
+
network;
|
|
12988
|
+
id;
|
|
12989
|
+
constructor(network) {
|
|
12990
|
+
this.network = network;
|
|
12991
|
+
}
|
|
12992
|
+
get accounts() {
|
|
12993
|
+
return this.getAccounts();
|
|
12994
|
+
}
|
|
12995
|
+
async getAccounts() {
|
|
12996
|
+
let accounts = await globalThis.walletStore.get('accounts');
|
|
12997
|
+
accounts = new TextDecoder().decode(accounts);
|
|
12998
|
+
return JSON.parse(accounts);
|
|
12999
|
+
}
|
|
13000
|
+
async load(password) {
|
|
13001
|
+
if (!password) {
|
|
13002
|
+
const importee = await import('./src/prompts/password.js');
|
|
13003
|
+
password = await importee.default();
|
|
13004
|
+
}
|
|
13005
|
+
const accountExists = await globalThis.accountStore.has('public');
|
|
13006
|
+
if (accountExists) {
|
|
13007
|
+
const pub = await globalThis.accountStore.get('public');
|
|
13008
|
+
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
|
|
13009
|
+
const selected = await globalThis.walletStore.get('selected-account');
|
|
13010
|
+
globalThis.peernet.selectedAccount = new TextDecoder().decode(selected);
|
|
13011
|
+
}
|
|
13012
|
+
else {
|
|
13013
|
+
const importee = await import(/* webpackChunkName: "generate-account" */ './index-3699fac4.js');
|
|
13014
|
+
const { identity, accounts } = await importee.default(password, this.network);
|
|
13015
|
+
await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
|
|
13016
|
+
await globalThis.walletStore.put('version', String(1));
|
|
13017
|
+
await globalThis.walletStore.put('accounts', JSON.stringify(accounts));
|
|
13018
|
+
await globalThis.walletStore.put('selected-account', accounts[0][1]);
|
|
13019
|
+
await globalThis.walletStore.put('identity', JSON.stringify(identity));
|
|
13020
|
+
globalThis.peernet.selectedAccount = accounts[0][1];
|
|
13021
|
+
this.id = identity.walletId;
|
|
13022
|
+
}
|
|
13023
|
+
const identity = JSON.parse(new TextDecoder().decode(await globalThis.walletStore.get('identity')));
|
|
13024
|
+
this.#wallet = new MultiWallet(this.network);
|
|
13025
|
+
await this.#wallet.recover(identity.mnemonic, password, this.network);
|
|
13026
|
+
}
|
|
13027
|
+
sign(hash) {
|
|
13028
|
+
return this.#wallet.sign(hash.subarray(0, 32));
|
|
13029
|
+
}
|
|
13030
|
+
async export(password) {
|
|
13031
|
+
if (!password)
|
|
13032
|
+
throw new Error('IdentityExportError: password required');
|
|
13033
|
+
const identity = JSON.parse(new TextDecoder().decode(await globalThis.walletStore.get('identity')));
|
|
13034
|
+
this.#wallet = new MultiWallet(this.network);
|
|
13035
|
+
await this.#wallet.recover(identity.mnemonic, password, this.network);
|
|
13036
|
+
return this.#wallet.toMultiWif();
|
|
13037
|
+
}
|
|
13038
|
+
async import(multiWIF) {
|
|
13039
|
+
this.#wallet = new MultiWallet(this.network);
|
|
13040
|
+
await this.#wallet.fromMultiWif(multiWIF);
|
|
13041
|
+
}
|
|
13042
|
+
async unlock({ key, iv, cipher }) {
|
|
10574
13043
|
}
|
|
10575
|
-
}
|
|
10576
|
-
// TODO: multihash addresses
|
|
10577
|
-
class HDAccount extends MultiWallet {
|
|
10578
13044
|
/**
|
|
10579
|
-
*
|
|
13045
|
+
* Lock current wallet or import wallet using a MultiWIF and lock
|
|
13046
|
+
*
|
|
13047
|
+
* @param password
|
|
13048
|
+
* @param multiWIF
|
|
10580
13049
|
*/
|
|
10581
|
-
|
|
10582
|
-
super(network, hdnode);
|
|
10583
|
-
this.hdnode = hdnode;
|
|
10584
|
-
this.depth = depth;
|
|
10585
|
-
this._prefix = `m/44'/${hdnode.network.coin_type}'/${depth}'/`;
|
|
13050
|
+
async lock(password, multiWIF) {
|
|
10586
13051
|
}
|
|
13052
|
+
}
|
|
13053
|
+
|
|
13054
|
+
globalThis.LeofcoinStorage = LeofcoinStorage;
|
|
13055
|
+
globalThis.leofcoin = globalThis.leofcoin || {};
|
|
13056
|
+
globalThis.pubsub = globalThis.pubsub || new LittlePubSub();
|
|
13057
|
+
globalThis.globalSub = globalThis.globalSub || new LittlePubSub(true);
|
|
13058
|
+
/**
|
|
13059
|
+
* @access public
|
|
13060
|
+
* @example
|
|
13061
|
+
* const peernet = new Peernet();
|
|
13062
|
+
*/
|
|
13063
|
+
class Peernet {
|
|
13064
|
+
identity;
|
|
13065
|
+
stores = [];
|
|
10587
13066
|
/**
|
|
10588
|
-
* @
|
|
13067
|
+
* @type {Object}
|
|
13068
|
+
* @property {Object} peer Instance of Peer
|
|
10589
13069
|
*/
|
|
10590
|
-
|
|
10591
|
-
|
|
13070
|
+
dht = new DhtEarth();
|
|
13071
|
+
/** @leofcoin/peernet-swarm/client */
|
|
13072
|
+
client;
|
|
13073
|
+
network;
|
|
13074
|
+
stars;
|
|
13075
|
+
networkVersion;
|
|
13076
|
+
bw;
|
|
13077
|
+
/**
|
|
13078
|
+
* @access public
|
|
13079
|
+
* @param {Object} options
|
|
13080
|
+
* @param {String} options.network - desired network
|
|
13081
|
+
* @param {String} options.stars - star list for selected network (these should match, don't mix networks)
|
|
13082
|
+
* @param {String} options.root - path to root directory
|
|
13083
|
+
* @param {String} options.storePrefix - prefix for datatores (lfc)
|
|
13084
|
+
*
|
|
13085
|
+
* @return {Promise} instance of Peernet
|
|
13086
|
+
*
|
|
13087
|
+
* @example
|
|
13088
|
+
* const peernet = new Peernet({network: 'leofcoin', root: '.leofcoin'});
|
|
13089
|
+
*/
|
|
13090
|
+
constructor(options, password) {
|
|
13091
|
+
/**
|
|
13092
|
+
* @property {String} network - current network
|
|
13093
|
+
*/
|
|
13094
|
+
this.network = options.network || 'leofcoin';
|
|
13095
|
+
this.stars = options.stars;
|
|
13096
|
+
const parts = this.network.split(':');
|
|
13097
|
+
this.networkVersion = options.networkVersion || parts.length > 1 ? parts[1] : 'mainnet';
|
|
13098
|
+
if (!options.storePrefix)
|
|
13099
|
+
options.storePrefix = 'lfc';
|
|
13100
|
+
if (!options.port)
|
|
13101
|
+
options.port = 2000;
|
|
13102
|
+
if (!options.root) {
|
|
13103
|
+
parts[1] ? options.root = `.${parts[0]}/${parts[1]}` : options.root = `.${this.network}`;
|
|
13104
|
+
}
|
|
13105
|
+
globalThis.peernet = this;
|
|
13106
|
+
this.bw = {
|
|
13107
|
+
up: 0,
|
|
13108
|
+
down: 0,
|
|
13109
|
+
};
|
|
13110
|
+
return this._init(options, password);
|
|
13111
|
+
}
|
|
13112
|
+
get id() {
|
|
13113
|
+
return this.identity.id;
|
|
13114
|
+
}
|
|
13115
|
+
get accounts() {
|
|
13116
|
+
return this.identity.accounts;
|
|
13117
|
+
}
|
|
13118
|
+
get defaultStores() {
|
|
13119
|
+
return ['account', 'wallet', 'block', 'transaction', 'chain', 'data', 'message'];
|
|
13120
|
+
}
|
|
13121
|
+
addProto(name, proto) {
|
|
13122
|
+
if (!globalThis.peernet.protos[name])
|
|
13123
|
+
globalThis.peernet.protos[name] = proto;
|
|
13124
|
+
}
|
|
13125
|
+
addCodec(codec) {
|
|
13126
|
+
return utils$1.addCodec(codec);
|
|
13127
|
+
}
|
|
13128
|
+
async addStore(name, prefix, root, isPrivate = true) {
|
|
13129
|
+
if (name === 'block' || name === 'transaction' || name === 'chain' ||
|
|
13130
|
+
name === 'data' || name === 'message')
|
|
13131
|
+
isPrivate = false;
|
|
13132
|
+
let Storage;
|
|
13133
|
+
this.hasDaemon ? Storage = LeofcoinStorageClient : Storage = LeofcoinStorage;
|
|
13134
|
+
if (!globalThis[`${name}Store`]) {
|
|
13135
|
+
globalThis[`${name}Store`] = new Storage(name, root);
|
|
13136
|
+
await globalThis[`${name}Store`].init();
|
|
13137
|
+
}
|
|
13138
|
+
globalThis[`${name}Store`].private = isPrivate;
|
|
13139
|
+
if (!isPrivate)
|
|
13140
|
+
this.stores.push(name);
|
|
10592
13141
|
}
|
|
10593
13142
|
/**
|
|
10594
|
-
* @
|
|
13143
|
+
* @see MessageHandler
|
|
10595
13144
|
*/
|
|
10596
|
-
|
|
10597
|
-
return this.
|
|
13145
|
+
prepareMessage(data) {
|
|
13146
|
+
return this._messageHandler.prepareMessage(data);
|
|
10598
13147
|
}
|
|
10599
|
-
|
|
13148
|
+
/**
|
|
13149
|
+
* @access public
|
|
13150
|
+
*
|
|
13151
|
+
* @return {Array} peerId
|
|
13152
|
+
*/
|
|
13153
|
+
get peers() {
|
|
13154
|
+
return Object.keys(this.client.connections);
|
|
13155
|
+
}
|
|
13156
|
+
get connections() {
|
|
13157
|
+
return Object.values(this.client.connections);
|
|
13158
|
+
}
|
|
13159
|
+
get peerEntries() {
|
|
13160
|
+
return Object.entries(this.client.connections);
|
|
13161
|
+
}
|
|
13162
|
+
/**
|
|
13163
|
+
* @return {String} id - peerId
|
|
13164
|
+
*/
|
|
13165
|
+
getConnection(id) {
|
|
13166
|
+
return this.client.connections[id];
|
|
13167
|
+
}
|
|
13168
|
+
/**
|
|
13169
|
+
* @private
|
|
13170
|
+
*
|
|
13171
|
+
* @param {Object} options
|
|
13172
|
+
* @param {String} options.root - path to root directory
|
|
13173
|
+
*
|
|
13174
|
+
* @return {Promise} instance of Peernet
|
|
13175
|
+
*/
|
|
13176
|
+
async _init(options, password) {
|
|
13177
|
+
this.requestProtos = {};
|
|
13178
|
+
this.storePrefix = options.storePrefix;
|
|
13179
|
+
this.root = options.root;
|
|
13180
|
+
const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
|
|
13181
|
+
// FolderMessageResponse
|
|
13182
|
+
} = await import(/* webpackChunkName: "messages" */ './messages-2214fcbb.js');
|
|
13183
|
+
/**
|
|
13184
|
+
* proto Object containing protos
|
|
13185
|
+
* @type {Object}
|
|
13186
|
+
* @property {PeernetMessage} protos[peernet-message] messageNode
|
|
13187
|
+
* @property {DHTMessage} protos[peernet-dht] messageNode
|
|
13188
|
+
* @property {DHTMessageResponse} protos[peernet-dht-response] messageNode
|
|
13189
|
+
* @property {DataMessage} protos[peernet-data] messageNode
|
|
13190
|
+
* @property {DataMessageResponse} protos[peernet-data-response] messageNode
|
|
13191
|
+
*/
|
|
13192
|
+
globalThis.peernet.protos = {
|
|
13193
|
+
'peernet-request': RequestMessage,
|
|
13194
|
+
'peernet-response': ResponseMessage,
|
|
13195
|
+
'peernet-peer': PeerMessage,
|
|
13196
|
+
'peernet-peer-response': PeerMessageResponse,
|
|
13197
|
+
'peernet-message': PeernetMessage,
|
|
13198
|
+
'peernet-dht': DHTMessage,
|
|
13199
|
+
'peernet-dht-response': DHTMessageResponse,
|
|
13200
|
+
'peernet-data': DataMessage,
|
|
13201
|
+
'peernet-data-response': DataMessageResponse,
|
|
13202
|
+
'peernet-ps': PsMessage,
|
|
13203
|
+
'chat-message': ChatMessage,
|
|
13204
|
+
'peernet-file': PeernetFile
|
|
13205
|
+
};
|
|
13206
|
+
this._messageHandler = new MessageHandler(this.network);
|
|
13207
|
+
const { daemon, environment } = await target();
|
|
13208
|
+
this.hasDaemon = daemon;
|
|
13209
|
+
for (const store of this.defaultStores) {
|
|
13210
|
+
await this.addStore(store, options.storePrefix, options.root);
|
|
13211
|
+
}
|
|
13212
|
+
this.identity = new Identity(this.network);
|
|
13213
|
+
await this.identity.load(password);
|
|
13214
|
+
this._peerHandler = new PeerDiscovery(this.id);
|
|
13215
|
+
this.peerId = this.id;
|
|
13216
|
+
pubsub.subscribe('peer:connected', async (peer) => {
|
|
13217
|
+
// console.log(peer);
|
|
13218
|
+
// console.log({connected: peer.id, as: this._getPeerId(peer.id) });
|
|
13219
|
+
// peer.on('peernet.data', async (message) => {
|
|
13220
|
+
// const id = message.id
|
|
13221
|
+
// message = new PeernetMessage(Buffer.from(message.data.data))
|
|
13222
|
+
// const proto = protoFor(message.decoded.data)
|
|
13223
|
+
// this._protoHandler({id, proto}, peer)
|
|
13224
|
+
// })
|
|
13225
|
+
});
|
|
13226
|
+
/**
|
|
13227
|
+
* converts data -> message -> proto
|
|
13228
|
+
* @see DataHandler
|
|
13229
|
+
*/
|
|
13230
|
+
pubsub.subscribe('peer:data', dataHandler);
|
|
13231
|
+
const importee = await import('./client-95423644.js');
|
|
13232
|
+
/**
|
|
13233
|
+
* @access public
|
|
13234
|
+
* @type {PeernetClient}
|
|
13235
|
+
*/
|
|
13236
|
+
this.client = new importee.default(this.id, this.networkVersion, this.stars);
|
|
13237
|
+
if (globalThis.navigator) {
|
|
13238
|
+
globalThis.addEventListener('beforeunload', async () => this.client.close());
|
|
13239
|
+
}
|
|
13240
|
+
else {
|
|
13241
|
+
process.on('SIGTERM', async () => {
|
|
13242
|
+
process.stdin.resume();
|
|
13243
|
+
await this.client.close();
|
|
13244
|
+
process.exit();
|
|
13245
|
+
});
|
|
13246
|
+
}
|
|
13247
|
+
return this;
|
|
13248
|
+
}
|
|
13249
|
+
addRequestHandler(name, method) {
|
|
13250
|
+
this.requestProtos[name] = method;
|
|
13251
|
+
}
|
|
13252
|
+
sendMessage(peer, id, data) {
|
|
13253
|
+
if (peer.readyState === 'open') {
|
|
13254
|
+
peer.send(data, id);
|
|
13255
|
+
this.bw.up += data.length;
|
|
13256
|
+
}
|
|
13257
|
+
else if (peer.readyState === 'closed') ;
|
|
13258
|
+
}
|
|
13259
|
+
async handleDHT(peer, id, proto) {
|
|
13260
|
+
let { hash, store } = proto.decoded;
|
|
13261
|
+
let has;
|
|
13262
|
+
if (store) {
|
|
13263
|
+
store = globalThis[`${store}Store`];
|
|
13264
|
+
has = store.private ? false : await store.has(hash);
|
|
13265
|
+
}
|
|
13266
|
+
else {
|
|
13267
|
+
has = await this.has(hash);
|
|
13268
|
+
}
|
|
13269
|
+
const data = await new globalThis.peernet.protos['peernet-dht-response']({ hash, has });
|
|
13270
|
+
const node = await this.prepareMessage(data);
|
|
13271
|
+
this.sendMessage(peer, id, node.encoded);
|
|
13272
|
+
}
|
|
13273
|
+
async handleData(peer, id, proto) {
|
|
13274
|
+
let { hash, store } = proto.decoded;
|
|
13275
|
+
let data;
|
|
13276
|
+
store = globalThis[`${store}Store`] || await this.whichStore([...this.stores], hash);
|
|
13277
|
+
if (store && !store.private) {
|
|
13278
|
+
data = await store.get(hash);
|
|
13279
|
+
if (data) {
|
|
13280
|
+
data = await new globalThis.peernet.protos['peernet-data-response']({ hash, data });
|
|
13281
|
+
const node = await this.prepareMessage(data);
|
|
13282
|
+
this.sendMessage(peer, id, node.encoded);
|
|
13283
|
+
}
|
|
13284
|
+
}
|
|
13285
|
+
}
|
|
13286
|
+
async handleRequest(peer, id, proto) {
|
|
13287
|
+
const method = this.requestProtos[proto.decoded.request];
|
|
13288
|
+
if (method) {
|
|
13289
|
+
const data = await method();
|
|
13290
|
+
const node = await this.prepareMessage(data);
|
|
13291
|
+
this.sendMessage(peer, id, node.encoded);
|
|
13292
|
+
}
|
|
13293
|
+
}
|
|
13294
|
+
/**
|
|
13295
|
+
* @private
|
|
13296
|
+
*
|
|
13297
|
+
* @param {Buffer} message - peernet message
|
|
13298
|
+
* @param {PeernetPeer} peer - peernet peer
|
|
13299
|
+
*/
|
|
13300
|
+
async _protoHandler(message, peer, from) {
|
|
13301
|
+
const { id, proto } = message;
|
|
13302
|
+
this.bw.down += proto.encoded.length;
|
|
13303
|
+
switch (proto.name) {
|
|
13304
|
+
case 'peernet-dht': {
|
|
13305
|
+
this.handleDHT(peer, id, proto);
|
|
13306
|
+
break;
|
|
13307
|
+
}
|
|
13308
|
+
case 'peernet-data': {
|
|
13309
|
+
this.handleData(peer, id, proto);
|
|
13310
|
+
break;
|
|
13311
|
+
}
|
|
13312
|
+
case 'peernet-request': {
|
|
13313
|
+
this.handleRequest(peer, id, proto);
|
|
13314
|
+
}
|
|
13315
|
+
case 'peernet-ps': {
|
|
13316
|
+
if (peer.peerId !== this.id)
|
|
13317
|
+
globalSub.publish(proto.decoded.topic, proto.decoded.data);
|
|
13318
|
+
}
|
|
13319
|
+
}
|
|
13320
|
+
}
|
|
13321
|
+
/**
|
|
13322
|
+
* performs a walk and resolves first encounter
|
|
13323
|
+
*
|
|
13324
|
+
* @param {String} hash
|
|
13325
|
+
*/
|
|
13326
|
+
async walk(hash) {
|
|
13327
|
+
if (!hash)
|
|
13328
|
+
throw new Error('hash expected, received undefined');
|
|
13329
|
+
const data = await new globalThis.peernet.protos['peernet-dht']({ hash });
|
|
13330
|
+
this.client.id;
|
|
13331
|
+
const walk = async (peer) => {
|
|
13332
|
+
const node = await this.prepareMessage(data);
|
|
13333
|
+
let result = await peer.request(node.encoded);
|
|
13334
|
+
result = new Uint8Array(Object.values(result));
|
|
13335
|
+
const proto = await protoFor(result);
|
|
13336
|
+
if (proto.name !== 'peernet-dht-response')
|
|
13337
|
+
throw dhtError(proto.name);
|
|
13338
|
+
// TODO: give ip and port (just used for location)
|
|
13339
|
+
if (!peer.connection.remoteAddress || !peer.connection.localAddress) {
|
|
13340
|
+
peer.connection.remoteFamily = 'ipv4';
|
|
13341
|
+
peer.connection.remoteAddress = '127.0.0.1';
|
|
13342
|
+
peer.connection.remotePort = '0000';
|
|
13343
|
+
}
|
|
13344
|
+
const peerInfo = {
|
|
13345
|
+
family: peer.connection.remoteFamily || peer.connection.localFamily,
|
|
13346
|
+
address: peer.connection.remoteAddress || peer.connection.localAddress,
|
|
13347
|
+
port: peer.connection.remotePort || peer.connection.localPort,
|
|
13348
|
+
id: peer.peerId,
|
|
13349
|
+
};
|
|
13350
|
+
if (proto.decoded.has)
|
|
13351
|
+
this.dht.addProvider(peerInfo, proto.decoded.hash);
|
|
13352
|
+
};
|
|
13353
|
+
let walks = [];
|
|
13354
|
+
for (const peer of this.connections) {
|
|
13355
|
+
if (peer.peerId !== this.id) {
|
|
13356
|
+
walks.push(walk(peer));
|
|
13357
|
+
}
|
|
13358
|
+
}
|
|
13359
|
+
return Promise.all(walks);
|
|
13360
|
+
}
|
|
13361
|
+
/**
|
|
13362
|
+
* Override DHT behavior, try's finding the content three times
|
|
13363
|
+
*
|
|
13364
|
+
* @param {String} hash
|
|
13365
|
+
*/
|
|
13366
|
+
async providersFor(hash) {
|
|
13367
|
+
let providers = await this.dht.providersFor(hash);
|
|
13368
|
+
// walk the network to find a provider
|
|
13369
|
+
if (!providers || providers.length === 0) {
|
|
13370
|
+
await this.walk(hash);
|
|
13371
|
+
providers = await this.dht.providersFor(hash);
|
|
13372
|
+
// second walk the network to find a provider
|
|
13373
|
+
if (!providers || providers.length === 0) {
|
|
13374
|
+
await this.walk(hash);
|
|
13375
|
+
providers = await this.dht.providersFor(hash);
|
|
13376
|
+
}
|
|
13377
|
+
// last walk
|
|
13378
|
+
if (!providers || providers.length === 0) {
|
|
13379
|
+
await this.walk(hash);
|
|
13380
|
+
providers = await this.dht.providersFor(hash);
|
|
13381
|
+
}
|
|
13382
|
+
}
|
|
13383
|
+
// undefined if no providers given
|
|
13384
|
+
return providers;
|
|
13385
|
+
}
|
|
13386
|
+
get block() {
|
|
13387
|
+
return {
|
|
13388
|
+
get: async (hash) => {
|
|
13389
|
+
const data = await blockStore.has(hash);
|
|
13390
|
+
if (data)
|
|
13391
|
+
return blockStore.get(hash);
|
|
13392
|
+
return this.requestData(hash, 'block');
|
|
13393
|
+
},
|
|
13394
|
+
put: async (hash, data) => {
|
|
13395
|
+
if (await blockStore.has(hash))
|
|
13396
|
+
return;
|
|
13397
|
+
return await blockStore.put(hash, data);
|
|
13398
|
+
},
|
|
13399
|
+
has: async (hash) => await blockStore.has(hash, 'block'),
|
|
13400
|
+
};
|
|
13401
|
+
}
|
|
13402
|
+
get transaction() {
|
|
13403
|
+
return {
|
|
13404
|
+
get: async (hash) => {
|
|
13405
|
+
const data = await transactionStore.has(hash);
|
|
13406
|
+
if (data)
|
|
13407
|
+
return await transactionStore.get(hash);
|
|
13408
|
+
return this.requestData(hash, 'transaction');
|
|
13409
|
+
},
|
|
13410
|
+
put: async (hash, data) => {
|
|
13411
|
+
if (await transactionStore.has(hash))
|
|
13412
|
+
return;
|
|
13413
|
+
return await transactionStore.put(hash, data);
|
|
13414
|
+
},
|
|
13415
|
+
has: async (hash) => await transactionStore.has(hash),
|
|
13416
|
+
};
|
|
13417
|
+
}
|
|
13418
|
+
async requestData(hash, store) {
|
|
13419
|
+
const providers = await this.providersFor(hash);
|
|
13420
|
+
if (!providers || providers.size === 0)
|
|
13421
|
+
throw nothingFoundError(hash);
|
|
13422
|
+
debug(`found ${providers.size} provider(s) for ${hash}`);
|
|
13423
|
+
// get closest peer on earth
|
|
13424
|
+
const closestPeer = await this.dht.closestPeer(providers);
|
|
13425
|
+
// get peer instance by id
|
|
13426
|
+
if (!closestPeer || !closestPeer.id)
|
|
13427
|
+
return this.requestData(hash, store?.name || store);
|
|
13428
|
+
const id = closestPeer.id;
|
|
13429
|
+
if (this.connections) {
|
|
13430
|
+
let closest = this.connections.filter((peer) => {
|
|
13431
|
+
if (peer.peerId === id)
|
|
13432
|
+
return peer;
|
|
13433
|
+
});
|
|
13434
|
+
let data = await new globalThis.peernet.protos['peernet-data']({ hash, store: store?.name || store });
|
|
13435
|
+
const node = await this.prepareMessage(data);
|
|
13436
|
+
if (closest[0])
|
|
13437
|
+
data = await closest[0].request(node.encoded);
|
|
13438
|
+
else {
|
|
13439
|
+
closest = this.connections.filter((peer) => {
|
|
13440
|
+
if (peer.peerId === id)
|
|
13441
|
+
return peer;
|
|
13442
|
+
});
|
|
13443
|
+
if (closest[0])
|
|
13444
|
+
data = await closest[0].request(node.encoded);
|
|
13445
|
+
}
|
|
13446
|
+
const proto = await protoFor(data);
|
|
13447
|
+
// TODO: store data automaticly or not
|
|
13448
|
+
return BufferToUint8Array(proto.decoded.data);
|
|
13449
|
+
// this.put(hash, proto.decoded.data)
|
|
13450
|
+
}
|
|
13451
|
+
return;
|
|
13452
|
+
}
|
|
13453
|
+
get message() {
|
|
13454
|
+
return {
|
|
13455
|
+
/**
|
|
13456
|
+
* Get content for given message hash
|
|
13457
|
+
*
|
|
13458
|
+
* @param {String} hash
|
|
13459
|
+
*/
|
|
13460
|
+
get: async (hash) => {
|
|
13461
|
+
debug(`get message ${hash}`);
|
|
13462
|
+
const message = await messageStore.has(hash);
|
|
13463
|
+
if (message)
|
|
13464
|
+
return await messageStore.get(hash);
|
|
13465
|
+
return this.requestData(hash, 'message');
|
|
13466
|
+
},
|
|
13467
|
+
/**
|
|
13468
|
+
* put message content
|
|
13469
|
+
*
|
|
13470
|
+
* @param {String} hash
|
|
13471
|
+
* @param {Buffer} message
|
|
13472
|
+
*/
|
|
13473
|
+
put: async (hash, message) => await messageStore.put(hash, message),
|
|
13474
|
+
/**
|
|
13475
|
+
* @param {String} hash
|
|
13476
|
+
* @return {Boolean}
|
|
13477
|
+
*/
|
|
13478
|
+
has: async (hash) => await messageStore.has(hash),
|
|
13479
|
+
};
|
|
13480
|
+
}
|
|
13481
|
+
get data() {
|
|
13482
|
+
return {
|
|
13483
|
+
/**
|
|
13484
|
+
* Get content for given data hash
|
|
13485
|
+
*
|
|
13486
|
+
* @param {String} hash
|
|
13487
|
+
*/
|
|
13488
|
+
get: async (hash) => {
|
|
13489
|
+
debug(`get data ${hash}`);
|
|
13490
|
+
const data = await dataStore.has(hash);
|
|
13491
|
+
if (data)
|
|
13492
|
+
return await dataStore.get(hash);
|
|
13493
|
+
return this.requestData(hash, 'data');
|
|
13494
|
+
},
|
|
13495
|
+
/**
|
|
13496
|
+
* put data content
|
|
13497
|
+
*
|
|
13498
|
+
* @param {String} hash
|
|
13499
|
+
* @param {Buffer} data
|
|
13500
|
+
*/
|
|
13501
|
+
put: async (hash, data) => await dataStore.put(hash, data),
|
|
13502
|
+
/**
|
|
13503
|
+
* @param {String} hash
|
|
13504
|
+
* @return {Boolean}
|
|
13505
|
+
*/
|
|
13506
|
+
has: async (hash) => await dataStore.has(hash),
|
|
13507
|
+
};
|
|
13508
|
+
}
|
|
13509
|
+
get folder() {
|
|
13510
|
+
return {
|
|
13511
|
+
/**
|
|
13512
|
+
* Get content for given data hash
|
|
13513
|
+
*
|
|
13514
|
+
* @param {String} hash
|
|
13515
|
+
*/
|
|
13516
|
+
get: async (hash) => {
|
|
13517
|
+
debug(`get data ${hash}`);
|
|
13518
|
+
const data = await dataStore.has(hash);
|
|
13519
|
+
if (data)
|
|
13520
|
+
return await dataStore.get(hash);
|
|
13521
|
+
return this.requestData(hash, 'data');
|
|
13522
|
+
},
|
|
13523
|
+
/**
|
|
13524
|
+
* put data content
|
|
13525
|
+
*
|
|
13526
|
+
* @param {String} hash
|
|
13527
|
+
* @param {Buffer} data
|
|
13528
|
+
*/
|
|
13529
|
+
put: async (hash, data) => await dataStore.put(hash, data),
|
|
13530
|
+
/**
|
|
13531
|
+
* @param {String} hash
|
|
13532
|
+
* @return {Boolean}
|
|
13533
|
+
*/
|
|
13534
|
+
has: async (hash) => await dataStore.has(hash),
|
|
13535
|
+
};
|
|
13536
|
+
}
|
|
13537
|
+
async addFolder(files) {
|
|
13538
|
+
const links = [];
|
|
13539
|
+
for (const file of files) {
|
|
13540
|
+
const fileNode = await new globalThis.peernet.protos['peernet-file'](file);
|
|
13541
|
+
const hash = await fileNode.hash;
|
|
13542
|
+
await dataStore.put(hash, fileNode.encoded);
|
|
13543
|
+
links.push({ hash, path: file.path });
|
|
13544
|
+
}
|
|
13545
|
+
const node = await new globalThis.peernet.protos['peernet-file']({ path: '/', links });
|
|
13546
|
+
const hash = await node.hash;
|
|
13547
|
+
await dataStore.put(hash, node.encoded);
|
|
13548
|
+
return hash;
|
|
13549
|
+
}
|
|
13550
|
+
async ls(hash, options) {
|
|
13551
|
+
let data;
|
|
13552
|
+
const has = await dataStore.has(hash);
|
|
13553
|
+
data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data');
|
|
13554
|
+
const node = await new peernet.protos['peernet-file'](data);
|
|
13555
|
+
await node.decode();
|
|
13556
|
+
console.log(data);
|
|
13557
|
+
const paths = [];
|
|
13558
|
+
if (node.decoded?.links.length === 0)
|
|
13559
|
+
throw new Error(`${hash} is a file`);
|
|
13560
|
+
for (const { path, hash } of node.decoded.links) {
|
|
13561
|
+
paths.push({ path, hash });
|
|
13562
|
+
}
|
|
13563
|
+
if (options?.pin)
|
|
13564
|
+
await dataStore.put(hash, node.encoded);
|
|
13565
|
+
return paths;
|
|
13566
|
+
}
|
|
13567
|
+
async cat(hash, options) {
|
|
13568
|
+
let data;
|
|
13569
|
+
const has = await dataStore.has(hash);
|
|
13570
|
+
data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data');
|
|
13571
|
+
const node = await new peernet.protos['peernet-file'](data);
|
|
13572
|
+
if (node.decoded?.links.length > 0)
|
|
13573
|
+
throw new Error(`${hash} is a directory`);
|
|
13574
|
+
if (options?.pin)
|
|
13575
|
+
await dataStore.put(hash, node.encoded);
|
|
13576
|
+
return node.decoded.content;
|
|
13577
|
+
}
|
|
13578
|
+
/**
|
|
13579
|
+
* goes trough given stores and tries to find data for given hash
|
|
13580
|
+
* @param {Array} stores
|
|
13581
|
+
* @param {string} hash
|
|
13582
|
+
*/
|
|
13583
|
+
async whichStore(stores, hash) {
|
|
13584
|
+
let store = stores.pop();
|
|
13585
|
+
store = globalThis[`${store}Store`];
|
|
13586
|
+
if (store) {
|
|
13587
|
+
const has = await store.has(hash);
|
|
13588
|
+
if (has)
|
|
13589
|
+
return store;
|
|
13590
|
+
if (stores.length > 0)
|
|
13591
|
+
return this.whichStore(stores, hash);
|
|
13592
|
+
}
|
|
13593
|
+
else
|
|
13594
|
+
return;
|
|
13595
|
+
}
|
|
13596
|
+
/**
|
|
13597
|
+
* Get content for given hash
|
|
13598
|
+
*
|
|
13599
|
+
* @param {String} hash - the hash of the wanted data
|
|
13600
|
+
* @param {String} store - storeName to access
|
|
13601
|
+
*/
|
|
13602
|
+
async get(hash, store) {
|
|
13603
|
+
debug(`get ${hash}`);
|
|
13604
|
+
let data;
|
|
13605
|
+
if (store)
|
|
13606
|
+
store = globalThis[`${store}Store`];
|
|
13607
|
+
if (!store)
|
|
13608
|
+
store = await this.whichStore([...this.stores], hash);
|
|
13609
|
+
if (store && await store.has(hash))
|
|
13610
|
+
data = await store.get(hash);
|
|
13611
|
+
if (data)
|
|
13612
|
+
return data;
|
|
13613
|
+
return this.requestData(hash, store?.name || store);
|
|
13614
|
+
}
|
|
13615
|
+
/**
|
|
13616
|
+
* put content
|
|
13617
|
+
*
|
|
13618
|
+
* @param {String} hash
|
|
13619
|
+
* @param {Buffer} data
|
|
13620
|
+
* @param {String} store - storeName to access
|
|
13621
|
+
*/
|
|
13622
|
+
async put(hash, data, store = 'data') {
|
|
13623
|
+
store = globalThis[`${store}Store`];
|
|
13624
|
+
return store.put(hash, data);
|
|
13625
|
+
}
|
|
13626
|
+
/**
|
|
13627
|
+
* @param {String} hash
|
|
13628
|
+
* @return {Boolean}
|
|
13629
|
+
*/
|
|
13630
|
+
async has(hash) {
|
|
13631
|
+
const store = await this.whichStore([...this.stores], hash);
|
|
13632
|
+
if (store) {
|
|
13633
|
+
return store.private ? false : true;
|
|
13634
|
+
}
|
|
13635
|
+
return false;
|
|
13636
|
+
}
|
|
13637
|
+
/**
|
|
13638
|
+
*
|
|
13639
|
+
* @param {String} topic
|
|
13640
|
+
* @param {String|Object|Array|Boolean|Buffer} data
|
|
13641
|
+
*/
|
|
13642
|
+
async publish(topic, data) {
|
|
13643
|
+
// globalSub.publish(topic, data)
|
|
13644
|
+
const id = Math.random().toString(36).slice(-12);
|
|
13645
|
+
data = await new globalThis.peernet.protos['peernet-ps']({ data, topic });
|
|
13646
|
+
for (const peer of this.connections) {
|
|
13647
|
+
if (peer.peerId !== this.peerId) {
|
|
13648
|
+
const node = await this.prepareMessage(data);
|
|
13649
|
+
this.sendMessage(peer, id, node.encoded);
|
|
13650
|
+
}
|
|
13651
|
+
// TODO: if peer subscribed
|
|
13652
|
+
}
|
|
13653
|
+
}
|
|
13654
|
+
createHash(data, name) {
|
|
13655
|
+
return new CodeHash(data, { name });
|
|
13656
|
+
}
|
|
13657
|
+
/**
|
|
13658
|
+
*
|
|
13659
|
+
* @param {String} topic
|
|
13660
|
+
* @param {Method} cb
|
|
13661
|
+
*/
|
|
13662
|
+
async subscribe(topic, callback) {
|
|
13663
|
+
// TODO: if peer subscribed
|
|
13664
|
+
globalSub.subscribe(topic, callback);
|
|
13665
|
+
}
|
|
13666
|
+
async removePeer(peer) {
|
|
13667
|
+
return this.client.removePeer(peer);
|
|
13668
|
+
}
|
|
13669
|
+
get Buffer() {
|
|
13670
|
+
return Buffer;
|
|
13671
|
+
}
|
|
13672
|
+
}
|
|
13673
|
+
globalThis.Peernet = Peernet;
|
|
10600
13674
|
|
|
10601
|
-
export { MultiWallet as
|
|
13675
|
+
export { FormatInterface as F, LittlePubSub as L, MultiWallet as M, Peernet as P };
|