Skip to content

Commit 27d650f

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 27d650f

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,8 +914,33 @@ 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:', psbtError.message);
929+
return TransactionBuilder.createTransaction({ ...params, usePsbt: false }).then((result: any) => {
930+
result.psbtAttempt = {
931+
success: false,
932+
error: psbtError.message,
933+
stack: psbtError.stack?.split('\n').slice(0, 5).join('\n'), // First 5 lines
934+
};
935+
return result;
936+
});
937+
})
938+
.then(callback)
939+
.catch(callback);
940+
}
918941

942+
// Legacy path
943+
params.usePsbt = false;
919944
return TransactionBuilder.createTransaction(params).then(callback).catch(callback);
920945
};
921946

@@ -1746,6 +1771,7 @@ Wallet.prototype.createAndSignTransaction = function (params, callback) {
17461771
travelInfos,
17471772
estimatedSize,
17481773
unspents,
1774+
psbtAttempt: transaction.psbtAttempt, // Propagate PSBT attempt info for error reporting
17491775
});
17501776
}
17511777
.call(this)

0 commit comments

Comments
 (0)