From df24eec8bea1e8e62f3e56a032eaf0abd731f53a Mon Sep 17 00:00:00 2001 From: J M Rossy Date: Wed, 24 Jan 2024 16:49:22 -0500 Subject: [PATCH] Fix address util nullish fallback handling (#3181) ### Description Small fix for address utils ### Related issues https://github.com/hyperlane-xyz/issues/issues/899 ### Backward compatibility Yes ### Testing In warp UI --- .changeset/polite-lobsters-boil.md | 5 +++++ typescript/utils/src/addresses.ts | 3 ++- typescript/utils/src/typeof.ts | 7 ++++--- 3 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 .changeset/polite-lobsters-boil.md diff --git a/.changeset/polite-lobsters-boil.md b/.changeset/polite-lobsters-boil.md new file mode 100644 index 0000000000..04f60468bb --- /dev/null +++ b/.changeset/polite-lobsters-boil.md @@ -0,0 +1,5 @@ +--- +'@hyperlane-xyz/utils': patch +--- + +Fix for address utils falsy fallbacks diff --git a/typescript/utils/src/addresses.ts b/typescript/utils/src/addresses.ts index 8dec0f7353..47852c4525 100644 --- a/typescript/utils/src/addresses.ts +++ b/typescript/utils/src/addresses.ts @@ -2,6 +2,7 @@ import { fromBech32, normalizeBech32, toBech32 } from '@cosmjs/encoding'; import { PublicKey } from '@solana/web3.js'; import { utils as ethersUtils } from 'ethers'; +import { isNullish } from './typeof'; import { Address, HexString, ProtocolType } from './types'; const EVM_ADDRESS_REGEX = /^0x[a-fA-F0-9]{40}$/; @@ -67,7 +68,7 @@ function routeAddressUtil( ) { protocol ||= getAddressProtocolType(param); if (protocol && fns[protocol]) return fns[protocol]!(param); - else if (fallback) return fallback; + else if (!isNullish(fallback)) return fallback; else throw new Error(`Unsupported protocol ${protocol}`); } diff --git a/typescript/utils/src/typeof.ts b/typescript/utils/src/typeof.ts index eb08c4f15a..a204981dbb 100644 --- a/typescript/utils/src/typeof.ts +++ b/typescript/utils/src/typeof.ts @@ -1,6 +1,7 @@ -export function isNullish(val: T) { - if (val === null || val === undefined) return true; - else return false; +export function isNullish( + val: T, +): val is T extends null | undefined ? T : never { + return val === null || val === undefined; } export function isNumeric(value: string | number) {