@solana/web3.js 1.53.0 → 1.55.0

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.
Files changed (39) hide show
  1. package/lib/index.browser.cjs.js +459 -1825
  2. package/lib/index.browser.cjs.js.map +1 -1
  3. package/lib/index.browser.esm.js +455 -1826
  4. package/lib/index.browser.esm.js.map +1 -1
  5. package/lib/index.cjs.js +462 -1847
  6. package/lib/index.cjs.js.map +1 -1
  7. package/lib/index.d.ts +114 -12
  8. package/lib/index.esm.js +458 -1848
  9. package/lib/index.esm.js.map +1 -1
  10. package/lib/index.iife.js +19272 -25771
  11. package/lib/index.iife.js.map +1 -1
  12. package/lib/index.iife.min.js +8 -5
  13. package/lib/index.iife.min.js.map +1 -1
  14. package/lib/index.native.js +459 -1824
  15. package/lib/index.native.js.map +1 -1
  16. package/package.json +5 -7
  17. package/src/account.ts +18 -9
  18. package/src/connection.ts +11 -9
  19. package/src/keypair.ts +19 -24
  20. package/src/layout.ts +7 -0
  21. package/src/message/index.ts +19 -6
  22. package/src/message/legacy.ts +58 -9
  23. package/src/message/v0.ts +324 -0
  24. package/src/message/versioned.ts +36 -0
  25. package/src/programs/ed25519.ts +2 -2
  26. package/src/programs/secp256k1.ts +6 -5
  27. package/src/programs/vote.ts +21 -0
  28. package/src/publickey.ts +14 -73
  29. package/src/transaction/constants.ts +2 -0
  30. package/src/transaction/index.ts +1 -0
  31. package/src/transaction/legacy.ts +3 -5
  32. package/src/transaction/versioned.ts +105 -0
  33. package/src/utils/ed25519.ts +46 -0
  34. package/src/utils/index.ts +1 -0
  35. package/src/utils/makeWebsocketUrl.ts +22 -16
  36. package/src/utils/secp256k1.ts +18 -0
  37. package/src/validator-info.ts +3 -5
  38. package/src/utils/__forks__/react-native/url-impl.ts +0 -2
  39. package/src/utils/url-impl.ts +0 -2
package/lib/index.esm.js CHANGED
@@ -1,1756 +1,67 @@
1
- import nacl from 'tweetnacl';
2
1
  import { Buffer } from 'buffer';
2
+ import { sha512 } from '@noble/hashes/sha512';
3
+ import * as ed25519 from '@noble/ed25519';
3
4
  import BN from 'bn.js';
4
5
  import bs58 from 'bs58';
6
+ import { sha256 } from '@noble/hashes/sha256';
5
7
  import { serialize, deserialize, deserializeUnchecked } from 'borsh';
6
8
  import * as BufferLayout from '@solana/buffer-layout';
7
9
  import { blob } from '@solana/buffer-layout';
8
10
  import { toBigIntLE, toBufferLE } from 'bigint-buffer';
9
- import { coerce, instance, string, tuple, literal, unknown, union, type, optional, any, number, array, nullable, create, boolean, record, assert as assert$7 } from 'superstruct';
11
+ import { coerce, instance, string, tuple, literal, unknown, union, type, optional, any, number, array, nullable, create, boolean, record, assert as assert$1 } from 'superstruct';
10
12
  import { Client } from 'rpc-websockets';
11
13
  import RpcClient from 'jayson/lib/client/browser';
12
14
  import http from 'http';
13
15
  import https from 'https';
14
16
  import * as nodeFetch from 'node-fetch';
15
- import secp256k1 from 'secp256k1';
16
17
  import sha3 from 'js-sha3';
18
+ import { hmac } from '@noble/hashes/hmac';
19
+ import * as secp256k1 from '@noble/secp256k1';
17
20
 
18
- const toBuffer = arr => {
19
- if (Buffer.isBuffer(arr)) {
20
- return arr;
21
- } else if (arr instanceof Uint8Array) {
22
- return Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength);
23
- } else {
24
- return Buffer.from(arr);
25
- }
26
- };
27
-
28
- var hash$1 = {};
29
-
30
- var utils$9 = {};
31
-
32
- var minimalisticAssert = assert$6;
21
+ /**
22
+ * A 64 byte secret key, the first 32 bytes of which is the
23
+ * private scalar and the last 32 bytes is the public key.
24
+ * Read more: https://blog.mozilla.org/warner/2011/11/29/ed25519-keys/
25
+ */
33
26
 
34
- function assert$6(val, msg) {
35
- if (!val)
36
- throw new Error(msg || 'Assertion failed');
37
- }
27
+ ed25519.utils.sha512Sync = (...m) => sha512(ed25519.utils.concatBytes(...m));
38
28
 
