@@ -449,16 +449,18 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
449449 }
450450
451451 preprocessBuildParams ( params : Record < string , any > ) : Record < string , any > {
452- params . recipients =
453- params . recipients && params . recipients instanceof Array
454- ? params ?. recipients ?. map ( ( recipient ) => {
455- if ( recipient . address . startsWith ( ScriptRecipientPrefix ) ) {
456- const { address, ...rest } = recipient ;
457- return { ...rest , script : address . replace ( ScriptRecipientPrefix , '' ) } ;
458- }
459- return recipient ;
460- } )
461- : params . recipients ;
452+ if ( params . recipients !== undefined ) {
453+ params . recipients =
454+ params . recipients instanceof Array
455+ ? params ?. recipients ?. map ( ( recipient ) => {
456+ if ( recipient . address . startsWith ( ScriptRecipientPrefix ) ) {
457+ const { address, ...rest } = recipient ;
458+ return { ...rest , script : address . replace ( ScriptRecipientPrefix , '' ) } ;
459+ }
460+ return recipient ;
461+ } )
462+ : params . recipients ;
463+ }
462464
463465 return params ;
464466 }
@@ -594,25 +596,47 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
594596 assert ( txParams . rbfTxIds . length === 1 ) ;
595597
596598 const txToBeReplaced = await wallet . getTransaction ( { txHash : txParams . rbfTxIds [ 0 ] , includeRbf : true } ) ;
597- expectedOutputs = txToBeReplaced . outputs
598- . filter ( ( output ) => output . wallet !== wallet . id ( ) ) // For self-sends, the walletId will be the same as the wallet's id
599- . map ( ( output ) => {
600- return { amount : BigInt ( output . valueString ) , address : this . canonicalAddress ( output . address ) } ;
601- } ) ;
599+ expectedOutputs = txToBeReplaced . outputs . flatMap (
600+ ( output : { valueString : string ; address ?: string ; wallet ?: string } ) => {
601+ // For self-sends, the walletId will be the same as the wallet's id
602+ if ( output . wallet === wallet . id ( ) ) {
603+ return [ ] ;
604+ }
605+ // In the case that this is an OP_RETURN output or another non-encodable scriptPubkey, we dont have an address.
606+ // We will verify that the amount is zero, and if it isnt then we will throw an error.
607+ if ( ! output . address ) {
608+ if ( output . valueString !== '0' ) {
609+ throw new Error ( `Only zero amounts allowed for non-encodeable scriptPubkeys: ${ JSON . stringify ( output ) } ` ) ;
610+ }
611+ return [ { amount : BigInt ( 0 ) } ] ;
612+ }
613+ return [ { amount : BigInt ( output . valueString ) , address : this . canonicalAddress ( output . address ) } ] ;
614+ }
615+ ) ;
602616 } else {
603617 // verify that each recipient from txParams has their own output
604- expectedOutputs = _ . get ( txParams , 'recipients' , [ ] as TransactionRecipient [ ] ) . map ( ( output ) => {
605- return { ...output , address : this . canonicalAddress ( output . address ) } ;
618+ expectedOutputs = _ . get ( txParams , 'recipients' , [ ] as TransactionRecipient [ ] ) . flatMap ( ( output ) => {
619+ if ( output . address === undefined ) {
620+ if ( output . amount . toString ( ) !== '0' ) {
621+ throw new Error ( `Only zero amounts allowed for non-encodeable scriptPubkeys: ${ output } ` ) ;
622+ }
623+ return [ output ] ;
624+ }
625+ return [ { ...output , address : this . canonicalAddress ( output . address ) } ] ;
606626 } ) ;
607627 if ( params . txParams . allowExternalChangeAddress && params . txParams . changeAddress ) {
608628 // when an external change address is explicitly specified, count all outputs going towards that
609629 // address in the expected outputs (regardless of the output amount)
610630 expectedOutputs . push (
611- ...allOutputs
612- . map ( ( output ) => {
613- return { ...output , address : this . canonicalAddress ( output . address ) } ;
614- } )
615- . filter ( ( output ) => output . address === this . canonicalAddress ( params . txParams . changeAddress as string ) )
631+ ...allOutputs . flatMap ( ( output ) => {
632+ if (
633+ output . address === undefined ||
634+ output . address !== this . canonicalAddress ( params . txParams . changeAddress as string )
635+ ) {
636+ return [ ] ;
637+ }
638+ return [ { ...output , address : this . canonicalAddress ( output . address ) } ] ;
639+ } )
616640 ) ;
617641 }
618642 }
0 commit comments