import { EstimationMode } from '@emeraldpay/api' ; import { AddEntry , AddressBookItem , AddressRole , CreateAddressBookItem , CurrentAddress , EntryId , IdSeedReference , IEmeraldVault , LedgerDetails , LedgerSeedReference , SeedDefinition , SeedDescription , SeedReference , UnsignedTx , Uuid , Wallet , WalletCreateOptions , WalletState , } from "@emeraldpay/emerald-vault-core" ; import { AnyCoinCode , BlockchainCode , IApi , IBackendApi , IStoredTransaction } from "@emeraldwallet/core" ; export class MemoryVault { seeds : Uuid [ ] = [ ] ; passwords : Record < Uuid , string > = { } ; seedAddresses : Record < Uuid , Record < string , string > > = { } ; setSeedPassword ( seedId , password ) { this . passwords [ seedId ] = password ; this . addSeedAddress ( seedId , "m/44'/15167'/8173'/68/164" , "0x11" ) ; } addSeedAddress ( seedId , hdpath , address ) { if ( this . seeds . indexOf ( seedId ) < 0 ) { this . seeds . push ( seedId ) ; this . seedAddresses [ seedId ] = { } ; } this . seedAddresses [ seedId ] [ hdpath ] = address ; } } export class BlockchainMock { balances : Record < string , Record < string , string > > = { } ; setBalance ( address , coin , balance ) { if ( typeof this . balances [ address ] == 'undefined' ) { this . balances [ address ] = { } ; } this . balances [ address ] [ coin ] = balance ; } } export const REAL_BTC_TX = "0200000000010109dc86940a177b31454881110398b265fef9c55a47e1017b9967408eb0afef8f010000001716001425c5c9ae97b8831eaef8184276ff55d72f5f85effeffffff027747c61b0000000017a914a0b4508cca72bf294e3cbddd9606644ae1fe6d3087006a18000000000017a91491acb73977a2bf1298686e61a72f62f4e94258a687024730440220438fb2c075aeeed1d1a0ff49efcae7bc9aa922d97d4395de856d76c39ef5069a02205599f95b7e7eadc742c309100d4db42e33225f8766279502d9f1068e8d517f2a012102e8e1d7659d6fbc0dbf653826937b09475ba6763c347138965bfebdb762a9b107f8ed0900" ; export class VaultMock implements IEmeraldVault { readonly vault : MemoryVault ; constructor ( vault ) { this . vault = vault ; } listSeedAddresses ( seedId , blockchain , hdpaths ) { console . log ( "list addresses" , seedId ) ; if ( typeof seedId == "object" ) { if ( seedId . type == "id" ) { const seed = seedId ; if ( typeof this . vault . passwords [ seed . value ] == 'string' ) { const expectedPassword = this . vault . passwords [ seed . value ] ; console . log ( "Password" , seed . value , expectedPassword , seed . password ) ; if ( expectedPassword !== seed . password ) { console . log ( `Password '${ seed . password }' != '${ expectedPassword }'` ) ; return Promise . reject ( new Error ( "Invalid password" ) ) ; } } const seedData = this . vault . seedAddresses [ seedId . value ] ; if ( ! seedData ) { return Promise . resolve ( { } ) ; } const result = { } ; hdpaths . forEach ( ( hdpath ) => { if ( seedData [ hdpath ] ) { result [ hdpath ] = seedData [ hdpath ] ; } } ) ; return Promise . resolve ( result ) ; } } console . log ( "Unsupported seed" , seedId ) ; return Promise . resolve ( { } ) ; } exportJsonPk ( entryId , password ? ) { return Promise . resolve ( "" ) ; } exportRawPk ( entryId , password ) { return Promise . resolve ( "" ) ; } listEntryAddresses ( id , role , start , limit ) { return Promise . resolve ( [ ] ) ; } removeEntry ( entryId ) { return Promise . resolve ( false ) ; } removeWallet ( walletId ) { return Promise . resolve ( false ) ; } setEntryLabel ( entryFullId , label ) { return Promise . resolve ( false ) ; } setEntryReceiveDisabled ( entryFullId , disabled ) { return Promise . resolve ( false ) ; } setState ( state ) { return Promise . resolve ( undefined ) ; } vaultVersion ( ) { return "" ; } addEntry ( walletId , entry ) { return Promise . resolve ( undefined ) ; } addToAddressBook ( item ) { return Promise . resolve ( false ) ; } addWallet ( labelOrOptions ? ) { return Promise . resolve ( undefined ) ; } generateMnemonic ( size ) { return Promise . resolve ( "" ) ; } getConnectedHWSeed ( create ) { return Promise . resolve ( undefined ) ; } getWallet ( id ) { return Promise . resolve ( undefined ) ; } importSeed ( seed ) { return Promise . resolve ( undefined ) ; } isSeedAvailable ( seed ) { return Promise . resolve ( false ) ; } listAddressBook ( blockchain ) { return Promise . resolve ( [ ] ) ; } listSeeds ( ) { return Promise . resolve ( [ ] ) ; } listWallets ( ) { return Promise . resolve ( [ ] ) ; } removeFromAddressBook ( blockchain , address ) { return Promise . resolve ( false ) ; } setWalletLabel ( walletId , label ) { return Promise . resolve ( false ) ; } signTx ( entryId , tx , password ? ) { console . log ( "Sign" , entryId , tx , password ) ; if ( entryId == "f1fa1c12-5ac0-48f3-a76d-5bfb75be37b4-3" ) { return Promise . resolve ( REAL_BTC_TX ) ; } return Promise . resolve ( "" ) ; } getConnectedHWDetails ( ) { return Promise . resolve ( [ { type : "ledger" , connected : true , app : "bitcoin" } ] ) ; } } export class ApiMock implements IApi { readonly vault : IEmeraldVault ; constructor ( vault ) { this . vault = vault ; } chain ( name ) { return undefined ; } } export class BackendMock implements IBackendApi { readonly vault : MemoryVault = new MemoryVault ( ) ; readonly blockchains : Record < string , BlockchainMock > = { } ; broadcastSignedTx ( blockchain , tx ) { return Promise . resolve ( "" ) ; } estimateTxCost ( blockchain , tx ) { return Promise . resolve ( 0 ) ; } getBalance ( blockchain , address , tokens ) { const state = this . blockchains [ blockchain . toLowerCase ( ) ] ; if ( typeof state == 'undefined' ) { return Promise . resolve ( { } ) ; } const result = { } ; tokens . forEach ( ( token ) => { if ( state . balances [ address ] ) { const balance = state . balances [ address ] [ token ] ; if ( balance ) { result [ token ] = balance ; } else { result [ token ] = "0" ; } } else { result [ token ] = "0" ; } } ) ; return Promise . resolve ( result ) ; } getGasPrice ( blockchain ) { return Promise . resolve ( 0 ) ; } persistTransactions ( blockchain , txs ) { return Promise . resolve ( undefined ) ; } getNonce ( blockchain , address ) { return Promise . resolve ( 0 ) ; } useBlockchains ( codes ) { codes . forEach ( ( code ) => { this . blockchains [ code . toLowerCase ( ) ] = new BlockchainMock ( ) ; } ) ; } estimateFee ( blockchain , blocks , mode ) { switch ( mode ) { case 'avgLast' : return Promise . resolve ( 1000 ) ; case 'avgMiddle' : return Promise . resolve ( 3000 ) ; case 'avgTail5' : return Promise . resolve ( 1500 ) ; } return Promise . resolve ( 0 ) ; } }