From 2afc06287dfd1a87bd247234c9a04b92a8394cac Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Thu, 3 Nov 2016 15:40:23 -0700 Subject: Implement private key exporting. --- app/scripts/keyring-controller.js | 9 +++++++-- app/scripts/keyrings/hd.js | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) (limited to 'app') diff --git a/app/scripts/keyring-controller.js b/app/scripts/keyring-controller.js index 505414c88..aa303c43c 100644 --- a/app/scripts/keyring-controller.js +++ b/app/scripts/keyring-controller.js @@ -353,7 +353,6 @@ module.exports = class KeyringController extends EventEmitter { gasMultiplier: configManager.getGasMultiplier() || 1, } - console.log('addUnconfirmedTransaction:', txData) // keep the onTxDoneCb around for after approval/denial (requires user interaction) // This onTxDoneCb fires completion to the Dapp's write operation. @@ -525,7 +524,13 @@ module.exports = class KeyringController extends EventEmitter { } exportAccount(address, cb) { - cb(null, '0xPrivateKey') + try { + const keyring = this.getKeyringForAccount(address) + const privateKey = keyring.exportAccount(normalize(address)) + cb(null, privateKey) + } catch (e) { + cb(e) + } } getNetwork(err) { diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js index d0ebee419..4bfc56c15 100644 --- a/app/scripts/keyrings/hd.js +++ b/app/scripts/keyrings/hd.js @@ -48,6 +48,11 @@ module.exports = class HdKeyring extends EventEmitter { } } + exportAccount(address) { + const wallet = this.getWalletForAccount(address) + return wallet.getPrivateKey().toString('hex') + } + addAccounts(n = 1) { if (!this.root) { this.initFromMnemonic(bip39.generateMnemonic()) @@ -87,7 +92,16 @@ module.exports = class HdKeyring extends EventEmitter { } getWalletForAccount(account) { - return this.wallets.find(w => w.getAddress().toString('hex') === account) + return this.wallets.find((w) => { + const address = w.getAddress().toString('hex') + return ((address === account) || (normalize(address) === account)) + }) } + + +} + +function normalize(address) { + return ethUtil.addHexPrefix(address.toLowerCase()) } -- cgit