@talismn/crypto 0.0.0-pr2277-20251211071316 → 0.0.0-pr2295-20260110044132
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.d.mts +124 -0
- package/dist/index.d.ts +124 -0
- package/dist/index.js +808 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +726 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +21 -11
- package/dist/declarations/src/address/addressFromPublicKey.d.ts +0 -5
- package/dist/declarations/src/address/encodeAnyAddress.d.ts +0 -5
- package/dist/declarations/src/address/encoding/addressEncodingFromCurve.d.ts +0 -3
- package/dist/declarations/src/address/encoding/bitcoin.d.ts +0 -37
- package/dist/declarations/src/address/encoding/detectAddressEncoding.d.ts +0 -2
- package/dist/declarations/src/address/encoding/ethereum.d.ts +0 -6
- package/dist/declarations/src/address/encoding/index.d.ts +0 -6
- package/dist/declarations/src/address/encoding/solana.d.ts +0 -2
- package/dist/declarations/src/address/encoding/ss58.d.ts +0 -3
- package/dist/declarations/src/address/index.d.ts +0 -6
- package/dist/declarations/src/address/isAddressEqual.d.ts +0 -1
- package/dist/declarations/src/address/isAddressValid.d.ts +0 -1
- package/dist/declarations/src/address/normalizeAddress.d.ts +0 -1
- package/dist/declarations/src/derivation/common.d.ts +0 -5
- package/dist/declarations/src/derivation/deriveEcdsa.d.ts +0 -3
- package/dist/declarations/src/derivation/deriveEd25519.d.ts +0 -3
- package/dist/declarations/src/derivation/deriveEthereum.d.ts +0 -3
- package/dist/declarations/src/derivation/deriveSolana.d.ts +0 -3
- package/dist/declarations/src/derivation/deriveSr25519.d.ts +0 -4
- package/dist/declarations/src/derivation/index.d.ts +0 -1
- package/dist/declarations/src/derivation/utils.d.ts +0 -7
- package/dist/declarations/src/encryption/encryptKemAead.d.ts +0 -1
- package/dist/declarations/src/encryption/index.d.ts +0 -1
- package/dist/declarations/src/hashing/index.d.ts +0 -9
- package/dist/declarations/src/index.d.ts +0 -8
- package/dist/declarations/src/mnemonic/index.d.ts +0 -9
- package/dist/declarations/src/platform/index.d.ts +0 -4
- package/dist/declarations/src/types/index.d.ts +0 -9
- package/dist/declarations/src/utils/exports.d.ts +0 -2
- package/dist/declarations/src/utils/index.d.ts +0 -2
- package/dist/declarations/src/utils/pbkdf2.d.ts +0 -1
- package/dist/talismn-crypto.cjs.d.ts +0 -1
- package/dist/talismn-crypto.cjs.dev.js +0 -784
- package/dist/talismn-crypto.cjs.js +0 -7
- package/dist/talismn-crypto.cjs.prod.js +0 -784
- package/dist/talismn-crypto.esm.js +0 -716
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
// src/mnemonic/index.ts
|
|
2
|
+
import {
|
|
3
|
+
entropyToMnemonic as entropyToMnemonicBip39,
|
|
4
|
+
generateMnemonic as generateMnemonicBip39,
|
|
5
|
+
mnemonicToEntropy as mnemonicToEntropyBip39,
|
|
6
|
+
validateMnemonic
|
|
7
|
+
} from "@scure/bip39";
|
|
8
|
+
import { wordlist } from "@scure/bip39/wordlists/english";
|
|
9
|
+
|
|
10
|
+
// src/utils/exports.ts
|
|
11
|
+
import { base58, base64, hex, utf8 } from "@scure/base";
|
|
12
|
+
import { ed25519 } from "@noble/curves/ed25519";
|
|
13
|
+
|
|
14
|
+
// src/utils/pbkdf2.ts
|
|
15
|
+
var pbkdf2 = async (hash, entropy, salt, iterations, outputLenBytes) => {
|
|
16
|
+
const keyMaterial = await crypto.subtle.importKey("raw", entropy, "PBKDF2", false, ["deriveBits"]);
|
|
17
|
+
const derivedBits = await crypto.subtle.deriveBits(
|
|
18
|
+
{ name: "PBKDF2", salt, iterations, hash },
|
|
19
|
+
keyMaterial,
|
|
20
|
+
outputLenBytes * 8
|
|
21
|
+
);
|
|
22
|
+
return new Uint8Array(derivedBits);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// src/mnemonic/index.ts
|
|
26
|
+
var mnemonicToEntropy = (mnemonic) => {
|
|
27
|
+
return mnemonicToEntropyBip39(mnemonic, wordlist);
|
|
28
|
+
};
|
|
29
|
+
var entropyToMnemonic = (entropy) => {
|
|
30
|
+
return entropyToMnemonicBip39(entropy, wordlist);
|
|
31
|
+
};
|
|
32
|
+
var entropyToSeedSubstrate = async (entropy, password) => await pbkdf2(
|
|
33
|
+
"SHA-512",
|
|
34
|
+
entropy,
|
|
35
|
+
mnemonicPasswordToSalt(password ?? ""),
|
|
36
|
+
2048,
|
|
37
|
+
// 2048 iterations
|
|
38
|
+
32
|
|
39
|
+
// 32 bytes (32 * 8 == 256 bits)
|
|
40
|
+
);
|
|
41
|
+
var entropyToSeedClassic = async (entropy, password) => await pbkdf2(
|
|
42
|
+
"SHA-512",
|
|
43
|
+
encodeNormalized(entropyToMnemonic(entropy)),
|
|
44
|
+
mnemonicPasswordToSalt(password ?? ""),
|
|
45
|
+
2048,
|
|
46
|
+
// 2048 iterations
|
|
47
|
+
64
|
|
48
|
+
// 64 bytes (64 * 8 == 512 bits)
|
|
49
|
+
);
|
|
50
|
+
var mnemonicPasswordToSalt = (password) => encodeNormalized(`mnemonic${password}`);
|
|
51
|
+
var encodeNormalized = (utf82) => new TextEncoder().encode(utf82.normalize("NFKD"));
|
|
52
|
+
var getSeedDerivationType = (curve) => {
|
|
53
|
+
switch (curve) {
|
|
54
|
+
case "sr25519":
|
|
55
|
+
case "ed25519":
|
|
56
|
+
case "ecdsa":
|
|
57
|
+
return "substrate";
|
|
58
|
+
case "ethereum":
|
|
59
|
+
case "solana":
|
|
60
|
+
return "classic";
|
|
61
|
+
case "bitcoin-ecdsa":
|
|
62
|
+
case "bitcoin-ed25519":
|
|
63
|
+
throw new Error("seed derivation is not implemented for Bitcoin");
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
var entropyToSeed = async (entropy, curve, password) => {
|
|
67
|
+
const type = getSeedDerivationType(curve);
|
|
68
|
+
switch (type) {
|
|
69
|
+
case "classic":
|
|
70
|
+
return await entropyToSeedClassic(entropy, password);
|
|
71
|
+
case "substrate":
|
|
72
|
+
return await entropyToSeedSubstrate(entropy, password);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
var isValidMnemonic = (mnemonic) => {
|
|
76
|
+
return validateMnemonic(mnemonic, wordlist);
|
|
77
|
+
};
|
|
78
|
+
var generateMnemonic = (words) => {
|
|
79
|
+
switch (words) {
|
|
80
|
+
case 12:
|
|
81
|
+
return generateMnemonicBip39(wordlist, 128);
|
|
82
|
+
case 24:
|
|
83
|
+
return generateMnemonicBip39(wordlist, 256);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
var DEV_MNEMONIC_POLKADOT = "bottom drive obey lake curtain smoke basket hold race lonely fit walk";
|
|
87
|
+
var DEV_MNEMONIC_ETHEREUM = "test test test test test test test test test test test junk";
|
|
88
|
+
var DEV_SEED_CACHE = /* @__PURE__ */ new Map();
|
|
89
|
+
var getDevSeed = async (curve) => {
|
|
90
|
+
const type = getSeedDerivationType(curve);
|
|
91
|
+
if (!DEV_SEED_CACHE.has(type)) {
|
|
92
|
+
switch (type) {
|
|
93
|
+
case "classic": {
|
|
94
|
+
const entropy = mnemonicToEntropy(DEV_MNEMONIC_ETHEREUM);
|
|
95
|
+
const seed = await entropyToSeedClassic(entropy);
|
|
96
|
+
DEV_SEED_CACHE.set(type, seed);
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
case "substrate": {
|
|
100
|
+
const entropy = mnemonicToEntropy(DEV_MNEMONIC_POLKADOT);
|
|
101
|
+
const seed = await entropyToSeedSubstrate(entropy);
|
|
102
|
+
DEV_SEED_CACHE.set(type, seed);
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
default:
|
|
106
|
+
throw new Error("Unsupported derivation type");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return DEV_SEED_CACHE.get(type);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// src/derivation/utils.ts
|
|
113
|
+
import { base58 as base585, hex as hex2 } from "@scure/base";
|
|
114
|
+
|
|
115
|
+
// src/derivation/deriveEcdsa.ts
|
|
116
|
+
import { secp256k1 } from "@noble/curves/secp256k1";
|
|
117
|
+
|
|
118
|
+
// src/address/encoding/addressEncodingFromCurve.ts
|
|
119
|
+
var addressEncodingFromCurve = (curve) => {
|
|
120
|
+
switch (curve) {
|
|
121
|
+
case "sr25519":
|
|
122
|
+
case "ed25519":
|
|
123
|
+
case "ecdsa":
|
|
124
|
+
return "ss58";
|
|
125
|
+
case "bitcoin-ecdsa":
|
|
126
|
+
case "bitcoin-ed25519":
|
|
127
|
+
return "bech32m";
|
|
128
|
+
case "ethereum":
|
|
129
|
+
return "ethereum";
|
|
130
|
+
case "solana":
|
|
131
|
+
return "base58solana";
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// src/address/encoding/solana.ts
|
|
136
|
+
import { base58 as base582 } from "@scure/base";
|
|
137
|
+
var encodeAddressSolana = (publicKey) => {
|
|
138
|
+
if (publicKey.length !== 32)
|
|
139
|
+
throw new Error("Public key must be 32 bytes long for Solana base58 encoding");
|
|
140
|
+
return base582.encode(publicKey);
|
|
141
|
+
};
|
|
142
|
+
function isSolanaAddress(address) {
|
|
143
|
+
try {
|
|
144
|
+
const bytes = base582.decode(address);
|
|
145
|
+
return bytes.length === 32;
|
|
146
|
+
} catch (error) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/address/encoding/bitcoin.ts
|
|
152
|
+
import { bech32, bech32m } from "bech32";
|
|
153
|
+
import bs58check from "bs58check";
|
|
154
|
+
var isBitcoinAddress = (address) => isBech32mAddress(address) || isBech32Address(address) || isBase58CheckAddress(address);
|
|
155
|
+
function isBech32mAddress(address) {
|
|
156
|
+
try {
|
|
157
|
+
fromBech32m(address);
|
|
158
|
+
} catch {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
function isBech32Address(address) {
|
|
164
|
+
try {
|
|
165
|
+
fromBech32(address);
|
|
166
|
+
} catch {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
function isBase58CheckAddress(address) {
|
|
172
|
+
try {
|
|
173
|
+
fromBase58Check(address);
|
|
174
|
+
} catch {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
function fromBech32m(address) {
|
|
180
|
+
const result = bech32m.decode(address);
|
|
181
|
+
const version = result.words[0];
|
|
182
|
+
if (version === 0) throw new TypeError(address + " uses wrong encoding");
|
|
183
|
+
const data = bech32m.fromWords(result.words.slice(1));
|
|
184
|
+
return {
|
|
185
|
+
version,
|
|
186
|
+
prefix: result.prefix,
|
|
187
|
+
data: Uint8Array.from(data)
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
function fromBech32(address) {
|
|
191
|
+
const result = bech32.decode(address);
|
|
192
|
+
const version = result.words[0];
|
|
193
|
+
if (version !== 0) throw new TypeError(address + " uses wrong encoding");
|
|
194
|
+
const data = bech32.fromWords(result.words.slice(1));
|
|
195
|
+
return {
|
|
196
|
+
version,
|
|
197
|
+
prefix: result.prefix,
|
|
198
|
+
data: Uint8Array.from(data)
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function fromBase58Check(address) {
|
|
202
|
+
const payload = bs58check.decode(address);
|
|
203
|
+
if (payload.length < 21) throw new TypeError(address + " is too short");
|
|
204
|
+
if (payload.length > 21) throw new TypeError(address + " is too long");
|
|
205
|
+
function readUInt8(buffer, offset) {
|
|
206
|
+
if (offset + 1 > buffer.length) {
|
|
207
|
+
throw new Error("Offset is outside the bounds of Uint8Array");
|
|
208
|
+
}
|
|
209
|
+
const buf = Buffer.from(buffer);
|
|
210
|
+
return buf.readUInt8(offset);
|
|
211
|
+
}
|
|
212
|
+
const version = readUInt8(payload, 0);
|
|
213
|
+
const hash = payload.slice(1);
|
|
214
|
+
return { version, hash };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// src/address/encoding/ethereum.ts
|
|
218
|
+
import { keccak_256 } from "@noble/hashes/sha3";
|
|
219
|
+
import { bytesToHex } from "@noble/hashes/utils";
|
|
220
|
+
var encodeAddressEthereum = (publicKey) => {
|
|
221
|
+
if (publicKey[0] !== 4) throw new Error("Invalid public key format");
|
|
222
|
+
const publicKeyWithoutPrefix = publicKey.slice(1);
|
|
223
|
+
const hash = keccak_256(publicKeyWithoutPrefix);
|
|
224
|
+
const address = hash.slice(-20);
|
|
225
|
+
return checksumEthereumAddress(`0x${bytesToHex(address)}`);
|
|
226
|
+
};
|
|
227
|
+
var checksumEthereumAddress = (address) => {
|
|
228
|
+
const addr = address.toLowerCase().replace(/^0x/, "");
|
|
229
|
+
const hash = keccak_256(new TextEncoder().encode(addr));
|
|
230
|
+
const hashHex = bytesToHex(hash);
|
|
231
|
+
const checksum = addr.split("").map((char, i) => parseInt(hashHex[i], 16) >= 8 ? char.toUpperCase() : char).join("");
|
|
232
|
+
return `0x${checksum}`;
|
|
233
|
+
};
|
|
234
|
+
function isEthereumAddress(address) {
|
|
235
|
+
return /^0x[a-fA-F0-9]{40}$/.test(address);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/address/encoding/ss58.ts
|
|
239
|
+
import { base58 as base584 } from "@scure/base";
|
|
240
|
+
|
|
241
|
+
// src/hashing/index.ts
|
|
242
|
+
import { blake2b } from "@noble/hashes/blake2b";
|
|
243
|
+
import { blake3 as blake3Hasher } from "@noble/hashes/blake3";
|
|
244
|
+
import { base58 as base583 } from "@scure/base";
|
|
245
|
+
var blake3 = blake3Hasher;
|
|
246
|
+
var blake2b256 = (msg) => blake2b(msg, { dkLen: 32 });
|
|
247
|
+
var blake2b512 = (msg) => blake2b(msg, { dkLen: 64 });
|
|
248
|
+
var getSafeHash = (bytes) => {
|
|
249
|
+
return base583.encode(blake3Hasher(bytes));
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// src/address/encoding/ss58.ts
|
|
253
|
+
var VALID_PUBLICKEY_LENGTHS = [32, 33];
|
|
254
|
+
var accountId = (publicKey) => {
|
|
255
|
+
if (!VALID_PUBLICKEY_LENGTHS.includes(publicKey.length)) throw new Error("Invalid publicKey");
|
|
256
|
+
return publicKey.length === 33 ? blake2b256(publicKey) : publicKey;
|
|
257
|
+
};
|
|
258
|
+
var SS58PRE = /* @__PURE__ */ new TextEncoder().encode("SS58PRE");
|
|
259
|
+
var CHECKSUM_LENGTH = 2;
|
|
260
|
+
var VALID_PAYLOAD_LENGTHS = [32, 33];
|
|
261
|
+
var ss58Encode = (payload, prefix = 42) => {
|
|
262
|
+
if (!VALID_PAYLOAD_LENGTHS.includes(payload.length)) throw new Error("Invalid payload");
|
|
263
|
+
const prefixBytes = prefix < 64 ? Uint8Array.of(prefix) : Uint8Array.of(
|
|
264
|
+
(prefix & 252) >> 2 | 64,
|
|
265
|
+
prefix >> 8 | (prefix & 3) << 6
|
|
266
|
+
);
|
|
267
|
+
const checksum = blake2b512(Uint8Array.of(...SS58PRE, ...prefixBytes, ...payload)).subarray(
|
|
268
|
+
0,
|
|
269
|
+
CHECKSUM_LENGTH
|
|
270
|
+
);
|
|
271
|
+
return base584.encode(Uint8Array.of(...prefixBytes, ...payload, ...checksum));
|
|
272
|
+
};
|
|
273
|
+
var VALID_ADDRESS_LENGTHS = [35, 36, 37];
|
|
274
|
+
var decodeSs58Address = (addressStr) => {
|
|
275
|
+
const address = base584.decode(addressStr);
|
|
276
|
+
if (!VALID_ADDRESS_LENGTHS.includes(address.length)) throw new Error("Invalid address length");
|
|
277
|
+
const addressChecksum = address.subarray(address.length - CHECKSUM_LENGTH);
|
|
278
|
+
const checksum = blake2b512(
|
|
279
|
+
Uint8Array.of(...SS58PRE, ...address.subarray(0, address.length - CHECKSUM_LENGTH))
|
|
280
|
+
).subarray(0, CHECKSUM_LENGTH);
|
|
281
|
+
if (addressChecksum[0] !== checksum[0] || addressChecksum[1] !== checksum[1])
|
|
282
|
+
throw new Error("Invalid checksum");
|
|
283
|
+
const prefixLength = address[0] & 64 ? 2 : 1;
|
|
284
|
+
const prefix = prefixLength === 1 ? address[0] : (address[0] & 63) << 2 | address[1] >> 6 | (address[1] & 63) << 8;
|
|
285
|
+
const publicKey = address.slice(prefixLength, address.length - CHECKSUM_LENGTH);
|
|
286
|
+
return [publicKey, prefix];
|
|
287
|
+
};
|
|
288
|
+
var encodeAddressSs58 = (publicKey, prefix = 42) => {
|
|
289
|
+
if (typeof publicKey === "string") [publicKey] = decodeSs58Address(publicKey);
|
|
290
|
+
return ss58Encode(accountId(publicKey), prefix);
|
|
291
|
+
};
|
|
292
|
+
function isSs58Address(address) {
|
|
293
|
+
try {
|
|
294
|
+
decodeSs58Address(address);
|
|
295
|
+
return true;
|
|
296
|
+
} catch (error) {
|
|
297
|
+
return false;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// src/address/encoding/detectAddressEncoding.ts
|
|
302
|
+
var CACHE = /* @__PURE__ */ new Map();
|
|
303
|
+
var detectAddressEncodingInner = (address) => {
|
|
304
|
+
if (isEthereumAddress(address)) return "ethereum";
|
|
305
|
+
if (isSs58Address(address)) return "ss58";
|
|
306
|
+
if (isSolanaAddress(address)) return "base58solana";
|
|
307
|
+
if (isBech32mAddress(address)) return "bech32m";
|
|
308
|
+
if (isBech32Address(address)) return "bech32";
|
|
309
|
+
if (isBase58CheckAddress(address)) return "base58check";
|
|
310
|
+
throw new Error(`Unknown address encoding`);
|
|
311
|
+
};
|
|
312
|
+
var detectAddressEncoding = (address) => {
|
|
313
|
+
if (!CACHE.has(address)) CACHE.set(address, detectAddressEncodingInner(address));
|
|
314
|
+
return CACHE.get(address);
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
// src/address/addressFromPublicKey.ts
|
|
318
|
+
var addressFromPublicKey = (publicKey, encoding, options) => {
|
|
319
|
+
switch (encoding) {
|
|
320
|
+
case "ss58":
|
|
321
|
+
return encodeAddressSs58(publicKey, options?.ss58Prefix);
|
|
322
|
+
case "ethereum":
|
|
323
|
+
return encodeAddressEthereum(publicKey);
|
|
324
|
+
case "base58solana":
|
|
325
|
+
return encodeAddressSolana(publicKey);
|
|
326
|
+
case "bech32m":
|
|
327
|
+
case "bech32":
|
|
328
|
+
case "base58check":
|
|
329
|
+
throw new Error("addressFromPublicKey is not implemented for Bitcoin");
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
// src/address/normalizeAddress.ts
|
|
334
|
+
var CACHE2 = /* @__PURE__ */ new Map();
|
|
335
|
+
var normalizeAddress = (address) => {
|
|
336
|
+
try {
|
|
337
|
+
if (!CACHE2.has(address)) CACHE2.set(address, normalizeAnyAddress(address));
|
|
338
|
+
return CACHE2.get(address);
|
|
339
|
+
} catch (cause) {
|
|
340
|
+
throw new Error(`Unable to normalize address: ${address}`, { cause });
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
var normalizeAnyAddress = (address) => {
|
|
344
|
+
switch (detectAddressEncoding(address)) {
|
|
345
|
+
case "ethereum":
|
|
346
|
+
return checksumEthereumAddress(address);
|
|
347
|
+
case "bech32m":
|
|
348
|
+
case "bech32":
|
|
349
|
+
case "base58check":
|
|
350
|
+
case "base58solana":
|
|
351
|
+
return address;
|
|
352
|
+
case "ss58": {
|
|
353
|
+
const [pk] = decodeSs58Address(address);
|
|
354
|
+
return encodeAddressSs58(pk, 42);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
// src/address/isAddressEqual.ts
|
|
360
|
+
var isAddressEqual = (address1, address2) => {
|
|
361
|
+
try {
|
|
362
|
+
return normalizeAddress(address1) === normalizeAddress(address2);
|
|
363
|
+
} catch (err) {
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
// src/address/encodeAnyAddress.ts
|
|
369
|
+
var encodeAnyAddress = (address, options) => {
|
|
370
|
+
const encoding = detectAddressEncoding(address);
|
|
371
|
+
if (encoding === "ss58" && options?.ss58Format !== void 0) {
|
|
372
|
+
const [publicKey] = decodeSs58Address(address);
|
|
373
|
+
return encodeAddressSs58(publicKey, options?.ss58Format ?? 42);
|
|
374
|
+
}
|
|
375
|
+
return normalizeAddress(address);
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
// src/address/isAddressValid.ts
|
|
379
|
+
var isAddressValid = (address) => {
|
|
380
|
+
try {
|
|
381
|
+
detectAddressEncoding(address);
|
|
382
|
+
return true;
|
|
383
|
+
} catch {
|
|
384
|
+
return false;
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
// src/derivation/common.ts
|
|
389
|
+
import { Bytes, str, Tuple, u32 } from "scale-ts";
|
|
390
|
+
var DERIVATION_RE = /(\/{1,2})(\w+)/g;
|
|
391
|
+
var parseSubstrateDerivations = (derivationsStr) => {
|
|
392
|
+
const derivations = [];
|
|
393
|
+
if (derivations)
|
|
394
|
+
for (const [_, type, code] of derivationsStr.matchAll(DERIVATION_RE)) {
|
|
395
|
+
derivations.push([type === "//" ? "hard" : "soft", code]);
|
|
396
|
+
}
|
|
397
|
+
return derivations;
|
|
398
|
+
};
|
|
399
|
+
var createChainCode = (code) => {
|
|
400
|
+
const chainCode = new Uint8Array(32);
|
|
401
|
+
chainCode.set(Number.isNaN(+code) ? str.enc(code) : u32.enc(+code));
|
|
402
|
+
return chainCode;
|
|
403
|
+
};
|
|
404
|
+
var derivationCodec = /* @__PURE__ */ Tuple(str, Bytes(32), Bytes(32));
|
|
405
|
+
var createSubstrateDeriveFn = (prefix) => (seed, chainCode) => blake2b256(derivationCodec.enc([prefix, seed, chainCode]));
|
|
406
|
+
var deriveSubstrateSecretKey = (seed, derivationPath, prefix) => {
|
|
407
|
+
const derivations = parseSubstrateDerivations(derivationPath);
|
|
408
|
+
const derive = createSubstrateDeriveFn(prefix);
|
|
409
|
+
return derivations.reduce((seed2, [type, chainCode]) => {
|
|
410
|
+
const code = createChainCode(chainCode);
|
|
411
|
+
if (type === "soft") throw new Error("Soft derivations are not supported");
|
|
412
|
+
return derive(seed2, code);
|
|
413
|
+
}, seed);
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
// src/derivation/deriveEcdsa.ts
|
|
417
|
+
var deriveEcdsa = (seed, derivationPath) => {
|
|
418
|
+
const secretKey = deriveSubstrateSecretKey(seed, derivationPath, "Secp256k1HDKD");
|
|
419
|
+
const publicKey = getPublicKeyEcdsa(secretKey);
|
|
420
|
+
return {
|
|
421
|
+
type: "ecdsa",
|
|
422
|
+
secretKey,
|
|
423
|
+
publicKey,
|
|
424
|
+
address: addressFromPublicKey(publicKey, "ss58")
|
|
425
|
+
};
|
|
426
|
+
};
|
|
427
|
+
var getPublicKeyEcdsa = (secretKey) => {
|
|
428
|
+
return secp256k1.getPublicKey(secretKey);
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
// src/derivation/deriveEd25519.ts
|
|
432
|
+
import { ed25519 as ed255192 } from "@noble/curves/ed25519";
|
|
433
|
+
var deriveEd25519 = (seed, derivationPath) => {
|
|
434
|
+
const secretKey = deriveSubstrateSecretKey(seed, derivationPath, "Ed25519HDKD");
|
|
435
|
+
const publicKey = getPublicKeyEd25519(secretKey);
|
|
436
|
+
return {
|
|
437
|
+
type: "ed25519",
|
|
438
|
+
secretKey,
|
|
439
|
+
publicKey,
|
|
440
|
+
address: addressFromPublicKey(publicKey, "ss58")
|
|
441
|
+
};
|
|
442
|
+
};
|
|
443
|
+
var getPublicKeyEd25519 = (secretKey) => {
|
|
444
|
+
if (secretKey.length === 64) {
|
|
445
|
+
const [privateComponent, publicComponent] = [secretKey.slice(0, 32), secretKey.slice(32)];
|
|
446
|
+
const publicKey = ed255192.getPublicKey(privateComponent);
|
|
447
|
+
if (!isUint8ArrayEq(publicComponent, publicKey)) return ed255192.getPublicKey(secretKey);
|
|
448
|
+
return publicKey;
|
|
449
|
+
}
|
|
450
|
+
return ed255192.getPublicKey(secretKey);
|
|
451
|
+
};
|
|
452
|
+
var isUint8ArrayEq = (a, b) => a.length !== b.length || a.some((v, i) => v !== b[i]) ? false : true;
|
|
453
|
+
|
|
454
|
+
// src/derivation/deriveEthereum.ts
|
|
455
|
+
import { secp256k1 as secp256k12 } from "@noble/curves/secp256k1";
|
|
456
|
+
import { HDKey } from "@scure/bip32";
|
|
457
|
+
var deriveEthereum = (seed, derivationPath) => {
|
|
458
|
+
const hdkey = HDKey.fromMasterSeed(seed);
|
|
459
|
+
const childKey = hdkey.derive(derivationPath);
|
|
460
|
+
if (!childKey.privateKey) throw new Error("Invalid derivation path");
|
|
461
|
+
const secretKey = new Uint8Array(childKey.privateKey);
|
|
462
|
+
const publicKey = getPublicKeyEthereum(secretKey);
|
|
463
|
+
return {
|
|
464
|
+
type: "ethereum",
|
|
465
|
+
secretKey,
|
|
466
|
+
publicKey,
|
|
467
|
+
address: addressFromPublicKey(publicKey, "ethereum")
|
|
468
|
+
};
|
|
469
|
+
};
|
|
470
|
+
var getPublicKeyEthereum = (secretKey) => {
|
|
471
|
+
const scalar = secp256k12.utils.normPrivateKeyToScalar(secretKey);
|
|
472
|
+
return secp256k12.getPublicKey(scalar, false);
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
// src/derivation/deriveSolana.ts
|
|
476
|
+
import { ed25519 as ed255193 } from "@noble/curves/ed25519";
|
|
477
|
+
import { hmac } from "@noble/hashes/hmac";
|
|
478
|
+
import { sha512 } from "@noble/hashes/sha512";
|
|
479
|
+
var parseDerivationPath = (path) => {
|
|
480
|
+
if (!path.startsWith("m/")) throw new Error("Path must start with 'm/'");
|
|
481
|
+
return path.split("/").slice(1).map((p) => {
|
|
482
|
+
if (!p.endsWith("'")) throw new Error("Only hardened derivation is supported");
|
|
483
|
+
return parseInt(p.slice(0, -1), 10) + 2147483648;
|
|
484
|
+
});
|
|
485
|
+
};
|
|
486
|
+
var deriveSolana = (seed, derivationPath) => {
|
|
487
|
+
let I = hmac(sha512, new TextEncoder().encode("ed25519 seed"), seed);
|
|
488
|
+
let secretKey = I.slice(0, 32);
|
|
489
|
+
let chainCode = I.slice(32);
|
|
490
|
+
for (const index of parseDerivationPath(derivationPath)) {
|
|
491
|
+
const data = new Uint8Array(1 + secretKey.length + 4);
|
|
492
|
+
data.set([0], 0);
|
|
493
|
+
data.set(secretKey, 1);
|
|
494
|
+
data.set(
|
|
495
|
+
new Uint8Array([
|
|
496
|
+
index >> 24 & 255,
|
|
497
|
+
index >> 16 & 255,
|
|
498
|
+
index >> 8 & 255,
|
|
499
|
+
index & 255
|
|
500
|
+
]),
|
|
501
|
+
1 + secretKey.length
|
|
502
|
+
);
|
|
503
|
+
I = hmac(sha512, chainCode, data);
|
|
504
|
+
secretKey = I.slice(0, 32);
|
|
505
|
+
chainCode = I.slice(32);
|
|
506
|
+
}
|
|
507
|
+
const publicKey = getPublicKeySolana(secretKey);
|
|
508
|
+
return {
|
|
509
|
+
type: "solana",
|
|
510
|
+
secretKey,
|
|
511
|
+
publicKey,
|
|
512
|
+
address: addressFromPublicKey(publicKey, "base58solana")
|
|
513
|
+
};
|
|
514
|
+
};
|
|
515
|
+
var getPublicKeySolana = (secretKey) => {
|
|
516
|
+
return ed255193.getPublicKey(secretKey);
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
// src/derivation/deriveSr25519.ts
|
|
520
|
+
import { randomBytes } from "@noble/hashes/utils";
|
|
521
|
+
import { getPublicKey, HDKD, secretFromSeed } from "micro-sr25519";
|
|
522
|
+
var deriveSr25519 = (seed, derivationPath) => {
|
|
523
|
+
const derivations = parseSubstrateDerivations(derivationPath);
|
|
524
|
+
const secretKey = derivations.reduce((secretKey2, [type, chainCode]) => {
|
|
525
|
+
const deriveFn = type === "hard" ? HDKD.secretHard : HDKD.secretSoft;
|
|
526
|
+
const code = createChainCode(chainCode);
|
|
527
|
+
return deriveFn(secretKey2, code, randomBytes);
|
|
528
|
+
}, secretFromSeed(seed));
|
|
529
|
+
const publicKey = getPublicKeySr25519(secretKey);
|
|
530
|
+
return {
|
|
531
|
+
type: "sr25519",
|
|
532
|
+
secretKey,
|
|
533
|
+
publicKey,
|
|
534
|
+
address: addressFromPublicKey(publicKey, "ss58")
|
|
535
|
+
};
|
|
536
|
+
};
|
|
537
|
+
var getPublicKeySr25519 = getPublicKey;
|
|
538
|
+
|
|
539
|
+
// src/derivation/utils.ts
|
|
540
|
+
var deriveKeypair = (seed, derivationPath, curve) => {
|
|
541
|
+
switch (curve) {
|
|
542
|
+
case "sr25519":
|
|
543
|
+
return deriveSr25519(seed, derivationPath);
|
|
544
|
+
case "ed25519":
|
|
545
|
+
return deriveEd25519(seed, derivationPath);
|
|
546
|
+
case "ecdsa":
|
|
547
|
+
return deriveEcdsa(seed, derivationPath);
|
|
548
|
+
case "bitcoin-ecdsa":
|
|
549
|
+
case "bitcoin-ed25519":
|
|
550
|
+
throw new Error("deriveKeypair is not implemented for Bitcoin");
|
|
551
|
+
case "ethereum":
|
|
552
|
+
return deriveEthereum(seed, derivationPath);
|
|
553
|
+
case "solana":
|
|
554
|
+
return deriveSolana(seed, derivationPath);
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
var getPublicKeyFromSecret = (secretKey, curve) => {
|
|
558
|
+
switch (curve) {
|
|
559
|
+
case "ecdsa":
|
|
560
|
+
return getPublicKeyEcdsa(secretKey);
|
|
561
|
+
case "ethereum":
|
|
562
|
+
return getPublicKeyEthereum(secretKey);
|
|
563
|
+
case "sr25519":
|
|
564
|
+
return getPublicKeySr25519(secretKey);
|
|
565
|
+
case "ed25519":
|
|
566
|
+
return getPublicKeyEd25519(secretKey);
|
|
567
|
+
case "bitcoin-ecdsa":
|
|
568
|
+
case "bitcoin-ed25519":
|
|
569
|
+
throw new Error("getPublicKeyFromSecret is not implemented for Bitcoin");
|
|
570
|
+
case "solana":
|
|
571
|
+
return getPublicKeySolana(secretKey);
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
var addressFromMnemonic = async (mnemonic, derivationPath, curve) => {
|
|
575
|
+
const entropy = mnemonicToEntropy(mnemonic);
|
|
576
|
+
const seed = await entropyToSeed(entropy, curve);
|
|
577
|
+
const { address } = deriveKeypair(seed, derivationPath, curve);
|
|
578
|
+
return address;
|
|
579
|
+
};
|
|
580
|
+
var removeHexPrefix = (secretKey) => {
|
|
581
|
+
if (secretKey.startsWith("0x")) return secretKey.slice(2);
|
|
582
|
+
return secretKey;
|
|
583
|
+
};
|
|
584
|
+
var parseSecretKey = (secretKey, platform) => {
|
|
585
|
+
switch (platform) {
|
|
586
|
+
case "ethereum": {
|
|
587
|
+
const privateKey = removeHexPrefix(secretKey);
|
|
588
|
+
return hex2.decode(privateKey);
|
|
589
|
+
}
|
|
590
|
+
case "solana": {
|
|
591
|
+
const bytes = secretKey.startsWith("[") ? (
|
|
592
|
+
// JSON bytes array (ex: solflare)
|
|
593
|
+
Uint8Array.from(JSON.parse(secretKey))
|
|
594
|
+
) : (
|
|
595
|
+
// base58 encoded string (ex: phantom)
|
|
596
|
+
base585.decode(secretKey)
|
|
597
|
+
);
|
|
598
|
+
if (bytes.length === 64) {
|
|
599
|
+
const privateKey = bytes.slice(0, 32);
|
|
600
|
+
const publicKey = bytes.slice(32, 64);
|
|
601
|
+
const computedPublicKey = getPublicKeySolana(privateKey);
|
|
602
|
+
if (!publicKey.every((b, i) => b === computedPublicKey[i]))
|
|
603
|
+
throw new Error("Invalid Solana secret key: public key does not match");
|
|
604
|
+
return privateKey;
|
|
605
|
+
} else if (bytes.length === 32) return bytes;
|
|
606
|
+
throw new Error("Invalid Solana secret key length");
|
|
607
|
+
}
|
|
608
|
+
default:
|
|
609
|
+
throw new Error("Not implemented");
|
|
610
|
+
}
|
|
611
|
+
};
|
|
612
|
+
var isValidDerivationPath = async (derivationPath, curve) => {
|
|
613
|
+
try {
|
|
614
|
+
deriveKeypair(await getDevSeed(curve), derivationPath, curve);
|
|
615
|
+
return true;
|
|
616
|
+
} catch (err) {
|
|
617
|
+
return false;
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
// src/platform/index.ts
|
|
622
|
+
var getAccountPlatformFromCurve = (curve) => {
|
|
623
|
+
switch (curve) {
|
|
624
|
+
case "sr25519":
|
|
625
|
+
case "ed25519":
|
|
626
|
+
case "ecdsa":
|
|
627
|
+
return "polkadot";
|
|
628
|
+
case "ethereum":
|
|
629
|
+
return "ethereum";
|
|
630
|
+
case "bitcoin-ecdsa":
|
|
631
|
+
case "bitcoin-ed25519":
|
|
632
|
+
return "bitcoin";
|
|
633
|
+
case "solana":
|
|
634
|
+
return "solana";
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
var getAccountPlatformFromEncoding = (encoding) => {
|
|
638
|
+
switch (encoding) {
|
|
639
|
+
case "ss58":
|
|
640
|
+
return "polkadot";
|
|
641
|
+
case "ethereum":
|
|
642
|
+
return "ethereum";
|
|
643
|
+
case "bech32m":
|
|
644
|
+
case "bech32":
|
|
645
|
+
case "base58check":
|
|
646
|
+
return "bitcoin";
|
|
647
|
+
case "base58solana":
|
|
648
|
+
return "solana";
|
|
649
|
+
}
|
|
650
|
+
};
|
|
651
|
+
var getAccountPlatformFromAddress = (address) => {
|
|
652
|
+
const encoding = detectAddressEncoding(address);
|
|
653
|
+
return getAccountPlatformFromEncoding(encoding);
|
|
654
|
+
};
|
|
655
|
+
|
|
656
|
+
// src/encryption/encryptKemAead.ts
|
|
657
|
+
import { xchacha20poly1305 } from "@noble/ciphers/chacha.js";
|
|
658
|
+
import { concatBytes } from "@noble/ciphers/utils.js";
|
|
659
|
+
import { MlKem768 } from "mlkem";
|
|
660
|
+
var encryptKemAead = async (publicKey, plaintext) => {
|
|
661
|
+
const kem = new MlKem768();
|
|
662
|
+
const [kemCt, sharedSecret] = await kem.encap(publicKey);
|
|
663
|
+
if (sharedSecret.length !== 32) {
|
|
664
|
+
throw new Error(`Expected 32-byte shared secret, got ${sharedSecret.length}`);
|
|
665
|
+
}
|
|
666
|
+
const nonce = crypto.getRandomValues(new Uint8Array(24));
|
|
667
|
+
const aead = xchacha20poly1305(sharedSecret, nonce);
|
|
668
|
+
const aeadCt = aead.encrypt(plaintext);
|
|
669
|
+
const kemLen = new Uint8Array(2);
|
|
670
|
+
new DataView(kemLen.buffer).setUint16(0, kemCt.length, true);
|
|
671
|
+
return concatBytes(kemLen, kemCt, nonce, aeadCt);
|
|
672
|
+
};
|
|
673
|
+
export {
|
|
674
|
+
DEV_MNEMONIC_ETHEREUM,
|
|
675
|
+
DEV_MNEMONIC_POLKADOT,
|
|
676
|
+
addressEncodingFromCurve,
|
|
677
|
+
addressFromMnemonic,
|
|
678
|
+
addressFromPublicKey,
|
|
679
|
+
base58,
|
|
680
|
+
base64,
|
|
681
|
+
blake2b256,
|
|
682
|
+
blake2b512,
|
|
683
|
+
blake3,
|
|
684
|
+
checksumEthereumAddress,
|
|
685
|
+
decodeSs58Address,
|
|
686
|
+
deriveKeypair,
|
|
687
|
+
detectAddressEncoding,
|
|
688
|
+
ed25519,
|
|
689
|
+
encodeAddressEthereum,
|
|
690
|
+
encodeAddressSolana,
|
|
691
|
+
encodeAddressSs58,
|
|
692
|
+
encodeAnyAddress,
|
|
693
|
+
encryptKemAead,
|
|
694
|
+
entropyToMnemonic,
|
|
695
|
+
entropyToSeed,
|
|
696
|
+
fromBase58Check,
|
|
697
|
+
fromBech32,
|
|
698
|
+
fromBech32m,
|
|
699
|
+
generateMnemonic,
|
|
700
|
+
getAccountPlatformFromAddress,
|
|
701
|
+
getAccountPlatformFromCurve,
|
|
702
|
+
getAccountPlatformFromEncoding,
|
|
703
|
+
getDevSeed,
|
|
704
|
+
getPublicKeyFromSecret,
|
|
705
|
+
getPublicKeySolana,
|
|
706
|
+
getSafeHash,
|
|
707
|
+
hex,
|
|
708
|
+
isAddressEqual,
|
|
709
|
+
isAddressValid,
|
|
710
|
+
isBase58CheckAddress,
|
|
711
|
+
isBech32Address,
|
|
712
|
+
isBech32mAddress,
|
|
713
|
+
isBitcoinAddress,
|
|
714
|
+
isEthereumAddress,
|
|
715
|
+
isSolanaAddress,
|
|
716
|
+
isSs58Address,
|
|
717
|
+
isValidDerivationPath,
|
|
718
|
+
isValidMnemonic,
|
|
719
|
+
mnemonicToEntropy,
|
|
720
|
+
normalizeAddress,
|
|
721
|
+
parseSecretKey,
|
|
722
|
+
pbkdf2,
|
|
723
|
+
removeHexPrefix,
|
|
724
|
+
utf8
|
|
725
|
+
};
|
|
726
|
+
//# sourceMappingURL=index.mjs.map
|