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