Skip to content

Commit

Permalink
fix(@fireblocks/recovery-shared): 🐛 fixed address validation for TRX …
Browse files Browse the repository at this point in the history
…& ADA_TEST
  • Loading branch information
a0ngo committed Oct 23, 2024
1 parent 28c8ebd commit 665e844
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/pretty-tools-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fireblocks/asset-config': patch
---

Fixed address validation for TRX & ADA_TEST
17 changes: 16 additions & 1 deletion packages/shared/lib/validateAddress.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions packages/shared/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ describe('AddressValidator', () => {
address: 'TQChTm3VxQc2gptvp4Rfm4ayoQ7wyvzj1B',
assetId: 'TRX',
},
{
networkProtocol: 'TRX',
address: 'TCqjeroh5XyCQwMHL7Csz5yJttWTPSsH3n',
assetId: 'TRX_TEST',
},
{
networkProtocol: 'SOL',
address: 'Es8cMivoZYEsCfKBYvZEkgeyYN3saNpzfkoagUBCLy5p',
Expand All @@ -88,6 +93,11 @@ describe('AddressValidator', () => {
address: 'addr1qxk0uuhghpk2zxpzth5ryv20fmfem3ym4pxektwjzvfumrchg6feqy3pzpzm63kpvt2ayh5qcwxf8prey4swhgmqld6srxkf3u',
assetId: 'ADA',
},
{
networkProtocol: 'ADA',
address: 'addr_test1qrpv7sases4yuljdkmg806335syqmfl30aaamy959hywx8gy80s43lrcxu6qttpl2a6h3lr8f02ff54nywzry4kge8lqgsdf0f',
assetId: 'ADA_TEST',
},
{
networkProtocol: 'DOT',
address: 'E8rsLhGkb2oALVKtFPEGXr47kF9pUKtUu1XRu1JPdwdGpgb',
Expand Down

0 comments on commit 665e844

Please sign in to comment.