This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.js
312 lines (271 loc) · 9.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const { HDKey } = require('ethereum-cryptography/hdkey');
const { keccak256 } = require('ethereum-cryptography/keccak');
const { bytesToHex } = require('ethereum-cryptography/utils');
const {
privateToPublic,
publicToAddress,
ecsign,
arrToBufArr,
bufferToHex,
} = require('@ethereumjs/util');
const bip39 = require('@metamask/scure-bip39');
const { wordlist } = require('@metamask/scure-bip39/dist/wordlists/english');
const {
concatSig,
decrypt,
getEncryptionPublicKey,
normalize,
personalSign,
signTypedData,
SignTypedDataVersion,
} = require('@metamask/eth-sig-util');
const { assertIsHexString, remove0x } = require('@metamask/utils');
// Options:
const hdPathString = `m/44'/60'/0'/0`;
const type = 'HD Key Tree';
class HdKeyring {
/* PUBLIC METHODS */
constructor(opts = {}) {
this.type = type;
this._wallets = [];
this.deserialize(opts);
}
generateRandomMnemonic() {
this._initFromMnemonic(bip39.generateMnemonic(wordlist));
}
_uint8ArrayToString(mnemonic) {
const recoveredIndices = Array.from(
new Uint16Array(new Uint8Array(mnemonic).buffer),
);
return recoveredIndices.map((i) => wordlist[i]).join(' ');
}
_stringToUint8Array(mnemonic) {
const indices = mnemonic.split(' ').map((word) => wordlist.indexOf(word));
return new Uint8Array(new Uint16Array(indices).buffer);
}
_mnemonicToUint8Array(mnemonic) {
let mnemonicData = mnemonic;
// when encrypted/decrypted, buffers get cast into js object with a property type set to buffer
if (mnemonic && mnemonic.type && mnemonic.type === 'Buffer') {
mnemonicData = mnemonic.data;
}
if (
// this block is for backwards compatibility with vaults that were previously stored as buffers, number arrays or plain text strings
typeof mnemonicData === 'string' ||
Buffer.isBuffer(mnemonicData) ||
Array.isArray(mnemonicData)
) {
let mnemonicAsString = mnemonicData;
if (Array.isArray(mnemonicData)) {
mnemonicAsString = Buffer.from(mnemonicData).toString();
} else if (Buffer.isBuffer(mnemonicData)) {
mnemonicAsString = mnemonicData.toString();
}
return this._stringToUint8Array(mnemonicAsString);
} else if (
mnemonicData instanceof Object &&
!(mnemonicData instanceof Uint8Array)
) {
// when encrypted/decrypted the Uint8Array becomes a js object we need to cast back to a Uint8Array
return Uint8Array.from(Object.values(mnemonicData));
}
return mnemonicData;
}
serialize() {
const mnemonicAsString = this._uint8ArrayToString(this.mnemonic);
const uint8ArrayMnemonic = new TextEncoder('utf-8').encode(
mnemonicAsString,
);
return Promise.resolve({
mnemonic: Array.from(uint8ArrayMnemonic),
numberOfAccounts: this._wallets.length,
hdPath: this.hdPath,
});
}
deserialize(opts = {}) {
if (opts.numberOfAccounts && !opts.mnemonic) {
throw new Error(
'Eth-Hd-Keyring: Deserialize method cannot be called with an opts value for numberOfAccounts and no menmonic',
);
}
if (this.root) {
throw new Error(
'Eth-Hd-Keyring: Secret recovery phrase already provided',
);
}
this.opts = opts;
this._wallets = [];
this.mnemonic = null;
this.root = null;
this.hdPath = opts.hdPath || hdPathString;
if (opts.mnemonic) {
this._initFromMnemonic(opts.mnemonic);
}
if (opts.numberOfAccounts) {
return this.addAccounts(opts.numberOfAccounts);
}
return Promise.resolve([]);
}
addAccounts(numberOfAccounts = 1) {
if (!this.root) {
throw new Error('Eth-Hd-Keyring: No secret recovery phrase provided');
}
const oldLen = this._wallets.length;
const newWallets = [];
for (let i = oldLen; i < numberOfAccounts + oldLen; i++) {
const wallet = this.root.deriveChild(i);
newWallets.push(wallet);
this._wallets.push(wallet);
}
const hexWallets = newWallets.map((w) => {
return this._addressfromPublicKey(w.publicKey);
});
return Promise.resolve(hexWallets);
}
getAccounts() {
return this._wallets.map((w) => this._addressfromPublicKey(w.publicKey));
}
/* BASE KEYRING METHODS */
// returns an address specific to an app
async getAppKeyAddress(address, origin) {
if (!origin || typeof origin !== 'string') {
throw new Error(`'origin' must be a non-empty string`);
}
const wallet = this._getWalletForAccount(address, {
withAppKeyOrigin: origin,
});
const appKeyAddress = normalize(
publicToAddress(wallet.publicKey).toString('hex'),
);
return appKeyAddress;
}
// exportAccount should return a hex-encoded private key:
async exportAccount(address, opts = {}) {
const wallet = this._getWalletForAccount(address, opts);
return bytesToHex(wallet.privateKey);
}
// tx is an instance of the ethereumjs-transaction class.
async signTransaction(address, tx, opts = {}) {
const privKey = this._getPrivateKeyFor(address, opts);
const signedTx = tx.sign(privKey);
// Newer versions of Ethereumjs-tx are immutable and return a new tx object
return signedTx === undefined ? tx : signedTx;
}
// For eth_sign, we need to sign arbitrary data:
async signMessage(address, data, opts = {}) {
assertIsHexString(data);
const message = remove0x(data);
const privKey = this._getPrivateKeyFor(address, opts);
const msgSig = ecsign(Buffer.from(message, 'hex'), privKey);
const rawMsgSig = concatSig(msgSig.v, msgSig.r, msgSig.s);
return rawMsgSig;
}
// For personal_sign, we need to prefix the message:
async signPersonalMessage(address, msgHex, opts = {}) {
const privKey = this._getPrivateKeyFor(address, opts);
const privateKey = Buffer.from(privKey, 'hex');
const sig = personalSign({ privateKey, data: msgHex });
return sig;
}
// For eth_decryptMessage:
async decryptMessage(withAccount, encryptedData) {
const wallet = this._getWalletForAccount(withAccount);
const { privateKey: privateKeyAsUint8Array } = wallet;
const privateKeyAsHex = Buffer.from(privateKeyAsUint8Array).toString('hex');
const sig = decrypt({ privateKey: privateKeyAsHex, encryptedData });
return sig;
}
// personal_signTypedData, signs data along with the schema
async signTypedData(
withAccount,
typedData,
opts = { version: SignTypedDataVersion.V1 },
) {
// Treat invalid versions as "V1"
const version = Object.keys(SignTypedDataVersion).includes(opts.version)
? opts.version
: SignTypedDataVersion.V1;
const privateKey = this._getPrivateKeyFor(withAccount, opts);
return signTypedData({ privateKey, data: typedData, version });
}
removeAccount(account) {
const address = normalize(account);
if (
!this._wallets
.map(({ publicKey }) => this._addressfromPublicKey(publicKey))
.includes(address)
) {
throw new Error(`Address ${address} not found in this keyring`);
}
this._wallets = this._wallets.filter(
({ publicKey }) => this._addressfromPublicKey(publicKey) !== address,
);
}
// get public key for nacl
async getEncryptionPublicKey(withAccount, opts = {}) {
const privKey = this._getPrivateKeyFor(withAccount, opts);
const publicKey = getEncryptionPublicKey(privKey);
return publicKey;
}
_getPrivateKeyFor(address, opts = {}) {
if (!address) {
throw new Error('Must specify address.');
}
const wallet = this._getWalletForAccount(address, opts);
return wallet.privateKey;
}
_getWalletForAccount(account, opts = {}) {
const address = normalize(account);
let wallet = this._wallets.find(({ publicKey }) => {
return this._addressfromPublicKey(publicKey) === address;
});
if (!wallet) {
throw new Error('HD Keyring - Unable to find matching address.');
}
if (opts.withAppKeyOrigin) {
const { privateKey } = wallet;
const appKeyOriginBuffer = Buffer.from(opts.withAppKeyOrigin, 'utf8');
const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);
const appKeyPrivateKey = arrToBufArr(keccak256(appKeyBuffer, 256));
const appKeyPublicKey = privateToPublic(appKeyPrivateKey);
wallet = { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };
}
return wallet;
}
/* PRIVATE / UTILITY METHODS */
/**
* Sets appropriate properties for the keyring based on the given
* BIP39-compliant mnemonic.
*
* @param {string|Array<number>|Buffer} mnemonic - A seed phrase represented
* as a string, an array of UTF-8 bytes, or a Buffer. Mnemonic input
* passed as type buffer or array of UTF-8 bytes must be NFKD normalized.
*/
_initFromMnemonic(mnemonic) {
if (this.root) {
throw new Error(
'Eth-Hd-Keyring: Secret recovery phrase already provided',
);
}
this.mnemonic = this._mnemonicToUint8Array(mnemonic);
// validate before initializing
const isValid = bip39.validateMnemonic(this.mnemonic, wordlist);
if (!isValid) {
throw new Error(
'Eth-Hd-Keyring: Invalid secret recovery phrase provided',
);
}
// eslint-disable-next-line node/no-sync
const seed = bip39.mnemonicToSeedSync(this.mnemonic, wordlist);
this.hdWallet = HDKey.fromMasterSeed(seed);
this.root = this.hdWallet.derive(this.hdPath);
}
// small helper function to convert publicKey in Uint8Array form to a publicAddress as a hex
_addressfromPublicKey(publicKey) {
return bufferToHex(
publicToAddress(Buffer.from(publicKey), true),
).toLowerCase();
}
}
HdKeyring.type = type;
module.exports = HdKeyring;