import Bip32KeyDerivation from './bip32-key-derivation' ; import Bip39Mnemonic from './bip39-mnemonic' ; import Ed25519 from './ed25519' ; import NanoAddress from './nano-address' ; import Signer from './signer' ; import Convert from './util/convert' ; export default class AddressImporter { param { string } mnemonic param { string } seedPassword returns { Wallet } fromMnemonic ( mnemonic , seedPassword = '' ) { const bip39 = new Bip39Mnemonic ( seedPassword ) ; if ( ! bip39 . validateMnemonic ( mnemonic ) ) { throw new Error ( 'Invalid mnemonic phrase' ) ; } const seed = bip39 . mnemonicToSeed ( mnemonic ) ; const accounts = this . accounts ( seed , 0 , 0 ) ; return { mnemonic , seed , accounts , } ; } param { string } mnemonic returns { Wallet } fromLegacyMnemonic ( mnemonic ) { const bip39 = new Bip39Mnemonic ( ) ; if ( ! bip39 . validateMnemonic ( mnemonic ) ) { throw new Error ( 'Invalid mnemonic phrase' ) ; } const seed = bip39 . mnemonicToLegacySeed ( mnemonic ) ; return this . fromLegacySeed ( seed , 0 , 0 , mnemonic ) ; } param mnemonic { string } validateMnemonic ( mnemonic ) { const bip39 = new Bip39Mnemonic ( ) ; return bip39 . validateMnemonic ( mnemonic ) ; } param { string } seed param { number } from param { number } to returns { Wallet } fromSeed ( seed , from = 0 , to = 0 ) { if ( seed . length !== 128 ) { throw new Error ( 'Invalid seed length, must be a 128 byte hexadecimal string' ) ; } if ( ! /^[0-9a-fA-F]+$/i . test ( seed ) ) { throw new Error ( 'Seed is not a valid hexadecimal string' ) ; } const accounts = this . accounts ( seed , from , to ) ; return { mnemonic : undefined , seed , accounts , } ; } param { string } seed param { number } from param { number } to returns { Wallet } fromLegacySeed ( seed , from = 0 , to = 0 , mnemonic ? ) { if ( seed . length !== 64 ) { throw new Error ( 'Invalid seed length, must be a 64 byte hexadecimal string' ) ; } if ( ! /^[0-9a-fA-F]+$/i . test ( seed ) ) { throw new Error ( 'Seed is not a valid hexadecimal string' ) ; } const accounts = this . legacyAccounts ( seed , from , to ) ; return { mnemonic : mnemonic || new Bip39Mnemonic ( ) . deriveMnemonic ( seed ) , seed , accounts , } ; } param { string } seed param { number } from param { number } to private accounts ( seed , from , to ) { const accounts = [ ] ; for ( let i = from ; i <= to ; i ++ ) { const bip44 = new Bip32KeyDerivation ( `44'/165'/${ i }'` , seed ) ; const privateKey = bip44 . derivePath ( ) . key ; const ed25519 = new Ed25519 ( ) ; const keyPair = ed25519 . generateKeys ( privateKey ) ; const nano = new NanoAddress ( ) ; const address = nano . deriveAddress ( keyPair . publicKey ) ; accounts . push ( { accountIndex : i , privateKey : keyPair . privateKey , publicKey : keyPair . publicKey , address , } ) ; } return accounts ; } param { string } seed param { number } from param { number } to private legacyAccounts ( seed , from , to ) { const signer = new Signer ( ) ; const accounts = [ ] ; for ( let i = from ; i <= to ; i ++ ) { const privateKey = Convert . ab2hex ( signer . generateHash ( [ seed , Convert . dec2hex ( i , 4 ) ] ) ) ; const ed25519 = new Ed25519 ( ) ; const keyPair = ed25519 . generateKeys ( privateKey ) ; const nano = new NanoAddress ( ) ; const address = nano . deriveAddress ( keyPair . publicKey ) ; accounts . push ( { accountIndex : i , privateKey : keyPair . privateKey , publicKey : keyPair . publicKey , address , } ) ; } return accounts ; } } export interface Wallet { mnemonic : string ; seed : string ; accounts : Account [ ] ; } export interface Account { accountIndex : number ; privateKey : string ; publicKey : string ; address : string ; }