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