@talismn/crypto 0.0.0-pr2277-20251211071316 → 0.0.0-pr2295-20260110031023

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 (43) hide show
  1. package/dist/index.d.mts +124 -0
  2. package/dist/index.d.ts +124 -0
  3. package/dist/index.js +808 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.mjs +726 -0
  6. package/dist/index.mjs.map +1 -0
  7. package/package.json +20 -6
  8. package/dist/declarations/src/address/addressFromPublicKey.d.ts +0 -5
  9. package/dist/declarations/src/address/encodeAnyAddress.d.ts +0 -5
  10. package/dist/declarations/src/address/encoding/addressEncodingFromCurve.d.ts +0 -3
  11. package/dist/declarations/src/address/encoding/bitcoin.d.ts +0 -37
  12. package/dist/declarations/src/address/encoding/detectAddressEncoding.d.ts +0 -2
  13. package/dist/declarations/src/address/encoding/ethereum.d.ts +0 -6
  14. package/dist/declarations/src/address/encoding/index.d.ts +0 -6
  15. package/dist/declarations/src/address/encoding/solana.d.ts +0 -2
  16. package/dist/declarations/src/address/encoding/ss58.d.ts +0 -3
  17. package/dist/declarations/src/address/index.d.ts +0 -6
  18. package/dist/declarations/src/address/isAddressEqual.d.ts +0 -1
  19. package/dist/declarations/src/address/isAddressValid.d.ts +0 -1
  20. package/dist/declarations/src/address/normalizeAddress.d.ts +0 -1
  21. package/dist/declarations/src/derivation/common.d.ts +0 -5
  22. package/dist/declarations/src/derivation/deriveEcdsa.d.ts +0 -3
  23. package/dist/declarations/src/derivation/deriveEd25519.d.ts +0 -3
  24. package/dist/declarations/src/derivation/deriveEthereum.d.ts +0 -3
  25. package/dist/declarations/src/derivation/deriveSolana.d.ts +0 -3
  26. package/dist/declarations/src/derivation/deriveSr25519.d.ts +0 -4
  27. package/dist/declarations/src/derivation/index.d.ts +0 -1
  28. package/dist/declarations/src/derivation/utils.d.ts +0 -7
  29. package/dist/declarations/src/encryption/encryptKemAead.d.ts +0 -1
  30. package/dist/declarations/src/encryption/index.d.ts +0 -1
  31. package/dist/declarations/src/hashing/index.d.ts +0 -9
  32. package/dist/declarations/src/index.d.ts +0 -8
  33. package/dist/declarations/src/mnemonic/index.d.ts +0 -9
  34. package/dist/declarations/src/platform/index.d.ts +0 -4
  35. package/dist/declarations/src/types/index.d.ts +0 -9
  36. package/dist/declarations/src/utils/exports.d.ts +0 -2
  37. package/dist/declarations/src/utils/index.d.ts +0 -2
  38. package/dist/declarations/src/utils/pbkdf2.d.ts +0 -1
  39. package/dist/talismn-crypto.cjs.d.ts +0 -1
  40. package/dist/talismn-crypto.cjs.dev.js +0 -784
  41. package/dist/talismn-crypto.cjs.js +0 -7
  42. package/dist/talismn-crypto.cjs.prod.js +0 -784
  43. package/dist/talismn-crypto.esm.js +0 -716
