2023-03-06 18:48:28 +01:00
import { isValidMnemonic } from '@ethersproject/hdnode' ;
import {
bufferToHex ,
getBinarySize ,
isValidPrivate ,
toBuffer ,
} from 'ethereumjs-util' ;
2021-02-04 19:15:23 +01:00
import Wallet from 'ethereumjs-wallet' ;
import importers from 'ethereumjs-wallet/thirdparty' ;
2023-03-06 18:48:28 +01:00
import log from 'loglevel' ;
2022-09-22 17:04:24 +02:00
import { stripHexPrefix } from '../../../shared/modules/hexstring-utils' ;
2023-03-06 18:48:28 +01:00
import { addHexPrefix } from '../lib/util' ;
2017-01-19 00:17:08 +01:00
const accountImporter = {
2023-01-20 21:20:18 +01:00
async importAccount ( strategy , args ) {
const importer = this . strategies [ strategy ] ;
const privateKeyHex = importer ( ... args ) ;
return privateKeyHex ;
2017-01-19 00:17:08 +01:00
} ,
strategies : {
'Private Key' : ( privateKey ) => {
2018-06-05 22:44:03 +02:00
if ( ! privateKey ) {
2023-03-06 18:48:28 +01:00
throw new Error ( 'Cannot import an empty key.' ) ; // It should never get here, because this should be stopped in the UI
}
// Check if the user has entered an SRP by mistake instead of a private key
if ( isValidMnemonic ( privateKey . trim ( ) ) ) {
throw new Error ( ` t('importAccountErrorIsSRP') ` ) ;
2018-06-05 22:44:03 +02:00
}
2018-06-11 23:58:05 +02:00
2023-03-06 18:48:28 +01:00
const trimmedPrivateKey = privateKey . replace ( /\s+/gu , '' ) ; // Remove all whitespace
2018-06-11 23:58:05 +02:00
2023-03-06 18:48:28 +01:00
const prefixedPrivateKey = addHexPrefix ( trimmedPrivateKey ) ;
let buffer ;
try {
buffer = toBuffer ( prefixedPrivateKey ) ;
} catch ( e ) {
throw new Error ( ` t('importAccountErrorNotHexadecimal') ` ) ;
}
try {
if (
! isValidPrivate ( buffer ) ||
getBinarySize ( prefixedPrivateKey ) !== 64 + '0x' . length // Fixes issue #17719 -- isValidPrivate() will let a key of 63 hex digits through without complaining, this line ensures 64 hex digits + '0x' = 66 digits
) {
throw new Error ( ` t('importAccountErrorNotAValidPrivateKey') ` ) ;
}
} catch ( e ) {
throw new Error ( ` t('importAccountErrorNotAValidPrivateKey') ` ) ;
2018-06-11 23:58:05 +02:00
}
2023-03-06 18:48:28 +01:00
const strippedPrivateKey = stripHexPrefix ( prefixedPrivateKey ) ;
return strippedPrivateKey ;
2017-01-19 00:17:08 +01:00
} ,
'JSON File' : ( input , password ) => {
2021-02-04 19:15:23 +01:00
let wallet ;
2017-01-19 01:45:39 +01:00
try {
2021-02-04 19:15:23 +01:00
wallet = importers . fromEtherWallet ( input , password ) ;
2017-01-19 01:45:39 +01:00
} catch ( e ) {
2021-02-04 19:15:23 +01:00
log . debug ( 'Attempt to import as EtherWallet format failed, trying V3' ) ;
wallet = Wallet . fromV3 ( input , password , true ) ;
2017-01-19 01:45:39 +01:00
}
2021-02-04 19:15:23 +01:00
return walletToPrivateKey ( wallet ) ;
2017-01-19 00:17:08 +01:00
} ,
} ,
2021-02-04 19:15:23 +01:00
} ;
2017-01-19 00:17:08 +01:00
2020-11-03 00:41:28 +01:00
function walletToPrivateKey ( wallet ) {
2021-02-04 19:15:23 +01:00
const privateKeyBuffer = wallet . getPrivateKey ( ) ;
2021-04-16 17:05:13 +02:00
return bufferToHex ( privateKeyBuffer ) ;
2017-01-19 00:17:08 +01:00
}
2021-02-04 19:15:23 +01:00
export default accountImporter ;