@@ -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 /**
0 commit comments