Skip to content

Commit ad01a92

Browse files
committed
feat: add PSBT support with 10% rollout for v1 transactions
Add PSBT transaction building capability for legacy v1 wallets with gradual rollout: - 10% of mainnet transactions use PSBT format - 100% of testnet transactions use PSBT format - Explicit usePsbt: true/false always takes precedence TICKET: BTC-2894
1 parent 1617968 commit ad01a92

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ 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;
36+
37+
function shouldUsePsbt(bitgo: any, explicitUsePsbt?: boolean): boolean {
38+
// Explicit setting always wins
39+
if (explicitUsePsbt !== undefined) {
40+
return explicitUsePsbt;
41+
}
42+
43+
// Testnet: always PSBT
44+
const network = common.Environments[bitgo.getEnv()]?.network;
45+
if (network !== 'bitcoin') {
46+
return true;
47+
}
48+
49+
// Mainnet: 10% rollout
50+
return Math.random() * 100 < V1_PSBT_ROLLOUT_PERCENT;
51+
}
52+
3453
const { getExternalChainCode, getInternalChainCode, isChainCode, scriptTypeForChain } = utxolib.bitgo;
3554
const request = require('superagent');
3655

@@ -894,6 +913,9 @@ Wallet.prototype.createTransaction = function (params, callback) {
894913
params.validate = params.validate !== undefined ? params.validate : this.bitgo.getValidate();
895914
params.wallet = this;
896915

916+
// Apply PSBT rollout logic (respects explicit usePsbt if set)
917+
params.usePsbt = shouldUsePsbt(this.bitgo, params.usePsbt);
918+
897919
return TransactionBuilder.createTransaction(params).then(callback).catch(callback);
898920
};
899921

@@ -1609,6 +1631,7 @@ Wallet.prototype.accelerateTransaction = function accelerateTransaction(params,
16091631
const changeAddress = await this.createAddress({ chain: changeChain });
16101632

16111633
// create the child tx and broadcast
1634+
// Use legacy format - PSBT rollout applies to user-facing createTransaction only
16121635
// @ts-expect-error - no implicit this
16131636
const tx = await this.createAndSignTransaction({
16141637
unspents: unspentsToUse,
@@ -1625,6 +1648,7 @@ Wallet.prototype.accelerateTransaction = function accelerateTransaction(params,
16251648
},
16261649
xprv: params.xprv,
16271650
walletPassphrase: params.walletPassphrase,
1651+
usePsbt: false,
16281652
});
16291653

16301654
// child fee rate must be in sat/kB, so we need to convert

0 commit comments

Comments
 (0)