2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-11-10 11:27:05 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import scrypt from 'scryptsy';
|
2017-02-09 15:30:57 +01:00
|
|
|
import Transaction from 'ethereumjs-tx';
|
2016-11-10 11:27:05 +01:00
|
|
|
import { pbkdf2Sync } from 'crypto';
|
|
|
|
import { createDecipheriv } from 'browserify-aes';
|
|
|
|
|
2016-12-05 11:47:13 +01:00
|
|
|
import { inHex } from '~/api/format/input';
|
|
|
|
import { sha3 } from '~/api/util/sha3';
|
2016-11-10 11:27:05 +01:00
|
|
|
|
|
|
|
// Adapted from https://github.com/kvhnuke/etherwallet/blob/mercury/app/scripts/myetherwallet.js
|
|
|
|
|
2017-01-09 11:14:36 +01:00
|
|
|
export class Signer {
|
2016-11-10 11:27:05 +01:00
|
|
|
static fromJson (json, password) {
|
2017-01-09 11:14:36 +01:00
|
|
|
return Signer
|
|
|
|
.getSeed(json, password)
|
|
|
|
.then((seed) => {
|
|
|
|
return new Signer(seed);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static getSeed (json, password) {
|
|
|
|
try {
|
|
|
|
const seed = Signer.getSyncSeed(json, password);
|
2017-01-23 13:39:52 +01:00
|
|
|
|
2017-01-09 11:14:36 +01:00
|
|
|
return Promise.resolve(seed);
|
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static getSyncSeed (json, password) {
|
2016-11-10 11:27:05 +01:00
|
|
|
if (json.version !== 3) {
|
|
|
|
throw new Error('Only V3 wallets are supported');
|
|
|
|
}
|
|
|
|
|
|
|
|
const { kdf } = json.crypto;
|
|
|
|
const kdfparams = json.crypto.kdfparams || {};
|
|
|
|
const pwd = Buffer.from(password);
|
|
|
|
const salt = Buffer.from(kdfparams.salt, 'hex');
|
|
|
|
let derivedKey;
|
|
|
|
|
|
|
|
if (kdf === 'scrypt') {
|
|
|
|
derivedKey = scrypt(pwd, salt, kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
|
|
|
|
} else if (kdf === 'pbkdf2') {
|
|
|
|
if (kdfparams.prf !== 'hmac-sha256') {
|
|
|
|
throw new Error('Unsupported parameters to PBKDF2');
|
|
|
|
}
|
2017-01-09 11:14:36 +01:00
|
|
|
|
2016-11-10 11:27:05 +01:00
|
|
|
derivedKey = pbkdf2Sync(pwd, salt, kdfparams.c, kdfparams.dklen, 'sha256');
|
|
|
|
} else {
|
|
|
|
throw new Error('Unsupported key derivation scheme');
|
|
|
|
}
|
|
|
|
|
|
|
|
const ciphertext = Buffer.from(json.crypto.ciphertext, 'hex');
|
2017-01-09 11:14:36 +01:00
|
|
|
const mac = sha3(Buffer.concat([derivedKey.slice(16, 32), ciphertext]));
|
|
|
|
|
2016-11-10 11:27:05 +01:00
|
|
|
if (mac !== inHex(json.crypto.mac)) {
|
2017-01-09 11:14:36 +01:00
|
|
|
throw new Error('Key derivation failed - possibly wrong password');
|
2016-11-10 11:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const decipher = createDecipheriv(
|
|
|
|
json.crypto.cipher,
|
|
|
|
derivedKey.slice(0, 16),
|
|
|
|
Buffer.from(json.crypto.cipherparams.iv, 'hex')
|
|
|
|
);
|
2017-01-09 11:14:36 +01:00
|
|
|
|
2016-11-10 11:27:05 +01:00
|
|
|
let seed = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
|
|
|
|
|
|
while (seed.length < 32) {
|
|
|
|
const nullBuff = Buffer.from([0x00]);
|
2017-01-23 13:39:52 +01:00
|
|
|
|
2016-11-10 11:27:05 +01:00
|
|
|
seed = Buffer.concat([nullBuff, seed]);
|
|
|
|
}
|
|
|
|
|
2017-01-09 11:14:36 +01:00
|
|
|
return seed;
|
2016-11-10 11:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor (seed) {
|
|
|
|
this.seed = seed;
|
|
|
|
}
|
|
|
|
|
2017-03-31 23:36:24 +02:00
|
|
|
signTransactionObject (tx) {
|
|
|
|
tx.sign(this.seed);
|
|
|
|
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|
2016-11-10 11:27:05 +01:00
|
|
|
signTransaction (transaction) {
|
|
|
|
const tx = new Transaction(transaction);
|
2017-01-23 13:39:52 +01:00
|
|
|
|
2017-03-31 23:36:24 +02:00
|
|
|
return inHex(this.signTransactionObject(tx).serialize().toString('hex'));
|
2016-11-10 11:27:05 +01:00
|
|
|
}
|
|
|
|
}
|