11import * as utxolib from '@bitgo/utxo-lib' ;
22import { BIP32Interface , bip32 } from '@bitgo/secp256k1' ;
33import { Dimensions } from '@bitgo/unspents' ;
4- import { fixedScriptWallet , CoinName } from '@bitgo/wasm-utxo' ;
4+ import { CoinName , fixedScriptWallet } from '@bitgo/wasm-utxo' ;
55import { BitGoBase , IWallet , Keychain , Triple , Wallet } from '@bitgo/sdk-core' ;
66import { decrypt } from '@bitgo/sdk-api' ;
77
88import { AbstractUtxoCoin , TransactionInfo } from '../abstractUtxoCoin' ;
9- import { signAndVerifyPsbt } from '../transaction/fixedScript/signPsbt' ;
9+ import { signAndVerifyPsbt } from '../transaction/fixedScript/signTransaction' ;
10+ import { getNetworkFromCoinName } from '../names' ;
11+ import { encodeTransaction } from '../transaction/decode' ;
12+ import { getReplayProtectionPubkeys } from '../transaction/fixedScript/replayProtection' ;
13+ import { toTNumber } from '../tnumber' ;
1014
1115import {
1216 PsbtBackend ,
1317 createEmptyWasmPsbt ,
1418 addWalletInputsToWasmPsbt ,
1519 addOutputToWasmPsbt ,
16- wasmPsbtToUtxolibPsbt ,
20+ getRecoveryAmount ,
1721} from './psbt' ;
1822
1923const { unspentSum } = utxolib . bitgo ;
@@ -343,12 +347,13 @@ async function getPrv(xprv?: string, passphrase?: string, wallet?: IWallet | Wal
343347 * @return unsigned PSBT
344348 */
345349function createSweepTransactionUtxolib < TNumber extends number | bigint = number > (
346- network : utxolib . Network ,
350+ coinName : CoinName ,
347351 walletKeys : RootWalletKeys ,
348352 unspents : WalletUnspent < TNumber > [ ] ,
349353 targetAddress : string ,
350354 feeRateSatVB : number
351355) : utxolib . bitgo . UtxoPsbt {
356+ const network = getNetworkFromCoinName ( coinName ) ;
352357 const inputValue = unspentSum < bigint > (
353358 unspents . map ( ( u ) => ( { ...u , value : BigInt ( u . value ) } ) ) ,
354359 'bigint'
@@ -392,21 +397,20 @@ function createSweepTransactionUtxolib<TNumber extends number | bigint = number>
392397 * @return unsigned PSBT
393398 */
394399function createSweepTransactionWasm < TNumber extends number | bigint = number > (
395- network : utxolib . Network ,
400+ coinName : CoinName ,
396401 walletKeys : RootWalletKeys ,
397402 unspents : WalletUnspent < TNumber > [ ] ,
398403 targetAddress : string ,
399- feeRateSatVB : number ,
400- coinName : CoinName
401- ) : utxolib . bitgo . UtxoPsbt {
404+ feeRateSatVB : number
405+ ) : fixedScriptWallet . BitGoPsbt {
402406 const inputValue = unspentSum < bigint > (
403407 unspents . map ( ( u ) => ( { ...u , value : BigInt ( u . value ) } ) ) ,
404408 'bigint'
405409 ) ;
406410
407411 // Create PSBT with wasm-utxo and add wallet inputs using shared utilities
408412 const unspentsBigint = unspents . map ( ( u ) => ( { ...u , value : BigInt ( u . value ) } ) ) ;
409- const wasmPsbt = createEmptyWasmPsbt ( network , walletKeys ) ;
413+ const wasmPsbt = createEmptyWasmPsbt ( coinName , walletKeys ) ;
410414 addWalletInputsToWasmPsbt ( wasmPsbt , unspentsBigint , walletKeys ) ;
411415
412416 // Calculate dimensions using wasm-utxo Dimensions
@@ -416,10 +420,10 @@ function createSweepTransactionWasm<TNumber extends number | bigint = number>(
416420 const fee = BigInt ( Math . round ( vsize * feeRateSatVB ) ) ;
417421
418422 // Add output to wasm PSBT
419- addOutputToWasmPsbt ( wasmPsbt , targetAddress , inputValue - fee , network ) ;
423+ addOutputToWasmPsbt ( wasmPsbt , targetAddress , inputValue - fee , coinName ) ;
420424
421425 // Convert to utxolib PSBT for signing and return
422- return wasmPsbtToUtxolibPsbt ( wasmPsbt , network ) ;
426+ return wasmPsbt ;
423427}
424428
425429/**
@@ -434,21 +438,17 @@ function createSweepTransactionWasm<TNumber extends number | bigint = number>(
434438 * @return unsigned PSBT
435439 */
436440function createSweepTransaction < TNumber extends number | bigint = number > (
437- network : utxolib . Network ,
441+ coinName : CoinName ,
438442 walletKeys : RootWalletKeys ,
439443 unspents : WalletUnspent < TNumber > [ ] ,
440444 targetAddress : string ,
441445 feeRateSatVB : number ,
442- backend : PsbtBackend = 'wasm-utxo' ,
443- coinName ?: CoinName
444- ) : utxolib . bitgo . UtxoPsbt {
446+ backend : PsbtBackend = 'wasm-utxo'
447+ ) : utxolib . bitgo . UtxoPsbt | fixedScriptWallet . BitGoPsbt {
445448 if ( backend === 'wasm-utxo' ) {
446- if ( ! coinName ) {
447- throw new Error ( 'coinName is required for wasm-utxo backend' ) ;
448- }
449- return createSweepTransactionWasm ( network , walletKeys , unspents , targetAddress , feeRateSatVB , coinName ) ;
449+ return createSweepTransactionWasm ( coinName , walletKeys , unspents , targetAddress , feeRateSatVB ) ;
450450 } else {
451- return createSweepTransactionUtxolib ( network , walletKeys , unspents , targetAddress , feeRateSatVB ) ;
451+ return createSweepTransactionUtxolib ( coinName , walletKeys , unspents , targetAddress , feeRateSatVB ) ;
452452 }
453453}
454454
@@ -502,36 +502,39 @@ export async function recoverCrossChain<TNumber extends number | bigint = number
502502 // Create PSBT for both signed and unsigned recovery
503503 // Use wasm-utxo for testnet coins only, utxolib for mainnet
504504 const backend : PsbtBackend = utxolib . isTestnet ( params . sourceCoin . network ) ? 'wasm-utxo' : 'utxolib' ;
505- const psbt = createSweepTransaction < TNumber > (
506- params . sourceCoin . network ,
505+ let psbt = createSweepTransaction < TNumber > (
506+ params . sourceCoin . getChain ( ) ,
507507 walletKeys ,
508508 walletUnspents ,
509509 params . recoveryAddress ,
510510 feeRateSatVB ,
511- backend ,
512- params . sourceCoin . getChain ( ) as CoinName
511+ backend
513512 ) ;
514513
515514 // For unsigned recovery, return unsigned PSBT hex
516515 if ( ! prv ) {
517516 return {
518- txHex : psbt . toHex ( ) ,
517+ txHex : encodeTransaction ( psbt ) . toString ( 'hex' ) ,
519518 walletId : params . walletId ,
520519 address : params . recoveryAddress ,
521520 coin : params . sourceCoin . getChain ( ) ,
522521 } ;
523522 }
524523
525524 // For signed recovery, sign the PSBT with user key and return half-signed PSBT
526- signAndVerifyPsbt ( psbt , prv , { isLastSignature : false } ) ;
527- const recoveryAmount = utxolib . bitgo . toTNumber < TNumber > ( psbt . txOutputs [ 0 ] . value , params . sourceCoin . amountType ) ;
525+ psbt = signAndVerifyPsbt ( psbt , prv , fixedScriptWallet . RootWalletKeys . from ( walletKeys ) , {
526+ publicKeys : getReplayProtectionPubkeys ( params . sourceCoin . network ) ,
527+ } ) ;
528528
529529 return {
530530 version : wallet instanceof Wallet ? 2 : 1 ,
531531 walletId : params . walletId ,
532- txHex : psbt . toHex ( ) ,
532+ txHex : encodeTransaction ( psbt ) . toString ( 'hex' ) ,
533533 sourceCoin : params . sourceCoin . getChain ( ) ,
534534 recoveryCoin : params . recoveryCoin . getChain ( ) ,
535- recoveryAmount,
535+ recoveryAmount : toTNumber (
536+ getRecoveryAmount ( psbt , walletKeys , params . recoveryAddress ) ,
537+ params . sourceCoin . amountType
538+ ) as TNumber ,
536539 } ;
537540}
0 commit comments