From b52346388b8d4518ffb2eb34236c6d17579085f3 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 18 Jan 2017 15:17:08 -0800 Subject: Added new modular private key import system 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! --- app/scripts/account-import-strategies/index.js | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 app/scripts/account-import-strategies/index.js (limited to 'app/scripts/account-import-strategies') diff --git a/app/scripts/account-import-strategies/index.js b/app/scripts/account-import-strategies/index.js new file mode 100644 index 000000000..8f4456cdf --- /dev/null +++ b/app/scripts/account-import-strategies/index.js @@ -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 -- cgit From 5d8a3dd99b0cebad48e2fdcc4c407d7d89d4717c Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 18 Jan 2017 16:45:39 -0800 Subject: Add ability to import v3 JSON wallets There is now a menu item labeled "JSON File" for importing, and it can digest either: - v1 MyEtherWallet JSON files - v3 Account files (used by Geth, Mist, and MyEtherWallet). Fixes #715 --- app/scripts/account-import-strategies/index.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'app/scripts/account-import-strategies') diff --git a/app/scripts/account-import-strategies/index.js b/app/scripts/account-import-strategies/index.js index 8f4456cdf..d5124eb7f 100644 --- a/app/scripts/account-import-strategies/index.js +++ b/app/scripts/account-import-strategies/index.js @@ -7,8 +7,7 @@ const accountImporter = { importAccount(strategy, args) { try { const importer = this.strategies[strategy] - const wallet = importer.apply(null, args) - const privateKeyHex = walletToPrivateKey(wallet) + const privateKeyHex = importer.apply(null, args) return Promise.resolve(privateKeyHex) } catch (e) { return Promise.reject(e) @@ -18,11 +17,20 @@ const accountImporter = { strategies: { 'Private Key': (privateKey) => { const stripped = ethUtil.stripHexPrefix(privateKey) - const buffer = new Buffer(stripped, 'hex') - return Wallet.fromPrivateKey(buffer) + return stripped }, 'JSON File': (input, password) => { - const wallet = importers.fromEtherWallet(input, password) + let wallet + try { + wallet = importers.fromEtherWallet(input, password) + } catch (e) { + console.log('Attempt to import as EtherWallet format failed, trying V3...') + } + + if (!wallet) { + wallet = Wallet.fromV3(input, password, true) + } + return walletToPrivateKey(wallet) }, }, -- cgit