Now any strategy for importing a private key that can be described as a pure function can be very easily turned into a MetaMask import strategy. I've created a generic and reusable UI action called `importNewAccount(strategy, args)`. The `strategy` is a unique identifier defined in `app/scripts/account-import-strategies`, and the `args` will be passed to the member of the `strategies` array whose key matches the strategy string. Strategies return private key hex strings, and are used by the metamask-controller to create a new keyring, and select that new account, before calling back. This also implements @frankiebee's idea of showing the imported account when it's been imported (my oversight!). This commit only moves us to this architecture, keeping feature parity for private key import, but has some untested code for importing geth-style JSON files as well!feature/default_network_editable
parent
28212d167c
commit
b52346388b
@ -0,0 +1,37 @@ |
|||||||
|
const Wallet = require('ethereumjs-wallet') |
||||||
|
const importers = require('ethereumjs-wallet/thirdparty') |
||||||
|
const ethUtil = require('ethereumjs-util') |
||||||
|
|
||||||
|
const accountImporter = { |
||||||
|
|
||||||
|
importAccount(strategy, args) { |
||||||
|
try { |
||||||
|
const importer = this.strategies[strategy] |
||||||
|
const wallet = importer.apply(null, args) |
||||||
|
const privateKeyHex = walletToPrivateKey(wallet) |
||||||
|
return Promise.resolve(privateKeyHex) |
||||||
|
} catch (e) { |
||||||
|
return Promise.reject(e) |
||||||
|
} |
||||||
|
}, |
||||||
|
|
||||||
|
strategies: { |
||||||
|
'Private Key': (privateKey) => { |
||||||
|
const stripped = ethUtil.stripHexPrefix(privateKey) |
||||||
|
const buffer = new Buffer(stripped, 'hex') |
||||||
|
return Wallet.fromPrivateKey(buffer) |
||||||
|
}, |
||||||
|
'JSON File': (input, password) => { |
||||||
|
const wallet = importers.fromEtherWallet(input, password) |
||||||
|
return walletToPrivateKey(wallet) |
||||||
|
}, |
||||||
|
}, |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
function walletToPrivateKey (wallet) { |
||||||
|
const privateKeyBuffer = wallet.getPrivateKey() |
||||||
|
return ethUtil.bufferToHex(privateKeyBuffer) |
||||||
|
} |
||||||
|
|
||||||
|
module.exports = accountImporter |
Loading…
Reference in new issue