Skip to content

Commit

Permalink
Use viem function to check function signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
sembrestels committed Sep 24, 2024
1 parent 55bf1cf commit d7fc41e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/evmcrispr/src/modules/aragonos/commands/act.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import { batchForwarderActions } from "../utils/forwarders";
import { ErrorException } from "../../../errors";
import {
ComparisonType,
SIGNATURE_REGEX,
addressesEqual,
beforeOrEqualNode,
checkArgsLength,
encodeAction,
fetchAbi,
insideNodeLine,
interpretNodeSync,
isFunctionSignature,
tryAndCacheNotFound,
} from "../../../utils";
import type { AragonOS } from "../AragonOS";
Expand Down Expand Up @@ -50,7 +50,7 @@ export const act: ICommand<AragonOS> = {
);
}

if (!SIGNATURE_REGEX.test(signature)) {
if (!isFunctionSignature(signature)) {
throw new ErrorException(
`expected a valid signature, but got ${signature}`,
);
Expand Down
4 changes: 2 additions & 2 deletions packages/evmcrispr/src/modules/std/commands/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type { Abi, AbiBinding, Address, ICommand } from "../../../types";

import {
ComparisonType,
SIGNATURE_REGEX,
addressesEqual,
beforeOrEqualNode,
checkArgsLength,
Expand All @@ -16,6 +15,7 @@ import {
getOptValue,
insideNodeLine,
interpretNodeSync,
isFunctionSignature,
isNumberish,
tryAndCacheNotFound,
} from "../../../utils";
Expand Down Expand Up @@ -58,7 +58,7 @@ export const exec: ICommand<Std> = {
);
}

if (!SIGNATURE_REGEX.test(signature)) {
if (!isFunctionSignature(signature)) {
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(signature)) {
throw new ErrorException(`invalid signature "${signature}"`);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/evmcrispr/src/modules/std/helpers/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { ErrorException } from "../../../errors";
import type { HelperFunction } from "../../../types";
import {
ComparisonType,
SIGNATURE_REGEX,
checkArgsLength,
isFunctionSignature,
} from "../../../utils";
import type { Std } from "../Std";

Expand All @@ -31,7 +31,7 @@ export const get: HelperFunction<Std> = async (

const [body, returns, index] = abi.split(":");

if (!SIGNATURE_REGEX.test(body)) {
if (!isFunctionSignature(body)) {
throw new ErrorException(
`expected a valid function signature, but got "${abi}"`,
);
Expand Down
13 changes: 9 additions & 4 deletions packages/evmcrispr/src/utils/web3.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import type { AbiItem } from "viem";
import { isAddressEqual } from "viem";
import { isAddressEqual, parseAbiItem } from "viem";

import type { Address } from "../types";

// JS regex do not support balancing groups, so we do not check parentheses are balanced
export const SIGNATURE_REGEX =
/^\w+\(((\(?\w+(\[\d*\])*\)?)+(,\(?\w+(\[\d*\])*\)?)*)?\)$/;
export const isFunctionSignature = (signature: string) => {
try {
parseAbiItem(`function ${signature} external`);
return true;
} catch (error) {
return false;
}
};

export const toDecimals = (amount: number | string, decimals = 18): bigint => {
const [integer, decimal] = String(amount).split(".");
Expand Down

0 comments on commit d7fc41e

Please sign in to comment.