Skip to content

Commit 7825bc7

Browse files
committed
Add code to check types
1 parent b43621b commit 7825bc7

File tree

2 files changed

+40
-45
lines changed

2 files changed

+40
-45
lines changed

packages/module/src/method/MethodParameterEncoder.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function getAllPropertyNamesOfPrototypeChain(type: unknown): string[] {
6262
);
6363
}
6464

65-
function isFlexibleProvablePure(
65+
export function isFlexibleProvablePure(
6666
type: unknown
6767
): type is FlexibleProvablePure<unknown> {
6868
// The required properties are defined on the prototype for Structs and CircuitValues
@@ -73,35 +73,43 @@ function isFlexibleProvablePure(
7373
return mandatory.every((prop) => props.includes(prop));
7474
}
7575

76-
export class MethodParameterEncoder {
77-
public static fromMethod(target: RuntimeModule<unknown>, methodName: string) {
78-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
79-
const paramtypes: ArgTypeArray = Reflect.getMetadata(
80-
"design:paramtypes",
81-
target,
82-
methodName
76+
export function checkArgsProvable(
77+
target: RuntimeModule<unknown>,
78+
methodName: string
79+
) {
80+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
81+
const paramtypes: ArgTypeArray = Reflect.getMetadata(
82+
"design:paramtypes",
83+
target,
84+
methodName
85+
);
86+
87+
if (paramtypes === undefined) {
88+
throw new Error(
89+
`Method with name ${methodName} doesn't exist on this module`
8390
);
91+
}
8492

85-
if (paramtypes === undefined) {
86-
throw new Error(
87-
`Method with name ${methodName} doesn't exist on this module`
88-
);
89-
}
93+
const indizes = paramtypes
94+
.map((type, index) => {
95+
if (isFlexibleProvablePure(type)) {
96+
return undefined;
97+
}
98+
return `${index}`;
99+
})
100+
.filter(filterNonUndefined);
101+
if (indizes.length > 0) {
102+
const indexString = indizes.reduce((a, b) => `${a}, ${b}`);
103+
throw new Error(
104+
`Not all arguments of method '${target.name}.${methodName}' are provable types or proofs (indizes: [${indexString}])`
105+
);
106+
}
107+
return paramtypes;
108+
}
90109

91-
const indizes = paramtypes
92-
.map((type, index) => {
93-
if (isProofBaseType(type) || isFlexibleProvablePure(type)) {
94-
return undefined;
95-
}
96-
return `${index}`;
97-
})
98-
.filter(filterNonUndefined);
99-
if (indizes.length > 0) {
100-
const indexString = indizes.reduce((a, b) => `${a}, ${b}`);
101-
throw new Error(
102-
`Not all arguments of method '${target.name}.${methodName}' are provable types or proofs (indizes: [${indexString}])`
103-
);
104-
}
110+
export class MethodParameterEncoder {
111+
public static fromMethod(target: RuntimeModule<unknown>, methodName: string) {
112+
const paramtypes = checkArgsProvable(target, methodName);
105113

106114
return new MethodParameterEncoder(paramtypes);
107115
}

packages/module/src/method/runtimeMethod.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import {
1313
toProver,
1414
ZkProgrammable,
1515
ArgumentTypes,
16-
TypedClass,
1716
} from "@proto-kit/common";
1817

1918
import type { RuntimeModule } from "../runtime/RuntimeModule.js";
2019

21-
import { MethodParameterEncoder } from "./MethodParameterEncoder";
20+
import {
21+
MethodParameterEncoder,
22+
checkArgsProvable,
23+
} from "./MethodParameterEncoder";
2224

2325
const errors = {
2426
runtimeNotProvided: (name: string) =>
@@ -191,13 +193,6 @@ export function isRuntimeMethod(
191193

192194
export type RuntimeMethodInvocationType = "SIGNATURE" | "INCOMING_MESSAGE";
193195

194-
function isSubtypeOfName(clas: TypedClass<unknown>, name: string): boolean {
195-
if (clas.name === name) {
196-
return true;
197-
}
198-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
199-
return isSubtypeOfName(Object.getPrototypeOf(clas), name);
200-
}
201196
function runtimeMethodInternal(options: {
202197
invocationType: RuntimeMethodInvocationType;
203198
}) {
@@ -206,6 +201,7 @@ function runtimeMethodInternal(options: {
206201
methodName: string,
207202
descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>
208203
) => {
204+
checkArgsProvable(target, methodName);
209205
const executionContext = container.resolve<RuntimeMethodExecutionContext>(
210206
RuntimeMethodExecutionContext
211207
);
@@ -238,15 +234,6 @@ function runtimeMethodInternal(options: {
238234
this: RuntimeModule<unknown>,
239235
...args: ArgumentTypes
240236
) {
241-
args.forEach((arg) => {
242-
const argData: any | undefined = Reflect.getMetadata(
243-
runtimeMethodNamesMetadataKey,
244-
arg
245-
);
246-
if (isSubtypeOfName(argData, "FlexibleProvablePure")) {
247-
throw Error("Argument to method not of type FlexibleProvablePure.");
248-
}
249-
});
250237
const constructorName = this.name!;
251238

252239
/**

0 commit comments

Comments
 (0)