@metamask/eth-simple-keyring 5.0.0 → 5.1.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/test/index.js DELETED
@@ -1,759 +0,0 @@
1
- const {
2
- stripHexPrefix,
3
- bufferToHex,
4
- toBuffer,
5
- ecrecover,
6
- pubToAddress,
7
- isValidAddress,
8
- } = require('@ethereumjs/util');
9
- const { keccak256 } = require('ethereum-cryptography/keccak');
10
- const {
11
- encrypt,
12
- getEncryptionPublicKey,
13
- personalSign,
14
- recoverPersonalSignature,
15
- recoverTypedSignature,
16
- signTypedData,
17
- SignTypedDataVersion,
18
- } = require('@metamask/eth-sig-util');
19
- const {
20
- TransactionFactory,
21
- Transaction: EthereumTx,
22
- } = require('@ethereumjs/tx');
23
- const SimpleKeyring = require('..');
24
-
25
- const TYPE_STR = 'Simple Key Pair';
26
-
27
- // Sample account:
28
- const testAccount = {
29
- key: '0xb8a9c05beeedb25df85f8d641538cbffedf67216048de9c678ee26260eb91952',
30
- address: '0x01560cd3bac62cc6d7e6380600d9317363400896',
31
- };
32
-
33
- const notKeyringAddress = '0xbD20F6F5F1616947a39E11926E78ec94817B3931';
34
-
35
- describe('simple-keyring', function () {
36
- let keyring;
37
- beforeEach(function () {
38
- keyring = new SimpleKeyring();
39
- });
40
-
41
- describe('Keyring.type', function () {
42
- it('is a class property that returns the type string.', function () {
43
- const { type } = SimpleKeyring;
44
- expect(type).toBe(TYPE_STR);
45
- });
46
- });
47
-
48
- describe('#serialize empty wallets.', function () {
49
- it('serializes an empty array', async function () {
50
- const output = await keyring.serialize();
51
- expect(output).toHaveLength(0);
52
- });
53
- });
54
-
55
- describe('#deserialize a private key', function () {
56
- it('serializes what it deserializes', async function () {
57
- await keyring.deserialize([testAccount.key]);
58
- const serialized = await keyring.serialize();
59
- expect(serialized).toHaveLength(1);
60
- expect(serialized[0]).toBe(stripHexPrefix(testAccount.key));
61
- });
62
- });
63
-
64
- describe('#constructor with a private key', function () {
65
- it('has the correct addresses', async function () {
66
- const newKeyring = new SimpleKeyring([testAccount.key]);
67
- const accounts = await newKeyring.getAccounts();
68
- expect(accounts).toStrictEqual([testAccount.address]);
69
- });
70
- });
71
-
72
- describe('#signTransaction', function () {
73
- const address = '0x9858e7d8b79fc3e6d989636721584498926da38a';
74
- const privateKey =
75
- '0x7dd98753d7b4394095de7d176c58128e2ed6ee600abe97c9f6d9fd65015d9b18';
76
- const txParams = {
77
- from: address,
78
- nonce: '0x00',
79
- gasPrice: '0x09184e72a000',
80
- gasLimit: '0x2710',
81
- to: address,
82
- value: '0x1000',
83
- };
84
-
85
- it('returns a signed legacy tx object', async function () {
86
- await keyring.deserialize([privateKey]);
87
- const tx = new EthereumTx(txParams);
88
- expect(tx.isSigned()).toBe(false);
89
-
90
- const signed = await keyring.signTransaction(address, tx);
91
- expect(signed.isSigned()).toBe(true);
92
- });
93
-
94
- it('returns a signed tx object', async function () {
95
- await keyring.deserialize([privateKey]);
96
- const tx = TransactionFactory.fromTxData(txParams);
97
- expect(tx.isSigned()).toBe(false);
98
-
99
- const signed = await keyring.signTransaction(address, tx);
100
- expect(signed.isSigned()).toBe(true);
101
- });
102
-
103
- it('returns rejected promise if empty address is passed', async function () {
104
- await keyring.deserialize([privateKey]);
105
- const tx = TransactionFactory.fromTxData(txParams);
106
- await expect(keyring.signTransaction('', tx)).rejects.toThrow(
107
- 'Must specify address.',
108
- );
109
- });
110
-
111
- it('throw error if wrong address is passed', async function () {
112
- await keyring.deserialize([privateKey]);
113
- const tx = TransactionFactory.fromTxData(txParams);
114
- await expect(
115
- keyring.signTransaction(notKeyringAddress, tx),
116
- ).rejects.toThrow('Simple Keyring - Unable to find matching address.');
117
- });
118
- });
119
-
120
- describe('#signMessage', function () {
121
- const address = '0x9858e7d8b79fc3e6d989636721584498926da38a';
122
- const message =
123
- '0x879a053d4800c6354e76c7985a865d2922c82fb5b3f4577b2fe08b998954f2e0';
124
- const privateKey =
125
- '0x7dd98753d7b4394095de7d176c58128e2ed6ee600abe97c9f6d9fd65015d9b18';
126
- const expectedResult =
127
- '0x28fcb6768e5110144a55b2e6ce9d1ea5a58103033632d272d2b5cf506906f7941a00b539383fd872109633d8c71c404e13dba87bc84166ee31b0e36061a69e161c';
128
-
129
- it('passes the dennis test', async function () {
130
- await keyring.deserialize([privateKey]);
131
- const result = await keyring.signMessage(address, message);
132
- expect(result).toBe(expectedResult);
133
- });
134
-
135
- it('reliably can decode messages it signs', async function () {
136
- await keyring.deserialize([privateKey]);
137
- const localMessage = 'hello there!';
138
- const msgHashHex = bufferToHex(keccak256(Buffer.from(localMessage)));
139
-
140
- await keyring.addAccounts(9);
141
- const addresses = await keyring.getAccounts();
142
- const signatures = await Promise.all(
143
- addresses.map(async (accountAddress) => {
144
- return await keyring.signMessage(accountAddress, msgHashHex);
145
- }),
146
- );
147
- signatures.forEach((sgn, index) => {
148
- const accountAddress = addresses[index];
149
-
150
- const r = toBuffer(sgn.slice(0, 66));
151
- const s = toBuffer(`0x${sgn.slice(66, 130)}`);
152
- const v = BigInt(`0x${sgn.slice(130, 132)}`);
153
- const m = toBuffer(msgHashHex);
154
- const pub = ecrecover(m, v, r, s);
155
- const adr = `0x${pubToAddress(pub).toString('hex')}`;
156
-
157
- expect(adr).toBe(accountAddress);
158
- });
159
- });
160
-
161
- it('throw error for invalid message', async function () {
162
- await keyring.deserialize([privateKey]);
163
- await expect(keyring.signMessage(address, '')).rejects.toThrow(
164
- 'Cannot convert 0x to a BigInt',
165
- );
166
- });
167
-
168
- it('throw error if empty address is passed', async function () {
169
- await keyring.deserialize([privateKey]);
170
- await expect(keyring.signMessage('', message)).rejects.toThrow(
171
- 'Must specify address.',
172
- );
173
- });
174
-
175
- it('throw error if address not associated with the current keyring is passed', async function () {
176
- await keyring.deserialize([privateKey]);
177
- await expect(
178
- keyring.signMessage(notKeyringAddress, message),
179
- ).rejects.toThrow('Simple Keyring - Unable to find matching address.');
180
- });
181
- });
182
-
183
- describe('#addAccounts', function () {
184
- describe('with no arguments', function () {
185
- it('creates a single wallet', async function () {
186
- await keyring.addAccounts();
187
- const serializedKeyring = await keyring.serialize();
188
- expect(serializedKeyring).toHaveLength(1);
189
- });
190
- });
191
-
192
- describe('with a numeric argument', function () {
193
- it('creates that number of wallets', async function () {
194
- await keyring.addAccounts(3);
195
- const serializedKeyring = await keyring.serialize();
196
- expect(serializedKeyring).toHaveLength(3);
197
- });
198
- });
199
- });
200
-
201
- describe('#getAccounts', function () {
202
- it('should return a list of addresses in wallet', async function () {
203
- // Push a mock wallet
204
- keyring.deserialize([testAccount.key]);
205
-
206
- const output = await keyring.getAccounts();
207
- expect(output).toHaveLength(1);
208
- expect(output[0]).toBe(testAccount.address);
209
- });
210
- });
211
-
212
- describe('#removeAccount', function () {
213
- describe('if the account exists', function () {
214
- it('should remove that account', async function () {
215
- await keyring.addAccounts();
216
- const addresses = await keyring.getAccounts();
217
- expect(addresses).toHaveLength(1);
218
- keyring.removeAccount(addresses[0]);
219
- const addressesAfterRemoval = await keyring.getAccounts();
220
- expect(addressesAfterRemoval).toHaveLength(0);
221
- });
222
- });
223
-
224
- describe('if the account does not exist', function () {
225
- it('should throw an error', function () {
226
- const unexistingAccount = '0x0000000000000000000000000000000000000000';
227
- expect(() => keyring.removeAccount(unexistingAccount)).toThrow(
228
- `Address ${unexistingAccount} not found in this keyring`,
229
- );
230
- });
231
- });
232
- });
233
-
234
- describe('#signPersonalMessage', function () {
235
- const address = '0xbe93f9bacbcffc8ee6663f2647917ed7a20a57bb';
236
- const privateKey = Buffer.from(
237
- '6969696969696969696969696969696969696969696969696969696969696969',
238
- 'hex',
239
- );
240
- const privKeyHex = bufferToHex(privateKey);
241
- const message = '0x68656c6c6f20776f726c64';
242
- const expectedSignature =
243
- '0xce909e8ea6851bc36c007a0072d0524b07a3ff8d4e623aca4c71ca8e57250c4d0a3fc38fa8fbaaa81ead4b9f6bd03356b6f8bf18bccad167d78891636e1d69561b';
244
-
245
- it('returns the expected value', async function () {
246
- await keyring.deserialize([privKeyHex]);
247
- const signature = await keyring.signPersonalMessage(address, message);
248
- expect(signature).toBe(expectedSignature);
249
-
250
- const restored = recoverPersonalSignature({
251
- data: message,
252
- signature,
253
- });
254
- expect(restored).toBe(address);
255
- });
256
-
257
- it('throw error if empty address is passed', async function () {
258
- await keyring.deserialize([privKeyHex]);
259
- await expect(keyring.signPersonalMessage('', message)).rejects.toThrow(
260
- 'Must specify address.',
261
- );
262
- });
263
-
264
- it('throw error if wrong address is passed', async function () {
265
- await keyring.deserialize([privKeyHex]);
266
- await expect(
267
- keyring.signPersonalMessage(notKeyringAddress, message),
268
- ).rejects.toThrow('Simple Keyring - Unable to find matching address.');
269
- });
270
- });
271
-
272
- describe('#signTypedData', function () {
273
- const address = '0x29c76e6ad8f28bb1004902578fb108c507be341b';
274
- const privKeyHex =
275
- '4af1bceebf7f3634ec3cff8a2c38e51178d5d4ce585c52d6043e5e2cc3418bb0';
276
- const expectedSignature =
277
- '0x49e75d475d767de7fcc67f521e0d86590723d872e6111e51c393e8c1e2f21d032dfaf5833af158915f035db6af4f37bf2d5d29781cd81f28a44c5cb4b9d241531b';
278
-
279
- const typedData = [
280
- {
281
- type: 'string',
282
- name: 'message',
283
- value: 'Hi, Alice!',
284
- },
285
- ];
286
-
287
- it('returns the expected value', async function () {
288
- await keyring.deserialize([privKeyHex]);
289
- const signature = await keyring.signTypedData(address, typedData);
290
- expect(signature).toBe(expectedSignature);
291
- const restored = recoverTypedSignature({
292
- data: typedData,
293
- signature,
294
- version: SignTypedDataVersion.V1,
295
- });
296
- expect(restored).toBe(address);
297
- });
298
-
299
- it('returns the expected value if invalid version is given', async function () {
300
- await keyring.deserialize([privKeyHex]);
301
- const signature = await keyring.signTypedData(address, typedData, {
302
- version: 'FOO',
303
- });
304
- expect(signature).toBe(expectedSignature);
305
- const restored = recoverTypedSignature({
306
- data: typedData,
307
- signature,
308
- version: SignTypedDataVersion.V1,
309
- });
310
- expect(restored).toBe(address);
311
- });
312
- });
313
-
314
- describe('#signTypedData V1', function () {
315
- const address = '0x29c76e6ad8f28bb1004902578fb108c507be341b';
316
- const privKeyHex =
317
- '4af1bceebf7f3634ec3cff8a2c38e51178d5d4ce585c52d6043e5e2cc3418bb0';
318
- const expectedSignature =
319
- '0x49e75d475d767de7fcc67f521e0d86590723d872e6111e51c393e8c1e2f21d032dfaf5833af158915f035db6af4f37bf2d5d29781cd81f28a44c5cb4b9d241531b';
320
-
321
- const typedData = [
322
- {
323
- type: 'string',
324
- name: 'message',
325
- value: 'Hi, Alice!',
326
- },
327
- ];
328
-
329
- it('returns the expected value', async function () {
330
- await keyring.deserialize([privKeyHex]);
331
- const signature = await keyring.signTypedData(address, typedData, {
332
- version: 'V1',
333
- });
334
- expect(signature).toBe(expectedSignature);
335
- const restored = recoverTypedSignature({
336
- data: typedData,
337
- signature,
338
- version: SignTypedDataVersion.V1,
339
- });
340
- expect(restored).toBe(address);
341
- });
342
-
343
- it('works via version paramter', async function () {
344
- await keyring.deserialize([privKeyHex]);
345
- const signature = await keyring.signTypedData(address, typedData);
346
- expect(signature).toBe(expectedSignature);
347
- const restored = recoverTypedSignature({
348
- data: typedData,
349
- signature,
350
- version: SignTypedDataVersion.V1,
351
- });
352
- expect(restored).toBe(address);
353
- });
354
- });
355
-
356
- describe('#signTypedData V3', function () {
357
- const address = '0x29c76e6ad8f28bb1004902578fb108c507be341b';
358
- const privKeyHex =
359
- '0x4af1bceebf7f3634ec3cff8a2c38e51178d5d4ce585c52d6043e5e2cc3418bb0';
360
-
361
- it('returns the expected value', async function () {
362
- const typedData = {
363
- types: {
364
- EIP712Domain: [],
365
- },
366
- domain: {},
367
- primaryType: 'EIP712Domain',
368
- message: {},
369
- };
370
-
371
- await keyring.deserialize([privKeyHex]);
372
- const signature = await keyring.signTypedData(address, typedData, {
373
- version: 'V3',
374
- });
375
- const restored = recoverTypedSignature({
376
- data: typedData,
377
- signature,
378
- version: SignTypedDataVersion.V3,
379
- });
380
- expect(restored).toBe(address);
381
- });
382
- });
383
-
384
- describe('#signTypedData V3 signature verification', function () {
385
- const privKeyHex =
386
- 'c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4';
387
- const expectedSignature =
388
- '0x4355c47d63924e8a72e509b65029052eb6c299d53a04e167c5775fd466751c9d07299936d304c153f6443dfa05f40ff007d72911b6f72307f996231605b915621c';
389
-
390
- it('returns the expected value', async function () {
391
- const typedData = {
392
- types: {
393
- EIP712Domain: [
394
- { name: 'name', type: 'string' },
395
- { name: 'version', type: 'string' },
396
- { name: 'chainId', type: 'uint256' },
397
- { name: 'verifyingContract', type: 'address' },
398
- ],
399
- Person: [
400
- { name: 'name', type: 'string' },
401
- { name: 'wallet', type: 'address' },
402
- ],
403
- Mail: [
404
- { name: 'from', type: 'Person' },
405
- { name: 'to', type: 'Person' },
406
- { name: 'contents', type: 'string' },
407
- ],
408
- },
409
- primaryType: 'Mail',
410
- domain: {
411
- name: 'Ether Mail',
412
- version: '1',
413
- chainId: 1,
414
- verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
415
- },
416
- message: {
417
- from: {
418
- name: 'Cow',
419
- wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
420
- },
421
- to: {
422
- name: 'Bob',
423
- wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
424
- },
425
- contents: 'Hello, Bob!',
426
- },
427
- };
428
-
429
- await keyring.deserialize([privKeyHex]);
430
- const addresses = await keyring.getAccounts();
431
- const [address] = addresses;
432
- const signature = await keyring.signTypedData(address, typedData, {
433
- version: 'V3',
434
- });
435
- expect(signature).toBe(expectedSignature);
436
- const restored = recoverTypedSignature({
437
- data: typedData,
438
- signature,
439
- version: SignTypedDataVersion.V3,
440
- });
441
- expect(restored).toBe(address);
442
- });
443
- });
444
-
445
- describe('#signTypedData V4', function () {
446
- const address = '0x29c76e6ad8f28bb1004902578fb108c507be341b';
447
- const privKeyHex =
448
- '0x4af1bceebf7f3634ec3cff8a2c38e51178d5d4ce585c52d6043e5e2cc3418bb0';
449
-
450
- it('returns the expected value', async function () {
451
- const typedData = {
452
- types: {
453
- EIP712Domain: [],
454
- },
455
- domain: {},
456
- primaryType: 'EIP712Domain',
457
- message: {},
458
- };
459
-
460
- await keyring.deserialize([privKeyHex]);
461
- const signature = await keyring.signTypedData(address, typedData, {
462
- version: 'V4',
463
- });
464
- const restored = recoverTypedSignature({
465
- data: typedData,
466
- signature,
467
- version: SignTypedDataVersion.V4,
468
- });
469
- expect(restored).toBe(address);
470
- });
471
- });
472
-
473
- describe('#decryptMessage', function () {
474
- const address = '0xbe93f9bacbcffc8ee6663f2647917ed7a20a57bb';
475
- const privateKey = Buffer.from(
476
- '6969696969696969696969696969696969696969696969696969696969696969',
477
- 'hex',
478
- );
479
- const privKeyHex = bufferToHex(privateKey);
480
- const message = 'Hello world!';
481
- const encryptedMessage = encrypt({
482
- publicKey: getEncryptionPublicKey(privateKey),
483
- data: message,
484
- version: 'x25519-xsalsa20-poly1305',
485
- });
486
-
487
- it('returns the expected value', async function () {
488
- await keyring.deserialize([privKeyHex]);
489
- const decryptedMessage = await keyring.decryptMessage(
490
- address,
491
- encryptedMessage,
492
- );
493
- expect(message).toBe(decryptedMessage);
494
- });
495
-
496
- it('throw error if address passed is not present in the keyring', async function () {
497
- await keyring.deserialize([privKeyHex]);
498
- await expect(
499
- keyring.decryptMessage(notKeyringAddress, encryptedMessage),
500
- ).rejects.toThrow('Simple Keyring - Unable to find matching address.');
501
- });
502
-
503
- it('throw error if wrong encrypted data object is passed', async function () {
504
- await keyring.deserialize([privKeyHex]);
505
- await expect(keyring.decryptMessage(address, {})).rejects.toThrow(
506
- 'Encryption type/version not supported.',
507
- );
508
- });
509
- });
510
-
511
- describe('#encryptionPublicKey', function () {
512
- const address = '0xbe93f9bacbcffc8ee6663f2647917ed7a20a57bb';
513
- const privateKey = Buffer.from(
514
- '6969696969696969696969696969696969696969696969696969696969696969',
515
- 'hex',
516
- );
517
- const publicKey = 'GxuMqoE2oHsZzcQtv/WMNB3gCH2P6uzynuwO1P0MM1U=';
518
- const privKeyHex = bufferToHex(privateKey);
519
-
520
- it('returns the expected value', async function () {
521
- await keyring.deserialize([privKeyHex]);
522
- const encryptionPublicKey = await keyring.getEncryptionPublicKey(
523
- address,
524
- privateKey,
525
- );
526
- expect(publicKey).toBe(encryptionPublicKey);
527
- });
528
-
529
- it('throw error if address is blank', async function () {
530
- await keyring.deserialize([privKeyHex]);
531
- await expect(
532
- keyring.getEncryptionPublicKey('', privateKey),
533
- ).rejects.toThrow('Must specify address.');
534
- });
535
-
536
- it('throw error if address is not present in the keyring', async function () {
537
- await keyring.deserialize([privKeyHex]);
538
- await expect(
539
- keyring.getEncryptionPublicKey(notKeyringAddress, privateKey),
540
- ).rejects.toThrow('Simple Keyring - Unable to find matching address.');
541
- });
542
- });
543
-
544
- describe('#signTypedData V4 signature verification', function () {
545
- const privKeyHex =
546
- 'c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4';
547
- const expectedSignature =
548
- '0x65cbd956f2fae28a601bebc9b906cea0191744bd4c4247bcd27cd08f8eb6b71c78efdf7a31dc9abee78f492292721f362d296cf86b4538e07b51303b67f749061b';
549
-
550
- it('returns the expected value', async function () {
551
- const typedData = {
552
- types: {
553
- EIP712Domain: [
554
- { name: 'name', type: 'string' },
555
- { name: 'version', type: 'string' },
556
- { name: 'chainId', type: 'uint256' },
557
- { name: 'verifyingContract', type: 'address' },
558
- ],
559
- Person: [
560
- { name: 'name', type: 'string' },
561
- { name: 'wallets', type: 'address[]' },
562
- ],
563
- Mail: [
564
- { name: 'from', type: 'Person' },
565
- { name: 'to', type: 'Person[]' },
566
- { name: 'contents', type: 'string' },
567
- ],
568
- Group: [
569
- { name: 'name', type: 'string' },
570
- { name: 'members', type: 'Person[]' },
571
- ],
572
- },
573
- domain: {
574
- name: 'Ether Mail',
575
- version: '1',
576
- chainId: 1,
577
- verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
578
- },
579
- primaryType: 'Mail',
580
- message: {
581
- from: {
582
- name: 'Cow',
583
- wallets: [
584
- '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
585
- '0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF',
586
- ],
587
- },
588
- to: [
589
- {
590
- name: 'Bob',
591
- wallets: [
592
- '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
593
- '0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57',
594
- '0xB0B0b0b0b0b0B000000000000000000000000000',
595
- ],
596
- },
597
- ],
598
- contents: 'Hello, Bob!',
599
- },
600
- };
601
-
602
- await keyring.deserialize([privKeyHex]);
603
-
604
- const addresses = await keyring.getAccounts();
605
- const [address] = addresses;
606
-
607
- const signature = await keyring.signTypedData(address, typedData, {
608
- version: 'V4',
609
- });
610
- expect(signature).toBe(expectedSignature);
611
- const restored = recoverTypedSignature({
612
- data: typedData,
613
- signature,
614
- version: SignTypedDataVersion.V4,
615
- });
616
- expect(restored).toBe(address);
617
- });
618
- });
619
-
620
- describe('getAppKeyAddress', function () {
621
- it('should return a public address custom to the provided app key origin', async function () {
622
- const { address } = testAccount;
623
- const simpleKeyring = new SimpleKeyring([testAccount.key]);
624
-
625
- const appKeyAddress = await simpleKeyring.getAppKeyAddress(
626
- address,
627
- 'someapp.origin.io',
628
- );
629
-
630
- expect(address).not.toBe(appKeyAddress);
631
- expect(isValidAddress(appKeyAddress)).toBe(true);
632
- });
633
-
634
- it('should return different addresses when provided different app key origins', async function () {
635
- const { address } = testAccount;
636
- const simpleKeyring = new SimpleKeyring([testAccount.key]);
637
-
638
- const appKeyAddress1 = await simpleKeyring.getAppKeyAddress(
639
- address,
640
- 'someapp.origin.io',
641
- );
642
-
643
- expect(isValidAddress(appKeyAddress1)).toBe(true);
644
-
645
- const appKeyAddress2 = await simpleKeyring.getAppKeyAddress(
646
- address,
647
- 'anotherapp.origin.io',
648
- );
649
-
650
- expect(isValidAddress(appKeyAddress2)).toBe(true);
651
- expect(appKeyAddress1).not.toBe(appKeyAddress2);
652
- });
653
-
654
- it('should return the same address when called multiple times with the same params', async function () {
655
- const { address } = testAccount;
656
- const simpleKeyring = new SimpleKeyring([testAccount.key]);
657
-
658
- const appKeyAddress1 = await simpleKeyring.getAppKeyAddress(
659
- address,
660
- 'someapp.origin.io',
661
- );
662
-
663
- expect(isValidAddress(appKeyAddress1)).toBe(true);
664
-
665
- const appKeyAddress2 = await simpleKeyring.getAppKeyAddress(
666
- address,
667
- 'someapp.origin.io',
668
- );
669
-
670
- expect(isValidAddress(appKeyAddress2)).toBe(true);
671
- expect(appKeyAddress1).toBe(appKeyAddress2);
672
- });
673
-
674
- it('should throw error if the provided origin is not a string', async function () {
675
- const { address } = testAccount;
676
- const simpleKeyring = new SimpleKeyring([testAccount.key]);
677
-
678
- await expect(simpleKeyring.getAppKeyAddress(address, [])).rejects.toThrow(
679
- `'origin' must be a non-empty string`,
680
- );
681
- });
682
-
683
- it('should throw error if the provided origin is an empty string', async function () {
684
- const { address } = testAccount;
685
- const simpleKeyring = new SimpleKeyring([testAccount.key]);
686
-
687
- await expect(simpleKeyring.getAppKeyAddress(address, '')).rejects.toThrow(
688
- `'origin' must be a non-empty string`,
689
- );
690
- });
691
- });
692
-
693
- describe('exportAccount', function () {
694
- it('should return a hex-encoded private key', async function () {
695
- const { address } = testAccount;
696
- const simpleKeyring = new SimpleKeyring([testAccount.key]);
697
- const privKeyHexValue = await simpleKeyring.exportAccount(address);
698
- expect(testAccount.key).toBe(`0x${privKeyHexValue}`);
699
- });
700
-
701
- it('throw error if account is not present', async function () {
702
- await expect(keyring.exportAccount(notKeyringAddress)).rejects.toThrow(
703
- 'Simple Keyring - Unable to find matching address.',
704
- );
705
- });
706
- });
707
-
708
- describe('signing methods withAppKeyOrigin option', function () {
709
- it('should signPersonalMessage with the expected key when passed a withAppKeyOrigin', async function () {
710
- const { address } = testAccount;
711
- const message = '0x68656c6c6f20776f726c64';
712
-
713
- const privateKeyHex =
714
- '4fbe006f0e9c2374f53eb1aef1b6970d20206c61ea05ad9591ef42176eb842c0';
715
- const privateKey = Buffer.from(privateKeyHex, 'hex');
716
- const expectedSignature = personalSign({ privateKey, data: message });
717
-
718
- const simpleKeyring = new SimpleKeyring([testAccount.key]);
719
- const signature = await simpleKeyring.signPersonalMessage(
720
- address,
721
- message,
722
- {
723
- withAppKeyOrigin: 'someapp.origin.io',
724
- },
725
- );
726
-
727
- expect(expectedSignature).toBe(signature);
728
- });
729
-
730
- it('should signTypedData V3 with the expected key when passed a withAppKeyOrigin', async function () {
731
- const { address } = testAccount;
732
- const typedData = {
733
- types: {
734
- EIP712Domain: [],
735
- },
736
- domain: {},
737
- primaryType: 'EIP712Domain',
738
- message: {},
739
- };
740
-
741
- const privateKeyHex =
742
- '4fbe006f0e9c2374f53eb1aef1b6970d20206c61ea05ad9591ef42176eb842c0';
743
- const privateKey = Buffer.from(privateKeyHex, 'hex');
744
- const expectedSignature = signTypedData({
745
- privateKey,
746
- data: typedData,
747
- version: SignTypedDataVersion.V3,
748
- });
749
-
750
- const simpleKeyring = new SimpleKeyring([testAccount.key]);
751
- const signature = await simpleKeyring.signTypedData(address, typedData, {
752
- withAppKeyOrigin: 'someapp.origin.io',
753
- version: 'V3',
754
- });
755
-
756
- expect(expectedSignature).toBe(signature);
757
- });
758
- });
759
- });