mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-27 12:56:01 +01:00
5d8a3dd99b
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
46 lines
1.1 KiB
JavaScript
46 lines
1.1 KiB
JavaScript
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
|