Skip to content

Commit

Permalink
refactor(abstract-utxo): handle txBase64 response
Browse files Browse the repository at this point in the history
Issue: BTC-1450
  • Loading branch information
OttoAllmendinger committed Nov 28, 2024
1 parent eda209a commit 3d03551
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
29 changes: 16 additions & 13 deletions modules/abstract-utxo/src/abstractUtxoCoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,13 @@ type UtxoCustomSigningFunction<TNumber extends number | bigint> = {
};

const { getExternalChainCode, isChainCode, scriptTypeForChain, outputScripts } = bitgo;

type Unspent<TNumber extends number | bigint = number> = bitgo.Unspent<TNumber>;

type DecodedTransaction<TNumber extends number | bigint> =
| utxolib.bitgo.UtxoTransaction<TNumber>
| utxolib.bitgo.UtxoPsbt;

type RootWalletKeys = bitgo.RootWalletKeys;

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

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

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

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

if (_.isUndefined(txPrebuild.txHex)) {
throw new Error('missing required txPrebuild property txHex');
let tx: DecodedTransaction<TNumber>;
if (txPrebuild.txHex !== undefined) {
tx = this.decodeTransaction<TNumber>(txPrebuild.txHex);
} else if (txPrebuild.txBase64 !== undefined) {
tx = this.decodeTransaction(txPrebuild.txBase64);
} else {
throw new Error('missing required txPrebuild property txHex or txBase64');
}
// obtain all outputs
const explanation: TransactionExplanation = await this.explainTransaction<TNumber>({
txHex: txPrebuild.txHex,
tx,
txInfo: txPrebuild.txInfo,
pubs: keychainArray.map((k) => k.pub) as Triple<string>,
});
Expand Down Expand Up @@ -1533,11 +1540,7 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
async explainTransaction<TNumber extends number | bigint = number>(
params: ExplainTransactionOptions<TNumber>
): Promise<TransactionExplanation> {
const { txHex } = params;
if (typeof txHex !== 'string' || !txHex.match(/^([a-f0-9]{2})+$/i)) {
throw new Error('invalid transaction hex, must be a valid hex string');
}
return explainTx(this.decodeTransaction(txHex), params, this.network);
return explainTx(params.tx, params, this.network);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/bitgo/test/v2/unit/coins/utxo/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ function run<TNumber extends number | bigint = number>(
pubs?: Triple<string>
): Promise<void> {
const explanation = await coin.explainTransaction<TNumber>({
txHex,
tx: coin.decodeTransaction(txHex),
txInfo: {
unspents,
},
Expand Down
2 changes: 1 addition & 1 deletion modules/sdk-coin-doge/src/doge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class Doge extends AbstractUtxoCoin {
return super.explainTransaction({
...params,
txInfo: params.txInfo ? parseTransactionInfo(params.txInfo as TransactionInfoJSON) : undefined,
});
} as ExplainTransactionOptions<bigint>);
}

async recoverFromWrongChain<TNumber extends number | bigint = bigint>(
Expand Down

0 comments on commit 3d03551

Please sign in to comment.