@@ -1,716 +0,0 @@
1
- import { mnemonicToEntropy as mnemonicToEntropy$1, entropyToMnemonic as entropyToMnemonic$1, validateMnemonic, generateMnemonic as generateMnemonic$1 } from '@scure/bip39';
2
- import { wordlist } from '@scure/bip39/wordlists/english';
3
- import { base58, hex } from '@scure/base';
4
- export { base58, base64, hex, utf8 } from '@scure/base';
5
- import { ed25519 } from '@noble/curves/ed25519';
6
- export { ed25519 } from '@noble/curves/ed25519';
7
- import { secp256k1 } from '@noble/curves/secp256k1';
8
- import { bech32m, bech32 } from 'bech32';
9
- import bs58check from 'bs58check';
10
- import { keccak_256 } from '@noble/hashes/sha3';
11
- import { bytesToHex, randomBytes } from '@noble/hashes/utils';
12
- import { blake2b } from '@noble/hashes/blake2b';
13
- import { blake3 as blake3$1 } from '@noble/hashes/blake3';
14
- import { Tuple, str, Bytes, u32 } from 'scale-ts';
15
- import { HDKey } from '@scure/bip32';
16
- import { hmac } from '@noble/hashes/hmac';
17
- import { sha512 } from '@noble/hashes/sha512';
18
- import { getPublicKey, HDKD, secretFromSeed } from 'micro-sr25519';
19
- import { xchacha20poly1305 } from '@noble/ciphers/chacha.js';
20
- import { concatBytes } from '@noble/ciphers/utils.js';
21
- import { MlKem768 } from 'mlkem';
22
-
23
- const pbkdf2 = async (hash, entropy, salt, iterations, outputLenBytes) => {
24
- // NOTE: react-native-quick-crypto (our `global.crypto` polyfill on Talisman Mobile) doesn't support `crypto.subtle.deriveKey`.
25
- // But, we can work around this by using `crypto.subtle.deriveBits` and `crypto.subtle.importKey`, which when used together
26
- // can provide the same functionality as `crypto.subtle.deriveKey`.
27
- const keyMaterial = await crypto.subtle.importKey("raw", entropy, "PBKDF2", false, ["deriveBits"]);
28
- const derivedBits = await crypto.subtle.deriveBits({
29
- name: "PBKDF2",
30
- salt,
31
- iterations,
32
- hash
33
- }, keyMaterial, outputLenBytes * 8);
34
- return new Uint8Array(derivedBits);
35
- };
36
-
37
- const mnemonicToEntropy = mnemonic => {
38
- return mnemonicToEntropy$1(mnemonic, wordlist);
39
- };
40
- const entropyToMnemonic = entropy => {
41
- return entropyToMnemonic$1(entropy, wordlist);
42
- };
43
- const entropyToSeedSubstrate = async (entropy, password) => await pbkdf2("SHA-512", entropy, mnemonicPasswordToSalt(password ?? ""), 2048,
44
- // 2048 iterations
45
- 32 // 32 bytes (32 * 8 == 256 bits)
46
- );
47
- const entropyToSeedClassic = async (entropy, password) => await pbkdf2("SHA-512", encodeNormalized(entropyToMnemonic(entropy)), mnemonicPasswordToSalt(password ?? ""), 2048,
48
- // 2048 iterations
49
- 64 // 64 bytes (64 * 8 == 512 bits)
50
- );
51
- const mnemonicPasswordToSalt = password => encodeNormalized(`mnemonic${password}`);
52
-
53
- /** Normalizes a UTF-8 string using `NFKD` form, then encodes it into bytes */
54
- const encodeNormalized = utf8 => new TextEncoder().encode(utf8.normalize("NFKD"));
55
- const getSeedDerivationType = curve => {
56
- switch (curve) {
57
- case "sr25519":
58
- case "ed25519":
59
- case "ecdsa":
60
- return "substrate";
61
- case "ethereum":
62
- case "solana":
63
- return "classic";
64
- case "bitcoin-ecdsa":
65
- case "bitcoin-ed25519":
66
- throw new Error("seed derivation is not implemented for Bitcoin");
67
- }
68
- };
69
-
70
- // when deriving keys from a mnemonic, we usually dont want a password here.
71
- // a password provided here would be used as a 25th mnemonic word.
72
- const entropyToSeed = async (entropy, curve, password) => {
73
- const type = getSeedDerivationType(curve);
74
- switch (type) {
75
- case "classic":
76
- return await entropyToSeedClassic(entropy, password);
77
- case "substrate":
78
- return await entropyToSeedSubstrate(entropy, password);
79
- }
80
- };
81
- const isValidMnemonic = mnemonic => {
82
- return validateMnemonic(mnemonic, wordlist);
83
- };
84
- const generateMnemonic = words => {
85
- switch (words) {
86
- case 12:
87
- return generateMnemonic$1(wordlist, 128);
88
- case 24:
89
- return generateMnemonic$1(wordlist, 256);
90
- }
91
- };
92
-
93
- // well-known mnemonic used by polkadot.js, can be checked on polkadot wiki
94
- const DEV_MNEMONIC_POLKADOT = "bottom drive obey lake curtain smoke basket hold race lonely fit walk";
95
-
96
- // well-known phrase used by hardhat and anvil
97
- const DEV_MNEMONIC_ETHEREUM = "test test test test test test test test test test test junk";
98
-
99
- // keep dev seeds in cache as we will reuse them to validate multiple derivation paths
100
- const DEV_SEED_CACHE = new Map();
101
- const getDevSeed = async curve => {
102
- const type = getSeedDerivationType(curve);
103
- if (!DEV_SEED_CACHE.has(type)) {
104
- switch (type) {
105
- case "classic":
106
- {
107
- const entropy = mnemonicToEntropy(DEV_MNEMONIC_ETHEREUM);
108
- const seed = await entropyToSeedClassic(entropy); // 80ms
109
- DEV_SEED_CACHE.set(type, seed);
110
- break;
111
- }
112
- case "substrate":
113
- {
114
- const entropy = mnemonicToEntropy(DEV_MNEMONIC_POLKADOT);
115
- const seed = await entropyToSeedSubstrate(entropy); // 80ms
116
- DEV_SEED_CACHE.set(type, seed);
117
- break;
118
- }
119
- default:
120
- throw new Error("Unsupported derivation type");
121
- }
122
- }
123
- return DEV_SEED_CACHE.get(type);
124
- };
125
-
126
- /** NOTE: Try not to use this too much, it will need to change */
127
- const addressEncodingFromCurve = curve => {
128
- switch (curve) {
129
- case "sr25519":
130
- case "ed25519":
131
- case "ecdsa":
132
- return "ss58";
133
- case "bitcoin-ecdsa":
134
- case "bitcoin-ed25519":
135
- // NOTE: Bitcoin has multiple address formats, so this isn't necessarily correct
136
- // The format MAY be bech32m, but it might also be bech32 or base58check.
137
- // bech32m is the most recent format.
138
- return "bech32m";
139
- case "ethereum":
140
- return "ethereum";
141
- case "solana":
142
- return "base58solana";
143
- }
144
- };
145
-
146
- const encodeAddressSolana = publicKey => {
147
- if (publicKey.length !== 32) throw new Error("Public key must be 32 bytes long for Solana base58 encoding");
148
- return base58.encode(publicKey);
149
- };
150
- function isSolanaAddress(address) {
151
- try {
152
- const bytes = base58.decode(address);
153
- return bytes.length === 32;
154
- } catch (error) {
155
- return false;
156
- }
157
- }
158
-
159
- const isBitcoinAddress = address => isBech32mAddress(address) || isBech32Address(address) || isBase58CheckAddress(address);
160
- function isBech32mAddress(address) {
161
- try {
162
- fromBech32m(address);
163
- } catch {
164
- return false;
165
- }
166
- return true;
167
- }
168
- function isBech32Address(address) {
169
- try {
170
- fromBech32(address);
171
- } catch {
172
- return false;
173
- }
174
- return true;
175
- }
176
- function isBase58CheckAddress(address) {
177
- try {
178
- fromBase58Check(address);
179
- } catch {
180
- return false;
181
- }
182
- return true;
183
- }
184
-
185
- /**
186
- * Converts a Bech32m encoded address to its corresponding data representation.
187
- * @param address - The Bech32m encoded address.
188
- * @returns An object containing the version, prefix, and data of the address.
189
- * @throws {TypeError} If the address uses the wrong encoding.
190
- */
191
- function fromBech32m(address) {
192
- const result = bech32m.decode(address);
193
- const version = result.words[0];
194
- if (version === 0) throw new TypeError(address + " uses wrong encoding");
195
- const data = bech32m.fromWords(result.words.slice(1));
196
- return {
197
- version,
198
- prefix: result.prefix,
199
- data: Uint8Array.from(data)
200
- };
201
- }
202
-
203
- /**
204
- * Converts a Bech32 encoded address to its corresponding data representation.
205
- * @param address - The Bech32 encoded address.
206
- * @returns An object containing the version, prefix, and data of the address.
207
- * @throws {TypeError} If the address uses the wrong encoding.
208
- */
209
- function fromBech32(address) {
210
- const result = bech32.decode(address);
211
- const version = result.words[0];
212
- if (version !== 0) throw new TypeError(address + " uses wrong encoding");
213
- const data = bech32.fromWords(result.words.slice(1));
214
- return {
215
- version,
216
- prefix: result.prefix,
217
- data: Uint8Array.from(data)
218
- };
219
- }
220
-
221
- /**
222
- * Decodes a base58check encoded Bitcoin address and returns the version and hash.
223
- *
224
- * @param address - The base58check encoded Bitcoin address to decode.
225
- * @returns An object containing the version and hash of the decoded address.
226
- * @throws {TypeError} If the address is too short or too long.
227
- */
228
- function fromBase58Check(address) {
229
- const payload = bs58check.decode(address);
230
- if (payload.length < 21) throw new TypeError(address + " is too short");
231
- if (payload.length > 21) throw new TypeError(address + " is too long");
232
- function readUInt8(buffer, offset) {
233
- if (offset + 1 > buffer.length) {
234
- throw new Error("Offset is outside the bounds of Uint8Array");
235
- }
236
- const buf = Buffer.from(buffer);
237
- return buf.readUInt8(offset);
238
- }
239
- const version = readUInt8(payload, 0);
240
- const hash = payload.slice(1);
241
- return {
242
- version,
243
- hash
244
- };
245
- }
246
-
247
- /**
248
- * Encodes a public key using H160 encoding with Ethereum checksum.
249
- */
250
- const encodeAddressEthereum = publicKey => {
251
- // Ensure the public key is in uncompressed format (starts with 0x04)
252
- if (publicKey[0] !== 0x04) throw new Error("Invalid public key format");
253
-
254
- // Remove the prefix (0x04)
255
- const publicKeyWithoutPrefix = publicKey.slice(1);
256
-
257
- // Apply Keccak-256 hashing to the public key
258
- const hash = keccak_256(publicKeyWithoutPrefix);
259
-
260
- // Take the last 20 bytes of the hash to get the Ethereum address
261
- const address = hash.slice(-20);
262
-
263
- // Apply checksum
264
- return checksumEthereumAddress(`0x${bytesToHex(address)}`);
265
- };
266
- const checksumEthereumAddress = address => {
267
- const addr = address.toLowerCase().replace(/^0x/, "");
268
- const hash = keccak_256(new TextEncoder().encode(addr));
269
- const hashHex = bytesToHex(hash);
270
- const checksum = addr.split("").map((char, i) => parseInt(hashHex[i], 16) >= 8 ? char.toUpperCase() : char).join("");
271
- return `0x${checksum}`;
272
- };
273
- function isEthereumAddress(address) {
274
- return /^0x[a-fA-F0-9]{40}$/.test(address);
275
- }
276
-
277
- // Note: public key cannot be retrieved from an address, because address is hashed from only the last 20 bytes of the pk
278
-
279
- const blake3 = blake3$1;
280
- const blake2b256 = msg => blake2b(msg, {
281
- dkLen: 32
282
- });
283
- const blake2b512 = msg => blake2b(msg, {
284
- dkLen: 64
285
- });
286
- const getSafeHash = bytes => {
287
- // cryptographically secure one way hash
288
- // outputs 44 characters without special characters
289
- return base58.encode(blake3$1(bytes));
290
- };
291
-
292
- // Inspired from MIT licensed @polkadot-labs/hdkd-helpers
293
- // https://github.com/polkadot-labs/hdkd/blob/3ef6e02827212d934b59a4e566d8aa61d3ba7b27/packages/hdkd-helpers/src/accountId.ts#L3
294
-
295
- const VALID_PUBLICKEY_LENGTHS = [32, 33];
296
- const accountId = publicKey => {
297
- if (!VALID_PUBLICKEY_LENGTHS.includes(publicKey.length)) throw new Error("Invalid publicKey");
298
- return publicKey.length === 33 ? blake2b256(publicKey) : publicKey;
299
- };
300
- const SS58PRE = /* @__PURE__ */new TextEncoder().encode("SS58PRE");
301
- const CHECKSUM_LENGTH = 2;
302
- const VALID_PAYLOAD_LENGTHS = [32, 33];
303
- const ss58Encode = (payload, prefix = 42) => {
304
- if (!VALID_PAYLOAD_LENGTHS.includes(payload.length)) throw new Error("Invalid payload");
305
- const prefixBytes = prefix < 64 ? Uint8Array.of(prefix) : Uint8Array.of((prefix & 0b0000_0000_1111_1100) >> 2 | 0b0100_0000, prefix >> 8 | (prefix & 0b0000_0000_0000_0011) << 6);
306
- const checksum = blake2b512(Uint8Array.of(...SS58PRE, ...prefixBytes, ...payload)).subarray(0, CHECKSUM_LENGTH);
307
- return base58.encode(Uint8Array.of(...prefixBytes, ...payload, ...checksum));
308
- };
309
- const VALID_ADDRESS_LENGTHS = [35, 36, 37];
310
- const decodeSs58Address = addressStr => {
311
- const address = base58.decode(addressStr);
312
- if (!VALID_ADDRESS_LENGTHS.includes(address.length)) throw new Error("Invalid address length");
313
- const addressChecksum = address.subarray(address.length - CHECKSUM_LENGTH);
314
- const checksum = blake2b512(Uint8Array.of(...SS58PRE, ...address.subarray(0, address.length - CHECKSUM_LENGTH))).subarray(0, CHECKSUM_LENGTH);
315
- if (addressChecksum[0] !== checksum[0] || addressChecksum[1] !== checksum[1]) throw new Error("Invalid checksum");
316
- const prefixLength = address[0] & 0b0100_0000 ? 2 : 1;
317
- const prefix = prefixLength === 1 ? address[0] : (address[0] & 0b0011_1111) << 2 | address[1] >> 6 | (address[1] & 0b0011_1111) << 8;
318
- const publicKey = address.slice(prefixLength, address.length - CHECKSUM_LENGTH);
319
- return [publicKey, prefix];
320
- };
321
- const encodeAddressSs58 = (publicKey, prefix = 42) => {
322
- if (typeof publicKey === "string") [publicKey] = decodeSs58Address(publicKey);
323
- return ss58Encode(accountId(publicKey), prefix);
324
- };
325
- function isSs58Address(address) {
326
- try {
327
- decodeSs58Address(address);
328
- return true;
329
- } catch (error) {
330
- return false;
331
- }
332
- }
333
-
334
- const CACHE$1 = new Map();
335
- const detectAddressEncodingInner = address => {
336
- if (isEthereumAddress(address)) return "ethereum";
337
- if (isSs58Address(address)) return "ss58";
338
- if (isSolanaAddress(address)) return "base58solana";
339
- if (isBech32mAddress(address)) return "bech32m";
340
- if (isBech32Address(address)) return "bech32";
341
- if (isBase58CheckAddress(address)) return "base58check";
342
- throw new Error(`Unknown address encoding`);
343
- };
344
- const detectAddressEncoding = address => {
345
- if (!CACHE$1.has(address)) CACHE$1.set(address, detectAddressEncodingInner(address));
346
- return CACHE$1.get(address);
347
- };
348
-
349
- const addressFromPublicKey = (publicKey, encoding, options) => {
350
- switch (encoding) {
351
- case "ss58":
352
- return encodeAddressSs58(publicKey, options?.ss58Prefix);
353
- case "ethereum":
354
- return encodeAddressEthereum(publicKey);
355
- case "base58solana":
356
- return encodeAddressSolana(publicKey);
357
- case "bech32m":
358
- case "bech32":
359
- case "base58check":
360
- throw new Error("addressFromPublicKey is not implemented for Bitcoin");
361
- }
362
- };
363
-
364
- const CACHE = new Map();
365
-
366
- // Normalize an address in a way that it can be compared to other addresses that have also been normalized
367
- const normalizeAddress = address => {
368
- try {
369
- if (!CACHE.has(address)) CACHE.set(address, normalizeAnyAddress(address));
370
- return CACHE.get(address);
371
- } catch (cause) {
372
- throw new Error(`Unable to normalize address: ${address}`, {
373
- cause
374
- });
375
- }
376
- };
377
- const normalizeAnyAddress = address => {
378
- switch (detectAddressEncoding(address)) {
379
- case "ethereum":
380
- return checksumEthereumAddress(address);
381
- case "bech32m":
382
- case "bech32":
383
- case "base58check":
384
- case "base58solana":
385
- return address;
386
- case "ss58":
387
- {
388
- const [pk] = decodeSs58Address(address);
389
- return encodeAddressSs58(pk, 42);
390
- }
391
- }
392
- };
393
-
394
- const isAddressEqual = (address1, address2) => {
395
- try {
396
- return normalizeAddress(address1) === normalizeAddress(address2);
397
- } catch (err) {
398
- // if normalization fails, assume the addresses are not equal
399
- return false;
400
- }
401
- };
402
-
403
- const encodeAnyAddress = (address, options) => {
404
- // this leverages cache
405
- const encoding = detectAddressEncoding(address);
406
-
407
- // this does NOT leverage cache
408
- if (encoding === "ss58" && options?.ss58Format !== undefined) {
409
- const [publicKey] = decodeSs58Address(address);
410
- return encodeAddressSs58(publicKey, options?.ss58Format ?? 42);
411
- }
412
-
413
- // this leverages cache
414
- return normalizeAddress(address);
415
- };
416
-
417
- const isAddressValid = address => {
418
- try {
419
- detectAddressEncoding(address);
420
- return true;
421
- } catch {
422
- return false;
423
- }
424
- };
425
-
426
- // Inspired from MIT licensed @polkadot-labs/hdkd helpers
427
- // https://github.com/polkadot-labs/hdkd/blob/3ef6e02827212d934b59a4e566d8aa61d3ba7b27/packages/hdkd-helpers/src/parseDerivations.ts#L1
428
-
429
- const DERIVATION_RE = /(\/{1,2})(\w+)/g;
430
- const parseSubstrateDerivations = derivationsStr => {
431
- const derivations = [];
432
- if (derivations) for (const [_, type, code] of derivationsStr.matchAll(DERIVATION_RE)) {
433
- derivations.push([type === "//" ? "hard" : "soft", code]);
434
- }
435
- return derivations;
436
- };
437
- const createChainCode = code => {
438
- const chainCode = new Uint8Array(32);
439
- chainCode.set(Number.isNaN(+code) ? str.enc(code) : u32.enc(+code));
440
- return chainCode;
441
- };
442
- const derivationCodec = /* @__PURE__ */Tuple(str, Bytes(32), Bytes(32));
443
- const createSubstrateDeriveFn = prefix => (seed, chainCode) => blake2b256(derivationCodec.enc([prefix, seed, chainCode]));
444
- const deriveSubstrateSecretKey = (seed, derivationPath, prefix) => {
445
- const derivations = parseSubstrateDerivations(derivationPath);
446
- const derive = createSubstrateDeriveFn(prefix);
447
- return derivations.reduce((seed, [type, chainCode]) => {
448
- const code = createChainCode(chainCode);
449
- if (type === "soft") throw new Error("Soft derivations are not supported");
450
- return derive(seed, code);
451
- }, seed);
452
- };
453
-
454
- const deriveEcdsa = (seed, derivationPath) => {
455
- const secretKey = deriveSubstrateSecretKey(seed, derivationPath, "Secp256k1HDKD");
456
- const publicKey = getPublicKeyEcdsa(secretKey);
457
- return {
458
- type: "ecdsa",
459
- secretKey,
460
- publicKey,
461
- address: addressFromPublicKey(publicKey, "ss58")
462
- };
463
- };
464
- const getPublicKeyEcdsa = secretKey => {
465
- return secp256k1.getPublicKey(secretKey);
466
- };
467
-
468
- const deriveEd25519 = (seed, derivationPath) => {
469
- const secretKey = deriveSubstrateSecretKey(seed, derivationPath, "Ed25519HDKD");
470
- const publicKey = getPublicKeyEd25519(secretKey);
471
- return {
472
- type: "ed25519",
473
- secretKey,
474
- publicKey,
475
- address: addressFromPublicKey(publicKey, "ss58")
476
- };
477
- };
478
- const getPublicKeyEd25519 = secretKey => {
479
- // When importing ed25519 polkadot-js accounts via json, which we do inside of `packages/extension-core/src/domains/keyring/getSecretKeyFromPjsJson.ts`,
480
- // the secretKey we produce is 64 bytes in length.
481
- //
482
- // When using the ed25519 curve to derive a publicKey for this 64 bytes privateKey, we should only take the first 32 bytes:
483
- // - https://github.com/paulmillr/noble-curves/issues/53#issuecomment-1577362759
484
- // - https://github.com/paulmillr/noble-curves/discussions/33#discussioncomment-5685971
485
- // - https://github.com/paulmillr/noble-curves/pull/54
486
- // - https://github.com/paulmillr/noble-curves/issues/88
487
- //
488
- // When you compare the ed25519 publicKey of a given account produced by this function to the publicKey produced by
489
- // polkadot-js, you will find that they are the same as eachother.
490
- if (secretKey.length === 64) {
491
- const [privateComponent, publicComponent] = [secretKey.slice(0, 32), secretKey.slice(32)];
492
- const publicKey = ed25519.getPublicKey(privateComponent);
493
-
494
- // NOTE: We only accept a 64 byte secretKey when the first 32 bytes successfully produce a public key which equals the second 32 bytes of the secretKey.
495
- //
496
- // In this scenario, we assume the creator of the secretKey has given us an array of bytes which equals `[...privateKey, ...publicKey]`.
497
- //
498
- // However, if the second 32 bytes **don't** match the publicKey produced by the first 32 bytes, we no longer know what's going on.
499
- //
500
- // In that case we pass the 64 bytes directly through to `@noble/curves/ed25519`, which we expect will throw the error: `private key of length 32 expected, got 64`.
501
- // But if there's some 64 byte key format for ed25519 we don't know about, or one is added in the future, `@noble/curves/ed25519` can handle that for us instead of throwing.
502
- if (!isUint8ArrayEq(publicComponent, publicKey)) return ed25519.getPublicKey(secretKey);
503
- return publicKey;
504
- }
505
- return ed25519.getPublicKey(secretKey);
506
- };
507
-
508
- /** If a is identical to b, this function returns true, otherwise it returns false */
509
- const isUint8ArrayEq = (a, b) => a.length !== b.length || a.some((v, i) => v !== b[i]) ? false : true;
510
-
511
- const deriveEthereum = (seed, derivationPath) => {
512
- const hdkey = HDKey.fromMasterSeed(seed);
513
- const childKey = hdkey.derive(derivationPath);
514
- if (!childKey.privateKey) throw new Error("Invalid derivation path");
515
- const secretKey = new Uint8Array(childKey.privateKey);
516
- const publicKey = getPublicKeyEthereum(secretKey);
517
- return {
518
- type: "ethereum",
519
- secretKey,
520
- publicKey,
521
- address: addressFromPublicKey(publicKey, "ethereum")
522
- };
523
- };
524
- const getPublicKeyEthereum = secretKey => {
525
- const scalar = secp256k1.utils.normPrivateKeyToScalar(secretKey);
526
- return secp256k1.getPublicKey(scalar, false);
527
- };
528
-
529
- // Convert a path like "m/44'/501'/0'/0'" to an array of indexes
530
- const parseDerivationPath = path => {
531
- if (!path.startsWith("m/")) throw new Error("Path must start with 'm/'");
532
- return path.split("/").slice(1) // Remove 'm'
533
- .map(p => {
534
- if (!p.endsWith("'")) throw new Error("Only hardened derivation is supported");
535
- return parseInt(p.slice(0, -1), 10) + 0x80000000; // Convert to hardened index
536
- });
537
- };
538
- const deriveSolana = (seed, derivationPath) => {
539
- // Generate master private key using SLIP-0010 (HMAC-SHA512 with "ed25519 seed")
540
- let I = hmac(sha512, new TextEncoder().encode("ed25519 seed"), seed);
541
- let secretKey = I.slice(0, 32);
542
- let chainCode = I.slice(32);
543
-
544
- // Iterate over the derivation path and apply hardened key derivation
545
- for (const index of parseDerivationPath(derivationPath)) {
546
- // HMAC-SHA512(Key = chainCode, Data = 0x00 || privateKey || index)
547
- const data = new Uint8Array(1 + secretKey.length + 4);
548
- data.set([0x00], 0);
549
- data.set(secretKey, 1);
550
- data.set(new Uint8Array([index >> 24 & 0xff, index >> 16 & 0xff, index >> 8 & 0xff, index & 0xff]), 1 + secretKey.length);
551
- I = hmac(sha512, chainCode, data);
552
- secretKey = I.slice(0, 32);
553
- chainCode = I.slice(32);
554
- }
555
- const publicKey = getPublicKeySolana(secretKey);
556
- return {
557
- type: "solana",
558
- secretKey,
559
- publicKey,
560
- address: addressFromPublicKey(publicKey, "base58solana")
561
- };
562
- };
563
- const getPublicKeySolana = secretKey => {
564
- return ed25519.getPublicKey(secretKey);
565
- };
566
-
567
- const deriveSr25519 = (seed, derivationPath) => {
568
- const derivations = parseSubstrateDerivations(derivationPath);
569
- const secretKey = derivations.reduce((secretKey, [type, chainCode]) => {
570
- const deriveFn = type === "hard" ? HDKD.secretHard : HDKD.secretSoft;
571
- const code = createChainCode(chainCode);
572
- return deriveFn(secretKey, code, randomBytes);
573
- }, secretFromSeed(seed));
574
- const publicKey = getPublicKeySr25519(secretKey);
575
- return {
576
- type: "sr25519",
577
- secretKey,
578
- publicKey,
579
- address: addressFromPublicKey(publicKey, "ss58")
580
- };
581
- };
582
- const getPublicKeySr25519 = getPublicKey;
583
-
584
- const deriveKeypair = (seed, derivationPath, curve) => {
585
- switch (curve) {
586
- case "sr25519":
587
- return deriveSr25519(seed, derivationPath);
588
- case "ed25519":
589
- return deriveEd25519(seed, derivationPath);
590
- case "ecdsa":
591
- return deriveEcdsa(seed, derivationPath);
592
- case "bitcoin-ecdsa":
593
- case "bitcoin-ed25519":
594
- throw new Error("deriveKeypair is not implemented for Bitcoin");
595
- case "ethereum":
596
- return deriveEthereum(seed, derivationPath);
597
- case "solana":
598
- return deriveSolana(seed, derivationPath);
599
- }
600
- };
601
- const getPublicKeyFromSecret = (secretKey, curve) => {
602
- switch (curve) {
603
- case "ecdsa":
604
- return getPublicKeyEcdsa(secretKey);
605
- case "ethereum":
606
- return getPublicKeyEthereum(secretKey);
607
- case "sr25519":
608
- return getPublicKeySr25519(secretKey);
609
- case "ed25519":
610
- return getPublicKeyEd25519(secretKey);
611
- case "bitcoin-ecdsa":
612
- case "bitcoin-ed25519":
613
- throw new Error("getPublicKeyFromSecret is not implemented for Bitcoin");
614
- case "solana":
615
- return getPublicKeySolana(secretKey);
616
- }
617
- };
618
- const addressFromMnemonic = async (mnemonic, derivationPath, curve) => {
619
- const entropy = mnemonicToEntropy(mnemonic);
620
- const seed = await entropyToSeed(entropy, curve);
621
- const {
622
- address
623
- } = deriveKeypair(seed, derivationPath, curve);
624
- return address;
625
- };
626
- const removeHexPrefix = secretKey => {
627
- if (secretKey.startsWith("0x")) return secretKey.slice(2);
628
- return secretKey;
629
- };
630
- const parseSecretKey = (secretKey, platform) => {
631
- switch (platform) {
632
- case "ethereum":
633
- {
634
- const privateKey = removeHexPrefix(secretKey);
635
- return hex.decode(privateKey);
636
- }
637
- case "solana":
638
- {
639
- const bytes = secretKey.startsWith("[") ?
640
- // JSON bytes array (ex: solflare)
641
- Uint8Array.from(JSON.parse(secretKey)) :
642
- // base58 encoded string (ex: phantom)
643
- base58.decode(secretKey);
644
- if (bytes.length === 64) {
645
- const privateKey = bytes.slice(0, 32);
646
- const publicKey = bytes.slice(32, 64);
647
- const computedPublicKey = getPublicKeySolana(privateKey);
648
- if (!publicKey.every((b, i) => b === computedPublicKey[i])) throw new Error("Invalid Solana secret key: public key does not match");
649
- return privateKey;
650
- } else if (bytes.length === 32) return bytes;
651
- throw new Error("Invalid Solana secret key length");
652
- }
653
- default:
654
- throw new Error("Not implemented");
655
- }
656
- };
657
-
658
- // @dev: didn't find a reliable source of information on which characters are valid => assume it s valid if a keypair can be generated from it
659
- const isValidDerivationPath = async (derivationPath, curve) => {
660
- try {
661
- deriveKeypair(await getDevSeed(curve), derivationPath, curve);
662
- return true;
663
- } catch (err) {
664
- return false;
665
- }
666
- };
667
-
668
- const getAccountPlatformFromCurve = curve => {
669
- switch (curve) {
670
- case "sr25519":
671
- case "ed25519":
672
- case "ecdsa":
673
- return "polkadot";
674
- case "ethereum":
675
- return "ethereum";
676
- case "bitcoin-ecdsa":
677
- case "bitcoin-ed25519":
678
- return "bitcoin";
679
- case "solana":
680
- return "solana";
681
- }
682
- };
683
- const getAccountPlatformFromEncoding = encoding => {
684
- switch (encoding) {
685
- case "ss58":
686
- return "polkadot";
687
- case "ethereum":
688
- return "ethereum";
689
- case "bech32m":
690
- case "bech32":
691
- case "base58check":
692
- return "bitcoin";
693
- case "base58solana":
694
- return "solana";
695
- }
696
- };
697
- const getAccountPlatformFromAddress = address => {
698
- const encoding = detectAddressEncoding(address);
699
- return getAccountPlatformFromEncoding(encoding);
700
- };
701
-
702
- const encryptKemAead = async (publicKey, plaintext) => {
703
- const kem = new MlKem768();
704
- const [kemCt, sharedSecret] = await kem.encap(publicKey);
705
- if (sharedSecret.length !== 32) {
706
- throw new Error(`Expected 32-byte shared secret, got ${sharedSecret.length}`);
707
- }
708
- const nonce = crypto.getRandomValues(new Uint8Array(24));
709
- const aead = xchacha20poly1305(sharedSecret, nonce);
710
- const aeadCt = aead.encrypt(plaintext);
711
- const kemLen = new Uint8Array(2);
712
- new DataView(kemLen.buffer).setUint16(0, kemCt.length, true);
713
- return concatBytes(kemLen, kemCt, nonce, aeadCt);
714
- };
715
-
716
- export { DEV_MNEMONIC_ETHEREUM, DEV_MNEMONIC_POLKADOT, addressEncodingFromCurve, addressFromMnemonic, addressFromPublicKey, blake2b256, blake2b512, blake3, checksumEthereumAddress, decodeSs58Address, deriveKeypair, detectAddressEncoding, encodeAddressEthereum, encodeAddressSolana, encodeAddressSs58, encodeAnyAddress, encryptKemAead, entropyToMnemonic, entropyToSeed, fromBase58Check, fromBech32, fromBech32m, generateMnemonic, getAccountPlatformFromAddress, getAccountPlatformFromCurve, getAccountPlatformFromEncoding, getDevSeed, getPublicKeyFromSecret, getSafeHash, isAddressEqual, isAddressValid, isBase58CheckAddress, isBech32Address, isBech32mAddress, isBitcoinAddress, isEthereumAddress, isSolanaAddress, isSs58Address, isValidDerivationPath, isValidMnemonic, mnemonicToEntropy, normalizeAddress, parseSecretKey, pbkdf2, removeHexPrefix };