@talismn/crypto 0.3.5 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,816 +1,877 @@
1
- "use strict";
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region \0rolldown/runtime.js
2
3
  var __create = Object.create;
3
4
  var __defProp = Object.defineProperty;
4
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
7
  var __getProtoOf = Object.getPrototypeOf;
7
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
9
  var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/index.ts
31
- var index_exports = {};
32
- __export(index_exports, {
33
- DEV_MNEMONIC_ETHEREUM: () => DEV_MNEMONIC_ETHEREUM,
34
- DEV_MNEMONIC_POLKADOT: () => DEV_MNEMONIC_POLKADOT,
35
- addressEncodingFromCurve: () => addressEncodingFromCurve,
36
- addressFromMnemonic: () => addressFromMnemonic,
37
- addressFromPublicKey: () => addressFromPublicKey,
38
- base58: () => import_base4.base58,
39
- base64: () => import_base4.base64,
40
- blake2b256: () => blake2b256,
41
- blake2b512: () => blake2b512,
42
- blake3: () => blake3,
43
- checksumEthereumAddress: () => checksumEthereumAddress,
44
- decodeSs58Address: () => decodeSs58Address,
45
- deriveKeypair: () => deriveKeypair,
46
- detectAddressEncoding: () => detectAddressEncoding,
47
- ed25519: () => import_ed255192.ed25519,
48
- encodeAddressEthereum: () => encodeAddressEthereum,
49
- encodeAddressSolana: () => encodeAddressSolana,
50
- encodeAddressSs58: () => encodeAddressSs58,
51
- encodeAnyAddress: () => encodeAnyAddress,
52
- encryptKemAead: () => encryptKemAead,
53
- entropyToMnemonic: () => entropyToMnemonic,
54
- entropyToSeed: () => entropyToSeed,
55
- fromBase58Check: () => fromBase58Check,
56
- fromBech32: () => fromBech32,
57
- fromBech32m: () => fromBech32m,
58
- generateMnemonic: () => generateMnemonic,
59
- getAccountPlatformFromAddress: () => getAccountPlatformFromAddress,
60
- getAccountPlatformFromCurve: () => getAccountPlatformFromCurve,
61
- getAccountPlatformFromEncoding: () => getAccountPlatformFromEncoding,
62
- getDevSeed: () => getDevSeed,
63
- getPublicKeyFromSecret: () => getPublicKeyFromSecret,
64
- getPublicKeySolana: () => getPublicKeySolana,
65
- getSafeHash: () => getSafeHash,
66
- hex: () => import_base4.hex,
67
- isAddressEqual: () => isAddressEqual,
68
- isAddressValid: () => isAddressValid,
69
- isBase58CheckAddress: () => isBase58CheckAddress,
70
- isBech32Address: () => isBech32Address,
71
- isBech32mAddress: () => isBech32mAddress,
72
- isBitcoinAddress: () => isBitcoinAddress,
73
- isEthereumAddress: () => isEthereumAddress,
74
- isSolanaAddress: () => isSolanaAddress,
75
- isSs58Address: () => isSs58Address,
76
- isValidDerivationPath: () => isValidDerivationPath,
77
- isValidMnemonic: () => isValidMnemonic,
78
- mnemonicToEntropy: () => mnemonicToEntropy,
79
- normalizeAddress: () => normalizeAddress,
80
- parseSecretKey: () => parseSecretKey,
81
- pbkdf2: () => pbkdf2,
82
- removeHexPrefix: () => removeHexPrefix,
83
- utf8: () => import_base4.utf8
84
- });
85
- module.exports = __toCommonJS(index_exports);
86
-
87
- // src/address/encoding/addressEncodingFromCurve.ts
88
- var addressEncodingFromCurve = (curve) => {
89
- switch (curve) {
90
- case "sr25519":
91
- case "ed25519":
92
- case "ecdsa":
93
- return "ss58";
94
- case "bitcoin-ecdsa":
95
- case "bitcoin-ed25519":
96
- return "bech32m";
97
- case "ethereum":
98
- return "ethereum";
99
- case "solana":
100
- return "base58solana";
101
- }
102
- };
103
-
104
- // src/address/encoding/bitcoin.ts
105
- var import_bech32 = require("bech32");
106
- var import_bs58check = __toESM(require("bs58check"));
107
- var isBitcoinAddress = (address) => isBech32mAddress(address) || isBech32Address(address) || isBase58CheckAddress(address);
10
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
+ get: ((k) => from[k]).bind(null, key),
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
+ value: mod,
21
+ enumerable: true
22
+ }) : target, mod));
23
+ //#endregion
24
+ let bech32 = require("bech32");
25
+ let bs58check = require("bs58check");
26
+ bs58check = __toESM(bs58check);
27
+ let _noble_curves_secp256k1_js = require("@noble/curves/secp256k1.js");
28
+ let _noble_hashes_sha3_js = require("@noble/hashes/sha3.js");
29
+ let _noble_hashes_utils_js = require("@noble/hashes/utils.js");
30
+ let _noble_curves_ed25519_js = require("@noble/curves/ed25519.js");
31
+ let _scure_base = require("@scure/base");
32
+ let _noble_hashes_blake2_js = require("@noble/hashes/blake2.js");
33
+ let _noble_hashes_blake3_js = require("@noble/hashes/blake3.js");
34
+ let _noble_hashes_hmac_js = require("@noble/hashes/hmac.js");
35
+ let _noble_hashes_sha2_js = require("@noble/hashes/sha2.js");
36
+ let _scure_bip39 = require("@scure/bip39");
37
+ let _scure_bip39_wordlists_english_js = require("@scure/bip39/wordlists/english.js");
38
+ let scale_ts = require("scale-ts");
39
+ let _scure_bip32 = require("@scure/bip32");
40
+ let _scure_sr25519 = require("@scure/sr25519");
41
+ let _noble_ciphers_chacha_js = require("@noble/ciphers/chacha.js");
42
+ let _noble_ciphers_utils_js = require("@noble/ciphers/utils.js");
43
+ let mlkem = require("mlkem");
44
+ let _noble_ciphers_salsa_js = require("@noble/ciphers/salsa.js");
45
+ let _noble_hashes_scrypt_js = require("@noble/hashes/scrypt.js");
46
+ //#region src/address/encoding/addressEncodingFromCurve.ts
47
+ /** NOTE: Try not to use this too much, it will need to change */
48
+ const addressEncodingFromCurve = (curve) => {
49
+ switch (curve) {
50
+ case "sr25519":
51
+ case "ed25519":
52
+ case "ecdsa": return "ss58";
53
+ case "bitcoin-ecdsa":
54
+ case "bitcoin-ed25519": return "bech32m";
55
+ case "ethereum": return "ethereum";
56
+ case "solana": return "base58solana";
57
+ }
58
+ };
59
+ //#endregion
60
+ //#region src/address/encoding/bitcoin.ts
61
+ const isBitcoinAddress = (address) => isBech32mAddress(address) || isBech32Address(address) || isBase58CheckAddress(address);
108
62
  function isBech32mAddress(address) {
109
- try {
110
- fromBech32m(address);
111
- } catch {
112
- return false;
113
- }
114
- return true;
63
+ try {
64
+ fromBech32m(address);
65
+ } catch {
66
+ return false;
67
+ }
68
+ return true;
115
69
  }
116
70
  function isBech32Address(address) {
117
- try {
118
- fromBech32(address);
119
- } catch {
120
- return false;
121
- }
122
- return true;
71
+ try {
72
+ fromBech32(address);
73
+ } catch {
74
+ return false;
75
+ }
76
+ return true;
123
77
  }
124
78
  function isBase58CheckAddress(address) {
125
- try {
126
- fromBase58Check(address);
127
- } catch {
128
- return false;
129
- }
130
- return true;
79
+ try {
80
+ fromBase58Check(address);
81
+ } catch {
82
+ return false;
83
+ }
84
+ return true;
131
85
  }
86
+ /**
87
+ * Converts a Bech32m encoded address to its corresponding data representation.
88
+ * @param address - The Bech32m encoded address.
89
+ * @returns An object containing the version, prefix, and data of the address.
90
+ * @throws {TypeError} If the address uses the wrong encoding.
91
+ */
132
92
  function fromBech32m(address) {
133
- const result = import_bech32.bech32m.decode(address);
134
- const version = result.words[0];
135
- if (version === 0) throw new TypeError(`${address} uses wrong encoding`);
136
- const data = import_bech32.bech32m.fromWords(result.words.slice(1));
137
- return {
138
- version,
139
- prefix: result.prefix,
140
- data: Uint8Array.from(data)
141
- };
93
+ const result = bech32.bech32m.decode(address);
94
+ const version = result.words[0];
95
+ if (version === 0) throw new TypeError(`${address} uses wrong encoding`);
96
+ const data = bech32.bech32m.fromWords(result.words.slice(1));
97
+ return {
98
+ version,
99
+ prefix: result.prefix,
100
+ data: Uint8Array.from(data)
101
+ };
142
102
  }
103
+ /**
104
+ * Converts a Bech32 encoded address to its corresponding data representation.
105
+ * @param address - The Bech32 encoded address.
106
+ * @returns An object containing the version, prefix, and data of the address.
107
+ * @throws {TypeError} If the address uses the wrong encoding.
108
+ */
143
109
  function fromBech32(address) {
144
- const result = import_bech32.bech32.decode(address);
145
- const version = result.words[0];
146
- if (version !== 0) throw new TypeError(`${address} uses wrong encoding`);
147
- const data = import_bech32.bech32.fromWords(result.words.slice(1));
148
- return {
149
- version,
150
- prefix: result.prefix,
151
- data: Uint8Array.from(data)
152
- };
110
+ const result = bech32.bech32.decode(address);
111
+ const version = result.words[0];
112
+ if (version !== 0) throw new TypeError(`${address} uses wrong encoding`);
113
+ const data = bech32.bech32.fromWords(result.words.slice(1));
114
+ return {
115
+ version,
116
+ prefix: result.prefix,
117
+ data: Uint8Array.from(data)
118
+ };
153
119
  }
