@@ -415,24 +415,19 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
415415 throw new Error ( 'deprecated' ) ;
416416 }
417417
418- // Default to anyFormat to true - if anyFormat is false then only check the default. Otherwise, check
419- // all formats
420- const anyFormat = ( param as { anyFormat : boolean | undefined } ) ?. anyFormat ;
421- const formats =
422- anyFormat === undefined || anyFormat
423- ? utxolib . addressFormat . addressFormats . filter ( ( format ) =>
424- utxolib . addressFormat . isSupportedAddressFormat ( format , this . network )
425- )
426- : [ 'default' as const ] ;
427-
418+ // By default, allow all address formats.
419+ // At the time of writing, the only additional address format is bch cashaddr.
420+ const anyFormat = ( param as { anyFormat : boolean } | undefined ) ?. anyFormat ?? true ;
428421 try {
429- // To make sure that we are preserving `fromOutputScript(script, network) === address`, we need to do the
430- // circular check below. An example of something that this protects against is uppercase Bech32.
431- const script = utxolib . addressFormat . toOutputScriptTryFormats ( address , this . network , formats ) ;
432- const genAddresses = formats . map ( ( format ) =>
433- utxolib . addressFormat . fromOutputScriptWithFormat ( script , format , this . network )
434- ) ;
435- return genAddresses . includes ( address ) ;
422+ // Find out if the address is valid for any format. Tries all supported formats by default.
423+ // Throws if address cannot be decoded with any format.
424+ const [ format , script ] = utxolib . addressFormat . toOutputScriptAndFormat ( address , this . network ) ;
425+ // unless anyFormat is set, only 'default' is allowed.
426+ if ( ! anyFormat && format !== 'default' ) {
427+ return false ;
428+ }
429+ // make sure that address is in normal representation for given format.
430+ return address === utxolib . addressFormat . fromOutputScriptWithFormat ( script , format , this . network ) ;
436431 } catch ( e ) {
437432 return false ;
438433 }
0 commit comments