@@ -25,9 +25,10 @@ jest.mock('../../../src/Accounts');
25
25
* AccountTest test
26
26
*/
27
27
describe ( 'AccountTest' , ( ) => {
28
- let account , accountsMock , transactionSignerMock ;
28
+ let account , accountsMock , transactionSignerMock , mockKey ;
29
29
30
30
beforeEach ( ( ) => {
31
+ mockKey = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' ;
31
32
transactionSignerMock = new TransactionSigner ( ) ;
32
33
33
34
new Accounts ( ) ;
@@ -55,6 +56,20 @@ describe('AccountTest', () => {
55
56
expect ( accountsMock . signTransaction ) . toHaveBeenCalledWith ( { } , 'pk' , callback ) ;
56
57
} ) ;
57
58
59
+ it ( 'calls fromPrivateKey with incorrect private key length and throws error' , ( ) => {
60
+ expect ( ( ) => {
61
+ Account . fromPrivateKey ( 'asdfasdf' )
62
+ } ) . toThrow ( 'Private key must be 32 bytes long' ) ;
63
+ } ) ;
64
+
65
+ it ( 'calls fromPrivateKey with incorrect private key prefix and throws error' , ( ) => {
66
+ mockKey = '0z0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' ;
67
+
68
+ expect ( ( ) => {
69
+ Account . fromPrivateKey ( mockKey )
70
+ } ) . toThrow ( 'Private key must be 32 bytes long' ) ;
71
+ } ) ;
72
+
58
73
it ( 'calls sign with non-strict hex and returns the expected string' , ( ) => {
59
74
isHexStrict . mockReturnValue ( false ) ;
60
75
@@ -152,17 +167,15 @@ describe('AccountTest', () => {
152
167
final : jest . fn ( )
153
168
} ;
154
169
155
- decipher . update . mockReturnValueOnce ( Buffer . from ( '0 ') ) ;
170
+ decipher . update . mockReturnValueOnce ( Buffer . from ( mockKey . slice ( 2 , 34 ) , 'hex ') ) ;
156
171
157
- decipher . final . mockReturnValueOnce ( Buffer . from ( '0 ') ) ;
172
+ decipher . final . mockReturnValueOnce ( Buffer . from ( mockKey . slice ( 34 , 66 ) , 'hex ') ) ;
158
173
159
174
createDecipheriv . mockReturnValueOnce ( decipher ) ;
160
175
161
176
expect ( Account . fromV3Keystore ( json , 'password' , false ) ) . toBeInstanceOf ( Account ) ;
162
177
163
- expect ( fromPrivate ) . toHaveBeenLastCalledWith (
164
- `0x${ Buffer . concat ( [ Buffer . from ( '0' ) , Buffer . from ( '0' ) ] ) . toString ( 'hex' ) } `
165
- ) ;
178
+ expect ( fromPrivate ) . toHaveBeenLastCalledWith ( mockKey ) ;
166
179
167
180
expect ( scrypt ) . toHaveBeenCalledWith (
168
181
Buffer . from ( 'password' ) ,
@@ -220,19 +233,17 @@ describe('AccountTest', () => {
220
233
final : jest . fn ( )
221
234
} ;
222
235
223
- decipher . update . mockReturnValueOnce ( Buffer . from ( '0 ') ) ;
236
+ decipher . update . mockReturnValueOnce ( Buffer . from ( mockKey . slice ( 2 , 34 ) , 'hex ') ) ;
224
237
225
- decipher . final . mockReturnValueOnce ( Buffer . from ( '0 ') ) ;
238
+ decipher . final . mockReturnValueOnce ( Buffer . from ( mockKey . slice ( 34 , 66 ) , 'hex ') ) ;
226
239
227
240
createDecipheriv . mockReturnValueOnce ( decipher ) ;
228
241
229
242
pbkdf2Sync . mockReturnValueOnce ( Buffer . from ( '00000000000000000000000000000000' ) ) ;
230
243
231
244
expect ( Account . fromV3Keystore ( json , 'password' , false ) ) . toBeInstanceOf ( Account ) ;
232
245
233
- expect ( fromPrivate ) . toHaveBeenCalledWith (
234
- `0x${ Buffer . concat ( [ Buffer . from ( '0' ) , Buffer . from ( '0' ) ] ) . toString ( 'hex' ) } `
235
- ) ;
246
+ expect ( fromPrivate ) . toHaveBeenCalledWith ( mockKey ) ;
236
247
237
248
expect ( pbkdf2Sync ) . toHaveBeenCalledWith (
238
249
Buffer . from ( 'password' ) ,
@@ -349,7 +360,7 @@ describe('AccountTest', () => {
349
360
350
361
uuid . v4 . mockReturnValueOnce ( 0 ) ;
351
362
352
- expect ( Account . fromPrivateKey ( 'pk' ) . toV3Keystore ( 'password' , options ) ) . toEqual ( {
363
+ expect ( Account . fromPrivateKey ( mockKey ) . toV3Keystore ( 'password' , options ) ) . toEqual ( {
353
364
version : 3 ,
354
365
id : 0 ,
355
366
address : 'a' ,
@@ -369,7 +380,7 @@ describe('AccountTest', () => {
369
380
}
370
381
} ) ;
371
382
372
- expect ( fromPrivate ) . toHaveBeenCalledWith ( 'pk' ) ;
383
+ expect ( fromPrivate ) . toHaveBeenCalledWith ( mockKey ) ;
373
384
374
385
expect ( randomBytes ) . toHaveBeenNthCalledWith ( 1 , 32 ) ;
375
386
@@ -426,7 +437,7 @@ describe('AccountTest', () => {
426
437
427
438
uuid . v4 . mockReturnValueOnce ( 0 ) ;
428
439
429
- expect ( Account . fromPrivateKey ( 'pk' ) . toV3Keystore ( 'password' , options ) ) . toEqual ( {
440
+ expect ( Account . fromPrivateKey ( mockKey ) . toV3Keystore ( 'password' , options ) ) . toEqual ( {
430
441
version : 3 ,
431
442
id : 0 ,
432
443
address : 'a' ,
@@ -445,7 +456,7 @@ describe('AccountTest', () => {
445
456
}
446
457
} ) ;
447
458
448
- expect ( fromPrivate ) . toHaveBeenCalledWith ( 'pk' ) ;
459
+ expect ( fromPrivate ) . toHaveBeenCalledWith ( mockKey ) ;
449
460
450
461
expect ( randomBytes ) . toHaveBeenNthCalledWith ( 1 , 32 ) ;
451
462
@@ -484,10 +495,10 @@ describe('AccountTest', () => {
484
495
randomBytes . mockReturnValue ( Buffer . from ( 'random' ) ) ;
485
496
486
497
expect ( ( ) => {
487
- Account . fromPrivateKey ( 'pk' ) . toV3Keystore ( 'password' , { kdf : 'nope' } ) ;
498
+ Account . fromPrivateKey ( mockKey ) . toV3Keystore ( 'password' , { kdf : 'nope' } ) ;
488
499
} ) . toThrow ( 'Unsupported kdf' ) ;
489
500
490
- expect ( fromPrivate ) . toHaveBeenCalledWith ( 'pk' ) ;
501
+ expect ( fromPrivate ) . toHaveBeenCalledWith ( mockKey ) ;
491
502
492
503
expect ( randomBytes ) . toHaveBeenNthCalledWith ( 1 , 32 ) ;
493
504
@@ -511,10 +522,10 @@ describe('AccountTest', () => {
511
522
keccak256 . mockReturnValueOnce ( '0xmac' ) ;
512
523
513
524
expect ( ( ) => {
514
- Account . fromPrivateKey ( 'pk' ) . toV3Keystore ( 'password' , options ) ;
525
+ Account . fromPrivateKey ( mockKey ) . toV3Keystore ( 'password' , options ) ;
515
526
} ) . toThrow ( 'Unsupported cipher' ) ;
516
527
517
- expect ( fromPrivate ) . toHaveBeenCalledWith ( 'pk' ) ;
528
+ expect ( fromPrivate ) . toHaveBeenCalledWith ( mockKey ) ;
518
529
519
530
expect ( randomBytes ) . toHaveBeenNthCalledWith ( 1 , 32 ) ;
520
531
0 commit comments