Skip to content

Commit df6a0fc

Browse files
Merge pull request #5019 from BitGo/BTC-1553-false-invalid-bch-cashaddr
BTC-1553: fix isValidAddress returning false for valid cashaddr bch addresses
2 parents 189a0fc + 74be031 commit df6a0fc

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

modules/abstract-utxo/src/abstractUtxoCoin.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,19 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
415415
throw new Error('deprecated');
416416
}
417417

418-
const formats = param && param.anyFormat ? undefined : ['default' as const];
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;
419421
try {
420-
const script = utxolib.addressFormat.toOutputScriptTryFormats(address, this.network, formats);
421-
return address === utxolib.address.fromOutputScript(script, this.network);
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);
422431
} catch (e) {
423432
return false;
424433
}

modules/bitgo/test/v2/unit/wallet.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,18 @@ describe('V2 Wallet:', function () {
242242
});
243243
});
244244

245+
it('should verify bch cashaddr format as valid', async function () {
246+
const coin = bitgo.coin('tbch');
247+
const valid = coin.isValidAddress('bchtest:pzfkxv532t0q5zchck2mhmmf2y02cdejyssq5qrz7a');
248+
valid.should.be.True();
249+
});
250+
251+
it('should verify bch legacy format as valid', async function () {
252+
const coin = bitgo.coin('tbch');
253+
const valid = coin.isValidAddress('2N6gY9r9iuXQQzZiSyngWJeoUuL5mC1x4Ac');
254+
valid.should.be.True();
255+
});
256+
245257
describe('TETH Wallet Addresses', function () {
246258
let ethWallet;
247259

0 commit comments

Comments
 (0)