Skip to content

Commit d7fc41e

Browse files
committed
Use viem function to check function signatures
1 parent 55bf1cf commit d7fc41e

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

packages/evmcrispr/src/modules/aragonos/commands/act.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import { batchForwarderActions } from "../utils/forwarders";
77
import { ErrorException } from "../../../errors";
88
import {
99
ComparisonType,
10-
SIGNATURE_REGEX,
1110
addressesEqual,
1211
beforeOrEqualNode,
1312
checkArgsLength,
1413
encodeAction,
1514
fetchAbi,
1615
insideNodeLine,
1716
interpretNodeSync,
17+
isFunctionSignature,
1818
tryAndCacheNotFound,
1919
} from "../../../utils";
2020
import type { AragonOS } from "../AragonOS";
@@ -50,7 +50,7 @@ export const act: ICommand<AragonOS> = {
5050
);
5151
}
5252

53-
if (!SIGNATURE_REGEX.test(signature)) {
53+
if (!isFunctionSignature(signature)) {
5454
throw new ErrorException(
5555
`expected a valid signature, but got ${signature}`,
5656
);

packages/evmcrispr/src/modules/std/commands/exec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { Abi, AbiBinding, Address, ICommand } from "../../../types";
66

77
import {
88
ComparisonType,
9-
SIGNATURE_REGEX,
109
addressesEqual,
1110
beforeOrEqualNode,
1211
checkArgsLength,
@@ -16,6 +15,7 @@ import {
1615
getOptValue,
1716
insideNodeLine,
1817
interpretNodeSync,
18+
isFunctionSignature,
1919
isNumberish,
2020
tryAndCacheNotFound,
2121
} from "../../../utils";
@@ -58,7 +58,7 @@ export const exec: ICommand<Std> = {
5858
);
5959
}
6060

61-
if (!SIGNATURE_REGEX.test(signature)) {
61+
if (!isFunctionSignature(signature)) {
6262
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(signature)) {
6363
throw new ErrorException(`invalid signature "${signature}"`);
6464
}

packages/evmcrispr/src/modules/std/helpers/get.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { ErrorException } from "../../../errors";
44
import type { HelperFunction } from "../../../types";
55
import {
66
ComparisonType,
7-
SIGNATURE_REGEX,
87
checkArgsLength,
8+
isFunctionSignature,
99
} from "../../../utils";
1010
import type { Std } from "../Std";
1111

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

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

34-
if (!SIGNATURE_REGEX.test(body)) {
34+
if (!isFunctionSignature(body)) {
3535
throw new ErrorException(
3636
`expected a valid function signature, but got "${abi}"`,
3737
);

packages/evmcrispr/src/utils/web3.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import type { AbiItem } from "viem";
2-
import { isAddressEqual } from "viem";
2+
import { isAddressEqual, parseAbiItem } from "viem";
33

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

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

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

0 commit comments

Comments
 (0)