@taquito/sapling 24.3.0-beta.0 → 24.3.0-beta.2

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.
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('bignumber.js'), require('@taquito/taquito'), require('@taquito/utils'), require('@taquito/core'), require('@airgap/sapling-wasm'), require('blakejs'), require('@stablelib/nacl'), require('@stablelib/random'), require('typedarray-to-buffer'), require('pbkdf2'), require('bip39')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'bignumber.js', '@taquito/taquito', '@taquito/utils', '@taquito/core', '@airgap/sapling-wasm', 'blakejs', '@stablelib/nacl', '@stablelib/random', 'typedarray-to-buffer', 'pbkdf2', 'bip39'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.taquitoSapling = {}, global.BigNumber, global.taquito, global.utils, global.core, global.sapling, global.blake, global.nacl, global.random, global.toBuffer, global.pbkdf2, global.bip39));
5
- })(this, (function (exports, BigNumber, taquito, utils, core, sapling, blake, nacl, random, toBuffer, pbkdf2, bip39) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('bignumber.js'), require('@taquito/taquito'), require('@taquito/utils'), require('@taquito/core'), require('@airgap/sapling-wasm'), require('blakejs'), require('@stablelib/nacl'), require('@stablelib/random'), require('typedarray-to-buffer')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'bignumber.js', '@taquito/taquito', '@taquito/utils', '@taquito/core', '@airgap/sapling-wasm', 'blakejs', '@stablelib/nacl', '@stablelib/random', 'typedarray-to-buffer'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.taquitoSapling = {}, global.BigNumber, global.taquito, global.utils, global.core, global.sapling, global.blake, global.nacl, global.random, global.toBuffer));
5
+ })(this, (function (exports, BigNumber, taquito, utils, core, sapling, blake, nacl, random, toBuffer) { 'use strict';
6
6
 
7
7
  function _interopNamespaceDefault(e) {
8
8
  var n = Object.create(null);
@@ -22,7 +22,6 @@
22
22
  }
23
23
 
24
24
  var sapling__namespace = /*#__PURE__*/_interopNamespaceDefault(sapling);
25
- var bip39__namespace = /*#__PURE__*/_interopNamespaceDefault(bip39);
26
25
 
27
26
  /******************************************************************************
28
27
  Copyright (c) Microsoft Corporation.
@@ -908,6 +907,647 @@
908
907
  }
909
908
  _SaplingTransactionBuilder_inMemorySpendingKey = new WeakMap(), _SaplingTransactionBuilder_inMemoryProvingKey = new WeakMap(), _SaplingTransactionBuilder_saplingForger = new WeakMap(), _SaplingTransactionBuilder_contractAddress = new WeakMap(), _SaplingTransactionBuilder_saplingId = new WeakMap(), _SaplingTransactionBuilder_memoSize = new WeakMap(), _SaplingTransactionBuilder_readProvider = new WeakMap(), _SaplingTransactionBuilder_saplingWrapper = new WeakMap(), _SaplingTransactionBuilder_chainId = new WeakMap(), _SaplingTransactionBuilder_saplingState = new WeakMap();
910
909
 
910
+ /**
911
+ * Utilities for hex, bytes, CSPRNG.
912
+ * @module
913
+ */
914
+ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
915
+ /** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
916
+ function isBytes(a) {
917
+ return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
918
+ }
919
+ /** Asserts something is positive integer. */
920
+ function anumber(n, title = '') {
921
+ if (!Number.isSafeInteger(n) || n < 0) {
922
+ const prefix = title && `"${title}" `;
923
+ throw new Error(`${prefix}expected integer >= 0, got ${n}`);
924
+ }
925
+ }
926
+ /** Asserts something is Uint8Array. */
927
+ function abytes(value, length, title = '') {
928
+ const bytes = isBytes(value);
929
+ const len = value?.length;
930
+ const needsLen = length !== undefined;
931
+ if (!bytes || (needsLen && len !== length)) {
932
+ const prefix = title && `"${title}" `;
933
+ const ofLen = needsLen ? ` of length ${length}` : '';
934
+ const got = bytes ? `length=${len}` : `type=${typeof value}`;
935
+ throw new Error(prefix + 'expected Uint8Array' + ofLen + ', got ' + got);
936
+ }
937
+ return value;
938
+ }
939
+ /** Asserts something is hash */
940
+ function ahash(h) {
941
+ if (typeof h !== 'function' || typeof h.create !== 'function')
942
+ throw new Error('Hash must wrapped by utils.createHasher');
943
+ anumber(h.outputLen);
944
+ anumber(h.blockLen);
945
+ }
946
+ /** Asserts a hash instance has not been destroyed / finished */
947
+ function aexists(instance, checkFinished = true) {
948
+ if (instance.destroyed)
949
+ throw new Error('Hash instance has been destroyed');
950
+ if (checkFinished && instance.finished)
951
+ throw new Error('Hash#digest() has already been called');
952
+ }
953
+ /** Asserts output is properly-sized byte array */
954
+ function aoutput(out, instance) {
955
+ abytes(out, undefined, 'digestInto() output');
956
+ const min = instance.outputLen;
957
+ if (out.length < min) {
958
+ throw new Error('"digestInto() output" expected to be of length >=' + min);
959
+ }
960
+ }
961
+ /** Zeroize a byte array. Warning: JS provides no guarantees. */
962
+ function clean(...arrays) {
963
+ for (let i = 0; i < arrays.length; i++) {
964
+ arrays[i].fill(0);
965
+ }
966
+ }
967
+ /** Create DataView of an array for easy byte-level manipulation. */
968
+ function createView(arr) {
969
+ return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
970
+ }
971
+ /**
972
+ * There is no setImmediate in browser and setTimeout is slow.
973
+ * Call of async fn will return Promise, which will be fullfiled only on
974
+ * next scheduler queue processing step and this is exactly what we need.
975
+ */
976
+ const nextTick = async () => { };
977
+ /** Returns control to thread each 'tick' ms to avoid blocking. */
978
+ async function asyncLoop(iters, tick, cb) {
979
+ let ts = Date.now();
980
+ for (let i = 0; i < iters; i++) {
981
+ cb(i);
982
+ // Date.now() is not monotonic, so in case if clock goes backwards we return return control too
983
+ const diff = Date.now() - ts;
984
+ if (diff >= 0 && diff < tick)
985
+ continue;
986
+ await nextTick();
987
+ ts += diff;
988
+ }
989
+ }
990
+ /**
991
+ * Converts string to bytes using UTF8 encoding.
992
+ * Built-in doesn't validate input to be string: we do the check.
993
+ * @example utf8ToBytes('abc') // Uint8Array.from([97, 98, 99])
994
+ */
995
+ function utf8ToBytes(str) {
996
+ if (typeof str !== 'string')
997
+ throw new Error('string expected');
998
+ return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
999
+ }
1000
+ /**
1001
+ * Helper for KDFs: consumes uint8array or string.
1002
+ * When string is passed, does utf8 decoding, using TextDecoder.
1003
+ */
1004
+ function kdfInputToBytes(data, errorTitle = '') {
1005
+ if (typeof data === 'string')
1006
+ return utf8ToBytes(data);
1007
+ return abytes(data, undefined, errorTitle);
1008
+ }
1009
+ /** Merges default options and passed options. */
1010
+ function checkOpts(defaults, opts) {
1011
+ if (opts !== undefined && {}.toString.call(opts) !== '[object Object]')
1012
+ throw new Error('options must be object or undefined');
1013
+ const merged = Object.assign(defaults, opts);
1014
+ return merged;
1015
+ }
1016
+ /** Creates function with outputLen, blockLen, create properties from a class constructor. */
1017
+ function createHasher(hashCons, info = {}) {
1018
+ const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
1019
+ const tmp = hashCons(undefined);
1020
+ hashC.outputLen = tmp.outputLen;
1021
+ hashC.blockLen = tmp.blockLen;
1022
+ hashC.create = (opts) => hashCons(opts);
1023
+ Object.assign(hashC, info);
1024
+ return Object.freeze(hashC);
1025
+ }
1026
+ /** Creates OID opts for NIST hashes, with prefix 06 09 60 86 48 01 65 03 04 02. */
1027
+ const oidNist = (suffix) => ({
1028
+ oid: Uint8Array.from([0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, suffix]),
1029
+ });
1030
+
1031
+ /**
1032
+ * HMAC: RFC2104 message authentication code.
1033
+ * @module
1034
+ */
1035
+ /** Internal class for HMAC. */
1036
+ class _HMAC {
1037
+ oHash;
1038
+ iHash;
1039
+ blockLen;
1040
+ outputLen;
1041
+ finished = false;
1042
+ destroyed = false;
1043
+ constructor(hash, key) {
1044
+ ahash(hash);
1045
+ abytes(key, undefined, 'key');
1046
+ this.iHash = hash.create();
1047
+ if (typeof this.iHash.update !== 'function')
1048
+ throw new Error('Expected instance of class which extends utils.Hash');
1049
+ this.blockLen = this.iHash.blockLen;
1050
+ this.outputLen = this.iHash.outputLen;
1051
+ const blockLen = this.blockLen;
1052
+ const pad = new Uint8Array(blockLen);
1053
+ // blockLen can be bigger than outputLen
1054
+ pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
1055
+ for (let i = 0; i < pad.length; i++)
1056
+ pad[i] ^= 0x36;
1057
+ this.iHash.update(pad);
1058
+ // By doing update (processing of first block) of outer hash here we can re-use it between multiple calls via clone
1059
+ this.oHash = hash.create();
1060
+ // Undo internal XOR && apply outer XOR
1061
+ for (let i = 0; i < pad.length; i++)
1062
+ pad[i] ^= 0x36 ^ 0x5c;
1063
+ this.oHash.update(pad);
1064
+ clean(pad);
1065
+ }
1066
+ update(buf) {
1067
+ aexists(this);
1068
+ this.iHash.update(buf);
1069
+ return this;
1070
+ }
1071
+ digestInto(out) {
1072
+ aexists(this);
1073
+ abytes(out, this.outputLen, 'output');
1074
+ this.finished = true;
1075
+ this.iHash.digestInto(out);
1076
+ this.oHash.update(out);
1077
+ this.oHash.digestInto(out);
1078
+ this.destroy();
1079
+ }
1080
+ digest() {
1081
+ const out = new Uint8Array(this.oHash.outputLen);
1082
+ this.digestInto(out);
1083
+ return out;
1084
+ }
1085
+ _cloneInto(to) {
1086
+ // Create new instance without calling constructor since key already in state and we don't know it.
1087
+ to ||= Object.create(Object.getPrototypeOf(this), {});
1088
+ const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
1089
+ to = to;
1090
+ to.finished = finished;
1091
+ to.destroyed = destroyed;
1092
+ to.blockLen = blockLen;
1093
+ to.outputLen = outputLen;
1094
+ to.oHash = oHash._cloneInto(to.oHash);
1095
+ to.iHash = iHash._cloneInto(to.iHash);
1096
+ return to;
1097
+ }
1098
+ clone() {
1099
+ return this._cloneInto();
1100
+ }
1101
+ destroy() {
1102
+ this.destroyed = true;
1103
+ this.oHash.destroy();
1104
+ this.iHash.destroy();
1105
+ }
1106
+ }
1107
+ /**
1108
+ * HMAC: RFC2104 message authentication code.
1109
+ * @param hash - function that would be used e.g. sha256
1110
+ * @param key - message key
1111
+ * @param message - message data
1112
+ * @example
1113
+ * import { hmac } from '@noble/hashes/hmac';
1114
+ * import { sha256 } from '@noble/hashes/sha2';
1115
+ * const mac1 = hmac(sha256, 'key', 'message');
1116
+ */
1117
+ const hmac = (hash, key, message) => new _HMAC(hash, key).update(message).digest();
1118
+ hmac.create = (hash, key) => new _HMAC(hash, key);
1119
+
1120
+ /**
1121
+ * PBKDF (RFC 2898). Can be used to create a key from password and salt.
1122
+ * @module
1123
+ */
1124
+ // Common start and end for sync/async functions
1125
+ function pbkdf2Init(hash, _password, _salt, _opts) {
1126
+ ahash(hash);
1127
+ const opts = checkOpts({ dkLen: 32, asyncTick: 10 }, _opts);
1128
+ const { c, dkLen, asyncTick } = opts;
1129
+ anumber(c, 'c');
1130
+ anumber(dkLen, 'dkLen');
1131
+ anumber(asyncTick, 'asyncTick');
1132
+ if (c < 1)
1133
+ throw new Error('iterations (c) must be >= 1');
1134
+ const password = kdfInputToBytes(_password, 'password');
1135
+ const salt = kdfInputToBytes(_salt, 'salt');
1136
+ // DK = PBKDF2(PRF, Password, Salt, c, dkLen);
1137
+ const DK = new Uint8Array(dkLen);
1138
+ // U1 = PRF(Password, Salt + INT_32_BE(i))
1139
+ const PRF = hmac.create(hash, password);
1140
+ const PRFSalt = PRF._cloneInto().update(salt);
1141
+ return { c, dkLen, asyncTick, DK, PRF, PRFSalt };
1142
+ }
1143
+ function pbkdf2Output(PRF, PRFSalt, DK, prfW, u) {
1144
+ PRF.destroy();
1145
+ PRFSalt.destroy();
1146
+ if (prfW)
1147
+ prfW.destroy();
1148
+ clean(u);
1149
+ return DK;
1150
+ }
1151
+ /**
1152
+ * PBKDF2-HMAC: RFC 2898 key derivation function
1153
+ * @param hash - hash function that would be used e.g. sha256
1154
+ * @param password - password from which a derived key is generated
1155
+ * @param salt - cryptographic salt
1156
+ * @param opts - {c, dkLen} where c is work factor and dkLen is output message size
1157
+ * @example
1158
+ * const key = pbkdf2(sha256, 'password', 'salt', { dkLen: 32, c: Math.pow(2, 18) });
1159
+ */
1160
+ function pbkdf2(hash, password, salt, opts) {
1161
+ const { c, dkLen, DK, PRF, PRFSalt } = pbkdf2Init(hash, password, salt, opts);
1162
+ let prfW; // Working copy
1163
+ const arr = new Uint8Array(4);
1164
+ const view = createView(arr);
1165
+ const u = new Uint8Array(PRF.outputLen);
1166
+ // DK = T1 + T2 + ⋯ + Tdklen/hlen
1167
+ for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
1168
+ // Ti = F(Password, Salt, c, i)
1169
+ const Ti = DK.subarray(pos, pos + PRF.outputLen);
1170
+ view.setInt32(0, ti, false);
1171
+ // F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
1172
+ // U1 = PRF(Password, Salt + INT_32_BE(i))
1173
+ (prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);
1174
+ Ti.set(u.subarray(0, Ti.length));
1175
+ for (let ui = 1; ui < c; ui++) {
1176
+ // Uc = PRF(Password, Uc−1)
1177
+ PRF._cloneInto(prfW).update(u).digestInto(u);
1178
+ for (let i = 0; i < Ti.length; i++)
1179
+ Ti[i] ^= u[i];
1180
+ }
1181
+ }
1182
+ return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);
1183
+ }
1184
+ /**
1185
+ * PBKDF2-HMAC: RFC 2898 key derivation function. Async version.
1186
+ * @example
1187
+ * await pbkdf2Async(sha256, 'password', 'salt', { dkLen: 32, c: 500_000 });
1188
+ */
1189
+ async function pbkdf2Async(hash, password, salt, opts) {
1190
+ const { c, dkLen, asyncTick, DK, PRF, PRFSalt } = pbkdf2Init(hash, password, salt, opts);
1191
+ let prfW; // Working copy
1192
+ const arr = new Uint8Array(4);
1193
+ const view = createView(arr);
1194
+ const u = new Uint8Array(PRF.outputLen);
1195
+ // DK = T1 + T2 + ⋯ + Tdklen/hlen
1196
+ for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
1197
+ // Ti = F(Password, Salt, c, i)
1198
+ const Ti = DK.subarray(pos, pos + PRF.outputLen);
1199
+ view.setInt32(0, ti, false);
1200
+ // F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
1201
+ // U1 = PRF(Password, Salt + INT_32_BE(i))
1202
+ (prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);
1203
+ Ti.set(u.subarray(0, Ti.length));
1204
+ await asyncLoop(c - 1, asyncTick, () => {
1205
+ // Uc = PRF(Password, Uc−1)
1206
+ PRF._cloneInto(prfW).update(u).digestInto(u);
1207
+ for (let i = 0; i < Ti.length; i++)
1208
+ Ti[i] ^= u[i];
1209
+ });
1210
+ }
1211
+ return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);
1212
+ }
1213
+
1214
+ /**
1215
+ * Internal Merkle-Damgard hash utils.
1216
+ * @module
1217
+ */
1218
+ /**
1219
+ * Merkle-Damgard hash construction base class.
1220
+ * Could be used to create MD5, RIPEMD, SHA1, SHA2.
1221
+ */
1222
+ class HashMD {
1223
+ blockLen;
1224
+ outputLen;
1225
+ padOffset;
1226
+ isLE;
1227
+ // For partial updates less than block size
1228
+ buffer;
1229
+ view;
1230
+ finished = false;
1231
+ length = 0;
1232
+ pos = 0;
1233
+ destroyed = false;
1234
+ constructor(blockLen, outputLen, padOffset, isLE) {
1235
+ this.blockLen = blockLen;
1236
+ this.outputLen = outputLen;
1237
+ this.padOffset = padOffset;
1238
+ this.isLE = isLE;
1239
+ this.buffer = new Uint8Array(blockLen);
1240
+ this.view = createView(this.buffer);
1241
+ }
1242
+ update(data) {
1243
+ aexists(this);
1244
+ abytes(data);
1245
+ const { view, buffer, blockLen } = this;
1246
+ const len = data.length;
1247
+ for (let pos = 0; pos < len;) {
1248
+ const take = Math.min(blockLen - this.pos, len - pos);
1249
+ // Fast path: we have at least one block in input, cast it to view and process
1250
+ if (take === blockLen) {
1251
+ const dataView = createView(data);
1252
+ for (; blockLen <= len - pos; pos += blockLen)
1253
+ this.process(dataView, pos);
1254
+ continue;
1255
+ }
1256
+ buffer.set(data.subarray(pos, pos + take), this.pos);
1257
+ this.pos += take;
1258
+ pos += take;
1259
+ if (this.pos === blockLen) {
1260
+ this.process(view, 0);
1261
+ this.pos = 0;
1262
+ }
1263
+ }
1264
+ this.length += data.length;
1265
+ this.roundClean();
1266
+ return this;
1267
+ }
1268
+ digestInto(out) {
1269
+ aexists(this);
1270
+ aoutput(out, this);
1271
+ this.finished = true;
1272
+ // Padding
1273
+ // We can avoid allocation of buffer for padding completely if it
1274
+ // was previously not allocated here. But it won't change performance.
1275
+ const { buffer, view, blockLen, isLE } = this;
1276
+ let { pos } = this;
1277
+ // append the bit '1' to the message
1278
+ buffer[pos++] = 0b10000000;
1279
+ clean(this.buffer.subarray(pos));
1280
+ // we have less than padOffset left in buffer, so we cannot put length in
1281
+ // current block, need process it and pad again
1282
+ if (this.padOffset > blockLen - pos) {
1283
+ this.process(view, 0);
1284
+ pos = 0;
1285
+ }
1286
+ // Pad until full block byte with zeros
1287
+ for (let i = pos; i < blockLen; i++)
1288
+ buffer[i] = 0;
1289
+ // Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
1290
+ // You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
1291
+ // So we just write lowest 64 bits of that value.
1292
+ view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE);
1293
+ this.process(view, 0);
1294
+ const oview = createView(out);
1295
+ const len = this.outputLen;
1296
+ // NOTE: we do division by 4 later, which must be fused in single op with modulo by JIT
1297
+ if (len % 4)
1298
+ throw new Error('_sha2: outputLen must be aligned to 32bit');
1299
+ const outLen = len / 4;
1300
+ const state = this.get();
1301
+ if (outLen > state.length)
1302
+ throw new Error('_sha2: outputLen bigger than state');
1303
+ for (let i = 0; i < outLen; i++)
1304
+ oview.setUint32(4 * i, state[i], isLE);
1305
+ }
1306
+ digest() {
1307
+ const { buffer, outputLen } = this;
1308
+ this.digestInto(buffer);
1309
+ const res = buffer.slice(0, outputLen);
1310
+ this.destroy();
1311
+ return res;
1312
+ }
1313
+ _cloneInto(to) {
1314
+ to ||= new this.constructor();
1315
+ to.set(...this.get());
1316
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
1317
+ to.destroyed = destroyed;
1318
+ to.finished = finished;
1319
+ to.length = length;
1320
+ to.pos = pos;
1321
+ if (length % blockLen)
1322
+ to.buffer.set(buffer);
1323
+ return to;
1324
+ }
1325
+ clone() {
1326
+ return this._cloneInto();
1327
+ }
1328
+ }
1329
+ /** Initial SHA512 state. Bits 0..64 of frac part of sqrt of primes 2..19 */
1330
+ const SHA512_IV = /* @__PURE__ */ Uint32Array.from([
1331
+ 0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1,
1332
+ 0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179,
1333
+ ]);
1334
+
1335
+ /**
1336
+ * Internal helpers for u64. BigUint64Array is too slow as per 2025, so we implement it using Uint32Array.
1337
+ * @todo re-check https://issues.chromium.org/issues/42212588
1338
+ * @module
1339
+ */
1340
+ const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
1341
+ const _32n = /* @__PURE__ */ BigInt(32);
1342
+ function fromBig(n, le = false) {
1343
+ if (le)
1344
+ return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
1345
+ return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
1346
+ }
1347
+ function split(lst, le = false) {
1348
+ const len = lst.length;
1349
+ let Ah = new Uint32Array(len);
1350
+ let Al = new Uint32Array(len);
1351
+ for (let i = 0; i < len; i++) {
1352
+ const { h, l } = fromBig(lst[i], le);
1353
+ [Ah[i], Al[i]] = [h, l];
1354
+ }
1355
+ return [Ah, Al];
1356
+ }
1357
+ // for Shift in [0, 32)
1358
+ const shrSH = (h, _l, s) => h >>> s;
1359
+ const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
1360
+ // Right rotate for Shift in [1, 32)
1361
+ const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
1362
+ const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
1363
+ // Right rotate for Shift in (32, 64), NOTE: 32 is special case.
1364
+ const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
1365
+ const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
1366
+ // JS uses 32-bit signed integers for bitwise operations which means we cannot
1367
+ // simple take carry out of low bit sum by shift, we need to use division.
1368
+ function add(Ah, Al, Bh, Bl) {
1369
+ const l = (Al >>> 0) + (Bl >>> 0);
1370
+ return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
1371
+ }
1372
+ // Addition with more than 2 elements
1373
+ const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
1374
+ const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
1375
+ const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
1376
+ const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
1377
+ const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
1378
+ const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
1379
+
1380
+ /**
1381
+ * SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
1382
+ * SHA256 is the fastest hash implementable in JS, even faster than Blake3.
1383
+ * Check out [RFC 4634](https://www.rfc-editor.org/rfc/rfc4634) and
1384
+ * [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
1385
+ * @module
1386
+ */
1387
+ // SHA2-512 is slower than sha256 in js because u64 operations are slow.
1388
+ // Round contants
1389
+ // First 32 bits of the fractional parts of the cube roots of the first 80 primes 2..409
1390
+ // prettier-ignore
1391
+ const K512 = /* @__PURE__ */ (() => split([
1392
+ '0x428a2f98d728ae22', '0x7137449123ef65cd', '0xb5c0fbcfec4d3b2f', '0xe9b5dba58189dbbc',
1393
+ '0x3956c25bf348b538', '0x59f111f1b605d019', '0x923f82a4af194f9b', '0xab1c5ed5da6d8118',
1394
+ '0xd807aa98a3030242', '0x12835b0145706fbe', '0x243185be4ee4b28c', '0x550c7dc3d5ffb4e2',
1395
+ '0x72be5d74f27b896f', '0x80deb1fe3b1696b1', '0x9bdc06a725c71235', '0xc19bf174cf692694',
1396
+ '0xe49b69c19ef14ad2', '0xefbe4786384f25e3', '0x0fc19dc68b8cd5b5', '0x240ca1cc77ac9c65',
1397
+ '0x2de92c6f592b0275', '0x4a7484aa6ea6e483', '0x5cb0a9dcbd41fbd4', '0x76f988da831153b5',
1398
+ '0x983e5152ee66dfab', '0xa831c66d2db43210', '0xb00327c898fb213f', '0xbf597fc7beef0ee4',
1399
+ '0xc6e00bf33da88fc2', '0xd5a79147930aa725', '0x06ca6351e003826f', '0x142929670a0e6e70',
1400
+ '0x27b70a8546d22ffc', '0x2e1b21385c26c926', '0x4d2c6dfc5ac42aed', '0x53380d139d95b3df',
1401
+ '0x650a73548baf63de', '0x766a0abb3c77b2a8', '0x81c2c92e47edaee6', '0x92722c851482353b',
1402
+ '0xa2bfe8a14cf10364', '0xa81a664bbc423001', '0xc24b8b70d0f89791', '0xc76c51a30654be30',
1403
+ '0xd192e819d6ef5218', '0xd69906245565a910', '0xf40e35855771202a', '0x106aa07032bbd1b8',
1404
+ '0x19a4c116b8d2d0c8', '0x1e376c085141ab53', '0x2748774cdf8eeb99', '0x34b0bcb5e19b48a8',
1405
+ '0x391c0cb3c5c95a63', '0x4ed8aa4ae3418acb', '0x5b9cca4f7763e373', '0x682e6ff3d6b2b8a3',
1406
+ '0x748f82ee5defb2fc', '0x78a5636f43172f60', '0x84c87814a1f0ab72', '0x8cc702081a6439ec',
1407
+ '0x90befffa23631e28', '0xa4506cebde82bde9', '0xbef9a3f7b2c67915', '0xc67178f2e372532b',
1408
+ '0xca273eceea26619c', '0xd186b8c721c0c207', '0xeada7dd6cde0eb1e', '0xf57d4f7fee6ed178',
1409
+ '0x06f067aa72176fba', '0x0a637dc5a2c898a6', '0x113f9804bef90dae', '0x1b710b35131c471b',
1410
+ '0x28db77f523047d84', '0x32caab7b40c72493', '0x3c9ebe0a15c9bebc', '0x431d67c49c100d4c',
1411
+ '0x4cc5d4becb3e42b6', '0x597f299cfc657e2a', '0x5fcb6fab3ad6faec', '0x6c44198c4a475817'
1412
+ ].map(n => BigInt(n))))();
1413
+ const SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
1414
+ const SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
1415
+ // Reusable temporary buffers
1416
+ const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
1417
+ const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
1418
+ /** Internal 64-byte base SHA2 hash class. */
1419
+ class SHA2_64B extends HashMD {
1420
+ constructor(outputLen) {
1421
+ super(128, outputLen, 16, false);
1422
+ }
1423
+ // prettier-ignore
1424
+ get() {
1425
+ const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
1426
+ return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
1427
+ }
1428
+ // prettier-ignore
1429
+ set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
1430
+ this.Ah = Ah | 0;
1431
+ this.Al = Al | 0;
1432
+ this.Bh = Bh | 0;
1433
+ this.Bl = Bl | 0;
1434
+ this.Ch = Ch | 0;
1435
+ this.Cl = Cl | 0;
1436
+ this.Dh = Dh | 0;
1437
+ this.Dl = Dl | 0;
1438
+ this.Eh = Eh | 0;
1439
+ this.El = El | 0;
1440
+ this.Fh = Fh | 0;
1441
+ this.Fl = Fl | 0;
1442
+ this.Gh = Gh | 0;
1443
+ this.Gl = Gl | 0;
1444
+ this.Hh = Hh | 0;
1445
+ this.Hl = Hl | 0;
1446
+ }
1447
+ process(view, offset) {
1448
+ // Extend the first 16 words into the remaining 64 words w[16..79] of the message schedule array
1449
+ for (let i = 0; i < 16; i++, offset += 4) {
1450
+ SHA512_W_H[i] = view.getUint32(offset);
1451
+ SHA512_W_L[i] = view.getUint32((offset += 4));
1452
+ }
1453
+ for (let i = 16; i < 80; i++) {
1454
+ // s0 := (w[i-15] rightrotate 1) xor (w[i-15] rightrotate 8) xor (w[i-15] rightshift 7)
1455
+ const W15h = SHA512_W_H[i - 15] | 0;
1456
+ const W15l = SHA512_W_L[i - 15] | 0;
1457
+ const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
1458
+ const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
1459
+ // s1 := (w[i-2] rightrotate 19) xor (w[i-2] rightrotate 61) xor (w[i-2] rightshift 6)
1460
+ const W2h = SHA512_W_H[i - 2] | 0;
1461
+ const W2l = SHA512_W_L[i - 2] | 0;
1462
+ const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
1463
+ const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
1464
+ // SHA256_W[i] = s0 + s1 + SHA256_W[i - 7] + SHA256_W[i - 16];
1465
+ const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
1466
+ const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
1467
+ SHA512_W_H[i] = SUMh | 0;
1468
+ SHA512_W_L[i] = SUMl | 0;
1469
+ }
1470
+ let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
1471
+ // Compression function main loop, 80 rounds
1472
+ for (let i = 0; i < 80; i++) {
1473
+ // S1 := (e rightrotate 14) xor (e rightrotate 18) xor (e rightrotate 41)
1474
+ const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
1475
+ const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
1476
+ //const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
1477
+ const CHIh = (Eh & Fh) ^ (~Eh & Gh);
1478
+ const CHIl = (El & Fl) ^ (~El & Gl);
1479
+ // T1 = H + sigma1 + Chi(E, F, G) + SHA512_K[i] + SHA512_W[i]
1480
+ // prettier-ignore
1481
+ const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
1482
+ const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
1483
+ const T1l = T1ll | 0;
1484
+ // S0 := (a rightrotate 28) xor (a rightrotate 34) xor (a rightrotate 39)
1485
+ const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
1486
+ const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
1487
+ const MAJh = (Ah & Bh) ^ (Ah & Ch) ^ (Bh & Ch);
1488
+ const MAJl = (Al & Bl) ^ (Al & Cl) ^ (Bl & Cl);
1489
+ Hh = Gh | 0;
1490
+ Hl = Gl | 0;
1491
+ Gh = Fh | 0;
1492
+ Gl = Fl | 0;
1493
+ Fh = Eh | 0;
1494
+ Fl = El | 0;
1495
+ ({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
1496
+ Dh = Ch | 0;
1497
+ Dl = Cl | 0;
1498
+ Ch = Bh | 0;
1499
+ Cl = Bl | 0;
1500
+ Bh = Ah | 0;
1501
+ Bl = Al | 0;
1502
+ const All = add3L(T1l, sigma0l, MAJl);
1503
+ Ah = add3H(All, T1h, sigma0h, MAJh);
1504
+ Al = All | 0;
1505
+ }
1506
+ // Add the compressed chunk to the current hash value
1507
+ ({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
1508
+ ({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
1509
+ ({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
1510
+ ({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
1511
+ ({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
1512
+ ({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
1513
+ ({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
1514
+ ({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
1515
+ this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
1516
+ }
1517
+ roundClean() {
1518
+ clean(SHA512_W_H, SHA512_W_L);
1519
+ }
1520
+ destroy() {
1521
+ clean(this.buffer);
1522
+ this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1523
+ }
1524
+ }
1525
+ /** Internal SHA2-512 hash class. */
1526
+ class _SHA512 extends SHA2_64B {
1527
+ Ah = SHA512_IV[0] | 0;
1528
+ Al = SHA512_IV[1] | 0;
1529
+ Bh = SHA512_IV[2] | 0;
1530
+ Bl = SHA512_IV[3] | 0;
1531
+ Ch = SHA512_IV[4] | 0;
1532
+ Cl = SHA512_IV[5] | 0;
1533
+ Dh = SHA512_IV[6] | 0;
1534
+ Dl = SHA512_IV[7] | 0;
1535
+ Eh = SHA512_IV[8] | 0;
1536
+ El = SHA512_IV[9] | 0;
1537
+ Fh = SHA512_IV[10] | 0;
1538
+ Fl = SHA512_IV[11] | 0;
1539
+ Gh = SHA512_IV[12] | 0;
1540
+ Gl = SHA512_IV[13] | 0;
1541
+ Hh = SHA512_IV[14] | 0;
1542
+ Hl = SHA512_IV[15] | 0;
1543
+ constructor() {
1544
+ super(64);
1545
+ }
1546
+ }
1547
+ /** SHA2-512 hash function from RFC 4634. */
1548
+ const sha512 = /* @__PURE__ */ createHasher(() => new _SHA512(),
1549
+ /* @__PURE__ */ oidNist(0x03));
1550
+
911
1551
  function decryptKey(spendingKey, password) {
912
1552
  const [keyArr, pre] = (() => {
913
1553
  try {
@@ -931,7 +1571,7 @@
931
1571
  }
932
1572
  const salt = toBuffer(keyArr.slice(0, 8));
933
1573
  const encryptedSk = toBuffer(keyArr.slice(8));
934
- const encryptionKey = pbkdf2.pbkdf2Sync(password, salt, 32768, 32, 'sha512');
1574
+ const encryptionKey = pbkdf2(sha512, password, salt, { c: 32768, dkLen: 32 });
935
1575
  // Zero nonce is safe: fresh random salt per encryption produces unique PBKDF2-derived key.
936
1576
  // See: https://gitlab.com/tezos/tezos/-/blob/master/src/lib_signer_backends/encrypted.ml
937
1577
  const decrypted = nacl.openSecretBox(new Uint8Array(encryptionKey), new Uint8Array(24), // zero nonce - uniqueness provided by per-encryption derived key
@@ -1011,6 +1651,38 @@
1011
1651
  }
1012
1652
  _InMemoryViewingKey_fullViewingKey = new WeakMap();
1013
1653
 
1654
+ /*! scure-bip39 - MIT License (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com) */
1655
+ // Normalization replaces equivalent sequences of characters
1656
+ // so that any two texts that are equivalent will be reduced
1657
+ // to the same sequence of code points, called the normal form of the original text.
1658
+ // https://tonsky.me/blog/unicode/#why-is-a----
1659
+ function nfkd(str) {
1660
+ if (typeof str !== 'string')
1661
+ throw new TypeError('invalid mnemonic type: ' + typeof str);
1662
+ return str.normalize('NFKD');
1663
+ }
1664
+ function normalize(str) {
1665
+ const norm = nfkd(str);
1666
+ const words = norm.split(' ');
1667
+ if (![12, 15, 18, 21, 24].includes(words.length))
1668
+ throw new Error('Invalid mnemonic');
1669
+ return { nfkd: norm, words };
1670
+ }
1671
+ const psalt = (passphrase) => nfkd('mnemonic' + passphrase);
1672
+ /**
1673
+ * Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
1674
+ * @param mnemonic 12-24 words
1675
+ * @param passphrase string that will additionally protect the key
1676
+ * @returns 64 bytes of key data
1677
+ * @example
1678
+ * const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
1679
+ * await mnemonicToSeed(mnem, 'password');
1680
+ * // new Uint8Array([...64 bytes])
1681
+ */
1682
+ function mnemonicToSeed(mnemonic, passphrase = '') {
1683
+ return pbkdf2Async(sha512, normalize(mnemonic).nfkd, psalt(passphrase), { c: 2048, dkLen: 64 });
1684
+ }
1685
+
1014
1686
  var _InMemorySpendingKey_spendingKeyBuf, _InMemorySpendingKey_saplingViewingKey;
1015
1687
  /**
1016
1688
  * holds the spending key, create proof and signature for spend descriptions
@@ -1036,7 +1708,7 @@
1036
1708
  */
1037
1709
  static async fromMnemonic(mnemonic, derivationPath = 'm/') {
1038
1710
  // no password passed here. password provided only changes from sask -> MMXj
1039
- const fullSeed = await bip39__namespace.mnemonicToSeed(mnemonic);
1711
+ const fullSeed = await mnemonicToSeed(mnemonic);
1040
1712
  const first32 = fullSeed.slice(0, 32);
1041
1713
  const second32 = fullSeed.slice(32);
1042
1714
  // reduce seed bytes must be 32 bytes reflecting both halves