Skip to content

Commit 9ad6d79

Browse files
fix: changed aleo validation to validate the token_id format (#1643)
1 parent a5ee3af commit 9ad6d79

File tree

4 files changed

+48
-51
lines changed

4 files changed

+48
-51
lines changed

packages/currency/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"@requestnetwork/utils": "0.54.0",
4848
"@ton/core": "0.61.0",
4949
"@ton/crypto": "3.3.0",
50-
"bech32": "2.0.0",
5150
"multicoin-address-validator": "0.5.15",
5251
"node-dijkstra": "2.5.0",
5352
"starknet": "7.6.4",

packages/currency/src/currencyManager.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { utils } from 'ethers';
33
import { Address } from '@ton/core';
44
import { validateAndParseAddress } from 'starknet';
55
import addressValidator from 'multicoin-address-validator';
6-
import { bech32 } from 'bech32';
76
import { getSupportedERC20Tokens } from './erc20';
87
import { getSupportedERC777Tokens } from './erc777';
98
import { getHash } from './getHash';
@@ -326,22 +325,26 @@ export class CurrencyManager<TMeta = unknown> implements CurrencyTypes.ICurrency
326325
}
327326

328327
/**
329-
* Validate an Aleo address using proper Bech32 validation with checksum verification.
330-
* Aleo addresses use Bech32 encoding with:
331-
* - HRP (Human Readable Part): "aleo"
332-
* - Separator: "1"
333-
* - Data + checksum: 58 characters
334-
* - Total length: 63 characters
335-
* - Strict Bech32 character set with checksum validation
328+
* Validate an Aleo currency address (field element).
329+
* Aleo currency addresses are field elements with exactly 76 digits followed by "field".
330+
* See https://developer.aleo.org/guides/standards/token_registry#token-registry-program-constants
331+
* And https://developer.aleo.org/concepts/fundamentals/accounts#prime-fields
336332
*
337-
* See https://namespaces.chainagnostic.org/aleo/caip10 for more details.
338-
* @param address - The address to validate
333+
* Examples:
334+
* - 7311977476241952331367670434347097026669181172395481678807963832961201831695field
335+
* - 6088188135219746443092391282916151282477828391085949070550825603498725268775field
336+
*
337+
* @param address - The Aleo currency address to validate
339338
* @returns True if the address is valid, false otherwise
340339
*/
341340
validateAleoAddress(address: string): boolean {
342341
try {
343-
const { prefix } = bech32.decode(address);
344-
return prefix === 'aleo';
342+
if (!address || typeof address !== 'string' || !address.endsWith('field')) {
343+
return false;
344+
}
345+
346+
const numericPart = address.slice(0, -5);
347+
return numericPart.length === 76 && /^\d+$/.test(numericPart);
345348
} catch {
346349
return false;
347350
}

packages/currency/test/currencyManager.test.ts

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -767,44 +767,39 @@ describe('CurrencyManager', () => {
767767
});
768768

769769
describe('validateAleoAddress', () => {
770-
it('should validate correct Aleo addresses', () => {
771-
const validAddress = 'aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8';
772-
expect(currencyManager.validateAleoAddress(validAddress)).toBe(true);
773-
expect(currencyManager.validateAleoAddress(validAddress.toUpperCase())).toBe(true);
774-
});
775-
776-
it('should reject invalid Aleo addresses', () => {
777-
const invalidAddresses = [
778-
// Empty or null inputs
779-
'',
780-
' ',
781-
null,
782-
undefined,
783-
// Wrong prefix
784-
'bitcoin1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8',
785-
'cosmos1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8',
786-
// Mixed case
787-
'aleo1Qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8',
788-
// Wrong format
789-
'aleo1',
790-
'aleo1abc',
791-
'not-an-address',
792-
'random-string',
793-
// Invalid characters that would pass simple regex but fail Bech32
794-
'aleo1' + 'b'.repeat(58), // 'b' not in Bech32 alphabet
795-
'aleo1' + 'i'.repeat(58), // 'i' not in Bech32 alphabet
796-
'aleo1' + 'o'.repeat(58), // 'o' not in Bech32 alphabet
797-
// Non-string inputs
798-
123,
799-
{},
800-
[],
801-
// valid address with whitespace
802-
' aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8 ',
803-
];
770+
it('should validate correct Aleo field elements', () => {
771+
// Known valid Aleo field elements
772+
expect(
773+
currencyManager.validateAleoAddress(
774+
'7311977476241952331367670434347097026669181172395481678807963832961201831695field',
775+
),
776+
).toBe(true);
777+
expect(
778+
currencyManager.validateAleoAddress(
779+
'6088188135219746443092391282916151282477828391085949070550825603498725268775field',
780+
),
781+
).toBe(true);
782+
});
804783

805-
invalidAddresses.forEach((address) => {
806-
expect(currencyManager.validateAleoAddress(address as any)).toBe(false);
807-
});
784+
it('should reject invalid addresses', () => {
785+
expect(
786+
currencyManager.validateAleoAddress(
787+
'7311977476241952331367670434347097026669181172395481678807963832961201831695',
788+
),
789+
).toBe(false);
790+
expect(currencyManager.validateAleoAddress('123FIELD')).toBe(false);
791+
expect(currencyManager.validateAleoAddress('123field')).toBe(false);
792+
expect(
793+
currencyManager.validateAleoAddress(
794+
'731197747624195233136767043434709702666918117239548167880796383296120183169512345field',
795+
),
796+
).toBe(false);
797+
expect(
798+
currencyManager.validateAleoAddress(
799+
'73119774762419523313676704343470970266691811723954816788079638329612018316abfield',
800+
),
801+
).toBe(false);
802+
expect(currencyManager.validateAleoAddress('')).toBe(false);
808803
});
809804
});
810805
});

packages/request-client.js/test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1653,7 +1653,7 @@ describe('request-client.js', () => {
16531653
['solana', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'],
16541654
['ton', 'EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs'],
16551655
['starknet', '0x028757d11c97078Dd182023B1cC7b9E7659716c631ADF94D24f1fa7Dc5943072'],
1656-
['aleo', 'aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8'],
1656+
['aleo', '7311977476241952331367670434347097026669181172395481678807963832961201831695field'],
16571657
];
16581658

16591659
it.each(cases)(

0 commit comments

Comments
 (0)