@seljs/checker 1.3.0-beta.19 → 1.3.0-beta.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,12 +25,7 @@ var CelCodecRegistry = class {
25
25
  this.codecs.set("bool", zod.z.boolean());
26
26
  this.codecs.set("string", zod.z.string());
27
27
  this.codecs.set("int", zod.z.bigint());
28
- this.codecs.set("bytes", zod.z.union([zod.z.instanceof(Uint8Array), zod.z.string().transform((s) => {
29
- const hex = s.startsWith("0x") ? s.slice(2) : s;
30
- const bytes = new Uint8Array(hex.length / 2);
31
- for (let i = 0; i < bytes.length; i++) bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
32
- return bytes;
33
- })]));
28
+ this.codecs.set("bytes", zod.z.union([zod.z.instanceof(Uint8Array), zod.z.string().transform((s) => (0, _seljs_types.hexToBytes)(s))]));
34
29
  this.codecs.set("dyn", zod.z.unknown().transform((v) => typeof v === "bigint" ? new _seljs_types.SolidityIntTypeWrapper(v) : v));
35
30
  for (const desc of options?.structs ?? []) {
36
31
  this.codecs.set(desc.name, this.buildStructCodec(desc));
@@ -1,4 +1,4 @@
1
- import { SolidityAddressTypeWrapper, SolidityIntTypeWrapper } from "@seljs/types";
1
+ import { SolidityAddressTypeWrapper, SolidityIntTypeWrapper, hexToBytes } from "@seljs/types";
2
2
  import { z } from "zod";
3
3
  //#region src/environment/codec-registry.ts
4
4
  const solidityAddressCodec = z.codec(z.string(), z.instanceof(SolidityAddressTypeWrapper), {
@@ -24,12 +24,7 @@ var CelCodecRegistry = class {
24
24
  this.codecs.set("bool", z.boolean());
25
25
  this.codecs.set("string", z.string());
26
26
  this.codecs.set("int", z.bigint());
27
- this.codecs.set("bytes", z.union([z.instanceof(Uint8Array), z.string().transform((s) => {
28
- const hex = s.startsWith("0x") ? s.slice(2) : s;
29
- const bytes = new Uint8Array(hex.length / 2);
30
- for (let i = 0; i < bytes.length; i++) bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
31
- return bytes;
32
- })]));
27
+ this.codecs.set("bytes", z.union([z.instanceof(Uint8Array), z.string().transform((s) => hexToBytes(s))]));
33
28
  this.codecs.set("dyn", z.unknown().transform((v) => typeof v === "bigint" ? new SolidityIntTypeWrapper(v) : v));
34
29
  for (const desc of options?.structs ?? []) {
35
30
  this.codecs.set(desc.name, this.buildStructCodec(desc));
@@ -1,5 +1,6 @@
1
1
  require("../_virtual/_rolldown/runtime.cjs");
2
2
  let _seljs_types = require("@seljs/types");
3
+ let viem = require("viem");
3
4
  //#region src/environment/register-types.ts
4
5
  /**
5
6
  * Try to register an operator, silently ignoring "already registered" errors.
@@ -54,6 +55,19 @@ const registerIntegerOperators = (env) => {
54
55
  if (divisor === 0n) throw new Error("modulo by zero");
55
56
  return toSolInt((0, _seljs_types.toBigInt)(a) % divisor);
56
57
  });
58
+ tryRegisterOperator(env, "int + sol_int: sol_int", (a, b) => toSolInt((0, _seljs_types.toBigInt)(a) + (0, _seljs_types.toBigInt)(b)));
59
+ tryRegisterOperator(env, "int - sol_int: sol_int", (a, b) => toSolInt((0, _seljs_types.toBigInt)(a) - (0, _seljs_types.toBigInt)(b)));
60
+ tryRegisterOperator(env, "int * sol_int: sol_int", (a, b) => toSolInt((0, _seljs_types.toBigInt)(a) * (0, _seljs_types.toBigInt)(b)));
61
+ tryRegisterOperator(env, "int / sol_int: sol_int", (a, b) => {
62
+ const divisor = (0, _seljs_types.toBigInt)(b);
63
+ if (divisor === 0n) throw new Error("division by zero");
64
+ return toSolInt((0, _seljs_types.toBigInt)(a) / divisor);
65
+ });
66
+ tryRegisterOperator(env, "int % sol_int: sol_int", (a, b) => {
67
+ const divisor = (0, _seljs_types.toBigInt)(b);
68
+ if (divisor === 0n) throw new Error("modulo by zero");
69
+ return toSolInt((0, _seljs_types.toBigInt)(a) % divisor);
70
+ });
57
71
  tryRegisterOperator(env, "sol_int == int", (a, b) => (0, _seljs_types.toBigInt)(a) === (0, _seljs_types.toBigInt)(b));
58
72
  tryRegisterOperator(env, "int == sol_int", (a, b) => (0, _seljs_types.toBigInt)(a) === (0, _seljs_types.toBigInt)(b));
59
73
  tryRegisterOperator(env, "sol_int < int", (a, b) => (0, _seljs_types.toBigInt)(a) < (0, _seljs_types.toBigInt)(b));
@@ -78,11 +92,17 @@ const registerSolidityTypes = (env) => {
78
92
  for (const sig of entry.castSignatures) env.registerFunction(sig, (value) => new Ctor(value));
79
93
  }
80
94
  env.registerFunction("parseUnits(string, int): sol_int", (value, decimals) => (0, _seljs_types.parseUnitsValue)(value, Number((0, _seljs_types.toBigInt)(decimals))));
95
+ env.registerFunction("parseUnits(string, sol_int): sol_int", (value, decimals) => (0, _seljs_types.parseUnitsValue)(value, Number((0, _seljs_types.toBigInt)(decimals))));
81
96
  env.registerFunction("parseUnits(int, int): sol_int", (value, decimals) => (0, _seljs_types.parseUnitsValue)(value, Number((0, _seljs_types.toBigInt)(decimals))));
97
+ env.registerFunction("parseUnits(int, sol_int): sol_int", (value, decimals) => (0, _seljs_types.parseUnitsValue)(value, Number((0, _seljs_types.toBigInt)(decimals))));
82
98
  env.registerFunction("parseUnits(double, int): sol_int", (value, decimals) => (0, _seljs_types.parseUnitsValue)(value, Number((0, _seljs_types.toBigInt)(decimals))));
99
+ env.registerFunction("parseUnits(double, sol_int): sol_int", (value, decimals) => (0, _seljs_types.parseUnitsValue)(value, Number((0, _seljs_types.toBigInt)(decimals))));
83
100
  env.registerFunction("parseUnits(sol_int, int): sol_int", (value, decimals) => (0, _seljs_types.parseUnitsValue)(value, Number((0, _seljs_types.toBigInt)(decimals))));
101
+ env.registerFunction("parseUnits(sol_int, sol_int): sol_int", (value, decimals) => (0, _seljs_types.parseUnitsValue)(value, Number((0, _seljs_types.toBigInt)(decimals))));
84
102
  env.registerFunction("formatUnits(sol_int, int): double", (value, decimals) => (0, _seljs_types.formatUnitsValue)(value, Number((0, _seljs_types.toBigInt)(decimals))));
103
+ env.registerFunction("formatUnits(sol_int, sol_int): double", (value, decimals) => (0, _seljs_types.formatUnitsValue)(value, Number((0, _seljs_types.toBigInt)(decimals))));
85
104
  env.registerFunction("formatUnits(int, int): double", (value, decimals) => (0, _seljs_types.formatUnitsValue)(value, Number((0, _seljs_types.toBigInt)(decimals))));
105
+ env.registerFunction("formatUnits(int, sol_int): double", (value, decimals) => (0, _seljs_types.formatUnitsValue)(value, Number((0, _seljs_types.toBigInt)(decimals))));
86
106
  env.registerFunction("min(sol_int, sol_int): sol_int", (a, b) => (0, _seljs_types.toBigInt)(a) <= (0, _seljs_types.toBigInt)(b) ? toSolInt(a) : toSolInt(b));
87
107
  env.registerFunction("min(sol_int, int): sol_int", (a, b) => (0, _seljs_types.toBigInt)(a) <= (0, _seljs_types.toBigInt)(b) ? toSolInt(a) : toSolInt(b));
88
108
  env.registerFunction("min(int, sol_int): sol_int", (a, b) => (0, _seljs_types.toBigInt)(a) <= (0, _seljs_types.toBigInt)(b) ? toSolInt(a) : toSolInt(b));
@@ -102,6 +122,29 @@ const registerSolidityTypes = (env) => {
102
122
  const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
103
123
  env.registerFunction("isZeroAddress(sol_address): bool", (addr) => (0, _seljs_types.toAddress)(addr) === ZERO_ADDRESS);
104
124
  env.registerFunction("isZeroAddress(string): bool", (addr) => (0, _seljs_types.toAddress)(addr) === ZERO_ADDRESS);
125
+ env.registerFunction("hexBytes(string): bytes", (hex) => (0, _seljs_types.hexToBytes)(hex));
126
+ env.registerFunction("hexBytes(string, int): bytes", (hex, length) => {
127
+ const out = (0, _seljs_types.hexToBytes)(hex);
128
+ const expected = Number((0, _seljs_types.toBigInt)(length));
129
+ if (out.length !== expected) throw new Error(`hexBytes: expected ${String(expected)} bytes, got ${String(out.length)}`);
130
+ return out;
131
+ });
132
+ env.registerFunction("keccak256(string): bytes", (value) => (0, _seljs_types.hexToBytes)((0, viem.keccak256)((0, viem.toBytes)(value))));
133
+ env.registerFunction("keccak256(bytes): bytes", (value) => (0, _seljs_types.hexToBytes)((0, viem.keccak256)(value)));
134
+ class SelNamespace {}
135
+ const selFields = {};
136
+ const selValues = {};
137
+ for (const constant of _seljs_types.EVM_CONSTANTS) {
138
+ selFields[constant.name] = constant.type;
139
+ if (constant.type === "sol_int") selValues[constant.name] = new _seljs_types.SolidityIntTypeWrapper(constant.value);
140
+ else if (constant.type === "sol_address") selValues[constant.name] = new _seljs_types.SolidityAddressTypeWrapper(constant.value);
141
+ else selValues[constant.name] = constant.value;
142
+ }
143
+ env.registerType("SelNamespace", {
144
+ ctor: SelNamespace,
145
+ fields: selFields
146
+ });
147
+ env.registerConstant("sel", "SelNamespace", Object.assign(new SelNamespace(), selValues));
105
148
  };
106
149
  //#endregion
107
150
  exports.registerSolidityTypes = registerSolidityTypes;
@@ -6,7 +6,7 @@ import { Environment } from "@marcbachmann/cel-js";
6
6
  * Mirrors the parameter signatures from Environment but drops the return type
7
7
  * to void — registerSolidityTypes never chains the return value.
8
8
  */
9
- type SolidityTypeHost = { [K in "registerType" | "registerOperator" | "registerFunction"]: (...args: Parameters<Environment[K]>) => void };
9
+ type SolidityTypeHost = { [K in "registerType" | "registerOperator" | "registerFunction" | "registerConstant"]: (...args: Parameters<Environment[K]>) => void };
10
10
  /**
11
11
  * Register all Solidity primitive types on a CEL Environment.
12
12
  */
@@ -6,7 +6,7 @@ import { Environment } from "@marcbachmann/cel-js";
6
6
  * Mirrors the parameter signatures from Environment but drops the return type
7
7
  * to void — registerSolidityTypes never chains the return value.
8
8
  */
9
- type SolidityTypeHost = { [K in "registerType" | "registerOperator" | "registerFunction"]: (...args: Parameters<Environment[K]>) => void };
9
+ type SolidityTypeHost = { [K in "registerType" | "registerOperator" | "registerFunction" | "registerConstant"]: (...args: Parameters<Environment[K]>) => void };
10
10
  /**
11
11
  * Register all Solidity primitive types on a CEL Environment.
12
12
  */
@@ -1,4 +1,5 @@
1
- import { SOLIDITY_TYPES, SolidityIntTypeWrapper, formatUnitsValue, parseUnitsValue, toAddress, toBigInt } from "@seljs/types";
1
+ import { EVM_CONSTANTS, SOLIDITY_TYPES, SolidityAddressTypeWrapper, SolidityIntTypeWrapper, formatUnitsValue, hexToBytes, parseUnitsValue, toAddress, toBigInt } from "@seljs/types";
2
+ import { keccak256, toBytes } from "viem";
2
3
  //#region src/environment/register-types.ts
3
4
  /**
4
5
  * Try to register an operator, silently ignoring "already registered" errors.
@@ -53,6 +54,19 @@ const registerIntegerOperators = (env) => {
53
54
  if (divisor === 0n) throw new Error("modulo by zero");
54
55
  return toSolInt(toBigInt(a) % divisor);
55
56
  });
57
+ tryRegisterOperator(env, "int + sol_int: sol_int", (a, b) => toSolInt(toBigInt(a) + toBigInt(b)));
58
+ tryRegisterOperator(env, "int - sol_int: sol_int", (a, b) => toSolInt(toBigInt(a) - toBigInt(b)));
59
+ tryRegisterOperator(env, "int * sol_int: sol_int", (a, b) => toSolInt(toBigInt(a) * toBigInt(b)));
60
+ tryRegisterOperator(env, "int / sol_int: sol_int", (a, b) => {
61
+ const divisor = toBigInt(b);
62
+ if (divisor === 0n) throw new Error("division by zero");
63
+ return toSolInt(toBigInt(a) / divisor);
64
+ });
65
+ tryRegisterOperator(env, "int % sol_int: sol_int", (a, b) => {
66
+ const divisor = toBigInt(b);
67
+ if (divisor === 0n) throw new Error("modulo by zero");
68
+ return toSolInt(toBigInt(a) % divisor);
69
+ });
56
70
  tryRegisterOperator(env, "sol_int == int", (a, b) => toBigInt(a) === toBigInt(b));
57
71
  tryRegisterOperator(env, "int == sol_int", (a, b) => toBigInt(a) === toBigInt(b));
58
72
  tryRegisterOperator(env, "sol_int < int", (a, b) => toBigInt(a) < toBigInt(b));
@@ -77,11 +91,17 @@ const registerSolidityTypes = (env) => {
77
91
  for (const sig of entry.castSignatures) env.registerFunction(sig, (value) => new Ctor(value));
78
92
  }
79
93
  env.registerFunction("parseUnits(string, int): sol_int", (value, decimals) => parseUnitsValue(value, Number(toBigInt(decimals))));
94
+ env.registerFunction("parseUnits(string, sol_int): sol_int", (value, decimals) => parseUnitsValue(value, Number(toBigInt(decimals))));
80
95
  env.registerFunction("parseUnits(int, int): sol_int", (value, decimals) => parseUnitsValue(value, Number(toBigInt(decimals))));
96
+ env.registerFunction("parseUnits(int, sol_int): sol_int", (value, decimals) => parseUnitsValue(value, Number(toBigInt(decimals))));
81
97
  env.registerFunction("parseUnits(double, int): sol_int", (value, decimals) => parseUnitsValue(value, Number(toBigInt(decimals))));
98
+ env.registerFunction("parseUnits(double, sol_int): sol_int", (value, decimals) => parseUnitsValue(value, Number(toBigInt(decimals))));
82
99
  env.registerFunction("parseUnits(sol_int, int): sol_int", (value, decimals) => parseUnitsValue(value, Number(toBigInt(decimals))));
100
+ env.registerFunction("parseUnits(sol_int, sol_int): sol_int", (value, decimals) => parseUnitsValue(value, Number(toBigInt(decimals))));
83
101
  env.registerFunction("formatUnits(sol_int, int): double", (value, decimals) => formatUnitsValue(value, Number(toBigInt(decimals))));
102
+ env.registerFunction("formatUnits(sol_int, sol_int): double", (value, decimals) => formatUnitsValue(value, Number(toBigInt(decimals))));
84
103
  env.registerFunction("formatUnits(int, int): double", (value, decimals) => formatUnitsValue(value, Number(toBigInt(decimals))));
104
+ env.registerFunction("formatUnits(int, sol_int): double", (value, decimals) => formatUnitsValue(value, Number(toBigInt(decimals))));
85
105
  env.registerFunction("min(sol_int, sol_int): sol_int", (a, b) => toBigInt(a) <= toBigInt(b) ? toSolInt(a) : toSolInt(b));
86
106
  env.registerFunction("min(sol_int, int): sol_int", (a, b) => toBigInt(a) <= toBigInt(b) ? toSolInt(a) : toSolInt(b));
87
107
  env.registerFunction("min(int, sol_int): sol_int", (a, b) => toBigInt(a) <= toBigInt(b) ? toSolInt(a) : toSolInt(b));
@@ -101,6 +121,29 @@ const registerSolidityTypes = (env) => {
101
121
  const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
102
122
  env.registerFunction("isZeroAddress(sol_address): bool", (addr) => toAddress(addr) === ZERO_ADDRESS);
103
123
  env.registerFunction("isZeroAddress(string): bool", (addr) => toAddress(addr) === ZERO_ADDRESS);
124
+ env.registerFunction("hexBytes(string): bytes", (hex) => hexToBytes(hex));
125
+ env.registerFunction("hexBytes(string, int): bytes", (hex, length) => {
126
+ const out = hexToBytes(hex);
127
+ const expected = Number(toBigInt(length));
128
+ if (out.length !== expected) throw new Error(`hexBytes: expected ${String(expected)} bytes, got ${String(out.length)}`);
129
+ return out;
130
+ });
131
+ env.registerFunction("keccak256(string): bytes", (value) => hexToBytes(keccak256(toBytes(value))));
132
+ env.registerFunction("keccak256(bytes): bytes", (value) => hexToBytes(keccak256(value)));
133
+ class SelNamespace {}
134
+ const selFields = {};
135
+ const selValues = {};
136
+ for (const constant of EVM_CONSTANTS) {
137
+ selFields[constant.name] = constant.type;
138
+ if (constant.type === "sol_int") selValues[constant.name] = new SolidityIntTypeWrapper(constant.value);
139
+ else if (constant.type === "sol_address") selValues[constant.name] = new SolidityAddressTypeWrapper(constant.value);
140
+ else selValues[constant.name] = constant.value;
141
+ }
142
+ env.registerType("SelNamespace", {
143
+ ctor: SelNamespace,
144
+ fields: selFields
145
+ });
146
+ env.registerConstant("sel", "SelNamespace", Object.assign(new SelNamespace(), selValues));
104
147
  };
105
148
  //#endregion
106
149
  export { registerSolidityTypes };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seljs/checker",
3
- "version": "1.3.0-beta.19",
3
+ "version": "1.3.0-beta.21",
4
4
  "repository": {
5
5
  "url": "https://github.com/abinnovision/seljs"
6
6
  },
@@ -50,10 +50,11 @@
50
50
  },
51
51
  "dependencies": {
52
52
  "@marcbachmann/cel-js": "^7.5.3",
53
- "@seljs/common": "1.3.0-beta.19",
53
+ "@seljs/common": "1.3.0-beta.21",
54
54
  "@seljs/schema": "1.2.0",
55
- "@seljs/types": "1.3.0-beta.19",
56
- "debug": "^4.4.3"
55
+ "@seljs/types": "1.3.0-beta.21",
56
+ "debug": "^4.4.3",
57
+ "viem": "^2.47.6"
57
58
  },
58
59
  "devDependencies": {
59
60
  "@abinnovision/eslint-config-base": "^3.2.2",