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/contract address validation #1192

Merged
merged 4 commits into from
Jan 14, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- [#1192](https://github.com/alleslabs/celatone-frontend/pull/1192) Add fixed bytes hex address util
- [#1189](https://github.com/alleslabs/celatone-frontend/pull/1189) Add constructor args to EVM contract verify page
- [#1187](https://github.com/alleslabs/celatone-frontend/pull/1187) Add onboarding section to EVM contract details page and add EVM contract verify page
- [#1184](https://github.com/alleslabs/celatone-frontend/pull/1184) Add custom layer to Initia Widget
Expand Down
6 changes: 3 additions & 3 deletions src/lib/pages/evm-contract-verify/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
zEvmContractVerifyForm,
} from "./types";
import { zodResolver } from "@hookform/resolvers/zod";
import { isHexModuleAddress, isHexWalletAddress, truncate } from "lib/utils";
import { isHex20Bytes, truncate } from "lib/utils";
import { EvmContractVerifySolidity } from "./components/solidity/EvmContractVerifySolidity";
import { EvmContractVerifyVyper } from "./components/vyper/EvmContractVerifyVyper";
import { NoMobile } from "lib/components/modal";
Expand Down Expand Up @@ -47,7 +47,7 @@ export const EvmContractVerify = () => {
mode: "all",
reValidateMode: "onChange",
defaultValues: {
contractAddress: isHexWalletAddress(String(contractAddressQueryParam))
contractAddress: isHex20Bytes(String(contractAddressQueryParam))
? contractAddressQueryParam
: "",
compilerVersion: "",
Expand Down Expand Up @@ -153,7 +153,7 @@ export const EvmContractVerify = () => {
control={control}
variant="fixed-floating"
status={{
state: isHexModuleAddress(contractAddress)
state: isHex20Bytes(contractAddress)
? "success"
: "init",
}}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/pages/evm-contract-verify/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { zHexAddr20 } from "lib/types";
import { isHexWalletAddress } from "lib/utils";
import { isHex20Bytes } from "lib/utils";
import { z, ZodIssueCode } from "zod";

export enum EvmProgrammingLanguage {
Expand Down Expand Up @@ -86,7 +86,7 @@ const zEvmContractAddressAndLicenseForm = z.object({
message: " ",
});

if (!isHexWalletAddress(val))
if (!isHex20Bytes(val))
ctx.addIssue({
code: ZodIssueCode.custom,
message: "Invalid address",
Expand Down
13 changes: 13 additions & 0 deletions src/lib/utils/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,23 @@ const isHexAddress = (address: string, length: number): boolean => {
return isHex(strip);
};

const isHexFixedBytes = (address: string, length: number): boolean => {
const regex = new RegExp(`^0x[a-fA-F0-9]{${length}}$`);
if (!regex.test(address)) {
return false;
}

const strip = padHexAddress(address as HexAddr, length).slice(2);
return isHex(strip);
};

export const isHexWalletAddress = (address: string) =>
isHexAddress(address, HEX_WALLET_ADDRESS_LENGTH);

export const isHexModuleAddress = (address: string) =>
isHexAddress(address, HEX_MODULE_ADDRESS_LENGTH);

export const isHex20Bytes = (address: string) =>
isHexFixedBytes(address, HEX_WALLET_ADDRESS_LENGTH);

export const is0x = (address: string): boolean => address === "0x";
Loading