import { RadixAccount , RadixIdentity } from '../..' ; import { RadixAddress , RadixAtom } from '../atommodel' ; import { sleep } from '../common/RadixUtil' ; import { radixUniverse } from '../universe/RadixUniverse' ; export interface LedgerApp { getRadixAddress ( bip44 , p2 , p1 ? ) : Promise < { radixAddress : Buffer ; done : ( ) => void ; } > ; getPublicKey ( bip44 , p1 ? , p2 ? ) : Promise < { publicKey : Buffer ; } > ; getVersion ( ) : Promise < string > ; signAtom ( bip44 , atom ) : Promise < any > ; signHash ( bip44 , hash ) : Promise < { signature : Buffer ; } > ; } export default class RadixHardwareWalletIdentity extends RadixIdentity { public account : RadixAccount ; private app : LedgerApp ; private bip44 : string ; private constructor ( account , app , bip44 ) { super ( account . address ) ; this . account = account ; this . app = app ; this . bip44 = bip44 ; } public static async createNew ( app , bip44 ) { let response ; while ( ! response ) { try { response = await app . getVersion ( ) ; } catch ( e ) { await sleep ( 500 ) ; } } const addressResponse = await app . getRadixAddress ( bip44 , radixUniverse . getMagicByte ( ) ) ; const address = RadixAddress . fromAddress ( addressResponse . radixAddress . toString ( ) ) ; const account = new RadixAccount ( address ) ; const identity = new RadixHardwareWalletIdentity ( account , app , bip44 ) ; account . enableDecryption ( identity ) ; return { identity , done : addressResponse . done , } ; } public async signAtom ( atom ) { return this . app . signAtom ( this . bip44 , atom ) ; } public async signAtomHash ( atom ) { const response = await this . app . signHash ( this . bip44 , atom . getHash ( ) ) ; return response . signature ; } public async decryptECIESPayload ( payload ) { return Buffer . alloc ( 0 ) ; } public async decryptECIESPayloadWithProtectors ( protectors , payload ) { return Buffer . alloc ( 0 ) ; } public getPublicKey ( ) { return this . address . getPublic ( ) ; } }