Skip to content

Commit 3d03551

Browse files
refactor(abstract-utxo): handle txBase64 response
Issue: BTC-1450
1 parent eda209a commit 3d03551

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

modules/abstract-utxo/src/abstractUtxoCoin.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,13 @@ type UtxoCustomSigningFunction<TNumber extends number | bigint> = {
115115
};
116116

117117
const { getExternalChainCode, isChainCode, scriptTypeForChain, outputScripts } = bitgo;
118+
118119
type Unspent<TNumber extends number | bigint = number> = bitgo.Unspent<TNumber>;
119120

121+
type DecodedTransaction<TNumber extends number | bigint> =
122+
| utxolib.bitgo.UtxoTransaction<TNumber>
123+
| utxolib.bitgo.UtxoPsbt;
124+
120125
type RootWalletKeys = bitgo.RootWalletKeys;
121126

122127
export type UtxoCoinSpecific = AddressCoinSpecific | DescriptorAddressCoinSpecific;
@@ -172,7 +177,7 @@ export interface TransactionInfo<TNumber extends number | bigint = number> {
172177
}
173178

174179
export interface ExplainTransactionOptions<TNumber extends number | bigint = number> {
175-
txHex: string;
180+
tx: DecodedTransaction<TNumber>;
176181
txInfo?: TransactionInfo<TNumber>;
177182
feeInfo?: string;
178183
pubs?: Triple<string>;
@@ -565,9 +570,7 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
565570
return utxolib.bitgo.createTransactionFromHex<TNumber>(hex, this.network, this.amountType);
566571
}
567572

568-
decodeTransaction<TNumber extends number | bigint>(
569-
input: Buffer | string
570-
): utxolib.bitgo.UtxoTransaction<TNumber> | utxolib.bitgo.UtxoPsbt {
573+
decodeTransaction<TNumber extends number | bigint>(input: Buffer | string): DecodedTransaction<TNumber> {
571574
if (typeof input === 'string') {
572575
for (const format of ['hex', 'base64'] as const) {
573576
const buffer = Buffer.from(input, format);
@@ -640,12 +643,16 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
640643

641644
const keySignatures = _.get(wallet, '_wallet.keySignatures', {});
642645

643-
if (_.isUndefined(txPrebuild.txHex)) {
644-
throw new Error('missing required txPrebuild property txHex');
646+
let tx: DecodedTransaction<TNumber>;
647+
if (txPrebuild.txHex !== undefined) {
648+
tx = this.decodeTransaction<TNumber>(txPrebuild.txHex);
649+
} else if (txPrebuild.txBase64 !== undefined) {
650+
tx = this.decodeTransaction(txPrebuild.txBase64);
651+
} else {
652+
throw new Error('missing required txPrebuild property txHex or txBase64');
645653
}
646-
// obtain all outputs
647654
const explanation: TransactionExplanation = await this.explainTransaction<TNumber>({
648-
txHex: txPrebuild.txHex,
655+
tx,
649656
txInfo: txPrebuild.txInfo,
650657
pubs: keychainArray.map((k) => k.pub) as Triple<string>,
651658
});
@@ -1533,11 +1540,7 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
15331540
async explainTransaction<TNumber extends number | bigint = number>(
15341541
params: ExplainTransactionOptions<TNumber>
15351542
): Promise<TransactionExplanation> {
1536-
const { txHex } = params;
1537-
if (typeof txHex !== 'string' || !txHex.match(/^([a-f0-9]{2})+$/i)) {
1538-
throw new Error('invalid transaction hex, must be a valid hex string');
1539-
}
1540-
return explainTx(this.decodeTransaction(txHex), params, this.network);
1543+
return explainTx(params.tx, params, this.network);
15411544
}
15421545

15431546
/**

modules/bitgo/test/v2/unit/coins/utxo/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ function run<TNumber extends number | bigint = number>(
541541
pubs?: Triple<string>
542542
): Promise<void> {
543543
const explanation = await coin.explainTransaction<TNumber>({
544-
txHex,
544+
tx: coin.decodeTransaction(txHex),
545545
txInfo: {
546546
unspents,
547547
},

modules/sdk-coin-doge/src/doge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class Doge extends AbstractUtxoCoin {
118118
return super.explainTransaction({
119119
...params,
120120
txInfo: params.txInfo ? parseTransactionInfo(params.txInfo as TransactionInfoJSON) : undefined,
121-
});
121+
} as ExplainTransactionOptions<bigint>);
122122
}
123123

124124
async recoverFromWrongChain<TNumber extends number | bigint = bigint>(

0 commit comments

Comments
 (0)