Skip to content

Commit 4c06e7e

Browse files
refactor(abstract-utxo): add decodeTransaction method
TICKET: BTC-1450
1 parent 120bea7 commit 4c06e7e

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

modules/abstract-utxo/src/abstractUtxoCoin.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,7 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
500500
if (_.isUndefined(prebuild.txHex)) {
501501
throw new Error('missing required txPrebuild property txHex');
502502
}
503-
const tx = bitgo.isPsbt(prebuild.txHex)
504-
? bitgo.createPsbtFromHex(prebuild.txHex, this.network)
505-
: this.createTransactionFromHex<TNumber>(prebuild.txHex);
503+
const tx = this.decodeTransaction(prebuild.txHex);
506504
if (_.isUndefined(prebuild.blockHeight)) {
507505
prebuild.blockHeight = (await this.getLatestBlockHeight()) as number;
508506
}
@@ -542,6 +540,29 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
542540
return utxolib.bitgo.createTransactionFromHex<TNumber>(hex, this.network, this.amountType);
543541
}
544542

543+
decodeTransaction<TNumber extends number | bigint>(
544+
input: Buffer | string
545+
): utxolib.bitgo.UtxoTransaction<TNumber> | utxolib.bitgo.UtxoPsbt {
546+
if (typeof input === 'string') {
547+
for (const format of ['hex', 'base64'] as const) {
548+
const buffer = Buffer.from(input, format);
549+
if (buffer.toString(format) === input.toLowerCase()) {
550+
return this.decodeTransaction(buffer);
551+
}
552+
}
553+
554+
throw new Error('input must be a valid hex or base64 string');
555+
}
556+
557+
if (utxolib.bitgo.isPsbt(input)) {
558+
return utxolib.bitgo.createPsbtFromBuffer(input, this.network);
559+
} else {
560+
return utxolib.bitgo.createTransactionFromBuffer(input, this.network, {
561+
amountType: this.amountType,
562+
});
563+
}
564+
}
565+
545566
toCanonicalTransactionRecipient(output: { valueString: string; address?: string }): {
546567
amount: bigint;
547568
address?: string;
@@ -1278,9 +1299,7 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
12781299
throw new Error('missing txPrebuild parameter');
12791300
}
12801301

1281-
let tx = bitgo.isPsbt(txPrebuild.txHex)
1282-
? bitgo.createPsbtFromHex(txPrebuild.txHex, this.network)
1283-
: this.createTransactionFromHex<TNumber>(txPrebuild.txHex);
1302+
let tx = this.decodeTransaction(params.txPrebuild.txHex);
12841303

12851304
const isTxWithKeyPathSpendInput = tx instanceof bitgo.UtxoPsbt && bitgo.isTransactionWithKeyPathSpendInput(tx);
12861305

@@ -1413,9 +1432,7 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
14131432
const txHex = signTransactionParams.txPrebuild.txHex;
14141433
assert(txHex, 'missing txHex parameter');
14151434

1416-
const tx = bitgo.isPsbt(txHex)
1417-
? bitgo.createPsbtFromHex(txHex, this.network)
1418-
: this.createTransactionFromHex<TNumber>(txHex);
1435+
const tx = this.decodeTransaction(txHex);
14191436

14201437
const isTxWithKeyPathSpendInput = tx instanceof bitgo.UtxoPsbt && bitgo.isTransactionWithKeyPathSpendInput(tx);
14211438

@@ -1503,10 +1520,11 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
15031520
if (typeof txHex !== 'string' || !txHex.match(/^([a-f0-9]{2})+$/i)) {
15041521
throw new Error('invalid transaction hex, must be a valid hex string');
15051522
}
1506-
if (utxolib.bitgo.isPsbt(txHex)) {
1507-
return explainPsbt(utxolib.bitgo.createPsbtFromHex(txHex, this.network), params, this.network);
1523+
const tx = this.decodeTransaction(txHex);
1524+
if (tx instanceof bitgo.UtxoPsbt) {
1525+
return explainPsbt(tx, params, this.network);
15081526
} else {
1509-
return explainTx(this.createTransactionFromHex<TNumber>(txHex), params, this.network);
1527+
return explainTx(tx, params, this.network);
15101528
}
15111529
}
15121530

0 commit comments

Comments
 (0)