Better fromV1() implementation

This commit is contained in:
Alex Beregszaszi 2016-02-23 21:25:23 +00:00
parent 0f0992cb4c
commit f1f93d6c02
1 changed files with 27 additions and 22 deletions

View File

@ -103,30 +103,35 @@ Wallet.fromPrivateKey = function (priv) {
} }
// https://github.com/ethereum/go-ethereum/wiki/Passphrase-protected-key-store-spec // https://github.com/ethereum/go-ethereum/wiki/Passphrase-protected-key-store-spec
// Let's just transform it to be compatible with V3
// FIXME: this might not be fully correct in all cases
Wallet.fromV1 = function (input, password) { Wallet.fromV1 = function (input, password) {
var json = (typeof input === 'object') ? input : JSON.parse(input) var json = (typeof input === 'object') ? input : JSON.parse(input)
return Wallet.fromV3({
Crypto: { if (json.Version !== '1') {
ciphertext: json.Crypto.CipherText, throw new Error('Not a V1 wallet')
cipherparams: { }
iv: json.Crypto.IV
}, if (json.Crypto.KeyHeader.Kdf !== 'scrypt') {
cipher: 'aes-128-cbc', throw new Error('Unsupported key derivation scheme')
kdf: json.Crypto.KeyHeader.Kdf, }
kdfparams: {
dklen: json.Crypto.KeyHeader.KdfParams.DkLen, var kdfparams = json.Crypto.KeyHeader.KdfParams
n: json.Crypto.KeyHeader.KdfParams.N, var derivedKey = scryptsy(new Buffer(password), new Buffer(json.Crypto.Salt, 'hex'), kdfparams.N, kdfparams.R, kdfparams.P, kdfparams.DkLen)
p: json.Crypto.KeyHeader.KdfParams.P,
r: json.Crypto.KeyHeader.KdfParams.R, var ciphertext = new Buffer(json.Crypto.CipherText, 'hex')
salt: json.Crypto.Salt
}, var mac = ethUtil.sha3(Buffer.concat([ derivedKey.slice(16, 32), ciphertext ]))
mac: json.Crypto.MAC console.log(mac, json.Crypto.MAC)
},
id: json.Id, if (mac.toString('hex') !== json.Crypto.MAC) {
version: 3 throw new Error('Key derivation failed - possibly wrong passphrase')
}, password) }
var decipher = crypto.createDecipheriv('aes-128-cbc', ethUtil.sha3(derivedKey.slice(0, 16)).slice(0, 16), new Buffer(json.Crypto.IV, 'hex'))
var seed = decipherBuffer(decipher, ciphertext)
// FIXME: Remove PKCS#7 padding here?
return new Wallet(seed)
} }
Wallet.fromV3 = function (input, password) { Wallet.fromV3 = function (input, password) {