mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
92971d3c87
* Update eslint-plugin-import version * Convert JS files to use ESM * Update ESLint rules to check imports * Fix test:unit:global command env * Cleanup mock-dev script
57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
import Wallet from 'ethereumjs-wallet'
|
|
import importers from 'ethereumjs-wallet/thirdparty'
|
|
import ethUtil from '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) => {
|
|
if (!privateKey) {
|
|
throw new Error('Cannot import an empty key.')
|
|
}
|
|
|
|
const prefixed = ethUtil.addHexPrefix(privateKey)
|
|
const buffer = ethUtil.toBuffer(prefixed)
|
|
|
|
if (!ethUtil.isValidPrivate(buffer)) {
|
|
throw new Error('Cannot import invalid private key.')
|
|
}
|
|
|
|
const stripped = ethUtil.stripHexPrefix(prefixed)
|
|
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)
|
|
}
|
|
|
|
export default accountImporter
|