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