Skip to content

Commit 290c03e

Browse files
committed
feat(sdk-coin-icp): enhance address validation and add memo ID support
TICKET: WIN-4759
1 parent cfc5650 commit 290c03e

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

modules/sdk-coin-icp/src/icp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class Icp extends BaseCoin {
7878
}
7979

8080
isValidAddress(address: string): boolean {
81-
throw new Error('Method not implemented.');
81+
return utils.isValidAddress(address);
8282
}
8383

8484
signTransaction(_: SignTransactionOptions): Promise<SignedTransaction> {

modules/sdk-coin-icp/src/lib/utils.ts

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,31 @@ export class Utils implements BaseUtils {
3737
}
3838

3939
/**
40-
* Checks if the provided address is a valid hexadecimal string.
40+
* Checks if the provided address is a valid ICP address.
4141
*
4242
* @param {string} address - The address to validate.
43-
* @returns {boolean} - Returns `true` if the address is a valid 64-character hexadecimal string, otherwise `false`.
43+
* @returns {boolean} - Returns `true` if the address is valid, otherwise `false`.
4444
*/
4545
isValidAddress(address: string): boolean {
46-
return this.isValidHash(address);
46+
const rootAddress = this.validateMemoAndReturnRootAddress(address);
47+
return rootAddress !== undefined && this.isValidHash(rootAddress);
48+
}
49+
50+
/**
51+
* Validates the memo ID in the address and returns the root address.
52+
*
53+
* @param {string} address - The address to validate and extract the root address from.
54+
* @returns {string | undefined} - The root address if valid, otherwise `undefined`.
55+
*/
56+
validateMemoAndReturnRootAddress(address: string): string | undefined {
57+
if (!address) {
58+
return undefined;
59+
}
60+
const [rootAddress, memoId] = address.split('?memoId=');
61+
if (memoId && this.validateMemo(BigInt(memoId))) {
62+
return rootAddress;
63+
}
64+
return address;
4765
}
4866

4967
/**
@@ -268,32 +286,48 @@ export class Utils implements BaseUtils {
268286
return { pub, prv };
269287
}
270288

271-
validateFee(fee: string): void {
272-
if (new BigNumber(fee).isEqualTo(0)) {
273-
throw new BuildTransactionError('Fee equal to zero');
274-
}
275-
if (fee !== this.gasData()) {
276-
throw new BuildTransactionError('Invalid fee value');
289+
/**
290+
* Validates the provided fee.
291+
*
292+
* @param {string} fee - The fee to validate.
293+
* @throws {BuildTransactionError} - If the fee is zero or invalid.
294+
*/
295+
validateFee(fee: string): boolean {
296+
const feeValue = new BigNumber(fee);
297+
if (feeValue.isZero()) {
298+
throw new BuildTransactionError('Fee cannot be zero');
277299
}
300+
return true;
278301
}
279302

280303
/** @inheritdoc */
281-
validateValue(value: BigNumber): void {
304+
validateValue(value: BigNumber): boolean {
282305
if (value.isLessThanOrEqualTo(0)) {
283306
throw new BuildTransactionError('amount cannot be less than or equal to zero');
284307
}
308+
return true;
285309
}
286310

287-
validateMemo(memo: number | BigInt): void {
288-
if (Number(memo) < 0 || Number(memo) === null || Number(memo) === undefined || Number.isNaN(Number(memo))) {
311+
/**
312+
* Validates the provided memo.
313+
*
314+
* @param {number | BigInt} memo - The memo to validate.
315+
* @returns {boolean} - Returns `true` if the memo is valid.
316+
* @throws {BuildTransactionError} - If the memo is invalid.
317+
*/
318+
validateMemo(memo: number | BigInt): boolean {
319+
const memoNumber = Number(memo);
320+
if (memoNumber < 0 || Number.isNaN(memoNumber)) {
289321
throw new BuildTransactionError('Invalid memo');
290322
}
323+
return true;
291324
}
292325

293-
validateExpireTime(expireTime: number | BigInt): void {
326+
validateExpireTime(expireTime: number | BigInt): boolean {
294327
if (Number(expireTime) < Date.now() * 1000_000) {
295328
throw new BuildTransactionError('Invalid expiry time');
296329
}
330+
return true;
297331
}
298332

299333
/**

modules/sdk-coin-icp/test/resources/icp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const accounts = {
3434
account6: {
3535
secretKey: '8d08c0393b707cd90c37213025fe7ed13c05b267d946ca1b6e0fd3b0e47ec188',
3636
publicKey: '02ad010ce68b75266c723bf25fbe3a0c48eb29f14b25925b06b7f5026a0f12702e',
37-
address: '2b9b89604362e185544c8bba76cadff1a3af26e1467e8530d13743a08a52dd7b',
37+
address: '2b9b89604362e185544c8bba76cadff1a3af26e1467e8530d13743a08a52dd7b?memoId=0',
3838
},
3939
errorsAccounts: {
4040
account1: {

0 commit comments

Comments
 (0)