Skip to content

Commit

Permalink
Add code to check types
Browse files Browse the repository at this point in the history
  • Loading branch information
ejMina226 committed Dec 16, 2024
1 parent b43621b commit 7825bc7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 45 deletions.
62 changes: 35 additions & 27 deletions packages/module/src/method/MethodParameterEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function getAllPropertyNamesOfPrototypeChain(type: unknown): string[] {
);
}

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

export class MethodParameterEncoder {
public static fromMethod(target: RuntimeModule<unknown>, methodName: string) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const paramtypes: ArgTypeArray = Reflect.getMetadata(
"design:paramtypes",
target,
methodName
export function checkArgsProvable(
target: RuntimeModule<unknown>,
methodName: string
) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const paramtypes: ArgTypeArray = Reflect.getMetadata(
"design:paramtypes",
target,
methodName
);

if (paramtypes === undefined) {
throw new Error(
`Method with name ${methodName} doesn't exist on this module`
);
}

if (paramtypes === undefined) {
throw new Error(
`Method with name ${methodName} doesn't exist on this module`
);
}
const indizes = paramtypes
.map((type, index) => {
if (isFlexibleProvablePure(type)) {
return undefined;
}
return `${index}`;
})
.filter(filterNonUndefined);
if (indizes.length > 0) {
const indexString = indizes.reduce((a, b) => `${a}, ${b}`);
throw new Error(
`Not all arguments of method '${target.name}.${methodName}' are provable types or proofs (indizes: [${indexString}])`
);
}
return paramtypes;
}

const indizes = paramtypes
.map((type, index) => {
if (isProofBaseType(type) || isFlexibleProvablePure(type)) {
return undefined;
}
return `${index}`;
})
.filter(filterNonUndefined);
if (indizes.length > 0) {
const indexString = indizes.reduce((a, b) => `${a}, ${b}`);
throw new Error(
`Not all arguments of method '${target.name}.${methodName}' are provable types or proofs (indizes: [${indexString}])`
);
}
export class MethodParameterEncoder {
public static fromMethod(target: RuntimeModule<unknown>, methodName: string) {
const paramtypes = checkArgsProvable(target, methodName);

return new MethodParameterEncoder(paramtypes);
}
Expand Down
23 changes: 5 additions & 18 deletions packages/module/src/method/runtimeMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import {
toProver,
ZkProgrammable,
ArgumentTypes,
TypedClass,
} from "@proto-kit/common";

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

import { MethodParameterEncoder } from "./MethodParameterEncoder";
import {
MethodParameterEncoder,
checkArgsProvable,
} from "./MethodParameterEncoder";

const errors = {
runtimeNotProvided: (name: string) =>
Expand Down Expand Up @@ -191,13 +193,6 @@ export function isRuntimeMethod(

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

function isSubtypeOfName(clas: TypedClass<unknown>, name: string): boolean {
if (clas.name === name) {
return true;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return isSubtypeOfName(Object.getPrototypeOf(clas), name);
}
function runtimeMethodInternal(options: {
invocationType: RuntimeMethodInvocationType;
}) {
Expand All @@ -206,6 +201,7 @@ function runtimeMethodInternal(options: {
methodName: string,
descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>
) => {
checkArgsProvable(target, methodName);
const executionContext = container.resolve<RuntimeMethodExecutionContext>(
RuntimeMethodExecutionContext
);
Expand Down Expand Up @@ -238,15 +234,6 @@ function runtimeMethodInternal(options: {
this: RuntimeModule<unknown>,
...args: ArgumentTypes
) {
args.forEach((arg) => {
const argData: any | undefined = Reflect.getMetadata(
runtimeMethodNamesMetadataKey,
arg
);
if (isSubtypeOfName(argData, "FlexibleProvablePure")) {
throw Error("Argument to method not of type FlexibleProvablePure.");
}
});
const constructorName = this.name!;

/**
Expand Down

0 comments on commit 7825bc7

Please sign in to comment.