@leofcoin/chain 1.4.35 → 1.4.36
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-10ffabe1-10ffabe1.js +56 -0
- package/exports/browser/browser-e1cd4e67-e1cd4e67.js +198 -0
- package/exports/browser/browser-store.js +524 -0
- package/exports/browser/chain.js +12759 -0
- package/exports/browser/client-111c93a3-b0dcdc19.js +612 -0
- package/exports/browser/client-1d0234a7-df40059a.js +624 -0
- package/exports/browser/client-21cee984-77440b6b.js +624 -0
- package/exports/browser/contract-163f031f.js +1859 -0
- package/exports/browser/index-1ce30988-44319c80.js +37 -0
- package/exports/browser/index-3d3f56ca-064d16ce.js +37 -0
- package/exports/browser/index-543bc6a3-be806e11.js +37 -0
- package/exports/browser/messages-48deb2c3-73e8ace1.js +205 -0
- package/exports/browser/messages-af41e873-479a93a8.js +205 -0
- package/exports/browser/messages-e025829d-a0354268.js +205 -0
- package/exports/browser/node-browser-13cbe71a.js +16881 -0
- package/exports/browser/node-browser-63c7cb33.js +16877 -0
- package/exports/browser/node-browser-64b82fc8.js +16877 -0
- package/exports/browser/node-browser.js +2 -0
- package/exports/browser/pako.esm-aa674ebf-aa674ebf.js +6876 -0
- package/exports/browser/password-4357020e.js +3 -0
- package/exports/browser/qr-scanner-worker.min-c002e984-c002e984.js +100 -0
- package/exports/browser/simple-peer-743c19fe-03a0830e.js +7856 -0
- package/exports/node.js +2 -1
- package/exports/typings/node-browser.d.ts +10 -0
- package/package.json +9 -5
|
@@ -0,0 +1,1859 @@
|
|
|
1
|
+
// base-x encoding / decoding
|
|
2
|
+
// Copyright (c) 2018 base-x contributors
|
|
3
|
+
// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
|
|
4
|
+
// Distributed under the MIT software license, see the accompanying
|
|
5
|
+
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
|
6
|
+
const base = (ALPHABET) => {
|
|
7
|
+
if (ALPHABET.length >= 255) {
|
|
8
|
+
throw new TypeError('Alphabet too long');
|
|
9
|
+
}
|
|
10
|
+
const BASE_MAP = new Uint8Array(256);
|
|
11
|
+
for (let j = 0; j < BASE_MAP.length; j++) {
|
|
12
|
+
BASE_MAP[j] = 255;
|
|
13
|
+
}
|
|
14
|
+
for (let i = 0; i < ALPHABET.length; i++) {
|
|
15
|
+
const x = ALPHABET.charAt(i);
|
|
16
|
+
const xc = x.charCodeAt(0);
|
|
17
|
+
if (BASE_MAP[xc] !== 255) {
|
|
18
|
+
throw new TypeError(x + ' is ambiguous');
|
|
19
|
+
}
|
|
20
|
+
BASE_MAP[xc] = i;
|
|
21
|
+
}
|
|
22
|
+
const BASE = ALPHABET.length;
|
|
23
|
+
const LEADER = ALPHABET.charAt(0);
|
|
24
|
+
const FACTOR = Math.log(BASE) / Math.log(256); // log(BASE) / log(256), rounded up
|
|
25
|
+
const iFACTOR = Math.log(256) / Math.log(BASE); // log(256) / log(BASE), rounded up
|
|
26
|
+
const encode = (source) => {
|
|
27
|
+
if (source instanceof Uint8Array) ;
|
|
28
|
+
else if (ArrayBuffer.isView(source)) {
|
|
29
|
+
source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
30
|
+
}
|
|
31
|
+
else if (Array.isArray(source)) {
|
|
32
|
+
source = Uint8Array.from(source);
|
|
33
|
+
}
|
|
34
|
+
if (!(source instanceof Uint8Array)) {
|
|
35
|
+
throw new TypeError('Expected Uint8Array');
|
|
36
|
+
}
|
|
37
|
+
if (source.length === 0) {
|
|
38
|
+
return '';
|
|
39
|
+
}
|
|
40
|
+
// Skip & count leading zeroes.
|
|
41
|
+
let zeroes = 0;
|
|
42
|
+
let length = 0;
|
|
43
|
+
let pbegin = 0;
|
|
44
|
+
const pend = source.length;
|
|
45
|
+
while (pbegin !== pend && source[pbegin] === 0) {
|
|
46
|
+
pbegin++;
|
|
47
|
+
zeroes++;
|
|
48
|
+
}
|
|
49
|
+
// Allocate enough space in big-endian base58 representation.
|
|
50
|
+
const size = ((pend - pbegin) * iFACTOR + 1) >>> 0;
|
|
51
|
+
const b58 = new Uint8Array(size);
|
|
52
|
+
// Process the bytes.
|
|
53
|
+
while (pbegin !== pend) {
|
|
54
|
+
let carry = source[pbegin];
|
|
55
|
+
// Apply "b58 = b58 * 256 + ch".
|
|
56
|
+
let i = 0;
|
|
57
|
+
for (let it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
|
|
58
|
+
carry += (256 * b58[it1]) >>> 0;
|
|
59
|
+
b58[it1] = (carry % BASE) >>> 0;
|
|
60
|
+
carry = (carry / BASE) >>> 0;
|
|
61
|
+
}
|
|
62
|
+
if (carry !== 0) {
|
|
63
|
+
throw new Error('Non-zero carry');
|
|
64
|
+
}
|
|
65
|
+
length = i;
|
|
66
|
+
pbegin++;
|
|
67
|
+
}
|
|
68
|
+
// Skip leading zeroes in base58 result.
|
|
69
|
+
let it2 = size - length;
|
|
70
|
+
while (it2 !== size && b58[it2] === 0) {
|
|
71
|
+
it2++;
|
|
72
|
+
}
|
|
73
|
+
// Translate the result into a string.
|
|
74
|
+
let str = LEADER.repeat(zeroes);
|
|
75
|
+
for (; it2 < size; ++it2) {
|
|
76
|
+
str += ALPHABET.charAt(b58[it2]);
|
|
77
|
+
}
|
|
78
|
+
return str;
|
|
79
|
+
};
|
|
80
|
+
const decodeUnsafe = (source) => {
|
|
81
|
+
if (typeof source !== 'string') {
|
|
82
|
+
throw new TypeError('Expected String');
|
|
83
|
+
}
|
|
84
|
+
if (source.length === 0) {
|
|
85
|
+
return new Uint8Array();
|
|
86
|
+
}
|
|
87
|
+
let psz = 0;
|
|
88
|
+
// Skip and count leading '1's.
|
|
89
|
+
let zeroes = 0;
|
|
90
|
+
let length = 0;
|
|
91
|
+
while (source[psz] === LEADER) {
|
|
92
|
+
zeroes++;
|
|
93
|
+
psz++;
|
|
94
|
+
}
|
|
95
|
+
// Allocate enough space in big-endian base256 representation.
|
|
96
|
+
const size = (((source.length - psz) * FACTOR) + 1) >>> 0; // log(58) / log(256), rounded up.
|
|
97
|
+
let b256 = new Uint8Array(size);
|
|
98
|
+
// Process the characters.
|
|
99
|
+
while (source[psz]) {
|
|
100
|
+
// Decode character
|
|
101
|
+
let carry = BASE_MAP[source.charCodeAt(psz)];
|
|
102
|
+
// Invalid character
|
|
103
|
+
if (carry === 255) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
let i = 0;
|
|
107
|
+
for (let it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
|
|
108
|
+
carry += (BASE * b256[it3]) >>> 0;
|
|
109
|
+
b256[it3] = (carry % 256) >>> 0;
|
|
110
|
+
carry = (carry / 256) >>> 0;
|
|
111
|
+
}
|
|
112
|
+
if (carry !== 0) {
|
|
113
|
+
throw new Error('Non-zero carry');
|
|
114
|
+
}
|
|
115
|
+
length = i;
|
|
116
|
+
psz++;
|
|
117
|
+
}
|
|
118
|
+
// Skip leading zeroes in b256.
|
|
119
|
+
let it4 = size - length;
|
|
120
|
+
while (it4 !== size && b256[it4] === 0) {
|
|
121
|
+
it4++;
|
|
122
|
+
}
|
|
123
|
+
let vch = new Uint8Array(zeroes + (size - it4));
|
|
124
|
+
let j = zeroes;
|
|
125
|
+
while (it4 !== size) {
|
|
126
|
+
vch[j++] = b256[it4++];
|
|
127
|
+
}
|
|
128
|
+
return vch;
|
|
129
|
+
};
|
|
130
|
+
const decode = (string) => {
|
|
131
|
+
const buffer = decodeUnsafe(string);
|
|
132
|
+
if (buffer) {
|
|
133
|
+
return buffer;
|
|
134
|
+
}
|
|
135
|
+
throw new Error('Non-base' + BASE + ' character');
|
|
136
|
+
};
|
|
137
|
+
return {
|
|
138
|
+
encode,
|
|
139
|
+
decodeUnsafe,
|
|
140
|
+
decode
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const ALPHABET$3 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
|
|
145
|
+
const ALPHABET_HEX$1 = '0123456789ABCDEFGHIJKLMNOPQRSTUV';
|
|
146
|
+
const base32 = base(ALPHABET$3);
|
|
147
|
+
const base32Hex = base(ALPHABET_HEX$1);
|
|
148
|
+
const decode$5 = base32.decode;
|
|
149
|
+
const decodeHex$1 = base32Hex.decode;
|
|
150
|
+
const encode$5 = base32.encode;
|
|
151
|
+
const encodeHex$1 = base32Hex.encode;
|
|
152
|
+
const isBase32 = (string, hex = false) => {
|
|
153
|
+
try {
|
|
154
|
+
if (hex)
|
|
155
|
+
decodeHex$1(string);
|
|
156
|
+
else
|
|
157
|
+
decode$5(string);
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const isBase32Hex = (string) => {
|
|
165
|
+
return isBase32(string, true);
|
|
166
|
+
};
|
|
167
|
+
var index$7 = {
|
|
168
|
+
encode: encode$5,
|
|
169
|
+
decode: decode$5,
|
|
170
|
+
encodeHex: encodeHex$1,
|
|
171
|
+
decodeHex: decodeHex$1,
|
|
172
|
+
isBase32,
|
|
173
|
+
isBase32Hex
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
var ALPHABET$2 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
177
|
+
var ALPHABET_HEX = '0123456789ABCDEFGHJKLMNPQRSTUVabcdefghijklmnopqrstuv';
|
|
178
|
+
var base58 = base(ALPHABET$2);
|
|
179
|
+
var base58Hex = base(ALPHABET_HEX);
|
|
180
|
+
var encode$4 = base58.encode;
|
|
181
|
+
var decode$4 = base58.decode;
|
|
182
|
+
var encodeHex = base58Hex.encode;
|
|
183
|
+
var decodeHex = base58Hex.decode;
|
|
184
|
+
var isBase58 = function (string) {
|
|
185
|
+
try {
|
|
186
|
+
decode$4(string);
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
catch (e) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
var isBase58Hex = function (string) {
|
|
194
|
+
try {
|
|
195
|
+
decodeHex(string);
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
catch (e) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
var whatType = function (string) {
|
|
203
|
+
try {
|
|
204
|
+
decode$4(string);
|
|
205
|
+
return 'base58';
|
|
206
|
+
}
|
|
207
|
+
catch (e) {
|
|
208
|
+
try {
|
|
209
|
+
decodeHex(string);
|
|
210
|
+
return 'base58Hex';
|
|
211
|
+
}
|
|
212
|
+
catch (_a) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
var base58$1 = { encode: encode$4, decode: decode$4, isBase58: isBase58, isBase58Hex: isBase58Hex, encodeHex: encodeHex, decodeHex: decodeHex, whatType: whatType };
|
|
218
|
+
|
|
219
|
+
const MSB$1 = 0x80;
|
|
220
|
+
const REST$1 = 0x7F;
|
|
221
|
+
const MSBALL = ~REST$1;
|
|
222
|
+
const INT = Math.pow(2, 31);
|
|
223
|
+
const encode$3 = (num, out, offset) => {
|
|
224
|
+
if (Number.MAX_SAFE_INTEGER && num > Number.MAX_SAFE_INTEGER) {
|
|
225
|
+
encode$3.bytes = 0;
|
|
226
|
+
throw new RangeError('Could not encode varint');
|
|
227
|
+
}
|
|
228
|
+
out = out || [];
|
|
229
|
+
offset = offset || 0;
|
|
230
|
+
const oldOffset = offset;
|
|
231
|
+
while (num >= INT) {
|
|
232
|
+
out[offset++] = (num & 0xFF) | MSB$1;
|
|
233
|
+
num /= 128;
|
|
234
|
+
}
|
|
235
|
+
while (num & MSBALL) {
|
|
236
|
+
out[offset++] = (num & 0xFF) | MSB$1;
|
|
237
|
+
num >>>= 7;
|
|
238
|
+
}
|
|
239
|
+
out[offset] = num | 0;
|
|
240
|
+
encode$3.bytes = offset - oldOffset + 1;
|
|
241
|
+
return out;
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const MSB = 0x80;
|
|
245
|
+
const REST = 0x7F;
|
|
246
|
+
const decode$3 = (buf, offset) => {
|
|
247
|
+
offset = offset || 0;
|
|
248
|
+
const l = buf.length;
|
|
249
|
+
let counter = offset;
|
|
250
|
+
let result = 0;
|
|
251
|
+
let shift = 0;
|
|
252
|
+
let b;
|
|
253
|
+
do {
|
|
254
|
+
if (counter >= l || shift > 49) {
|
|
255
|
+
decode$3.bytes = 0;
|
|
256
|
+
throw new RangeError('Could not decode varint');
|
|
257
|
+
}
|
|
258
|
+
b = buf[counter++];
|
|
259
|
+
result += shift < 28
|
|
260
|
+
? (b & REST) << shift
|
|
261
|
+
: (b & REST) * Math.pow(2, shift);
|
|
262
|
+
shift += 7;
|
|
263
|
+
} while (b >= MSB);
|
|
264
|
+
decode$3.bytes = counter - offset;
|
|
265
|
+
return result;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
const N1 = Math.pow(2, 7);
|
|
269
|
+
const N2 = Math.pow(2, 14);
|
|
270
|
+
const N3 = Math.pow(2, 21);
|
|
271
|
+
const N4 = Math.pow(2, 28);
|
|
272
|
+
const N5 = Math.pow(2, 35);
|
|
273
|
+
const N6 = Math.pow(2, 42);
|
|
274
|
+
const N7 = Math.pow(2, 49);
|
|
275
|
+
const N8 = Math.pow(2, 56);
|
|
276
|
+
const N9 = Math.pow(2, 63);
|
|
277
|
+
var encodingLength = (value) => (value < N1 ? 1
|
|
278
|
+
: value < N2 ? 2
|
|
279
|
+
: value < N3 ? 3
|
|
280
|
+
: value < N4 ? 4
|
|
281
|
+
: value < N5 ? 5
|
|
282
|
+
: value < N6 ? 6
|
|
283
|
+
: value < N7 ? 7
|
|
284
|
+
: value < N8 ? 8
|
|
285
|
+
: value < N9 ? 9
|
|
286
|
+
: 10);
|
|
287
|
+
|
|
288
|
+
var index$6 = {
|
|
289
|
+
encode: encode$3,
|
|
290
|
+
decode: decode$3,
|
|
291
|
+
encodingLength
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
var index$5 = (input, prefix) => {
|
|
295
|
+
const encodedArray = [];
|
|
296
|
+
const length = input.reduce((total, current) => {
|
|
297
|
+
const encoded = index$6.encode(current.length);
|
|
298
|
+
encodedArray.push(encoded);
|
|
299
|
+
total += current.length + encoded.length;
|
|
300
|
+
return total;
|
|
301
|
+
}, 0);
|
|
302
|
+
const typedArray = new Uint8Array(prefix ? prefix.length + length : length);
|
|
303
|
+
let currentIndex = 0;
|
|
304
|
+
let index = 0;
|
|
305
|
+
if (prefix) {
|
|
306
|
+
typedArray.set(prefix);
|
|
307
|
+
currentIndex += prefix.length;
|
|
308
|
+
}
|
|
309
|
+
for (const source of input) {
|
|
310
|
+
typedArray.set(encodedArray[index], currentIndex);
|
|
311
|
+
currentIndex += encodedArray[index].length;
|
|
312
|
+
typedArray.set(source, currentIndex);
|
|
313
|
+
currentIndex += source.length;
|
|
314
|
+
index += 1;
|
|
315
|
+
}
|
|
316
|
+
return typedArray;
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
var index$4 = (typedArray, prefix) => {
|
|
320
|
+
const set = [];
|
|
321
|
+
if (prefix)
|
|
322
|
+
typedArray = typedArray.subarray(prefix.length);
|
|
323
|
+
const varintAndSub = (typedArray) => {
|
|
324
|
+
const length = index$6.decode(typedArray);
|
|
325
|
+
// remove length
|
|
326
|
+
typedArray = typedArray.subarray(index$6.decode.bytes);
|
|
327
|
+
// push value
|
|
328
|
+
set.push(typedArray.subarray(0, length));
|
|
329
|
+
// remove value
|
|
330
|
+
typedArray = typedArray.subarray(length);
|
|
331
|
+
if (typedArray.length !== 0)
|
|
332
|
+
return varintAndSub(typedArray);
|
|
333
|
+
return set;
|
|
334
|
+
};
|
|
335
|
+
return varintAndSub(typedArray);
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const ALPHABET$1 = '0123456789ABCDEF';
|
|
339
|
+
const base16 = base(ALPHABET$1);
|
|
340
|
+
const decode$2 = base16.decode;
|
|
341
|
+
const encode$2 = base16.encode;
|
|
342
|
+
const isBase16 = (string) => {
|
|
343
|
+
try {
|
|
344
|
+
decode$2(string);
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
347
|
+
catch (e) {
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
var index$3 = {
|
|
352
|
+
encode: encode$2,
|
|
353
|
+
decode: decode$2,
|
|
354
|
+
isBase16
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
358
|
+
const base64 = base(ALPHABET);
|
|
359
|
+
const decode$1 = base64.decode;
|
|
360
|
+
const encode$1 = base64.encode;
|
|
361
|
+
const isBase64 = (string) => {
|
|
362
|
+
try {
|
|
363
|
+
decode$1(string);
|
|
364
|
+
return true;
|
|
365
|
+
}
|
|
366
|
+
catch (e) {
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
var index$2 = {
|
|
371
|
+
encode: encode$1,
|
|
372
|
+
decode: decode$1,
|
|
373
|
+
isBase64
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
const isTypedArrayCompatible = (possibleUint8Array) => {
|
|
377
|
+
if (typeof possibleUint8Array === 'string') {
|
|
378
|
+
possibleUint8Array = possibleUint8Array.split(',').map(number => Number(number));
|
|
379
|
+
for (const number of possibleUint8Array) {
|
|
380
|
+
if (isNaN(number))
|
|
381
|
+
return false;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
for (const number of possibleUint8Array) {
|
|
385
|
+
if (isNaN(number))
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
return true;
|
|
389
|
+
};
|
|
390
|
+
/**
|
|
391
|
+
* Returns a String as Uint8Array
|
|
392
|
+
* @param string string to encode to Uint8Array
|
|
393
|
+
* @returns Uint8Array
|
|
394
|
+
*/
|
|
395
|
+
const fromString$1 = (string) => new TextEncoder().encode(string);
|
|
396
|
+
/**
|
|
397
|
+
* Returns a Uint8Array as String
|
|
398
|
+
* @param uint8Array Uint8Array to encode to String
|
|
399
|
+
* @returns String
|
|
400
|
+
*/
|
|
401
|
+
const toString$1 = (uint8Array) => new TextDecoder().decode(uint8Array);
|
|
402
|
+
/**
|
|
403
|
+
* Returns a String as Uint8Array
|
|
404
|
+
* @param string string to encode to Uint8Array
|
|
405
|
+
* @returns Uint8Array
|
|
406
|
+
*/
|
|
407
|
+
const fromUintArrayString = (string) => Uint8Array.from(string.split(',').map(string => Number(string)));
|
|
408
|
+
/**
|
|
409
|
+
* Returns a Uint8Array as String
|
|
410
|
+
* @param uint8Array Uint8Array to encode to String
|
|
411
|
+
* @returns String
|
|
412
|
+
*/
|
|
413
|
+
const toUintArrayString = (uint8Array) => uint8Array.toString();
|
|
414
|
+
/**
|
|
415
|
+
* hexString -> uint8Array
|
|
416
|
+
* @param string hex encoded string
|
|
417
|
+
* @returns UintArray
|
|
418
|
+
*/
|
|
419
|
+
const fromHex = (string) => Uint8Array.from(string.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
|
420
|
+
/**
|
|
421
|
+
* uint8Array -> hexString
|
|
422
|
+
* @param bytes number[]
|
|
423
|
+
* @returns hexString
|
|
424
|
+
*/
|
|
425
|
+
const toHex = (bytes) => bytes.reduce((string, byte) => string + byte.toString(16).padStart(2, '0'), '');
|
|
426
|
+
/**
|
|
427
|
+
* number[] -> Uint8Array
|
|
428
|
+
* @param array number[]
|
|
429
|
+
* @returns Uint8Array
|
|
430
|
+
*/
|
|
431
|
+
const fromArrayLike = (array) => Uint8Array.from(array);
|
|
432
|
+
/**
|
|
433
|
+
* Uint8Array -> number[]
|
|
434
|
+
* @param uint8Array Uint8Array
|
|
435
|
+
* @returns Uint8Array
|
|
436
|
+
*/
|
|
437
|
+
const toArrayLike = (uint8Array) => [...uint8Array.values()];
|
|
438
|
+
const toBase64 = (uint8Array) => index$2.encode(uint8Array);
|
|
439
|
+
const fromBase64 = (string) => index$2.decode(string);
|
|
440
|
+
const toBase58 = (uint8Array) => base58$1.encode(uint8Array);
|
|
441
|
+
const fromBase58 = (string) => base58$1.decode(string);
|
|
442
|
+
const toBase32 = (uint8Array) => index$7.encode(uint8Array);
|
|
443
|
+
const fromBase32 = (string) => index$7.decode(string);
|
|
444
|
+
const toBase16 = (uint8Array) => index$3.encode(uint8Array);
|
|
445
|
+
const fromBase16 = (string) => index$3.decode(string);
|
|
446
|
+
let FormatInterface$2 = class FormatInterface {
|
|
447
|
+
encoded;
|
|
448
|
+
constructor(input) {
|
|
449
|
+
if (input) {
|
|
450
|
+
if (index$3.isBase16(input))
|
|
451
|
+
this.encoded = this.fromBase16(input);
|
|
452
|
+
else if (index$7.isBase32(input))
|
|
453
|
+
this.encoded = this.fromBase32(input);
|
|
454
|
+
else if (base58$1.isBase58(input))
|
|
455
|
+
this.encoded = this.fromBase58(input);
|
|
456
|
+
else if (index$2.isBase64(input))
|
|
457
|
+
this.encoded = this.fromBase64(input);
|
|
458
|
+
else if (typeof input === 'string') {
|
|
459
|
+
let isCompatible = isTypedArrayCompatible(input);
|
|
460
|
+
if (isCompatible)
|
|
461
|
+
this.encoded = fromUintArrayString(input);
|
|
462
|
+
else
|
|
463
|
+
this.encoded = this.fromString(input); // normal string
|
|
464
|
+
}
|
|
465
|
+
else if (typeof input === 'object')
|
|
466
|
+
this.encoded = this.fromObject(input);
|
|
467
|
+
else if (input instanceof Uint8Array)
|
|
468
|
+
this.encoded = input;
|
|
469
|
+
else if (Array.isArray(input) && isTypedArrayCompatible(input))
|
|
470
|
+
this.encoded = this.fromArrayLike(input);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Returns a String as Uint8Array
|
|
475
|
+
* @param string string to encode to Uint8Array
|
|
476
|
+
* @returns Uint8Array
|
|
477
|
+
*/
|
|
478
|
+
fromString(string) {
|
|
479
|
+
return new TextEncoder().encode(string);
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Returns a Uint8Array as String
|
|
483
|
+
* @param uint8Array Uint8Array to encode to String
|
|
484
|
+
* @returns String
|
|
485
|
+
*/
|
|
486
|
+
toString(uint8Array) {
|
|
487
|
+
return new TextDecoder().decode(uint8Array);
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Returns a String as Uint8Array
|
|
491
|
+
* @param string string to encode to Uint8Array
|
|
492
|
+
* @returns Uint8Array
|
|
493
|
+
*/
|
|
494
|
+
fromUintArrayString(string) {
|
|
495
|
+
return Uint8Array.from(string.split(',').map(string => Number(string)));
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Returns a Uint8Array as String
|
|
499
|
+
* @param uint8Array Uint8Array to encode to String
|
|
500
|
+
* @returns String
|
|
501
|
+
*/
|
|
502
|
+
toUintArrayString(uint8Array) {
|
|
503
|
+
return uint8Array.toString();
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* hexString -> uint8Array
|
|
507
|
+
* @param string hex encoded string
|
|
508
|
+
* @returns UintArray
|
|
509
|
+
*/
|
|
510
|
+
fromHex(string) {
|
|
511
|
+
return Uint8Array.from(string.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* uint8Array -> hexString
|
|
515
|
+
* @param bytes number[]
|
|
516
|
+
* @returns hexString
|
|
517
|
+
*/
|
|
518
|
+
toHex(bytes) {
|
|
519
|
+
return bytes.reduce((string, byte) => string + byte.toString(16).padStart(2, '0'), '');
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* number[] -> Uint8Array
|
|
523
|
+
* @param array number[]
|
|
524
|
+
* @returns Uint8Array
|
|
525
|
+
*/
|
|
526
|
+
fromArrayLike(array) {
|
|
527
|
+
return Uint8Array.from(array);
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Uint8Array -> number[]
|
|
531
|
+
* @param uint8Array Uint8Array
|
|
532
|
+
* @returns Uint8Array
|
|
533
|
+
*/
|
|
534
|
+
toArrayLike(uint8Array) {
|
|
535
|
+
return [...uint8Array.values()];
|
|
536
|
+
}
|
|
537
|
+
fromObject(object) {
|
|
538
|
+
return new TextEncoder().encode(JSON.stringify(object));
|
|
539
|
+
}
|
|
540
|
+
toObject(uint8Array) {
|
|
541
|
+
return JSON.parse(new TextDecoder().decode(uint8Array));
|
|
542
|
+
}
|
|
543
|
+
toBase64(uint8Array) {
|
|
544
|
+
return index$2.encode(uint8Array);
|
|
545
|
+
}
|
|
546
|
+
fromBase64(string) {
|
|
547
|
+
return index$2.decode(string);
|
|
548
|
+
}
|
|
549
|
+
toBase58(uint8Array) {
|
|
550
|
+
return base58$1.encode(uint8Array);
|
|
551
|
+
}
|
|
552
|
+
fromBase58(string) {
|
|
553
|
+
return base58$1.decode(string);
|
|
554
|
+
}
|
|
555
|
+
toBase32(uint8Array) {
|
|
556
|
+
return index$7.encode(uint8Array);
|
|
557
|
+
}
|
|
558
|
+
fromBase32(string) {
|
|
559
|
+
return index$7.decode(string);
|
|
560
|
+
}
|
|
561
|
+
toBase16(uint8Array) {
|
|
562
|
+
return index$3.encode(uint8Array);
|
|
563
|
+
}
|
|
564
|
+
fromBase16(string) {
|
|
565
|
+
return index$3.decode(string);
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
var index$1 = {
|
|
569
|
+
fromString: fromString$1,
|
|
570
|
+
toString: toString$1,
|
|
571
|
+
fromHex,
|
|
572
|
+
toHex,
|
|
573
|
+
fromArrayLike,
|
|
574
|
+
toArrayLike,
|
|
575
|
+
fromUintArrayString,
|
|
576
|
+
toUintArrayString,
|
|
577
|
+
toBase64,
|
|
578
|
+
fromBase64,
|
|
579
|
+
toBase58,
|
|
580
|
+
fromBase58,
|
|
581
|
+
toBase32,
|
|
582
|
+
fromBase32,
|
|
583
|
+
toBase16,
|
|
584
|
+
fromBase16,
|
|
585
|
+
FormatInterface: FormatInterface$2
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
const { fromString, toString } = index$1;
|
|
589
|
+
const isJson = (type) => type === 'object' || 'array';
|
|
590
|
+
const isString = (type) => type === 'string';
|
|
591
|
+
const isNumber = (type) => type === 'number';
|
|
592
|
+
const isBoolean = (type) => type === 'boolean';
|
|
593
|
+
const isUint8Array = (type) => type === 'uint8Array';
|
|
594
|
+
const tokenize = (key, value) => {
|
|
595
|
+
const optional = key.endsWith('?');
|
|
596
|
+
let type = value;
|
|
597
|
+
type = Array.isArray(type) ? 'array' : typeof type;
|
|
598
|
+
if (value instanceof Uint8Array)
|
|
599
|
+
type = 'uint8Array';
|
|
600
|
+
const parts = key.split('?');
|
|
601
|
+
const minimumLength = parts[2]?.includes('min') ? parts[2].split['min:'][1] : 0;
|
|
602
|
+
return { type, optional, key: parts[0], minimumLength };
|
|
603
|
+
};
|
|
604
|
+
const toType = (data) => {
|
|
605
|
+
// always return uint8Arrays as they are
|
|
606
|
+
if (data instanceof Uint8Array)
|
|
607
|
+
return data;
|
|
608
|
+
// returns the ArrayBuffer as a UintArray
|
|
609
|
+
if (data instanceof ArrayBuffer)
|
|
610
|
+
return new Uint8Array(data);
|
|
611
|
+
// returns the string as a UintArray
|
|
612
|
+
if (typeof data === 'string')
|
|
613
|
+
return new TextEncoder().encode(data);
|
|
614
|
+
// returns the object as a UintArray
|
|
615
|
+
if (typeof data === 'object')
|
|
616
|
+
return new TextEncoder().encode(JSON.stringify(data));
|
|
617
|
+
// returns the number as a UintArray
|
|
618
|
+
if (typeof data === 'number' || typeof data === 'boolean')
|
|
619
|
+
return new TextEncoder().encode(data.toString());
|
|
620
|
+
throw new Error(`unsuported type ${typeof data || data}`);
|
|
621
|
+
};
|
|
622
|
+
const encode = (proto, input) => {
|
|
623
|
+
const keys = Object.keys(proto);
|
|
624
|
+
const values = Object.values(proto);
|
|
625
|
+
const set = [];
|
|
626
|
+
for (let i = 0; i < keys.length; i++) {
|
|
627
|
+
const token = tokenize(keys[i], values[i]);
|
|
628
|
+
const data = input[token.key];
|
|
629
|
+
if (!token.optional && data === undefined)
|
|
630
|
+
throw new Error(`missing required property: ${token.key}`);
|
|
631
|
+
if (token.type === 'array' && token.minimumLength > data.length || token.type === 'object' && token.minimumLength > Object.keys(data).length)
|
|
632
|
+
throw new Error(`minimumLength for ${token.key} is set to ${token.minimumLength} but got ${data.length}`);
|
|
633
|
+
// always push data to the set.
|
|
634
|
+
// when data is undefined push the default value of the proto
|
|
635
|
+
set.push(toType(data || values[i]));
|
|
636
|
+
}
|
|
637
|
+
return index$5(set);
|
|
638
|
+
};
|
|
639
|
+
const decode = (proto, uint8Array) => {
|
|
640
|
+
let deconcated = index$4(uint8Array);
|
|
641
|
+
const output = {};
|
|
642
|
+
const keys = Object.keys(proto);
|
|
643
|
+
const values = Object.values(proto);
|
|
644
|
+
if (keys.length !== deconcated.length)
|
|
645
|
+
console.warn(`length mismatch: expected ${keys.length} got ${uint8Array.length}`);
|
|
646
|
+
for (let i = 0; i < keys.length; i++) {
|
|
647
|
+
const token = tokenize(keys[i], values[i]);
|
|
648
|
+
if (isUint8Array(token.type))
|
|
649
|
+
output[token.key] = deconcated[i];
|
|
650
|
+
else if (isString(token.type))
|
|
651
|
+
output[token.key] = toString(deconcated[i]);
|
|
652
|
+
else if (isBoolean(token.type))
|
|
653
|
+
output[token.key] = Boolean(new TextDecoder().decode(deconcated[i]));
|
|
654
|
+
else if (isNumber(token.type))
|
|
655
|
+
output[token.key] = Number(new TextDecoder().decode(deconcated[i]));
|
|
656
|
+
else if (isJson(token.type))
|
|
657
|
+
output[token.key] = JSON.parse(new TextDecoder().decode(deconcated[i]));
|
|
658
|
+
if (token.optional) {
|
|
659
|
+
if (!output[token.key] || output[token.key].length === 0)
|
|
660
|
+
delete output[token.key];
|
|
661
|
+
}
|
|
662
|
+
if (!token.optional && output[token.key] === undefined)
|
|
663
|
+
throw new Error(`missing required property: ${token.key}`);
|
|
664
|
+
}
|
|
665
|
+
return output;
|
|
666
|
+
};
|
|
667
|
+
var index = {
|
|
668
|
+
encode,
|
|
669
|
+
decode
|
|
670
|
+
};
|
|
671
|
+
|
|
672
|
+
/*!
|
|
673
|
+
* hash-wasm (https://www.npmjs.com/package/hash-wasm)
|
|
674
|
+
* (c) Dani Biro
|
|
675
|
+
* @license MIT
|
|
676
|
+
*/
|
|
677
|
+
|
|
678
|
+
/*! *****************************************************************************
|
|
679
|
+
Copyright (c) Microsoft Corporation.
|
|
680
|
+
|
|
681
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
682
|
+
purpose with or without fee is hereby granted.
|
|
683
|
+
|
|
684
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
685
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
686
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
687
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
688
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
689
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
690
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
691
|
+
***************************************************************************** */
|
|
692
|
+
|
|
693
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
694
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
695
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
696
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
697
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
698
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
699
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
class Mutex {
|
|
704
|
+
constructor() {
|
|
705
|
+
this.mutex = Promise.resolve();
|
|
706
|
+
}
|
|
707
|
+
lock() {
|
|
708
|
+
let begin = () => { };
|
|
709
|
+
this.mutex = this.mutex.then(() => new Promise(begin));
|
|
710
|
+
return new Promise((res) => {
|
|
711
|
+
begin = res;
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
dispatch(fn) {
|
|
715
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
716
|
+
const unlock = yield this.lock();
|
|
717
|
+
try {
|
|
718
|
+
return yield Promise.resolve(fn());
|
|
719
|
+
}
|
|
720
|
+
finally {
|
|
721
|
+
unlock();
|
|
722
|
+
}
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/* eslint-disable import/prefer-default-export */
|
|
728
|
+
/* eslint-disable no-bitwise */
|
|
729
|
+
var _a;
|
|
730
|
+
function getGlobal() {
|
|
731
|
+
if (typeof globalThis !== 'undefined')
|
|
732
|
+
return globalThis;
|
|
733
|
+
// eslint-disable-next-line no-restricted-globals
|
|
734
|
+
if (typeof self !== 'undefined')
|
|
735
|
+
return self;
|
|
736
|
+
if (typeof window !== 'undefined')
|
|
737
|
+
return window;
|
|
738
|
+
return global;
|
|
739
|
+
}
|
|
740
|
+
const globalObject = getGlobal();
|
|
741
|
+
const nodeBuffer = (_a = globalObject.Buffer) !== null && _a !== void 0 ? _a : null;
|
|
742
|
+
const textEncoder = globalObject.TextEncoder ? new globalObject.TextEncoder() : null;
|
|
743
|
+
function hexCharCodesToInt(a, b) {
|
|
744
|
+
return (((a & 0xF) + ((a >> 6) | ((a >> 3) & 0x8))) << 4) | ((b & 0xF) + ((b >> 6) | ((b >> 3) & 0x8)));
|
|
745
|
+
}
|
|
746
|
+
function writeHexToUInt8(buf, str) {
|
|
747
|
+
const size = str.length >> 1;
|
|
748
|
+
for (let i = 0; i < size; i++) {
|
|
749
|
+
const index = i << 1;
|
|
750
|
+
buf[i] = hexCharCodesToInt(str.charCodeAt(index), str.charCodeAt(index + 1));
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
function hexStringEqualsUInt8(str, buf) {
|
|
754
|
+
if (str.length !== buf.length * 2) {
|
|
755
|
+
return false;
|
|
756
|
+
}
|
|
757
|
+
for (let i = 0; i < buf.length; i++) {
|
|
758
|
+
const strIndex = i << 1;
|
|
759
|
+
if (buf[i] !== hexCharCodesToInt(str.charCodeAt(strIndex), str.charCodeAt(strIndex + 1))) {
|
|
760
|
+
return false;
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
return true;
|
|
764
|
+
}
|
|
765
|
+
const alpha = 'a'.charCodeAt(0) - 10;
|
|
766
|
+
const digit = '0'.charCodeAt(0);
|
|
767
|
+
function getDigestHex(tmpBuffer, input, hashLength) {
|
|
768
|
+
let p = 0;
|
|
769
|
+
/* eslint-disable no-plusplus */
|
|
770
|
+
for (let i = 0; i < hashLength; i++) {
|
|
771
|
+
let nibble = input[i] >>> 4;
|
|
772
|
+
tmpBuffer[p++] = nibble > 9 ? nibble + alpha : nibble + digit;
|
|
773
|
+
nibble = input[i] & 0xF;
|
|
774
|
+
tmpBuffer[p++] = nibble > 9 ? nibble + alpha : nibble + digit;
|
|
775
|
+
}
|
|
776
|
+
/* eslint-enable no-plusplus */
|
|
777
|
+
return String.fromCharCode.apply(null, tmpBuffer);
|
|
778
|
+
}
|
|
779
|
+
const getUInt8Buffer = nodeBuffer !== null
|
|
780
|
+
? (data) => {
|
|
781
|
+
if (typeof data === 'string') {
|
|
782
|
+
const buf = nodeBuffer.from(data, 'utf8');
|
|
783
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.length);
|
|
784
|
+
}
|
|
785
|
+
if (nodeBuffer.isBuffer(data)) {
|
|
786
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.length);
|
|
787
|
+
}
|
|
788
|
+
if (ArrayBuffer.isView(data)) {
|
|
789
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
790
|
+
}
|
|
791
|
+
throw new Error('Invalid data type!');
|
|
792
|
+
}
|
|
793
|
+
: (data) => {
|
|
794
|
+
if (typeof data === 'string') {
|
|
795
|
+
return textEncoder.encode(data);
|
|
796
|
+
}
|
|
797
|
+
if (ArrayBuffer.isView(data)) {
|
|
798
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
799
|
+
}
|
|
800
|
+
throw new Error('Invalid data type!');
|
|
801
|
+
};
|
|
802
|
+
const base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
803
|
+
const base64Lookup = new Uint8Array(256);
|
|
804
|
+
for (let i = 0; i < base64Chars.length; i++) {
|
|
805
|
+
base64Lookup[base64Chars.charCodeAt(i)] = i;
|
|
806
|
+
}
|
|
807
|
+
function getDecodeBase64Length(data) {
|
|
808
|
+
let bufferLength = Math.floor(data.length * 0.75);
|
|
809
|
+
const len = data.length;
|
|
810
|
+
if (data[len - 1] === '=') {
|
|
811
|
+
bufferLength -= 1;
|
|
812
|
+
if (data[len - 2] === '=') {
|
|
813
|
+
bufferLength -= 1;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
return bufferLength;
|
|
817
|
+
}
|
|
818
|
+
function decodeBase64(data) {
|
|
819
|
+
const bufferLength = getDecodeBase64Length(data);
|
|
820
|
+
const len = data.length;
|
|
821
|
+
const bytes = new Uint8Array(bufferLength);
|
|
822
|
+
let p = 0;
|
|
823
|
+
for (let i = 0; i < len; i += 4) {
|
|
824
|
+
const encoded1 = base64Lookup[data.charCodeAt(i)];
|
|
825
|
+
const encoded2 = base64Lookup[data.charCodeAt(i + 1)];
|
|
826
|
+
const encoded3 = base64Lookup[data.charCodeAt(i + 2)];
|
|
827
|
+
const encoded4 = base64Lookup[data.charCodeAt(i + 3)];
|
|
828
|
+
bytes[p] = (encoded1 << 2) | (encoded2 >> 4);
|
|
829
|
+
p += 1;
|
|
830
|
+
bytes[p] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
|
|
831
|
+
p += 1;
|
|
832
|
+
bytes[p] = ((encoded3 & 3) << 6) | (encoded4 & 63);
|
|
833
|
+
p += 1;
|
|
834
|
+
}
|
|
835
|
+
return bytes;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
const MAX_HEAP = 16 * 1024;
|
|
839
|
+
const WASM_FUNC_HASH_LENGTH = 4;
|
|
840
|
+
const wasmMutex = new Mutex();
|
|
841
|
+
const wasmModuleCache = new Map();
|
|
842
|
+
function WASMInterface(binary, hashLength) {
|
|
843
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
844
|
+
let wasmInstance = null;
|
|
845
|
+
let memoryView = null;
|
|
846
|
+
let initialized = false;
|
|
847
|
+
if (typeof WebAssembly === 'undefined') {
|
|
848
|
+
throw new Error('WebAssembly is not supported in this environment!');
|
|
849
|
+
}
|
|
850
|
+
const writeMemory = (data, offset = 0) => {
|
|
851
|
+
memoryView.set(data, offset);
|
|
852
|
+
};
|
|
853
|
+
const getMemory = () => memoryView;
|
|
854
|
+
const getExports = () => wasmInstance.exports;
|
|
855
|
+
const setMemorySize = (totalSize) => {
|
|
856
|
+
wasmInstance.exports.Hash_SetMemorySize(totalSize);
|
|
857
|
+
const arrayOffset = wasmInstance.exports.Hash_GetBuffer();
|
|
858
|
+
const memoryBuffer = wasmInstance.exports.memory.buffer;
|
|
859
|
+
memoryView = new Uint8Array(memoryBuffer, arrayOffset, totalSize);
|
|
860
|
+
};
|
|
861
|
+
const getStateSize = () => {
|
|
862
|
+
const view = new DataView(wasmInstance.exports.memory.buffer);
|
|
863
|
+
const stateSize = view.getUint32(wasmInstance.exports.STATE_SIZE, true);
|
|
864
|
+
return stateSize;
|
|
865
|
+
};
|
|
866
|
+
const loadWASMPromise = wasmMutex.dispatch(() => __awaiter(this, void 0, void 0, function* () {
|
|
867
|
+
if (!wasmModuleCache.has(binary.name)) {
|
|
868
|
+
const asm = decodeBase64(binary.data);
|
|
869
|
+
const promise = WebAssembly.compile(asm);
|
|
870
|
+
wasmModuleCache.set(binary.name, promise);
|
|
871
|
+
}
|
|
872
|
+
const module = yield wasmModuleCache.get(binary.name);
|
|
873
|
+
wasmInstance = yield WebAssembly.instantiate(module, {
|
|
874
|
+
// env: {
|
|
875
|
+
// emscripten_memcpy_big: (dest, src, num) => {
|
|
876
|
+
// const memoryBuffer = wasmInstance.exports.memory.buffer;
|
|
877
|
+
// const memView = new Uint8Array(memoryBuffer, 0);
|
|
878
|
+
// memView.set(memView.subarray(src, src + num), dest);
|
|
879
|
+
// },
|
|
880
|
+
// print_memory: (offset, len) => {
|
|
881
|
+
// const memoryBuffer = wasmInstance.exports.memory.buffer;
|
|
882
|
+
// const memView = new Uint8Array(memoryBuffer, 0);
|
|
883
|
+
// console.log('print_int32', memView.subarray(offset, offset + len));
|
|
884
|
+
// },
|
|
885
|
+
// },
|
|
886
|
+
});
|
|
887
|
+
// wasmInstance.exports._start();
|
|
888
|
+
}));
|
|
889
|
+
const setupInterface = () => __awaiter(this, void 0, void 0, function* () {
|
|
890
|
+
if (!wasmInstance) {
|
|
891
|
+
yield loadWASMPromise;
|
|
892
|
+
}
|
|
893
|
+
const arrayOffset = wasmInstance.exports.Hash_GetBuffer();
|
|
894
|
+
const memoryBuffer = wasmInstance.exports.memory.buffer;
|
|
895
|
+
memoryView = new Uint8Array(memoryBuffer, arrayOffset, MAX_HEAP);
|
|
896
|
+
});
|
|
897
|
+
const init = (bits = null) => {
|
|
898
|
+
initialized = true;
|
|
899
|
+
wasmInstance.exports.Hash_Init(bits);
|
|
900
|
+
};
|
|
901
|
+
const updateUInt8Array = (data) => {
|
|
902
|
+
let read = 0;
|
|
903
|
+
while (read < data.length) {
|
|
904
|
+
const chunk = data.subarray(read, read + MAX_HEAP);
|
|
905
|
+
read += chunk.length;
|
|
906
|
+
memoryView.set(chunk);
|
|
907
|
+
wasmInstance.exports.Hash_Update(chunk.length);
|
|
908
|
+
}
|
|
909
|
+
};
|
|
910
|
+
const update = (data) => {
|
|
911
|
+
if (!initialized) {
|
|
912
|
+
throw new Error('update() called before init()');
|
|
913
|
+
}
|
|
914
|
+
const Uint8Buffer = getUInt8Buffer(data);
|
|
915
|
+
updateUInt8Array(Uint8Buffer);
|
|
916
|
+
};
|
|
917
|
+
const digestChars = new Uint8Array(hashLength * 2);
|
|
918
|
+
const digest = (outputType, padding = null) => {
|
|
919
|
+
if (!initialized) {
|
|
920
|
+
throw new Error('digest() called before init()');
|
|
921
|
+
}
|
|
922
|
+
initialized = false;
|
|
923
|
+
wasmInstance.exports.Hash_Final(padding);
|
|
924
|
+
if (outputType === 'binary') {
|
|
925
|
+
// the data is copied to allow GC of the original memory object
|
|
926
|
+
return memoryView.slice(0, hashLength);
|
|
927
|
+
}
|
|
928
|
+
return getDigestHex(digestChars, memoryView, hashLength);
|
|
929
|
+
};
|
|
930
|
+
const save = () => {
|
|
931
|
+
if (!initialized) {
|
|
932
|
+
throw new Error('save() can only be called after init() and before digest()');
|
|
933
|
+
}
|
|
934
|
+
const stateOffset = wasmInstance.exports.Hash_GetState();
|
|
935
|
+
const stateLength = getStateSize();
|
|
936
|
+
const memoryBuffer = wasmInstance.exports.memory.buffer;
|
|
937
|
+
const internalState = new Uint8Array(memoryBuffer, stateOffset, stateLength);
|
|
938
|
+
// prefix is 4 bytes from SHA1 hash of the WASM binary
|
|
939
|
+
// it is used to detect incompatible internal states between different versions of hash-wasm
|
|
940
|
+
const prefixedState = new Uint8Array(WASM_FUNC_HASH_LENGTH + stateLength);
|
|
941
|
+
writeHexToUInt8(prefixedState, binary.hash);
|
|
942
|
+
prefixedState.set(internalState, WASM_FUNC_HASH_LENGTH);
|
|
943
|
+
return prefixedState;
|
|
944
|
+
};
|
|
945
|
+
const load = (state) => {
|
|
946
|
+
if (!(state instanceof Uint8Array)) {
|
|
947
|
+
throw new Error('load() expects an Uint8Array generated by save()');
|
|
948
|
+
}
|
|
949
|
+
const stateOffset = wasmInstance.exports.Hash_GetState();
|
|
950
|
+
const stateLength = getStateSize();
|
|
951
|
+
const overallLength = WASM_FUNC_HASH_LENGTH + stateLength;
|
|
952
|
+
const memoryBuffer = wasmInstance.exports.memory.buffer;
|
|
953
|
+
if (state.length !== overallLength) {
|
|
954
|
+
throw new Error(`Bad state length (expected ${overallLength} bytes, got ${state.length})`);
|
|
955
|
+
}
|
|
956
|
+
if (!hexStringEqualsUInt8(binary.hash, state.subarray(0, WASM_FUNC_HASH_LENGTH))) {
|
|
957
|
+
throw new Error('This state was written by an incompatible hash implementation');
|
|
958
|
+
}
|
|
959
|
+
const internalState = state.subarray(WASM_FUNC_HASH_LENGTH);
|
|
960
|
+
new Uint8Array(memoryBuffer, stateOffset, stateLength).set(internalState);
|
|
961
|
+
initialized = true;
|
|
962
|
+
};
|
|
963
|
+
const isDataShort = (data) => {
|
|
964
|
+
if (typeof data === 'string') {
|
|
965
|
+
// worst case is 4 bytes / char
|
|
966
|
+
return data.length < MAX_HEAP / 4;
|
|
967
|
+
}
|
|
968
|
+
return data.byteLength < MAX_HEAP;
|
|
969
|
+
};
|
|
970
|
+
let canSimplify = isDataShort;
|
|
971
|
+
switch (binary.name) {
|
|
972
|
+
case 'argon2':
|
|
973
|
+
case 'scrypt':
|
|
974
|
+
canSimplify = () => true;
|
|
975
|
+
break;
|
|
976
|
+
case 'blake2b':
|
|
977
|
+
case 'blake2s':
|
|
978
|
+
// if there is a key at blake2 then cannot simplify
|
|
979
|
+
canSimplify = (data, initParam) => initParam <= 512 && isDataShort(data);
|
|
980
|
+
break;
|
|
981
|
+
case 'blake3':
|
|
982
|
+
// if there is a key at blake3 then cannot simplify
|
|
983
|
+
canSimplify = (data, initParam) => initParam === 0 && isDataShort(data);
|
|
984
|
+
break;
|
|
985
|
+
case 'xxhash64': // cannot simplify
|
|
986
|
+
case 'xxhash3':
|
|
987
|
+
case 'xxhash128':
|
|
988
|
+
canSimplify = () => false;
|
|
989
|
+
break;
|
|
990
|
+
}
|
|
991
|
+
// shorthand for (init + update + digest) for better performance
|
|
992
|
+
const calculate = (data, initParam = null, digestParam = null) => {
|
|
993
|
+
if (!canSimplify(data, initParam)) {
|
|
994
|
+
init(initParam);
|
|
995
|
+
update(data);
|
|
996
|
+
return digest('hex', digestParam);
|
|
997
|
+
}
|
|
998
|
+
const buffer = getUInt8Buffer(data);
|
|
999
|
+
memoryView.set(buffer);
|
|
1000
|
+
wasmInstance.exports.Hash_Calculate(buffer.length, initParam, digestParam);
|
|
1001
|
+
return getDigestHex(digestChars, memoryView, hashLength);
|
|
1002
|
+
};
|
|
1003
|
+
yield setupInterface();
|
|
1004
|
+
return {
|
|
1005
|
+
getMemory,
|
|
1006
|
+
writeMemory,
|
|
1007
|
+
getExports,
|
|
1008
|
+
setMemorySize,
|
|
1009
|
+
init,
|
|
1010
|
+
update,
|
|
1011
|
+
digest,
|
|
1012
|
+
save,
|
|
1013
|
+
load,
|
|
1014
|
+
calculate,
|
|
1015
|
+
hashLength,
|
|
1016
|
+
};
|
|
1017
|
+
});
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
new Mutex();
|
|
1021
|
+
|
|
1022
|
+
new Mutex();
|
|
1023
|
+
|
|
1024
|
+
new Mutex();
|
|
1025
|
+
|
|
1026
|
+
new Mutex();
|
|
1027
|
+
|
|
1028
|
+
new Mutex();
|
|
1029
|
+
|
|
1030
|
+
new Mutex();
|
|
1031
|
+
|
|
1032
|
+
new Mutex();
|
|
1033
|
+
|
|
1034
|
+
new Mutex();
|
|
1035
|
+
|
|
1036
|
+
new Mutex();
|
|
1037
|
+
|
|
1038
|
+
var name$b = "sha3";
|
|
1039
|
+
var data$b = "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";
|
|
1040
|
+
var hash$b = "ec266d91";
|
|
1041
|
+
var wasmJson$b = {
|
|
1042
|
+
name: name$b,
|
|
1043
|
+
data: data$b,
|
|
1044
|
+
hash: hash$b
|
|
1045
|
+
};
|
|
1046
|
+
|
|
1047
|
+
new Mutex();
|
|
1048
|
+
|
|
1049
|
+
new Mutex();
|
|
1050
|
+
function validateBits(bits) {
|
|
1051
|
+
if (![224, 256, 384, 512].includes(bits)) {
|
|
1052
|
+
return new Error('Invalid variant! Valid values: 224, 256, 384, 512');
|
|
1053
|
+
}
|
|
1054
|
+
return null;
|
|
1055
|
+
}
|
|
1056
|
+
/**
|
|
1057
|
+
* Creates a new Keccak hash instance
|
|
1058
|
+
* @param bits Number of output bits. Valid values: 224, 256, 384, 512
|
|
1059
|
+
*/
|
|
1060
|
+
function createKeccak(bits = 512) {
|
|
1061
|
+
if (validateBits(bits)) {
|
|
1062
|
+
return Promise.reject(validateBits(bits));
|
|
1063
|
+
}
|
|
1064
|
+
const outputSize = bits / 8;
|
|
1065
|
+
return WASMInterface(wasmJson$b, outputSize).then((wasm) => {
|
|
1066
|
+
wasm.init(bits);
|
|
1067
|
+
const obj = {
|
|
1068
|
+
init: () => { wasm.init(bits); return obj; },
|
|
1069
|
+
update: (data) => { wasm.update(data); return obj; },
|
|
1070
|
+
digest: (outputType) => wasm.digest(outputType, 0x01),
|
|
1071
|
+
save: () => wasm.save(),
|
|
1072
|
+
load: (data) => { wasm.load(data); return obj; },
|
|
1073
|
+
blockSize: 200 - 2 * outputSize,
|
|
1074
|
+
digestSize: outputSize,
|
|
1075
|
+
};
|
|
1076
|
+
return obj;
|
|
1077
|
+
});
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
new Mutex();
|
|
1081
|
+
|
|
1082
|
+
new Mutex();
|
|
1083
|
+
|
|
1084
|
+
new Mutex();
|
|
1085
|
+
|
|
1086
|
+
new Mutex();
|
|
1087
|
+
|
|
1088
|
+
new Mutex();
|
|
1089
|
+
|
|
1090
|
+
new Mutex();
|
|
1091
|
+
|
|
1092
|
+
new Mutex();
|
|
1093
|
+
|
|
1094
|
+
new Mutex();
|
|
1095
|
+
|
|
1096
|
+
new Mutex();
|
|
1097
|
+
|
|
1098
|
+
new Mutex();
|
|
1099
|
+
|
|
1100
|
+
new Mutex();
|
|
1101
|
+
|
|
1102
|
+
const blockchainCodecs = [
|
|
1103
|
+
{
|
|
1104
|
+
name: 'leofcoin-block',
|
|
1105
|
+
codec: '0x6c62',
|
|
1106
|
+
hashAlg: 'dbl-keccak-512',
|
|
1107
|
+
},
|
|
1108
|
+
{
|
|
1109
|
+
name: 'leofcoin-tx',
|
|
1110
|
+
codec: '0x6c74',
|
|
1111
|
+
hashAlg: 'dbl-keccak-512',
|
|
1112
|
+
},
|
|
1113
|
+
{
|
|
1114
|
+
name: 'leofcoin-itx',
|
|
1115
|
+
codec: '0x6c69',
|
|
1116
|
+
hashAlg: 'keccak-512',
|
|
1117
|
+
},
|
|
1118
|
+
{
|
|
1119
|
+
name: 'leofcoin-pr',
|
|
1120
|
+
codec: '0x6c70',
|
|
1121
|
+
hashAlg: 'keccak-256',
|
|
1122
|
+
},
|
|
1123
|
+
{
|
|
1124
|
+
name: 'contract-message',
|
|
1125
|
+
codec: '0x63636d',
|
|
1126
|
+
hashAlg: 'keccak-256'
|
|
1127
|
+
},
|
|
1128
|
+
{
|
|
1129
|
+
name: 'transaction-message',
|
|
1130
|
+
codec: '0x746d',
|
|
1131
|
+
hashAlg: 'keccak-256'
|
|
1132
|
+
},
|
|
1133
|
+
{
|
|
1134
|
+
name: 'block-message',
|
|
1135
|
+
codec: '0x626d',
|
|
1136
|
+
hashAlg: 'keccak-256'
|
|
1137
|
+
},
|
|
1138
|
+
{
|
|
1139
|
+
name: 'bw-message',
|
|
1140
|
+
codec: '0x62776d',
|
|
1141
|
+
hashAlg: 'keccak-256'
|
|
1142
|
+
},
|
|
1143
|
+
{
|
|
1144
|
+
name: 'bw-request-message',
|
|
1145
|
+
codec: '0x6277726d',
|
|
1146
|
+
hashAlg: 'keccak-256'
|
|
1147
|
+
},
|
|
1148
|
+
{
|
|
1149
|
+
name: 'validator-message',
|
|
1150
|
+
codec: '0x766d',
|
|
1151
|
+
hashAlg: 'keccak-256'
|
|
1152
|
+
}
|
|
1153
|
+
];
|
|
1154
|
+
|
|
1155
|
+
const internalCodecs = [
|
|
1156
|
+
{
|
|
1157
|
+
name: 'disco-hash',
|
|
1158
|
+
codec: '0x30',
|
|
1159
|
+
hashAlg: 'dbl-keccak-256',
|
|
1160
|
+
},
|
|
1161
|
+
{
|
|
1162
|
+
name: 'peernet-peer-response',
|
|
1163
|
+
codec: '0x707072',
|
|
1164
|
+
hashAlg: 'keccak-256',
|
|
1165
|
+
},
|
|
1166
|
+
{
|
|
1167
|
+
name: 'peernet-peer',
|
|
1168
|
+
codec: '0x7070',
|
|
1169
|
+
hashAlg: 'keccak-256',
|
|
1170
|
+
},
|
|
1171
|
+
{
|
|
1172
|
+
name: 'peernet-dht',
|
|
1173
|
+
codec: '0x706468',
|
|
1174
|
+
hashAlg: 'keccak-256',
|
|
1175
|
+
},
|
|
1176
|
+
{
|
|
1177
|
+
name: 'peernet-dht-response',
|
|
1178
|
+
codec: '0x706472',
|
|
1179
|
+
hashAlg: 'keccak-256',
|
|
1180
|
+
},
|
|
1181
|
+
{
|
|
1182
|
+
name: 'peernet-data',
|
|
1183
|
+
codec: '0x706461',
|
|
1184
|
+
hashAlg: 'keccak-256',
|
|
1185
|
+
},
|
|
1186
|
+
{
|
|
1187
|
+
name: 'peernet-data-response',
|
|
1188
|
+
codec: '0x70646172',
|
|
1189
|
+
hashAlg: 'keccak-256',
|
|
1190
|
+
},
|
|
1191
|
+
{
|
|
1192
|
+
name: 'peernet-message',
|
|
1193
|
+
codec: '0x706d65',
|
|
1194
|
+
hashAlg: 'keccak-256',
|
|
1195
|
+
},
|
|
1196
|
+
{
|
|
1197
|
+
name: 'peernet-ps',
|
|
1198
|
+
codec: '707073',
|
|
1199
|
+
hashAlg: 'keccak-256',
|
|
1200
|
+
},
|
|
1201
|
+
{
|
|
1202
|
+
name: 'peernet-response',
|
|
1203
|
+
codec: '0x7072',
|
|
1204
|
+
hashAlg: 'keccak-256',
|
|
1205
|
+
},
|
|
1206
|
+
{
|
|
1207
|
+
name: 'peernet-request',
|
|
1208
|
+
codec: '0x707271',
|
|
1209
|
+
hashAlg: 'keccak-256',
|
|
1210
|
+
},
|
|
1211
|
+
{
|
|
1212
|
+
name: 'peernet-file',
|
|
1213
|
+
codec: '0x7066',
|
|
1214
|
+
hashAlg: 'keccak-256',
|
|
1215
|
+
},
|
|
1216
|
+
{
|
|
1217
|
+
name: 'peernet-file-response',
|
|
1218
|
+
codec: '0x706672',
|
|
1219
|
+
hashAlg: 'keccak-256',
|
|
1220
|
+
}
|
|
1221
|
+
];
|
|
1222
|
+
|
|
1223
|
+
const codecs = [
|
|
1224
|
+
...internalCodecs,
|
|
1225
|
+
...blockchainCodecs,
|
|
1226
|
+
{
|
|
1227
|
+
name: 'chat-message',
|
|
1228
|
+
codec: '0x70636d',
|
|
1229
|
+
hashAlg: 'dbl-keccak-256',
|
|
1230
|
+
}
|
|
1231
|
+
];
|
|
1232
|
+
|
|
1233
|
+
globalThis.peernetCodecs = globalThis.peernetCodecs || {};
|
|
1234
|
+
const addCodec = (codecInput) => {
|
|
1235
|
+
let { hashAlg, codec, name } = codecInput;
|
|
1236
|
+
if (!globalThis.peernetCodecs[name])
|
|
1237
|
+
globalThis.peernetCodecs[name] = {
|
|
1238
|
+
hashAlg,
|
|
1239
|
+
codec: typeof codec === 'string' ? parseInt(codec, 16) : codec
|
|
1240
|
+
};
|
|
1241
|
+
};
|
|
1242
|
+
const getCodec = (name) => {
|
|
1243
|
+
if (typeof name === 'number')
|
|
1244
|
+
return name;
|
|
1245
|
+
return getCodecByName(name).codec;
|
|
1246
|
+
};
|
|
1247
|
+
const getCodecName = (codec) => {
|
|
1248
|
+
return Object.keys(globalThis.peernetCodecs).reduce((p, c) => {
|
|
1249
|
+
const item = globalThis.peernetCodecs[c];
|
|
1250
|
+
if (item.codec === codec)
|
|
1251
|
+
return c;
|
|
1252
|
+
else
|
|
1253
|
+
return p;
|
|
1254
|
+
}, undefined);
|
|
1255
|
+
};
|
|
1256
|
+
const getCodecByName = (name) => globalThis.peernetCodecs[name];
|
|
1257
|
+
const getHashAlg = (name) => {
|
|
1258
|
+
if (typeof name === 'number')
|
|
1259
|
+
return getCodecByName(getCodecName(name)).hashAlg;
|
|
1260
|
+
return getCodecByName(name).hashAlg;
|
|
1261
|
+
};
|
|
1262
|
+
const isCodec = (codec) => {
|
|
1263
|
+
if (codec.codec !== undefined && codec.hashAlg)
|
|
1264
|
+
return true;
|
|
1265
|
+
return false;
|
|
1266
|
+
};
|
|
1267
|
+
const validateCodec = (codec) => {
|
|
1268
|
+
if (codec.codec === undefined ||
|
|
1269
|
+
codec.hashAlg === undefined ||
|
|
1270
|
+
codec.name === undefined)
|
|
1271
|
+
throw new Error(`invalid codecInput: ${codec}`);
|
|
1272
|
+
};
|
|
1273
|
+
for (const codec of codecs) {
|
|
1274
|
+
addCodec(codec);
|
|
1275
|
+
}
|
|
1276
|
+
var utils = {
|
|
1277
|
+
isCodec,
|
|
1278
|
+
addCodec,
|
|
1279
|
+
getCodec,
|
|
1280
|
+
getHashAlg,
|
|
1281
|
+
getCodecName,
|
|
1282
|
+
validateCodec,
|
|
1283
|
+
codecs: globalThis.peernetCodecs
|
|
1284
|
+
};
|
|
1285
|
+
|
|
1286
|
+
/**
|
|
1287
|
+
* @param {string}
|
|
1288
|
+
*/
|
|
1289
|
+
var isHex = (function (string) { return /^[A-F0-9]+$/i.test(string); });
|
|
1290
|
+
|
|
1291
|
+
let BasicInterface$1 = class BasicInterface {
|
|
1292
|
+
encoded;
|
|
1293
|
+
decoded;
|
|
1294
|
+
keys;
|
|
1295
|
+
name;
|
|
1296
|
+
#proto;
|
|
1297
|
+
set proto(value) {
|
|
1298
|
+
this.#proto = value;
|
|
1299
|
+
this.keys = Object.keys(value);
|
|
1300
|
+
}
|
|
1301
|
+
get proto() {
|
|
1302
|
+
return this.#proto;
|
|
1303
|
+
}
|
|
1304
|
+
decode(encoded) {
|
|
1305
|
+
encoded = encoded || this.encoded;
|
|
1306
|
+
return new Object();
|
|
1307
|
+
}
|
|
1308
|
+
encode(decoded) {
|
|
1309
|
+
decoded = decoded || this.decoded;
|
|
1310
|
+
return new Uint8Array();
|
|
1311
|
+
}
|
|
1312
|
+
// get Codec(): Codec {}
|
|
1313
|
+
protoEncode(data) {
|
|
1314
|
+
// check schema
|
|
1315
|
+
return index.encode(this.proto, data);
|
|
1316
|
+
}
|
|
1317
|
+
protoDecode(data) {
|
|
1318
|
+
// check schema
|
|
1319
|
+
return index.decode(this.proto, data);
|
|
1320
|
+
}
|
|
1321
|
+
isHex(string) {
|
|
1322
|
+
return isHex(string);
|
|
1323
|
+
}
|
|
1324
|
+
isBase32(string) {
|
|
1325
|
+
return index$7.isBase32(string);
|
|
1326
|
+
}
|
|
1327
|
+
isBase58(string) {
|
|
1328
|
+
return base58$1.isBase58(string);
|
|
1329
|
+
}
|
|
1330
|
+
fromBs32(encoded) {
|
|
1331
|
+
return this.decode(index$7.decode(encoded));
|
|
1332
|
+
}
|
|
1333
|
+
fromBs58(encoded) {
|
|
1334
|
+
return this.decode(fromBase58(encoded));
|
|
1335
|
+
}
|
|
1336
|
+
async toArray() {
|
|
1337
|
+
const array = [];
|
|
1338
|
+
for await (const value of this.encoded.values()) {
|
|
1339
|
+
array.push(value);
|
|
1340
|
+
}
|
|
1341
|
+
return array;
|
|
1342
|
+
}
|
|
1343
|
+
fromString(string) {
|
|
1344
|
+
const array = string.split(',');
|
|
1345
|
+
const arrayLike = array.map(string => Number(string));
|
|
1346
|
+
return this.decode(Uint8Array.from(arrayLike));
|
|
1347
|
+
}
|
|
1348
|
+
fromHex(string) {
|
|
1349
|
+
return this.decode(fromHex(string));
|
|
1350
|
+
}
|
|
1351
|
+
fromArray(array) {
|
|
1352
|
+
return this.decode(Uint8Array.from([...array]));
|
|
1353
|
+
}
|
|
1354
|
+
fromEncoded(encoded) {
|
|
1355
|
+
return this.decode(encoded);
|
|
1356
|
+
}
|
|
1357
|
+
toString() {
|
|
1358
|
+
if (!this.encoded)
|
|
1359
|
+
this.encode();
|
|
1360
|
+
return this.encoded.toString();
|
|
1361
|
+
}
|
|
1362
|
+
toHex() {
|
|
1363
|
+
if (!this.encoded)
|
|
1364
|
+
this.encode();
|
|
1365
|
+
return toHex(this.encoded.toString().split(',').map(number => Number(number)));
|
|
1366
|
+
}
|
|
1367
|
+
/**
|
|
1368
|
+
* @return {String} encoded
|
|
1369
|
+
*/
|
|
1370
|
+
toBs32() {
|
|
1371
|
+
if (!this.encoded)
|
|
1372
|
+
this.encode();
|
|
1373
|
+
return toBase32(this.encoded);
|
|
1374
|
+
}
|
|
1375
|
+
/**
|
|
1376
|
+
* @return {String} encoded
|
|
1377
|
+
*/
|
|
1378
|
+
toBs58() {
|
|
1379
|
+
if (!this.encoded)
|
|
1380
|
+
this.encode();
|
|
1381
|
+
return toBase58(this.encoded);
|
|
1382
|
+
}
|
|
1383
|
+
};
|
|
1384
|
+
|
|
1385
|
+
let Codec$1 = class Codec extends BasicInterface$1 {
|
|
1386
|
+
codecBuffer;
|
|
1387
|
+
codec;
|
|
1388
|
+
hashAlg;
|
|
1389
|
+
constructor(buffer) {
|
|
1390
|
+
super();
|
|
1391
|
+
if (buffer) {
|
|
1392
|
+
if (buffer instanceof Uint8Array) {
|
|
1393
|
+
const codec = index$6.decode(buffer);
|
|
1394
|
+
const name = this.getCodecName(codec);
|
|
1395
|
+
if (name) {
|
|
1396
|
+
this.name = name;
|
|
1397
|
+
this.encoded = buffer;
|
|
1398
|
+
this.decode(buffer);
|
|
1399
|
+
}
|
|
1400
|
+
else {
|
|
1401
|
+
this.encode(Number(new TextDecoder().decode(buffer)));
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
else if (buffer instanceof ArrayBuffer) {
|
|
1405
|
+
const codec = index$6.decode(new Uint8Array(buffer));
|
|
1406
|
+
const name = this.getCodecName(codec);
|
|
1407
|
+
if (name) {
|
|
1408
|
+
this.name = name;
|
|
1409
|
+
this.decode(buffer);
|
|
1410
|
+
}
|
|
1411
|
+
else {
|
|
1412
|
+
this.encode(Number(new TextDecoder().decode(new Uint8Array(buffer))));
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
else if (typeof buffer === 'string') {
|
|
1416
|
+
if (utils.getCodec(buffer))
|
|
1417
|
+
this.fromName(buffer);
|
|
1418
|
+
else if (this.isHex(buffer))
|
|
1419
|
+
this.fromHex(buffer);
|
|
1420
|
+
else if (this.isBase32(buffer))
|
|
1421
|
+
this.fromBs32(buffer);
|
|
1422
|
+
else if (this.isBase58(buffer))
|
|
1423
|
+
this.fromBs58(buffer);
|
|
1424
|
+
else
|
|
1425
|
+
this.fromString(buffer);
|
|
1426
|
+
}
|
|
1427
|
+
if (!isNaN(buffer))
|
|
1428
|
+
if (utils.getCodec(buffer))
|
|
1429
|
+
this.fromCodec(buffer);
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
fromEncoded(encoded) {
|
|
1433
|
+
const codec = index$6.decode(encoded);
|
|
1434
|
+
const name = this.getCodecName(codec);
|
|
1435
|
+
this.name = name;
|
|
1436
|
+
this.encoded = encoded;
|
|
1437
|
+
return this.decode(encoded);
|
|
1438
|
+
}
|
|
1439
|
+
getCodec(name) {
|
|
1440
|
+
return utils.getCodec(name);
|
|
1441
|
+
}
|
|
1442
|
+
getCodecName(codec) {
|
|
1443
|
+
return utils.getCodecName(codec);
|
|
1444
|
+
}
|
|
1445
|
+
getHashAlg(name) {
|
|
1446
|
+
return utils.getHashAlg(name);
|
|
1447
|
+
}
|
|
1448
|
+
fromCodec(codec) {
|
|
1449
|
+
this.name = this.getCodecName(codec);
|
|
1450
|
+
this.hashAlg = this.getHashAlg(this.name);
|
|
1451
|
+
this.codec = this.getCodec(this.name);
|
|
1452
|
+
this.codecBuffer = index$6.encode(this.codec);
|
|
1453
|
+
}
|
|
1454
|
+
fromName(name) {
|
|
1455
|
+
const codec = this.getCodec(name);
|
|
1456
|
+
this.name = name;
|
|
1457
|
+
this.codec = codec;
|
|
1458
|
+
this.hashAlg = this.getHashAlg(name);
|
|
1459
|
+
this.codecBuffer = index$6.encode(this.codec);
|
|
1460
|
+
}
|
|
1461
|
+
decode(encoded) {
|
|
1462
|
+
encoded = encoded || this.encoded;
|
|
1463
|
+
const codec = index$6.decode(encoded);
|
|
1464
|
+
this.fromCodec(codec);
|
|
1465
|
+
return this.decoded;
|
|
1466
|
+
}
|
|
1467
|
+
encode(codec) {
|
|
1468
|
+
codec = codec || this.codec;
|
|
1469
|
+
this.encoded = index$6.encode(codec);
|
|
1470
|
+
return this.encoded;
|
|
1471
|
+
}
|
|
1472
|
+
};
|
|
1473
|
+
|
|
1474
|
+
let CodecHash$1 = class CodecHash extends BasicInterface$1 {
|
|
1475
|
+
codec;
|
|
1476
|
+
codecs;
|
|
1477
|
+
digest;
|
|
1478
|
+
size;
|
|
1479
|
+
constructor(buffer, options) {
|
|
1480
|
+
super();
|
|
1481
|
+
if (options.name)
|
|
1482
|
+
this.name = options.name;
|
|
1483
|
+
else
|
|
1484
|
+
this.name = 'disco-hash';
|
|
1485
|
+
if (options.codecs)
|
|
1486
|
+
this.codecs = options.codecs;
|
|
1487
|
+
return this.init(buffer);
|
|
1488
|
+
}
|
|
1489
|
+
async init(uint8Array) {
|
|
1490
|
+
if (uint8Array) {
|
|
1491
|
+
if (uint8Array instanceof Uint8Array) {
|
|
1492
|
+
this.codec = new Codec$1(uint8Array);
|
|
1493
|
+
const name = this.codec.name;
|
|
1494
|
+
if (name) {
|
|
1495
|
+
this.name = name;
|
|
1496
|
+
this.decode(uint8Array);
|
|
1497
|
+
}
|
|
1498
|
+
else {
|
|
1499
|
+
await this.encode(uint8Array);
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
if (typeof uint8Array === 'string') {
|
|
1503
|
+
if (this.isHex(uint8Array))
|
|
1504
|
+
await this.fromHex(uint8Array);
|
|
1505
|
+
if (this.isBase32(uint8Array))
|
|
1506
|
+
await this.fromBs32(uint8Array);
|
|
1507
|
+
else if (this.isBase58(uint8Array))
|
|
1508
|
+
await this.fromBs58(uint8Array);
|
|
1509
|
+
else
|
|
1510
|
+
throw new Error(`unsupported string ${uint8Array}`);
|
|
1511
|
+
}
|
|
1512
|
+
else if (typeof uint8Array === 'object')
|
|
1513
|
+
await this.fromJSON(uint8Array);
|
|
1514
|
+
}
|
|
1515
|
+
return this;
|
|
1516
|
+
}
|
|
1517
|
+
get prefix() {
|
|
1518
|
+
const length = this.length;
|
|
1519
|
+
const uint8Array = new Uint8Array(length.length + this.codec.codecBuffer.length);
|
|
1520
|
+
uint8Array.set(length);
|
|
1521
|
+
uint8Array.set(this.codec.codecBuffer, length.length);
|
|
1522
|
+
return uint8Array;
|
|
1523
|
+
}
|
|
1524
|
+
get length() {
|
|
1525
|
+
return index$6.encode(this.size);
|
|
1526
|
+
}
|
|
1527
|
+
get buffer() {
|
|
1528
|
+
return this.encoded;
|
|
1529
|
+
}
|
|
1530
|
+
get hash() {
|
|
1531
|
+
return this.encoded;
|
|
1532
|
+
}
|
|
1533
|
+
fromJSON(json) {
|
|
1534
|
+
return this.encode(new TextEncoder().encode(JSON.stringify(json)));
|
|
1535
|
+
}
|
|
1536
|
+
async encode(buffer, name) {
|
|
1537
|
+
if (!this.name && name)
|
|
1538
|
+
this.name = name;
|
|
1539
|
+
if (!buffer)
|
|
1540
|
+
buffer = this.buffer;
|
|
1541
|
+
this.codec = new Codec$1(this.name);
|
|
1542
|
+
this.codec.fromName(this.name);
|
|
1543
|
+
let hashAlg = this.codec.hashAlg;
|
|
1544
|
+
const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1]);
|
|
1545
|
+
if (hashAlg.includes('dbl')) {
|
|
1546
|
+
hashAlg = hashAlg.replace('dbl-', '');
|
|
1547
|
+
const hasher = await createKeccak(hashVariant);
|
|
1548
|
+
await hasher.init();
|
|
1549
|
+
hasher.update(buffer);
|
|
1550
|
+
buffer = hasher.digest('binary');
|
|
1551
|
+
}
|
|
1552
|
+
const hasher = await createKeccak(hashVariant);
|
|
1553
|
+
await hasher.init();
|
|
1554
|
+
hasher.update(buffer);
|
|
1555
|
+
this.digest = hasher.digest('binary');
|
|
1556
|
+
this.size = this.digest.length;
|
|
1557
|
+
const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
|
|
1558
|
+
uint8Array.set(this.prefix);
|
|
1559
|
+
uint8Array.set(this.digest, this.prefix.length);
|
|
1560
|
+
this.encoded = uint8Array;
|
|
1561
|
+
return this.encoded;
|
|
1562
|
+
}
|
|
1563
|
+
async validate(buffer) {
|
|
1564
|
+
if (Buffer.isBuffer(buffer)) {
|
|
1565
|
+
const codec = index$6.decode(buffer);
|
|
1566
|
+
if (this.codecs[codec]) {
|
|
1567
|
+
this.decode(buffer);
|
|
1568
|
+
}
|
|
1569
|
+
else {
|
|
1570
|
+
await this.encode(buffer);
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
if (typeof buffer === 'string') {
|
|
1574
|
+
if (this.isHex(buffer))
|
|
1575
|
+
this.fromHex(buffer);
|
|
1576
|
+
if (this.isBase32(buffer))
|
|
1577
|
+
this.fromBs32(buffer);
|
|
1578
|
+
}
|
|
1579
|
+
if (typeof buffer === 'object')
|
|
1580
|
+
this.fromJSON(buffer);
|
|
1581
|
+
}
|
|
1582
|
+
decode(buffer) {
|
|
1583
|
+
this.encoded = buffer;
|
|
1584
|
+
const codec = index$6.decode(buffer);
|
|
1585
|
+
this.codec = new Codec$1(codec);
|
|
1586
|
+
// TODO: validate codec
|
|
1587
|
+
buffer = buffer.slice(index$6.decode.bytes);
|
|
1588
|
+
this.size = index$6.decode(buffer);
|
|
1589
|
+
this.digest = buffer.slice(index$6.decode.bytes);
|
|
1590
|
+
if (this.digest.length !== this.size) {
|
|
1591
|
+
throw new Error(`hash length inconsistent: ${this.encoded.toString()}`);
|
|
1592
|
+
}
|
|
1593
|
+
// const codec = new Codec(codec, this.codecs)
|
|
1594
|
+
this.name = this.codec.name;
|
|
1595
|
+
this.size = this.digest.length;
|
|
1596
|
+
return {
|
|
1597
|
+
codec: this.codec,
|
|
1598
|
+
name: this.name,
|
|
1599
|
+
size: this.size,
|
|
1600
|
+
length: this.length,
|
|
1601
|
+
digest: this.digest,
|
|
1602
|
+
};
|
|
1603
|
+
}
|
|
1604
|
+
};
|
|
1605
|
+
|
|
1606
|
+
let FormatInterface$1 = class FormatInterface extends BasicInterface$1 {
|
|
1607
|
+
hashFormat;
|
|
1608
|
+
init(buffer) {
|
|
1609
|
+
if (buffer instanceof Uint8Array)
|
|
1610
|
+
this.fromUint8Array(buffer);
|
|
1611
|
+
else if (buffer instanceof ArrayBuffer)
|
|
1612
|
+
this.fromArrayBuffer(buffer);
|
|
1613
|
+
else if (buffer instanceof FormatInterface$1 && buffer?.name === this.name)
|
|
1614
|
+
return buffer;
|
|
1615
|
+
else if (typeof buffer === 'string') {
|
|
1616
|
+
if (this.isHex(buffer))
|
|
1617
|
+
this.fromHex(buffer);
|
|
1618
|
+
else if (this.isBase58(buffer))
|
|
1619
|
+
this.fromBs58(buffer);
|
|
1620
|
+
else if (this.isBase32(buffer))
|
|
1621
|
+
this.fromBs32(buffer);
|
|
1622
|
+
else
|
|
1623
|
+
this.fromString(buffer);
|
|
1624
|
+
}
|
|
1625
|
+
else {
|
|
1626
|
+
this.create(buffer);
|
|
1627
|
+
}
|
|
1628
|
+
return this;
|
|
1629
|
+
}
|
|
1630
|
+
hasCodec() {
|
|
1631
|
+
if (!this.encoded)
|
|
1632
|
+
return false;
|
|
1633
|
+
const codec = new Codec$1(this.encoded);
|
|
1634
|
+
if (codec.name)
|
|
1635
|
+
return true;
|
|
1636
|
+
}
|
|
1637
|
+
decode(encoded) {
|
|
1638
|
+
encoded = encoded || this.encoded;
|
|
1639
|
+
const codec = new Codec$1(this.encoded);
|
|
1640
|
+
if (codec.codecBuffer) {
|
|
1641
|
+
encoded = encoded.slice(codec.codecBuffer.length);
|
|
1642
|
+
this.name = codec.name;
|
|
1643
|
+
this.decoded = this.protoDecode(encoded);
|
|
1644
|
+
// try {
|
|
1645
|
+
// this.decoded = JSON.parse(this.decoded)
|
|
1646
|
+
// } catch {
|
|
1647
|
+
// }
|
|
1648
|
+
}
|
|
1649
|
+
else {
|
|
1650
|
+
throw new Error(`no codec found`);
|
|
1651
|
+
}
|
|
1652
|
+
return this.decoded;
|
|
1653
|
+
}
|
|
1654
|
+
encode(decoded) {
|
|
1655
|
+
let encoded;
|
|
1656
|
+
if (!decoded)
|
|
1657
|
+
decoded = this.decoded;
|
|
1658
|
+
const codec = new Codec$1(this.name);
|
|
1659
|
+
if (decoded instanceof Uint8Array)
|
|
1660
|
+
encoded = decoded;
|
|
1661
|
+
else
|
|
1662
|
+
encoded = this.protoEncode(decoded);
|
|
1663
|
+
if (codec.codecBuffer) {
|
|
1664
|
+
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
|
|
1665
|
+
uint8Array.set(codec.codecBuffer);
|
|
1666
|
+
uint8Array.set(encoded, codec.codecBuffer.length);
|
|
1667
|
+
this.encoded = uint8Array;
|
|
1668
|
+
}
|
|
1669
|
+
else {
|
|
1670
|
+
throw new Error(`invalid codec`);
|
|
1671
|
+
}
|
|
1672
|
+
return this.encoded;
|
|
1673
|
+
}
|
|
1674
|
+
/**
|
|
1675
|
+
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
1676
|
+
* @param {Object} proto - {protoObject}
|
|
1677
|
+
* @param {Object} options - {hashFormat, name}
|
|
1678
|
+
*/
|
|
1679
|
+
constructor(buffer, proto, options) {
|
|
1680
|
+
super();
|
|
1681
|
+
this.proto = proto;
|
|
1682
|
+
this.hashFormat = options?.hashFormat ? options.hashFormat : 'bs32';
|
|
1683
|
+
if (options?.name)
|
|
1684
|
+
this.name = options.name;
|
|
1685
|
+
this.init(buffer);
|
|
1686
|
+
}
|
|
1687
|
+
/**
|
|
1688
|
+
* @return {PeernetHash}
|
|
1689
|
+
*/
|
|
1690
|
+
get peernetHash() {
|
|
1691
|
+
return new CodecHash$1(this.decoded, { name: this.name });
|
|
1692
|
+
}
|
|
1693
|
+
/**
|
|
1694
|
+
* @return {peernetHash}
|
|
1695
|
+
*/
|
|
1696
|
+
async hash() {
|
|
1697
|
+
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
1698
|
+
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
1699
|
+
return (await this.peernetHash)[`to${format}`]();
|
|
1700
|
+
}
|
|
1701
|
+
fromUint8Array(buffer) {
|
|
1702
|
+
this.encoded = buffer;
|
|
1703
|
+
return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
1704
|
+
}
|
|
1705
|
+
fromArrayBuffer(buffer) {
|
|
1706
|
+
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
1707
|
+
return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
1708
|
+
}
|
|
1709
|
+
/**
|
|
1710
|
+
* @param {Object} data
|
|
1711
|
+
*/
|
|
1712
|
+
create(data) {
|
|
1713
|
+
const decoded = {};
|
|
1714
|
+
if (this.keys?.length > 0) {
|
|
1715
|
+
for (const key of this.keys) {
|
|
1716
|
+
Object.defineProperties(decoded, {
|
|
1717
|
+
[key]: {
|
|
1718
|
+
enumerable: true,
|
|
1719
|
+
configurable: true,
|
|
1720
|
+
set: (value) => data[key],
|
|
1721
|
+
get: () => data[key]
|
|
1722
|
+
}
|
|
1723
|
+
});
|
|
1724
|
+
}
|
|
1725
|
+
this.decoded = decoded;
|
|
1726
|
+
return this.encode(decoded);
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
};
|
|
1730
|
+
const FormatInterface = FormatInterface$1;
|
|
1731
|
+
const CodecHash = CodecHash$1;
|
|
1732
|
+
|
|
1733
|
+
var proto$5 = {
|
|
1734
|
+
timestamp: Number(),
|
|
1735
|
+
from: String(),
|
|
1736
|
+
to: String(),
|
|
1737
|
+
nonce: Number(),
|
|
1738
|
+
method: String(),
|
|
1739
|
+
params: Array(),
|
|
1740
|
+
signature: new Uint8Array()
|
|
1741
|
+
};
|
|
1742
|
+
|
|
1743
|
+
class TransactionMessage extends FormatInterface {
|
|
1744
|
+
get messageName() {
|
|
1745
|
+
return 'TransactionMessage';
|
|
1746
|
+
}
|
|
1747
|
+
constructor(buffer) {
|
|
1748
|
+
const name = 'transaction-message';
|
|
1749
|
+
super(buffer, proto$5, { name });
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
|
|
1753
|
+
var proto$4 = {
|
|
1754
|
+
address: String(),
|
|
1755
|
+
reward: Number()
|
|
1756
|
+
};
|
|
1757
|
+
|
|
1758
|
+
class ValidatorMessage extends FormatInterface {
|
|
1759
|
+
get messageName() {
|
|
1760
|
+
return 'ValidatorMessage';
|
|
1761
|
+
}
|
|
1762
|
+
constructor(buffer) {
|
|
1763
|
+
const name = 'validator-message';
|
|
1764
|
+
super(buffer, proto$4, { name });
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
var proto$3 = {
|
|
1769
|
+
index: Number(),
|
|
1770
|
+
previousHash: String(),
|
|
1771
|
+
timestamp: Number(),
|
|
1772
|
+
reward: Number(),
|
|
1773
|
+
fees: Number(),
|
|
1774
|
+
transactions: new Uint8Array(),
|
|
1775
|
+
validators: new Uint8Array()
|
|
1776
|
+
};
|
|
1777
|
+
|
|
1778
|
+
class BlockMessage extends FormatInterface {
|
|
1779
|
+
get messageName() {
|
|
1780
|
+
return 'BlockMessage';
|
|
1781
|
+
}
|
|
1782
|
+
constructor(buffer) {
|
|
1783
|
+
const name = 'block-message';
|
|
1784
|
+
super(buffer, proto$3, { name });
|
|
1785
|
+
}
|
|
1786
|
+
encode() {
|
|
1787
|
+
const decoded = this.decoded;
|
|
1788
|
+
const validators = [];
|
|
1789
|
+
const transactions = [];
|
|
1790
|
+
for (const validator of decoded.validators) {
|
|
1791
|
+
if (validator instanceof ValidatorMessage)
|
|
1792
|
+
validators.push(validator.encoded);
|
|
1793
|
+
else
|
|
1794
|
+
validators.push(new ValidatorMessage(validator).encoded);
|
|
1795
|
+
}
|
|
1796
|
+
for (const transaction of decoded.transactions) {
|
|
1797
|
+
if (transaction instanceof TransactionMessage)
|
|
1798
|
+
transactions.push(transaction.encoded);
|
|
1799
|
+
else
|
|
1800
|
+
transactions.push(new TransactionMessage(transaction).encoded);
|
|
1801
|
+
}
|
|
1802
|
+
return super.encode({
|
|
1803
|
+
...decoded,
|
|
1804
|
+
validators: index$5(validators),
|
|
1805
|
+
transactions: index$5(transactions)
|
|
1806
|
+
});
|
|
1807
|
+
}
|
|
1808
|
+
decode() {
|
|
1809
|
+
super.decode();
|
|
1810
|
+
this.decoded.transactions = index$4(this.decoded.transactions).map(transaction => new TransactionMessage(transaction).decoded);
|
|
1811
|
+
this.decoded.validators = index$4(this.decoded.validators).map(validator => new ValidatorMessage(validator).decoded);
|
|
1812
|
+
return this.decoded;
|
|
1813
|
+
}
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
var proto$2 = {
|
|
1817
|
+
up: Number(),
|
|
1818
|
+
down: Number()
|
|
1819
|
+
};
|
|
1820
|
+
|
|
1821
|
+
class BWMessage extends FormatInterface {
|
|
1822
|
+
get messageName() {
|
|
1823
|
+
return 'BWMessage';
|
|
1824
|
+
}
|
|
1825
|
+
constructor(buffer) {
|
|
1826
|
+
const name = 'bw-message';
|
|
1827
|
+
super(buffer, proto$2, { name });
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
|
|
1831
|
+
var proto$1 = {
|
|
1832
|
+
};
|
|
1833
|
+
|
|
1834
|
+
class BWRequestMessage extends FormatInterface {
|
|
1835
|
+
get messageName() {
|
|
1836
|
+
return 'BWRequestMessage';
|
|
1837
|
+
}
|
|
1838
|
+
constructor(buffer) {
|
|
1839
|
+
const name = 'bw-request-message';
|
|
1840
|
+
super(buffer, proto$1, { name });
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
var proto = {
|
|
1845
|
+
creator: String(),
|
|
1846
|
+
contract: new Uint8Array(),
|
|
1847
|
+
constructorParameters: Array()
|
|
1848
|
+
};
|
|
1849
|
+
|
|
1850
|
+
class ContractMessage extends FormatInterface {
|
|
1851
|
+
get messageName() {
|
|
1852
|
+
return 'ContractMessage';
|
|
1853
|
+
}
|
|
1854
|
+
constructor(buffer) {
|
|
1855
|
+
super(buffer, proto, { name: 'contract-message' });
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
|
|
1859
|
+
export { BlockMessage as B, ContractMessage as C, TransactionMessage as T, ValidatorMessage as V, CodecHash as a, BWMessage as b, BWRequestMessage as c };
|