@@ -4,12 +4,19 @@ var Wallet = require('../')
4
4
var Thirdparty = require ( '../thirdparty.js' )
5
5
var ethUtil = require ( 'ethereumjs-util' )
6
6
7
- var fixturekey = Buffer . from ( 'efca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378' , 'hex' )
8
- var fixturewallet = Wallet . fromPrivateKey ( fixturekey )
7
+ var fixturePrivateKey = 'efca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378'
8
+ var fixturePrivateKeyStr = '0x' + fixturePrivateKey
9
+ var fixturePrivateKeyBuffer = Buffer . from ( fixturePrivateKey , 'hex' )
10
+
11
+ var fixturePublicKey = '5d4392f450262b276652c1fc037606abac500f3160830ce9df53aa70d95ce7cfb8b06010b2f3691c78c65c21eb4cf3dfdbfc0745d89b664ee10435bb3a0f906c'
12
+ var fixturePublicKeyStr = '0x' + fixturePublicKey
13
+ var fixturePublicKeyBuffer = Buffer . from ( fixturePublicKey , 'hex' )
14
+
15
+ var fixtureWallet = Wallet . fromPrivateKey ( fixturePrivateKeyBuffer )
9
16
10
17
describe ( '.getPrivateKey()' , function ( ) {
11
18
it ( 'should work' , function ( ) {
12
- assert . equal ( fixturewallet . getPrivateKey ( ) . toString ( 'hex' ) , 'efca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378' )
19
+ assert . equal ( fixtureWallet . getPrivateKey ( ) . toString ( 'hex' ) , fixturePrivateKey )
13
20
} )
14
21
it ( 'should fail' , function ( ) {
15
22
assert . throws ( function ( ) {
@@ -20,45 +27,45 @@ describe('.getPrivateKey()', function () {
20
27
21
28
describe ( '.getPrivateKeyString()' , function ( ) {
22
29
it ( 'should work' , function ( ) {
23
- assert . equal ( fixturewallet . getPrivateKeyString ( ) , '0xefca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378' )
30
+ assert . equal ( fixtureWallet . getPrivateKeyString ( ) , fixturePrivateKeyStr )
24
31
} )
25
32
} )
26
33
27
34
describe ( '.getPublicKey()' , function ( ) {
28
35
it ( 'should work' , function ( ) {
29
- assert . equal ( fixturewallet . getPublicKey ( ) . toString ( 'hex' ) , '5d4392f450262b276652c1fc037606abac500f3160830ce9df53aa70d95ce7cfb8b06010b2f3691c78c65c21eb4cf3dfdbfc0745d89b664ee10435bb3a0f906c' )
36
+ assert . equal ( fixtureWallet . getPublicKey ( ) . toString ( 'hex' ) , fixturePublicKey )
30
37
} )
31
38
} )
32
39
33
40
describe ( '.getPublicKeyString()' , function ( ) {
34
41
it ( 'should work' , function ( ) {
35
- assert . equal ( fixturewallet . getPublicKeyString ( ) , '0x5d4392f450262b276652c1fc037606abac500f3160830ce9df53aa70d95ce7cfb8b06010b2f3691c78c65c21eb4cf3dfdbfc0745d89b664ee10435bb3a0f906c' )
42
+ assert . equal ( fixtureWallet . getPublicKeyString ( ) , fixturePublicKeyStr )
36
43
} )
37
44
} )
38
45
39
46
describe ( '.getAddress()' , function ( ) {
40
47
it ( 'should work' , function ( ) {
41
- assert . equal ( fixturewallet . getAddress ( ) . toString ( 'hex' ) , 'b14ab53e38da1c172f877dbc6d65e4a1b0474c3c' )
48
+ assert . equal ( fixtureWallet . getAddress ( ) . toString ( 'hex' ) , 'b14ab53e38da1c172f877dbc6d65e4a1b0474c3c' )
42
49
} )
43
50
} )
44
51
45
52
describe ( '.getAddressString()' , function ( ) {
46
53
it ( 'should work' , function ( ) {
47
- assert . equal ( fixturewallet . getAddressString ( ) , '0xb14ab53e38da1c172f877dbc6d65e4a1b0474c3c' )
54
+ assert . equal ( fixtureWallet . getAddressString ( ) , '0xb14ab53e38da1c172f877dbc6d65e4a1b0474c3c' )
48
55
} )
49
56
} )
50
57
51
58
describe ( '.getChecksumAddressString()' , function ( ) {
52
59
it ( 'should work' , function ( ) {
53
- assert . equal ( fixturewallet . getChecksumAddressString ( ) , '0xB14Ab53E38DA1C172f877DBC6d65e4a1B0474C3c' )
60
+ assert . equal ( fixtureWallet . getChecksumAddressString ( ) , '0xB14Ab53E38DA1C172f877DBC6d65e4a1B0474C3c' )
54
61
} )
55
62
} )
56
63
57
64
describe ( 'public key only wallet' , function ( ) {
58
- var pubKey = Buffer . from ( '5d4392f450262b276652c1fc037606abac500f3160830ce9df53aa70d95ce7cfb8b06010b2f3691c78c65c21eb4cf3dfdbfc0745d89b664ee10435bb3a0f906c' , 'hex' )
65
+ var pubKey = Buffer . from ( fixturePublicKey , 'hex' )
59
66
it ( '.fromPublicKey() should work' , function ( ) {
60
67
assert . equal ( Wallet . fromPublicKey ( pubKey ) . getPublicKey ( ) . toString ( 'hex' ) ,
61
- '5d4392f450262b276652c1fc037606abac500f3160830ce9df53aa70d95ce7cfb8b06010b2f3691c78c65c21eb4cf3dfdbfc0745d89b664ee10435bb3a0f906c' )
68
+ fixturePublicKey )
62
69
} )
63
70
it ( '.fromPublicKey() should not accept compressed keys in strict mode' , function ( ) {
64
71
assert . throws ( function ( ) {
@@ -112,17 +119,25 @@ describe('.generate()', function () {
112
119
} )
113
120
114
121
describe ( '.generateVanityAddress()' , function ( ) {
115
- it ( 'should generate an account with 000 prefix' , function ( ) {
122
+ it ( 'should generate an account with 000 prefix (object)' , function ( ) {
123
+ this . timeout ( 180000 ) // 3minutes
116
124
var wallet = Wallet . generateVanityAddress ( / ^ 0 0 0 / )
117
125
assert . equal ( wallet . getPrivateKey ( ) . length , 32 )
118
126
assert . equal ( wallet . getAddress ( ) [ 0 ] , 0 )
119
127
assert . equal ( wallet . getAddress ( ) [ 1 ] >>> 4 , 0 )
120
128
} )
129
+ it ( 'should generate an account with 000 prefix (string)' , function ( ) {
130
+ this . timeout ( 180000 ) // 3minutes
131
+ var wallet = Wallet . generateVanityAddress ( '^000' )
132
+ assert . equal ( wallet . getPrivateKey ( ) . length , 32 )
133
+ assert . equal ( wallet . getAddress ( ) [ 0 ] , 0 )
134
+ assert . equal ( wallet . getAddress ( ) [ 1 ] >>> 4 , 0 )
135
+ } )
121
136
} )
122
137
123
138
describe ( '.getV3Filename()' , function ( ) {
124
139
it ( 'should work' , function ( ) {
125
- assert . equal ( fixturewallet . getV3Filename ( 1457917509265 ) , 'UTC--2016-03-14T01-05-09.265Z--b14ab53e38da1c172f877dbc6d65e4a1b0474c3c' )
140
+ assert . equal ( fixtureWallet . getV3Filename ( 1457917509265 ) , 'UTC--2016-03-14T01-05-09.265Z--b14ab53e38da1c172f877dbc6d65e4a1b0474c3c' )
126
141
} )
127
142
} )
128
143
@@ -132,19 +147,25 @@ describe('.toV3()', function () {
132
147
var uuid = Buffer . from ( '7e59dc028d42d09db29aa8a0f862cc81' , 'hex' )
133
148
134
149
it ( 'should work with PBKDF2' , function ( ) {
135
- var key = Buffer . from ( 'efca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378' , 'hex' )
136
- var wallet = Wallet . fromPrivateKey ( key )
137
150
var w = '{"version":3,"id":"7e59dc02-8d42-409d-b29a-a8a0f862cc81","address":"b14ab53e38da1c172f877dbc6d65e4a1b0474c3c","crypto":{"ciphertext":"01ee7f1a3c8d187ea244c92eea9e332ab0bb2b4c902d89bdd71f80dc384da1be","cipherparams":{"iv":"cecacd85e9cb89788b5aab2f93361233"},"cipher":"aes-128-ctr","kdf":"pbkdf2","kdfparams":{"dklen":32,"salt":"dc9e4a98886738bd8aae134a1f89aaa5a502c3fbd10e336136d4d5fe47448ad6","c":262144,"prf":"hmac-sha256"},"mac":"0c02cd0badfebd5e783e0cf41448f84086a96365fc3456716c33641a86ebc7cc"}}'
138
151
// FIXME: just test for ciphertext and mac?
139
- assert . equal ( wallet . toV3String ( 'testtest' , { kdf : 'pbkdf2' , uuid : uuid , salt : salt , iv : iv } ) , w )
152
+ assert . equal ( fixtureWallet . toV3String ( 'testtest' , { kdf : 'pbkdf2' , uuid : uuid , salt : salt , iv : iv } ) , w )
140
153
} )
141
154
it ( 'should work with Scrypt' , function ( ) {
142
- var key = Buffer . from ( 'efca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378' , 'hex' )
143
- var wallet = Wallet . fromPrivateKey ( key )
144
155
var w = '{"version":3,"id":"7e59dc02-8d42-409d-b29a-a8a0f862cc81","address":"b14ab53e38da1c172f877dbc6d65e4a1b0474c3c","crypto":{"ciphertext":"c52682025b1e5d5c06b816791921dbf439afe7a053abb9fac19f38a57499652c","cipherparams":{"iv":"cecacd85e9cb89788b5aab2f93361233"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"dc9e4a98886738bd8aae134a1f89aaa5a502c3fbd10e336136d4d5fe47448ad6","n":262144,"r":8,"p":1},"mac":"27b98c8676dc6619d077453b38db645a4c7c17a3e686ee5adaf53c11ac1b890e"}}'
145
156
this . timeout ( 180000 ) // 3minutes
146
157
// FIXME: just test for ciphertext and mac?
147
- assert . equal ( wallet . toV3String ( 'testtest' , { kdf : 'scrypt' , uuid : uuid , salt : salt , iv : iv } ) , w )
158
+ assert . equal ( fixtureWallet . toV3String ( 'testtest' , { kdf : 'scrypt' , uuid : uuid , salt : salt , iv : iv } ) , w )
159
+ } )
160
+ it ( 'should work without providing options' , function ( ) {
161
+ this . timeout ( 180000 ) // 3minutes
162
+ assert . equal ( fixtureWallet . toV3 ( 'testtest' ) [ 'version' ] , 3 )
163
+ } )
164
+ it ( 'should fail for unsupported kdf' , function ( ) {
165
+ this . timeout ( 180000 ) // 3minutes
166
+ assert . throws ( function ( ) {
167
+ fixtureWallet . toV3 ( 'testtest' , { kdf : 'superkey' } )
168
+ } , / ^ E r r o r : U n s u p p o r t e d k d f $ / )
148
169
} )
149
170
} )
150
171
@@ -187,6 +208,18 @@ describe('.fromV3()', function () {
187
208
Wallet . fromV3 ( w , 'testpassword' )
188
209
} ) // FIXME: check for assert message(s)
189
210
} )
211
+ it ( 'should fail for wrong version' , function ( ) {
212
+ var w = '{"version":2}'
213
+ assert . throws ( function ( ) {
214
+ Wallet . fromV3 ( w , 'testpassword' )
215
+ } , / ^ E r r o r : N o t a V 3 w a l l e t $ / )
216
+ } )
217
+ it ( 'should fail for wrong kdf' , function ( ) {
218
+ var w = '{"Crypto":{"kdf":"superkey"},"version":3}'
219
+ assert . throws ( function ( ) {
220
+ Wallet . fromV3 ( w , 'testpassword' , true )
221
+ } , / ^ E r r o r : U n s u p p o r t e d k e y d e r i v a t i o n s c h e m e $ / )
222
+ } )
190
223
} )
191
224
192
225
describe ( '.fromEthSale()' , function ( ) {
@@ -235,3 +268,11 @@ describe('.fromQuorumWallet()', function () {
235
268
assert . equal ( wallet . getAddressString ( ) , '0x1b86ccc22e8f137f204a41a23033541242a48815' )
236
269
} )
237
270
} )
271
+
272
+ describe ( 'raw new Wallet() init' , function ( ) {
273
+ it ( 'should fail when both priv and pub key provided' , function ( ) {
274
+ assert . throws ( function ( ) {
275
+ new Wallet ( fixturePrivateKeyBuffer , fixturePublicKeyBuffer ) // eslint-disable-line
276
+ } , / ^ E r r o r : C a n n o t s u p p l y b o t h a p r i v a t e a n d a p u b l i c k e y t o t h e c o n s t r u c t o r $ / )
277
+ } )
278
+ } )
0 commit comments