Skip to content

Commit 08bc215

Browse files
OttoAllmendingerllm-git
andcommitted
refactor(abstract-utxo): update getMusig2Nonces to accept UtxoPsbt
Update the getMusig2Nonces method to accept a UtxoPsbt object directly instead of a hex string. This improves type safety and reduces unnecessary encoding/decoding. Also add a helper method decodeTransactionAsPsbt to validate and cast decoded transactions to UtxoPsbt. Update dependent functions to use the new method signature and refactor to avoid unnecessary conversion steps. Issue: BTC-2806 Co-authored-by: llm-git <[email protected]>
1 parent b73057e commit 08bc215

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

modules/abstract-utxo/src/abstractUtxoCoin.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,14 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
547547
}
548548
}
549549

550+
decodeTransactionAsPsbt(input: Buffer | string): utxolib.bitgo.UtxoPsbt {
551+
const decoded = this.decodeTransaction(input);
552+
if (!(decoded instanceof utxolib.bitgo.UtxoPsbt)) {
553+
throw new Error('expected psbt but got transaction');
554+
}
555+
return decoded;
556+
}
557+
550558
decodeTransactionFromPrebuild<TNumber extends number | bigint>(prebuild: {
551559
txHex?: string;
552560
txBase64?: string;
@@ -711,12 +719,13 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
711719
* @param psbtHex all MuSig2 inputs should contain user MuSig2 nonce
712720
* @param walletId
713721
*/
714-
async getMusig2Nonces(psbtHex: string, walletId: string): Promise<SignPsbtResponse> {
715-
const params: SignPsbtRequest = { psbt: psbtHex };
716-
return await this.bitgo
722+
async getMusig2Nonces(psbt: utxolib.bitgo.UtxoPsbt, walletId: string): Promise<utxolib.bitgo.UtxoPsbt> {
723+
const params: SignPsbtRequest = { psbt: psbt.toHex() };
724+
const response = await this.bitgo
717725
.post(this.url('/wallet/' + walletId + '/tx/signpsbt'))
718726
.send(params)
719727
.result();
728+
return this.decodeTransactionAsPsbt(response.psbt);
720729
}
721730

722731
/**
@@ -726,7 +735,7 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
726735
* @param walletId
727736
*/
728737
async signPsbt(psbtHex: string, walletId: string): Promise<SignPsbtResponse> {
729-
return this.getMusig2Nonces(psbtHex, walletId);
738+
return { psbt: (await this.getMusig2Nonces(this.decodeTransactionAsPsbt(psbtHex), walletId)).toHex() };
730739
}
731740

732741
/**
@@ -736,9 +745,11 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
736745
async signPsbtFromOVC(ovcJson: Record<string, unknown>): Promise<Record<string, unknown>> {
737746
assert(ovcJson['psbtHex'], 'ovcJson must contain psbtHex');
738747
assert(ovcJson['walletId'], 'ovcJson must contain walletId');
739-
const psbt = (await this.getMusig2Nonces(ovcJson['psbtHex'] as string, ovcJson['walletId'] as string)).psbt;
740-
assert(psbt, 'psbt not found');
741-
return _.extend(ovcJson, { txHex: psbt });
748+
const psbt = await this.getMusig2Nonces(
749+
this.decodeTransactionAsPsbt(ovcJson['psbtHex'] as string),
750+
ovcJson['walletId'] as string
751+
);
752+
return _.extend(ovcJson, { txHex: psbt.toHex() });
742753
}
743754

744755
/**

modules/abstract-utxo/src/transaction/fixedScript/signTransaction.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { signAndVerifyPsbt, signAndVerifyWalletTransaction } from './sign';
1313
type RootWalletKeys = bitgo.RootWalletKeys;
1414

1515
export interface Musig2Participant {
16-
getMusig2Nonces(psbtHex: string, walletId: string): Promise<{ psbt: string }>;
16+
getMusig2Nonces(psbt: utxolib.bitgo.UtxoPsbt, walletId: string): Promise<utxolib.bitgo.UtxoPsbt>;
1717
}
1818

1919
/**
@@ -66,7 +66,7 @@ export async function signTransaction<TNumber extends number | bigint>(
6666
return { txHex: tx.toHex() };
6767
case 'cosignerNonce':
6868
assert(params.walletId, 'walletId is required for MuSig2 bitgo nonce');
69-
return { txHex: (await coin.getMusig2Nonces(tx.toHex(), params.walletId)).psbt };
69+
return { txHex: (await coin.getMusig2Nonces(tx, params.walletId)).toHex() };
7070
case 'signerSignature':
7171
const txId = tx.getUnsignedTx().getId();
7272
const psbt = PSBT_CACHE.get(txId);
@@ -83,8 +83,8 @@ export async function signTransaction<TNumber extends number | bigint>(
8383
assert(params.walletId, 'walletId is required for MuSig2 bitgo nonce');
8484
assert(signerKeychain);
8585
tx.setAllInputsMusig2NonceHD(signerKeychain);
86-
const response = await coin.getMusig2Nonces(tx.toHex(), params.walletId);
87-
tx.combine(bitgo.createPsbtFromHex(response.psbt, network));
86+
const response = await coin.getMusig2Nonces(tx, params.walletId);
87+
tx = tx.combine(response);
8888
break;
8989
}
9090
} else {

0 commit comments

Comments
 (0)