diff options
author | Dan Finlay <dan@danfinlay.com> | 2017-02-16 08:09:16 +0800 |
---|---|---|
committer | Dan Finlay <dan@danfinlay.com> | 2017-02-16 08:09:16 +0800 |
commit | 6d103dc1e7eeb9a1f55e4387fcc1fe194c7eb4cf (patch) | |
tree | 47906b974dee789ffd544b4c85da67e6f2323016 /app/scripts/account-import-strategies | |
parent | 245e779f37763ce0633119c257877706d0bf3554 (diff) | |
parent | 943bcec0d702b2c70b323000ed25d3c425e2a44f (diff) | |
download | tangerine-wallet-browser-6d103dc1e7eeb9a1f55e4387fcc1fe194c7eb4cf.tar.gz tangerine-wallet-browser-6d103dc1e7eeb9a1f55e4387fcc1fe194c7eb4cf.tar.zst tangerine-wallet-browser-6d103dc1e7eeb9a1f55e4387fcc1fe194c7eb4cf.zip |
Merge branch 'kumavis-patch-1' of github.com:MetaMask/metamask-plugin into kumavis-patch-1
Diffstat (limited to 'app/scripts/account-import-strategies')
-rw-r--r-- | app/scripts/account-import-strategies/index.js | 45 |
1 files changed, 45 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..d5124eb7f --- /dev/null +++ b/app/scripts/account-import-strategies/index.js @@ -0,0 +1,45 @@ +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 privateKeyHex = importer.apply(null, args) + return Promise.resolve(privateKeyHex) + } catch (e) { + return Promise.reject(e) + } + }, + + strategies: { + 'Private Key': (privateKey) => { + const stripped = ethUtil.stripHexPrefix(privateKey) + return stripped + }, + 'JSON File': (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) + }, + }, + +} + +function walletToPrivateKey (wallet) { + const privateKeyBuffer = wallet.getPrivateKey() + return ethUtil.bufferToHex(privateKeyBuffer) +} + +module.exports = accountImporter |