120
+ /**
121
+ * Decodes a base58check encoded Bitcoin address and returns the version and hash.
122
+ *
123
+ * @param address - The base58check encoded Bitcoin address to decode.
124
+ * @returns An object containing the version and hash of the decoded address.
125
+ * @throws {TypeError} If the address is too short or too long.
126
+ */
154
127
  function fromBase58Check(address) {
155
- const payload = import_bs58check.default.decode(address);
156
- if (payload.length < 21) throw new TypeError(`${address} is too short`);
157
- if (payload.length > 21) throw new TypeError(`${address} is too long`);
158
- function readUInt8(buffer, offset) {
159
- if (offset + 1 > buffer.length) {
160
- throw new Error("Offset is outside the bounds of Uint8Array");
161
- }
162
- return buffer[offset];
163
- }
164
- const version = readUInt8(payload, 0);
165
- const hash = payload.slice(1);
166
- return { version, hash };
128
+ const payload = bs58check.default.decode(address);
129
+ if (payload.length < 21) throw new TypeError(`${address} is too short`);
130
+ if (payload.length > 21) throw new TypeError(`${address} is too long`);
131
+ function readUInt8(buffer, offset) {
132
+ if (offset + 1 > buffer.length) throw new Error("Offset is outside the bounds of Uint8Array");
133
+ return buffer[offset];
134
+ }
135
+ return {
136
+ version: readUInt8(payload, 0),
137
+ hash: payload.slice(1)
138
+ };
167
139
  }
