Skip to content

Commit

Permalink
simplify address validation, include solana addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysu committed Jan 22, 2025
1 parent 4f5d8d9 commit fe2392e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export function SendTransactionForm({
<Controller
name="tokenId"
control={control}
defaultValue={balances.find((b) => b.token.symbol === 'USDC')?.token.id ?? ''}
render={({ field }) => (
<TokenSelect
balances={balances}
Expand Down
31 changes: 7 additions & 24 deletions packages/circle-demo-webapp/app/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,16 @@ export async function callGetFetch<ReturnType>(
return (await res.json()) as ReturnType;
}

export type ValidInputTypes = Uint8Array | bigint | string | number | boolean;
export const isSolanaAddress = (value: string): boolean => {
return /^[1-9A-HJ-NP-Za-km-z]{43,44}$/.test(value);
};

export const isHexStrict = (hex: ValidInputTypes) =>
typeof hex === 'string' && /^((-)?0x[0-9a-f]+|(0x))$/i.test(hex);
const isEthAddress = (address: string): boolean => {
return /^0x[a-fA-F0-9]{40}$/.test(address);
};

export const isAddress = (value: string): boolean => {
let valueToCheck: string;

if (!isHexStrict(value)) {
valueToCheck = value.toLowerCase().startsWith('0x') ? value : `0x${value}`;
} else {
valueToCheck = value;
}

// check if it has the basic requirements of an address
if (!/^(0x)?[0-9a-f]{40}$/i.test(valueToCheck)) {
return false;
}
// If it's ALL lowercase or ALL upppercase
if (
/^(0x|0X)?[0-9a-f]{40}$/.test(valueToCheck) ||
/^(0x|0X)?[0-9A-F]{40}$/.test(valueToCheck)
) {
return true;
// Otherwise check each case
}
return true;
return isSolanaAddress(value) || isEthAddress(value);
};

/**
Expand Down

0 comments on commit fe2392e

Please sign in to comment.