From 2ea77fc00660f4e0a543090dc2215dbc931e6c92 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 16 Mar 2016 13:13:12 +0000 Subject: [PATCH] Be strict in '.fromV3()' and only use lowercase field names --- index.js | 15 +++++++-------- test/index.js | 6 +++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 712a97f..2509891 100644 --- a/index.js +++ b/index.js @@ -137,7 +137,6 @@ Wallet.fromV1 = function (input, password) { var ciphertext = new Buffer(json.Crypto.CipherText, 'hex') var mac = ethUtil.sha3(Buffer.concat([ derivedKey.slice(16, 32), ciphertext ])) - console.log(mac, json.Crypto.MAC) if (mac.toString('hex') !== json.Crypto.MAC) { throw new Error('Key derivation failed - possibly wrong passphrase') @@ -160,13 +159,13 @@ Wallet.fromV3 = function (input, password) { var derivedKey var kdfparams - if (json.Crypto.kdf === 'scrypt') { - kdfparams = json.Crypto.kdfparams + if (json.crypto.kdf === 'scrypt') { + kdfparams = json.crypto.kdfparams // FIXME: support progress reporting callback derivedKey = scryptsy(new Buffer(password), new Buffer(kdfparams.salt, 'hex'), kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen) - } else if (json.Crypto.kdf === 'pbkdf2') { - kdfparams = json.Crypto.kdfparams + } else if (json.crypto.kdf === 'pbkdf2') { + kdfparams = json.crypto.kdfparams if (kdfparams.prf !== 'hmac-sha256') { throw new Error('Unsupported parameters to PBKDF2') @@ -177,14 +176,14 @@ Wallet.fromV3 = function (input, password) { throw new Error('Unsupported key derivation scheme') } - var ciphertext = new Buffer(json.Crypto.ciphertext, 'hex') + var ciphertext = new Buffer(json.crypto.ciphertext, 'hex') var mac = ethUtil.sha3(Buffer.concat([ derivedKey.slice(16, 32), ciphertext ])) - if (mac.toString('hex') !== json.Crypto.mac) { + if (mac.toString('hex') !== json.crypto.mac) { throw new Error('Key derivation failed - possibly wrong passphrase') } - var decipher = crypto.createDecipheriv(json.Crypto.cipher, derivedKey.slice(0, 16), new Buffer(json.Crypto.cipherparams.iv, 'hex')) + var decipher = crypto.createDecipheriv(json.crypto.cipher, derivedKey.slice(0, 16), new Buffer(json.crypto.cipherparams.iv, 'hex')) var seed = decipherBuffer(decipher, ciphertext, 'hex') // FIXME: Remove PKCS#7 padding here? diff --git a/test/index.js b/test/index.js index 32e6d2c..39899a4 100644 --- a/test/index.js +++ b/test/index.js @@ -92,18 +92,18 @@ describe('.fromV1()', function () { describe('.fromV3()', function () { it('should work with PBKDF2', function () { - var w = '{"Crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"6087dab2f9fdbbfaddc31a909735c1e6"},"ciphertext":"5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46","kdf":"pbkdf2","kdfparams":{"c":262144,"dklen":32,"prf":"hmac-sha256","salt":"ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"},"mac":"517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2"},"id":"3198bc9c-6672-5ab3-d995-4942343ae5b6","version":3}' + var w = '{"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"6087dab2f9fdbbfaddc31a909735c1e6"},"ciphertext":"5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46","kdf":"pbkdf2","kdfparams":{"c":262144,"dklen":32,"prf":"hmac-sha256","salt":"ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"},"mac":"517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2"},"id":"3198bc9c-6672-5ab3-d995-4942343ae5b6","version":3}' var wallet = Wallet.fromV3(w, 'testpassword') assert.equal(wallet.getAddressString(), '0x008aeeda4d805471df9b2a5b0f38a0c3bcba786b') }) it('should work with Scrypt', function () { - var sample = '{"address":"2f91eb73a6cd5620d7abb50889f24eea7a6a4feb","Crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"a2bc4f71e8445d64ceebd1247079fbd8"},"ciphertext":"6b9ab7954c9066fa1e54e04e2c527c7d78a77611d5f84fede1bd61ab13c51e3e","kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"r":1,"p":8,"salt":"caf551e2b7ec12d93007e528093697a4c68e8a50e663b2a929754a8085d9ede4"},"mac":"506cace9c5c32544d39558025cb3bf23ed94ba2626e5338c82e50726917e1a15"},"id":"1b3cad9b-fa7b-4817-9022-d5e598eb5fe3","version":3}' + var sample = '{"address":"2f91eb73a6cd5620d7abb50889f24eea7a6a4feb","crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"a2bc4f71e8445d64ceebd1247079fbd8"},"ciphertext":"6b9ab7954c9066fa1e54e04e2c527c7d78a77611d5f84fede1bd61ab13c51e3e","kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"r":1,"p":8,"salt":"caf551e2b7ec12d93007e528093697a4c68e8a50e663b2a929754a8085d9ede4"},"mac":"506cace9c5c32544d39558025cb3bf23ed94ba2626e5338c82e50726917e1a15"},"id":"1b3cad9b-fa7b-4817-9022-d5e598eb5fe3","version":3}' var wallet = Wallet.fromV3(sample, 'testtest') this.timeout(180000) // 3minutes assert.equal(wallet.getAddressString(), '0x2f91eb73a6cd5620d7abb50889f24eea7a6a4feb') }) it('should work with \'unencrypted\' wallets', function () { - var w = '{"address":"a9886ac7489ecbcbd79268a79ef00d940e5fe1f2","Crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"c542cf883299b5b0a29155091054028d"},"ciphertext":"0a83c77235840cffcfcc5afe5908f2d7f89d7d54c4a796dfe2f193e90413ee9d","kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"r":1,"p":8,"salt":"699f7bf5f6985068dfaaff9db3b06aea8fe3dd3140b3addb4e60620ee97a0316"},"mac":"613fed2605240a2ff08b8d93ccc48c5b3d5023b7088189515d70df41d65f44de"},"id":"0edf817a-ee0e-4e25-8314-1f9e88a60811","version":3}' + var w = '{"address":"a9886ac7489ecbcbd79268a79ef00d940e5fe1f2","crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"c542cf883299b5b0a29155091054028d"},"ciphertext":"0a83c77235840cffcfcc5afe5908f2d7f89d7d54c4a796dfe2f193e90413ee9d","kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"r":1,"p":8,"salt":"699f7bf5f6985068dfaaff9db3b06aea8fe3dd3140b3addb4e60620ee97a0316"},"mac":"613fed2605240a2ff08b8d93ccc48c5b3d5023b7088189515d70df41d65f44de"},"id":"0edf817a-ee0e-4e25-8314-1f9e88a60811","version":3}' var wallet = Wallet.fromV3(w, '') this.timeout(180000) // 3minutes assert.equal(wallet.getAddressString(), '0xa9886ac7489ecbcbd79268a79ef00d940e5fe1f2')