import { BaseEthereumPayments } from './BaseEthereumPayments' ; import { HdEthereumPaymentsConfig , EthereumSignatory } from './types' ; import { deriveSignatory } from './bip44' ; import { Payport } from '@bitaccess/coinlib-common' ; import { omit } from 'lodash' ; import { PUBLIC_CONFIG_OMIT_FIELDS } from './constants' ; export class HdEthereumPayments extends BaseEthereumPayments < HdEthereumPaymentsConfig > { readonly xprv : string | null ; readonly xpub : string ; constructor ( config ) { super ( config ) ; try { this . xprv = '' ; this . xpub = '' ; if ( this . isValidXpub ( config . hdKey ) ) { this . xpub = config . hdKey ; } else if ( this . isValidXprv ( config . hdKey ) ) { this . xprv = config . hdKey ; this . xpub = deriveSignatory ( config . hdKey , 0 ) . xkeys . xpub ; } else { throw new Error ( config . hdKey ) ; } } catch ( e ) { throw new Error ( `Account must be a valid xprv or xpub: ${ e . message }` ) ; } } static generateNewKeys ( ) { return deriveSignatory ( ) ; } getXpub ( ) { return this . xpub ; } getPublicConfig ( ) { return { ... omit ( this . getFullConfig ( ) , PUBLIC_CONFIG_OMIT_FIELDS ) , depositKeyIndex : this . depositKeyIndex , hdKey : this . getXpub ( ) , } ; } getAccountId ( index ) { return this . getXpub ( ) ; } getAccountIds ( ) { return [ this . getXpub ( ) ] ; } async getPayport ( index ) { const { address } = deriveSignatory ( this . getXpub ( ) , index ) ; if ( ! this . isValidAddress ( address ) ) { throw new Error ( `Cannot get address ${ index } - validation failed for derived address` ) ; } return { address } ; } async getPrivateKey ( index ) { if ( ! this . xprv ) { throw new Error ( `Cannot get private key ${ index } - HdEthereumPayments was created with an xpub` ) ; } return deriveSignatory ( deriveSignatory ( this . xprv , 0 ) . xkeys . xprv , index ) . keys . prv ; } } export default HdEthereumPayments ;