Skip to content

Commit 679e775

Browse files
committed
Coderabbitai feedback, improved aleo validation with bech32 check and added tests
1 parent 56e7ee2 commit 679e775

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

packages/currency/package.json

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

packages/currency/src/currencyManager.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ 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';
67
import { getSupportedERC20Tokens } from './erc20';
78
import { getSupportedERC777Tokens } from './erc777';
89
import { getHash } from './getHash';
@@ -325,20 +326,22 @@ export class CurrencyManager<TMeta = unknown> implements CurrencyTypes.ICurrency
325326
}
326327

327328
/**
328-
* Validate an Aleo address using the official format specification.
329-
* Note we do not use the @provablehq/sdk package because it is ESM-only
330-
* For regex validation, see https://namespaces.chainagnostic.org/aleo/caip10 for more details.
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
331336
* @param address - The address to validate
332337
* @returns True if the address is valid, false otherwise
333338
*/
334339
validateAleoAddress(address: string): boolean {
335340
try {
336-
if (!address || typeof address !== 'string') {
337-
return false;
338-
}
339-
340-
const aleoAddressRegex = /^aleo1[a-z0-9]{58}$/;
341-
return aleoAddressRegex.test(address);
341+
const input = address?.trim();
342+
if (!input) return false;
343+
const { prefix } = bech32.decode(input.toLowerCase());
344+
return prefix === 'aleo';
342345
} catch {
343346
return false;
344347
}

packages/currency/test/currencyManager.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,4 +765,48 @@ describe('CurrencyManager', () => {
765765
});
766766
});
767767
});
768+
769+
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+
expect(currencyManager.validateAleoAddress(` ${validAddress} `)).toBe(true);
775+
});
776+
777+
it('should reject invalid Aleo addresses', () => {
778+
const invalidAddresses = [
779+
// Empty or null inputs
780+
'',
781+
' ',
782+
null,
783+
undefined,
784+
// Wrong prefix
785+
'bitcoin1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8',
786+
'cosmos1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8',
787+
// Wrong format
788+
'aleo1',
789+
'aleo1abc',
790+
'not-an-address',
791+
'random-string',
792+
// Invalid characters that would pass simple regex but fail Bech32
793+
'aleo1' + 'b'.repeat(58), // 'b' not in Bech32 alphabet
794+
'aleo1' + 'i'.repeat(58), // 'i' not in Bech32 alphabet
795+
'aleo1' + 'o'.repeat(58), // 'o' not in Bech32 alphabet
796+
// Non-string inputs
797+
123,
798+
{},
799+
[],
800+
];
801+
802+
invalidAddresses.forEach((address) => {
803+
expect(currencyManager.validateAleoAddress(address as any)).toBe(false);
804+
});
805+
});
806+
807+
it('should handle whitespace trimming', () => {
808+
expect(currencyManager.validateAleoAddress(' ')).toBe(false);
809+
expect(currencyManager.validateAleoAddress('')).toBe(false);
810+
});
811+
});
768812
});

0 commit comments

Comments
 (0)