Skip to content

Commit 0aacdce

Browse files
Merge pull request #7861 from BitGo/BTC-2909-dims-2
feat: use fromOutput to remove utxolib dependency in wasm path
2 parents 03f6707 + 55e446d commit 0aacdce

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

modules/abstract-utxo/src/recovery/backupKeyRecovery.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
krsProviders,
1212
} from '@bitgo/sdk-core';
1313
import { getMainnet, networks } from '@bitgo/utxo-lib';
14+
import { CoinName } from '@bitgo/wasm-utxo';
1415

1516
import { AbstractUtxoCoin } from '../abstractUtxoCoin';
1617
import { signAndVerifyPsbt } from '../transaction/fixedScript/signPsbt';
@@ -377,6 +378,7 @@ export async function backupKeyRecovery(
377378
recoveryDestination: params.recoveryDestination,
378379
keyRecoveryServiceFee: krsFee,
379380
keyRecoveryServiceFeeAddress: krsFeeAddress,
381+
coinName: coin.getChain() as CoinName,
380382
},
381383
backend
382384
);

modules/abstract-utxo/src/recovery/crossChainRecovery.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as utxolib from '@bitgo/utxo-lib';
22
import { BIP32Interface, bip32 } from '@bitgo/secp256k1';
33
import { Dimensions } from '@bitgo/unspents';
4-
import { fixedScriptWallet } from '@bitgo/wasm-utxo';
4+
import { fixedScriptWallet, CoinName } from '@bitgo/wasm-utxo';
55
import { BitGoBase, IWallet, Keychain, Triple, Wallet } from '@bitgo/sdk-core';
66
import { decrypt } from '@bitgo/sdk-api';
77

