Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix address util nullish fallback handling #3181

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polite-lobsters-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperlane-xyz/utils': patch
---

Fix for address utils falsy fallbacks
3 changes: 2 additions & 1 deletion typescript/utils/src/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}$/;
Expand Down Expand Up @@ -67,7 +68,7 @@ function routeAddressUtil<T>(
) {
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}`);
}

Expand Down
7 changes: 4 additions & 3 deletions typescript/utils/src/typeof.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export function isNullish<T>(val: T) {
if (val === null || val === undefined) return true;
else return false;
export function isNullish<T>(
val: T,
): val is T extends null | undefined ? T : never {
return val === null || val === undefined;
}
jmrossy marked this conversation as resolved.
Show resolved Hide resolved

export function isNumeric(value: string | number) {
Expand Down
Loading