@@ -15,8 +15,9 @@ import {
1515} from '@bitgo/sdk-core' ;
1616import { getMainnet , networks } from '@bitgo/utxo-lib' ;
1717
18- import { AbstractUtxoCoin , MultiSigAddress } from '../abstractUtxoCoin' ;
18+ import { AbstractUtxoCoin } from '../abstractUtxoCoin' ;
1919import { signAndVerifyPsbt } from '../sign' ;
20+ import { generateAddressWithChainAndIndex } from '../address' ;
2021
2122import { forCoin , RecoveryProvider } from './RecoveryProvider' ;
2223import { MempoolApi } from './mempoolApi' ;
@@ -125,14 +126,6 @@ export interface RecoverParams {
125126 feeRate ?: number ;
126127}
127128
128- function getFormattedAddress ( coin : AbstractUtxoCoin , address : MultiSigAddress ) {
129- // Blockchair uses cashaddr format when querying the API for address information. Convert legacy addresses to cashaddr
130- // before querying the API.
131- return coin . getChain ( ) === 'bch' || coin . getChain ( ) === 'bcha'
132- ? coin . canonicalAddress ( address . address , 'cashaddr' ) . split ( ':' ) [ 1 ]
133- : address . address ;
134- }
135-
136129async function queryBlockchainUnspentsPath (
137130 coin : AbstractUtxoCoin ,
138131 params : RecoverParams ,
@@ -157,10 +150,12 @@ async function queryBlockchainUnspentsPath(
157150 }
158151
159152 async function gatherUnspents ( addrIndex : number ) {
160- const walletKeysForUnspent = walletKeys . deriveForChainAndIndex ( chain , addrIndex ) ;
161- const address = coin . createMultiSigAddress ( scriptType , 2 , walletKeysForUnspent . publicKeys ) ;
153+ const keychains = walletKeys . triple . map ( ( k ) => ( { pub : k . neutered ( ) . toBase58 ( ) } ) ) ;
154+ const format = coin . getChain ( ) === 'bch' || coin . getChain ( ) === 'bcha' ? 'cashaddr' : undefined ;
155+ const address = generateAddressWithChainAndIndex ( coin . network , keychains , chain , addrIndex , format ) ;
162156
163- const formattedAddress = getFormattedAddress ( coin , address ) ;
157+ // Blockchair uses cashaddr format when querying the API for address information. Strip the prefix for BCH/BCHA.
158+ const formattedAddress = format === 'cashaddr' ? address . split ( ':' ) [ 1 ] : address ;
164159 const addrInfo = await recoveryProvider . getAddressInfo ( formattedAddress ) ;
165160 // we use txCount here because it implies usage - having tx'es means the addr was generated and used
166161 if ( addrInfo . txCount === 0 ) {
@@ -169,7 +164,7 @@ async function queryBlockchainUnspentsPath(
169164 numSequentialAddressesWithoutTxs = 0 ;
170165
171166 if ( addrInfo . balance > 0 ) {
172- console . log ( `Found an address with balance: ${ address . address } with balance ${ addrInfo . balance } ` ) ;
167+ console . log ( `Found an address with balance: ${ address } with balance ${ addrInfo . balance } ` ) ;
173168 const addressUnspents = await recoveryProvider . getUnspentsForAddresses ( [ formattedAddress ] ) ;
174169 const processedUnspents = await Promise . all (
175170 addressUnspents . map ( async ( u ) : Promise < WalletUnspent < bigint > > => {
@@ -375,9 +370,9 @@ export async function backupKeyRecovery(
375370 const recoveryAmount = totalInputAmount - approximateFee - krsFee ;
376371
377372 if ( recoveryAmount < BigInt ( 0 ) ) {
378- throw new Error ( `this wallet\'s balance is too low to pay the fees specified by the KRS provider.
373+ throw new Error ( `this wallet\'s balance is too low to pay the fees specified by the KRS provider.
379374 Existing balance on wallet: ${ totalInputAmount . toString ( ) } . Estimated network fee for the recovery transaction
380- : ${ approximateFee . toString ( ) } , KRS fee to pay: ${ krsFee . toString ( ) } . After deducting fees, your total
375+ : ${ approximateFee . toString ( ) } , KRS fee to pay: ${ krsFee . toString ( ) } . After deducting fees, your total
381376 recoverable balance is ${ recoveryAmount . toString ( ) } ` ) ;
382377 }
383378
0 commit comments