@@ -388,14 +388,16 @@ function createSweepTransactionUtxolib<TNumber extends number | bigint = number>
388388
* @param unspents
389389
* @param targetAddress
390390
* @param feeRateSatVB
391+
* @param coinName - BitGo coin name (e.g. 'btc', 'tbtc', 'ltc')
391392
* @return unsigned PSBT
392393
*/
393394
function createSweepTransactionWasm<TNumber extends number | bigint = number>(
394395
network: utxolib.Network,
395396
walletKeys: RootWalletKeys,
396397
unspents: WalletUnspent<TNumber>[],
397398
targetAddress: string,
398-
feeRateSatVB: number
399+
feeRateSatVB: number,
400+
coinName: CoinName
399401
): utxolib.bitgo.UtxoPsbt {
400402
const inputValue = unspentSum<bigint>(
401403
unspents.map((u) => ({ ...u, value: BigInt(u.value) })),
@@ -408,9 +410,8 @@ function createSweepTransactionWasm<TNumber extends number | bigint = number>(
408410
addWalletInputsToWasmPsbt(wasmPsbt, unspentsBigint, walletKeys);
409411

410412
// Calculate dimensions using wasm-utxo Dimensions
411-
const targetOutputScript = utxolib.address.toOutputScript(targetAddress, network);
412413
const vsize = fixedScriptWallet.Dimensions.fromPsbt(wasmPsbt)
413-
.plus(fixedScriptWallet.Dimensions.fromOutput(new Uint8Array(targetOutputScript)))
414+
.plus(fixedScriptWallet.Dimensions.fromOutput(targetAddress, coinName))
414415
.getVSize();
415416
const fee = BigInt(Math.round(vsize * feeRateSatVB));
416417

@@ -429,6 +430,7 @@ function createSweepTransactionWasm<TNumber extends number | bigint = number>(
429430
* @param targetAddress
430431
* @param feeRateSatVB
431432
* @param backend - Which backend to use for PSBT creation (default: 'wasm-utxo')
433+
* @param coinName - BitGo coin name (required for wasm-utxo backend)
432434
* @return unsigned PSBT
433435
*/
434436
function createSweepTransaction<TNumber extends number | bigint = number>(
@@ -437,10 +439,14 @@ function createSweepTransaction<TNumber extends number | bigint = number>(
437439
unspents: WalletUnspent<TNumber>[],
438440
targetAddress: string,
439441
feeRateSatVB: number,
440-
backend: PsbtBackend = 'wasm-utxo'
442+
backend: PsbtBackend = 'wasm-utxo',
443+
coinName?: CoinName
441444
): utxolib.bitgo.UtxoPsbt {
442445
if (backend === 'wasm-utxo') {
443-
return createSweepTransactionWasm(network, walletKeys, unspents, targetAddress, feeRateSatVB);
446+
if (!coinName) {
447+
throw new Error('coinName is required for wasm-utxo backend');
448+
}
449+
return createSweepTransactionWasm(network, walletKeys, unspents, targetAddress, feeRateSatVB, coinName);
444450
} else {
445451
return createSweepTransactionUtxolib(network, walletKeys, unspents, targetAddress, feeRateSatVB);
446452
}
@@ -502,7 +508,8 @@ export async function recoverCrossChain<TNumber extends number | bigint = number
502508
walletUnspents,
503509
params.recoveryAddress,
504510
feeRateSatVB,
505-
backend
511+
backend,
512+
params.sourceCoin.getChain() as CoinName
506513
);
507514

508515
// For unsigned recovery, return unsigned PSBT hex

modules/abstract-utxo/src/recovery/psbt.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as utxolib from '@bitgo/utxo-lib';
22
import { Dimensions } from '@bitgo/unspents';
3-
import { fixedScriptWallet, utxolibCompat } from '@bitgo/wasm-utxo';
3+
import { fixedScriptWallet, utxolibCompat, CoinName } from '@bitgo/wasm-utxo';
44

55
type RootWalletKeys = utxolib.bitgo.RootWalletKeys;
66
type WalletUnspent<TNumber extends number | bigint> = utxolib.bitgo.WalletUnspent<TNumber>;
@@ -60,6 +60,8 @@ interface CreateBackupKeyRecoveryPsbtOptions {
6060
keyRecoveryServiceFeeAddress: string | undefined;
6161
/** Block height for Zcash networks (required to determine consensus branch ID) */
6262
blockHeight?: number;
63+
/** Coin name for wasm-utxo (e.g. 'btc', 'tbtc', 'ltc') */
64+
coinName?: CoinName;
6365
}
6466

6567
/**
@@ -243,21 +245,23 @@ function createBackupKeyRecoveryPsbtWasm(
243245
unspents: WalletUnspent<bigint>[],
244246
options: CreateBackupKeyRecoveryPsbtOptions
245247
): utxolib.bitgo.UtxoPsbt {
246-
const { feeRateSatVB, recoveryDestination, keyRecoveryServiceFee, keyRecoveryServiceFeeAddress } = options;
248+
const { feeRateSatVB, recoveryDestination, keyRecoveryServiceFee, keyRecoveryServiceFeeAddress, coinName } = options;
249+
250+
if (!coinName) {
251+
throw new Error('coinName is required for wasm-utxo backend');
252+
}
247253

248254
// Create PSBT with wasm-utxo and add wallet inputs using shared utilities
249255
const wasmPsbt = createEmptyWasmPsbt(network, rootWalletKeys, { blockHeight: options.blockHeight });
250256
addWalletInputsToWasmPsbt(wasmPsbt, unspents, rootWalletKeys);
251257

252258
// Calculate dimensions using wasm-utxo Dimensions
253-
const recoveryOutputScript = utxolib.address.toOutputScript(recoveryDestination, network);
254259
let dimensions = fixedScriptWallet.Dimensions.fromPsbt(wasmPsbt).plus(
255-
fixedScriptWallet.Dimensions.fromOutput(new Uint8Array(recoveryOutputScript))
260+
fixedScriptWallet.Dimensions.fromOutput(recoveryDestination, coinName)
256261
);
257262

258263
if (keyRecoveryServiceFeeAddress) {
259-
const krsOutputScript = utxolib.address.toOutputScript(keyRecoveryServiceFeeAddress, network);
260-
dimensions = dimensions.plus(fixedScriptWallet.Dimensions.fromOutput(new Uint8Array(krsOutputScript)));
264+
dimensions = dimensions.plus(fixedScriptWallet.Dimensions.fromOutput(keyRecoveryServiceFeeAddress, coinName));
261265
}
262266

263267
const approximateFee = BigInt(dimensions.getVSize() * feeRateSatVB);

0 commit comments

Comments
 (0)