@metamask/eth-hd-keyring 10.0.1 → 11.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/CHANGELOG.md +8 -1
- package/dist/hd-keyring.cjs +388 -0
- package/dist/hd-keyring.cjs.map +1 -0
- package/dist/hd-keyring.d.cts +166 -0
- package/dist/hd-keyring.d.cts.map +1 -0
- package/dist/hd-keyring.d.mts +166 -0
- package/dist/hd-keyring.d.mts.map +1 -0
- package/dist/hd-keyring.mjs +384 -0
- package/dist/hd-keyring.mjs.map +1 -0
- package/dist/index.cjs +3 -384
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -166
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -166
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -384
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [11.0.0]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- **BREAKING:** The `HdKeyring` is not exported as default anymore ([#161](https://github.com/MetaMask/accounts/pull/161))
|
|
15
|
+
|
|
10
16
|
## [10.0.1]
|
|
11
17
|
|
|
12
18
|
### Changed
|
|
@@ -175,7 +181,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
175
181
|
- Deserialize method (and `HdKeyring` constructor by extension) can no longer be passed an options object containing a value for `numberOfAccounts` if it is not also containing a value for `mnemonic`.
|
|
176
182
|
- Package name changed from `eth-hd-keyring` to `@metamask/eth-hd-keyring`.
|
|
177
183
|
|
|
178
|
-
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@
|
|
184
|
+
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@11.0.0...HEAD
|
|
185
|
+
[11.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@10.0.1...@metamask/eth-hd-keyring@11.0.0
|
|
179
186
|
[10.0.1]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@10.0.0...@metamask/eth-hd-keyring@10.0.1
|
|
180
187
|
[10.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@9.0.1...@metamask/eth-hd-keyring@10.0.0
|
|
181
188
|
[9.0.1]: https://github.com/MetaMask/accounts/compare/@metamask/eth-hd-keyring@9.0.0...@metamask/eth-hd-keyring@9.0.1
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _HdKeyring_instances, _HdKeyring_wallets, _HdKeyring_cryptographicFunctions, _HdKeyring_uint8ArrayToString, _HdKeyring_stringToUint8Array, _HdKeyring_mnemonicToUint8Array, _HdKeyring_getPrivateKeyFor, _HdKeyring_getWalletForAccount, _HdKeyring_initFromMnemonic, _HdKeyring_addressfromPublicKey, _HdKeyring_normalizeAddress;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.HdKeyring = void 0;
|
|
16
|
+
const util_1 = require("@ethereumjs/util");
|
|
17
|
+
const eth_sig_util_1 = require("@metamask/eth-sig-util");
|
|
18
|
+
const key_tree_1 = require("@metamask/key-tree");
|
|
19
|
+
const scure_bip39_1 = require("@metamask/scure-bip39");
|
|
20
|
+
const english_1 = require("@metamask/scure-bip39/dist/wordlists/english.js");
|
|
21
|
+
const utils_1 = require("@metamask/utils");
|
|
22
|
+
const hdkey_1 = require("ethereum-cryptography/hdkey");
|
|
23
|
+
const keccak_1 = require("ethereum-cryptography/keccak");
|
|
24
|
+
const utils_2 = require("ethereum-cryptography/utils");
|
|
25
|
+
// Options:
|
|
26
|
+
const hdPathString = `m/44'/60'/0'/0`;
|
|
27
|
+
const type = 'HD Key Tree';
|
|
28
|
+
/**
|
|
29
|
+
* Checks if the given value is a valid serialized Buffer compatible with
|
|
30
|
+
* the return type of `Buffer.toJSON`.
|
|
31
|
+
*
|
|
32
|
+
* @param value - The value to check.
|
|
33
|
+
* @returns `true` if the value is a valid serialized buffer, `false` otherwise.
|
|
34
|
+
*/
|
|
35
|
+
function isSerializedBuffer(value) {
|
|
36
|
+
return (typeof value === 'object' &&
|
|
37
|
+
value !== null &&
|
|
38
|
+
'type' in value &&
|
|
39
|
+
value.type === 'Buffer' &&
|
|
40
|
+
'data' in value &&
|
|
41
|
+
Array.isArray(value.data));
|
|
42
|
+
}
|
|
43
|
+
class HdKeyring {
|
|
44
|
+
constructor(opts = {}) {
|
|
45
|
+
_HdKeyring_instances.add(this);
|
|
46
|
+
this.type = type;
|
|
47
|
+
this.hdPath = hdPathString;
|
|
48
|
+
_HdKeyring_wallets.set(this, []);
|
|
49
|
+
_HdKeyring_cryptographicFunctions.set(this, void 0);
|
|
50
|
+
// Cryptographic functions to be used by `@metamask/key-tree`. It will use built-in implementations if not provided here.
|
|
51
|
+
__classPrivateFieldSet(this, _HdKeyring_cryptographicFunctions, opts.cryptographicFunctions, "f");
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Initialize the keyring with a random mnemonic.
|
|
55
|
+
*
|
|
56
|
+
* @returns A promise that resolves when the process is complete.
|
|
57
|
+
*/
|
|
58
|
+
async generateRandomMnemonic() {
|
|
59
|
+
await __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_initFromMnemonic).call(this, (0, scure_bip39_1.generateMnemonic)(english_1.wordlist));
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Return the serialized state of the keyring.
|
|
63
|
+
*
|
|
64
|
+
* @returns The serialized state of the keyring.
|
|
65
|
+
*/
|
|
66
|
+
async serialize() {
|
|
67
|
+
let mnemonic = [];
|
|
68
|
+
if (this.mnemonic) {
|
|
69
|
+
const mnemonicAsString = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_uint8ArrayToString).call(this, this.mnemonic);
|
|
70
|
+
mnemonic = Array.from(new TextEncoder().encode(mnemonicAsString));
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
mnemonic,
|
|
74
|
+
numberOfAccounts: __classPrivateFieldGet(this, _HdKeyring_wallets, "f").length,
|
|
75
|
+
hdPath: this.hdPath,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Initialize the keyring with the given serialized state.
|
|
80
|
+
*
|
|
81
|
+
* @param opts - The serialized state of the keyring.
|
|
82
|
+
* @returns An empty array.
|
|
83
|
+
*/
|
|
84
|
+
async deserialize(opts = {}) {
|
|
85
|
+
if (opts.numberOfAccounts && !opts.mnemonic) {
|
|
86
|
+
throw new Error('Eth-Hd-Keyring: Deserialize method cannot be called with an opts value for numberOfAccounts and no menmonic');
|
|
87
|
+
}
|
|
88
|
+
if (this.root) {
|
|
89
|
+
throw new Error('Eth-Hd-Keyring: Secret recovery phrase already provided');
|
|
90
|
+
}
|
|
91
|
+
__classPrivateFieldSet(this, _HdKeyring_wallets, [], "f");
|
|
92
|
+
this.mnemonic = null;
|
|
93
|
+
this.root = null;
|
|
94
|
+
this.hdPath = opts.hdPath ?? hdPathString;
|
|
95
|
+
if (opts.mnemonic) {
|
|
96
|
+
await __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_initFromMnemonic).call(this, opts.mnemonic);
|
|
97
|
+
}
|
|
98
|
+
if (opts.numberOfAccounts) {
|
|
99
|
+
return this.addAccounts(opts.numberOfAccounts);
|
|
100
|
+
}
|
|
101
|
+
return [];
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Add new accounts to the keyring. The accounts will be derived
|
|
105
|
+
* sequentially from the root HD wallet, using increasing indices.
|
|
106
|
+
*
|
|
107
|
+
* @param numberOfAccounts - The number of accounts to add.
|
|
108
|
+
* @returns The addresses of the new accounts.
|
|
109
|
+
*/
|
|
110
|
+
async addAccounts(numberOfAccounts = 1) {
|
|
111
|
+
if (!this.root) {
|
|
112
|
+
throw new Error('Eth-Hd-Keyring: No secret recovery phrase provided');
|
|
113
|
+
}
|
|
114
|
+
const oldLen = __classPrivateFieldGet(this, _HdKeyring_wallets, "f").length;
|
|
115
|
+
const newWallets = [];
|
|
116
|
+
for (let i = oldLen; i < numberOfAccounts + oldLen; i++) {
|
|
117
|
+
const wallet = this.root.deriveChild(i);
|
|
118
|
+
newWallets.push(wallet);
|
|
119
|
+
__classPrivateFieldGet(this, _HdKeyring_wallets, "f").push(wallet);
|
|
120
|
+
}
|
|
121
|
+
const hexWallets = newWallets.map((wallet) => {
|
|
122
|
+
(0, utils_1.assert)(wallet.publicKey, 'Expected public key to be set');
|
|
123
|
+
return __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_addressfromPublicKey).call(this, wallet.publicKey);
|
|
124
|
+
});
|
|
125
|
+
return Promise.resolve(hexWallets);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Get the addresses of all accounts in the keyring.
|
|
129
|
+
*
|
|
130
|
+
* @returns The addresses of all accounts in the keyring.
|
|
131
|
+
*/
|
|
132
|
+
getAccounts() {
|
|
133
|
+
return __classPrivateFieldGet(this, _HdKeyring_wallets, "f").map((wallet) => {
|
|
134
|
+
(0, utils_1.assert)(wallet.publicKey, 'Expected public key to be set');
|
|
135
|
+
return __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_addressfromPublicKey).call(this, wallet.publicKey);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Get the public address of the account for the given app key origin.
|
|
140
|
+
*
|
|
141
|
+
* @param address - The address of the account.
|
|
142
|
+
* @param origin - The origin of the app requesting the account.
|
|
143
|
+
* @returns The public address of the account.
|
|
144
|
+
*/
|
|
145
|
+
async getAppKeyAddress(address, origin) {
|
|
146
|
+
if (!origin || typeof origin !== 'string') {
|
|
147
|
+
throw new Error(`'origin' must be a non-empty string`);
|
|
148
|
+
}
|
|
149
|
+
const wallet = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getWalletForAccount).call(this, address, {
|
|
150
|
+
withAppKeyOrigin: origin,
|
|
151
|
+
});
|
|
152
|
+
(0, utils_1.assert)(wallet.publicKey, 'Expected public key to be set');
|
|
153
|
+
const appKeyAddress = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_normalizeAddress).call(this, (0, util_1.publicToAddress)(wallet.publicKey).toString('hex'));
|
|
154
|
+
return appKeyAddress;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Export the private key for a specific account.
|
|
158
|
+
*
|
|
159
|
+
* @param address - The address of the account.
|
|
160
|
+
* @param opts - The options for exporting the account.
|
|
161
|
+
* @param opts.withAppKeyOrigin - An optional string to export the account
|
|
162
|
+
* for a specific app origin.
|
|
163
|
+
* @returns The private key of the account.
|
|
164
|
+
*/
|
|
165
|
+
async exportAccount(address, opts) {
|
|
166
|
+
const wallet = opts
|
|
167
|
+
? __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getWalletForAccount).call(this, address, opts)
|
|
168
|
+
: __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getWalletForAccount).call(this, address);
|
|
169
|
+
const { privateKey } = wallet;
|
|
170
|
+
(0, utils_1.assert)(privateKey instanceof Uint8Array, 'Expected private key to be of type Uint8Array');
|
|
171
|
+
return (0, utils_2.bytesToHex)(privateKey);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Sign a transaction using the private key of the specified account.
|
|
175
|
+
*
|
|
176
|
+
* @param address - The address of the account.
|
|
177
|
+
* @param tx - The transaction to sign.
|
|
178
|
+
* @param opts - The options for signing the transaction.
|
|
179
|
+
* @returns The signed transaction.
|
|
180
|
+
*/
|
|
181
|
+
async signTransaction(address, tx, opts = {}) {
|
|
182
|
+
const privKey = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getPrivateKeyFor).call(this, address, opts);
|
|
183
|
+
const signedTx = tx.sign(Buffer.from(privKey));
|
|
184
|
+
// Newer versions of Ethereumjs-tx are immutable and return a new tx object
|
|
185
|
+
return signedTx ?? tx;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Sign a message using the private key of the specified account.
|
|
189
|
+
*
|
|
190
|
+
* @param address - The address of the account.
|
|
191
|
+
* @param data - The data to sign.
|
|
192
|
+
* @param opts - The options for signing the message.
|
|
193
|
+
* @returns The signature of the message.
|
|
194
|
+
*/
|
|
195
|
+
async signMessage(address, data, opts = {}) {
|
|
196
|
+
(0, utils_1.assertIsHexString)(data);
|
|
197
|
+
const message = (0, utils_1.remove0x)(data);
|
|
198
|
+
const privKey = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getPrivateKeyFor).call(this, address, opts);
|
|
199
|
+
const msgSig = (0, util_1.ecsign)(Buffer.from(message, 'hex'), Buffer.from(privKey));
|
|
200
|
+
const rawMsgSig = (0, eth_sig_util_1.concatSig)(
|
|
201
|
+
// WARN: verify this cast to Buffer
|
|
202
|
+
msgSig.v, msgSig.r, msgSig.s);
|
|
203
|
+
return rawMsgSig;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Sign a personal message using the private key of the specified account.
|
|
207
|
+
* This method is compatible with the `personal_sign` RPC method.
|
|
208
|
+
*
|
|
209
|
+
* @param address - The address of the account.
|
|
210
|
+
* @param msgHex - The message to sign.
|
|
211
|
+
* @param opts - The options for signing the message.
|
|
212
|
+
* @returns The signature of the message.
|
|
213
|
+
*/
|
|
214
|
+
async signPersonalMessage(address, msgHex, opts = {}) {
|
|
215
|
+
const privKey = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getPrivateKeyFor).call(this, address, opts);
|
|
216
|
+
const privateKey = Buffer.from(privKey);
|
|
217
|
+
return (0, eth_sig_util_1.personalSign)({ privateKey, data: msgHex });
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Decrypt an encrypted message using the private key of the specified account.
|
|
221
|
+
* The message must be encrypted using the public key of the account.
|
|
222
|
+
* This method is compatible with the `eth_decryptMessage` RPC method
|
|
223
|
+
*
|
|
224
|
+
* @param withAccount - The address of the account.
|
|
225
|
+
* @param encryptedData - The encrypted data.
|
|
226
|
+
* @returns The decrypted message.
|
|
227
|
+
*/
|
|
228
|
+
async decryptMessage(withAccount, encryptedData) {
|
|
229
|
+
const wallet = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getWalletForAccount).call(this, withAccount);
|
|
230
|
+
const { privateKey: privateKeyAsUint8Array } = wallet;
|
|
231
|
+
(0, utils_1.assert)(privateKeyAsUint8Array, 'Expected private key to be set');
|
|
232
|
+
const privateKeyAsHex = Buffer.from(privateKeyAsUint8Array).toString('hex');
|
|
233
|
+
return (0, eth_sig_util_1.decrypt)({ privateKey: privateKeyAsHex, encryptedData });
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Sign a typed message using the private key of the specified account.
|
|
237
|
+
* This method is compatible with the `eth_signTypedData` RPC method.
|
|
238
|
+
*
|
|
239
|
+
* @param withAccount - The address of the account.
|
|
240
|
+
* @param typedData - The typed data to sign.
|
|
241
|
+
* @param opts - The options for signing the message.
|
|
242
|
+
* @returns The signature of the message.
|
|
243
|
+
*/
|
|
244
|
+
async signTypedData(withAccount, typedData, opts = { version: eth_sig_util_1.SignTypedDataVersion.V1 }) {
|
|
245
|
+
// Treat invalid versions as "V1"
|
|
246
|
+
const version = Object.keys(eth_sig_util_1.SignTypedDataVersion).includes(opts.version)
|
|
247
|
+
? opts.version
|
|
248
|
+
: eth_sig_util_1.SignTypedDataVersion.V1;
|
|
249
|
+
const privateKey = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getPrivateKeyFor).call(this, withAccount, opts);
|
|
250
|
+
return (0, eth_sig_util_1.signTypedData)({
|
|
251
|
+
privateKey: Buffer.from(privateKey),
|
|
252
|
+
data: typedData,
|
|
253
|
+
version,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Sign an EIP-7702 authorization using the private key of the specified account.
|
|
258
|
+
* This method is compatible with the EIP-7702 standard for enabling smart contract code for EOAs.
|
|
259
|
+
*
|
|
260
|
+
* @param withAccount - The address of the account.
|
|
261
|
+
* @param authorization - The EIP-7702 authorization to sign.
|
|
262
|
+
* @param opts - The options for selecting the account.
|
|
263
|
+
* @returns The signature of the authorization.
|
|
264
|
+
*/
|
|
265
|
+
async signEip7702Authorization(withAccount, authorization, opts) {
|
|
266
|
+
const privateKey = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getPrivateKeyFor).call(this, withAccount, opts);
|
|
267
|
+
return (0, eth_sig_util_1.signEIP7702Authorization)({
|
|
268
|
+
privateKey: Buffer.from(privateKey),
|
|
269
|
+
authorization,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Remove an account from the keyring.
|
|
274
|
+
*
|
|
275
|
+
* @param account - The address of the account to remove.
|
|
276
|
+
*/
|
|
277
|
+
removeAccount(account) {
|
|
278
|
+
const address = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_normalizeAddress).call(this, account);
|
|
279
|
+
if (!__classPrivateFieldGet(this, _HdKeyring_wallets, "f")
|
|
280
|
+
.map(({ publicKey }) => publicKey && __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_addressfromPublicKey).call(this, publicKey))
|
|
281
|
+
.includes(address)) {
|
|
282
|
+
throw new Error(`Address ${address} not found in this keyring`);
|
|
283
|
+
}
|
|
284
|
+
__classPrivateFieldSet(this, _HdKeyring_wallets, __classPrivateFieldGet(this, _HdKeyring_wallets, "f").filter(({ publicKey }) => publicKey && __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_addressfromPublicKey).call(this, publicKey) !== address), "f");
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Get the public key of the account to be used for encryption.
|
|
288
|
+
*
|
|
289
|
+
* @param withAccount - The address of the account.
|
|
290
|
+
* @param opts - The options for selecting the account.
|
|
291
|
+
* @returns The public key of the account.
|
|
292
|
+
*/
|
|
293
|
+
async getEncryptionPublicKey(withAccount, opts = {}) {
|
|
294
|
+
const privKey = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getPrivateKeyFor).call(this, withAccount, opts);
|
|
295
|
+
const publicKey = (0, eth_sig_util_1.getEncryptionPublicKey)((0, utils_2.bytesToHex)(privKey));
|
|
296
|
+
return publicKey;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
exports.HdKeyring = HdKeyring;
|
|
300
|
+
_HdKeyring_wallets = new WeakMap(), _HdKeyring_cryptographicFunctions = new WeakMap(), _HdKeyring_instances = new WeakSet(), _HdKeyring_uint8ArrayToString = function _HdKeyring_uint8ArrayToString(mnemonic) {
|
|
301
|
+
const recoveredIndices = Array.from(new Uint16Array(new Uint8Array(mnemonic).buffer));
|
|
302
|
+
return recoveredIndices.map((i) => english_1.wordlist[i]).join(' ');
|
|
303
|
+
}, _HdKeyring_stringToUint8Array = function _HdKeyring_stringToUint8Array(mnemonic) {
|
|
304
|
+
const indices = mnemonic.split(' ').map((word) => english_1.wordlist.indexOf(word));
|
|
305
|
+
return new Uint8Array(new Uint16Array(indices).buffer);
|
|
306
|
+
}, _HdKeyring_mnemonicToUint8Array = function _HdKeyring_mnemonicToUint8Array(mnemonic) {
|
|
307
|
+
let mnemonicData = mnemonic;
|
|
308
|
+
// When using `Buffer.toJSON()`, the Buffer is serialized into an object
|
|
309
|
+
// with the structure `{ type: 'Buffer', data: [...] }`
|
|
310
|
+
if (isSerializedBuffer(mnemonic)) {
|
|
311
|
+
mnemonicData = mnemonic.data;
|
|
312
|
+
}
|
|
313
|
+
if (
|
|
314
|
+
// this block is for backwards compatibility with vaults that were previously stored as buffers, number arrays or plain text strings
|
|
315
|
+
typeof mnemonicData === 'string' ||
|
|
316
|
+
Buffer.isBuffer(mnemonicData) ||
|
|
317
|
+
Array.isArray(mnemonicData)) {
|
|
318
|
+
let mnemonicAsString;
|
|
319
|
+
if (Array.isArray(mnemonicData)) {
|
|
320
|
+
mnemonicAsString = Buffer.from(mnemonicData).toString();
|
|
321
|
+
}
|
|
322
|
+
else if (Buffer.isBuffer(mnemonicData)) {
|
|
323
|
+
mnemonicAsString = mnemonicData.toString();
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
mnemonicAsString = mnemonicData;
|
|
327
|
+
}
|
|
328
|
+
return __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_stringToUint8Array).call(this, mnemonicAsString);
|
|
329
|
+
}
|
|
330
|
+
else if (mnemonicData instanceof Object &&
|
|
331
|
+
!(mnemonicData instanceof Uint8Array)) {
|
|
332
|
+
// when encrypted/decrypted the Uint8Array becomes a js object we need to cast back to a Uint8Array
|
|
333
|
+
return Uint8Array.from(Object.values(mnemonicData));
|
|
334
|
+
}
|
|
335
|
+
(0, utils_1.assert)(mnemonicData instanceof Uint8Array, 'Expected Uint8Array mnemonic');
|
|
336
|
+
return mnemonicData;
|
|
337
|
+
}, _HdKeyring_getPrivateKeyFor = function _HdKeyring_getPrivateKeyFor(address, opts) {
|
|
338
|
+
if (!address) {
|
|
339
|
+
throw new Error('Must specify address.');
|
|
340
|
+
}
|
|
341
|
+
const wallet = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_getWalletForAccount).call(this, address, opts);
|
|
342
|
+
(0, utils_1.assert)(wallet.privateKey, 'Missing private key');
|
|
343
|
+
return wallet.privateKey;
|
|
344
|
+
}, _HdKeyring_getWalletForAccount = function _HdKeyring_getWalletForAccount(account, { withAppKeyOrigin } = {}) {
|
|
345
|
+
const address = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_normalizeAddress).call(this, account);
|
|
346
|
+
const wallet = __classPrivateFieldGet(this, _HdKeyring_wallets, "f").find(({ publicKey }) => {
|
|
347
|
+
return publicKey && __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_addressfromPublicKey).call(this, publicKey) === address;
|
|
348
|
+
});
|
|
349
|
+
if (!wallet) {
|
|
350
|
+
throw new Error('HD Keyring - Unable to find matching address.');
|
|
351
|
+
}
|
|
352
|
+
if (withAppKeyOrigin) {
|
|
353
|
+
const { privateKey } = wallet;
|
|
354
|
+
(0, utils_1.assert)(privateKey, 'Expected private key to be set');
|
|
355
|
+
const appKeyOriginBuffer = Buffer.from(withAppKeyOrigin, 'utf8');
|
|
356
|
+
const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);
|
|
357
|
+
const appKeyPrivateKey = (0, util_1.arrToBufArr)((0, keccak_1.keccak256)(appKeyBuffer));
|
|
358
|
+
const appKeyPublicKey = (0, util_1.privateToPublic)(appKeyPrivateKey);
|
|
359
|
+
return { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };
|
|
360
|
+
}
|
|
361
|
+
return wallet;
|
|
362
|
+
}, _HdKeyring_initFromMnemonic =
|
|
363
|
+
/**
|
|
364
|
+
* Sets appropriate properties for the keyring based on the given
|
|
365
|
+
* BIP39-compliant mnemonic.
|
|
366
|
+
*
|
|
367
|
+
* @param mnemonic - A seed phrase represented
|
|
368
|
+
* as a string, an array of UTF-8 bytes, or a Buffer. Mnemonic input
|
|
369
|
+
* passed as type buffer or array of UTF-8 bytes must be NFKD normalized.
|
|
370
|
+
*/
|
|
371
|
+
async function _HdKeyring_initFromMnemonic(mnemonic) {
|
|
372
|
+
if (this.root) {
|
|
373
|
+
throw new Error('Eth-Hd-Keyring: Secret recovery phrase already provided');
|
|
374
|
+
}
|
|
375
|
+
this.mnemonic = __classPrivateFieldGet(this, _HdKeyring_instances, "m", _HdKeyring_mnemonicToUint8Array).call(this, mnemonic);
|
|
376
|
+
const seed = await (0, key_tree_1.mnemonicToSeed)(this.mnemonic, '', // No passphrase
|
|
377
|
+
__classPrivateFieldGet(this, _HdKeyring_cryptographicFunctions, "f"));
|
|
378
|
+
this.hdWallet = hdkey_1.HDKey.fromMasterSeed(seed);
|
|
379
|
+
this.root = this.hdWallet.derive(this.hdPath);
|
|
380
|
+
}, _HdKeyring_addressfromPublicKey = function _HdKeyring_addressfromPublicKey(publicKey) {
|
|
381
|
+
return (0, utils_1.add0x)((0, util_1.bufferToHex)((0, util_1.publicToAddress)(Buffer.from(publicKey), true)).toLowerCase());
|
|
382
|
+
}, _HdKeyring_normalizeAddress = function _HdKeyring_normalizeAddress(address) {
|
|
383
|
+
const normalized = (0, eth_sig_util_1.normalize)(address);
|
|
384
|
+
(0, utils_1.assert)(normalized, 'Expected address to be set');
|
|
385
|
+
return (0, utils_1.add0x)(normalized);
|
|
386
|
+
};
|
|
387
|
+
HdKeyring.type = type;
|
|
388
|
+
//# sourceMappingURL=hd-keyring.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hd-keyring.cjs","sourceRoot":"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AAI,IAAI,CAAC,IAAI,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CACb,yDAAyD,CAC1D,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,QAAQ,GAAG,uBAAA,IAAI,6DAAsB,MAA1B,IAAI,EAAuB,QAAQ,CAAC,CAAC;IAErD,MAAM,IAAI,GAAG,MAAM,IAAA,yBAAc,EAC/B,IAAI,CAAC,QAAQ,EACb,EAAE,EAAE,gBAAgB;IACpB,uBAAA,IAAI,yCAAwB,CAC7B,CAAC;IACF,IAAI,CAAC,QAAQ,GAAG,aAAK,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IAC3C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAChD,CAAC,6EAQqB,SAAqB;IACzC,OAAO,IAAA,aAAK,EACV,IAAA,kBAAW,EAAC,IAAA,sBAAe,EAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,WAAW,EAAE,CACzE,CAAC;AACJ,CAAC,qEAQiB,OAAe;IAC/B,MAAM,UAAU,GAAG,IAAA,wBAAS,EAAC,OAAO,CAAC,CAAC;IACtC,IAAA,cAAM,EAAC,UAAU,EAAE,4BAA4B,CAAC,CAAC;IACjD,OAAO,IAAA,aAAK,EAAC,UAAU,CAAC,CAAC;AAC3B,CAAC;AAxhBM,cAAI,GAAW,IAAI,AAAf,CAAgB","sourcesContent":["import type { TypedTransaction } from '@ethereumjs/tx';\nimport {\n privateToPublic,\n publicToAddress,\n ecsign,\n arrToBufArr,\n bufferToHex,\n} from '@ethereumjs/util';\nimport {\n concatSig,\n decrypt,\n type EIP7702Authorization,\n type EthEncryptedData,\n getEncryptionPublicKey,\n type MessageTypes,\n normalize,\n personalSign,\n signEIP7702Authorization,\n signTypedData,\n SignTypedDataVersion,\n type TypedDataV1,\n type TypedMessage,\n} from '@metamask/eth-sig-util';\nimport {\n type CryptographicFunctions,\n mnemonicToSeed,\n} from '@metamask/key-tree';\nimport { generateMnemonic } from '@metamask/scure-bip39';\nimport { wordlist } from '@metamask/scure-bip39/dist/wordlists/english';\nimport {\n add0x,\n assert,\n assertIsHexString,\n type Hex,\n remove0x,\n} from '@metamask/utils';\nimport { HDKey } from 'ethereum-cryptography/hdkey';\nimport { keccak256 } from 'ethereum-cryptography/keccak';\nimport { bytesToHex } from 'ethereum-cryptography/utils';\n\n// Options:\nconst hdPathString = `m/44'/60'/0'/0`;\nconst type = 'HD Key Tree';\n\nexport type HDKeyringOptions = {\n cryptographicFunctions?: CryptographicFunctions;\n};\n\n/**\n * The serialized state of an `HDKeyring` instance.\n *\n * @property mnemonic - The mnemonic seed phrase as an array of numbers.\n * @property numberOfAccounts - The number of accounts in the keyring.\n * @property hdPath - The HD path used to derive accounts.\n */\nexport type HDKeyringState = {\n mnemonic: number[] | Uint8Array | Buffer | string;\n numberOfAccounts: number;\n hdPath: string;\n};\n\n/**\n * Options for selecting an account from an `HDKeyring` instance.\n *\n * @property withAppKeyOrigin - The origin of the app requesting the account.\n */\nexport type HDKeyringAccountSelectionOptions = {\n withAppKeyOrigin?: string;\n};\n\ntype SerializedBuffer = ReturnType<Buffer['toJSON']>;\n\n/**\n * Checks if the given value is a valid serialized Buffer compatible with\n * the return type of `Buffer.toJSON`.\n *\n * @param value - The value to check.\n * @returns `true` if the value is a valid serialized buffer, `false` otherwise.\n */\nfunction isSerializedBuffer(value: unknown): value is SerializedBuffer {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'type' in value &&\n value.type === 'Buffer' &&\n 'data' in value &&\n Array.isArray(value.data)\n );\n}\n\nexport class HdKeyring {\n static type: string = type;\n\n type: string = type;\n\n mnemonic?: Uint8Array | null;\n\n root?: HDKey | null;\n\n hdWallet?: HDKey;\n\n hdPath: string = hdPathString;\n\n #wallets: HDKey[] = [];\n\n readonly #cryptographicFunctions?: CryptographicFunctions;\n\n constructor(opts: HDKeyringOptions = {}) {\n // Cryptographic functions to be used by `@metamask/key-tree`. It will use built-in implementations if not provided here.\n this.#cryptographicFunctions = opts.cryptographicFunctions;\n }\n\n /**\n * Initialize the keyring with a random mnemonic.\n *\n * @returns A promise that resolves when the process is complete.\n */\n async generateRandomMnemonic(): Promise<void> {\n await this.#initFromMnemonic(generateMnemonic(wordlist));\n }\n\n /**\n * Return the serialized state of the keyring.\n *\n * @returns The serialized state of the keyring.\n */\n async serialize(): Promise<HDKeyringState> {\n let mnemonic: number[] = [];\n\n if (this.mnemonic) {\n const mnemonicAsString = this.#uint8ArrayToString(this.mnemonic);\n mnemonic = Array.from(new TextEncoder().encode(mnemonicAsString));\n }\n\n return {\n mnemonic,\n numberOfAccounts: this.#wallets.length,\n hdPath: this.hdPath,\n };\n }\n\n /**\n * Initialize the keyring with the given serialized state.\n *\n * @param opts - The serialized state of the keyring.\n * @returns An empty array.\n */\n async deserialize(opts: Partial<HDKeyringState> = {}): Promise<string[]> {\n if (opts.numberOfAccounts && !opts.mnemonic) {\n throw new Error(\n 'Eth-Hd-Keyring: Deserialize method cannot be called with an opts value for numberOfAccounts and no menmonic',\n );\n }\n\n if (this.root) {\n throw new Error(\n 'Eth-Hd-Keyring: Secret recovery phrase already provided',\n );\n }\n this.#wallets = [];\n this.mnemonic = null;\n this.root = null;\n this.hdPath = opts.hdPath ?? hdPathString;\n\n if (opts.mnemonic) {\n await this.#initFromMnemonic(opts.mnemonic);\n }\n\n if (opts.numberOfAccounts) {\n return this.addAccounts(opts.numberOfAccounts);\n }\n\n return [];\n }\n\n /**\n * Add new accounts to the keyring. The accounts will be derived\n * sequentially from the root HD wallet, using increasing indices.\n *\n * @param numberOfAccounts - The number of accounts to add.\n * @returns The addresses of the new accounts.\n */\n async addAccounts(numberOfAccounts = 1): Promise<Hex[]> {\n if (!this.root) {\n throw new Error('Eth-Hd-Keyring: No secret recovery phrase provided');\n }\n\n const oldLen = this.#wallets.length;\n const newWallets: HDKey[] = [];\n for (let i = oldLen; i < numberOfAccounts + oldLen; i++) {\n const wallet = this.root.deriveChild(i);\n newWallets.push(wallet);\n this.#wallets.push(wallet);\n }\n const hexWallets = newWallets.map((wallet) => {\n assert(wallet.publicKey, 'Expected public key to be set');\n return this.#addressfromPublicKey(wallet.publicKey);\n });\n return Promise.resolve(hexWallets);\n }\n\n /**\n * Get the addresses of all accounts in the keyring.\n *\n * @returns The addresses of all accounts in the keyring.\n */\n getAccounts(): Hex[] {\n return this.#wallets.map((wallet) => {\n assert(wallet.publicKey, 'Expected public key to be set');\n return this.#addressfromPublicKey(wallet.publicKey);\n });\n }\n\n /**\n * Get the public address of the account for the given app key origin.\n *\n * @param address - The address of the account.\n * @param origin - The origin of the app requesting the account.\n * @returns The public address of the account.\n */\n async getAppKeyAddress(address: Hex, origin: string): Promise<Hex> {\n if (!origin || typeof origin !== 'string') {\n throw new Error(`'origin' must be a non-empty string`);\n }\n const wallet = this.#getWalletForAccount(address, {\n withAppKeyOrigin: origin,\n });\n assert(wallet.publicKey, 'Expected public key to be set');\n const appKeyAddress = this.#normalizeAddress(\n publicToAddress(wallet.publicKey).toString('hex'),\n );\n return appKeyAddress;\n }\n\n /**\n * Export the private key for a specific account.\n *\n * @param address - The address of the account.\n * @param opts - The options for exporting the account.\n * @param opts.withAppKeyOrigin - An optional string to export the account\n * for a specific app origin.\n * @returns The private key of the account.\n */\n async exportAccount(\n address: Hex,\n opts?: { withAppKeyOrigin: string },\n ): Promise<string> {\n const wallet = opts\n ? this.#getWalletForAccount(address, opts)\n : this.#getWalletForAccount(address);\n const { privateKey } = wallet;\n assert(\n privateKey instanceof Uint8Array,\n 'Expected private key to be of type Uint8Array',\n );\n return bytesToHex(privateKey);\n }\n\n /**\n * Sign a transaction using the private key of the specified account.\n *\n * @param address - The address of the account.\n * @param tx - The transaction to sign.\n * @param opts - The options for signing the transaction.\n * @returns The signed transaction.\n */\n async signTransaction(\n address: Hex,\n tx: TypedTransaction,\n opts = {},\n ): Promise<TypedTransaction> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const signedTx = tx.sign(Buffer.from(privKey));\n // Newer versions of Ethereumjs-tx are immutable and return a new tx object\n return signedTx ?? tx;\n }\n\n /**\n * Sign a message using the private key of the specified account.\n *\n * @param address - The address of the account.\n * @param data - The data to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signMessage(\n address: Hex,\n data: string,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n assertIsHexString(data);\n const message = remove0x(data);\n const privKey = this.#getPrivateKeyFor(address, opts);\n const msgSig = ecsign(Buffer.from(message, 'hex'), Buffer.from(privKey));\n const rawMsgSig = concatSig(\n // WARN: verify this cast to Buffer\n msgSig.v as unknown as Buffer,\n msgSig.r,\n msgSig.s,\n );\n return rawMsgSig;\n }\n\n /**\n * Sign a personal message using the private key of the specified account.\n * This method is compatible with the `personal_sign` RPC method.\n *\n * @param address - The address of the account.\n * @param msgHex - The message to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signPersonalMessage(\n address: Hex,\n msgHex: string,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const privateKey = Buffer.from(privKey);\n return personalSign({ privateKey, data: msgHex });\n }\n\n /**\n * Decrypt an encrypted message using the private key of the specified account.\n * The message must be encrypted using the public key of the account.\n * This method is compatible with the `eth_decryptMessage` RPC method\n *\n * @param withAccount - The address of the account.\n * @param encryptedData - The encrypted data.\n * @returns The decrypted message.\n */\n async decryptMessage(\n withAccount: Hex,\n encryptedData: EthEncryptedData,\n ): Promise<string> {\n const wallet = this.#getWalletForAccount(withAccount);\n const { privateKey: privateKeyAsUint8Array } = wallet;\n assert(privateKeyAsUint8Array, 'Expected private key to be set');\n const privateKeyAsHex = Buffer.from(privateKeyAsUint8Array).toString('hex');\n return decrypt({ privateKey: privateKeyAsHex, encryptedData });\n }\n\n /**\n * Sign a typed message using the private key of the specified account.\n * This method is compatible with the `eth_signTypedData` RPC method.\n *\n * @param withAccount - The address of the account.\n * @param typedData - The typed data to sign.\n * @param opts - The options for signing the message.\n * @returns The signature of the message.\n */\n async signTypedData<Types extends MessageTypes>(\n withAccount: Hex,\n typedData: TypedDataV1 | TypedMessage<Types>,\n opts: HDKeyringAccountSelectionOptions & {\n version: SignTypedDataVersion;\n } = { version: SignTypedDataVersion.V1 },\n ): Promise<string> {\n // Treat invalid versions as \"V1\"\n const version = Object.keys(SignTypedDataVersion).includes(opts.version)\n ? opts.version\n : SignTypedDataVersion.V1;\n\n const privateKey = this.#getPrivateKeyFor(withAccount, opts);\n return signTypedData({\n privateKey: Buffer.from(privateKey),\n data: typedData,\n version,\n });\n }\n\n /**\n * Sign an EIP-7702 authorization using the private key of the specified account.\n * This method is compatible with the EIP-7702 standard for enabling smart contract code for EOAs.\n *\n * @param withAccount - The address of the account.\n * @param authorization - The EIP-7702 authorization to sign.\n * @param opts - The options for selecting the account.\n * @returns The signature of the authorization.\n */\n async signEip7702Authorization(\n withAccount: Hex,\n authorization: EIP7702Authorization,\n opts?: HDKeyringAccountSelectionOptions,\n ): Promise<string> {\n const privateKey = this.#getPrivateKeyFor(withAccount, opts);\n return signEIP7702Authorization({\n privateKey: Buffer.from(privateKey),\n authorization,\n });\n }\n\n /**\n * Remove an account from the keyring.\n *\n * @param account - The address of the account to remove.\n */\n removeAccount(account: Hex): void {\n const address = this.#normalizeAddress(account);\n if (\n !this.#wallets\n .map(\n ({ publicKey }) => publicKey && this.#addressfromPublicKey(publicKey),\n )\n .includes(address)\n ) {\n throw new Error(`Address ${address} not found in this keyring`);\n }\n\n this.#wallets = this.#wallets.filter(\n ({ publicKey }) =>\n publicKey && this.#addressfromPublicKey(publicKey) !== address,\n );\n }\n\n /**\n * Get the public key of the account to be used for encryption.\n *\n * @param withAccount - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns The public key of the account.\n */\n async getEncryptionPublicKey(\n withAccount: Hex,\n opts: HDKeyringAccountSelectionOptions = {},\n ): Promise<string> {\n const privKey = this.#getPrivateKeyFor(withAccount, opts);\n const publicKey = getEncryptionPublicKey(bytesToHex(privKey));\n return publicKey;\n }\n\n /**\n * Convert a Uint8Array mnemonic to a secret recovery phrase,\n * using the english wordlist.\n *\n * @param mnemonic - The Uint8Array mnemonic.\n * @returns The string mnemonic.\n */\n #uint8ArrayToString(mnemonic: Uint8Array): string {\n const recoveredIndices = Array.from(\n new Uint16Array(new Uint8Array(mnemonic).buffer),\n );\n return recoveredIndices.map((i) => wordlist[i]).join(' ');\n }\n\n /**\n * Convert a secret recovery phrase to a Uint8Array mnemonic,\n * using the english wordlist.\n *\n * @param mnemonic - The string mnemonic.\n * @returns The Uint8Array mnemonic.\n */\n #stringToUint8Array(mnemonic: string): Uint8Array {\n const indices = mnemonic.split(' ').map((word) => wordlist.indexOf(word));\n return new Uint8Array(new Uint16Array(indices).buffer);\n }\n\n /**\n * Convert a mnemonic to a Uint8Array mnemonic.\n *\n * @param mnemonic - The mnemonic seed phrase.\n * @returns The Uint8Array mnemonic.\n */\n #mnemonicToUint8Array(\n mnemonic: Buffer | SerializedBuffer | string | Uint8Array | number[],\n ): Uint8Array {\n let mnemonicData: unknown = mnemonic;\n // When using `Buffer.toJSON()`, the Buffer is serialized into an object\n // with the structure `{ type: 'Buffer', data: [...] }`\n if (isSerializedBuffer(mnemonic)) {\n mnemonicData = mnemonic.data;\n }\n\n if (\n // this block is for backwards compatibility with vaults that were previously stored as buffers, number arrays or plain text strings\n typeof mnemonicData === 'string' ||\n Buffer.isBuffer(mnemonicData) ||\n Array.isArray(mnemonicData)\n ) {\n let mnemonicAsString: string;\n if (Array.isArray(mnemonicData)) {\n mnemonicAsString = Buffer.from(mnemonicData).toString();\n } else if (Buffer.isBuffer(mnemonicData)) {\n mnemonicAsString = mnemonicData.toString();\n } else {\n mnemonicAsString = mnemonicData;\n }\n return this.#stringToUint8Array(mnemonicAsString);\n } else if (\n mnemonicData instanceof Object &&\n !(mnemonicData instanceof Uint8Array)\n ) {\n // when encrypted/decrypted the Uint8Array becomes a js object we need to cast back to a Uint8Array\n return Uint8Array.from(Object.values(mnemonicData));\n }\n\n assert(mnemonicData instanceof Uint8Array, 'Expected Uint8Array mnemonic');\n return mnemonicData;\n }\n\n /**\n * Get the private key for the specified account.\n *\n * @param address - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns The private key of the account.\n */\n #getPrivateKeyFor(\n address: Hex,\n opts?: HDKeyringAccountSelectionOptions,\n ): Uint8Array | Buffer {\n if (!address) {\n throw new Error('Must specify address.');\n }\n const wallet = this.#getWalletForAccount(address, opts);\n assert(wallet.privateKey, 'Missing private key');\n return wallet.privateKey;\n }\n\n /**\n * Get the wallet for the specified account.\n *\n * @param account - The address of the account.\n * @returns The wallet for the account as HDKey.\n */\n #getWalletForAccount(account: Hex): HDKey;\n\n /**\n * Get the wallet for the specified account and app origin.\n *\n * @param account - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns A key pair representing the wallet.\n */\n #getWalletForAccount(\n accounts: Hex,\n opts: { withAppKeyOrigin: string },\n ): { privateKey: Buffer; publicKey: Buffer };\n\n /**\n * Get the wallet for the specified account with optional\n * additional options.\n *\n * @param account - The address of the account.\n * @param opts - The options for selecting the account.\n * @returns A key pair representing the wallet.\n */\n #getWalletForAccount(\n account: Hex,\n opts?: HDKeyringAccountSelectionOptions,\n ): HDKey | { privateKey: Buffer; publicKey: Buffer };\n\n #getWalletForAccount(\n account: Hex,\n { withAppKeyOrigin }: HDKeyringAccountSelectionOptions = {},\n ): HDKey | { privateKey: Buffer; publicKey: Buffer } {\n const address = this.#normalizeAddress(account);\n const wallet = this.#wallets.find(({ publicKey }) => {\n return publicKey && this.#addressfromPublicKey(publicKey) === address;\n });\n if (!wallet) {\n throw new Error('HD Keyring - Unable to find matching address.');\n }\n\n if (withAppKeyOrigin) {\n const { privateKey } = wallet;\n assert(privateKey, 'Expected private key to be set');\n const appKeyOriginBuffer = Buffer.from(withAppKeyOrigin, 'utf8');\n const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);\n const appKeyPrivateKey = arrToBufArr(keccak256(appKeyBuffer));\n const appKeyPublicKey = privateToPublic(appKeyPrivateKey);\n return { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };\n }\n\n return wallet;\n }\n\n /**\n * Sets appropriate properties for the keyring based on the given\n * BIP39-compliant mnemonic.\n *\n * @param mnemonic - A seed phrase represented\n * as a string, an array of UTF-8 bytes, or a Buffer. Mnemonic input\n * passed as type buffer or array of UTF-8 bytes must be NFKD normalized.\n */\n async #initFromMnemonic(\n mnemonic: string | number[] | Buffer | Uint8Array,\n ): Promise<void> {\n if (this.root) {\n throw new Error(\n 'Eth-Hd-Keyring: Secret recovery phrase already provided',\n );\n }\n\n this.mnemonic = this.#mnemonicToUint8Array(mnemonic);\n\n const seed = await mnemonicToSeed(\n this.mnemonic,\n '', // No passphrase\n this.#cryptographicFunctions,\n );\n this.hdWallet = HDKey.fromMasterSeed(seed);\n this.root = this.hdWallet.derive(this.hdPath);\n }\n\n /**\n * Get the address of the account from the public key.\n *\n * @param publicKey - The public key of the account.\n * @returns The address of the account.\n */\n #addressfromPublicKey(publicKey: Uint8Array): Hex {\n return add0x(\n bufferToHex(publicToAddress(Buffer.from(publicKey), true)).toLowerCase(),\n );\n }\n\n /**\n * Normalize an address to a lower-cased '0x'-prefixed hex string.\n *\n * @param address - The address to normalize.\n * @returns The normalized address.\n */\n #normalizeAddress(address: string): Hex {\n const normalized = normalize(address);\n assert(normalized, 'Expected address to be set');\n return add0x(normalized);\n }\n}\n"]}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import type { TypedTransaction } from "@ethereumjs/tx";
|
|
2
|
+
import { type EIP7702Authorization, type EthEncryptedData, type MessageTypes, SignTypedDataVersion, type TypedDataV1, type TypedMessage } from "@metamask/eth-sig-util";
|
|
3
|
+
import { type CryptographicFunctions } from "@metamask/key-tree";
|
|
4
|
+
import { type Hex } from "@metamask/utils";
|
|
5
|
+
import { HDKey } from "ethereum-cryptography/hdkey";
|
|
6
|
+
export type HDKeyringOptions = {
|
|
7
|
+
cryptographicFunctions?: CryptographicFunctions;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* The serialized state of an `HDKeyring` instance.
|
|
11
|
+
*
|
|
12
|
+
* @property mnemonic - The mnemonic seed phrase as an array of numbers.
|
|
13
|
+
* @property numberOfAccounts - The number of accounts in the keyring.
|
|
14
|
+
* @property hdPath - The HD path used to derive accounts.
|
|
15
|
+
*/
|
|
16
|
+
export type HDKeyringState = {
|
|
17
|
+
mnemonic: number[] | Uint8Array | Buffer | string;
|
|
18
|
+
numberOfAccounts: number;
|
|
19
|
+
hdPath: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Options for selecting an account from an `HDKeyring` instance.
|
|
23
|
+
*
|
|
24
|
+
* @property withAppKeyOrigin - The origin of the app requesting the account.
|
|
25
|
+
*/
|
|
26
|
+
export type HDKeyringAccountSelectionOptions = {
|
|
27
|
+
withAppKeyOrigin?: string;
|
|
28
|
+
};
|
|
29
|
+
export declare class HdKeyring {
|
|
30
|
+
#private;
|
|
31
|
+
static type: string;
|
|
32
|
+
type: string;
|
|
33
|
+
mnemonic?: Uint8Array | null;
|
|
34
|
+
root?: HDKey | null;
|
|
35
|
+
hdWallet?: HDKey;
|
|
36
|
+
hdPath: string;
|
|
37
|
+
constructor(opts?: HDKeyringOptions);
|
|
38
|
+
/**
|
|
39
|
+
* Initialize the keyring with a random mnemonic.
|
|
40
|
+
*
|
|
41
|
+
* @returns A promise that resolves when the process is complete.
|
|
42
|
+
*/
|
|
43
|
+
generateRandomMnemonic(): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Return the serialized state of the keyring.
|
|
46
|
+
*
|
|
47
|
+
* @returns The serialized state of the keyring.
|
|
48
|
+
*/
|
|
49
|
+
serialize(): Promise<HDKeyringState>;
|
|
50
|
+
/**
|
|
51
|
+
* Initialize the keyring with the given serialized state.
|
|
52
|
+
*
|
|
53
|
+
* @param opts - The serialized state of the keyring.
|
|
54
|
+
* @returns An empty array.
|
|
55
|
+
*/
|
|
56
|
+
deserialize(opts?: Partial<HDKeyringState>): Promise<string[]>;
|
|
57
|
+
/**
|
|
58
|
+
* Add new accounts to the keyring. The accounts will be derived
|
|
59
|
+
* sequentially from the root HD wallet, using increasing indices.
|
|
60
|
+
*
|
|
61
|
+
* @param numberOfAccounts - The number of accounts to add.
|
|
62
|
+
* @returns The addresses of the new accounts.
|
|
63
|
+
*/
|
|
64
|
+
addAccounts(numberOfAccounts?: number): Promise<Hex[]>;
|
|
65
|
+
/**
|
|
66
|
+
* Get the addresses of all accounts in the keyring.
|
|
67
|
+
*
|
|
68
|
+
* @returns The addresses of all accounts in the keyring.
|
|
69
|
+
*/
|
|
70
|
+
getAccounts(): Hex[];
|
|
71
|
+
/**
|
|
72
|
+
* Get the public address of the account for the given app key origin.
|
|
73
|
+
*
|
|
74
|
+
* @param address - The address of the account.
|
|
75
|
+
* @param origin - The origin of the app requesting the account.
|
|
76
|
+
* @returns The public address of the account.
|
|
77
|
+
*/
|
|
78
|
+
getAppKeyAddress(address: Hex, origin: string): Promise<Hex>;
|
|
79
|
+
/**
|
|
80
|
+
* Export the private key for a specific account.
|
|
81
|
+
*
|
|
82
|
+
* @param address - The address of the account.
|
|
83
|
+
* @param opts - The options for exporting the account.
|
|
84
|
+
* @param opts.withAppKeyOrigin - An optional string to export the account
|
|
85
|
+
* for a specific app origin.
|
|
86
|
+
* @returns The private key of the account.
|
|
87
|
+
*/
|
|
88
|
+
exportAccount(address: Hex, opts?: {
|
|
89
|
+
withAppKeyOrigin: string;
|
|
90
|
+
}): Promise<string>;
|
|
91
|
+
/**
|
|
92
|
+
* Sign a transaction using the private key of the specified account.
|
|
93
|
+
*
|
|
94
|
+
* @param address - The address of the account.
|
|
95
|
+
* @param tx - The transaction to sign.
|
|
96
|
+
* @param opts - The options for signing the transaction.
|
|
97
|
+
* @returns The signed transaction.
|
|
98
|
+
*/
|
|
99
|
+
signTransaction(address: Hex, tx: TypedTransaction, opts?: {}): Promise<TypedTransaction>;
|
|
100
|
+
/**
|
|
101
|
+
* Sign a message using the private key of the specified account.
|
|
102
|
+
*
|
|
103
|
+
* @param address - The address of the account.
|
|
104
|
+
* @param data - The data to sign.
|
|
105
|
+
* @param opts - The options for signing the message.
|
|
106
|
+
* @returns The signature of the message.
|
|
107
|
+
*/
|
|
108
|
+
signMessage(address: Hex, data: string, opts?: HDKeyringAccountSelectionOptions): Promise<string>;
|
|
109
|
+
/**
|
|
110
|
+
* Sign a personal message using the private key of the specified account.
|
|
111
|
+
* This method is compatible with the `personal_sign` RPC method.
|
|
112
|
+
*
|
|
113
|
+
* @param address - The address of the account.
|
|
114
|
+
* @param msgHex - The message to sign.
|
|
115
|
+
* @param opts - The options for signing the message.
|
|
116
|
+
* @returns The signature of the message.
|
|
117
|
+
*/
|
|
118
|
+
signPersonalMessage(address: Hex, msgHex: string, opts?: HDKeyringAccountSelectionOptions): Promise<string>;
|
|
119
|
+
/**
|
|
120
|
+
* Decrypt an encrypted message using the private key of the specified account.
|
|
121
|
+
* The message must be encrypted using the public key of the account.
|
|
122
|
+
* This method is compatible with the `eth_decryptMessage` RPC method
|
|
123
|
+
*
|
|
124
|
+
* @param withAccount - The address of the account.
|
|
125
|
+
* @param encryptedData - The encrypted data.
|
|
126
|
+
* @returns The decrypted message.
|
|
127
|
+
*/
|
|
128
|
+
decryptMessage(withAccount: Hex, encryptedData: EthEncryptedData): Promise<string>;
|
|
129
|
+
/**
|
|
130
|
+
* Sign a typed message using the private key of the specified account.
|
|
131
|
+
* This method is compatible with the `eth_signTypedData` RPC method.
|
|
132
|
+
*
|
|
133
|
+
* @param withAccount - The address of the account.
|
|
134
|
+
* @param typedData - The typed data to sign.
|
|
135
|
+
* @param opts - The options for signing the message.
|
|
136
|
+
* @returns The signature of the message.
|
|
137
|
+
*/
|
|
138
|
+
signTypedData<Types extends MessageTypes>(withAccount: Hex, typedData: TypedDataV1 | TypedMessage<Types>, opts?: HDKeyringAccountSelectionOptions & {
|
|
139
|
+
version: SignTypedDataVersion;
|
|
140
|
+
}): Promise<string>;
|
|
141
|
+
/**
|
|
142
|
+
* Sign an EIP-7702 authorization using the private key of the specified account.
|
|
143
|
+
* This method is compatible with the EIP-7702 standard for enabling smart contract code for EOAs.
|
|
144
|
+
*
|
|
145
|
+
* @param withAccount - The address of the account.
|
|
146
|
+
* @param authorization - The EIP-7702 authorization to sign.
|
|
147
|
+
* @param opts - The options for selecting the account.
|
|
148
|
+
* @returns The signature of the authorization.
|
|
149
|
+
*/
|
|
150
|
+
signEip7702Authorization(withAccount: Hex, authorization: EIP7702Authorization, opts?: HDKeyringAccountSelectionOptions): Promise<string>;
|
|
151
|
+
/**
|
|
152
|
+
* Remove an account from the keyring.
|
|
153
|
+
*
|
|
154
|
+
* @param account - The address of the account to remove.
|
|
155
|
+
*/
|
|
156
|
+
removeAccount(account: Hex): void;
|
|
157
|
+
/**
|
|
158
|
+
* Get the public key of the account to be used for encryption.
|
|
159
|
+
*
|
|
160
|
+
* @param withAccount - The address of the account.
|
|
161
|
+
* @param opts - The options for selecting the account.
|
|
162
|
+
* @returns The public key of the account.
|
|
163
|
+
*/
|
|
164
|
+
getEncryptionPublicKey(withAccount: Hex, opts?: HDKeyringAccountSelectionOptions): Promise<string>;
|
|
165
|
+
}
|
|
166
|
+
//# sourceMappingURL=hd-keyring.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hd-keyring.d.cts","sourceRoot":"","sources":["../src/hd-keyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,uBAAuB;AAQvD,OAAO,EAGL,KAAK,oBAAoB,EACzB,KAAK,gBAAgB,EAErB,KAAK,YAAY,EAKjB,oBAAoB,EACpB,KAAK,WAAW,EAChB,KAAK,YAAY,EAClB,+BAA+B;AAChC,OAAO,EACL,KAAK,sBAAsB,EAE5B,2BAA2B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