From 665e84419b875cacf873616935ee79af35fd206b Mon Sep 17 00:00:00 2001 From: agoltzman <106151463+agoltzman@users.noreply.github.com> Date: Wed, 23 Oct 2024 14:02:05 +0300 Subject: [PATCH] fix(@fireblocks/recovery-shared): :bug: fixed address validation for TRX & ADA_TEST --- .changeset/pretty-tools-share.md | 5 +++++ packages/shared/lib/validateAddress.ts | 17 ++++++++++++++++- packages/shared/tests/index.test.ts | 10 ++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 .changeset/pretty-tools-share.md diff --git a/.changeset/pretty-tools-share.md b/.changeset/pretty-tools-share.md new file mode 100644 index 0000000..2288565 --- /dev/null +++ b/.changeset/pretty-tools-share.md @@ -0,0 +1,5 @@ +--- +'@fireblocks/asset-config': patch +--- + +Fixed address validation for TRX & ADA_TEST diff --git a/packages/shared/lib/validateAddress.ts b/packages/shared/lib/validateAddress.ts index 969f4c2..e52e30a 100644 --- a/packages/shared/lib/validateAddress.ts +++ b/packages/shared/lib/validateAddress.ts @@ -1,8 +1,8 @@ import * as WAValidator from 'multicoin-address-validator'; import { bech32 } from 'bech32'; +import { isTestnetAsset } from '@fireblocks/asset-config'; import { getLogger } from './getLogger'; import { LOGGER_NAME_SHARED } from '../constants'; -import { isTestnetAsset } from '@fireblocks/asset-config'; const logger = getLogger(LOGGER_NAME_SHARED); @@ -30,6 +30,7 @@ export class AddressValidator { public isValidAddress(address: string, networkProtocol: string | undefined, assetId: string): boolean { try { + this.patchValidator(); let isValid = false; let validationOptions: WAValidator.ValidateOpts = {}; let asset = assetId; @@ -58,6 +59,20 @@ export class AddressValidator { } } + /** + * Fixes the validator dependency to fix some validations. + */ + private patchValidator() { + // Fix cardano testnet prefix: + const adaTestBech32 = WAValidator.findCurrency('ADA')?.bech32Hrp.testnet; + adaTestBech32.pop(); + adaTestBech32.push('addr_test'); + + const trxTestAddrTypes = WAValidator.findCurrency('TRX')?.addressTypes.testnet; + trxTestAddrTypes.pop(); + trxTestAddrTypes.push(0x41); + } + private doesValidatorExist(networkProtocol: string | undefined): boolean { return !!networkProtocol && !!WAValidator.findCurrency(networkProtocol); } diff --git a/packages/shared/tests/index.test.ts b/packages/shared/tests/index.test.ts index 5836211..5353078 100644 --- a/packages/shared/tests/index.test.ts +++ b/packages/shared/tests/index.test.ts @@ -73,6 +73,11 @@ describe('AddressValidator', () => { address: 'TQChTm3VxQc2gptvp4Rfm4ayoQ7wyvzj1B', assetId: 'TRX', }, + { + networkProtocol: 'TRX', + address: 'TCqjeroh5XyCQwMHL7Csz5yJttWTPSsH3n', + assetId: 'TRX_TEST', + }, { networkProtocol: 'SOL', address: 'Es8cMivoZYEsCfKBYvZEkgeyYN3saNpzfkoagUBCLy5p', @@ -88,6 +93,11 @@ describe('AddressValidator', () => { address: 'addr1qxk0uuhghpk2zxpzth5ryv20fmfem3ym4pxektwjzvfumrchg6feqy3pzpzm63kpvt2ayh5qcwxf8prey4swhgmqld6srxkf3u', assetId: 'ADA', }, + { + networkProtocol: 'ADA', + address: 'addr_test1qrpv7sases4yuljdkmg806335syqmfl30aaamy959hywx8gy80s43lrcxu6qttpl2a6h3lr8f02ff54nywzry4kge8lqgsdf0f', + assetId: 'ADA_TEST', + }, { networkProtocol: 'DOT', address: 'E8rsLhGkb2oALVKtFPEGXr47kF9pUKtUu1XRu1JPdwdGpgb',