39
- assert$6.equal = function assertEqual(l, r, msg) {
40
- if (l != r)
41
- throw new Error(msg || ('Assertion failed: ' + l + ' != ' + r));
29
+ const generatePrivateKey = ed25519.utils.randomPrivateKey;
30
+ const generateKeypair = () => {
31
+ const privateScalar = ed25519.utils.randomPrivateKey();
32
+ const publicKey = getPublicKey(privateScalar);
33
+ const secretKey = new Uint8Array(64);
34
+ secretKey.set(privateScalar);
35
+ secretKey.set(publicKey, 32);
36
+ return {
37
+ publicKey,
38
+ secretKey
39
+ };
42
40
  };
43
-
44
- var inherits$1 = {exports: {}};
45
-
46
- var inherits_browser = {exports: {}};
47
-
48
- var hasRequiredInherits_browser;
49
-
50
- function requireInherits_browser () {
51
- if (hasRequiredInherits_browser) return inherits_browser.exports;
52
- hasRequiredInherits_browser = 1;
53
- if (typeof Object.create === 'function') {
54
- // implementation from standard node.js 'util' module
55
- inherits_browser.exports = function inherits(ctor, superCtor) {
56
- if (superCtor) {
57
- ctor.super_ = superCtor;
58
- ctor.prototype = Object.create(superCtor.prototype, {
59
- constructor: {
60
- value: ctor,
61
- enumerable: false,
62
- writable: true,
63
- configurable: true
64
- }
65
- });
66
- }
67
- };
68
- } else {
69
- // old school shim for old browsers
70
- inherits_browser.exports = function inherits(ctor, superCtor) {
71
- if (superCtor) {
72
- ctor.super_ = superCtor;
73
- var TempCtor = function () {};
74
- TempCtor.prototype = superCtor.prototype;
75
- ctor.prototype = new TempCtor();
76
- ctor.prototype.constructor = ctor;
77
- }
78
- };
79
- }
80
- return inherits_browser.exports;
81
- }
82
-
83
- (function (module) {
84
- try {
85
- var util = require('util');
86
- /* istanbul ignore next */
87
- if (typeof util.inherits !== 'function') throw '';
88
- module.exports = util.inherits;
89
- } catch (e) {
90
- /* istanbul ignore next */
91
- module.exports = requireInherits_browser();
92
- }
93
- } (inherits$1));
94
-
95
- var assert$5 = minimalisticAssert;
96
- var inherits = inherits$1.exports;
97
-
98
- utils$9.inherits = inherits;
99
-
100
- function isSurrogatePair(msg, i) {
101
- if ((msg.charCodeAt(i) & 0xFC00) !== 0xD800) {
102
- return false;
103
- }
104
- if (i < 0 || i + 1 >= msg.length) {
41
+ const getPublicKey = ed25519.sync.getPublicKey;
42
+ function isOnCurve(publicKey) {
43
+ try {
44
+ ed25519.Point.fromHex(publicKey, true
45
+ /* strict */
46
+ );
47
+ return true;
48
+ } catch {
105
49
  return false;
106
50
  }
107
- return (msg.charCodeAt(i + 1) & 0xFC00) === 0xDC00;
108
- }
109
-
110
- function toArray(msg, enc) {
111
- if (Array.isArray(msg))
112
- return msg.slice();
113
- if (!msg)
114
- return [];
115
- var res = [];
116
- if (typeof msg === 'string') {
117
- if (!enc) {
118
- // Inspired by stringToUtf8ByteArray() in closure-library by Google
119
- // https://github.com/google/closure-library/blob/8598d87242af59aac233270742c8984e2b2bdbe0/closure/goog/crypt/crypt.js#L117-L143
120
- // Apache License 2.0
121
- // https://github.com/google/closure-library/blob/master/LICENSE
122
- var p = 0;
123
- for (var i = 0; i < msg.length; i++) {
124
- var c = msg.charCodeAt(i);
125
- if (c < 128) {
126
- res[p++] = c;
127
- } else if (c < 2048) {
128
- res[p++] = (c >> 6) | 192;
129
- res[p++] = (c & 63) | 128;
130
- } else if (isSurrogatePair(msg, i)) {
131
- c = 0x10000 + ((c & 0x03FF) << 10) + (msg.charCodeAt(++i) & 0x03FF);
132
- res[p++] = (c >> 18) | 240;
133
- res[p++] = ((c >> 12) & 63) | 128;
134
- res[p++] = ((c >> 6) & 63) | 128;
135
- res[p++] = (c & 63) | 128;
136
- } else {
137
- res[p++] = (c >> 12) | 224;
138
- res[p++] = ((c >> 6) & 63) | 128;
139
- res[p++] = (c & 63) | 128;
140
- }
141
- }
142
- } else if (enc === 'hex') {
143
- msg = msg.replace(/[^a-z0-9]+/ig, '');
144
- if (msg.length % 2 !== 0)
145
- msg = '0' + msg;
146
- for (i = 0; i < msg.length; i += 2)
147
- res.push(parseInt(msg[i] + msg[i + 1], 16));
148
- }
149
- } else {
150
- for (i = 0; i < msg.length; i++)
151
- res[i] = msg[i] | 0;
152
- }
153
- return res;
154
- }
155
- utils$9.toArray = toArray;
156
-
157
- function toHex(msg) {
158
- var res = '';
159
- for (var i = 0; i < msg.length; i++)
160
- res += zero2(msg[i].toString(16));
161
- return res;
162
- }
163
- utils$9.toHex = toHex;
164
-
165
- function htonl(w) {
166
- var res = (w >>> 24) |
167
- ((w >>> 8) & 0xff00) |
168
- ((w << 8) & 0xff0000) |
169
- ((w & 0xff) << 24);
170
- return res >>> 0;
171
- }
172
- utils$9.htonl = htonl;
173
-
174
- function toHex32(msg, endian) {
175
- var res = '';
176
- for (var i = 0; i < msg.length; i++) {
177
- var w = msg[i];
178
- if (endian === 'little')
179
- w = htonl(w);
180
- res += zero8(w.toString(16));
181
- }
182
- return res;
183
- }
184
- utils$9.toHex32 = toHex32;
185
-
186
- function zero2(word) {
187
- if (word.length === 1)
188
- return '0' + word;
189
- else
190
- return word;
191
- }
192
- utils$9.zero2 = zero2;
193
-
194
- function zero8(word) {
195
- if (word.length === 7)
196
- return '0' + word;
197
- else if (word.length === 6)
198
- return '00' + word;
199
- else if (word.length === 5)
200
- return '000' + word;
201
- else if (word.length === 4)
202
- return '0000' + word;
203
- else if (word.length === 3)
204
- return '00000' + word;
205
- else if (word.length === 2)
206
- return '000000' + word;
207
- else if (word.length === 1)
208
- return '0000000' + word;
209
- else
210
- return word;
211
- }
212
- utils$9.zero8 = zero8;
213
-
214
- function join32(msg, start, end, endian) {
215
- var len = end - start;
216
- assert$5(len % 4 === 0);
217
- var res = new Array(len / 4);
218
- for (var i = 0, k = start; i < res.length; i++, k += 4) {
219
- var w;
220
- if (endian === 'big')
221
- w = (msg[k] << 24) | (msg[k + 1] << 16) | (msg[k + 2] << 8) | msg[k + 3];
222
- else
223
- w = (msg[k + 3] << 24) | (msg[k + 2] << 16) | (msg[k + 1] << 8) | msg[k];
224
- res[i] = w >>> 0;
225
- }
226
- return res;
227
- }
228
- utils$9.join32 = join32;
229
-
230
- function split32(msg, endian) {
231
- var res = new Array(msg.length * 4);
232
- for (var i = 0, k = 0; i < msg.length; i++, k += 4) {
233
- var m = msg[i];
234
- if (endian === 'big') {
235
- res[k] = m >>> 24;
236
- res[k + 1] = (m >>> 16) & 0xff;
237
- res[k + 2] = (m >>> 8) & 0xff;
238
- res[k + 3] = m & 0xff;
239
- } else {
240
- res[k + 3] = m >>> 24;
241
- res[k + 2] = (m >>> 16) & 0xff;
242
- res[k + 1] = (m >>> 8) & 0xff;
243
- res[k] = m & 0xff;
244
- }
245
- }
246
- return res;
247
- }
248
- utils$9.split32 = split32;
249
-
250
- function rotr32$1(w, b) {
251
- return (w >>> b) | (w << (32 - b));
252
- }
253
- utils$9.rotr32 = rotr32$1;
254
-
255
- function rotl32$2(w, b) {
256
- return (w << b) | (w >>> (32 - b));
257
- }
258
- utils$9.rotl32 = rotl32$2;
259
-
260
- function sum32$3(a, b) {
261
- return (a + b) >>> 0;
262
- }
263
- utils$9.sum32 = sum32$3;
264
-
265
- function sum32_3$1(a, b, c) {
266
- return (a + b + c) >>> 0;
267
- }
268
- utils$9.sum32_3 = sum32_3$1;
269
-
270
- function sum32_4$2(a, b, c, d) {
271
- return (a + b + c + d) >>> 0;
272
- }
273
- utils$9.sum32_4 = sum32_4$2;
274
-
275
- function sum32_5$2(a, b, c, d, e) {
276
- return (a + b + c + d + e) >>> 0;
277
- }
278
- utils$9.sum32_5 = sum32_5$2;
279
-
280
- function sum64$1(buf, pos, ah, al) {
281
- var bh = buf[pos];
282
- var bl = buf[pos + 1];
283
-
284
- var lo = (al + bl) >>> 0;
285
- var hi = (lo < al ? 1 : 0) + ah + bh;
286
- buf[pos] = hi >>> 0;
287
- buf[pos + 1] = lo;
288
- }
289
- utils$9.sum64 = sum64$1;
290
-
291
- function sum64_hi$1(ah, al, bh, bl) {
292
- var lo = (al + bl) >>> 0;
293
- var hi = (lo < al ? 1 : 0) + ah + bh;
294
- return hi >>> 0;
295
51
  }
296
- utils$9.sum64_hi = sum64_hi$1;
297
-
298
- function sum64_lo$1(ah, al, bh, bl) {
299
- var lo = al + bl;
300
- return lo >>> 0;
301
- }
302
- utils$9.sum64_lo = sum64_lo$1;
303
-
304
- function sum64_4_hi$1(ah, al, bh, bl, ch, cl, dh, dl) {
305
- var carry = 0;
306
- var lo = al;
307
- lo = (lo + bl) >>> 0;
308
- carry += lo < al ? 1 : 0;
309
- lo = (lo + cl) >>> 0;
310
- carry += lo < cl ? 1 : 0;
311
- lo = (lo + dl) >>> 0;
312
- carry += lo < dl ? 1 : 0;
313
-
314
- var hi = ah + bh + ch + dh + carry;
315
- return hi >>> 0;
316
- }
317
- utils$9.sum64_4_hi = sum64_4_hi$1;
318
-
319
- function sum64_4_lo$1(ah, al, bh, bl, ch, cl, dh, dl) {
320
- var lo = al + bl + cl + dl;
321
- return lo >>> 0;
322
- }
323
- utils$9.sum64_4_lo = sum64_4_lo$1;
324
-
325
- function sum64_5_hi$1(ah, al, bh, bl, ch, cl, dh, dl, eh, el) {
326
- var carry = 0;
327
- var lo = al;
328
- lo = (lo + bl) >>> 0;
329
- carry += lo < al ? 1 : 0;
330
- lo = (lo + cl) >>> 0;
331
- carry += lo < cl ? 1 : 0;
332
- lo = (lo + dl) >>> 0;
333
- carry += lo < dl ? 1 : 0;
334
- lo = (lo + el) >>> 0;
335
- carry += lo < el ? 1 : 0;
336
-
337
- var hi = ah + bh + ch + dh + eh + carry;
338
- return hi >>> 0;
339
- }
340
- utils$9.sum64_5_hi = sum64_5_hi$1;
341
-
342
- function sum64_5_lo$1(ah, al, bh, bl, ch, cl, dh, dl, eh, el) {
343
- var lo = al + bl + cl + dl + el;
344
-
345
- return lo >>> 0;
346
- }
347
- utils$9.sum64_5_lo = sum64_5_lo$1;
348
-
349
- function rotr64_hi$1(ah, al, num) {
350
- var r = (al << (32 - num)) | (ah >>> num);
351
- return r >>> 0;
352
- }
353
- utils$9.rotr64_hi = rotr64_hi$1;
354
-
355
- function rotr64_lo$1(ah, al, num) {
356
- var r = (ah << (32 - num)) | (al >>> num);
357
- return r >>> 0;
358
- }
359
- utils$9.rotr64_lo = rotr64_lo$1;
360
-
361
- function shr64_hi$1(ah, al, num) {
362
- return ah >>> num;
363
- }
364
- utils$9.shr64_hi = shr64_hi$1;
365
-
366
- function shr64_lo$1(ah, al, num) {
367
- var r = (ah << (32 - num)) | (al >>> num);
368
- return r >>> 0;
369
- }
370
- utils$9.shr64_lo = shr64_lo$1;
371
-
372
- var common$5 = {};
373
-
374
- var utils$8 = utils$9;
375
- var assert$4 = minimalisticAssert;
376
-
377
- function BlockHash$4() {
378
- this.pending = null;
379
- this.pendingTotal = 0;
380
- this.blockSize = this.constructor.blockSize;
381
- this.outSize = this.constructor.outSize;
382
- this.hmacStrength = this.constructor.hmacStrength;
383
- this.padLength = this.constructor.padLength / 8;
384
- this.endian = 'big';
385
-
386
- this._delta8 = this.blockSize / 8;
387
- this._delta32 = this.blockSize / 32;
388
- }
389
- common$5.BlockHash = BlockHash$4;
390
-
391
- BlockHash$4.prototype.update = function update(msg, enc) {
392
- // Convert message to array, pad it, and join into 32bit blocks
393
- msg = utils$8.toArray(msg, enc);
394
- if (!this.pending)
395
- this.pending = msg;
396
- else
397
- this.pending = this.pending.concat(msg);
398
- this.pendingTotal += msg.length;
52
+ const sign = (message, secretKey) => ed25519.sync.sign(message, secretKey.slice(0, 32));
53
+ const verify = ed25519.sync.verify;
399
54
 
400
- // Enough data, try updating
401
- if (this.pending.length >= this._delta8) {
402
- msg = this.pending;
403
-
404
- // Process pending data in blocks
405
- var r = msg.length % this._delta8;
406
- this.pending = msg.slice(msg.length - r, msg.length);
407
- if (this.pending.length === 0)
408
- this.pending = null;
409
-
410
- msg = utils$8.join32(msg, 0, msg.length - r, this.endian);
411
- for (var i = 0; i < msg.length; i += this._delta32)
412
- this._update(msg, i, i + this._delta32);
413
- }
414
-
415
- return this;
416
- };
417
-
418
- BlockHash$4.prototype.digest = function digest(enc) {
419
- this.update(this._pad());
420
- assert$4(this.pending === null);
421
-
422
- return this._digest(enc);
423
- };
424
-
425
- BlockHash$4.prototype._pad = function pad() {
426
- var len = this.pendingTotal;
427
- var bytes = this._delta8;
428
- var k = bytes - ((len + this.padLength) % bytes);
429
- var res = new Array(k + this.padLength);
430
- res[0] = 0x80;
431
- for (var i = 1; i < k; i++)
432
- res[i] = 0;
433
-
434
- // Append length
435
- len <<= 3;
436
- if (this.endian === 'big') {
437
- for (var t = 8; t < this.padLength; t++)
438
- res[i++] = 0;
439
-
440
- res[i++] = 0;
441
- res[i++] = 0;
442
- res[i++] = 0;
443
- res[i++] = 0;
444
- res[i++] = (len >>> 24) & 0xff;
445
- res[i++] = (len >>> 16) & 0xff;
446
- res[i++] = (len >>> 8) & 0xff;
447
- res[i++] = len & 0xff;
55
+ const toBuffer = arr => {
56
+ if (Buffer.isBuffer(arr)) {
57
+ return arr;
58
+ } else if (arr instanceof Uint8Array) {
59
+ return Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength);
448
60
  } else {
449
- res[i++] = len & 0xff;
450
- res[i++] = (len >>> 8) & 0xff;
451
- res[i++] = (len >>> 16) & 0xff;
452
- res[i++] = (len >>> 24) & 0xff;
453
- res[i++] = 0;
454
- res[i++] = 0;
455
- res[i++] = 0;
456
- res[i++] = 0;
457
-
458
- for (t = 8; t < this.padLength; t++)
459
- res[i++] = 0;
460
- }
461
-
462
- return res;
463
- };
464
-
465
- var sha = {};
466
-
467
- var common$4 = {};
468
-
469
- var utils$7 = utils$9;
470
- var rotr32 = utils$7.rotr32;
471
-
472
- function ft_1$1(s, x, y, z) {
473
- if (s === 0)
474
- return ch32$1(x, y, z);
475
- if (s === 1 || s === 3)
476
- return p32(x, y, z);
477
- if (s === 2)
478
- return maj32$1(x, y, z);
479
- }
480
- common$4.ft_1 = ft_1$1;
481
-
482
- function ch32$1(x, y, z) {
483
- return (x & y) ^ ((~x) & z);
484
- }
485
- common$4.ch32 = ch32$1;
486
-
487
- function maj32$1(x, y, z) {
488
- return (x & y) ^ (x & z) ^ (y & z);
489
- }
490
- common$4.maj32 = maj32$1;
491
-
492
- function p32(x, y, z) {
493
- return x ^ y ^ z;
494
- }
495
- common$4.p32 = p32;
496
-
497
- function s0_256$1(x) {
498
- return rotr32(x, 2) ^ rotr32(x, 13) ^ rotr32(x, 22);
499
- }
500
- common$4.s0_256 = s0_256$1;
501
-
502
- function s1_256$1(x) {
503
- return rotr32(x, 6) ^ rotr32(x, 11) ^ rotr32(x, 25);
504
- }
505
- common$4.s1_256 = s1_256$1;
506
-
507
- function g0_256$1(x) {
508
- return rotr32(x, 7) ^ rotr32(x, 18) ^ (x >>> 3);
509
- }
510
- common$4.g0_256 = g0_256$1;
511
-
512
- function g1_256$1(x) {
513
- return rotr32(x, 17) ^ rotr32(x, 19) ^ (x >>> 10);
514
- }
515
- common$4.g1_256 = g1_256$1;
516
-
517
- var utils$6 = utils$9;
518
- var common$3 = common$5;
519
- var shaCommon$1 = common$4;
520
-
521
- var rotl32$1 = utils$6.rotl32;
522
- var sum32$2 = utils$6.sum32;
523
- var sum32_5$1 = utils$6.sum32_5;
524
- var ft_1 = shaCommon$1.ft_1;
525
- var BlockHash$3 = common$3.BlockHash;
526
-
527
- var sha1_K = [
528
- 0x5A827999, 0x6ED9EBA1,
529
- 0x8F1BBCDC, 0xCA62C1D6
530
- ];
531
-
532
- function SHA1() {
533
- if (!(this instanceof SHA1))
534
- return new SHA1();
535
-
536
- BlockHash$3.call(this);
537
- this.h = [
538
- 0x67452301, 0xefcdab89, 0x98badcfe,
539
- 0x10325476, 0xc3d2e1f0 ];
540
- this.W = new Array(80);
541
- }
542
-
543
- utils$6.inherits(SHA1, BlockHash$3);
544
- var _1 = SHA1;
545
-
546
- SHA1.blockSize = 512;
547
- SHA1.outSize = 160;
548
- SHA1.hmacStrength = 80;
549
- SHA1.padLength = 64;
550
-
551
- SHA1.prototype._update = function _update(msg, start) {
552
- var W = this.W;
553
-
554
- for (var i = 0; i < 16; i++)
555
- W[i] = msg[start + i];
556
-
557
- for(; i < W.length; i++)
558
- W[i] = rotl32$1(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
559
-
560
- var a = this.h[0];
561
- var b = this.h[1];
562
- var c = this.h[2];
563
- var d = this.h[3];
564
- var e = this.h[4];
565
-
566
- for (i = 0; i < W.length; i++) {
567
- var s = ~~(i / 20);
568
- var t = sum32_5$1(rotl32$1(a, 5), ft_1(s, b, c, d), e, W[i], sha1_K[s]);
569
- e = d;
570
- d = c;
571
- c = rotl32$1(b, 30);
572
- b = a;
573
- a = t;
574
- }
575
-
576
- this.h[0] = sum32$2(this.h[0], a);
577
- this.h[1] = sum32$2(this.h[1], b);
578
- this.h[2] = sum32$2(this.h[2], c);
579
- this.h[3] = sum32$2(this.h[3], d);
580
- this.h[4] = sum32$2(this.h[4], e);
581
- };
582
-
583
- SHA1.prototype._digest = function digest(enc) {
584
- if (enc === 'hex')
585
- return utils$6.toHex32(this.h, 'big');
586
- else
587
- return utils$6.split32(this.h, 'big');
588
- };
589
-
590
- var utils$5 = utils$9;
591
- var common$2 = common$5;
592
- var shaCommon = common$4;
593
- var assert$3 = minimalisticAssert;
594
-
595
- var sum32$1 = utils$5.sum32;
596
- var sum32_4$1 = utils$5.sum32_4;
597
- var sum32_5 = utils$5.sum32_5;
598
- var ch32 = shaCommon.ch32;
599
- var maj32 = shaCommon.maj32;
600
- var s0_256 = shaCommon.s0_256;
601
- var s1_256 = shaCommon.s1_256;
602
- var g0_256 = shaCommon.g0_256;
603
- var g1_256 = shaCommon.g1_256;
604
-
605
- var BlockHash$2 = common$2.BlockHash;
606
-
607
- var sha256_K = [
608
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
609
- 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
610
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
611
- 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
612
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
613
- 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
614
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
615
- 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
616
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
617
- 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
618
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
619
- 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
620
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
621
- 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
622
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
623
- 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
624
- ];
625
-
626
- function SHA256$1() {
627
- if (!(this instanceof SHA256$1))
628
- return new SHA256$1();
629
-
630
- BlockHash$2.call(this);
631
- this.h = [
632
- 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
633
- 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
634
- ];
635
- this.k = sha256_K;
636
- this.W = new Array(64);
637
- }
638
- utils$5.inherits(SHA256$1, BlockHash$2);
639
- var _256 = SHA256$1;
640
-
641
- SHA256$1.blockSize = 512;
642
- SHA256$1.outSize = 256;
643
- SHA256$1.hmacStrength = 192;
644
- SHA256$1.padLength = 64;
645
-
646
- SHA256$1.prototype._update = function _update(msg, start) {
647
- var W = this.W;
648
-
649
- for (var i = 0; i < 16; i++)
650
- W[i] = msg[start + i];
651
- for (; i < W.length; i++)
652
- W[i] = sum32_4$1(g1_256(W[i - 2]), W[i - 7], g0_256(W[i - 15]), W[i - 16]);
653
-
654
- var a = this.h[0];
655
- var b = this.h[1];
656
- var c = this.h[2];
657
- var d = this.h[3];
658
- var e = this.h[4];
659
- var f = this.h[5];
660
- var g = this.h[6];
661
- var h = this.h[7];
662
-
663
- assert$3(this.k.length === W.length);
664
- for (i = 0; i < W.length; i++) {
665
- var T1 = sum32_5(h, s1_256(e), ch32(e, f, g), this.k[i], W[i]);
666
- var T2 = sum32$1(s0_256(a), maj32(a, b, c));
667
- h = g;
668
- g = f;
669
- f = e;
670
- e = sum32$1(d, T1);
671
- d = c;
672
- c = b;
673
- b = a;
674
- a = sum32$1(T1, T2);
675
- }
676
-
677
- this.h[0] = sum32$1(this.h[0], a);
678
- this.h[1] = sum32$1(this.h[1], b);
679
- this.h[2] = sum32$1(this.h[2], c);
680
- this.h[3] = sum32$1(this.h[3], d);
681
- this.h[4] = sum32$1(this.h[4], e);
682
- this.h[5] = sum32$1(this.h[5], f);
683
- this.h[6] = sum32$1(this.h[6], g);
684
- this.h[7] = sum32$1(this.h[7], h);
685
- };
686
-
687
- SHA256$1.prototype._digest = function digest(enc) {
688
- if (enc === 'hex')
689
- return utils$5.toHex32(this.h, 'big');
690
- else
691
- return utils$5.split32(this.h, 'big');
692
- };
693
-
694
- var utils$4 = utils$9;
695
- var SHA256 = _256;
696
-
697
- function SHA224() {
698
- if (!(this instanceof SHA224))
699
- return new SHA224();
700
-
701
- SHA256.call(this);
702
- this.h = [
703
- 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
704
- 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4 ];
705
- }
706
- utils$4.inherits(SHA224, SHA256);
707
- var _224 = SHA224;
708
-
709
- SHA224.blockSize = 512;
710
- SHA224.outSize = 224;
711
- SHA224.hmacStrength = 192;
712
- SHA224.padLength = 64;
713
-
714
- SHA224.prototype._digest = function digest(enc) {
715
- // Just truncate output
716
- if (enc === 'hex')
717
- return utils$4.toHex32(this.h.slice(0, 7), 'big');
718
- else
719
- return utils$4.split32(this.h.slice(0, 7), 'big');
720
- };
721
-
722
- var utils$3 = utils$9;
723
- var common$1 = common$5;
724
- var assert$2 = minimalisticAssert;
725
-
726
- var rotr64_hi = utils$3.rotr64_hi;
727
- var rotr64_lo = utils$3.rotr64_lo;
728
- var shr64_hi = utils$3.shr64_hi;
729
- var shr64_lo = utils$3.shr64_lo;
730
- var sum64 = utils$3.sum64;
731
- var sum64_hi = utils$3.sum64_hi;
732
- var sum64_lo = utils$3.sum64_lo;
733
- var sum64_4_hi = utils$3.sum64_4_hi;
734
- var sum64_4_lo = utils$3.sum64_4_lo;
735
- var sum64_5_hi = utils$3.sum64_5_hi;
736
- var sum64_5_lo = utils$3.sum64_5_lo;
737
-
738
- var BlockHash$1 = common$1.BlockHash;
739
-
740
- var sha512_K = [
741
- 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
742
- 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
743
- 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
744
- 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
745
- 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
746
- 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
747
- 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
748
- 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
749
- 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
750
- 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
751
- 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
752
- 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
753
- 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
754
- 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
755
- 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
756
- 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
757
- 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
758
- 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
759
- 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
760
- 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
761
- 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
762
- 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
763
- 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
764
- 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
765
- 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
766
- 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
767
- 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
768
- 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
769
- 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
770
- 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
771
- 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
772
- 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
773
- 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
774
- 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
775
- 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
776
- 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
777
- 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
778
- 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
779
- 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
780
- 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
781
- ];
782
-
783
- function SHA512$1() {
784
- if (!(this instanceof SHA512$1))
785
- return new SHA512$1();
786
-
787
- BlockHash$1.call(this);
788
- this.h = [
789
- 0x6a09e667, 0xf3bcc908,
790
- 0xbb67ae85, 0x84caa73b,
791
- 0x3c6ef372, 0xfe94f82b,
792
- 0xa54ff53a, 0x5f1d36f1,
793
- 0x510e527f, 0xade682d1,
794
- 0x9b05688c, 0x2b3e6c1f,
795
- 0x1f83d9ab, 0xfb41bd6b,
796
- 0x5be0cd19, 0x137e2179 ];
797
- this.k = sha512_K;
798
- this.W = new Array(160);
799
- }
800
- utils$3.inherits(SHA512$1, BlockHash$1);
801
- var _512 = SHA512$1;
802
-
803
- SHA512$1.blockSize = 1024;
804
- SHA512$1.outSize = 512;
805
- SHA512$1.hmacStrength = 192;
806
- SHA512$1.padLength = 128;
807
-
808
- SHA512$1.prototype._prepareBlock = function _prepareBlock(msg, start) {
809
- var W = this.W;
810
-
811
- // 32 x 32bit words
812
- for (var i = 0; i < 32; i++)
813
- W[i] = msg[start + i];
814
- for (; i < W.length; i += 2) {
815
- var c0_hi = g1_512_hi(W[i - 4], W[i - 3]); // i - 2
816
- var c0_lo = g1_512_lo(W[i - 4], W[i - 3]);
817
- var c1_hi = W[i - 14]; // i - 7
818
- var c1_lo = W[i - 13];
819
- var c2_hi = g0_512_hi(W[i - 30], W[i - 29]); // i - 15
820
- var c2_lo = g0_512_lo(W[i - 30], W[i - 29]);
821
- var c3_hi = W[i - 32]; // i - 16
822
- var c3_lo = W[i - 31];
823
-
824
- W[i] = sum64_4_hi(
825
- c0_hi, c0_lo,
826
- c1_hi, c1_lo,
827
- c2_hi, c2_lo,
828
- c3_hi, c3_lo);
829
- W[i + 1] = sum64_4_lo(
830
- c0_hi, c0_lo,
831
- c1_hi, c1_lo,
832
- c2_hi, c2_lo,
833
- c3_hi, c3_lo);
61
+ return Buffer.from(arr);
834
62
  }
835
63
  };
836
64
 
837
- SHA512$1.prototype._update = function _update(msg, start) {
838
- this._prepareBlock(msg, start);
839
-
840
- var W = this.W;
841
-
842
- var ah = this.h[0];
843
- var al = this.h[1];
844
- var bh = this.h[2];
845
- var bl = this.h[3];
846
- var ch = this.h[4];
847
- var cl = this.h[5];
848
- var dh = this.h[6];
849
- var dl = this.h[7];
850
- var eh = this.h[8];
851
- var el = this.h[9];
852
- var fh = this.h[10];
853
- var fl = this.h[11];
854
- var gh = this.h[12];
855
- var gl = this.h[13];
856
- var hh = this.h[14];
857
- var hl = this.h[15];
858
-
859
- assert$2(this.k.length === W.length);
860
- for (var i = 0; i < W.length; i += 2) {
861
- var c0_hi = hh;
862
- var c0_lo = hl;
863
- var c1_hi = s1_512_hi(eh, el);
864
- var c1_lo = s1_512_lo(eh, el);
865
- var c2_hi = ch64_hi(eh, el, fh, fl, gh);
866
- var c2_lo = ch64_lo(eh, el, fh, fl, gh, gl);
867
- var c3_hi = this.k[i];
868
- var c3_lo = this.k[i + 1];
869
- var c4_hi = W[i];
870
- var c4_lo = W[i + 1];
871
-
872
- var T1_hi = sum64_5_hi(
873
- c0_hi, c0_lo,
874
- c1_hi, c1_lo,
875
- c2_hi, c2_lo,
876
- c3_hi, c3_lo,
877
- c4_hi, c4_lo);
878
- var T1_lo = sum64_5_lo(
879
- c0_hi, c0_lo,
880
- c1_hi, c1_lo,
881
- c2_hi, c2_lo,
882
- c3_hi, c3_lo,
883
- c4_hi, c4_lo);
884
-
885
- c0_hi = s0_512_hi(ah, al);
886
- c0_lo = s0_512_lo(ah, al);
887
- c1_hi = maj64_hi(ah, al, bh, bl, ch);
888
- c1_lo = maj64_lo(ah, al, bh, bl, ch, cl);
889
-
890
- var T2_hi = sum64_hi(c0_hi, c0_lo, c1_hi, c1_lo);
891
- var T2_lo = sum64_lo(c0_hi, c0_lo, c1_hi, c1_lo);
892
-
893
- hh = gh;
894
- hl = gl;
895
-
896
- gh = fh;
897
- gl = fl;
898
-
899
- fh = eh;
900
- fl = el;
901
-
902
- eh = sum64_hi(dh, dl, T1_hi, T1_lo);
903
- el = sum64_lo(dl, dl, T1_hi, T1_lo);
904
-
905
- dh = ch;
906
- dl = cl;
907
-
908
- ch = bh;
909
- cl = bl;
910
-
911
- bh = ah;
912
- bl = al;
913
-
914
- ah = sum64_hi(T1_hi, T1_lo, T2_hi, T2_lo);
915
- al = sum64_lo(T1_hi, T1_lo, T2_hi, T2_lo);
916
- }
917
-
918
- sum64(this.h, 0, ah, al);
919
- sum64(this.h, 2, bh, bl);
920
- sum64(this.h, 4, ch, cl);
921
- sum64(this.h, 6, dh, dl);
922
- sum64(this.h, 8, eh, el);
923
- sum64(this.h, 10, fh, fl);
924
- sum64(this.h, 12, gh, gl);
925
- sum64(this.h, 14, hh, hl);
926
- };
927
-
928
- SHA512$1.prototype._digest = function digest(enc) {
929
- if (enc === 'hex')
930
- return utils$3.toHex32(this.h, 'big');
931
- else
932
- return utils$3.split32(this.h, 'big');
933
- };
934
-
935
- function ch64_hi(xh, xl, yh, yl, zh) {
936
- var r = (xh & yh) ^ ((~xh) & zh);
937
- if (r < 0)
938
- r += 0x100000000;
939
- return r;
940
- }
941
-
942
- function ch64_lo(xh, xl, yh, yl, zh, zl) {
943
- var r = (xl & yl) ^ ((~xl) & zl);
944
- if (r < 0)
945
- r += 0x100000000;
946
- return r;
947
- }
948
-
949
- function maj64_hi(xh, xl, yh, yl, zh) {
950
- var r = (xh & yh) ^ (xh & zh) ^ (yh & zh);
951
- if (r < 0)
952
- r += 0x100000000;
953
- return r;
954
- }
955
-
956
- function maj64_lo(xh, xl, yh, yl, zh, zl) {
957
- var r = (xl & yl) ^ (xl & zl) ^ (yl & zl);
958
- if (r < 0)
959
- r += 0x100000000;
960
- return r;
961
- }
962
-
963
- function s0_512_hi(xh, xl) {
964
- var c0_hi = rotr64_hi(xh, xl, 28);
965
- var c1_hi = rotr64_hi(xl, xh, 2); // 34
966
- var c2_hi = rotr64_hi(xl, xh, 7); // 39
967
-
968
- var r = c0_hi ^ c1_hi ^ c2_hi;
969
- if (r < 0)
970
- r += 0x100000000;
971
- return r;
972
- }
973
-
974
- function s0_512_lo(xh, xl) {
975
- var c0_lo = rotr64_lo(xh, xl, 28);
976
- var c1_lo = rotr64_lo(xl, xh, 2); // 34
977
- var c2_lo = rotr64_lo(xl, xh, 7); // 39
978
-
979
- var r = c0_lo ^ c1_lo ^ c2_lo;
980
- if (r < 0)
981
- r += 0x100000000;
982
- return r;
983
- }
984
-
985
- function s1_512_hi(xh, xl) {
986
- var c0_hi = rotr64_hi(xh, xl, 14);
987
- var c1_hi = rotr64_hi(xh, xl, 18);
988
- var c2_hi = rotr64_hi(xl, xh, 9); // 41
989
-
990
- var r = c0_hi ^ c1_hi ^ c2_hi;
991
- if (r < 0)
992
- r += 0x100000000;
993
- return r;
994
- }
995
-
996
- function s1_512_lo(xh, xl) {
997
- var c0_lo = rotr64_lo(xh, xl, 14);
998
- var c1_lo = rotr64_lo(xh, xl, 18);
999
- var c2_lo = rotr64_lo(xl, xh, 9); // 41
1000
-
1001
- var r = c0_lo ^ c1_lo ^ c2_lo;
1002
- if (r < 0)
1003
- r += 0x100000000;
1004
- return r;
1005
- }
1006
-
1007
- function g0_512_hi(xh, xl) {
1008
- var c0_hi = rotr64_hi(xh, xl, 1);
1009
- var c1_hi = rotr64_hi(xh, xl, 8);
1010
- var c2_hi = shr64_hi(xh, xl, 7);
1011
-
1012
- var r = c0_hi ^ c1_hi ^ c2_hi;
1013
- if (r < 0)
1014
- r += 0x100000000;
1015
- return r;
1016
- }
1017
-
1018
- function g0_512_lo(xh, xl) {
1019
- var c0_lo = rotr64_lo(xh, xl, 1);
1020
- var c1_lo = rotr64_lo(xh, xl, 8);
1021
- var c2_lo = shr64_lo(xh, xl, 7);
1022
-
1023
- var r = c0_lo ^ c1_lo ^ c2_lo;
1024
- if (r < 0)
1025
- r += 0x100000000;
1026
- return r;
1027
- }
1028
-
1029
- function g1_512_hi(xh, xl) {
1030
- var c0_hi = rotr64_hi(xh, xl, 19);
1031
- var c1_hi = rotr64_hi(xl, xh, 29); // 61
1032
- var c2_hi = shr64_hi(xh, xl, 6);
1033
-
1034
- var r = c0_hi ^ c1_hi ^ c2_hi;
1035
- if (r < 0)
1036
- r += 0x100000000;
1037
- return r;
1038
- }
1039
-
1040
- function g1_512_lo(xh, xl) {
1041
- var c0_lo = rotr64_lo(xh, xl, 19);
1042
- var c1_lo = rotr64_lo(xl, xh, 29); // 61
1043
- var c2_lo = shr64_lo(xh, xl, 6);
1044
-
1045
- var r = c0_lo ^ c1_lo ^ c2_lo;
1046
- if (r < 0)
1047
- r += 0x100000000;
1048
- return r;
1049
- }
1050
-
1051
- var utils$2 = utils$9;
1052
-
1053
- var SHA512 = _512;
1054
-
1055
- function SHA384() {
1056
- if (!(this instanceof SHA384))
1057
- return new SHA384();
1058
-
1059
- SHA512.call(this);
1060
- this.h = [
1061
- 0xcbbb9d5d, 0xc1059ed8,
1062
- 0x629a292a, 0x367cd507,
1063
- 0x9159015a, 0x3070dd17,
1064
- 0x152fecd8, 0xf70e5939,
1065
- 0x67332667, 0xffc00b31,
1066
- 0x8eb44a87, 0x68581511,
1067
- 0xdb0c2e0d, 0x64f98fa7,
1068
- 0x47b5481d, 0xbefa4fa4 ];
1069
- }
1070
- utils$2.inherits(SHA384, SHA512);
1071
- var _384 = SHA384;
1072
-
1073
- SHA384.blockSize = 1024;
1074
- SHA384.outSize = 384;
1075
- SHA384.hmacStrength = 192;
1076
- SHA384.padLength = 128;
1077
-
1078
- SHA384.prototype._digest = function digest(enc) {
1079
- if (enc === 'hex')
1080
- return utils$2.toHex32(this.h.slice(0, 12), 'big');
1081
- else
1082
- return utils$2.split32(this.h.slice(0, 12), 'big');
1083
- };
1084
-
1085
- sha.sha1 = _1;
1086
- sha.sha224 = _224;
1087
- sha.sha256 = _256;
1088
- sha.sha384 = _384;
1089
- sha.sha512 = _512;
1090
-
1091
- var ripemd = {};
1092
-
1093
- var utils$1 = utils$9;
1094
- var common = common$5;
1095
-
1096
- var rotl32 = utils$1.rotl32;
1097
- var sum32 = utils$1.sum32;
1098
- var sum32_3 = utils$1.sum32_3;
1099
- var sum32_4 = utils$1.sum32_4;
1100
- var BlockHash = common.BlockHash;
1101
-
1102
- function RIPEMD160() {
1103
- if (!(this instanceof RIPEMD160))
1104
- return new RIPEMD160();
1105
-
1106
- BlockHash.call(this);
1107
-
1108
- this.h = [ 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 ];
1109
- this.endian = 'little';
1110
- }
1111
- utils$1.inherits(RIPEMD160, BlockHash);
1112
- ripemd.ripemd160 = RIPEMD160;
1113
-
1114
- RIPEMD160.blockSize = 512;
1115
- RIPEMD160.outSize = 160;
1116
- RIPEMD160.hmacStrength = 192;
1117
- RIPEMD160.padLength = 64;
1118
-
1119
- RIPEMD160.prototype._update = function update(msg, start) {
1120
- var A = this.h[0];
1121
- var B = this.h[1];
1122
- var C = this.h[2];
1123
- var D = this.h[3];
1124
- var E = this.h[4];
1125
- var Ah = A;
1126
- var Bh = B;
1127
- var Ch = C;
1128
- var Dh = D;
1129
- var Eh = E;
1130
- for (var j = 0; j < 80; j++) {
1131
- var T = sum32(
1132
- rotl32(
1133
- sum32_4(A, f(j, B, C, D), msg[r[j] + start], K(j)),
1134
- s[j]),
1135
- E);
1136
- A = E;
1137
- E = D;
1138
- D = rotl32(C, 10);
1139
- C = B;
1140
- B = T;
1141
- T = sum32(
1142
- rotl32(
1143
- sum32_4(Ah, f(79 - j, Bh, Ch, Dh), msg[rh[j] + start], Kh(j)),
1144
- sh[j]),
1145
- Eh);
1146
- Ah = Eh;
1147
- Eh = Dh;
1148
- Dh = rotl32(Ch, 10);
1149
- Ch = Bh;
1150
- Bh = T;
1151
- }
1152
- T = sum32_3(this.h[1], C, Dh);
1153
- this.h[1] = sum32_3(this.h[2], D, Eh);
1154
- this.h[2] = sum32_3(this.h[3], E, Ah);
1155
- this.h[3] = sum32_3(this.h[4], A, Bh);
1156
- this.h[4] = sum32_3(this.h[0], B, Ch);
1157
- this.h[0] = T;
1158
- };
1159
-
1160
- RIPEMD160.prototype._digest = function digest(enc) {
1161
- if (enc === 'hex')
1162
- return utils$1.toHex32(this.h, 'little');
1163
- else
1164
- return utils$1.split32(this.h, 'little');
1165
- };
1166
-
1167
- function f(j, x, y, z) {
1168
- if (j <= 15)
1169
- return x ^ y ^ z;
1170
- else if (j <= 31)
1171
- return (x & y) | ((~x) & z);
1172
- else if (j <= 47)
1173
- return (x | (~y)) ^ z;
1174
- else if (j <= 63)
1175
- return (x & z) | (y & (~z));
1176
- else
1177
- return x ^ (y | (~z));
1178
- }
1179
-
1180
- function K(j) {
1181
- if (j <= 15)
1182
- return 0x00000000;
1183
- else if (j <= 31)
1184
- return 0x5a827999;
1185
- else if (j <= 47)
1186
- return 0x6ed9eba1;
1187
- else if (j <= 63)
1188
- return 0x8f1bbcdc;
1189
- else
1190
- return 0xa953fd4e;
1191
- }
1192
-
1193
- function Kh(j) {
1194
- if (j <= 15)
1195
- return 0x50a28be6;
1196
- else if (j <= 31)
1197
- return 0x5c4dd124;
1198
- else if (j <= 47)
1199
- return 0x6d703ef3;
1200
- else if (j <= 63)
1201
- return 0x7a6d76e9;
1202
- else
1203
- return 0x00000000;
1204
- }
1205
-
1206
- var r = [
1207
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1208
- 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
1209
- 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
1210
- 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
1211
- 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
1212
- ];
1213
-
1214
- var rh = [
1215
- 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
1216
- 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
1217
- 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
1218
- 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
1219
- 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
1220
- ];
1221
-
1222
- var s = [
1223
- 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
1224
- 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
1225
- 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
1226
- 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
1227
- 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
1228
- ];
1229
-
1230
- var sh = [
1231
- 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
1232
- 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
1233
- 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
1234
- 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
1235
- 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
1236
- ];
1237
-
1238
- var utils = utils$9;
1239
- var assert$1 = minimalisticAssert;
1240
-
1241
- function Hmac(hash, key, enc) {
1242
- if (!(this instanceof Hmac))
1243
- return new Hmac(hash, key, enc);
1244
- this.Hash = hash;
1245
- this.blockSize = hash.blockSize / 8;
1246
- this.outSize = hash.outSize / 8;
1247
- this.inner = null;
1248
- this.outer = null;
1249
-
1250
- this._init(utils.toArray(key, enc));
1251
- }
1252
- var hmac = Hmac;
1253
-
1254
- Hmac.prototype._init = function init(key) {
1255
- // Shorten key, if needed
1256
- if (key.length > this.blockSize)
1257
- key = new this.Hash().update(key).digest();
1258
- assert$1(key.length <= this.blockSize);
1259
-
1260
- // Add padding to key
1261
- for (var i = key.length; i < this.blockSize; i++)
1262
- key.push(0);
1263
-
1264
- for (i = 0; i < key.length; i++)
1265
- key[i] ^= 0x36;
1266
- this.inner = new this.Hash().update(key);
1267
-
1268
- // 0x36 ^ 0x5c = 0x6a
1269
- for (i = 0; i < key.length; i++)
1270
- key[i] ^= 0x6a;
1271
- this.outer = new this.Hash().update(key);
1272
- };
1273
-
1274
- Hmac.prototype.update = function update(msg, enc) {
1275
- this.inner.update(msg, enc);
1276
- return this;
1277
- };
1278
-
1279
- Hmac.prototype.digest = function digest(enc) {
1280
- this.outer.update(this.inner.digest());
1281
- return this.outer.digest(enc);
1282
- };
1283
-
1284
- (function (exports) {
1285
- var hash = exports;
1286
-
1287
- hash.utils = utils$9;
1288
- hash.common = common$5;
1289
- hash.sha = sha;
1290
- hash.ripemd = ripemd;
1291
- hash.hmac = hmac;
1292
-
1293
- // Proxy hash functions to the main object
1294
- hash.sha1 = hash.sha.sha1;
1295
- hash.sha256 = hash.sha.sha256;
1296
- hash.sha224 = hash.sha.sha224;
1297
- hash.sha384 = hash.sha.sha384;
1298
- hash.sha512 = hash.sha.sha512;
1299
- hash.ripemd160 = hash.ripemd.ripemd160;
1300
- } (hash$1));
1301
-
1302
- var hash = hash$1;
1303
-
1304
- const version$2 = "logger/5.6.0";
1305
-
1306
- let _permanentCensorErrors = false;
1307
- let _censorErrors = false;
1308
- const LogLevels = { debug: 1, "default": 2, info: 2, warning: 3, error: 4, off: 5 };
1309
- let _logLevel = LogLevels["default"];
1310
- let _globalLogger = null;
1311
- function _checkNormalize() {
1312
- try {
1313
- const missing = [];
1314
- // Make sure all forms of normalization are supported
1315
- ["NFD", "NFC", "NFKD", "NFKC"].forEach((form) => {
1316
- try {
1317
- if ("test".normalize(form) !== "test") {
1318
- throw new Error("bad normalize");
1319
- }
1320
- ;
1321
- }
1322
- catch (error) {
1323
- missing.push(form);
1324
- }
1325
- });
1326
- if (missing.length) {
1327
- throw new Error("missing " + missing.join(", "));
1328
- }
1329
- if (String.fromCharCode(0xe9).normalize("NFD") !== String.fromCharCode(0x65, 0x0301)) {
1330
- throw new Error("broken implementation");
1331
- }
1332
- }
1333
- catch (error) {
1334
- return error.message;
1335
- }
1336
- return null;
1337
- }
1338
- const _normalizeError = _checkNormalize();
1339
- var LogLevel;
1340
- (function (LogLevel) {
1341
- LogLevel["DEBUG"] = "DEBUG";
1342
- LogLevel["INFO"] = "INFO";
1343
- LogLevel["WARNING"] = "WARNING";
1344
- LogLevel["ERROR"] = "ERROR";
1345
- LogLevel["OFF"] = "OFF";
1346
- })(LogLevel || (LogLevel = {}));
1347
- var ErrorCode;
1348
- (function (ErrorCode) {
1349
- ///////////////////
1350
- // Generic Errors
1351
- // Unknown Error
1352
- ErrorCode["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
1353
- // Not Implemented
1354
- ErrorCode["NOT_IMPLEMENTED"] = "NOT_IMPLEMENTED";
1355
- // Unsupported Operation
1356
- // - operation
1357
- ErrorCode["UNSUPPORTED_OPERATION"] = "UNSUPPORTED_OPERATION";
1358
- // Network Error (i.e. Ethereum Network, such as an invalid chain ID)
1359
- // - event ("noNetwork" is not re-thrown in provider.ready; otherwise thrown)
1360
- ErrorCode["NETWORK_ERROR"] = "NETWORK_ERROR";
1361
- // Some sort of bad response from the server
1362
- ErrorCode["SERVER_ERROR"] = "SERVER_ERROR";
1363
- // Timeout
1364
- ErrorCode["TIMEOUT"] = "TIMEOUT";
1365
- ///////////////////
1366
- // Operational Errors
1367
- // Buffer Overrun
1368
- ErrorCode["BUFFER_OVERRUN"] = "BUFFER_OVERRUN";
1369
- // Numeric Fault
1370
- // - operation: the operation being executed
1371
- // - fault: the reason this faulted
1372
- ErrorCode["NUMERIC_FAULT"] = "NUMERIC_FAULT";
1373
- ///////////////////
1374
- // Argument Errors
1375
- // Missing new operator to an object
1376
- // - name: The name of the class
1377
- ErrorCode["MISSING_NEW"] = "MISSING_NEW";
1378
- // Invalid argument (e.g. value is incompatible with type) to a function:
1379
- // - argument: The argument name that was invalid
1380
- // - value: The value of the argument
1381
- ErrorCode["INVALID_ARGUMENT"] = "INVALID_ARGUMENT";
1382
- // Missing argument to a function:
1383
- // - count: The number of arguments received
1384
- // - expectedCount: The number of arguments expected
1385
- ErrorCode["MISSING_ARGUMENT"] = "MISSING_ARGUMENT";
1386
- // Too many arguments
1387
- // - count: The number of arguments received
1388
- // - expectedCount: The number of arguments expected
1389
- ErrorCode["UNEXPECTED_ARGUMENT"] = "UNEXPECTED_ARGUMENT";
1390
- ///////////////////
1391
- // Blockchain Errors
1392
- // Call exception
1393
- // - transaction: the transaction
1394
- // - address?: the contract address
1395
- // - args?: The arguments passed into the function
1396
- // - method?: The Solidity method signature
1397
- // - errorSignature?: The EIP848 error signature
1398
- // - errorArgs?: The EIP848 error parameters
1399
- // - reason: The reason (only for EIP848 "Error(string)")
1400
- ErrorCode["CALL_EXCEPTION"] = "CALL_EXCEPTION";
1401
- // Insufficient funds (< value + gasLimit * gasPrice)
1402
- // - transaction: the transaction attempted
1403
- ErrorCode["INSUFFICIENT_FUNDS"] = "INSUFFICIENT_FUNDS";
1404
- // Nonce has already been used
1405
- // - transaction: the transaction attempted
1406
- ErrorCode["NONCE_EXPIRED"] = "NONCE_EXPIRED";
1407
- // The replacement fee for the transaction is too low
1408
- // - transaction: the transaction attempted
1409
- ErrorCode["REPLACEMENT_UNDERPRICED"] = "REPLACEMENT_UNDERPRICED";
1410
- // The gas limit could not be estimated
1411
- // - transaction: the transaction passed to estimateGas
1412
- ErrorCode["UNPREDICTABLE_GAS_LIMIT"] = "UNPREDICTABLE_GAS_LIMIT";
1413
- // The transaction was replaced by one with a higher gas price
1414
- // - reason: "cancelled", "replaced" or "repriced"
1415
- // - cancelled: true if reason == "cancelled" or reason == "replaced")
1416
- // - hash: original transaction hash
1417
- // - replacement: the full TransactionsResponse for the replacement
1418
- // - receipt: the receipt of the replacement
1419
- ErrorCode["TRANSACTION_REPLACED"] = "TRANSACTION_REPLACED";
1420
- })(ErrorCode || (ErrorCode = {}));
1421
- const HEX = "0123456789abcdef";
1422
- class Logger {
1423
- constructor(version) {
1424
- Object.defineProperty(this, "version", {
1425
- enumerable: true,
1426
- value: version,
1427
- writable: false
1428
- });
1429
- }
1430
- _log(logLevel, args) {
1431
- const level = logLevel.toLowerCase();
1432
- if (LogLevels[level] == null) {
1433
- this.throwArgumentError("invalid log level name", "logLevel", logLevel);
1434
- }
1435
- if (_logLevel > LogLevels[level]) {
1436
- return;
1437
- }
1438
- console.log.apply(console, args);
1439
- }
1440
- debug(...args) {
1441
- this._log(Logger.levels.DEBUG, args);
1442
- }
1443
- info(...args) {
1444
- this._log(Logger.levels.INFO, args);
1445
- }
1446
- warn(...args) {
1447
- this._log(Logger.levels.WARNING, args);
1448
- }
1449
- makeError(message, code, params) {
1450
- // Errors are being censored
1451
- if (_censorErrors) {
1452
- return this.makeError("censored error", code, {});
1453
- }
1454
- if (!code) {
1455
- code = Logger.errors.UNKNOWN_ERROR;
1456
- }
1457
- if (!params) {
1458
- params = {};
1459
- }
1460
- const messageDetails = [];
1461
- Object.keys(params).forEach((key) => {
1462
- const value = params[key];
1463
- try {
1464
- if (value instanceof Uint8Array) {
1465
- let hex = "";
1466
- for (let i = 0; i < value.length; i++) {
1467
- hex += HEX[value[i] >> 4];
1468
- hex += HEX[value[i] & 0x0f];
1469
- }
1470
- messageDetails.push(key + "=Uint8Array(0x" + hex + ")");
1471
- }
1472
- else {
1473
- messageDetails.push(key + "=" + JSON.stringify(value));
1474
- }
1475
- }
1476
- catch (error) {
1477
- messageDetails.push(key + "=" + JSON.stringify(params[key].toString()));
1478
- }
1479
- });
1480
- messageDetails.push(`code=${code}`);
1481
- messageDetails.push(`version=${this.version}`);
1482
- const reason = message;
1483
- let url = "";
1484
- switch (code) {
1485
- case ErrorCode.NUMERIC_FAULT: {
1486
- url = "NUMERIC_FAULT";
1487
- const fault = message;
1488
- switch (fault) {
1489
- case "overflow":
1490
- case "underflow":
1491
- case "division-by-zero":
1492
- url += "-" + fault;
1493
- break;
1494
- case "negative-power":
1495
- case "negative-width":
1496
- url += "-unsupported";
1497
- break;
1498
- case "unbound-bitwise-result":
1499
- url += "-unbound-result";
1500
- break;
1501
- }
1502
- break;
1503
- }
1504
- case ErrorCode.CALL_EXCEPTION:
1505
- case ErrorCode.INSUFFICIENT_FUNDS:
1506
- case ErrorCode.MISSING_NEW:
1507
- case ErrorCode.NONCE_EXPIRED:
1508
- case ErrorCode.REPLACEMENT_UNDERPRICED:
1509
- case ErrorCode.TRANSACTION_REPLACED:
1510
- case ErrorCode.UNPREDICTABLE_GAS_LIMIT:
1511
- url = code;
1512
- break;
1513
- }
1514
- if (url) {
1515
- message += " [ See: https:/\/links.ethers.org/v5-errors-" + url + " ]";
1516
- }
1517
- if (messageDetails.length) {
1518
- message += " (" + messageDetails.join(", ") + ")";
1519
- }
1520
- // @TODO: Any??
1521
- const error = new Error(message);
1522
- error.reason = reason;
1523
- error.code = code;
1524
- Object.keys(params).forEach(function (key) {
1525
- error[key] = params[key];
1526
- });
1527
- return error;
1528
- }
1529
- throwError(message, code, params) {
1530
- throw this.makeError(message, code, params);
1531
- }
1532
- throwArgumentError(message, name, value) {
1533
- return this.throwError(message, Logger.errors.INVALID_ARGUMENT, {
1534
- argument: name,
1535
- value: value
1536
- });
1537
- }
1538
- assert(condition, message, code, params) {
1539
- if (!!condition) {
1540
- return;
1541
- }
1542
- this.throwError(message, code, params);
1543
- }
1544
- assertArgument(condition, message, name, value) {
1545
- if (!!condition) {
1546
- return;
1547
- }
1548
- this.throwArgumentError(message, name, value);
1549
- }
1550
- checkNormalize(message) {
1551
- if (_normalizeError) {
1552
- this.throwError("platform missing String.prototype.normalize", Logger.errors.UNSUPPORTED_OPERATION, {
1553
- operation: "String.prototype.normalize", form: _normalizeError
1554
- });
1555
- }
1556
- }
1557
- checkSafeUint53(value, message) {
1558
- if (typeof (value) !== "number") {
1559
- return;
1560
- }
1561
- if (message == null) {
1562
- message = "value not safe";
1563
- }
1564
- if (value < 0 || value >= 0x1fffffffffffff) {
1565
- this.throwError(message, Logger.errors.NUMERIC_FAULT, {
1566
- operation: "checkSafeInteger",
1567
- fault: "out-of-safe-range",
1568
- value: value
1569
- });
1570
- }
1571
- if (value % 1) {
1572
- this.throwError(message, Logger.errors.NUMERIC_FAULT, {
1573
- operation: "checkSafeInteger",
1574
- fault: "non-integer",
1575
- value: value
1576
- });
1577
- }
1578
- }
1579
- checkArgumentCount(count, expectedCount, message) {
1580
- if (message) {
1581
- message = ": " + message;
1582
- }
1583
- else {
1584
- message = "";
1585
- }
1586
- if (count < expectedCount) {
1587
- this.throwError("missing argument" + message, Logger.errors.MISSING_ARGUMENT, {
1588
- count: count,
1589
- expectedCount: expectedCount
1590
- });
1591
- }
1592
- if (count > expectedCount) {
1593
- this.throwError("too many arguments" + message, Logger.errors.UNEXPECTED_ARGUMENT, {
1594
- count: count,
1595
- expectedCount: expectedCount
1596
- });
1597
- }
1598
- }
1599
- checkNew(target, kind) {
1600
- if (target === Object || target == null) {
1601
- this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name });
1602
- }
1603
- }
1604
- checkAbstract(target, kind) {
1605
- if (target === kind) {
1606
- this.throwError("cannot instantiate abstract class " + JSON.stringify(kind.name) + " directly; use a sub-class", Logger.errors.UNSUPPORTED_OPERATION, { name: target.name, operation: "new" });
1607
- }
1608
- else if (target === Object || target == null) {
1609
- this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name });
1610
- }
1611
- }
1612
- static globalLogger() {
1613
- if (!_globalLogger) {
1614
- _globalLogger = new Logger(version$2);
1615
- }
1616
- return _globalLogger;
1617
- }
1618
- static setCensorship(censorship, permanent) {
1619
- if (!censorship && permanent) {
1620
- this.globalLogger().throwError("cannot permanently disable censorship", Logger.errors.UNSUPPORTED_OPERATION, {
1621
- operation: "setCensorship"
1622
- });
1623
- }
1624
- if (_permanentCensorErrors) {
1625
- if (!censorship) {
1626
- return;
1627
- }
1628
- this.globalLogger().throwError("error censorship permanent", Logger.errors.UNSUPPORTED_OPERATION, {
1629
- operation: "setCensorship"
1630
- });
1631
- }
1632
- _censorErrors = !!censorship;
1633
- _permanentCensorErrors = !!permanent;
1634
- }
1635
- static setLogLevel(logLevel) {
1636
- const level = LogLevels[logLevel.toLowerCase()];
1637
- if (level == null) {
1638
- Logger.globalLogger().warn("invalid log level - " + logLevel);
1639
- return;
1640
- }
1641
- _logLevel = level;
1642
- }
1643
- static from(version) {
1644
- return new Logger(version);
1645
- }
1646
- }
1647
- Logger.errors = ErrorCode;
1648
- Logger.levels = LogLevel;
1649
-
1650
- const version$1 = "bytes/5.6.0";
1651
-
1652
- const logger = new Logger(version$1);
1653
- ///////////////////////////////
1654
- function isHexable(value) {
1655
- return !!(value.toHexString);
1656
- }
1657
- function addSlice(array) {
1658
- if (array.slice) {
1659
- return array;
1660
- }
1661
- array.slice = function () {
1662
- const args = Array.prototype.slice.call(arguments);
1663
- return addSlice(new Uint8Array(Array.prototype.slice.apply(array, args)));
1664
- };
1665
- return array;
1666
- }
1667
- function isInteger(value) {
1668
- return (typeof (value) === "number" && value == value && (value % 1) === 0);
1669
- }
1670
- function isBytes(value) {
1671
- if (value == null) {
1672
- return false;
1673
- }
1674
- if (value.constructor === Uint8Array) {
1675
- return true;
1676
- }
1677
- if (typeof (value) === "string") {
1678
- return false;
1679
- }
1680
- if (!isInteger(value.length) || value.length < 0) {
1681
- return false;
1682
- }
1683
- for (let i = 0; i < value.length; i++) {
1684
- const v = value[i];
1685
- if (!isInteger(v) || v < 0 || v >= 256) {
1686
- return false;
1687
- }
1688
- }
1689
- return true;
1690
- }
1691
- function arrayify(value, options) {
1692
- if (!options) {
1693
- options = {};
1694
- }
1695
- if (typeof (value) === "number") {
1696
- logger.checkSafeUint53(value, "invalid arrayify value");
1697
- const result = [];
1698
- while (value) {
1699
- result.unshift(value & 0xff);
1700
- value = parseInt(String(value / 256));
1701
- }
1702
- if (result.length === 0) {
1703
- result.push(0);
1704
- }
1705
- return addSlice(new Uint8Array(result));
1706
- }
1707
- if (options.allowMissingPrefix && typeof (value) === "string" && value.substring(0, 2) !== "0x") {
1708
- value = "0x" + value;
1709
- }
1710
- if (isHexable(value)) {
1711
- value = value.toHexString();
1712
- }
1713
- if (isHexString(value)) {
1714
- let hex = value.substring(2);
1715
- if (hex.length % 2) {
1716
- if (options.hexPad === "left") {
1717
- hex = "0x0" + hex.substring(2);
1718
- }
1719
- else if (options.hexPad === "right") {
1720
- hex += "0";
1721
- }
1722
- else {
1723
- logger.throwArgumentError("hex data is odd-length", "value", value);
1724
- }
1725
- }
1726
- const result = [];
1727
- for (let i = 0; i < hex.length; i += 2) {
1728
- result.push(parseInt(hex.substring(i, i + 2), 16));
1729
- }
1730
- return addSlice(new Uint8Array(result));
1731
- }
1732
- if (isBytes(value)) {
1733
- return addSlice(new Uint8Array(value));
1734
- }
1735
- return logger.throwArgumentError("invalid arrayify value", "value", value);
1736
- }
1737
- function isHexString(value, length) {
1738
- if (typeof (value) !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) {
1739
- return false;
1740
- }
1741
- if (length && value.length !== 2 + 2 * length) {
1742
- return false;
1743
- }
1744
- return true;
1745
- }
1746
-
1747
- const version = "sha2/5.6.0";
1748
-
1749
- new Logger(version);
1750
- function sha256(data) {
1751
- return "0x" + (hash.sha256().update(arrayify(data)).digest("hex"));
1752
- }
1753
-
1754
65
  class Struct {
1755
66
  constructor(properties) {
1756
67
  Object.assign(this, properties);
@@ -1793,6 +104,11 @@ const SOLANA_SCHEMA = new Map();
1793
104
  */
1794
105
 
1795
106
  const MAX_SEED_LENGTH = 32;
107
+ /**
108
+ * Size of public key in bytes
109
+ */
110
+
111
+ const PUBLIC_KEY_LENGTH = 32;
1796
112
  /**
1797
113
  * Value to be converted into public key
1798
114
  */
@@ -1823,7 +139,7 @@ class PublicKey extends Struct {
1823
139
  // assume base 58 encoding by default
1824
140
  const decoded = bs58.decode(value);
1825
141
 
1826
- if (decoded.length != 32) {
142
+ if (decoded.length != PUBLIC_KEY_LENGTH) {
1827
143
  throw new Error(`Invalid public key input`);
1828
144
  }
1829
145
 
@@ -1876,7 +192,7 @@ class PublicKey extends Struct {
1876
192
  toBuffer() {
1877
193
  const b = this._bn.toArrayLike(Buffer);
1878
194
 
1879
- if (b.length === 32) {
195
+ if (b.length === PUBLIC_KEY_LENGTH) {
1880
196
  return b;
1881
197
  }
1882
198
 
@@ -1903,8 +219,8 @@ class PublicKey extends Struct {
1903
219
 
1904
220
  static async createWithSeed(fromPublicKey, seed, programId) {
1905
221
  const buffer = Buffer.concat([fromPublicKey.toBuffer(), Buffer.from(seed), programId.toBuffer()]);
1906
- const hash = sha256(new Uint8Array(buffer)).slice(2);
1907
- return new PublicKey(Buffer.from(hash, 'hex'));
222
+ const publicKeyBytes = sha256(buffer);
223
+ return new PublicKey(publicKeyBytes);
1908
224
  }
1909
225
  /**
1910
226
  * Derive a program address from seeds and a program ID.
@@ -1923,10 +239,9 @@ class PublicKey extends Struct {
1923
239
  buffer = Buffer.concat([buffer, toBuffer(seed)]);
1924
240
  });
1925
241
  buffer = Buffer.concat([buffer, programId.toBuffer(), Buffer.from('ProgramDerivedAddress')]);
1926
- let hash = sha256(new Uint8Array(buffer)).slice(2);
1927
- let publicKeyBytes = new BN(hash, 16).toArray(undefined, 32);
242
+ const publicKeyBytes = sha256(buffer);
1928
243
 
1929
- if (is_on_curve(publicKeyBytes)) {
244
+ if (isOnCurve(publicKeyBytes)) {
1930
245
  throw new Error(`Invalid seeds, address must fall off the curve`);
1931
246
  }
1932
247
 
@@ -1990,7 +305,7 @@ class PublicKey extends Struct {
1990
305
 
1991
306
  static isOnCurve(pubkeyData) {
1992
307
  const pubkey = new PublicKey(pubkeyData);
1993
- return is_on_curve(pubkey.toBytes()) == 1;
308
+ return isOnCurve(pubkey.toBytes());
1994
309
  }
1995
310
 
1996
311
  }
@@ -1998,56 +313,7 @@ PublicKey.default = new PublicKey('11111111111111111111111111111111');
1998
313
  SOLANA_SCHEMA.set(PublicKey, {
1999
314
  kind: 'struct',
2000
315
  fields: [['_bn', 'u256']]
2001
- }); // @ts-ignore
2002
-
2003
- let naclLowLevel = nacl.lowlevel; // Check that a pubkey is on the curve.
2004
- // This function and its dependents were sourced from:
2005
- // https://github.com/dchest/tweetnacl-js/blob/f1ec050ceae0861f34280e62498b1d3ed9c350c6/nacl.js#L792
2006
-
2007
- function is_on_curve(p) {
2008
- var r = [naclLowLevel.gf(), naclLowLevel.gf(), naclLowLevel.gf(), naclLowLevel.gf()];
2009
- var t = naclLowLevel.gf(),
2010
- chk = naclLowLevel.gf(),
2011
- num = naclLowLevel.gf(),
2012
- den = naclLowLevel.gf(),
2013
- den2 = naclLowLevel.gf(),
2014
- den4 = naclLowLevel.gf(),
2015
- den6 = naclLowLevel.gf();
2016
- naclLowLevel.set25519(r[2], gf1);
2017
- naclLowLevel.unpack25519(r[1], p);
2018
- naclLowLevel.S(num, r[1]);
2019
- naclLowLevel.M(den, num, naclLowLevel.D);
2020
- naclLowLevel.Z(num, num, r[2]);
2021
- naclLowLevel.A(den, r[2], den);
2022
- naclLowLevel.S(den2, den);
2023
- naclLowLevel.S(den4, den2);
2024
- naclLowLevel.M(den6, den4, den2);
2025
- naclLowLevel.M(t, den6, num);
2026
- naclLowLevel.M(t, t, den);
2027
- naclLowLevel.pow2523(t, t);
2028
- naclLowLevel.M(t, t, num);
2029
- naclLowLevel.M(t, t, den);
2030
- naclLowLevel.M(t, t, den);
2031
- naclLowLevel.M(r[0], t, den);
2032
- naclLowLevel.S(chk, r[0]);
2033
- naclLowLevel.M(chk, chk, den);
2034
- if (neq25519(chk, num)) naclLowLevel.M(r[0], r[0], I);
2035
- naclLowLevel.S(chk, r[0]);
2036
- naclLowLevel.M(chk, chk, den);
2037
- if (neq25519(chk, num)) return 0;
2038
- return 1;
2039
- }
2040
-
2041
- let gf1 = naclLowLevel.gf([1]);
2042
- let I = naclLowLevel.gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83]);
2043
-
2044
- function neq25519(a, b) {
2045
- var c = new Uint8Array(32),
2046
- d = new Uint8Array(32);
2047
- naclLowLevel.pack25519(c, a);
2048
- naclLowLevel.pack25519(d, b);
2049
- return naclLowLevel.crypto_verify_32(c, 0, d, 0);
2050
- }
316
+ });
2051
317
 
2052
318
  /**
2053
319
  * An account key pair (public and secret keys).
@@ -2058,6 +324,8 @@ function neq25519(a, b) {
2058
324
  class Account {
2059
325
  /** @internal */
2060
326
 
327
+ /** @internal */
328
+
2061
329
  /**
2062
330
  * Create a new Account object
2063
331
  *
@@ -2067,12 +335,21 @@ class Account {
2067
335
  * @param secretKey Secret key for the account
2068
336
  */
2069
337
  constructor(secretKey) {
2070
- this._keypair = void 0;
338
+ this._publicKey = void 0;
339
+ this._secretKey = void 0;
2071
340
 
2072
341
  if (secretKey) {
2073
- this._keypair = nacl.sign.keyPair.fromSecretKey(toBuffer(secretKey));
342
+ const secretKeyBuffer = toBuffer(secretKey);
343
+
344
+ if (secretKey.length !== 64) {
345
+ throw new Error('bad secret key size');
346
+ }
347
+
348
+ this._publicKey = secretKeyBuffer.slice(32, 64);
349
+ this._secretKey = secretKeyBuffer.slice(0, 32);
2074
350
  } else {
2075
- this._keypair = nacl.sign.keyPair();
351
+ this._secretKey = toBuffer(generatePrivateKey());
352
+ this._publicKey = toBuffer(getPublicKey(this._secretKey));
2076
353
  }
2077
354
  }
2078
355
  /**
@@ -2081,15 +358,17 @@ class Account {
2081
358
 
2082
359
 
2083
360
  get publicKey() {
2084
- return new PublicKey(this._keypair.publicKey);
361
+ return new PublicKey(this._publicKey);
2085
362
  }
2086
363
  /**
2087
- * The **unencrypted** secret key for this account
364
+ * The **unencrypted** secret key for this account. The first 32 bytes
365
+ * is the private scalar and the last 32 bytes is the public key.
366
+ * Read more: https://blog.mozilla.org/warner/2011/11/29/ed25519-keys/
2088
367
  */
2089
368
 
2090
369
 
2091
370
  get secretKey() {
2092
- return toBuffer(this._keypair.secretKey);
371
+ return Buffer.concat([this._secretKey, this._publicKey], 64);
2093
372
  }
2094
373
 
2095
374
  }
@@ -2104,6 +383,7 @@ const BPF_LOADER_DEPRECATED_PROGRAM_ID = new PublicKey('BPFLoader111111111111111
2104
383
  * 8 bytes is the size of the fragment header
2105
384
  */
2106
385
  const PACKET_DATA_SIZE = 1280 - 40 - 8;
386
+ const VERSION_PREFIX_MASK = 0x7f;
2107
387
  const SIGNATURE_LENGTH_IN_BYTES = 64;
2108
388
 
2109
389
  class TransactionExpiredBlockheightExceededError extends Error {
@@ -2136,6 +416,13 @@ Object.defineProperty(TransactionExpiredTimeoutError.prototype, 'name', {
2136
416
  const publicKey = (property = 'publicKey') => {
2137
417
  return BufferLayout.blob(32, property);
2138
418
  };
419
+ /**
420
+ * Layout for a signature
421
+ */
422
+
423
+ const signature = (property = 'signature') => {
424
+ return BufferLayout.blob(64, property);
425
+ };
2139
426
 
2140
427
  /**
2141
428
  * Layout for a Rust String type
@@ -2247,11 +534,9 @@ function encodeLength(bytes, len) {
2247
534
  }
2248
535
  }
2249
536
 
2250
- const PUBKEY_LENGTH = 32;
2251
537
  /**
2252
538
  * List of instructions to be processed atomically
2253
539
  */
2254
-
2255
540
  class Message {
2256
541
  constructor(args) {
2257
542
  this.header = void 0;
@@ -2266,6 +551,26 @@ class Message {
2266
551
  this.instructions.forEach(ix => this.indexToProgramIds.set(ix.programIdIndex, this.accountKeys[ix.programIdIndex]));
2267
552
  }
2268
553
 
554
+ get version() {
555
+ return 'legacy';
556
+ }
557
+
558
+ get staticAccountKeys() {
559
+ return this.accountKeys;
560
+ }
561
+
562
+ get compiledInstructions() {
563
+ return this.instructions.map(ix => ({
564
+ programIdIndex: ix.programIdIndex,
565
+ accountKeyIndexes: ix.accounts,
566
+ data: bs58.decode(ix.data)
567
+ }));
568
+ }
569
+
570
+ get addressTableLookups() {
571
+ return [];
572
+ }
573
+
2269
574
  isAccountSigner(index) {
2270
575
  return index < this.header.numRequiredSignatures;
2271
576
  }
@@ -2342,19 +647,24 @@ class Message {
2342
647
  // Slice up wire data
2343
648
  let byteArray = [...buffer];
2344
649
  const numRequiredSignatures = byteArray.shift();
650
+
651
+ if (numRequiredSignatures !== (numRequiredSignatures & VERSION_PREFIX_MASK)) {
652
+ throw new Error('Versioned messages must be deserialized with VersionedMessage.deserialize()');
653
+ }
654
+
2345
655
  const numReadonlySignedAccounts = byteArray.shift();
2346
656
  const numReadonlyUnsignedAccounts = byteArray.shift();
2347
657
  const accountCount = decodeLength(byteArray);
2348
658
  let accountKeys = [];
2349
659
 
2350
660
  for (let i = 0; i < accountCount; i++) {
2351
- const account = byteArray.slice(0, PUBKEY_LENGTH);
2352
- byteArray = byteArray.slice(PUBKEY_LENGTH);
661
+ const account = byteArray.slice(0, PUBLIC_KEY_LENGTH);
662
+ byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
2353
663
  accountKeys.push(bs58.encode(Buffer.from(account)));
2354
664
  }
2355
665
 
2356
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
2357
- byteArray = byteArray.slice(PUBKEY_LENGTH);
666
+ const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH);
667
+ byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
2358
668
  const instructionCount = decodeLength(byteArray);
2359
669
  let instructions = [];
2360
670
 
@@ -2395,6 +705,194 @@ function assert (condition, message) {
2395
705
  }
2396
706
  }
2397
707
 
708
+ /**
709
+ * Message constructor arguments
710
+ */
711
+
712
+ class MessageV0 {
713
+ constructor(args) {
714
+ this.header = void 0;
715
+ this.staticAccountKeys = void 0;
716
+ this.recentBlockhash = void 0;
717
+ this.compiledInstructions = void 0;
718
+ this.addressTableLookups = void 0;
719
+ this.header = args.header;
720
+ this.staticAccountKeys = args.staticAccountKeys;
721
+ this.recentBlockhash = args.recentBlockhash;
722
+ this.compiledInstructions = args.compiledInstructions;
723
+ this.addressTableLookups = args.addressTableLookups;
724
+ }
725
+
726
+ get version() {
727
+ return 0;
728
+ }
729
+
730
+ serialize() {
731
+ const encodedStaticAccountKeysLength = Array();
732
+ encodeLength(encodedStaticAccountKeysLength, this.staticAccountKeys.length);
733
+ const serializedInstructions = this.serializeInstructions();
734
+ const encodedInstructionsLength = Array();
735
+ encodeLength(encodedInstructionsLength, this.compiledInstructions.length);
736
+ const serializedAddressTableLookups = this.serializeAddressTableLookups();
737
+ const encodedAddressTableLookupsLength = Array();
738
+ encodeLength(encodedAddressTableLookupsLength, this.addressTableLookups.length);
739
+ const messageLayout = BufferLayout.struct([BufferLayout.u8('prefix'), BufferLayout.struct([BufferLayout.u8('numRequiredSignatures'), BufferLayout.u8('numReadonlySignedAccounts'), BufferLayout.u8('numReadonlyUnsignedAccounts')], 'header'), BufferLayout.blob(encodedStaticAccountKeysLength.length, 'staticAccountKeysLength'), BufferLayout.seq(publicKey(), this.staticAccountKeys.length, 'staticAccountKeys'), publicKey('recentBlockhash'), BufferLayout.blob(encodedInstructionsLength.length, 'instructionsLength'), BufferLayout.blob(serializedInstructions.length, 'serializedInstructions'), BufferLayout.blob(encodedAddressTableLookupsLength.length, 'addressTableLookupsLength'), BufferLayout.blob(serializedAddressTableLookups.length, 'serializedAddressTableLookups')]);
740
+ const serializedMessage = new Uint8Array(PACKET_DATA_SIZE);
741
+ const MESSAGE_VERSION_0_PREFIX = 1 << 7;
742
+ const serializedMessageLength = messageLayout.encode({
743
+ prefix: MESSAGE_VERSION_0_PREFIX,
744
+ header: this.header,
745
+ staticAccountKeysLength: new Uint8Array(encodedStaticAccountKeysLength),
746
+ staticAccountKeys: this.staticAccountKeys.map(key => key.toBytes()),
747
+ recentBlockhash: bs58.decode(this.recentBlockhash),
748
+ instructionsLength: new Uint8Array(encodedInstructionsLength),
749
+ serializedInstructions,
750
+ addressTableLookupsLength: new Uint8Array(encodedAddressTableLookupsLength),
751
+ serializedAddressTableLookups
752
+ }, serializedMessage);
753
+ return serializedMessage.slice(0, serializedMessageLength);
754
+ }
755
+
756
+ serializeInstructions() {
757
+ let serializedLength = 0;
758
+ const serializedInstructions = new Uint8Array(PACKET_DATA_SIZE);
759
+
760
+ for (const instruction of this.compiledInstructions) {
761
+ const encodedAccountKeyIndexesLength = Array();
762
+ encodeLength(encodedAccountKeyIndexesLength, instruction.accountKeyIndexes.length);
763
+ const encodedDataLength = Array();
764
+ encodeLength(encodedDataLength, instruction.data.length);
765
+ const instructionLayout = BufferLayout.struct([BufferLayout.u8('programIdIndex'), BufferLayout.blob(encodedAccountKeyIndexesLength.length, 'encodedAccountKeyIndexesLength'), BufferLayout.seq(BufferLayout.u8(), instruction.accountKeyIndexes.length, 'accountKeyIndexes'), BufferLayout.blob(encodedDataLength.length, 'encodedDataLength'), BufferLayout.blob(instruction.data.length, 'data')]);
766
+ serializedLength += instructionLayout.encode({
767
+ programIdIndex: instruction.programIdIndex,
768
+ encodedAccountKeyIndexesLength: new Uint8Array(encodedAccountKeyIndexesLength),
769
+ accountKeyIndexes: instruction.accountKeyIndexes,
770
+ encodedDataLength: new Uint8Array(encodedDataLength),
771
+ data: instruction.data
772
+ }, serializedInstructions, serializedLength);
773
+ }
774
+
775
+ return serializedInstructions.slice(0, serializedLength);
776
+ }
777
+
778
+ serializeAddressTableLookups() {
779
+ let serializedLength = 0;
780
+ const serializedAddressTableLookups = new Uint8Array(PACKET_DATA_SIZE);
781
+
782
+ for (const lookup of this.addressTableLookups) {
783
+ const encodedWritableIndexesLength = Array();
784
+ encodeLength(encodedWritableIndexesLength, lookup.writableIndexes.length);
785
+ const encodedReadonlyIndexesLength = Array();
786
+ encodeLength(encodedReadonlyIndexesLength, lookup.readonlyIndexes.length);
787
+ const addressTableLookupLayout = BufferLayout.struct([publicKey('accountKey'), BufferLayout.blob(encodedWritableIndexesLength.length, 'encodedWritableIndexesLength'), BufferLayout.seq(BufferLayout.u8(), lookup.writableIndexes.length, 'writableIndexes'), BufferLayout.blob(encodedReadonlyIndexesLength.length, 'encodedReadonlyIndexesLength'), BufferLayout.seq(BufferLayout.u8(), lookup.readonlyIndexes.length, 'readonlyIndexes')]);
788
+ serializedLength += addressTableLookupLayout.encode({
789
+ accountKey: lookup.accountKey.toBytes(),
790
+ encodedWritableIndexesLength: new Uint8Array(encodedWritableIndexesLength),
791
+ writableIndexes: lookup.writableIndexes,
792
+ encodedReadonlyIndexesLength: new Uint8Array(encodedReadonlyIndexesLength),
793
+ readonlyIndexes: lookup.readonlyIndexes
794
+ }, serializedAddressTableLookups, serializedLength);
795
+ }
796
+
797
+ return serializedAddressTableLookups.slice(0, serializedLength);
798
+ }
799
+
800
+ static deserialize(serializedMessage) {
801
+ let byteArray = [...serializedMessage];
802
+ const prefix = byteArray.shift();
803
+ const maskedPrefix = prefix & VERSION_PREFIX_MASK;
804
+ assert(prefix !== maskedPrefix, `Expected versioned message but received legacy message`);
805
+ const version = maskedPrefix;
806
+ assert(version === 0, `Expected versioned message with version 0 but found version ${version}`);
807
+ const header = {
808
+ numRequiredSignatures: byteArray.shift(),
809
+ numReadonlySignedAccounts: byteArray.shift(),
810
+ numReadonlyUnsignedAccounts: byteArray.shift()
811
+ };
812
+ const staticAccountKeys = [];
813
+ const staticAccountKeysLength = decodeLength(byteArray);
814
+
815
+ for (let i = 0; i < staticAccountKeysLength; i++) {
816
+ staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
817
+ }
818
+
819
+ const recentBlockhash = bs58.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
820
+ const instructionCount = decodeLength(byteArray);
821
+ const compiledInstructions = [];
822
+
823
+ for (let i = 0; i < instructionCount; i++) {
824
+ const programIdIndex = byteArray.shift();
825
+ const accountKeyIndexesLength = decodeLength(byteArray);
826
+ const accountKeyIndexes = byteArray.splice(0, accountKeyIndexesLength);
827
+ const dataLength = decodeLength(byteArray);
828
+ const data = new Uint8Array(byteArray.splice(0, dataLength));
829
+ compiledInstructions.push({
830
+ programIdIndex,
831
+ accountKeyIndexes,
832
+ data
833
+ });
834
+ }
835
+
836
+ const addressTableLookupsCount = decodeLength(byteArray);
837
+ const addressTableLookups = [];
838
+
839
+ for (let i = 0; i < addressTableLookupsCount; i++) {
840
+ const accountKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
841
+ const writableIndexesLength = decodeLength(byteArray);
842
+ const writableIndexes = byteArray.splice(0, writableIndexesLength);
843
+ const readonlyIndexesLength = decodeLength(byteArray);
844
+ const readonlyIndexes = byteArray.splice(0, readonlyIndexesLength);
845
+ addressTableLookups.push({
846
+ accountKey,
847
+ writableIndexes,
848
+ readonlyIndexes
849
+ });
850
+ }
851
+
852
+ return new MessageV0({
853
+ header,
854
+ staticAccountKeys,
855
+ recentBlockhash,
856
+ compiledInstructions,
857
+ addressTableLookups
858
+ });
859
+ }
860
+
861
+ }
862
+
863
+ // eslint-disable-next-line no-redeclare
864
+ const VersionedMessage = {
865
+ deserializeMessageVersion(serializedMessage) {
866
+ const prefix = serializedMessage[0];
867
+ const maskedPrefix = prefix & VERSION_PREFIX_MASK; // if the highest bit of the prefix is not set, the message is not versioned
868
+
869
+ if (maskedPrefix === prefix) {
870
+ return 'legacy';
871
+ } // the lower 7 bits of the prefix indicate the message version
872
+
873
+
874
+ return maskedPrefix;
875
+ },
876
+
877
+ deserialize: serializedMessage => {
878
+ const version = VersionedMessage.deserializeMessageVersion(serializedMessage);
879
+
880
+ if (version === 'legacy') {
881
+ return Message.from(serializedMessage);
882
+ }
883
+
884
+ if (version === 0) {
885
+ return MessageV0.deserialize(serializedMessage);
886
+ } else {
887
+ throw new Error(`Transaction message version ${version} deserialization is not supported`);
888
+ }
889
+ }
890
+ };
891
+
892
+ /**
893
+ * Transaction signature as base-58 encoded string
894
+ */
895
+
2398
896
  let TransactionStatus;
2399
897
  /**
2400
898
  * Default (empty) signature
@@ -2920,7 +1418,7 @@ class Transaction {
2920
1418
  _partialSign(message, ...signers) {
2921
1419
  const signData = message.serialize();
2922
1420
  signers.forEach(signer => {
2923
- const signature = nacl.sign.detached(signData, signer.secretKey);
1421
+ const signature = sign(signData, signer.secretKey);
2924
1422
 
2925
1423
  this._addSignature(signer.publicKey, toBuffer(signature));
2926
1424
  });
@@ -2976,7 +1474,7 @@ class Transaction {
2976
1474
  return false;
2977
1475
  }
2978
1476
  } else {
2979
- if (!nacl.sign.detached.verify(signData, signature, publicKey.toBuffer())) {
1477
+ if (!verify(signature, signData, publicKey.toBuffer())) {
2980
1478
  return false;
2981
1479
  }
2982
1480
  }
@@ -3123,6 +1621,70 @@ class Transaction {
3123
1621
 
3124
1622
  }
3125
1623
 
1624
+ /**
1625
+ * Versioned transaction class
1626
+ */
1627
+ class VersionedTransaction {
1628
+ constructor(message, signatures) {
1629
+ this.signatures = void 0;
1630
+ this.message = void 0;
1631
+
1632
+ if (signatures !== undefined) {
1633
+ assert(signatures.length === message.header.numRequiredSignatures, 'Expected signatures length to be equal to the number of required signatures');
1634
+ this.signatures = signatures;
1635
+ } else {
1636
+ const defaultSignatures = [];
1637
+
1638
+ for (let i = 0; i < message.header.numRequiredSignatures; i++) {
1639
+ defaultSignatures.push(new Uint8Array(SIGNATURE_LENGTH_IN_BYTES));
1640
+ }
1641
+
1642
+ this.signatures = defaultSignatures;
1643
+ }
1644
+
1645
+ this.message = message;
1646
+ }
1647
+
1648
+ serialize() {
1649
+ const serializedMessage = this.message.serialize();
1650
+ const encodedSignaturesLength = Array();
1651
+ encodeLength(encodedSignaturesLength, this.signatures.length);
1652
+ const transactionLayout = BufferLayout.struct([BufferLayout.blob(encodedSignaturesLength.length, 'encodedSignaturesLength'), BufferLayout.seq(signature(), this.signatures.length, 'signatures'), BufferLayout.blob(serializedMessage.length, 'serializedMessage')]);
1653
+ const serializedTransaction = new Uint8Array(2048);
1654
+ const serializedTransactionLength = transactionLayout.encode({
1655
+ encodedSignaturesLength: new Uint8Array(encodedSignaturesLength),
1656
+ signatures: this.signatures,
1657
+ serializedMessage
1658
+ }, serializedTransaction);
1659
+ return serializedTransaction.slice(0, serializedTransactionLength);
1660
+ }
1661
+
1662
+ static deserialize(serializedTransaction) {
1663
+ let byteArray = [...serializedTransaction];
1664
+ const signatures = [];
1665
+ const signaturesLength = decodeLength(byteArray);
1666
+
1667
+ for (let i = 0; i < signaturesLength; i++) {
1668
+ signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
1669
+ }
1670
+
1671
+ const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
1672
+ return new VersionedTransaction(message, signatures);
1673
+ }
1674
+
1675
+ sign(signers) {
1676
+ const messageData = this.message.serialize();
1677
+ const signerPubkeys = this.message.staticAccountKeys.slice(0, this.message.header.numRequiredSignatures);
1678
+
1679
+ for (const signer of signers) {
1680
+ const signerIndex = signerPubkeys.findIndex(pubkey => pubkey.equals(signer.publicKey));
1681
+ assert(signerIndex >= 0, `Cannot sign with non signer key ${signer.publicKey.toBase58()}`);
1682
+ this.signatures[signerIndex] = sign(messageData, signer.secretKey);
1683
+ }
1684
+ }
1685
+
1686
+ }
1687
+
3126
1688
  const SYSVAR_CLOCK_PUBKEY = new PublicKey('SysvarC1ock11111111111111111111111111111111');
3127
1689
  const SYSVAR_EPOCH_SCHEDULE_PUBKEY = new PublicKey('SysvarEpochSchedu1e111111111111111111111111');
3128
1690
  const SYSVAR_INSTRUCTIONS_PUBKEY = new PublicKey('Sysvar1nstructions1111111111111111111111111');
@@ -4568,24 +3130,26 @@ const LookupTableMetaLayout = {
4568
3130
  BufferLayout.seq(publicKey(), BufferLayout.offset(BufferLayout.u8(), -1), 'authority')])
4569
3131
  };
4570
3132
 
4571
- const URL = globalThis.URL;
4572
-
3133
+ const URL_RE = /^[^:]+:\/\/([^:[]+|\[[^\]]+\])(:\d+)?(.*)/i;
4573
3134
  function makeWebsocketUrl(endpoint) {
4574
- let url = new URL(endpoint);
4575
- const useHttps = url.protocol === 'https:';
4576
- url.protocol = useHttps ? 'wss:' : 'ws:';
4577
- url.host = ''; // Only shift the port by +1 as a convention for ws(s) only if given endpoint
3135
+ const matches = endpoint.match(URL_RE);
3136
+
3137
+ if (matches == null) {
3138
+ throw TypeError(`Failed to validate endpoint URL \`${endpoint}\``);
3139
+ }
3140
+
3141
+ const [_, // eslint-disable-line @typescript-eslint/no-unused-vars
3142
+ hostish, portWithColon, rest] = matches;
3143
+ const protocol = endpoint.startsWith('https:') ? 'wss:' : 'ws:';
3144
+ const startPort = portWithColon == null ? null : parseInt(portWithColon.slice(1), 10);
3145
+ const websocketPort = // Only shift the port by +1 as a convention for ws(s) only if given endpoint
4578
3146
  // is explictly specifying the endpoint port (HTTP-based RPC), assuming
4579
3147
  // we're directly trying to connect to solana-validator's ws listening port.
4580
3148
  // When the endpoint omits the port, we're connecting to the protocol
4581
3149
  // default ports: http(80) or https(443) and it's assumed we're behind a reverse
4582
3150
  // proxy which manages WebSocket upgrade and backend port redirection.
4583
-
4584
- if (url.port !== '') {
4585
- url.port = String(Number(url.port) + 1);
4586
- }
4587
-
4588
- return url.toString();
3151
+ startPort == null ? '' : `:${startPort + 1}`;
3152
+ return `${protocol}//${hostish}${websocketPort}${rest}`;
4589
3153
  }
4590
3154
 
4591
3155
  var _process$env$npm_pack;
@@ -4605,7 +3169,17 @@ const BLOCKHASH_CACHE_TIMEOUT_MS = 30 * 1000;
4605
3169
  * https://gist.github.com/steveluscher/c057eca81d479ef705cdb53162f9971d
4606
3170
  */
4607
3171
 
3172
+ /* @internal */
3173
+ function assertEndpointUrl(putativeUrl) {
3174
+ if (/^https?:/.test(putativeUrl) === false) {
3175
+ throw new TypeError('Endpoint URL must start with `http:` or `https:`.');
3176
+ }
3177
+
3178
+ return putativeUrl;
3179
+ }
4608
3180
  /** @internal */
3181
+
3182
+
4609
3183
  function extractCommitmentFromConfig(commitmentOrConfig) {
4610
3184
  let commitment;
4611
3185
  let config;
@@ -4800,12 +3374,14 @@ const BlockProductionResponseStruct = jsonRpcResultAndContext(type({
4800
3374
  * A performance sample
4801
3375
  */
4802
3376
 
4803
- function createRpcClient(url, useHttps, httpHeaders, customFetch, fetchMiddleware, disableRetryOnRateLimit) {
3377
+ function createRpcClient(url, httpHeaders, customFetch, fetchMiddleware, disableRetryOnRateLimit) {
4804
3378
  const fetch = customFetch ? customFetch : fetchImpl;
4805
3379
  let agentManager;
4806
3380
 
4807
3381
  {
4808
- agentManager = new AgentManager(useHttps);
3382
+ agentManager = new AgentManager(url.startsWith('https:')
3383
+ /* useHttps */
3384
+ );
4809
3385
  }
4810
3386
 
4811
3387
  let fetchWithMiddleware;
@@ -5612,8 +4188,6 @@ class Connection {
5612
4188
  this._subscriptionCallbacksByServerSubscriptionId = {};
5613
4189
  this._subscriptionsByHash = {};
5614
4190
  this._subscriptionsAutoDisposedByRpc = new Set();
5615
- let url = new URL(endpoint);
5616
- const useHttps = url.protocol === 'https:';
5617
4191
  let wsEndpoint;
5618
4192
  let httpHeaders;
5619
4193
  let fetch;
@@ -5632,9 +4206,9 @@ class Connection {
5632
4206
  disableRetryOnRateLimit = commitmentOrConfig.disableRetryOnRateLimit;
5633
4207
  }
5634
4208
 
5635
- this._rpcEndpoint = endpoint;
4209
+ this._rpcEndpoint = assertEndpointUrl(endpoint);
5636
4210
  this._rpcWsEndpoint = wsEndpoint || makeWebsocketUrl(endpoint);
5637
- this._rpcClient = createRpcClient(url.toString(), useHttps, httpHeaders, fetch, fetchMiddleware, disableRetryOnRateLimit);
4211
+ this._rpcClient = createRpcClient(endpoint, httpHeaders, fetch, fetchMiddleware, disableRetryOnRateLimit);
5638
4212
  this._rpcRequest = createRpcRequest(this._rpcClient);
5639
4213
  this._rpcBatchRequest = createRpcBatchRequest(this._rpcClient);
5640
4214
  this._rpcWebSocket = new Client(this._rpcWsEndpoint, {
@@ -8311,12 +6885,7 @@ class Keypair {
8311
6885
  */
8312
6886
  constructor(keypair) {
8313
6887
  this._keypair = void 0;
8314
-
8315
- if (keypair) {
8316
- this._keypair = keypair;
8317
- } else {
8318
- this._keypair = nacl.sign.keyPair();
8319
- }
6888
+ this._keypair = keypair !== null && keypair !== void 0 ? keypair : generateKeypair();
8320
6889
  }
8321
6890
  /**
8322
6891
  * Generate a new random keypair
@@ -8324,7 +6893,7 @@ class Keypair {
8324
6893
 
8325
6894
 
8326
6895
  static generate() {
8327
- return new Keypair(nacl.sign.keyPair());
6896
+ return new Keypair(generateKeypair());
8328
6897
  }
8329
6898
  /**
8330
6899
  * Create a keypair from a raw secret key byte array.
@@ -8341,19 +6910,27 @@ class Keypair {
8341
6910
 
8342
6911
 
8343
6912
  static fromSecretKey(secretKey, options) {
8344
- const keypair = nacl.sign.keyPair.fromSecretKey(secretKey);
6913
+ if (secretKey.byteLength !== 64) {
6914
+ throw new Error('bad secret key size');
6915
+ }
6916
+
6917
+ const publicKey = secretKey.slice(32, 64);
8345
6918
 
8346
6919
  if (!options || !options.skipValidation) {
8347
- const encoder = new TextEncoder();
8348
- const signData = encoder.encode('@solana/web3.js-validation-v1');
8349
- const signature = nacl.sign.detached(signData, keypair.secretKey);
6920
+ const privateScalar = secretKey.slice(0, 32);
6921
+ const computedPublicKey = getPublicKey(privateScalar);
8350
6922
 
8351
- if (!nacl.sign.detached.verify(signData, signature, keypair.publicKey)) {
8352
- throw new Error('provided secretKey is invalid');
6923
+ for (let ii = 0; ii < 32; ii++) {
6924
+ if (publicKey[ii] !== computedPublicKey[ii]) {
6925
+ throw new Error('provided secretKey is invalid');
6926
+ }
8353
6927
  }
8354
6928
  }
8355
6929
 
8356
- return new Keypair(keypair);
6930
+ return new Keypair({
6931
+ publicKey,
6932
+ secretKey
6933
+ });
8357
6934
  }
8358
6935
  /**
8359
6936
  * Generate a keypair from a 32 byte seed.
@@ -8363,7 +6940,14 @@ class Keypair {
8363
6940
 
8364
6941
 
8365
6942
  static fromSeed(seed) {
8366
- return new Keypair(nacl.sign.keyPair.fromSeed(seed));
6943
+ const publicKey = getPublicKey(seed);
6944
+ const secretKey = new Uint8Array(64);
6945
+ secretKey.set(seed);
6946
+ secretKey.set(publicKey, 32);
6947
+ return new Keypair({
6948
+ publicKey,
6949
+ secretKey
6950
+ });
8367
6951
  }
8368
6952
  /**
8369
6953
  * The public key for this keypair
@@ -8915,7 +7499,7 @@ class Ed25519Program {
8915
7499
  try {
8916
7500
  const keypair = Keypair.fromSecretKey(privateKey);
8917
7501
  const publicKey = keypair.publicKey.toBytes();
8918
- const signature = nacl.sign.detached(message, keypair.secretKey);
7502
+ const signature = sign(message, keypair.secretKey);
8919
7503
  return this.createInstructionWithPublicKey({
8920
7504
  publicKey,
8921
7505
  message,
@@ -8930,10 +7514,21 @@ class Ed25519Program {
8930
7514
  }
8931
7515
  Ed25519Program.programId = new PublicKey('Ed25519SigVerify111111111111111111111111111');
8932
7516
 
8933
- const {
8934
- publicKeyCreate,
8935
- ecdsaSign
8936
- } = secp256k1;
7517
+ // library interoperable with the synchronous APIs in web3.js.
7518
+
7519
+ secp256k1.utils.hmacSha256Sync = (key, ...msgs) => {
7520
+ const h = hmac.create(sha256, key);
7521
+ msgs.forEach(msg => h.update(msg));
7522
+ return h.digest();
7523
+ };
7524
+
7525
+ const ecdsaSign = (msgHash, privKey) => secp256k1.signSync(msgHash, privKey, {
7526
+ der: false,
7527
+ recovered: true
7528
+ });
7529
+ secp256k1.utils.isValidPrivateKey;
7530
+ const publicKeyCreate = secp256k1.getPublicKey;
7531
+
8937
7532
  const PRIVATE_KEY_BYTES = 32;
8938
7533
  const ETHEREUM_ADDRESS_BYTES = 20;
8939
7534
  const PUBLIC_KEY_BYTES = 64;
@@ -9057,13 +7652,12 @@ class Secp256k1Program {
9057
7652
 
9058
7653
  try {
9059
7654
  const privateKey = toBuffer(pkey);
9060
- const publicKey = publicKeyCreate(privateKey, false).slice(1); // throw away leading byte
7655
+ const publicKey = publicKeyCreate(privateKey, false
7656
+ /* isCompressed */
7657
+ ).slice(1); // throw away leading byte
9061
7658
 
9062
7659
  const messageHash = Buffer.from(sha3.keccak_256.update(toBuffer(message)).digest());
9063
- const {
9064
- signature,
9065
- recid: recoveryId
9066
- } = ecdsaSign(messageHash, privateKey);
7660
+ const [signature, recoveryId] = ecdsaSign(messageHash, privateKey);
9067
7661
  return this.createInstructionWithPublicKey({
9068
7662
  publicKey,
9069
7663
  message,
@@ -10175,6 +8769,23 @@ class VoteProgram {
10175
8769
  data
10176
8770
  });
10177
8771
  }
8772
+ /**
8773
+ * Generate a transaction to withdraw safely from a Vote account.
8774
+ *
8775
+ * This function was created as a safeguard for vote accounts running validators, `safeWithdraw`
8776
+ * checks that the withdraw amount will not exceed the specified balance while leaving enough left
8777
+ * to cover rent. If you wish to close the vote account by withdrawing the full amount, call the
8778
+ * `withdraw` method directly.
8779
+ */
8780
+
8781
+
8782
+ static safeWithdraw(params, currentVoteAccountBalance, rentExemptMinimum) {
8783
+ if (params.lamports > currentVoteAccountBalance - rentExemptMinimum) {
8784
+ throw new Error('Withdraw will leave vote account with insuffcient funds.');
8785
+ }
8786
+
8787
+ return VoteProgram.withdraw(params);
8788
+ }
10178
8789
 
10179
8790
  }
10180
8791
  VoteProgram.programId = new PublicKey('Vote111111111111111111111111111111111111111');
@@ -10226,15 +8837,14 @@ class ValidatorInfo {
10226
8837
 
10227
8838
 
10228
8839
  static fromConfigData(buffer) {
10229
- const PUBKEY_LENGTH = 32;
10230
8840
  let byteArray = [...buffer];
10231
8841
  const configKeyCount = decodeLength(byteArray);
10232
8842
  if (configKeyCount !== 2) return null;
10233
8843
  const configKeys = [];
10234
8844
 
10235
8845
  for (let i = 0; i < 2; i++) {
10236
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
10237
- byteArray = byteArray.slice(PUBKEY_LENGTH);
8846
+ const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
8847
+ byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
10238
8848
  const isSigner = byteArray.slice(0, 1)[0] === 1;
10239
8849
  byteArray = byteArray.slice(1);
10240
8850
  configKeys.push({
@@ -10247,7 +8857,7 @@ class ValidatorInfo {
10247
8857
  if (configKeys[1].isSigner) {
10248
8858
  const rawInfo = rustString().decode(Buffer.from(byteArray));
10249
8859
  const info = JSON.parse(rawInfo);
10250
- assert$7(info, InfoString);
8860
+ assert$1(info, InfoString);
10251
8861
  return new ValidatorInfo(configKeys[1].publicKey, info);
10252
8862
  }
10253
8863
  }
@@ -10446,5 +9056,5 @@ async function sendAndConfirmRawTransaction(connection, rawTransaction, confirma
10446
9056
 
10447
9057
  const LAMPORTS_PER_SOL = 1000000000;
10448
9058
 
10449
- export { Account, AddressLookupTableAccount, AddressLookupTableInstruction, AddressLookupTableProgram, Authorized, BLOCKHASH_CACHE_TIMEOUT_MS, BPF_LOADER_DEPRECATED_PROGRAM_ID, BPF_LOADER_PROGRAM_ID, BpfLoader, COMPUTE_BUDGET_INSTRUCTION_LAYOUTS, ComputeBudgetInstruction, ComputeBudgetProgram, Connection, Ed25519Program, Enum, EpochSchedule, FeeCalculatorLayout, Keypair, LAMPORTS_PER_SOL, LOOKUP_TABLE_INSTRUCTION_LAYOUTS, Loader, Lockup, MAX_SEED_LENGTH, Message, NONCE_ACCOUNT_LENGTH, NonceAccount, PACKET_DATA_SIZE, PublicKey, SIGNATURE_LENGTH_IN_BYTES, SOLANA_SCHEMA, STAKE_CONFIG_ID, STAKE_INSTRUCTION_LAYOUTS, SYSTEM_INSTRUCTION_LAYOUTS, SYSVAR_CLOCK_PUBKEY, SYSVAR_EPOCH_SCHEDULE_PUBKEY, SYSVAR_INSTRUCTIONS_PUBKEY, SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY, SYSVAR_REWARDS_PUBKEY, SYSVAR_SLOT_HASHES_PUBKEY, SYSVAR_SLOT_HISTORY_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY, Secp256k1Program, SendTransactionError, SolanaJSONRPCError, SolanaJSONRPCErrorCode, StakeAuthorizationLayout, StakeInstruction, StakeProgram, Struct, SystemInstruction, SystemProgram, Transaction, TransactionExpiredBlockheightExceededError, TransactionExpiredTimeoutError, TransactionInstruction, TransactionStatus, VALIDATOR_INFO_KEY, VOTE_PROGRAM_ID, ValidatorInfo, VoteAccount, VoteAuthorizationLayout, VoteInit, VoteInstruction, VoteProgram, clusterApiUrl, sendAndConfirmRawTransaction, sendAndConfirmTransaction };
9059
+ export { Account, AddressLookupTableAccount, AddressLookupTableInstruction, AddressLookupTableProgram, Authorized, BLOCKHASH_CACHE_TIMEOUT_MS, BPF_LOADER_DEPRECATED_PROGRAM_ID, BPF_LOADER_PROGRAM_ID, BpfLoader, COMPUTE_BUDGET_INSTRUCTION_LAYOUTS, ComputeBudgetInstruction, ComputeBudgetProgram, Connection, Ed25519Program, Enum, EpochSchedule, FeeCalculatorLayout, Keypair, LAMPORTS_PER_SOL, LOOKUP_TABLE_INSTRUCTION_LAYOUTS, Loader, Lockup, MAX_SEED_LENGTH, Message, MessageV0, NONCE_ACCOUNT_LENGTH, NonceAccount, PACKET_DATA_SIZE, PUBLIC_KEY_LENGTH, PublicKey, SIGNATURE_LENGTH_IN_BYTES, SOLANA_SCHEMA, STAKE_CONFIG_ID, STAKE_INSTRUCTION_LAYOUTS, SYSTEM_INSTRUCTION_LAYOUTS, SYSVAR_CLOCK_PUBKEY, SYSVAR_EPOCH_SCHEDULE_PUBKEY, SYSVAR_INSTRUCTIONS_PUBKEY, SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY, SYSVAR_REWARDS_PUBKEY, SYSVAR_SLOT_HASHES_PUBKEY, SYSVAR_SLOT_HISTORY_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY, Secp256k1Program, SendTransactionError, SolanaJSONRPCError, SolanaJSONRPCErrorCode, StakeAuthorizationLayout, StakeInstruction, StakeProgram, Struct, SystemInstruction, SystemProgram, Transaction, TransactionExpiredBlockheightExceededError, TransactionExpiredTimeoutError, TransactionInstruction, TransactionStatus, VALIDATOR_INFO_KEY, VERSION_PREFIX_MASK, VOTE_PROGRAM_ID, ValidatorInfo, VersionedMessage, VersionedTransaction, VoteAccount, VoteAuthorizationLayout, VoteInit, VoteInstruction, VoteProgram, clusterApiUrl, sendAndConfirmRawTransaction, sendAndConfirmTransaction };
10450
9060
  //# sourceMappingURL=index.esm.js.map