168
-
169
- // src/address/encoding/ethereum.ts
170
- var import_sha3 = require("@noble/hashes/sha3.js");
171
- var import_utils = require("@noble/hashes/utils.js");
172
- var encodeAddressEthereum = (publicKey) => {
173
- if (publicKey[0] !== 4) throw new Error("Invalid public key format");
174
- const publicKeyWithoutPrefix = publicKey.slice(1);
175
- const hash = (0, import_sha3.keccak_256)(publicKeyWithoutPrefix);
176
- const address = hash.slice(-20);
177
- return checksumEthereumAddress(`0x${(0, import_utils.bytesToHex)(address)}`);
178
- };
179
- var checksumEthereumAddress = (address) => {
180
- const addr = address.toLowerCase().replace(/^0x/, "");
181
- const hash = (0, import_sha3.keccak_256)(new TextEncoder().encode(addr));
182
- const hashHex = (0, import_utils.bytesToHex)(hash);
183
- const checksum = addr.split("").map((char, i) => parseInt(hashHex[i], 16) >= 8 ? char.toUpperCase() : char).join("");
184
- return `0x${checksum}`;
140
+ //#endregion
141
+ //#region src/address/encoding/ethereum.ts
142
+ /**
143
+ * Encodes a public key using H160 encoding with Ethereum checksum.
144
+ * Accepts both uncompressed (65 bytes) and compressed (33 bytes) public keys -
145
+ * polkadot-js keystores store the compressed form as the account address.
146
+ */
147
+ const encodeAddressEthereum = (publicKey) => {
148
+ if (publicKey.length === 33 && (publicKey[0] === 2 || publicKey[0] === 3)) publicKey = _noble_curves_secp256k1_js.secp256k1.Point.fromBytes(publicKey).toBytes(false);
149
+ if (publicKey[0] !== 4) throw new Error("Invalid public key format");
150
+ const address = (0, _noble_hashes_sha3_js.keccak_256)(publicKey.slice(1)).slice(-20);
151
+ return checksumEthereumAddress(`0x${(0, _noble_hashes_utils_js.bytesToHex)(address)}`);
152
+ };
153
+ const checksumEthereumAddress = (address) => {
154
+ const addr = address.toLowerCase().replace(/^0x/, "");
155
+ const hashHex = (0, _noble_hashes_utils_js.bytesToHex)((0, _noble_hashes_sha3_js.keccak_256)(new TextEncoder().encode(addr)));
156
+ return `0x${addr.split("").map((char, i) => parseInt(hashHex[i], 16) >= 8 ? char.toUpperCase() : char).join("")}`;
185
157
  };
186
158
  function isEthereumAddress(address) {
187
- return /^0x[a-fA-F0-9]{40}$/.test(address);
159
+ return /^0x[a-fA-F0-9]{40}$/.test(address);
188
160
  }
189
-
190
- // src/address/encoding/solana.ts
191
- var import_base = require("@scure/base");
192
- var encodeAddressSolana = (publicKey) => {
193
- if (publicKey.length !== 32)
194
- throw new Error("Public key must be 32 bytes long for Solana base58 encoding");
195
- return import_base.base58.encode(publicKey);
161
+ //#endregion
162
+ //#region src/address/encoding/solana.ts
163
+ const encodeAddressSolana = (publicKey) => {
164
+ if (publicKey.length !== 32) throw new Error("Public key must be 32 bytes long for Solana base58 encoding");
165
+ return _scure_base.base58.encode(publicKey);
196
166
  };
197
167
  function isSolanaAddress(address) {
198
- try {
199
- const bytes = import_base.base58.decode(address);
200
- return bytes.length === 32;
201
- } catch {
202
- return false;
203
- }
168
+ try {
169
+ return _scure_base.base58.decode(address).length === 32;
170
+ } catch {
171
+ return false;
172
+ }
204
173
  }
205
-
206
- // src/address/encoding/ss58.ts
207
- var import_base3 = require("@scure/base");
208
-
209
- // src/hashing/index.ts
210
- var import_blake2 = require("@noble/hashes/blake2.js");
211
- var import_blake3 = require("@noble/hashes/blake3.js");
212
- var import_base2 = require("@scure/base");
213
- var blake3 = import_blake3.blake3;
214
- var blake2b256 = (msg) => (0, import_blake2.blake2b)(msg, { dkLen: 32 });
215
- var blake2b512 = (msg) => (0, import_blake2.blake2b)(msg, { dkLen: 64 });
216
- var getSafeHash = (bytes) => {
217
- return import_base2.base58.encode((0, import_blake3.blake3)(bytes));
218
- };
219
-
220
- // src/address/encoding/ss58.ts
221
- var VALID_PUBLICKEY_LENGTHS = [32, 33];
222
- var accountId = (publicKey) => {
223
- if (!VALID_PUBLICKEY_LENGTHS.includes(publicKey.length)) throw new Error("Invalid publicKey");
224
- return publicKey.length === 33 ? blake2b256(publicKey) : publicKey;
225
- };
226
- var SS58PRE = /* @__PURE__ */ new TextEncoder().encode("SS58PRE");
227
- var CHECKSUM_LENGTH = 2;
228
- var VALID_PAYLOAD_LENGTHS = [32, 33];
229
- var ss58Encode = (payload, prefix = 42) => {
230
- if (!VALID_PAYLOAD_LENGTHS.includes(payload.length)) throw new Error("Invalid payload");
231
- const prefixBytes = prefix < 64 ? Uint8Array.of(prefix) : Uint8Array.of(
232
- (prefix & 252) >> 2 | 64,
233
- prefix >> 8 | (prefix & 3) << 6
234
- );
235
- const checksum = blake2b512(Uint8Array.of(...SS58PRE, ...prefixBytes, ...payload)).subarray(
236
- 0,
237
- CHECKSUM_LENGTH
238
- );
239
- return import_base3.base58.encode(Uint8Array.of(...prefixBytes, ...payload, ...checksum));
240
- };
241
- var VALID_ADDRESS_LENGTHS = [35, 36, 37];
242
- var decodeSs58Address = (addressStr) => {
243
- const address = import_base3.base58.decode(addressStr);
244
- if (!VALID_ADDRESS_LENGTHS.includes(address.length)) throw new Error("Invalid address length");
245
- const addressChecksum = address.subarray(address.length - CHECKSUM_LENGTH);
246
- const checksum = blake2b512(
247
- Uint8Array.of(...SS58PRE, ...address.subarray(0, address.length - CHECKSUM_LENGTH))
248
- ).subarray(0, CHECKSUM_LENGTH);
249
- if (addressChecksum[0] !== checksum[0] || addressChecksum[1] !== checksum[1])
250
- throw new Error("Invalid checksum");
251
- const prefixLength = address[0] & 64 ? 2 : 1;
252
- const prefix = prefixLength === 1 ? address[0] : (address[0] & 63) << 2 | address[1] >> 6 | (address[1] & 63) << 8;
253
- const publicKey = address.slice(prefixLength, address.length - CHECKSUM_LENGTH);
254
- return [publicKey, prefix];
255
- };
256
- var encodeAddressSs58 = (publicKey, prefix = 42) => {
257
- if (typeof publicKey === "string") [publicKey] = decodeSs58Address(publicKey);
258
- return ss58Encode(accountId(publicKey), prefix);
174
+ /**
175
+ * Whether the address is a valid ed25519 public key (on-curve).
176
+ *
177
+ * Program-derived addresses (PDAs) are off-curve by construction: no private key can exist
178
+ * for them, so a regular wallet cannot recover tokens sent to an account they own.
179
+ */
180
+ function isOnCurveSolanaAddress(address) {
181
+ try {
182
+ _noble_curves_ed25519_js.ed25519.Point.fromBytes(_scure_base.base58.decode(address));
183
+ return true;
184
+ } catch {
185
+ return false;
186
+ }
187
+ }
188
+ //#endregion
189
+ //#region src/hashing/index.ts
190
+ const blake3 = _noble_hashes_blake3_js.blake3;
191
+ const blake2b256 = (msg) => (0, _noble_hashes_blake2_js.blake2b)(msg, { dkLen: 32 });
192
+ const blake2b512 = (msg) => (0, _noble_hashes_blake2_js.blake2b)(msg, { dkLen: 64 });
193
+ const getSafeHash = (bytes) => {
194
+ return _scure_base.base58.encode((0, _noble_hashes_blake3_js.blake3)(bytes));
195
+ };
196
+ //#endregion
197
+ //#region src/address/encoding/ss58.ts
198
+ const VALID_PUBLICKEY_LENGTHS = [32, 33];
199
+ const accountId = (publicKey) => {
200
+ if (!VALID_PUBLICKEY_LENGTHS.includes(publicKey.length)) throw new Error("Invalid publicKey");
201
+ return publicKey.length === 33 ? blake2b256(publicKey) : publicKey;
202
+ };
203
+ const SS58PRE = /* @__PURE__ */ new TextEncoder().encode("SS58PRE");
204
+ const CHECKSUM_LENGTH = 2;
205
+ const VALID_PAYLOAD_LENGTHS = [32, 33];
206
+ const ss58Encode = (payload, prefix = 42) => {
207
+ if (!VALID_PAYLOAD_LENGTHS.includes(payload.length)) throw new Error("Invalid payload");
208
+ const prefixBytes = prefix < 64 ? Uint8Array.of(prefix) : Uint8Array.of((prefix & 252) >> 2 | 64, prefix >> 8 | (prefix & 3) << 6);
209
+ const checksum = blake2b512(Uint8Array.of(...SS58PRE, ...prefixBytes, ...payload)).subarray(0, CHECKSUM_LENGTH);
210
+ return _scure_base.base58.encode(Uint8Array.of(...prefixBytes, ...payload, ...checksum));
211
+ };
212
+ const VALID_ADDRESS_LENGTHS = [
213
+ 35,
214
+ 36,
215
+ 37
216
+ ];
217
+ const decodeSs58Address = (addressStr, ignoreChecksum = false) => {
218
+ const address = _scure_base.base58.decode(addressStr);
219
+ if (!VALID_ADDRESS_LENGTHS.includes(address.length)) throw new Error("Invalid address length");
220
+ const addressChecksum = address.subarray(address.length - CHECKSUM_LENGTH);
221
+ const checksum = blake2b512(Uint8Array.of(...SS58PRE, ...address.subarray(0, address.length - CHECKSUM_LENGTH))).subarray(0, CHECKSUM_LENGTH);
222
+ if (!ignoreChecksum && (addressChecksum[0] !== checksum[0] || addressChecksum[1] !== checksum[1])) throw new Error("Invalid checksum");
223
+ const prefixLength = address[0] & 64 ? 2 : 1;
224
+ const prefix = prefixLength === 1 ? address[0] : (address[0] & 63) << 2 | address[1] >> 6 | (address[1] & 63) << 8;
225
+ return [address.slice(prefixLength, address.length - CHECKSUM_LENGTH), prefix];
226
+ };
227
+ const encodeAddressSs58 = (publicKey, prefix = 42) => {
228
+ if (typeof publicKey === "string") [publicKey] = decodeSs58Address(publicKey);
229
+ return ss58Encode(accountId(publicKey), prefix);
259
230
  };
260
231
  function isSs58Address(address) {
261
- try {
262
- decodeSs58Address(address);
263
- return true;
264
- } catch {
265
- return false;
266
- }
232
+ try {
233
+ decodeSs58Address(address);
234
+ return true;
235
+ } catch {
236
+ return false;
237
+ }
267
238
  }
268
-
269
- // src/address/encoding/detectAddressEncoding.ts
270
- var CACHE = /* @__PURE__ */ new Map();
271
- var detectAddressEncodingInner = (address) => {
272
- if (isEthereumAddress(address)) return "ethereum";
273
- if (isSs58Address(address)) return "ss58";
274
- if (isSolanaAddress(address)) return "base58solana";
275
- if (isBech32mAddress(address)) return "bech32m";
276
- if (isBech32Address(address)) return "bech32";
277
- if (isBase58CheckAddress(address)) return "base58check";
278
- throw new Error(`Unknown address encoding`);
279
- };
280
- var detectAddressEncoding = (address) => {
281
- if (!CACHE.has(address)) CACHE.set(address, detectAddressEncodingInner(address));
282
- return CACHE.get(address);
283
- };
284
-
285
- // src/address/addressFromPublicKey.ts
286
- var addressFromPublicKey = (publicKey, encoding, options) => {
287
- switch (encoding) {
288
- case "ss58":
289
- return encodeAddressSs58(publicKey, options?.ss58Prefix);
290
- case "ethereum":
291
- return encodeAddressEthereum(publicKey);
292
- case "base58solana":
293
- return encodeAddressSolana(publicKey);
294
- case "bech32m":
295
- case "bech32":
296
- case "base58check":
297
- throw new Error("addressFromPublicKey is not implemented for Bitcoin");
298
- }
299
- };
300
-
301
- // src/address/normalizeAddress.ts
302
- var CACHE2 = /* @__PURE__ */ new Map();
303
- var normalizeAddress = (address) => {
304
- try {
305
- if (!CACHE2.has(address)) CACHE2.set(address, normalizeAnyAddress(address));
306
- return CACHE2.get(address);
307
- } catch (cause) {
308
- throw new Error(`Unable to normalize address: ${address}`, { cause });
309
- }
310
- };
311
- var normalizeAnyAddress = (address) => {
312
- switch (detectAddressEncoding(address)) {
313
- case "ethereum":
314
- return checksumEthereumAddress(address);
315
- case "bech32m":
316
- case "bech32":
317
- case "base58check":
318
- case "base58solana":
319
- return address;
320
- case "ss58": {
321
- const [pk] = decodeSs58Address(address);
322
- return encodeAddressSs58(pk, 42);
323
- }
324
- }
325
- };
326
-
327
- // src/address/encodeAnyAddress.ts
328
- var encodeAnyAddress = (address, options) => {
329
- const encoding = detectAddressEncoding(address);
330
- if (encoding === "ss58" && options?.ss58Format !== void 0) {
331
- const [publicKey] = decodeSs58Address(address);
332
- return encodeAddressSs58(publicKey, options?.ss58Format ?? 42);
333
- }
334
- return normalizeAddress(address);
335
- };
336
-
337
- // src/address/isAddressEqual.ts
338
- var isAddressEqual = (address1, address2) => {
339
- try {
340
- return normalizeAddress(address1) === normalizeAddress(address2);
341
- } catch {
342
- return false;
343
- }
344
- };
345
-
346
- // src/address/isAddressValid.ts
347
- var isAddressValid = (address) => {
348
- try {
349
- detectAddressEncoding(address);
350
- return true;
351
- } catch {
352
- return false;
353
- }
354
- };
355
-
356
- // src/derivation/deriveSolana.ts
357
- var import_ed25519 = require("@noble/curves/ed25519.js");
358
- var import_hmac = require("@noble/hashes/hmac.js");
359
- var import_sha2 = require("@noble/hashes/sha2.js");
360
- var parseDerivationPath = (path) => {
361
- if (!path.startsWith("m/")) throw new Error("Path must start with 'm/'");
362
- return path.split("/").slice(1).map((p) => {
363
- if (!p.endsWith("'")) throw new Error("Only hardened derivation is supported");
364
- return parseInt(p.slice(0, -1), 10) + 2147483648;
365
- });
366
- };
367
- var deriveSolana = (seed, derivationPath) => {
368
- let I = (0, import_hmac.hmac)(import_sha2.sha512, new TextEncoder().encode("ed25519 seed"), seed);
369
- let secretKey = I.slice(0, 32);
370
- let chainCode = I.slice(32);
371
- for (const index of parseDerivationPath(derivationPath)) {
372
- const data = new Uint8Array(1 + secretKey.length + 4);
373
- data.set([0], 0);
374
- data.set(secretKey, 1);
375
- data.set(
376
- new Uint8Array([
377
- index >> 24 & 255,
378
- index >> 16 & 255,
379
- index >> 8 & 255,
380
- index & 255
381
- ]),
382
- 1 + secretKey.length
383
- );
384
- I = (0, import_hmac.hmac)(import_sha2.sha512, chainCode, data);
385
- secretKey = I.slice(0, 32);
386
- chainCode = I.slice(32);
387
- }
388
- const publicKey = getPublicKeySolana(secretKey);
389
- return {
390
- type: "solana",
391
- secretKey,
392
- publicKey,
393
- address: addressFromPublicKey(publicKey, "base58solana")
394
- };
395
- };
396
- var getPublicKeySolana = (secretKey) => {
397
- return import_ed25519.ed25519.getPublicKey(secretKey);
398
- };
399
-
400
- // src/derivation/utils.ts
401
- var import_base5 = require("@scure/base");
402
-
403
- // src/mnemonic/index.ts
404
- var import_bip39 = require("@scure/bip39");
405
- var import_english = require("@scure/bip39/wordlists/english.js");
406
-
407
- // src/utils/exports.ts
408
- var import_ed255192 = require("@noble/curves/ed25519.js");
409
- var import_base4 = require("@scure/base");
410
-
411
- // src/utils/pbkdf2.ts
412
- var pbkdf2 = async (hash, entropy, salt, iterations, outputLenBytes) => {
413
- const keyMaterial = await crypto.subtle.importKey(
414
- "raw",
415
- entropy,
416
- "PBKDF2",
417
- false,
418
- ["deriveBits"]
419
- );
420
- const derivedBits = await crypto.subtle.deriveBits(
421
- { name: "PBKDF2", salt, iterations, hash },
422
- keyMaterial,
423
- outputLenBytes * 8
424
- );
425
- return new Uint8Array(derivedBits);
426
- };
427
-
428
- // src/mnemonic/index.ts
429
- var mnemonicToEntropy = (mnemonic) => {
430
- return (0, import_bip39.mnemonicToEntropy)(mnemonic, import_english.wordlist);
431
- };
432
- var entropyToMnemonic = (entropy) => {
433
- return (0, import_bip39.entropyToMnemonic)(entropy, import_english.wordlist);
434
- };
435
- var entropyToSeedSubstrate = async (entropy, password) => await pbkdf2(
436
- "SHA-512",
437
- entropy,
438
- mnemonicPasswordToSalt(password ?? ""),
439
- 2048,
440
- // 2048 iterations
441
- 32
442
- // 32 bytes (32 * 8 == 256 bits)
443
- );
444
- var entropyToSeedClassic = async (entropy, password) => await pbkdf2(
445
- "SHA-512",
446
- encodeNormalized(entropyToMnemonic(entropy)),
447
- mnemonicPasswordToSalt(password ?? ""),
448
- 2048,
449
- // 2048 iterations
450
- 64
451
- // 64 bytes (64 * 8 == 512 bits)
452
- );
453
- var mnemonicPasswordToSalt = (password) => encodeNormalized(`mnemonic${password}`);
454
- var encodeNormalized = (utf82) => new TextEncoder().encode(utf82.normalize("NFKD"));
455
- var getSeedDerivationType = (curve) => {
456
- switch (curve) {
457
- case "sr25519":
458
- case "ed25519":
459
- case "ecdsa":
460
- return "substrate";
461
- case "ethereum":
462
- case "solana":
463
- return "classic";
464
- case "bitcoin-ecdsa":
465
- case "bitcoin-ed25519":
466
- throw new Error("seed derivation is not implemented for Bitcoin");
467
- }
468
- };
469
- var entropyToSeed = async (entropy, curve, password) => {
470
- const type = getSeedDerivationType(curve);
471
- switch (type) {
472
- case "classic":
473
- return await entropyToSeedClassic(entropy, password);
474
- case "substrate":
475
- return await entropyToSeedSubstrate(entropy, password);
476
- }
477
- };
478
- var isValidMnemonic = (mnemonic) => {
479
- return (0, import_bip39.validateMnemonic)(mnemonic, import_english.wordlist);
480
- };
481
- var generateMnemonic = (words) => {
482
- switch (words) {
483
- case 12:
484
- return (0, import_bip39.generateMnemonic)(import_english.wordlist, 128);
485
- case 24:
486
- return (0, import_bip39.generateMnemonic)(import_english.wordlist, 256);
487
- }
488
- };
489
- var DEV_MNEMONIC_POLKADOT = "bottom drive obey lake curtain smoke basket hold race lonely fit walk";
490
- var DEV_MNEMONIC_ETHEREUM = "test test test test test test test test test test test junk";
491
- var DEV_SEED_CACHE = /* @__PURE__ */ new Map();
492
- var getDevSeed = async (curve) => {
493
- const type = getSeedDerivationType(curve);
494
- if (!DEV_SEED_CACHE.has(type)) {
495
- switch (type) {
496
- case "classic": {
497
- const entropy = mnemonicToEntropy(DEV_MNEMONIC_ETHEREUM);
498
- const seed = await entropyToSeedClassic(entropy);
499
- DEV_SEED_CACHE.set(type, seed);
500
- break;
501
- }
502
- case "substrate": {
503
- const entropy = mnemonicToEntropy(DEV_MNEMONIC_POLKADOT);
504
- const seed = await entropyToSeedSubstrate(entropy);
505
- DEV_SEED_CACHE.set(type, seed);
506
- break;
507
- }
508
- default:
509
- throw new Error("Unsupported derivation type");
510
- }
511
- }
512
- return DEV_SEED_CACHE.get(type);
513
- };
514
-
515
- // src/derivation/deriveEcdsa.ts
516
- var import_secp256k1 = require("@noble/curves/secp256k1.js");
517
-
518
- // src/derivation/common.ts
519
- var import_scale_ts = require("scale-ts");
520
- var DERIVATION_RE = /(\/{1,2})(\w+)/g;
521
- var parseSubstrateDerivations = (derivationsStr) => {
522
- const derivations = [];
523
- if (derivations)
524
- for (const [_, type, code] of derivationsStr.matchAll(DERIVATION_RE)) {
525
- derivations.push([type === "//" ? "hard" : "soft", code]);
526
- }
527
- return derivations;
528
- };
529
- var createChainCode = (code) => {
530
- const chainCode = new Uint8Array(32);
531
- chainCode.set(Number.isNaN(+code) ? import_scale_ts.str.enc(code) : import_scale_ts.u32.enc(+code));
532
- return chainCode;
533
- };
534
- var derivationCodec = /* @__PURE__ */ (0, import_scale_ts.Tuple)(import_scale_ts.str, (0, import_scale_ts.Bytes)(32), (0, import_scale_ts.Bytes)(32));
535
- var createSubstrateDeriveFn = (prefix) => (seed, chainCode) => blake2b256(derivationCodec.enc([prefix, seed, chainCode]));
536
- var deriveSubstrateSecretKey = (seed, derivationPath, prefix) => {
537
- const derivations = parseSubstrateDerivations(derivationPath);
538
- const derive = createSubstrateDeriveFn(prefix);
539
- return derivations.reduce((seed2, [type, chainCode]) => {
540
- const code = createChainCode(chainCode);
541
- if (type === "soft") throw new Error("Soft derivations are not supported");
542
- return derive(seed2, code);
543
- }, seed);
544
- };
545
-
546
- // src/derivation/deriveEcdsa.ts
547
- var deriveEcdsa = (seed, derivationPath) => {
548
- const secretKey = deriveSubstrateSecretKey(seed, derivationPath, "Secp256k1HDKD");
549
- const publicKey = getPublicKeyEcdsa(secretKey);
550
- return {
551
- type: "ecdsa",
552
- secretKey,
553
- publicKey,
554
- address: addressFromPublicKey(publicKey, "ss58")
555
- };
556
- };
557
- var getPublicKeyEcdsa = (secretKey) => {
558
- return import_secp256k1.secp256k1.getPublicKey(secretKey);
559
- };
560
-
561
- // src/derivation/deriveEd25519.ts
562
- var import_ed255193 = require("@noble/curves/ed25519.js");
563
- var deriveEd25519 = (seed, derivationPath) => {
564
- const secretKey = deriveSubstrateSecretKey(seed, derivationPath, "Ed25519HDKD");
565
- const publicKey = getPublicKeyEd25519(secretKey);
566
- return {
567
- type: "ed25519",
568
- secretKey,
569
- publicKey,
570
- address: addressFromPublicKey(publicKey, "ss58")
571
- };
572
- };
573
- var getPublicKeyEd25519 = (secretKey) => {
574
- if (secretKey.length === 64) {
575
- const [privateComponent, publicComponent] = [secretKey.slice(0, 32), secretKey.slice(32)];
576
- const publicKey = import_ed255193.ed25519.getPublicKey(privateComponent);
577
- if (!isUint8ArrayEq(publicComponent, publicKey)) return import_ed255193.ed25519.getPublicKey(secretKey);
578
- return publicKey;
579
- }
580
- return import_ed255193.ed25519.getPublicKey(secretKey);
581
- };
582
- var isUint8ArrayEq = (a, b) => (
583
- // biome-ignore lint/complexity/noUselessTernary: legacy
584
- a.length !== b.length || a.some((v, i) => v !== b[i]) ? false : true
585
- );
586
-
587
- // src/derivation/deriveEthereum.ts
588
- var import_secp256k12 = require("@noble/curves/secp256k1.js");
589
- var import_bip32 = require("@scure/bip32");
590
- var deriveEthereum = (seed, derivationPath) => {
591
- const hdkey = import_bip32.HDKey.fromMasterSeed(seed);
592
- const childKey = hdkey.derive(derivationPath);
593
- if (!childKey.privateKey) throw new Error("Invalid derivation path");
594
- const secretKey = new Uint8Array(childKey.privateKey);
595
- const publicKey = getPublicKeyEthereum(secretKey);
596
- return {
597
- type: "ethereum",
598
- secretKey,
599
- publicKey,
600
- address: addressFromPublicKey(publicKey, "ethereum")
601
- };
602
- };
603
- var getPublicKeyEthereum = (secretKey) => {
604
- return import_secp256k12.secp256k1.getPublicKey(secretKey, false);
605
- };
606
-
607
- // src/derivation/deriveSr25519.ts
608
- var import_sr25519 = require("@scure/sr25519");
609
- var deriveSr25519 = (seed, derivationPath) => {
610
- const derivations = parseSubstrateDerivations(derivationPath);
611
- const secretKey = derivations.reduce((secretKey2, [type, chainCode]) => {
612
- const code = createChainCode(chainCode);
613
- return type === "hard" ? import_sr25519.HDKD.secretHard(secretKey2, code) : import_sr25519.HDKD.secretSoft(secretKey2, code);
614
- }, (0, import_sr25519.secretFromSeed)(seed));
615
- const publicKey = getPublicKeySr25519(secretKey);
616
- return {
617
- type: "sr25519",
618
- secretKey,
619
- publicKey,
620
- address: addressFromPublicKey(publicKey, "ss58")
621
- };
622
- };
623
- var getPublicKeySr25519 = import_sr25519.getPublicKey;
624
-
625
- // src/derivation/utils.ts
626
- var deriveKeypair = (seed, derivationPath, curve) => {
627
- switch (curve) {
628
- case "sr25519":
629
- return deriveSr25519(seed, derivationPath);
630
- case "ed25519":
631
- return deriveEd25519(seed, derivationPath);
632
- case "ecdsa":
633
- return deriveEcdsa(seed, derivationPath);
634
- case "bitcoin-ecdsa":
635
- case "bitcoin-ed25519":
636
- throw new Error("deriveKeypair is not implemented for Bitcoin");
637
- case "ethereum":
638
- return deriveEthereum(seed, derivationPath);
639
- case "solana":
640
- return deriveSolana(seed, derivationPath);
641
- }
642
- };
643
- var getPublicKeyFromSecret = (secretKey, curve) => {
644
- switch (curve) {
645
- case "ecdsa":
646
- return getPublicKeyEcdsa(secretKey);
647
- case "ethereum":
648
- return getPublicKeyEthereum(secretKey);
649
- case "sr25519":
650
- return getPublicKeySr25519(secretKey);
651
- case "ed25519":
652
- return getPublicKeyEd25519(secretKey);
653
- case "bitcoin-ecdsa":
654
- case "bitcoin-ed25519":
655
- throw new Error("getPublicKeyFromSecret is not implemented for Bitcoin");
656
- case "solana":
657
- return getPublicKeySolana(secretKey);
658
- }
659
- };
660
- var addressFromMnemonic = async (mnemonic, derivationPath, curve) => {
661
- const entropy = mnemonicToEntropy(mnemonic);
662
- const seed = await entropyToSeed(entropy, curve);
663
- const { address } = deriveKeypair(seed, derivationPath, curve);
664
- return address;
665
- };
666
- var removeHexPrefix = (secretKey) => {
667
- if (secretKey.startsWith("0x")) return secretKey.slice(2);
668
- return secretKey;
669
- };
670
- var parseSecretKey = (secretKey, platform) => {
671
- switch (platform) {
672
- case "ethereum": {
673
- const privateKey = removeHexPrefix(secretKey);
674
- return import_base5.hex.decode(privateKey);
675
- }
676
- case "solana": {
677
- const bytes = secretKey.startsWith("[") ? (
678
- // JSON bytes array (ex: solflare)
679
- Uint8Array.from(JSON.parse(secretKey))
680
- ) : (
681
- // base58 encoded string (ex: phantom)
682
- import_base5.base58.decode(secretKey)
683
- );
684
- if (bytes.length === 64) {
685
- const privateKey = bytes.slice(0, 32);
686
- const publicKey = bytes.slice(32, 64);
687
- const computedPublicKey = getPublicKeySolana(privateKey);
688
- if (!publicKey.every((b, i) => b === computedPublicKey[i]))
689
- throw new Error("Invalid Solana secret key: public key does not match");
690
- return privateKey;
691
- } else if (bytes.length === 32) return bytes;
692
- throw new Error("Invalid Solana secret key length");
693
- }
694
- default:
695
- throw new Error("Not implemented");
696
- }
697
- };
698
- var isValidDerivationPath = async (derivationPath, curve) => {
699
- try {
700
- deriveKeypair(await getDevSeed(curve), derivationPath, curve);
701
- return true;
702
- } catch {
703
- return false;
704
- }
705
- };
706
-
707
- // src/encryption/encryptKemAead.ts
708
- var import_chacha = require("@noble/ciphers/chacha.js");
709
- var import_utils3 = require("@noble/ciphers/utils.js");
710
- var import_mlkem = require("mlkem");
711
- var encryptKemAead = async (keyHash, publicKey, plaintext) => {
712
- if (keyHash.length !== 16) {
713
- throw new Error(`Expected 16-byte keyHash, got ${keyHash.length}`);
714
- }
715
- const kem = new import_mlkem.MlKem768();
716
- const [kemCt, sharedSecret] = await kem.encap(publicKey);
717
- if (sharedSecret.length !== 32) {
718
- throw new Error(`Expected 32-byte shared secret, got ${sharedSecret.length}`);
719
- }
720
- const nonce = crypto.getRandomValues(new Uint8Array(24));
721
- const aead = (0, import_chacha.xchacha20poly1305)(sharedSecret, nonce);
722
- const aeadCt = aead.encrypt(plaintext);
723
- const kemLen = new Uint8Array(2);
724
- new DataView(kemLen.buffer).setUint16(0, kemCt.length, true);
725
- return (0, import_utils3.concatBytes)(keyHash, kemLen, kemCt, nonce, aeadCt);
726
- };
727
-
728
- // src/platform/index.ts
729
- var getAccountPlatformFromCurve = (curve) => {
730
- switch (curve) {
731
- case "sr25519":
732
- case "ed25519":
733
- case "ecdsa":
734
- return "polkadot";
735
- case "ethereum":
736
- return "ethereum";
737
- case "bitcoin-ecdsa":
738
- case "bitcoin-ed25519":
739
- return "bitcoin";
740
- case "solana":
741
- return "solana";
742
- }
743
- };
744
- var getAccountPlatformFromEncoding = (encoding) => {
745
- switch (encoding) {
746
- case "ss58":
747
- return "polkadot";
748
- case "ethereum":
749
- return "ethereum";
750
- case "bech32m":
751
- case "bech32":
752
- case "base58check":
753
- return "bitcoin";
754
- case "base58solana":
755
- return "solana";
756
- }
757
- };
758
- var getAccountPlatformFromAddress = (address) => {
759
- const encoding = detectAddressEncoding(address);
760
- return getAccountPlatformFromEncoding(encoding);
761
- };
762
- // Annotate the CommonJS export names for ESM import in node:
763
- 0 && (module.exports = {
764
- DEV_MNEMONIC_ETHEREUM,
765
- DEV_MNEMONIC_POLKADOT,
766
- addressEncodingFromCurve,
767
- addressFromMnemonic,
768
- addressFromPublicKey,
769
- base58,
770
- base64,
771
- blake2b256,
772
- blake2b512,
773
- blake3,
774
- checksumEthereumAddress,
775
- decodeSs58Address,
776
- deriveKeypair,
777
- detectAddressEncoding,
778
- ed25519,
779
- encodeAddressEthereum,
780
- encodeAddressSolana,
781
- encodeAddressSs58,
782
- encodeAnyAddress,
783
- encryptKemAead,
784
- entropyToMnemonic,
785
- entropyToSeed,
786
- fromBase58Check,
787
- fromBech32,
788
- fromBech32m,
789
- generateMnemonic,
790
- getAccountPlatformFromAddress,
791
- getAccountPlatformFromCurve,
792
- getAccountPlatformFromEncoding,
793
- getDevSeed,
794
- getPublicKeyFromSecret,
795
- getPublicKeySolana,
796
- getSafeHash,
797
- hex,
798
- isAddressEqual,
799
- isAddressValid,
800
- isBase58CheckAddress,
801
- isBech32Address,
802
- isBech32mAddress,
803
- isBitcoinAddress,
804
- isEthereumAddress,
805
- isSolanaAddress,
806
- isSs58Address,
807
- isValidDerivationPath,
808
- isValidMnemonic,
809
- mnemonicToEntropy,
810
- normalizeAddress,
811
- parseSecretKey,
812
- pbkdf2,
813
- removeHexPrefix,
814
- utf8
239
+ //#endregion
240
+ //#region src/address/encoding/detectAddressEncoding.ts
241
+ const CACHE$1 = /* @__PURE__ */ new Map();
242
+ const detectAddressEncodingInner = (address) => {
243
+ if (isEthereumAddress(address)) return "ethereum";
244
+ if (isSs58Address(address)) return "ss58";
245
+ if (isSolanaAddress(address)) return "base58solana";
246
+ if (isBech32mAddress(address)) return "bech32m";
247
+ if (isBech32Address(address)) return "bech32";
248
+ if (isBase58CheckAddress(address)) return "base58check";
249
+ throw new Error(`Unknown address encoding`);
250
+ };
251
+ const detectAddressEncoding = (address) => {
252
+ if (!CACHE$1.has(address)) CACHE$1.set(address, detectAddressEncodingInner(address));
253
+ return CACHE$1.get(address);
254
+ };
255
+ //#endregion
256
+ //#region src/address/addressFromPublicKey.ts
257
+ const addressFromPublicKey = (publicKey, encoding, options) => {
258
+ switch (encoding) {
259
+ case "ss58": return encodeAddressSs58(publicKey, options?.ss58Prefix);
260
+ case "ethereum": return encodeAddressEthereum(publicKey);
261
+ case "base58solana": return encodeAddressSolana(publicKey);
262
+ case "bech32m":
263
+ case "bech32":
264
+ case "base58check": throw new Error("addressFromPublicKey is not implemented for Bitcoin");
265
+ }
266
+ };
267
+ //#endregion
268
+ //#region src/address/normalizeAddress.ts
269
+ const CACHE = /* @__PURE__ */ new Map();
270
+ const normalizeAddress = (address) => {
271
+ try {
272
+ if (!CACHE.has(address)) CACHE.set(address, normalizeAnyAddress(address));
273
+ return CACHE.get(address);
274
+ } catch (cause) {
275
+ throw new Error(`Unable to normalize address: ${address}`, { cause });
276
+ }
277
+ };
278
+ const normalizeAnyAddress = (address) => {
279
+ switch (detectAddressEncoding(address)) {
280
+ case "ethereum": return checksumEthereumAddress(address);
281
+ case "bech32m":
282
+ case "bech32":
283
+ case "base58check":
284
+ case "base58solana": return address;
285
+ case "ss58": {
286
+ const [pk] = decodeSs58Address(address);
287
+ return encodeAddressSs58(pk, 42);
288
+ }
289
+ }
290
+ };
291
+ //#endregion
292
+ //#region src/address/encodeAnyAddress.ts
293
+ const encodeAnyAddress = (address, options) => {
294
+ if (detectAddressEncoding(address) === "ss58" && options?.ss58Format !== void 0) {
295
+ const [publicKey] = decodeSs58Address(address);
296
+ return encodeAddressSs58(publicKey, options?.ss58Format ?? 42);
297
+ }
298
+ return normalizeAddress(address);
299
+ };
300
+ //#endregion
301
+ //#region src/address/isAddressEqual.ts
302
+ const isAddressEqual = (address1, address2) => {
303
+ try {
304
+ return normalizeAddress(address1) === normalizeAddress(address2);
305
+ } catch {
306
+ return false;
307
+ }
308
+ };
309
+ //#endregion
310
+ //#region src/address/isAddressValid.ts
311
+ const isAddressValid = (address) => {
312
+ try {
313
+ detectAddressEncoding(address);
314
+ return true;
315
+ } catch {
316
+ return false;
317
+ }
318
+ };
319
+ //#endregion
320
+ //#region src/derivation/deriveSolana.ts
321
+ const parseDerivationPath = (path) => {
322
+ if (!path.startsWith("m/")) throw new Error("Path must start with 'm/'");
323
+ return path.split("/").slice(1).map((p) => {
324
+ if (!p.endsWith("'")) throw new Error("Only hardened derivation is supported");
325
+ return parseInt(p.slice(0, -1), 10) + 2147483648;
326
+ });
327
+ };
328
+ const deriveSolana = (seed, derivationPath) => {
329
+ let I = (0, _noble_hashes_hmac_js.hmac)(_noble_hashes_sha2_js.sha512, new TextEncoder().encode("ed25519 seed"), seed);
330
+ let secretKey = I.slice(0, 32);
331
+ let chainCode = I.slice(32);
332
+ for (const index of parseDerivationPath(derivationPath)) {
333
+ const data = new Uint8Array(1 + secretKey.length + 4);
334
+ data.set([0], 0);
335
+ data.set(secretKey, 1);
336
+ data.set(new Uint8Array([
337
+ index >> 24 & 255,
338
+ index >> 16 & 255,
339
+ index >> 8 & 255,
340
+ index & 255
341
+ ]), 1 + secretKey.length);
342
+ I = (0, _noble_hashes_hmac_js.hmac)(_noble_hashes_sha2_js.sha512, chainCode, data);
343
+ secretKey = I.slice(0, 32);
344
+ chainCode = I.slice(32);
345
+ }
346
+ const publicKey = getPublicKeySolana(secretKey);
347
+ return {
348
+ type: "solana",
349
+ secretKey,
350
+ publicKey,
351
+ address: addressFromPublicKey(publicKey, "base58solana")
352
+ };
353
+ };
354
+ const getPublicKeySolana = (secretKey) => {
355
+ return _noble_curves_ed25519_js.ed25519.getPublicKey(secretKey);
356
+ };
357
+ //#endregion
358
+ //#region src/utils/pbkdf2.ts
359
+ const pbkdf2 = async (hash, entropy, salt, iterations, outputLenBytes) => {
360
+ const keyMaterial = await crypto.subtle.importKey("raw", entropy, "PBKDF2", false, ["deriveBits"]);
361
+ const derivedBits = await crypto.subtle.deriveBits({
362
+ name: "PBKDF2",
363
+ salt,
364
+ iterations,
365
+ hash
366
+ }, keyMaterial, outputLenBytes * 8);
367
+ return new Uint8Array(derivedBits);
368
+ };
369
+ //#endregion
370
+ //#region src/mnemonic/index.ts
371
+ const mnemonicToEntropy = (mnemonic) => {
372
+ return (0, _scure_bip39.mnemonicToEntropy)(mnemonic, _scure_bip39_wordlists_english_js.wordlist);
373
+ };
374
+ const entropyToMnemonic = (entropy) => {
375
+ return (0, _scure_bip39.entropyToMnemonic)(entropy, _scure_bip39_wordlists_english_js.wordlist);
376
+ };
377
+ const entropyToSeedSubstrate = async (entropy, password) => await pbkdf2("SHA-512", entropy, mnemonicPasswordToSalt(password ?? ""), 2048, 32);
378
+ const entropyToSeedClassic = async (entropy, password) => await pbkdf2("SHA-512", encodeNormalized(entropyToMnemonic(entropy)), mnemonicPasswordToSalt(password ?? ""), 2048, 64);
379
+ const mnemonicPasswordToSalt = (password) => encodeNormalized(`mnemonic${password}`);
380
+ /** Normalizes a UTF-8 string using `NFKD` form, then encodes it into bytes */
381
+ const encodeNormalized = (utf8) => new TextEncoder().encode(utf8.normalize("NFKD"));
382
+ const getSeedDerivationType = (curve) => {
383
+ switch (curve) {
384
+ case "sr25519":
385
+ case "ed25519":
386
+ case "ecdsa": return "substrate";
387
+ case "ethereum":
388
+ case "solana": return "classic";
389
+ case "bitcoin-ecdsa":
390
+ case "bitcoin-ed25519": throw new Error("seed derivation is not implemented for Bitcoin");
391
+ }
392
+ };
393
+ const entropyToSeed = async (entropy, curve, password) => {
394
+ switch (getSeedDerivationType(curve)) {
395
+ case "classic": return await entropyToSeedClassic(entropy, password);
396
+ case "substrate": return await entropyToSeedSubstrate(entropy, password);
397
+ }
398
+ };
399
+ const isValidMnemonic = (mnemonic) => {
400
+ return (0, _scure_bip39.validateMnemonic)(mnemonic, _scure_bip39_wordlists_english_js.wordlist);
401
+ };
402
+ const generateMnemonic = (words) => {
403
+ switch (words) {
404
+ case 12: return (0, _scure_bip39.generateMnemonic)(_scure_bip39_wordlists_english_js.wordlist, 128);
405
+ case 24: return (0, _scure_bip39.generateMnemonic)(_scure_bip39_wordlists_english_js.wordlist, 256);
406
+ }
407
+ };
408
+ const DEV_MNEMONIC_POLKADOT = "bottom drive obey lake curtain smoke basket hold race lonely fit walk";
409
+ const DEV_MNEMONIC_ETHEREUM = "test test test test test test test test test test test junk";
410
+ const DEV_SEED_CACHE = /* @__PURE__ */ new Map();
411
+ const getDevSeed = async (curve) => {
412
+ const type = getSeedDerivationType(curve);
413
+ if (!DEV_SEED_CACHE.has(type)) switch (type) {
414
+ case "classic": {
415
+ const entropy = mnemonicToEntropy(DEV_MNEMONIC_ETHEREUM);
416
+ const seed = await entropyToSeedClassic(entropy);
417
+ DEV_SEED_CACHE.set(type, seed);
418
+ break;
419
+ }
420
+ case "substrate": {
421
+ const entropy = mnemonicToEntropy(DEV_MNEMONIC_POLKADOT);
422
+ const seed = await entropyToSeedSubstrate(entropy);
423
+ DEV_SEED_CACHE.set(type, seed);
424
+ break;
425
+ }
426
+ default: throw new Error("Unsupported derivation type");
427
+ }
428
+ return DEV_SEED_CACHE.get(type);
429
+ };
430
+ //#endregion
431
+ //#region src/derivation/common.ts
432
+ const DERIVATION_RE = /(\/{1,2})(\w+)/g;
433
+ const parseSubstrateDerivations = (derivationsStr) => {
434
+ const derivations = [];
435
+ if (derivations) for (const [_, type, code] of derivationsStr.matchAll(DERIVATION_RE)) derivations.push([type === "//" ? "hard" : "soft", code]);
436
+ return derivations;
437
+ };
438
+ const createChainCode = (code) => {
439
+ const chainCode = /* @__PURE__ */ new Uint8Array(32);
440
+ chainCode.set(Number.isNaN(+code) ? scale_ts.str.enc(code) : scale_ts.u32.enc(+code));
441
+ return chainCode;
442
+ };
443
+ const derivationCodec = /* @__PURE__ */ (0, scale_ts.Tuple)(scale_ts.str, (0, scale_ts.Bytes)(32), (0, scale_ts.Bytes)(32));
444
+ const createSubstrateDeriveFn = (prefix) => (seed, chainCode) => blake2b256(derivationCodec.enc([
445
+ prefix,
446
+ seed,
447
+ chainCode
448
+ ]));
449
+ const deriveSubstrateSecretKey = (seed, derivationPath, prefix) => {
450
+ const derivations = parseSubstrateDerivations(derivationPath);
451
+ const derive = createSubstrateDeriveFn(prefix);
452
+ return derivations.reduce((seed, [type, chainCode]) => {
453
+ const code = createChainCode(chainCode);
454
+ if (type === "soft") throw new Error("Soft derivations are not supported");
455
+ return derive(seed, code);
456
+ }, seed);
457
+ };
458
+ //#endregion
459
+ //#region src/derivation/deriveEcdsa.ts
460
+ const deriveEcdsa = (seed, derivationPath) => {
461
+ const secretKey = deriveSubstrateSecretKey(seed, derivationPath, "Secp256k1HDKD");
462
+ const publicKey = getPublicKeyEcdsa(secretKey);
463
+ return {
464
+ type: "ecdsa",
465
+ secretKey,
466
+ publicKey,
467
+ address: addressFromPublicKey(publicKey, "ss58")
468
+ };
469
+ };
470
+ const getPublicKeyEcdsa = (secretKey) => {
471
+ return _noble_curves_secp256k1_js.secp256k1.getPublicKey(secretKey);
472
+ };
473
+ //#endregion
474
+ //#region src/derivation/deriveEd25519.ts
475
+ const deriveEd25519 = (seed, derivationPath) => {
476
+ const secretKey = deriveSubstrateSecretKey(seed, derivationPath, "Ed25519HDKD");
477
+ const publicKey = getPublicKeyEd25519(secretKey);
478
+ return {
479
+ type: "ed25519",
480
+ secretKey,
481
+ publicKey,
482
+ address: addressFromPublicKey(publicKey, "ss58")
483
+ };
484
+ };
485
+ const getPublicKeyEd25519 = (secretKey) => {
486
+ if (secretKey.length === 64) {
487
+ const [privateComponent, publicComponent] = [secretKey.slice(0, 32), secretKey.slice(32)];
488
+ const publicKey = _noble_curves_ed25519_js.ed25519.getPublicKey(privateComponent);
489
+ if (!isUint8ArrayEq(publicComponent, publicKey)) return _noble_curves_ed25519_js.ed25519.getPublicKey(secretKey);
490
+ return publicKey;
491
+ }
492
+ return _noble_curves_ed25519_js.ed25519.getPublicKey(secretKey);
493
+ };
494
+ /** If a is identical to b, this function returns true, otherwise it returns false */
495
+ const isUint8ArrayEq = (a, b) => a.length !== b.length || a.some((v, i) => v !== b[i]) ? false : true;
496
+ //#endregion
497
+ //#region src/derivation/deriveEthereum.ts
498
+ const deriveEthereum = (seed, derivationPath) => {
499
+ const childKey = _scure_bip32.HDKey.fromMasterSeed(seed).derive(derivationPath);
500
+ if (!childKey.privateKey) throw new Error("Invalid derivation path");
501
+ const secretKey = new Uint8Array(childKey.privateKey);
502
+ const publicKey = getPublicKeyEthereum(secretKey);
503
+ return {
504
+ type: "ethereum",
505
+ secretKey,
506
+ publicKey,
507
+ address: addressFromPublicKey(publicKey, "ethereum")
508
+ };
509
+ };
510
+ const getPublicKeyEthereum = (secretKey) => {
511
+ return _noble_curves_secp256k1_js.secp256k1.getPublicKey(secretKey, false);
512
+ };
513
+ //#endregion
514
+ //#region src/derivation/deriveSr25519.ts
515
+ const deriveSr25519 = (seed, derivationPath) => {
516
+ const secretKey = parseSubstrateDerivations(derivationPath).reduce((secretKey, [type, chainCode]) => {
517
+ const code = createChainCode(chainCode);
518
+ return type === "hard" ? _scure_sr25519.HDKD.secretHard(secretKey, code) : _scure_sr25519.HDKD.secretSoft(secretKey, code);
519
+ }, (0, _scure_sr25519.secretFromSeed)(seed));
520
+ const publicKey = getPublicKeySr25519(secretKey);
521
+ return {
522
+ type: "sr25519",
523
+ secretKey,
524
+ publicKey,
525
+ address: addressFromPublicKey(publicKey, "ss58")
526
+ };
527
+ };
528
+ const getPublicKeySr25519 = _scure_sr25519.getPublicKey;
529
+ //#endregion
530
+ //#region src/derivation/utils.ts
531
+ const deriveKeypair = (seed, derivationPath, curve) => {
532
+ switch (curve) {
533
+ case "sr25519": return deriveSr25519(seed, derivationPath);
534
+ case "ed25519": return deriveEd25519(seed, derivationPath);
535
+ case "ecdsa": return deriveEcdsa(seed, derivationPath);
536
+ case "bitcoin-ecdsa":
537
+ case "bitcoin-ed25519": throw new Error("deriveKeypair is not implemented for Bitcoin");
538
+ case "ethereum": return deriveEthereum(seed, derivationPath);
539
+ case "solana": return deriveSolana(seed, derivationPath);
540
+ }
541
+ };
542
+ const getPublicKeyFromSecret = (secretKey, curve) => {
543
+ switch (curve) {
544
+ case "ecdsa": return getPublicKeyEcdsa(secretKey);
545
+ case "ethereum": return getPublicKeyEthereum(secretKey);
546
+ case "sr25519": return getPublicKeySr25519(secretKey);
547
+ case "ed25519": return getPublicKeyEd25519(secretKey);
548
+ case "bitcoin-ecdsa":
549
+ case "bitcoin-ed25519": throw new Error("getPublicKeyFromSecret is not implemented for Bitcoin");
550
+ case "solana": return getPublicKeySolana(secretKey);
551
+ }
552
+ };
553
+ const addressFromMnemonic = async (mnemonic, derivationPath, curve) => {
554
+ const seed = await entropyToSeed(mnemonicToEntropy(mnemonic), curve);
555
+ const { address } = deriveKeypair(seed, derivationPath, curve);
556
+ return address;
557
+ };
558
+ const removeHexPrefix = (secretKey) => {
559
+ if (secretKey.startsWith("0x")) return secretKey.slice(2);
560
+ return secretKey;
561
+ };
562
+ const parseSecretKey = (secretKey, platform) => {
563
+ switch (platform) {
564
+ case "ethereum": {
565
+ const privateKey = removeHexPrefix(secretKey);
566
+ return _scure_base.hex.decode(privateKey);
567
+ }
568
+ case "solana": {
569
+ const bytes = secretKey.startsWith("[") ? Uint8Array.from(JSON.parse(secretKey)) : _scure_base.base58.decode(secretKey);
570
+ if (bytes.length === 64) {
571
+ const privateKey = bytes.slice(0, 32);
572
+ const publicKey = bytes.slice(32, 64);
573
+ const computedPublicKey = getPublicKeySolana(privateKey);
574
+ if (!publicKey.every((b, i) => b === computedPublicKey[i])) throw new Error("Invalid Solana secret key: public key does not match");
575
+ return privateKey;
576
+ } else if (bytes.length === 32) return bytes;
577
+ throw new Error("Invalid Solana secret key length");
578
+ }
579
+ default: throw new Error("Not implemented");
580
+ }
581
+ };
582
+ const isValidDerivationPath = async (derivationPath, curve) => {
583
+ try {
584
+ deriveKeypair(await getDevSeed(curve), derivationPath, curve);
585
+ return true;
586
+ } catch {
587
+ return false;
588
+ }
589
+ };
590
+ //#endregion
591
+ //#region src/encryption/encryptKemAead.ts
592
+ /**
593
+ * Encrypts plaintext using ML-KEM-768 + XChaCha20-Poly1305 (KEM-AEAD).
594
+ *
595
+ * Wire format (subtensor pallet-shield v2):
596
+ * key_hash (16 bytes) || kem_len (2 bytes LE) || kem_ct (variable) || nonce (24 bytes) || aead_ct (variable)
597
+ *
598
+ * @param keyHash 16-byte xxhash128 of the public key (caller must compute, e.g. via Twox128)
599
+ * @param publicKey ML-KEM-768 encapsulation key bytes
600
+ * @param plaintext Data to encrypt (typically a signed extrinsic)
601
+ */
602
+ const encryptKemAead = async (keyHash, publicKey, plaintext) => {
603
+ if (keyHash.length !== 16) throw new Error(`Expected 16-byte keyHash, got ${keyHash.length}`);
604
+ const [kemCt, sharedSecret] = await new mlkem.MlKem768().encap(publicKey);
605
+ if (sharedSecret.length !== 32) throw new Error(`Expected 32-byte shared secret, got ${sharedSecret.length}`);
606
+ const nonce = crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(24));
607
+ const aeadCt = (0, _noble_ciphers_chacha_js.xchacha20poly1305)(sharedSecret, nonce).encrypt(plaintext);
608
+ const kemLen = /* @__PURE__ */ new Uint8Array(2);
609
+ new DataView(kemLen.buffer).setUint16(0, kemCt.length, true);
610
+ return (0, _noble_ciphers_utils_js.concatBytes)(keyHash, kemLen, kemCt, nonce, aeadCt);
611
+ };
612
+ //#endregion
613
+ //#region src/keystore/index.ts
614
+ /**
615
+ * polkadot-js encrypted keystore JSON support (scrypt + xsalsa20-poly1305), byte-compatible
616
+ * with `@polkadot/util-crypto` `jsonEncrypt`/`jsonDecrypt`.
617
+ *
618
+ * Layout of the base64 `encoded` blob:
619
+ * salt(32) ++ N(u32 LE) ++ p(u32 LE) ++ r(u32 LE) ++ nonce(24) ++ ciphertext
620
+ */
621
+ const SALT_LENGTH = 32;
622
+ const NONCE_LENGTH = 24;
623
+ const ALLOWED_SCRYPT_PARAMS = [
624
+ {
625
+ N: 8192,
626
+ p: 10,
627
+ r: 8
628
+ },
629
+ {
630
+ N: 16384,
631
+ p: 5,
632
+ r: 8
633
+ },
634
+ {
635
+ N: 32768,
636
+ p: 3,
637
+ r: 8
638
+ },
639
+ {
640
+ N: 32768,
641
+ p: 1,
642
+ r: 8
643
+ },
644
+ {
645
+ N: 65536,
646
+ p: 2,
647
+ r: 8
648
+ },
649
+ {
650
+ N: 1 << 17,
651
+ p: 1,
652
+ r: 8
653
+ }
654
+ ];
655
+ const SCRYPT_N = 1 << 17;
656
+ const SCRYPT_P = 1;
657
+ const SCRYPT_R = 8;
658
+ const readU32LE = (bytes, offset) => bytes[offset] | bytes[offset + 1] << 8 | bytes[offset + 2] << 16 | bytes[offset + 3] << 24 >>> 0;
659
+ const writeU32LE = (value) => new Uint8Array([
660
+ value & 255,
661
+ value >> 8 & 255,
662
+ value >> 16 & 255,
663
+ value >> 24 & 255
664
+ ]);
665
+ const deriveKey = (password, salt, params) => (0, _noble_hashes_scrypt_js.scrypt)(new TextEncoder().encode(password), salt, {
666
+ ...params,
667
+ dkLen: 64
668
+ }).subarray(0, 32);
669
+ /** Decrypts a polkadot-js encrypted keystore (`jsonDecrypt` equivalent) */
670
+ const decryptPjsKeystore = ({ encoded, encoding }, password) => {
671
+ if (!encoded) throw new Error("No encrypted data available to decode");
672
+ const bytes = _scure_base.base64.decode(encoded);
673
+ if (!encoding.type.includes("xsalsa20-poly1305")) {
674
+ if (encoding.type.includes("none")) return bytes;
675
+ throw new Error(`Unsupported keystore encoding: ${encoding.type.join("/")}`);
676
+ }
677
+ let offset = 0;
678
+ let key;
679
+ if (encoding.type.includes("scrypt")) {
680
+ const salt = bytes.subarray(0, SALT_LENGTH);
681
+ const N = readU32LE(bytes, SALT_LENGTH);
682
+ const p = readU32LE(bytes, 36);
683
+ const r = readU32LE(bytes, 40);
684
+ if (!ALLOWED_SCRYPT_PARAMS.some((preset) => preset.N === N && preset.p === p && preset.r === r)) throw new Error("Invalid injected scrypt params found");
685
+ key = deriveKey(password, salt, {
686
+ N,
687
+ p,
688
+ r
689
+ });
690
+ offset = 44;
691
+ } else {
692
+ const padded = /* @__PURE__ */ new Uint8Array(32);
693
+ padded.set(new TextEncoder().encode(password).subarray(0, 32));
694
+ key = padded;
695
+ }
696
+ const nonce = bytes.subarray(offset, offset + NONCE_LENGTH);
697
+ const ciphertext = bytes.subarray(offset + NONCE_LENGTH);
698
+ return (0, _noble_ciphers_salsa_js.xsalsa20poly1305)(key, nonce).decrypt(ciphertext);
699
+ };
700
+ /** Encrypts data as a polkadot-js keystore (`jsonEncrypt` equivalent, always scrypt + v3) */
701
+ const encryptPjsKeystore = (data, contentType, password) => {
702
+ const salt = crypto.getRandomValues(new Uint8Array(SALT_LENGTH));
703
+ const nonce = crypto.getRandomValues(new Uint8Array(NONCE_LENGTH));
704
+ const ciphertext = (0, _noble_ciphers_salsa_js.xsalsa20poly1305)(deriveKey(password, salt, {
705
+ N: SCRYPT_N,
706
+ p: SCRYPT_P,
707
+ r: SCRYPT_R
708
+ }), nonce).encrypt(data);
709
+ const encoded = new Uint8Array(68 + ciphertext.length);
710
+ encoded.set(salt, 0);
711
+ encoded.set(writeU32LE(SCRYPT_N), SALT_LENGTH);
712
+ encoded.set(writeU32LE(SCRYPT_P), 36);
713
+ encoded.set(writeU32LE(SCRYPT_R), 40);
714
+ encoded.set(nonce, 44);
715
+ encoded.set(ciphertext, 68);
716
+ return {
717
+ encoded: _scure_base.base64.encode(encoded),
718
+ encoding: {
719
+ content: contentType,
720
+ type: ["scrypt", "xsalsa20-poly1305"],
721
+ version: "3"
722
+ }
723
+ };
724
+ };
725
+ //#endregion
726
+ //#region src/platform/index.ts
727
+ const getAccountPlatformFromCurve = (curve) => {
728
+ switch (curve) {
729
+ case "sr25519":
730
+ case "ed25519":
731
+ case "ecdsa": return "polkadot";
732
+ case "ethereum": return "ethereum";
733
+ case "bitcoin-ecdsa":
734
+ case "bitcoin-ed25519": return "bitcoin";
735
+ case "solana": return "solana";
736
+ }
737
+ };
738
+ const getAccountPlatformFromEncoding = (encoding) => {
739
+ switch (encoding) {
740
+ case "ss58": return "polkadot";
741
+ case "ethereum": return "ethereum";
742
+ case "bech32m":
743
+ case "bech32":
744
+ case "base58check": return "bitcoin";
745
+ case "base58solana": return "solana";
746
+ }
747
+ };
748
+ const getAccountPlatformFromAddress = (address) => {
749
+ const encoding = detectAddressEncoding(address);
750
+ return getAccountPlatformFromEncoding(encoding);
751
+ };
752
+ //#endregion
753
+ //#region src/signing/index.ts
754
+ /** noble's recovered signature format is v||r||s, substrate expects r||s||v */
755
+ const toRsv = (vrs) => {
756
+ const rsv = /* @__PURE__ */ new Uint8Array(65);
757
+ rsv.set(vrs.subarray(1), 0);
758
+ rsv[64] = vrs[0];
759
+ return rsv;
760
+ };
761
+ /**
762
+ * Signs a substrate payload with the given curve.
763
+ *
764
+ * Output is byte-compatible with polkadot-js `KeyringPair.sign` (without type prefix):
765
+ * - sr25519/ed25519: 64-byte signature over the raw message
766
+ * - ecdsa: 65-byte recoverable signature (r||s||v) over blake2b-256(message)
767
+ * - ethereum: 65-byte recoverable signature (r||s||v) over keccak-256(message)
768
+ *
769
+ * Note: callers are responsible for the substrate >256-byte rule (hash the payload
770
+ * with blake2b-256 before signing) — this matches where polkadot-js applies it.
771
+ */
772
+ const signSubstrate = (curve, secretKey, message) => {
773
+ switch (curve) {
774
+ case "sr25519": return (0, _scure_sr25519.sign)(secretKey, message);
775
+ case "ed25519": return _noble_curves_ed25519_js.ed25519.sign(message, secretKey.length === 64 ? secretKey.subarray(0, 32) : secretKey);
776
+ case "ecdsa": return toRsv(_noble_curves_secp256k1_js.secp256k1.sign(blake2b256(message), secretKey, {
777
+ format: "recovered",
778
+ prehash: false
779
+ }));
780
+ case "ethereum": return toRsv(_noble_curves_secp256k1_js.secp256k1.sign((0, _noble_hashes_sha3_js.keccak_256)(message), secretKey, {
781
+ format: "recovered",
782
+ prehash: false
783
+ }));
784
+ default: throw new Error(`Unsupported curve for substrate signing: ${curve}`);
785
+ }
786
+ };
787
+ /** MultiSignature enum variant index per signature scheme, used to type-prefix signatures */
788
+ const SIGNATURE_TYPE_PREFIX = {
789
+ ed25519: 0,
790
+ sr25519: 1,
791
+ ecdsa: 2,
792
+ ethereum: 2
793
+ };
794
+ //#endregion
795
+ exports.DEV_MNEMONIC_ETHEREUM = DEV_MNEMONIC_ETHEREUM;
796
+ exports.DEV_MNEMONIC_POLKADOT = DEV_MNEMONIC_POLKADOT;
797
+ exports.SIGNATURE_TYPE_PREFIX = SIGNATURE_TYPE_PREFIX;
798
+ exports.addressEncodingFromCurve = addressEncodingFromCurve;
799
+ exports.addressFromMnemonic = addressFromMnemonic;
800
+ exports.addressFromPublicKey = addressFromPublicKey;
801
+ Object.defineProperty(exports, "base58", {
802
+ enumerable: true,
803
+ get: function() {
804
+ return _scure_base.base58;
805
+ }
806
+ });
807
+ Object.defineProperty(exports, "base64", {
808
+ enumerable: true,
809
+ get: function() {
810
+ return _scure_base.base64;
811
+ }
812
+ });
813
+ exports.blake2b256 = blake2b256;
814
+ exports.blake2b512 = blake2b512;
815
+ exports.blake3 = blake3;
816
+ exports.checksumEthereumAddress = checksumEthereumAddress;
817
+ exports.decodeSs58Address = decodeSs58Address;
818
+ exports.decryptPjsKeystore = decryptPjsKeystore;
819
+ exports.deriveKeypair = deriveKeypair;
820
+ exports.detectAddressEncoding = detectAddressEncoding;
821
+ Object.defineProperty(exports, "ed25519", {
822
+ enumerable: true,
823
+ get: function() {
824
+ return _noble_curves_ed25519_js.ed25519;
825
+ }
815
826
  });
827
+ exports.encodeAddressEthereum = encodeAddressEthereum;
828
+ exports.encodeAddressSolana = encodeAddressSolana;
829
+ exports.encodeAddressSs58 = encodeAddressSs58;
830
+ exports.encodeAnyAddress = encodeAnyAddress;
831
+ exports.encryptKemAead = encryptKemAead;
832
+ exports.encryptPjsKeystore = encryptPjsKeystore;
833
+ exports.entropyToMnemonic = entropyToMnemonic;
834
+ exports.entropyToSeed = entropyToSeed;
835
+ exports.fromBase58Check = fromBase58Check;
836
+ exports.fromBech32 = fromBech32;
837
+ exports.fromBech32m = fromBech32m;
838
+ exports.generateMnemonic = generateMnemonic;
839
+ exports.getAccountPlatformFromAddress = getAccountPlatformFromAddress;
840
+ exports.getAccountPlatformFromCurve = getAccountPlatformFromCurve;
841
+ exports.getAccountPlatformFromEncoding = getAccountPlatformFromEncoding;
842
+ exports.getDevSeed = getDevSeed;
843
+ exports.getPublicKeyFromSecret = getPublicKeyFromSecret;
844
+ exports.getPublicKeySolana = getPublicKeySolana;
845
+ exports.getSafeHash = getSafeHash;
846
+ Object.defineProperty(exports, "hex", {
847
+ enumerable: true,
848
+ get: function() {
849
+ return _scure_base.hex;
850
+ }
851
+ });
852
+ exports.isAddressEqual = isAddressEqual;
853
+ exports.isAddressValid = isAddressValid;
854
+ exports.isBase58CheckAddress = isBase58CheckAddress;
855
+ exports.isBech32Address = isBech32Address;
856
+ exports.isBech32mAddress = isBech32mAddress;
857
+ exports.isBitcoinAddress = isBitcoinAddress;
858
+ exports.isEthereumAddress = isEthereumAddress;
859
+ exports.isOnCurveSolanaAddress = isOnCurveSolanaAddress;
860
+ exports.isSolanaAddress = isSolanaAddress;
861
+ exports.isSs58Address = isSs58Address;
862
+ exports.isValidDerivationPath = isValidDerivationPath;
863
+ exports.isValidMnemonic = isValidMnemonic;
864
+ exports.mnemonicToEntropy = mnemonicToEntropy;
865
+ exports.normalizeAddress = normalizeAddress;
866
+ exports.parseSecretKey = parseSecretKey;
867
+ exports.pbkdf2 = pbkdf2;
868
+ exports.removeHexPrefix = removeHexPrefix;
869
+ exports.signSubstrate = signSubstrate;
870
+ Object.defineProperty(exports, "utf8", {
871
+ enumerable: true,
872
+ get: function() {
873
+ return _scure_base.utf8;
874
+ }
875
+ });
876
+
816
877
  //# sourceMappingURL=index.js.map