@@ -7,18 +7,20 @@ import { BIP32, bip32 } from '@bitgo/wasm-utxo';
77import debugLib from 'debug' ;
88
99import { UtxoCoinName } from '../../names' ;
10- import type { Unspent , WalletUnspent } from '../../unspent' ;
10+ import { isWalletUnspent , type Unspent } from '../../unspent' ;
1111import { toUtxolibBIP32 } from '../../wasmUtil' ;
1212
1313import { getReplayProtectionAddresses } from './replayProtection' ;
1414import { InputSigningError , TransactionSigningError } from './SigningError' ;
1515
1616const debug = debugLib ( 'bitgo:v2:utxo' ) ;
1717
18- const { isWalletUnspent , signInputWithUnspent, toOutput } = utxolib . bitgo ;
18+ const { signInputWithUnspent, toOutput } = utxolib . bitgo ;
1919
2020type RootWalletKeys = utxolib . bitgo . RootWalletKeys ;
2121
22+ const UTXOLIB_VALID_CHAIN_CODES = new Set ( [ 0 , 1 , 10 , 11 , 20 , 21 , 30 , 31 , 40 , 41 ] as const ) ;
23+
2224/**
2325 * Sign all inputs of a wallet transaction and verify signatures after signing.
2426 * Collects and logs signing errors and verification errors, throws error in the end if any of them
@@ -71,8 +73,21 @@ export function signAndVerifyWalletTransaction<TNumber extends number | bigint>(
7173 if ( ! isWalletUnspent < TNumber > ( unspent ) ) {
7274 return InputSigningError . expectedWalletUnspent < TNumber > ( inputIndex , null , unspent ) ;
7375 }
76+ if ( ! UTXOLIB_VALID_CHAIN_CODES . has ( unspent . chain as utxolib . bitgo . ChainCode ) ) {
77+ return new InputSigningError < TNumber > (
78+ inputIndex ,
79+ null ,
80+ unspent ,
81+ new Error ( `Chain code ${ unspent . chain } is not supported for legacy signing` )
82+ ) ;
83+ }
7484 try {
75- signInputWithUnspent < TNumber > ( txBuilder , inputIndex , unspent as WalletUnspent < TNumber > , walletSigner ) ;
85+ signInputWithUnspent < TNumber > (
86+ txBuilder ,
87+ inputIndex ,
88+ unspent as unknown as utxolib . bitgo . WalletUnspent < TNumber > ,
89+ walletSigner
90+ ) ;
7691 debug ( 'Successfully signed input %d of %d' , inputIndex + 1 , unspents . length ) ;
7792 } catch ( e ) {
7893 return new InputSigningError < TNumber > ( inputIndex , null , unspent , e ) ;
@@ -96,10 +111,20 @@ export function signAndVerifyWalletTransaction<TNumber extends number | bigint>(
96111 if ( ! isWalletUnspent < TNumber > ( unspent ) ) {
97112 return InputSigningError . expectedWalletUnspent < TNumber > ( inputIndex , null , unspent ) ;
98113 }
99- const walletUnspent = unspent as WalletUnspent < TNumber > ;
114+ if ( ! UTXOLIB_VALID_CHAIN_CODES . has ( unspent . chain as utxolib . bitgo . ChainCode ) ) {
115+ return new InputSigningError < TNumber > (
116+ inputIndex ,
117+ null ,
118+ unspent ,
119+ new Error ( `Chain code ${ unspent . chain } is not supported for legacy verification` )
120+ ) ;
121+ }
122+ const walletUnspent = unspent ;
100123 try {
101- const publicKey = walletSigner . deriveForChainAndIndex ( walletUnspent . chain , walletUnspent . index ) . signer
102- . publicKey ;
124+ const publicKey = walletSigner . deriveForChainAndIndex (
125+ walletUnspent . chain as utxolib . bitgo . ChainCode ,
126+ walletUnspent . index
127+ ) . signer . publicKey ;
103128 if (
104129 ! utxolib . bitgo . verifySignatureWithPublicKey < TNumber > ( signedTransaction , inputIndex , prevOutputs , publicKey )
105130 ) {
0 commit comments