From 9e73a97c53d107fd95fa53c6877ac7ddce77a144 Mon Sep 17 00:00:00 2001 From: Phoebe Lartisant Date: Thu, 23 May 2024 10:25:08 +0200 Subject: [PATCH 1/2] add checkAddressFormat in utils --- src/lib/util/checkAddressFormat.ts | 18 ++++++++++++++++++ src/lib/util/hooks/checkAddressFormat.test.ts | 15 +++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/lib/util/checkAddressFormat.ts create mode 100644 src/lib/util/hooks/checkAddressFormat.test.ts diff --git a/src/lib/util/checkAddressFormat.ts b/src/lib/util/checkAddressFormat.ts new file mode 100644 index 00000000..77741b90 --- /dev/null +++ b/src/lib/util/checkAddressFormat.ts @@ -0,0 +1,18 @@ +import { Address } from '@massalabs/massa-web3/'; + +/** + * Checks if a recipient address is in the correct format. + * @param recipient - The recipient address to check. + * @returns `true` if the address is in the correct format, `false` otherwise. + */ + +export function checkAddressFormat(recipient: string): boolean { + try { + // eslint-disable-next-line no-new + new Address(recipient); + } catch (error) { + return false; + } + + return true; +} diff --git a/src/lib/util/hooks/checkAddressFormat.test.ts b/src/lib/util/hooks/checkAddressFormat.test.ts new file mode 100644 index 00000000..26300200 --- /dev/null +++ b/src/lib/util/hooks/checkAddressFormat.test.ts @@ -0,0 +1,15 @@ +import { checkAddressFormat } from '../checkAddressFormat'; + +describe('checkAddressFormat', () => { + test('should return true for a valid address', () => { + expect( + checkAddressFormat( + 'AU1WdPf44f1taEKRpafeEoAj9c6pHiJmCevH865U8MryzmsFdbAW', + ), + ).toBe(true); + }); + + test('should return false for an invalid address', () => { + expect(checkAddressFormat('0x17ZHH18sja19zjsjn19JE91JJDNJEU')).toBe(false); + }); +}); From 82bb9133de0fd00b6d0f5f3e11e4892a8312106f Mon Sep 17 00:00:00 2001 From: Phoebe Lartisant Date: Thu, 23 May 2024 15:21:26 +0200 Subject: [PATCH 2/2] change token dir --- src/lib/util/{hooks => }/checkAddressFormat.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/lib/util/{hooks => }/checkAddressFormat.test.ts (86%) diff --git a/src/lib/util/hooks/checkAddressFormat.test.ts b/src/lib/util/checkAddressFormat.test.ts similarity index 86% rename from src/lib/util/hooks/checkAddressFormat.test.ts rename to src/lib/util/checkAddressFormat.test.ts index 26300200..eb63a7a6 100644 --- a/src/lib/util/hooks/checkAddressFormat.test.ts +++ b/src/lib/util/checkAddressFormat.test.ts @@ -1,4 +1,4 @@ -import { checkAddressFormat } from '../checkAddressFormat'; +import { checkAddressFormat } from './checkAddressFormat'; describe('checkAddressFormat', () => { test('should return true for a valid address', () => {