Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 688bd06

Browse files
authored
Merge pull request #27 from ethereumjs/improve-test-coverage
Improve test coverage
2 parents 3a96b6e + 18ded5b commit 688bd06

File tree

1 file changed

+60
-19
lines changed

1 file changed

+60
-19
lines changed

test/index.js

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ var Wallet = require('../')
44
var Thirdparty = require('../thirdparty.js')
55
var ethUtil = require('ethereumjs-util')
66

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)
916

1017
describe('.getPrivateKey()', function () {
1118
it('should work', function () {
12-
assert.equal(fixturewallet.getPrivateKey().toString('hex'), 'efca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378')
19+
assert.equal(fixtureWallet.getPrivateKey().toString('hex'), fixturePrivateKey)
1320
})
1421
it('should fail', function () {
1522
assert.throws(function () {
@@ -20,45 +27,45 @@ describe('.getPrivateKey()', function () {
2027

2128
describe('.getPrivateKeyString()', function () {
2229
it('should work', function () {
23-
assert.equal(fixturewallet.getPrivateKeyString(), '0xefca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378')
30+
assert.equal(fixtureWallet.getPrivateKeyString(), fixturePrivateKeyStr)
2431
})
2532
})
2633

2734
describe('.getPublicKey()', function () {
2835
it('should work', function () {
29-
assert.equal(fixturewallet.getPublicKey().toString('hex'), '5d4392f450262b276652c1fc037606abac500f3160830ce9df53aa70d95ce7cfb8b06010b2f3691c78c65c21eb4cf3dfdbfc0745d89b664ee10435bb3a0f906c')
36+
assert.equal(fixtureWallet.getPublicKey().toString('hex'), fixturePublicKey)
3037
})
3138
})
3239

3340
describe('.getPublicKeyString()', function () {
3441
it('should work', function () {
35-
assert.equal(fixturewallet.getPublicKeyString(), '0x5d4392f450262b276652c1fc037606abac500f3160830ce9df53aa70d95ce7cfb8b06010b2f3691c78c65c21eb4cf3dfdbfc0745d89b664ee10435bb3a0f906c')
42+
assert.equal(fixtureWallet.getPublicKeyString(), fixturePublicKeyStr)
3643
})
3744
})
3845

3946
describe('.getAddress()', function () {
4047
it('should work', function () {
41-
assert.equal(fixturewallet.getAddress().toString('hex'), 'b14ab53e38da1c172f877dbc6d65e4a1b0474c3c')
48+
assert.equal(fixtureWallet.getAddress().toString('hex'), 'b14ab53e38da1c172f877dbc6d65e4a1b0474c3c')
4249
})
4350
})
4451

4552
describe('.getAddressString()', function () {
4653
it('should work', function () {
47-
assert.equal(fixturewallet.getAddressString(), '0xb14ab53e38da1c172f877dbc6d65e4a1b0474c3c')
54+
assert.equal(fixtureWallet.getAddressString(), '0xb14ab53e38da1c172f877dbc6d65e4a1b0474c3c')
4855
})
4956
})
5057

5158
describe('.getChecksumAddressString()', function () {
5259
it('should work', function () {
53-
assert.equal(fixturewallet.getChecksumAddressString(), '0xB14Ab53E38DA1C172f877DBC6d65e4a1B0474C3c')
60+
assert.equal(fixtureWallet.getChecksumAddressString(), '0xB14Ab53E38DA1C172f877DBC6d65e4a1B0474C3c')
5461
})
5562
})
5663

5764
describe('public key only wallet', function () {
58-
var pubKey = Buffer.from('5d4392f450262b276652c1fc037606abac500f3160830ce9df53aa70d95ce7cfb8b06010b2f3691c78c65c21eb4cf3dfdbfc0745d89b664ee10435bb3a0f906c', 'hex')
65+
var pubKey = Buffer.from(fixturePublicKey, 'hex')
5966
it('.fromPublicKey() should work', function () {
6067
assert.equal(Wallet.fromPublicKey(pubKey).getPublicKey().toString('hex'),
61-
'5d4392f450262b276652c1fc037606abac500f3160830ce9df53aa70d95ce7cfb8b06010b2f3691c78c65c21eb4cf3dfdbfc0745d89b664ee10435bb3a0f906c')
68+
fixturePublicKey)
6269
})
6370
it('.fromPublicKey() should not accept compressed keys in strict mode', function () {
6471
assert.throws(function () {
@@ -112,17 +119,25 @@ describe('.generate()', function () {
112119
})
113120

114121
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
116124
var wallet = Wallet.generateVanityAddress(/^000/)
117125
assert.equal(wallet.getPrivateKey().length, 32)
118126
assert.equal(wallet.getAddress()[0], 0)
119127
assert.equal(wallet.getAddress()[1] >>> 4, 0)
120128
})
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+
})
121136
})
122137

123138
describe('.getV3Filename()', function () {
124139
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')
126141
})
127142
})
128143

@@ -132,19 +147,25 @@ describe('.toV3()', function () {
132147
var uuid = Buffer.from('7e59dc028d42d09db29aa8a0f862cc81', 'hex')
133148

134149
it('should work with PBKDF2', function () {
135-
var key = Buffer.from('efca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378', 'hex')
136-
var wallet = Wallet.fromPrivateKey(key)
137150
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"}}'
138151
// 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)
140153
})
141154
it('should work with Scrypt', function () {
142-
var key = Buffer.from('efca4cdd31923b50f4214af5d2ae10e7ac45a5019e9431cc195482d707485378', 'hex')
143-
var wallet = Wallet.fromPrivateKey(key)
144155
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"}}'
145156
this.timeout(180000) // 3minutes
146157
// 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+
}, /^Error: Unsupported kdf$/)
148169
})
149170
})
150171

@@ -187,6 +208,18 @@ describe('.fromV3()', function () {
187208
Wallet.fromV3(w, 'testpassword')
188209
}) // FIXME: check for assert message(s)
189210
})
211+
it('should fail for wrong version', function () {
212+
var w = '{"version":2}'
213+
assert.throws(function () {
214+
Wallet.fromV3(w, 'testpassword')
215+
}, /^Error: Not a V3 wallet$/)
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+
}, /^Error: Unsupported key derivation scheme$/)
222+
})
190223
})
191224

192225
describe('.fromEthSale()', function () {
@@ -235,3 +268,11 @@ describe('.fromQuorumWallet()', function () {
235268
assert.equal(wallet.getAddressString(), '0x1b86ccc22e8f137f204a41a23033541242a48815')
236269
})
237270
})
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+
}, /^Error: Cannot supply both a private and a public key to the constructor$/)
277+
})
278+
})

0 commit comments

Comments
 (0)