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