aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorDan Finlay <dan@danfinlay.com>2017-01-19 07:17:08 +0800
committerDan Finlay <dan@danfinlay.com>2017-01-19 07:20:48 +0800
commitb52346388b8d4518ffb2eb34236c6d17579085f3 (patch)
treed5748386adc015b8ab1d9e28d1b5c68fab5c728e /app
parent28212d167cbd201f78e0253cf9c6fb676d71cb7a (diff)
downloadtangerine-wallet-browser-b52346388b8d4518ffb2eb34236c6d17579085f3.tar
tangerine-wallet-browser-b52346388b8d4518ffb2eb34236c6d17579085f3.tar.gz
tangerine-wallet-browser-b52346388b8d4518ffb2eb34236c6d17579085f3.tar.bz2
tangerine-wallet-browser-b52346388b8d4518ffb2eb34236c6d17579085f3.tar.lz
tangerine-wallet-browser-b52346388b8d4518ffb2eb34236c6d17579085f3.tar.xz
tangerine-wallet-browser-b52346388b8d4518ffb2eb34236c6d17579085f3.tar.zst
tangerine-wallet-browser-b52346388b8d4518ffb2eb34236c6d17579085f3.zip
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!
Diffstat (limited to 'app')
-rw-r--r--app/scripts/account-import-strategies/index.js37
-rw-r--r--app/scripts/metamask-controller.js11
2 files changed, 48 insertions, 0 deletions
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
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js
index 629216e42..7084bbc2d 100644
--- a/app/scripts/metamask-controller.js
+++ b/app/scripts/metamask-controller.js
@@ -13,6 +13,7 @@ const extension = require('./lib/extension')
const autoFaucet = require('./lib/auto-faucet')
const nodeify = require('./lib/nodeify')
const IdStoreMigrator = require('./lib/idStore-migrator')
+const accountImporter = require('./account-import-strategies')
const version = require('../manifest.json').version
module.exports = class MetamaskController extends EventEmitter {
@@ -121,6 +122,16 @@ module.exports = class MetamaskController extends EventEmitter {
.then((newState) => { cb(null, newState) })
.catch((reason) => { cb(reason) })
},
+ importAccountWithStrategy: (strategy, args, cb) => {
+ accountImporter.importAccount(strategy, args)
+ .then((privateKey) => {
+ return keyringController.addNewKeyring('Simple Key Pair', [ privateKey ])
+ })
+ .then(keyring => keyring.getAccounts())
+ .then((accounts) => keyringController.setSelectedAccount(accounts[0]))
+ .then(() => { cb(null, keyringController.fullUpdate()) })
+ .catch((reason) => { cb(reason) })
+ },
addNewAccount: nodeify(keyringController.addNewAccount).bind(keyringController),
setSelectedAccount: nodeify(keyringController.setSelectedAccount).bind(keyringController),
saveAccountLabel: nodeify(keyringController.saveAccountLabel).bind(keyringController),