Skip to content

Commit 1c1bde1

Browse files
committed
feat: implement PSBT fallback in wallet transactions
Add fallback mechanism for PSBT transactions in the SDK. When PSBT is requested but fails, automatically retry with legacy transaction format and record the error details for reporting. Co-authored-by: llm-git <llm-git@ttll.de> Ticket: BTC-2894 TICKET: BTC-2894
1 parent ad01a92 commit 1c1bde1

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

modules/sdk-api/src/v1/wallet.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import { tryPromise } from '../util';
3131
const TransactionBuilder = require('./transactionBuilder');
3232
const PendingApproval = require('./pendingapproval');
3333

34-
// PSBT rollout: 10% on mainnet, 100% on testnet
35-
const V1_PSBT_ROLLOUT_PERCENT = 10;
34+
// PSBT rollout: 0% on mainnet, 100% on testnet
35+
const V1_PSBT_ROLLOUT_PERCENT = 0;
3636

3737
function shouldUsePsbt(bitgo: any, explicitUsePsbt?: boolean): boolean {
3838
// Explicit setting always wins
@@ -914,8 +914,32 @@ Wallet.prototype.createTransaction = function (params, callback) {
914914
params.wallet = this;
915915

916916
// Apply PSBT rollout logic (respects explicit usePsbt if set)
917-
params.usePsbt = shouldUsePsbt(this.bitgo, params.usePsbt);
917+
const wantsPsbt = shouldUsePsbt(this.bitgo, params.usePsbt);
918+
919+
if (wantsPsbt) {
920+
// Try PSBT first, fall back to legacy on failure
921+
return TransactionBuilder.createTransaction({ ...params, usePsbt: true })
922+
.then((result: any) => {
923+
result.psbtAttempt = { success: true };
924+
return result;
925+
})
926+
.catch((psbtError: Error) => {
927+
// PSBT failed - fall back to legacy and capture error for backend reporting
928+
console.warn('PSBT transaction failed, falling back to legacy');
929+
return TransactionBuilder.createTransaction({ ...params, usePsbt: false }).then((result: any) => {
930+
result.psbtAttempt = {
931+
success: false,
932+
stack: psbtError.stack?.split('\n').slice(0, 5).join('\n'), // First 5 lines only
933+
};
934+
return result;
935+
});
936+
})
937+
.then(callback)
938+
.catch(callback);
939+
}
918940

941+
// Legacy path
942+
params.usePsbt = false;
919943
return TransactionBuilder.createTransaction(params).then(callback).catch(callback);
920944
};
921945

@@ -1746,6 +1770,7 @@ Wallet.prototype.createAndSignTransaction = function (params, callback) {
17461770
travelInfos,
17471771
estimatedSize,
17481772
unspents,
1773+
psbtAttempt: transaction.psbtAttempt, // Propagate PSBT attempt info for error reporting
17491774
});
17501775
}
17511776
.call(this)

0 commit comments

Comments
 (0)