Skip to content

Commit 3a8446b

Browse files
OttoAllmendingerllm-git
andcommitted
feat(abstract-utxo): update backup key recovery to use standard address generation
Remove deprecated createMultiSigAddress function and update the backup key recovery flow to use the standard address generation functions. This ensures consistency with the rest of the codebase and improves maintainability. Issue: BG-62668 Co-authored-by: llm-git <llm-git@ttll.de>
1 parent cb6df0f commit 3a8446b

2 files changed

Lines changed: 10 additions & 36 deletions

File tree

modules/abstract-utxo/src/abstractUtxoCoin.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -814,27 +814,6 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
814814
return explainTx(this.decodeTransactionFromPrebuild(params), params, this.network);
815815
}
816816

817-
/**
818-
* Create a multisig address of a given type from a list of keychains and a signing threshold
819-
* @param addressType
820-
* @param signatureThreshold
821-
* @param keys
822-
*/
823-
createMultiSigAddress(addressType: ScriptType2Of3, signatureThreshold: number, keys: Buffer[]): MultiSigAddress {
824-
const {
825-
scriptPubKey: outputScript,
826-
redeemScript,
827-
witnessScript,
828-
} = utxolib.bitgo.outputScripts.createOutputScript2of3(keys, addressType);
829-
830-
return {
831-
outputScript,
832-
redeemScript,
833-
witnessScript,
834-
address: utxolib.address.fromOutputScript(outputScript, this.network),
835-
};
836-
}
837-
838817
/**
839818
* @deprecated - use {@see backupKeyRecovery}
840819
* Builds a funds recovery transaction without BitGo

modules/abstract-utxo/src/recovery/backupKeyRecovery.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ import {
1515
} from '@bitgo/sdk-core';
1616
import { getMainnet, networks } from '@bitgo/utxo-lib';
1717

18-
import { AbstractUtxoCoin, MultiSigAddress } from '../abstractUtxoCoin';
18+
import { AbstractUtxoCoin } from '../abstractUtxoCoin';
1919
import { signAndVerifyPsbt } from '../sign';
20+
import { generateAddressWithChainAndIndex } from '../address';
2021

2122
import { forCoin, RecoveryProvider } from './RecoveryProvider';
2223
import { 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-
136129
async 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

Comments
 (0)