@proto-kit/module 0.1.1-develop.165 → 0.1.1-develop.1665
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.
- package/dist/factories/MethodIdFactory.d.ts +9 -0
- package/dist/factories/MethodIdFactory.d.ts.map +1 -0
- package/dist/factories/MethodIdFactory.js +12 -0
- package/dist/factories/MethodIdFactory.js.map +1 -0
- package/dist/index.d.ts +5 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -7
- package/dist/index.js.map +1 -0
- package/dist/messages/OutgoingMessages.d.ts +215 -0
- package/dist/messages/OutgoingMessages.d.ts.map +1 -0
- package/dist/messages/OutgoingMessages.js +66 -0
- package/dist/messages/OutgoingMessages.js.map +1 -0
- package/dist/method/MethodParameterEncoder.d.ts +26 -0
- package/dist/method/MethodParameterEncoder.d.ts.map +1 -0
- package/dist/method/MethodParameterEncoder.js +169 -0
- package/dist/method/MethodParameterEncoder.js.map +1 -0
- package/dist/method/runtimeMethod.d.ts +22 -5
- package/dist/method/runtimeMethod.d.ts.map +1 -1
- package/dist/method/runtimeMethod.js +79 -24
- package/dist/method/runtimeMethod.js.map +1 -0
- package/dist/module/decorator.js +1 -0
- package/dist/module/decorator.js.map +1 -0
- package/dist/runtime/MethodIdResolver.d.ts +20 -0
- package/dist/runtime/MethodIdResolver.d.ts.map +1 -0
- package/dist/runtime/MethodIdResolver.js +91 -0
- package/dist/runtime/MethodIdResolver.js.map +1 -0
- package/dist/runtime/Runtime.d.ts +22 -28
- package/dist/runtime/Runtime.d.ts.map +1 -1
- package/dist/runtime/Runtime.js +116 -70
- package/dist/runtime/Runtime.js.map +1 -0
- package/dist/runtime/RuntimeEnvironment.d.ts +10 -0
- package/dist/runtime/RuntimeEnvironment.d.ts.map +1 -0
- package/dist/runtime/RuntimeEnvironment.js +2 -0
- package/dist/runtime/RuntimeEnvironment.js.map +1 -0
- package/dist/runtime/RuntimeModule.d.ts +23 -14
- package/dist/runtime/RuntimeModule.d.ts.map +1 -1
- package/dist/runtime/RuntimeModule.js +47 -10
- package/dist/runtime/RuntimeModule.js.map +1 -0
- package/dist/state/InMemoryStateService.d.ts +11 -10
- package/dist/state/InMemoryStateService.d.ts.map +1 -1
- package/dist/state/InMemoryStateService.js +11 -8
- package/dist/state/InMemoryStateService.js.map +1 -0
- package/jest.config.cjs +12 -1
- package/package.json +10 -11
- package/src/factories/MethodIdFactory.ts +15 -0
- package/src/index.ts +5 -7
- package/src/messages/OutgoingMessages.ts +122 -0
- package/src/method/MethodParameterEncoder.ts +260 -0
- package/src/method/runtimeMethod.ts +146 -31
- package/src/runtime/MethodIdResolver.ts +108 -0
- package/src/runtime/Runtime.ts +191 -112
- package/src/runtime/RuntimeEnvironment.ts +16 -0
- package/src/runtime/RuntimeModule.ts +77 -27
- package/src/state/InMemoryStateService.ts +14 -18
- package/test/Runtime.test.ts +69 -36
- package/test/TestingRuntime.ts +43 -0
- package/test/messages/message.test.ts +42 -0
- package/test/method/MethodParameterEncoder.test.ts +121 -0
- package/test/method/runtimeMethod-fail.test.ts +50 -0
- package/{src/method/decorator.test.ts → test/method/runtimeMethod.test.ts} +3 -3
- package/test/modules/Admin.ts +4 -4
- package/test/modules/Balances.test.ts +92 -78
- package/test/modules/Balances.ts +19 -16
- package/test/modules/MethodIdResolver.test.ts +73 -0
- package/test/modules/State.test.ts +81 -0
- package/test/runtimeMethod.test.ts +192 -20
- package/test/tsconfig.json +7 -0
- package/tsconfig.json +2 -2
- package/dist/chain/Chain.d.ts +0 -109
- package/dist/chain/Chain.d.ts.map +0 -1
- package/dist/chain/Chain.js +0 -229
- package/dist/method/MethodExecutionContext.d.ts +0 -73
- package/dist/method/MethodExecutionContext.d.ts.map +0 -1
- package/dist/method/MethodExecutionContext.js +0 -112
- package/dist/method/MethodParameterDecoder.d.ts +0 -22
- package/dist/method/MethodParameterDecoder.d.ts.map +0 -1
- package/dist/method/MethodParameterDecoder.js +0 -33
- package/dist/method/RuntimeMethodExecutionContext.d.ts +0 -57
- package/dist/method/RuntimeMethodExecutionContext.d.ts.map +0 -1
- package/dist/method/RuntimeMethodExecutionContext.js +0 -92
- package/dist/method/assert.d.ts +0 -12
- package/dist/method/assert.d.ts.map +0 -1
- package/dist/method/assert.js +0 -23
- package/dist/method/decorator.d.ts +0 -45
- package/dist/method/decorator.d.ts.map +0 -1
- package/dist/method/decorator.js +0 -140
- package/dist/state/State.d.ts +0 -65
- package/dist/state/State.d.ts.map +0 -1
- package/dist/state/State.js +0 -114
- package/dist/state/StateMap.d.ts +0 -37
- package/dist/state/StateMap.d.ts.map +0 -1
- package/dist/state/StateMap.js +0 -56
- package/dist/state/StateServiceProvider.d.ts +0 -10
- package/dist/state/StateServiceProvider.d.ts.map +0 -1
- package/dist/state/StateServiceProvider.js +0 -34
- package/dist/state/decorator.d.ts +0 -7
- package/dist/state/decorator.d.ts.map +0 -1
- package/dist/state/decorator.js +0 -42
- package/src/method/MethodParameterDecoder.ts +0 -68
- package/src/method/RuntimeMethodExecutionContext.ts +0 -111
- package/src/method/assert.test.ts +0 -49
- package/src/method/assert.ts +0 -27
- package/src/state/State.ts +0 -154
- package/src/state/StateMap.ts +0 -69
- package/src/state/StateServiceProvider.ts +0 -24
- package/src/state/decorator.ts +0 -65
- package/test/state/MerkleTree.test.ts +0 -95
- package/test/state/MockAsyncMerkleStore.ts +0 -28
- package/test/transaction.test.ts +0 -82
- package/tsconfig.test.json +0 -9
|
@@ -1,41 +1,75 @@
|
|
|
1
|
-
import { ConfigurableModule, Presets } from "@proto-kit/common";
|
|
1
|
+
import { ConfigurableModule, NoConfig, Presets } from "@proto-kit/common";
|
|
2
2
|
import { container, injectable } from "tsyringe";
|
|
3
|
-
import { NetworkState, RuntimeTransaction } from "@proto-kit/protocol";
|
|
4
|
-
|
|
5
3
|
import {
|
|
4
|
+
NetworkState,
|
|
5
|
+
RuntimeTransaction,
|
|
6
6
|
RuntimeMethodExecutionContext,
|
|
7
7
|
RuntimeMethodExecutionData,
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
RuntimeMethodExecutionDataStruct,
|
|
9
|
+
} from "@proto-kit/protocol";
|
|
10
|
+
import { FlexibleProvablePure, Provable, Bool } from "o1js";
|
|
11
|
+
|
|
12
|
+
import { runtimeMethodNamesMetadataKey } from "../method/runtimeMethod";
|
|
13
|
+
import {
|
|
14
|
+
OutgoingMessages,
|
|
15
|
+
OutgoingMessagesRecord,
|
|
16
|
+
} from "../messages/OutgoingMessages";
|
|
10
17
|
|
|
11
|
-
import
|
|
12
|
-
Runtime,
|
|
13
|
-
RuntimeDefinition,
|
|
14
|
-
RuntimeModulesRecord,
|
|
15
|
-
} from "./Runtime";
|
|
18
|
+
import { RuntimeEnvironment } from "./RuntimeEnvironment";
|
|
16
19
|
|
|
17
20
|
const errors = {
|
|
18
21
|
inputDataNotSet: () => new Error("Input data for runtime execution not set"),
|
|
19
22
|
};
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
type EventRecord = Record<string, FlexibleProvablePure<any>>;
|
|
25
|
+
|
|
26
|
+
type InferProvable<T extends FlexibleProvablePure<any>> =
|
|
27
|
+
T extends Provable<infer U> ? U : never;
|
|
28
|
+
|
|
29
|
+
export class RuntimeEvents<Events extends EventRecord> {
|
|
30
|
+
public constructor(private readonly events: Events) {}
|
|
31
|
+
|
|
32
|
+
public emitIf<Key extends keyof Events>(
|
|
33
|
+
condition: Bool,
|
|
34
|
+
eventName: Key,
|
|
35
|
+
event: InferProvable<Events[Key]>
|
|
36
|
+
) {
|
|
37
|
+
if (this.events === undefined) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
"'events' property not defined, make sure to define the event types on your runtimemodule"
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
const eventType: FlexibleProvablePure<any> = this.events[eventName];
|
|
43
|
+
if (typeof eventName !== "string") {
|
|
44
|
+
throw new Error("Only string");
|
|
45
|
+
}
|
|
46
|
+
return container
|
|
47
|
+
.resolve(RuntimeMethodExecutionContext)
|
|
48
|
+
.addEvent(eventType, event, eventName, condition);
|
|
49
|
+
}
|
|
28
50
|
|
|
29
|
-
|
|
51
|
+
public emit<Key extends keyof Events>(
|
|
52
|
+
eventName: Key,
|
|
53
|
+
event: InferProvable<Events[Key]>
|
|
54
|
+
) {
|
|
55
|
+
this.emitIf(Bool(true), eventName, event);
|
|
56
|
+
}
|
|
30
57
|
}
|
|
31
58
|
|
|
32
59
|
/**
|
|
33
60
|
* Base class for runtime modules providing the necessary utilities.
|
|
34
61
|
*/
|
|
35
62
|
@injectable()
|
|
36
|
-
export class RuntimeModule<
|
|
63
|
+
export class RuntimeModule<
|
|
64
|
+
Config = NoConfig,
|
|
65
|
+
> extends ConfigurableModule<Config> {
|
|
37
66
|
public static presets: Presets<unknown> = {};
|
|
38
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Holds all method names that are callable throw transactions
|
|
70
|
+
*/
|
|
71
|
+
public readonly runtimeMethodNames: string[] = [];
|
|
72
|
+
|
|
39
73
|
/**
|
|
40
74
|
* This property exists only to typecheck that the RuntimeModule
|
|
41
75
|
* was extended correctly in e.g. a decorator. We need at least
|
|
@@ -45,17 +79,33 @@ export class RuntimeModule<Config> extends ConfigurableModule<Config> {
|
|
|
45
79
|
|
|
46
80
|
public name?: string;
|
|
47
81
|
|
|
48
|
-
public
|
|
82
|
+
public parent?: RuntimeEnvironment;
|
|
49
83
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
84
|
+
public events?: RuntimeEvents<any> = undefined;
|
|
85
|
+
|
|
86
|
+
public messages?: OutgoingMessages<OutgoingMessagesRecord> = undefined;
|
|
87
|
+
|
|
88
|
+
public constructor() {
|
|
89
|
+
super();
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
91
|
+
const methodNames: string[] | undefined = Reflect.getMetadata(
|
|
92
|
+
runtimeMethodNamesMetadataKey,
|
|
93
|
+
this
|
|
53
94
|
);
|
|
95
|
+
this.runtimeMethodNames = methodNames ?? [];
|
|
96
|
+
}
|
|
54
97
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
98
|
+
public getInputs(): RuntimeMethodExecutionData {
|
|
99
|
+
return Provable.witness(RuntimeMethodExecutionDataStruct, () => {
|
|
100
|
+
const { input } = container.resolve<RuntimeMethodExecutionContext>(
|
|
101
|
+
RuntimeMethodExecutionContext
|
|
102
|
+
);
|
|
103
|
+
if (input === undefined) {
|
|
104
|
+
throw errors.inputDataNotSet();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return input;
|
|
108
|
+
});
|
|
59
109
|
}
|
|
60
110
|
|
|
61
111
|
public get transaction(): RuntimeTransaction {
|
|
@@ -1,27 +1,23 @@
|
|
|
1
|
-
import { Field } from "
|
|
2
|
-
|
|
3
|
-
export interface StateService {
|
|
4
|
-
get: (key: Field) => Field[] | undefined;
|
|
5
|
-
set: (key: Field, value: Field[] | undefined) => void;
|
|
6
|
-
}
|
|
1
|
+
import { Field } from "o1js";
|
|
2
|
+
import { SimpleAsyncStateService } from "@proto-kit/protocol";
|
|
7
3
|
|
|
8
4
|
/**
|
|
9
|
-
* Naive implementation of
|
|
5
|
+
* Naive implementation of an in-memory variant of the StateService interface
|
|
10
6
|
*/
|
|
11
|
-
export class InMemoryStateService implements
|
|
12
|
-
|
|
7
|
+
export class InMemoryStateService implements SimpleAsyncStateService {
|
|
8
|
+
/**
|
|
9
|
+
* This mapping container null values if the specific entry has been deleted.
|
|
10
|
+
* This is used by the CachedState service to keep track of deletions
|
|
11
|
+
*/
|
|
12
|
+
public values: Record<string, Field[] | null> = {};
|
|
13
13
|
|
|
14
|
-
public get(key: Field): Field[] | undefined {
|
|
15
|
-
return this.values[key.toString()];
|
|
14
|
+
public async get(key: Field): Promise<Field[] | undefined> {
|
|
15
|
+
return this.values[key.toString()] ?? undefined;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
public set(key: Field, value: Field[] | undefined) {
|
|
19
|
-
if (
|
|
20
|
-
|
|
21
|
-
Object.prototype.hasOwnProperty.call(this.values, key.toString())
|
|
22
|
-
) {
|
|
23
|
-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
24
|
-
delete this.values[key.toString()];
|
|
18
|
+
public async set(key: Field, value: Field[] | undefined) {
|
|
19
|
+
if (value === undefined) {
|
|
20
|
+
this.values[key.toString()] = null;
|
|
25
21
|
} else {
|
|
26
22
|
this.values[key.toString()] = value;
|
|
27
23
|
}
|
package/test/Runtime.test.ts
CHANGED
|
@@ -1,37 +1,70 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
describe("noop", () => {
|
|
2
|
+
it("noop", () => {
|
|
3
|
+
expect(1).toBe(1);
|
|
4
|
+
});
|
|
5
|
+
});
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
7
|
+
// TODO disabled because of the reflect-metadata error in the CI
|
|
8
|
+
// import "reflect-metadata";
|
|
9
|
+
// import { container } from "tsyringe";
|
|
10
|
+
// import {
|
|
11
|
+
// RuntimeMethodExecutionContext,
|
|
12
|
+
// RuntimeTransaction,
|
|
13
|
+
// NetworkState,
|
|
14
|
+
// } from "@proto-kit/protocol";
|
|
15
|
+
//
|
|
16
|
+
// import { MethodIdResolver } from "../src";
|
|
17
|
+
//
|
|
18
|
+
// import { Balances } from "./modules/Balances";
|
|
19
|
+
// import { createTestingRuntime } from "./TestingRuntime";
|
|
20
|
+
//
|
|
21
|
+
// describe.skip("runtime", () => {
|
|
22
|
+
// it("should encode methodnames correctly", () => {
|
|
23
|
+
// expect.assertions(2);
|
|
24
|
+
//
|
|
25
|
+
// const { runtime } = createTestingRuntime(
|
|
26
|
+
// {
|
|
27
|
+
// Balances,
|
|
28
|
+
// },
|
|
29
|
+
// {
|
|
30
|
+
// Balances: {},
|
|
31
|
+
// }
|
|
32
|
+
// );
|
|
33
|
+
//
|
|
34
|
+
// const balances = runtime.resolve("Balances");
|
|
35
|
+
//
|
|
36
|
+
// expect(balances).toBeDefined();
|
|
37
|
+
//
|
|
38
|
+
// console.log(Object.keys(balances));
|
|
39
|
+
// console.log(balances.getTotalSupply);
|
|
40
|
+
//
|
|
41
|
+
// const moduleName = "Balances";
|
|
42
|
+
// const methodName = "getTotalSupply";
|
|
43
|
+
//
|
|
44
|
+
// const methodId = runtime.dependencyContainer
|
|
45
|
+
// .resolve<MethodIdResolver>("MethodIdResolver")
|
|
46
|
+
// .getMethodId(moduleName, methodName);
|
|
47
|
+
// const method = runtime.getMethodById(methodId);
|
|
48
|
+
//
|
|
49
|
+
// expect(method).toBeDefined();
|
|
50
|
+
// });
|
|
51
|
+
//
|
|
52
|
+
// it("regression - check that analyzeMethods works on runtime", async () => {
|
|
53
|
+
// const { runtime } = createTestingRuntime(
|
|
54
|
+
// {
|
|
55
|
+
// Balances,
|
|
56
|
+
// },
|
|
57
|
+
// {
|
|
58
|
+
// Balances: {},
|
|
59
|
+
// }
|
|
60
|
+
// );
|
|
61
|
+
//
|
|
62
|
+
// const context = container.resolve(RuntimeMethodExecutionContext);
|
|
63
|
+
// context.setup({
|
|
64
|
+
// transaction: RuntimeTransaction.dummyTransaction(),
|
|
65
|
+
// networkState: NetworkState.empty(),
|
|
66
|
+
// });
|
|
67
|
+
//
|
|
68
|
+
// await runtime.zkProgrammable.zkProgram.analyzeMethods();
|
|
69
|
+
// });
|
|
70
|
+
// });
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ModulesConfig } from "@proto-kit/common";
|
|
2
|
+
import { StateServiceProvider } from "@proto-kit/protocol";
|
|
3
|
+
import { container } from "tsyringe";
|
|
4
|
+
|
|
5
|
+
import { InMemoryStateService, Runtime, RuntimeModulesRecord } from "../src";
|
|
6
|
+
|
|
7
|
+
export function createTestingRuntime<Modules extends RuntimeModulesRecord>(
|
|
8
|
+
modules: Modules,
|
|
9
|
+
config: ModulesConfig<Modules>
|
|
10
|
+
): {
|
|
11
|
+
runtime: Runtime<Modules>;
|
|
12
|
+
state: InMemoryStateService;
|
|
13
|
+
} {
|
|
14
|
+
const state = new InMemoryStateService();
|
|
15
|
+
|
|
16
|
+
const Runtimeclass = Runtime.from(modules);
|
|
17
|
+
const runtime = new Runtimeclass();
|
|
18
|
+
|
|
19
|
+
runtime.configure(config);
|
|
20
|
+
|
|
21
|
+
runtime.create(() => container.createChildContainer());
|
|
22
|
+
|
|
23
|
+
runtime.dependencyContainer.register("AreProofsEnabled", {
|
|
24
|
+
useValue: {
|
|
25
|
+
areProofsEnabled: false,
|
|
26
|
+
|
|
27
|
+
setProofsEnabled(areProofsEnabled: boolean) {
|
|
28
|
+
this.areProofsEnabled = areProofsEnabled;
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
runtime.registerValue({
|
|
33
|
+
StateServiceProvider: new StateServiceProvider(),
|
|
34
|
+
Runtime: runtime,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
runtime.stateServiceProvider.setCurrentStateService(state);
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
runtime,
|
|
41
|
+
state,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { state } from "@proto-kit/protocol";
|
|
3
|
+
import { Field, Struct, TokenId } from "o1js";
|
|
4
|
+
import { describe } from "@jest/globals";
|
|
5
|
+
|
|
6
|
+
import { RuntimeModule } from "../../src/runtime/RuntimeModule";
|
|
7
|
+
import { runtimeMethod } from "../../src/method/runtimeMethod";
|
|
8
|
+
import { OutgoingMessages } from "../../src/messages/OutgoingMessages";
|
|
9
|
+
|
|
10
|
+
class SampleWithdrawal extends Struct({
|
|
11
|
+
data: Field,
|
|
12
|
+
}) {}
|
|
13
|
+
|
|
14
|
+
export class Example extends RuntimeModule {
|
|
15
|
+
@state()
|
|
16
|
+
messages = new OutgoingMessages({
|
|
17
|
+
sample: SampleWithdrawal,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
@runtimeMethod()
|
|
21
|
+
public async foo() {
|
|
22
|
+
await this.messages.emitMessage(
|
|
23
|
+
"sample",
|
|
24
|
+
new SampleWithdrawal({
|
|
25
|
+
data: Field(1),
|
|
26
|
+
}),
|
|
27
|
+
TokenId.default
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// TODO Add test
|
|
33
|
+
|
|
34
|
+
describe("d", () => {
|
|
35
|
+
it("a", () => {
|
|
36
|
+
class A {}
|
|
37
|
+
|
|
38
|
+
class B extends A {}
|
|
39
|
+
|
|
40
|
+
expect(new B() instanceof A).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Struct,
|
|
3
|
+
Field,
|
|
4
|
+
Bool,
|
|
5
|
+
PublicKey,
|
|
6
|
+
PrivateKey,
|
|
7
|
+
ZkProgram,
|
|
8
|
+
Proof,
|
|
9
|
+
} from "o1js";
|
|
10
|
+
import { NonMethods } from "@proto-kit/common";
|
|
11
|
+
|
|
12
|
+
import { MethodParameterEncoder } from "../../src";
|
|
13
|
+
|
|
14
|
+
class TestStruct extends Struct({
|
|
15
|
+
a: Field,
|
|
16
|
+
b: Bool,
|
|
17
|
+
}) {}
|
|
18
|
+
|
|
19
|
+
const TestProgram = ZkProgram({
|
|
20
|
+
name: "TestProgram",
|
|
21
|
+
publicInput: PublicKey,
|
|
22
|
+
publicOutput: TestStruct,
|
|
23
|
+
methods: {
|
|
24
|
+
foo: {
|
|
25
|
+
privateInputs: [],
|
|
26
|
+
method: async (input: PublicKey) => {
|
|
27
|
+
return {
|
|
28
|
+
a: Field(input.x),
|
|
29
|
+
b: Bool(input.isOdd),
|
|
30
|
+
};
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
class TestProof extends ZkProgram.Proof(TestProgram) {}
|
|
36
|
+
|
|
37
|
+
describe("MethodParameterEncoder", () => {
|
|
38
|
+
it("should en/decode Structs correctly", async () => {
|
|
39
|
+
expect.assertions(7);
|
|
40
|
+
|
|
41
|
+
const encoder = new MethodParameterEncoder([TestStruct]);
|
|
42
|
+
const { fields, auxiliary } = encoder.encode([
|
|
43
|
+
{ a: Field(2), b: Bool(true) },
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
expect(auxiliary).toHaveLength(0);
|
|
47
|
+
expect(fields).toHaveLength(2);
|
|
48
|
+
expect(fields[0].toString()).toBe("2");
|
|
49
|
+
expect(fields[1].toString()).toStrictEqual(Bool(true).toField().toString());
|
|
50
|
+
|
|
51
|
+
const decoded = await encoder.decode(fields, auxiliary);
|
|
52
|
+
expect(decoded).toHaveLength(1);
|
|
53
|
+
const decoded1 = decoded[0] as unknown as NonMethods<TestStruct>;
|
|
54
|
+
expect(decoded1.a.toString()).toStrictEqual("2");
|
|
55
|
+
expect(decoded1.b.toString()).toStrictEqual("true");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should en/decode CircuitValues correctly", async () => {
|
|
59
|
+
expect.assertions(6);
|
|
60
|
+
|
|
61
|
+
const encoder = new MethodParameterEncoder([PublicKey]);
|
|
62
|
+
const pk = PrivateKey.random().toPublicKey();
|
|
63
|
+
|
|
64
|
+
const { fields, auxiliary } = encoder.encode([pk]);
|
|
65
|
+
|
|
66
|
+
expect(auxiliary).toHaveLength(0);
|
|
67
|
+
expect(fields).toHaveLength(2);
|
|
68
|
+
expect(fields.map((x) => x.toString())).toStrictEqual(
|
|
69
|
+
pk.toFields().map((x) => x.toString())
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const decoded = await encoder.decode(fields, auxiliary);
|
|
73
|
+
expect(decoded).toHaveLength(1);
|
|
74
|
+
|
|
75
|
+
const decoded1 = decoded[0] as unknown as PublicKey;
|
|
76
|
+
expect(decoded1.x.toString()).toStrictEqual(pk.x.toString());
|
|
77
|
+
expect(decoded1.isOdd.toString()).toStrictEqual(pk.isOdd.toString());
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("should en/decode Proofs correctly", async () => {
|
|
81
|
+
expect.assertions(13);
|
|
82
|
+
|
|
83
|
+
const encoder = new MethodParameterEncoder([TestProof]);
|
|
84
|
+
const input = PrivateKey.random().toPublicKey();
|
|
85
|
+
const output = { a: input.x, b: input.isOdd };
|
|
86
|
+
const dummy = await TestProof.dummy(input, output, 0);
|
|
87
|
+
|
|
88
|
+
const { fields, auxiliary } = encoder.encode([dummy]);
|
|
89
|
+
|
|
90
|
+
expect(auxiliary).toHaveLength(1);
|
|
91
|
+
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
93
|
+
const json = JSON.parse(auxiliary[0]);
|
|
94
|
+
|
|
95
|
+
expect(json).toHaveProperty("maxProofsVerified");
|
|
96
|
+
expect(json).toHaveProperty("proof");
|
|
97
|
+
expect(json.maxProofsVerified).toStrictEqual(0);
|
|
98
|
+
expect(json.proof.length).toBeGreaterThan(20);
|
|
99
|
+
|
|
100
|
+
expect(fields).toHaveLength(4);
|
|
101
|
+
expect(fields.map((x) => x.toString())).toStrictEqual(
|
|
102
|
+
[...input.toFields(), ...TestStruct.toFields(output)].map((x) =>
|
|
103
|
+
x.toString()
|
|
104
|
+
)
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
const decoded = await encoder.decode(fields, auxiliary);
|
|
108
|
+
expect(decoded).toHaveLength(1);
|
|
109
|
+
|
|
110
|
+
const decoded1 = decoded[0] as unknown as Proof<PublicKey, TestStruct>;
|
|
111
|
+
expect(decoded1.maxProofsVerified).toStrictEqual(0);
|
|
112
|
+
expect(decoded1.proof).toBeDefined();
|
|
113
|
+
expect(decoded1.publicInput.equals(input).toBoolean()).toStrictEqual(true);
|
|
114
|
+
expect(decoded1.publicOutput.a.equals(output.a).toBoolean()).toStrictEqual(
|
|
115
|
+
true
|
|
116
|
+
);
|
|
117
|
+
expect(decoded1.publicOutput.b.equals(output.b).toBoolean()).toStrictEqual(
|
|
118
|
+
true
|
|
119
|
+
);
|
|
120
|
+
}, 30000);
|
|
121
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Bool, Field, PublicKey, Struct, ZkProgram } from "o1js";
|
|
2
|
+
import { noop } from "@proto-kit/common";
|
|
3
|
+
|
|
4
|
+
import { runtimeMethod, RuntimeModule, runtimeModule } from "../../src";
|
|
5
|
+
|
|
6
|
+
class TestStruct extends Struct({
|
|
7
|
+
a: Field,
|
|
8
|
+
b: Bool,
|
|
9
|
+
}) {}
|
|
10
|
+
class TieredStruct extends TestStruct {}
|
|
11
|
+
const TestProgram = ZkProgram({
|
|
12
|
+
name: "TestProgram",
|
|
13
|
+
publicInput: PublicKey,
|
|
14
|
+
publicOutput: TestStruct,
|
|
15
|
+
methods: {
|
|
16
|
+
foo: {
|
|
17
|
+
privateInputs: [],
|
|
18
|
+
method: async (input: PublicKey) => {
|
|
19
|
+
return {
|
|
20
|
+
a: Field(input.x),
|
|
21
|
+
b: Bool(input.isOdd),
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
class TestProof extends ZkProgram.Proof(TestProgram) {}
|
|
28
|
+
|
|
29
|
+
describe("Creating module with non-provable method argument", () => {
|
|
30
|
+
it("should throw on non-provable method signature", () => {
|
|
31
|
+
expect(() => {
|
|
32
|
+
@runtimeModule()
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
34
|
+
class TestModule extends RuntimeModule {
|
|
35
|
+
@runtimeMethod()
|
|
36
|
+
public async foo(
|
|
37
|
+
a: TieredStruct,
|
|
38
|
+
b: PublicKey,
|
|
39
|
+
c: Field,
|
|
40
|
+
d: TestProof,
|
|
41
|
+
e: string
|
|
42
|
+
) {
|
|
43
|
+
noop();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}).toThrow(
|
|
47
|
+
"Not all arguments of method 'undefined.foo' are provable types or proofs (indizes: [4])"
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
2
|
|
|
3
|
-
import { Bool, Field } from "
|
|
3
|
+
import { Bool, Field } from "o1js";
|
|
4
4
|
import { Option, StateTransition } from "@proto-kit/protocol";
|
|
5
5
|
|
|
6
|
-
import { toStateTransitionsHash } from "
|
|
6
|
+
import { toStateTransitionsHash } from "../../src/method/runtimeMethod";
|
|
7
7
|
|
|
8
|
-
describe("toStateTransitionsHash", () => {
|
|
8
|
+
describe.skip("toStateTransitionsHash", () => {
|
|
9
9
|
const noneStateTransition = StateTransition.from(
|
|
10
10
|
Field(0),
|
|
11
11
|
new Option(Bool(false), Field(0), Field)
|
package/test/modules/Admin.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { PublicKey } from "
|
|
1
|
+
import { PublicKey } from "o1js";
|
|
2
|
+
import { assert } from "@proto-kit/protocol";
|
|
2
3
|
|
|
3
4
|
import { runtimeModule } from "../../src/module/decorator.js";
|
|
4
5
|
import { RuntimeModule } from "../../src/runtime/RuntimeModule.js";
|
|
5
6
|
import { runtimeMethod } from "../../src/method/runtimeMethod.js";
|
|
6
|
-
import { assert } from "../../src/method/assert.js";
|
|
7
7
|
|
|
8
8
|
interface AdminConfig {
|
|
9
9
|
publicKey: string;
|
|
@@ -12,8 +12,8 @@ interface AdminConfig {
|
|
|
12
12
|
@runtimeModule()
|
|
13
13
|
export class Admin extends RuntimeModule<AdminConfig> {
|
|
14
14
|
@runtimeMethod()
|
|
15
|
-
public isAdmin(publicKey: PublicKey) {
|
|
16
|
-
const admin = PublicKey.empty().toConstant();
|
|
15
|
+
public async isAdmin(publicKey: PublicKey) {
|
|
16
|
+
const admin = PublicKey.empty<typeof PublicKey>().toConstant();
|
|
17
17
|
assert(admin.equals(publicKey));
|
|
18
18
|
}
|
|
19
19
|
}
|