From f7c4e39e4d7485b54bebf17e50de924786b10ec7 Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Mon, 10 Jun 2024 12:31:37 +0200 Subject: [PATCH] Improve checkAddress. Add checkAddressOrPubkey --- lib/displayHelpers.ts | 60 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/lib/displayHelpers.ts b/lib/displayHelpers.ts index 287b261..67c270d 100644 --- a/lib/displayHelpers.ts +++ b/lib/displayHelpers.ts @@ -113,6 +113,25 @@ function examplePubkey(index: number): string { return toBase64(data); } +function digestAddressError(error: unknown) { + if (!(error instanceof Error)) { + return "Expected a bech32 address"; + } + + const msg = error.message.toLowerCase(); + + switch (true) { + case msg.includes("too short"): + return "Too short"; + case msg.includes("no separator character"): + return "No separator character found"; + case msg.includes("invalid checksum"): + return "Invalid checksum"; + default: + return error.message; + } +} + /** * Returns an error message for invalid addresses. * Returns null of there is no error. @@ -126,9 +145,8 @@ const checkAddress = (input: string, chainAddressPrefix: string | null) => { let prefix; try { ({ data, prefix } = fromBech32(input)); - // eslint-disable-next-line @typescript-eslint/no-explicit-any - } catch (error: any) { - return error.toString(); + } catch (error: unknown) { + return digestAddressError(error); } if (chainAddressPrefix) { @@ -146,6 +164,27 @@ const checkAddress = (input: string, chainAddressPrefix: string | null) => { return null; }; +/** + * Returns an error message for invalid addresses or pubkeys. + * Returns null of there is no error. + */ +const checkAddressOrPubkey = (input: string, chainAddressPrefix: string) => { + if (!input) { + return "Empty"; + } + + if (!input.startsWith(chainAddressPrefix)) { + try { + fromBase64(input); + return null; + } catch { + return "Public key should be valid Base64"; + } + } + + return checkAddress(input, chainAddressPrefix); +}; + /** * Returns a link to a transaction in an explorer if an explorer is configured * for transactions. Returns null otherwise. @@ -183,16 +222,17 @@ const trimStringsObj = >(obj: StringsO }; export { - thinSpace, capitalizeFirstLetter, + checkAddress, + checkAddressOrPubkey, ellideMiddle, + exampleAddress, + examplePubkey, + exampleValidatorAddress, + explorerLinkAccount, + explorerLinkTx, printableCoin, printableCoins, - exampleAddress, - exampleValidatorAddress, - examplePubkey, - checkAddress, - explorerLinkTx, - explorerLinkAccount, + thinSpace, trimStringsObj, };