@@ -76,7 +76,15 @@ import ScriptType2Of3 = utxolib.bitgo.outputScripts.ScriptType2Of3;
7676import { isReplayProtectionUnspent } from './replayProtection' ;
7777import { signAndVerifyPsbt , signAndVerifyWalletTransaction } from './sign' ;
7878import { supportedCrossChainRecoveries } from './config' ;
79- import { explainPsbt , explainTx , getPsbtTxInputs , getTxInputs } from './transaction' ;
79+ import {
80+ assertValidTransactionRecipient ,
81+ explainPsbt ,
82+ explainTx ,
83+ fromExtendedAddressFormat ,
84+ getPsbtTxInputs ,
85+ getTxInputs ,
86+ isExtendedAddressFormat ,
87+ } from './transaction' ;
8088import { assertDescriptorWalletAddress } from './descriptor/assertDescriptorWalletAddress' ;
8189
8290type UtxoCustomSigningFunction < TNumber extends number | bigint > = {
@@ -104,7 +112,6 @@ type UtxoCustomSigningFunction<TNumber extends number | bigint> = {
104112 } ) : Promise < SignedTransaction > ;
105113} ;
106114
107- export const ScriptRecipientPrefix = 'scriptPubkey:' ;
108115const { getExternalChainCode, isChainCode, scriptTypeForChain, outputScripts } = bitgo ;
109116type Unspent < TNumber extends number | bigint = number > = bitgo . Unspent < TNumber > ;
110117
@@ -453,11 +460,8 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
453460 params . recipients =
454461 params . recipients instanceof Array
455462 ? 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 ;
463+ const { address, ...rest } = recipient ;
464+ return { ...rest , ...fromExtendedAddressFormat ( address ) } ;
461465 } )
462466 : params . recipients ;
463467 }
@@ -478,12 +482,8 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
478482 }
479483
480484 checkRecipient ( recipient : { address : string ; amount : number | string } ) : void {
481- if ( recipient . address . startsWith ( ScriptRecipientPrefix ) ) {
482- const amount = BigInt ( recipient . amount ) ;
483- if ( amount !== BigInt ( 0 ) ) {
484- throw new Error ( 'Only zero amounts allowed for non-encodeable scriptPubkeys' ) ;
485- }
486- } else {
485+ assertValidTransactionRecipient ( recipient ) ;
486+ if ( ! isExtendedAddressFormat ( recipient . address ) ) {
487487 super . checkRecipient ( recipient ) ;
488488 }
489489 }
@@ -540,6 +540,18 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
540540 return utxolib . bitgo . createTransactionFromHex < TNumber > ( hex , this . network , this . amountType ) ;
541541 }
542542
543+ toCanonicalTransactionRecipient ( output : { valueString : string ; address ?: string } ) : {
544+ amount : bigint ;
545+ address ?: string ;
546+ } {
547+ const amount = BigInt ( output . valueString ) ;
548+ assertValidTransactionRecipient ( { amount, address : output . address } ) ;
549+ if ( ! output . address ) {
550+ return { amount } ;
551+ }
552+ return { amount, address : this . canonicalAddress ( output . address ) } ;
553+ }
554+
543555 /**
544556 * Extract and fill transaction details such as internal/change spend, external spend (explicit vs. implicit), etc.
545557 * @param params
@@ -602,15 +614,7 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
602614 if ( output . wallet === wallet . id ( ) ) {
603615 return [ ] ;
604616 }
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 ) } ] ;
617+ return [ this . toCanonicalTransactionRecipient ( output ) ] ;
614618 }
615619 ) ;
616620 } else {
0 commit comments