@metamask/eth-hd-keyring 6.0.1 → 6.0.2
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/.eslintrc.js +6 -25
- package/.github/CODEOWNERS +4 -0
- package/.github/workflows/build-lint-test.yml +73 -0
- package/.github/workflows/create-release-pr.yml +41 -0
- package/.github/workflows/main.yml +70 -0
- package/.github/workflows/publish-release.yml +50 -0
- package/.yarn/releases/yarn-3.3.0.cjs +807 -0
- package/.yarnrc.yml +15 -0
- package/CHANGELOG.md +7 -5
- package/index.js +311 -0
- package/jest.config.js +4 -7
- package/package.json +7 -17
- package/{src/HDKeyring.test.ts → test/index.js} +111 -147
- package/src/HDKeyring.ts +0 -459
- package/src/errors.ts +0 -16
- package/src/index.ts +0 -1
- package/tsconfig.build.json +0 -13
- package/tsconfig.json +0 -16
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
const {
|
|
2
2
|
normalize,
|
|
3
3
|
personalSign,
|
|
4
4
|
recoverPersonalSignature,
|
|
@@ -6,27 +6,24 @@ import {
|
|
|
6
6
|
signTypedData,
|
|
7
7
|
SignTypedDataVersion,
|
|
8
8
|
encrypt,
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
import {
|
|
9
|
+
} = require('@metamask/eth-sig-util');
|
|
10
|
+
const { wordlist } = require('@metamask/scure-bip39/dist/wordlists/english');
|
|
11
|
+
const oldMMForkBIP39 = require('@metamask/bip39');
|
|
12
|
+
const {
|
|
14
13
|
isValidAddress,
|
|
15
14
|
bufferToHex,
|
|
16
15
|
toBuffer,
|
|
17
16
|
ecrecover,
|
|
18
17
|
pubToAddress,
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
import OldHDKeyring from '@metamask/eth-hd-keyring';
|
|
29
|
-
import { HDKeyring } from './HDKeyring';
|
|
18
|
+
} = require('@ethereumjs/util');
|
|
19
|
+
const {
|
|
20
|
+
TransactionFactory,
|
|
21
|
+
Transaction: EthereumTx,
|
|
22
|
+
} = require('@ethereumjs/tx');
|
|
23
|
+
|
|
24
|
+
const OldHdKeyring = require('@metamask/eth-hd-keyring');
|
|
25
|
+
const { keccak256 } = require('ethereum-cryptography/keccak');
|
|
26
|
+
const HdKeyring = require('..');
|
|
30
27
|
|
|
31
28
|
// Sample account:
|
|
32
29
|
const privKeyHex =
|
|
@@ -42,23 +39,25 @@ const notKeyringAddress = '0xbD20F6F5F1616947a39E11926E78ec94817B3931';
|
|
|
42
39
|
describe('hd-keyring', () => {
|
|
43
40
|
describe('compare old bip39 implementation with new', () => {
|
|
44
41
|
it('should derive the same accounts from the same mnemonics', async () => {
|
|
45
|
-
const mnemonics
|
|
42
|
+
const mnemonics = [];
|
|
46
43
|
for (let i = 0; i < 99; i++) {
|
|
47
|
-
mnemonics.push(
|
|
44
|
+
mnemonics.push(oldMMForkBIP39.generateMnemonic());
|
|
48
45
|
}
|
|
49
46
|
|
|
50
47
|
await Promise.all(
|
|
51
48
|
mnemonics.map(async (mnemonic) => {
|
|
52
|
-
const newHDKeyring = new
|
|
53
|
-
const oldHDKeyring = new
|
|
49
|
+
const newHDKeyring = new HdKeyring({ mnemonic, numberOfAccounts: 3 });
|
|
50
|
+
const oldHDKeyring = new OldHdKeyring({
|
|
54
51
|
mnemonic,
|
|
55
52
|
numberOfAccounts: 3,
|
|
56
53
|
});
|
|
57
54
|
const newAccounts = await newHDKeyring.getAccounts();
|
|
58
55
|
const oldAccounts = await oldHDKeyring.getAccounts();
|
|
59
|
-
expect(newAccounts[0]).toStrictEqual(oldAccounts[0]);
|
|
60
|
-
|
|
61
|
-
expect(newAccounts[
|
|
56
|
+
await expect(newAccounts[0]).toStrictEqual(oldAccounts[0]);
|
|
57
|
+
|
|
58
|
+
await expect(newAccounts[1]).toStrictEqual(oldAccounts[1]);
|
|
59
|
+
|
|
60
|
+
await expect(newAccounts[2]).toStrictEqual(oldAccounts[2]);
|
|
62
61
|
}),
|
|
63
62
|
);
|
|
64
63
|
});
|
|
@@ -66,7 +65,7 @@ describe('hd-keyring', () => {
|
|
|
66
65
|
|
|
67
66
|
describe('constructor', () => {
|
|
68
67
|
it('constructs with a typeof string mnemonic', async () => {
|
|
69
|
-
const keyring = new
|
|
68
|
+
const keyring = new HdKeyring({
|
|
70
69
|
mnemonic: sampleMnemonic,
|
|
71
70
|
numberOfAccounts: 2,
|
|
72
71
|
});
|
|
@@ -77,7 +76,7 @@ describe('hd-keyring', () => {
|
|
|
77
76
|
});
|
|
78
77
|
|
|
79
78
|
it('constructs with a typeof buffer mnemonic', async () => {
|
|
80
|
-
const keyring = new
|
|
79
|
+
const keyring = new HdKeyring({
|
|
81
80
|
mnemonic: Buffer.from(sampleMnemonic, 'utf8'),
|
|
82
81
|
numberOfAccounts: 2,
|
|
83
82
|
});
|
|
@@ -94,7 +93,7 @@ describe('hd-keyring', () => {
|
|
|
94
93
|
const uInt8ArrayOfMnemonic = new Uint8Array(
|
|
95
94
|
new Uint16Array(indices).buffer,
|
|
96
95
|
);
|
|
97
|
-
const keyring = new
|
|
96
|
+
const keyring = new HdKeyring({
|
|
98
97
|
mnemonic: uInt8ArrayOfMnemonic,
|
|
99
98
|
numberOfAccounts: 2,
|
|
100
99
|
});
|
|
@@ -104,35 +103,23 @@ describe('hd-keyring', () => {
|
|
|
104
103
|
expect(accounts[1]).toStrictEqual(secondAcct);
|
|
105
104
|
});
|
|
106
105
|
|
|
107
|
-
it('constructs with jscasted buffer', async () => {
|
|
108
|
-
const jscastedBuffer = Buffer.from(sampleMnemonic).toJSON();
|
|
109
|
-
const keyring = new HDKeyring({
|
|
110
|
-
mnemonic: jscastedBuffer,
|
|
111
|
-
numberOfAccounts: 2,
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
const accounts = await keyring.getAccounts();
|
|
115
|
-
expect(accounts[0]).toStrictEqual(firstAcct);
|
|
116
|
-
expect(accounts[1]).toStrictEqual(secondAcct);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
106
|
it('throws on invalid mnemonic', () => {
|
|
120
107
|
expect(
|
|
121
108
|
() =>
|
|
122
|
-
new
|
|
109
|
+
new HdKeyring({
|
|
123
110
|
mnemonic: 'abc xyz',
|
|
124
111
|
numberOfAccounts: 2,
|
|
125
112
|
}),
|
|
126
113
|
).toThrow('Eth-Hd-Keyring: Invalid secret recovery phrase provided');
|
|
127
114
|
});
|
|
128
115
|
|
|
129
|
-
it('throws when numberOfAccounts is passed with no mnemonic',
|
|
130
|
-
expect(
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
116
|
+
it('throws when numberOfAccounts is passed with no mnemonic', () => {
|
|
117
|
+
expect(
|
|
118
|
+
() =>
|
|
119
|
+
new HdKeyring({
|
|
120
|
+
numberOfAccounts: 2,
|
|
121
|
+
}),
|
|
122
|
+
).toThrow(
|
|
136
123
|
'Eth-Hd-Keyring: Deserialize method cannot be called with an opts value for numberOfAccounts and no menmonic',
|
|
137
124
|
);
|
|
138
125
|
});
|
|
@@ -142,7 +129,7 @@ describe('hd-keyring', () => {
|
|
|
142
129
|
const alreadyProvidedError =
|
|
143
130
|
'Eth-Hd-Keyring: Secret recovery phrase already provided';
|
|
144
131
|
it('double generateRandomMnemonic', () => {
|
|
145
|
-
const keyring = new
|
|
132
|
+
const keyring = new HdKeyring();
|
|
146
133
|
keyring.generateRandomMnemonic();
|
|
147
134
|
expect(() => {
|
|
148
135
|
keyring.generateRandomMnemonic();
|
|
@@ -150,7 +137,7 @@ describe('hd-keyring', () => {
|
|
|
150
137
|
});
|
|
151
138
|
|
|
152
139
|
it('constructor + generateRandomMnemonic', () => {
|
|
153
|
-
const keyring = new
|
|
140
|
+
const keyring = new HdKeyring({
|
|
154
141
|
mnemonic: sampleMnemonic,
|
|
155
142
|
numberOfAccounts: 2,
|
|
156
143
|
});
|
|
@@ -161,13 +148,12 @@ describe('hd-keyring', () => {
|
|
|
161
148
|
});
|
|
162
149
|
|
|
163
150
|
it('constructor + deserialize', () => {
|
|
164
|
-
const keyring = new
|
|
151
|
+
const keyring = new HdKeyring({
|
|
165
152
|
mnemonic: sampleMnemonic,
|
|
166
153
|
numberOfAccounts: 2,
|
|
167
154
|
});
|
|
168
155
|
|
|
169
156
|
expect(() => {
|
|
170
|
-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
171
157
|
keyring.deserialize({
|
|
172
158
|
mnemonic: sampleMnemonic,
|
|
173
159
|
numberOfAccounts: 1,
|
|
@@ -178,28 +164,28 @@ describe('hd-keyring', () => {
|
|
|
178
164
|
|
|
179
165
|
describe('Keyring.type', () => {
|
|
180
166
|
it('is a class property that returns the type string.', () => {
|
|
181
|
-
const { type } =
|
|
167
|
+
const { type } = HdKeyring;
|
|
182
168
|
expect(typeof type).toBe('string');
|
|
183
169
|
});
|
|
184
170
|
});
|
|
185
171
|
|
|
186
172
|
describe('#type', () => {
|
|
187
173
|
it('returns the correct value', () => {
|
|
188
|
-
const keyring = new
|
|
174
|
+
const keyring = new HdKeyring();
|
|
189
175
|
|
|
190
176
|
const { type } = keyring;
|
|
191
|
-
const correct =
|
|
177
|
+
const correct = HdKeyring.type;
|
|
192
178
|
expect(type).toStrictEqual(correct);
|
|
193
179
|
});
|
|
194
180
|
});
|
|
195
181
|
|
|
196
182
|
describe('#serialize mnemonic.', () => {
|
|
197
183
|
it('serializes the mnemonic in the same format as previous version (an array of utf8 encoded bytes)', async () => {
|
|
198
|
-
const keyring = new
|
|
184
|
+
const keyring = new HdKeyring({
|
|
199
185
|
mnemonic: sampleMnemonic,
|
|
200
186
|
});
|
|
201
187
|
// uses previous version of eth-hd-keyring to ensure backwards compatibility
|
|
202
|
-
const oldHDKeyring = new
|
|
188
|
+
const oldHDKeyring = new OldHdKeyring({ mnemonic: sampleMnemonic });
|
|
203
189
|
const { mnemonic: oldKeyringSerializedMnemonic } =
|
|
204
190
|
await oldHDKeyring.serialize();
|
|
205
191
|
|
|
@@ -208,7 +194,7 @@ describe('hd-keyring', () => {
|
|
|
208
194
|
});
|
|
209
195
|
|
|
210
196
|
it('serializes mnemonic passed in as a string to an array of utf8 encoded bytes', async () => {
|
|
211
|
-
const keyring = new
|
|
197
|
+
const keyring = new HdKeyring({
|
|
212
198
|
mnemonic: sampleMnemonic,
|
|
213
199
|
});
|
|
214
200
|
const output = await keyring.serialize();
|
|
@@ -218,9 +204,9 @@ describe('hd-keyring', () => {
|
|
|
218
204
|
});
|
|
219
205
|
|
|
220
206
|
it('serializes mnemonic passed in as a an array of utf8 encoded bytes in the same format', async () => {
|
|
221
|
-
const uint8Array = new TextEncoder().encode(sampleMnemonic);
|
|
207
|
+
const uint8Array = new TextEncoder('utf-8').encode(sampleMnemonic);
|
|
222
208
|
const mnemonicAsArrayOfUtf8EncodedBytes = Array.from(uint8Array);
|
|
223
|
-
const keyring = new
|
|
209
|
+
const keyring = new HdKeyring({
|
|
224
210
|
mnemonic: mnemonicAsArrayOfUtf8EncodedBytes,
|
|
225
211
|
});
|
|
226
212
|
|
|
@@ -229,18 +215,11 @@ describe('hd-keyring', () => {
|
|
|
229
215
|
const mnemonicAsString = Buffer.from(output.mnemonic).toString();
|
|
230
216
|
expect(mnemonicAsString).toStrictEqual(sampleMnemonic);
|
|
231
217
|
});
|
|
232
|
-
|
|
233
|
-
it('throws if mnemnoic is not set', async () => {
|
|
234
|
-
const keyring = new HDKeyring({});
|
|
235
|
-
await expect(keyring.serialize()).rejects.toThrow(
|
|
236
|
-
'Eth-Hd-Keyring: Missing mnemonic when serializing',
|
|
237
|
-
);
|
|
238
|
-
});
|
|
239
218
|
});
|
|
240
219
|
|
|
241
220
|
describe('#deserialize a private key', () => {
|
|
242
221
|
it('serializes what it deserializes', async () => {
|
|
243
|
-
const keyring = new
|
|
222
|
+
const keyring = new HdKeyring();
|
|
244
223
|
await keyring.deserialize({
|
|
245
224
|
mnemonic: sampleMnemonic,
|
|
246
225
|
numberOfAccounts: 1,
|
|
@@ -263,7 +242,7 @@ describe('hd-keyring', () => {
|
|
|
263
242
|
describe('#addAccounts', () => {
|
|
264
243
|
describe('with no arguments', () => {
|
|
265
244
|
it('creates a single wallet', async () => {
|
|
266
|
-
const keyring = new
|
|
245
|
+
const keyring = new HdKeyring();
|
|
267
246
|
keyring.generateRandomMnemonic();
|
|
268
247
|
await keyring.addAccounts();
|
|
269
248
|
const accounts = await keyring.getAccounts();
|
|
@@ -271,8 +250,8 @@ describe('hd-keyring', () => {
|
|
|
271
250
|
});
|
|
272
251
|
|
|
273
252
|
it('throws an error when no SRP has been generated yet', async () => {
|
|
274
|
-
const keyring = new
|
|
275
|
-
|
|
253
|
+
const keyring = new HdKeyring();
|
|
254
|
+
expect(() => keyring.addAccounts()).toThrow(
|
|
276
255
|
'Eth-Hd-Keyring: No secret recovery phrase provided',
|
|
277
256
|
);
|
|
278
257
|
});
|
|
@@ -280,7 +259,7 @@ describe('hd-keyring', () => {
|
|
|
280
259
|
|
|
281
260
|
describe('with a numeric argument', () => {
|
|
282
261
|
it('creates that number of wallets', async () => {
|
|
283
|
-
const keyring = new
|
|
262
|
+
const keyring = new HdKeyring();
|
|
284
263
|
keyring.generateRandomMnemonic();
|
|
285
264
|
await keyring.addAccounts(3);
|
|
286
265
|
const accounts = await keyring.getAccounts();
|
|
@@ -291,7 +270,7 @@ describe('hd-keyring', () => {
|
|
|
291
270
|
|
|
292
271
|
describe('#signPersonalMessage', () => {
|
|
293
272
|
it('returns the expected value', async () => {
|
|
294
|
-
const keyring = new
|
|
273
|
+
const keyring = new HdKeyring();
|
|
295
274
|
|
|
296
275
|
const address = firstAcct;
|
|
297
276
|
const message = '0x68656c6c6f20776f726c64';
|
|
@@ -314,7 +293,7 @@ describe('hd-keyring', () => {
|
|
|
314
293
|
|
|
315
294
|
describe('#signTypedData', () => {
|
|
316
295
|
it('can recover a basic signature', async () => {
|
|
317
|
-
const keyring = new
|
|
296
|
+
const keyring = new HdKeyring();
|
|
318
297
|
Buffer.from(privKeyHex, 'hex');
|
|
319
298
|
const typedData = [
|
|
320
299
|
{
|
|
@@ -325,9 +304,8 @@ describe('hd-keyring', () => {
|
|
|
325
304
|
];
|
|
326
305
|
keyring.generateRandomMnemonic();
|
|
327
306
|
await keyring.addAccounts(1);
|
|
328
|
-
const
|
|
329
|
-
|
|
330
|
-
const address = add0x(rawAddress);
|
|
307
|
+
const addresses = await keyring.getAccounts();
|
|
308
|
+
const address = addresses[0];
|
|
331
309
|
const signature = await keyring.signTypedData(address, typedData);
|
|
332
310
|
const restored = recoverTypedSignature({
|
|
333
311
|
data: typedData,
|
|
@@ -348,12 +326,11 @@ describe('hd-keyring', () => {
|
|
|
348
326
|
];
|
|
349
327
|
|
|
350
328
|
it('signs in a compliant and recoverable way', async () => {
|
|
351
|
-
const keyring = new
|
|
329
|
+
const keyring = new HdKeyring();
|
|
352
330
|
keyring.generateRandomMnemonic();
|
|
353
331
|
await keyring.addAccounts(1);
|
|
354
|
-
const
|
|
355
|
-
|
|
356
|
-
const address = add0x(rawAddress);
|
|
332
|
+
const addresses = await keyring.getAccounts();
|
|
333
|
+
const address = addresses[0];
|
|
357
334
|
const signature = await keyring.signTypedData(address, typedData, {
|
|
358
335
|
version: SignTypedDataVersion.V1,
|
|
359
336
|
});
|
|
@@ -368,13 +345,13 @@ describe('hd-keyring', () => {
|
|
|
368
345
|
|
|
369
346
|
describe('#signTypedData_v3', () => {
|
|
370
347
|
it('signs in a compliant and recoverable way', async () => {
|
|
371
|
-
const keyring = new
|
|
348
|
+
const keyring = new HdKeyring();
|
|
372
349
|
const typedData = {
|
|
373
350
|
types: {
|
|
374
351
|
EIP712Domain: [],
|
|
375
352
|
},
|
|
376
353
|
domain: {},
|
|
377
|
-
primaryType: 'EIP712Domain'
|
|
354
|
+
primaryType: 'EIP712Domain',
|
|
378
355
|
message: {},
|
|
379
356
|
};
|
|
380
357
|
|
|
@@ -382,9 +359,8 @@ describe('hd-keyring', () => {
|
|
|
382
359
|
mnemonic: sampleMnemonic,
|
|
383
360
|
numberOfAccounts: 1,
|
|
384
361
|
});
|
|
385
|
-
const
|
|
386
|
-
|
|
387
|
-
const address = add0x(rawAddress);
|
|
362
|
+
const addresses = await keyring.getAccounts();
|
|
363
|
+
const address = addresses[0];
|
|
388
364
|
const signature = await keyring.signTypedData(address, typedData, {
|
|
389
365
|
version: SignTypedDataVersion.V3,
|
|
390
366
|
});
|
|
@@ -399,7 +375,7 @@ describe('hd-keyring', () => {
|
|
|
399
375
|
|
|
400
376
|
describe('#signTypedData_v3 signature verification', () => {
|
|
401
377
|
it('signs in a recoverable way.', async () => {
|
|
402
|
-
const keyring = new
|
|
378
|
+
const keyring = new HdKeyring();
|
|
403
379
|
const typedData = {
|
|
404
380
|
types: {
|
|
405
381
|
EIP712Domain: [
|
|
@@ -418,7 +394,7 @@ describe('hd-keyring', () => {
|
|
|
418
394
|
{ name: 'contents', type: 'string' },
|
|
419
395
|
],
|
|
420
396
|
},
|
|
421
|
-
primaryType: 'Mail'
|
|
397
|
+
primaryType: 'Mail',
|
|
422
398
|
domain: {
|
|
423
399
|
name: 'Ether Mail',
|
|
424
400
|
version: '1',
|
|
@@ -440,9 +416,8 @@ describe('hd-keyring', () => {
|
|
|
440
416
|
|
|
441
417
|
keyring.generateRandomMnemonic();
|
|
442
418
|
await keyring.addAccounts(1);
|
|
443
|
-
const
|
|
444
|
-
|
|
445
|
-
const address = add0x(rawAddress);
|
|
419
|
+
const addresses = await keyring.getAccounts();
|
|
420
|
+
const address = addresses[0];
|
|
446
421
|
const signature = await keyring.signTypedData(address, typedData, {
|
|
447
422
|
version: SignTypedDataVersion.V3,
|
|
448
423
|
});
|
|
@@ -457,9 +432,9 @@ describe('hd-keyring', () => {
|
|
|
457
432
|
|
|
458
433
|
describe('custom hd paths', () => {
|
|
459
434
|
it('can deserialize with an hdPath param and generate the same accounts.', async () => {
|
|
460
|
-
const keyring = new
|
|
435
|
+
const keyring = new HdKeyring();
|
|
461
436
|
const hdPathString = `m/44'/60'/0'/0`;
|
|
462
|
-
|
|
437
|
+
keyring.deserialize({
|
|
463
438
|
mnemonic: sampleMnemonic,
|
|
464
439
|
numberOfAccounts: 1,
|
|
465
440
|
hdPath: hdPathString,
|
|
@@ -471,9 +446,9 @@ describe('hd-keyring', () => {
|
|
|
471
446
|
});
|
|
472
447
|
|
|
473
448
|
it('can deserialize with an hdPath param and generate different accounts.', async () => {
|
|
474
|
-
const keyring = new
|
|
449
|
+
const keyring = new HdKeyring();
|
|
475
450
|
const hdPathString = `m/44'/60'/0'/1`;
|
|
476
|
-
|
|
451
|
+
keyring.deserialize({
|
|
477
452
|
mnemonic: sampleMnemonic,
|
|
478
453
|
numberOfAccounts: 1,
|
|
479
454
|
hdPath: hdPathString,
|
|
@@ -493,14 +468,14 @@ describe('hd-keyring', () => {
|
|
|
493
468
|
|
|
494
469
|
for (let i = 0; i < 1e3; i++) {
|
|
495
470
|
|
|
496
|
-
const keyring = new
|
|
471
|
+
const keyring = new HdKeyring({
|
|
497
472
|
numberOfAccounts: 1,
|
|
498
473
|
})
|
|
499
474
|
const originalAccounts = await keyring.getAccounts()
|
|
500
475
|
const serialized = await keyring.serialize()
|
|
501
476
|
const mnemonic = serialized.mnemonic
|
|
502
477
|
|
|
503
|
-
const keyring = new
|
|
478
|
+
const keyring = new HdKeyring({
|
|
504
479
|
numberOfAccounts: 1,
|
|
505
480
|
mnemonic,
|
|
506
481
|
})
|
|
@@ -520,7 +495,7 @@ describe('hd-keyring', () => {
|
|
|
520
495
|
|
|
521
496
|
describe('signing methods withAppKeyOrigin option', () => {
|
|
522
497
|
it('should signPersonalMessage with the expected key when passed a withAppKeyOrigin', async () => {
|
|
523
|
-
const keyring = new
|
|
498
|
+
const keyring = new HdKeyring();
|
|
524
499
|
const address = firstAcct;
|
|
525
500
|
const message = '0x68656c6c6f20776f726c64';
|
|
526
501
|
|
|
@@ -542,14 +517,14 @@ describe('hd-keyring', () => {
|
|
|
542
517
|
});
|
|
543
518
|
|
|
544
519
|
it('should signTypedData with the expected key when passed a withAppKeyOrigin', async () => {
|
|
545
|
-
const keyring = new
|
|
520
|
+
const keyring = new HdKeyring();
|
|
546
521
|
const address = firstAcct;
|
|
547
522
|
const typedData = {
|
|
548
523
|
types: {
|
|
549
524
|
EIP712Domain: [],
|
|
550
525
|
},
|
|
551
526
|
domain: {},
|
|
552
|
-
primaryType: 'EIP712Domain'
|
|
527
|
+
primaryType: 'EIP712Domain',
|
|
553
528
|
message: {},
|
|
554
529
|
};
|
|
555
530
|
|
|
@@ -587,7 +562,7 @@ describe('hd-keyring', () => {
|
|
|
587
562
|
'0xb21867b2221db0172e970b7370825b71c57823ff8714168ce9748f32f450e2c43d0fe396eb5b5f59284b7fd108c8cf61a6180a6756bdd3d4b7b9ccc4ac6d51611b';
|
|
588
563
|
|
|
589
564
|
it('passes the dennis test', async function () {
|
|
590
|
-
const keyring = new
|
|
565
|
+
const keyring = new HdKeyring();
|
|
591
566
|
await keyring.deserialize({
|
|
592
567
|
mnemonic: sampleMnemonic,
|
|
593
568
|
numberOfAccounts: 1,
|
|
@@ -597,19 +572,17 @@ describe('hd-keyring', () => {
|
|
|
597
572
|
});
|
|
598
573
|
|
|
599
574
|
it('reliably can decode messages it signs', async function () {
|
|
600
|
-
const keyring = new
|
|
575
|
+
const keyring = new HdKeyring();
|
|
601
576
|
await keyring.deserialize({
|
|
602
577
|
mnemonic: sampleMnemonic,
|
|
603
578
|
numberOfAccounts: 1,
|
|
604
579
|
});
|
|
605
580
|
const localMessage = 'hello there!';
|
|
606
|
-
const msgHashHex = bufferToHex(
|
|
607
|
-
Buffer.from(keccak256(Buffer.from(localMessage))),
|
|
608
|
-
);
|
|
581
|
+
const msgHashHex = bufferToHex(keccak256(Buffer.from(localMessage)));
|
|
609
582
|
await keyring.addAccounts(9);
|
|
610
583
|
const addresses = await keyring.getAccounts();
|
|
611
584
|
const signatures = await Promise.all(
|
|
612
|
-
addresses.map(async (accountAddress
|
|
585
|
+
addresses.map(async (accountAddress) => {
|
|
613
586
|
return await keyring.signMessage(accountAddress, msgHashHex);
|
|
614
587
|
}),
|
|
615
588
|
);
|
|
@@ -628,7 +601,7 @@ describe('hd-keyring', () => {
|
|
|
628
601
|
});
|
|
629
602
|
|
|
630
603
|
it('throw error for invalid message', async function () {
|
|
631
|
-
const keyring = new
|
|
604
|
+
const keyring = new HdKeyring();
|
|
632
605
|
await keyring.deserialize({
|
|
633
606
|
mnemonic: sampleMnemonic,
|
|
634
607
|
numberOfAccounts: 1,
|
|
@@ -640,20 +613,19 @@ describe('hd-keyring', () => {
|
|
|
640
613
|
});
|
|
641
614
|
|
|
642
615
|
it('throw error if empty address is passed', async function () {
|
|
643
|
-
const keyring = new
|
|
616
|
+
const keyring = new HdKeyring();
|
|
644
617
|
await keyring.deserialize({
|
|
645
618
|
mnemonic: sampleMnemonic,
|
|
646
619
|
numberOfAccounts: 1,
|
|
647
620
|
});
|
|
648
621
|
|
|
649
|
-
// @ts-expect-error we inputting an invalid address
|
|
650
622
|
await expect(keyring.signMessage('', message)).rejects.toThrow(
|
|
651
623
|
'Must specify address.',
|
|
652
624
|
);
|
|
653
625
|
});
|
|
654
626
|
|
|
655
627
|
it('throw error if address not associated with the current keyring is passed', async function () {
|
|
656
|
-
const keyring = new
|
|
628
|
+
const keyring = new HdKeyring();
|
|
657
629
|
await keyring.deserialize({
|
|
658
630
|
mnemonic: sampleMnemonic,
|
|
659
631
|
numberOfAccounts: 1,
|
|
@@ -661,14 +633,14 @@ describe('hd-keyring', () => {
|
|
|
661
633
|
|
|
662
634
|
await expect(
|
|
663
635
|
keyring.signMessage(notKeyringAddress, message),
|
|
664
|
-
).rejects.toThrow('
|
|
636
|
+
).rejects.toThrow('HD Keyring - Unable to find matching address.');
|
|
665
637
|
});
|
|
666
638
|
});
|
|
667
639
|
|
|
668
640
|
describe('#removeAccount', function () {
|
|
669
|
-
let keyring
|
|
641
|
+
let keyring;
|
|
670
642
|
beforeEach(() => {
|
|
671
|
-
keyring = new
|
|
643
|
+
keyring = new HdKeyring({
|
|
672
644
|
mnemonic: sampleMnemonic,
|
|
673
645
|
numberOfAccounts: 1,
|
|
674
646
|
});
|
|
@@ -676,12 +648,9 @@ describe('hd-keyring', () => {
|
|
|
676
648
|
|
|
677
649
|
describe('if the account exists', function () {
|
|
678
650
|
it('should remove that account', async function () {
|
|
679
|
-
const
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
assert(rawAddress, 'rawAddress should be empty');
|
|
683
|
-
const address = add0x(rawAddress);
|
|
684
|
-
keyring.removeAccount(address);
|
|
651
|
+
const addresses = await keyring.getAccounts();
|
|
652
|
+
expect(addresses).toHaveLength(1);
|
|
653
|
+
keyring.removeAccount(addresses[0]);
|
|
685
654
|
const addressesAfterRemoval = await keyring.getAccounts();
|
|
686
655
|
expect(addressesAfterRemoval).toHaveLength(0);
|
|
687
656
|
});
|
|
@@ -698,9 +667,9 @@ describe('hd-keyring', () => {
|
|
|
698
667
|
});
|
|
699
668
|
|
|
700
669
|
describe('getAppKeyAddress', function () {
|
|
701
|
-
let keyring
|
|
670
|
+
let keyring;
|
|
702
671
|
beforeEach(() => {
|
|
703
|
-
keyring = new
|
|
672
|
+
keyring = new HdKeyring({
|
|
704
673
|
mnemonic: sampleMnemonic,
|
|
705
674
|
numberOfAccounts: 1,
|
|
706
675
|
});
|
|
@@ -751,7 +720,6 @@ describe('hd-keyring', () => {
|
|
|
751
720
|
});
|
|
752
721
|
|
|
753
722
|
it('should throw error if the provided origin is not a string', async function () {
|
|
754
|
-
// @ts-expect-error we are providing an incorrect origin key
|
|
755
723
|
await expect(keyring.getAppKeyAddress(firstAcct, [])).rejects.toThrow(
|
|
756
724
|
`'origin' must be a non-empty string`,
|
|
757
725
|
);
|
|
@@ -765,9 +733,9 @@ describe('hd-keyring', () => {
|
|
|
765
733
|
});
|
|
766
734
|
|
|
767
735
|
describe('exportAccount', function () {
|
|
768
|
-
let keyring
|
|
736
|
+
let keyring;
|
|
769
737
|
beforeEach(() => {
|
|
770
|
-
keyring = new
|
|
738
|
+
keyring = new HdKeyring({
|
|
771
739
|
mnemonic: sampleMnemonic,
|
|
772
740
|
numberOfAccounts: 1,
|
|
773
741
|
});
|
|
@@ -783,16 +751,16 @@ describe('hd-keyring', () => {
|
|
|
783
751
|
|
|
784
752
|
it('throw error if account is not present', async function () {
|
|
785
753
|
await expect(keyring.exportAccount(notKeyringAddress)).rejects.toThrow(
|
|
786
|
-
'
|
|
754
|
+
'HD Keyring - Unable to find matching address.',
|
|
787
755
|
);
|
|
788
756
|
});
|
|
789
757
|
});
|
|
790
758
|
|
|
791
759
|
describe('#encryptionPublicKey', function () {
|
|
792
760
|
const publicKey = 'LV7lWhd0mUDcvxkMU2o6uKXftu25zq4bMYdmMqppXic=';
|
|
793
|
-
let keyring
|
|
761
|
+
let keyring;
|
|
794
762
|
beforeEach(() => {
|
|
795
|
-
keyring = new
|
|
763
|
+
keyring = new HdKeyring({
|
|
796
764
|
mnemonic: sampleMnemonic,
|
|
797
765
|
numberOfAccounts: 1,
|
|
798
766
|
});
|
|
@@ -806,7 +774,6 @@ describe('hd-keyring', () => {
|
|
|
806
774
|
});
|
|
807
775
|
|
|
808
776
|
it('throw error if address is blank', async function () {
|
|
809
|
-
// @ts-expect-error provide an invalid key
|
|
810
777
|
await expect(keyring.getEncryptionPublicKey('')).rejects.toThrow(
|
|
811
778
|
'Must specify address.',
|
|
812
779
|
);
|
|
@@ -815,14 +782,14 @@ describe('hd-keyring', () => {
|
|
|
815
782
|
it('throw error if address is not present in the keyring', async function () {
|
|
816
783
|
await expect(
|
|
817
784
|
keyring.getEncryptionPublicKey(notKeyringAddress),
|
|
818
|
-
).rejects.toThrow('
|
|
785
|
+
).rejects.toThrow('HD Keyring - Unable to find matching address.');
|
|
819
786
|
});
|
|
820
787
|
});
|
|
821
788
|
|
|
822
789
|
describe('#signTypedData V4 signature verification', function () {
|
|
823
|
-
let keyring
|
|
790
|
+
let keyring;
|
|
824
791
|
beforeEach(() => {
|
|
825
|
-
keyring = new
|
|
792
|
+
keyring = new HdKeyring({
|
|
826
793
|
mnemonic: sampleMnemonic,
|
|
827
794
|
numberOfAccounts: 1,
|
|
828
795
|
});
|
|
@@ -860,7 +827,7 @@ describe('hd-keyring', () => {
|
|
|
860
827
|
chainId: 1,
|
|
861
828
|
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
|
|
862
829
|
},
|
|
863
|
-
primaryType: 'Mail'
|
|
830
|
+
primaryType: 'Mail',
|
|
864
831
|
message: {
|
|
865
832
|
from: {
|
|
866
833
|
name: 'Cow',
|
|
@@ -886,8 +853,8 @@ describe('hd-keyring', () => {
|
|
|
886
853
|
const addresses = await keyring.getAccounts();
|
|
887
854
|
const [address] = addresses;
|
|
888
855
|
|
|
889
|
-
const signature = await keyring.signTypedData(address
|
|
890
|
-
version:
|
|
856
|
+
const signature = await keyring.signTypedData(address, typedData, {
|
|
857
|
+
version: 'V4',
|
|
891
858
|
});
|
|
892
859
|
expect(signature).toBe(expectedSignature);
|
|
893
860
|
const restored = recoverTypedSignature({
|
|
@@ -901,10 +868,10 @@ describe('hd-keyring', () => {
|
|
|
901
868
|
|
|
902
869
|
describe('#decryptMessage', function () {
|
|
903
870
|
const message = 'Hello world!';
|
|
904
|
-
let encryptedMessage
|
|
871
|
+
let encryptedMessage, keyring;
|
|
905
872
|
|
|
906
873
|
beforeEach(async () => {
|
|
907
|
-
keyring = new
|
|
874
|
+
keyring = new HdKeyring({
|
|
908
875
|
mnemonic: sampleMnemonic,
|
|
909
876
|
numberOfAccounts: 1,
|
|
910
877
|
});
|
|
@@ -930,20 +897,20 @@ describe('hd-keyring', () => {
|
|
|
930
897
|
it('throw error if address passed is not present in the keyring', async function () {
|
|
931
898
|
await expect(
|
|
932
899
|
keyring.decryptMessage(notKeyringAddress, encryptedMessage),
|
|
933
|
-
).rejects.toThrow('
|
|
900
|
+
).rejects.toThrow('HD Keyring - Unable to find matching address.');
|
|
934
901
|
});
|
|
935
902
|
|
|
936
903
|
it('throw error if wrong encrypted data object is passed', async function () {
|
|
937
|
-
await expect(
|
|
938
|
-
|
|
939
|
-
)
|
|
904
|
+
await expect(keyring.decryptMessage(firstAcct, {})).rejects.toThrow(
|
|
905
|
+
'Encryption type/version not supported.',
|
|
906
|
+
);
|
|
940
907
|
});
|
|
941
908
|
});
|
|
942
909
|
|
|
943
910
|
describe('#signTransaction', function () {
|
|
944
|
-
let keyring
|
|
911
|
+
let keyring;
|
|
945
912
|
beforeEach(() => {
|
|
946
|
-
keyring = new
|
|
913
|
+
keyring = new HdKeyring({
|
|
947
914
|
mnemonic: sampleMnemonic,
|
|
948
915
|
numberOfAccounts: 1,
|
|
949
916
|
});
|
|
@@ -963,8 +930,7 @@ describe('hd-keyring', () => {
|
|
|
963
930
|
expect(tx.isSigned()).toBe(false);
|
|
964
931
|
|
|
965
932
|
const signed = await keyring.signTransaction(firstAcct, tx);
|
|
966
|
-
|
|
967
|
-
expect(signedTx.isSigned()).toBe(true);
|
|
933
|
+
expect(signed.isSigned()).toBe(true);
|
|
968
934
|
});
|
|
969
935
|
|
|
970
936
|
it('returns a signed tx object', async function () {
|
|
@@ -972,13 +938,11 @@ describe('hd-keyring', () => {
|
|
|
972
938
|
expect(tx.isSigned()).toBe(false);
|
|
973
939
|
|
|
974
940
|
const signed = await keyring.signTransaction(firstAcct, tx);
|
|
975
|
-
|
|
976
|
-
expect(signedTx.isSigned()).toBe(true);
|
|
941
|
+
expect(signed.isSigned()).toBe(true);
|
|
977
942
|
});
|
|
978
943
|
|
|
979
944
|
it('returns rejected promise if empty address is passed', async function () {
|
|
980
945
|
const tx = TransactionFactory.fromTxData(txParams);
|
|
981
|
-
// @ts-expect-error provide invalid address
|
|
982
946
|
await expect(keyring.signTransaction('', tx)).rejects.toThrow(
|
|
983
947
|
'Must specify address.',
|
|
984
948
|
);
|
|
@@ -988,7 +952,7 @@ describe('hd-keyring', () => {
|
|
|
988
952
|
const tx = TransactionFactory.fromTxData(txParams);
|
|
989
953
|
await expect(
|
|
990
954
|
keyring.signTransaction(notKeyringAddress, tx),
|
|
991
|
-
).rejects.toThrow('
|
|
955
|
+
).rejects.toThrow('HD Keyring - Unable to find matching address.');
|
|
992
956
|
});
|
|
993
957
|
});
|
|
994
958
|
});
|