@proto-kit/module 0.1.1-develop.245 → 0.1.1-develop.254

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.
Files changed (76) hide show
  1. package/dist/method/runtimeMethod.d.ts +2 -1
  2. package/dist/method/runtimeMethod.d.ts.map +1 -1
  3. package/dist/method/runtimeMethod.js +29 -3
  4. package/package.json +2 -2
  5. package/src/method/runtimeMethod.ts +58 -5
  6. package/test/modules/Balances.test.ts +4 -8
  7. package/test/modules/Balances.ts +2 -5
  8. package/test/modules/State.test.ts +8 -3
  9. package/test/runtimeMethod.test.ts +55 -6
  10. package/test/transaction.test.ts +1 -1
  11. package/dist/method/RuntimeMethodExecutionContext.d.ts +0 -57
  12. package/dist/method/RuntimeMethodExecutionContext.d.ts.map +0 -1
  13. package/dist/method/RuntimeMethodExecutionContext.js +0 -92
  14. package/dist/method/assert.d.ts +0 -12
  15. package/dist/method/assert.d.ts.map +0 -1
  16. package/dist/method/assert.js +0 -23
  17. package/dist/module/src/factories/MethodIdFactory.d.ts +0 -9
  18. package/dist/module/src/factories/MethodIdFactory.d.ts.map +0 -1
  19. package/dist/module/src/factories/MethodIdFactory.js +0 -36
  20. package/dist/module/src/index.d.ts +0 -11
  21. package/dist/module/src/index.d.ts.map +0 -1
  22. package/dist/module/src/index.js +0 -10
  23. package/dist/module/src/method/MethodParameterDecoder.d.ts +0 -22
  24. package/dist/module/src/method/MethodParameterDecoder.d.ts.map +0 -1
  25. package/dist/module/src/method/MethodParameterDecoder.js +0 -33
  26. package/dist/module/src/method/runtimeMethod.d.ts +0 -19
  27. package/dist/module/src/method/runtimeMethod.d.ts.map +0 -1
  28. package/dist/module/src/method/runtimeMethod.js +0 -123
  29. package/dist/module/src/module/decorator.d.ts +0 -8
  30. package/dist/module/src/module/decorator.d.ts.map +0 -1
  31. package/dist/module/src/module/decorator.js +0 -15
  32. package/dist/module/src/runtime/MethodIdResolver.d.ts +0 -18
  33. package/dist/module/src/runtime/MethodIdResolver.d.ts.map +0 -1
  34. package/dist/module/src/runtime/MethodIdResolver.js +0 -50
  35. package/dist/module/src/runtime/Runtime.d.ts +0 -72
  36. package/dist/module/src/runtime/Runtime.d.ts.map +0 -1
  37. package/dist/module/src/runtime/Runtime.js +0 -184
  38. package/dist/module/src/runtime/RuntimeModule.d.ts +0 -34
  39. package/dist/module/src/runtime/RuntimeModule.d.ts.map +0 -1
  40. package/dist/module/src/runtime/RuntimeModule.js +0 -56
  41. package/dist/module/src/state/InMemoryStateService.d.ts +0 -11
  42. package/dist/module/src/state/InMemoryStateService.d.ts.map +0 -1
  43. package/dist/module/src/state/InMemoryStateService.js +0 -21
  44. package/dist/module/src/state/decorator.d.ts +0 -7
  45. package/dist/module/src/state/decorator.d.ts.map +0 -1
  46. package/dist/module/src/state/decorator.js +0 -42
  47. package/dist/protocol/src/model/Option.d.ts +0 -98
  48. package/dist/protocol/src/model/Option.d.ts.map +0 -1
  49. package/dist/protocol/src/model/Option.js +0 -98
  50. package/dist/protocol/src/model/StateTransition.d.ts +0 -96
  51. package/dist/protocol/src/model/StateTransition.d.ts.map +0 -1
  52. package/dist/protocol/src/model/StateTransition.js +0 -65
  53. package/dist/protocol/src/model/network/NetworkState.d.ts +0 -64
  54. package/dist/protocol/src/model/network/NetworkState.d.ts.map +0 -1
  55. package/dist/protocol/src/model/network/NetworkState.js +0 -12
  56. package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts +0 -70
  57. package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts.map +0 -1
  58. package/dist/protocol/src/model/transaction/ProtocolTransaction.js +0 -18
  59. package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts +0 -63
  60. package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts.map +0 -1
  61. package/dist/protocol/src/model/transaction/RuntimeTransaction.js +0 -29
  62. package/dist/protocol/src/state/assert/assert.d.ts +0 -12
  63. package/dist/protocol/src/state/assert/assert.d.ts.map +0 -1
  64. package/dist/protocol/src/state/assert/assert.js +0 -23
  65. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts +0 -60
  66. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts.map +0 -1
  67. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.js +0 -105
  68. package/dist/state/State.d.ts +0 -65
  69. package/dist/state/State.d.ts.map +0 -1
  70. package/dist/state/State.js +0 -114
  71. package/dist/state/StateMap.d.ts +0 -37
  72. package/dist/state/StateMap.d.ts.map +0 -1
  73. package/dist/state/StateMap.js +0 -56
  74. package/dist/state/StateServiceProvider.d.ts +0 -10
  75. package/dist/state/StateServiceProvider.d.ts.map +0 -1
  76. package/dist/state/StateServiceProvider.js +0 -34
@@ -1,63 +0,0 @@
1
- import { Field, PublicKey, UInt64 } from "snarkyjs";
2
- import { ProtocolTransaction } from "./ProtocolTransaction";
3
- declare const RuntimeTransaction_base: (new (value: {
4
- methodId: import("snarkyjs/dist/node/lib/field").Field;
5
- nonce: UInt64;
6
- sender: PublicKey;
7
- argsHash: import("snarkyjs/dist/node/lib/field").Field;
8
- }) => {
9
- methodId: import("snarkyjs/dist/node/lib/field").Field;
10
- nonce: UInt64;
11
- sender: PublicKey;
12
- argsHash: import("snarkyjs/dist/node/lib/field").Field;
13
- }) & {
14
- _isStruct: true;
15
- } & import("snarkyjs/dist/node/snarky").ProvablePure<{
16
- methodId: import("snarkyjs/dist/node/lib/field").Field;
17
- nonce: UInt64;
18
- sender: PublicKey;
19
- argsHash: import("snarkyjs/dist/node/lib/field").Field;
20
- }> & {
21
- toInput: (x: {
22
- methodId: import("snarkyjs/dist/node/lib/field").Field;
23
- nonce: UInt64;
24
- sender: PublicKey;
25
- argsHash: import("snarkyjs/dist/node/lib/field").Field;
26
- }) => {
27
- fields?: import("snarkyjs/dist/node/lib/field").Field[] | undefined;
28
- packed?: [import("snarkyjs/dist/node/lib/field").Field, number][] | undefined;
29
- };
30
- toJSON: (x: {
31
- methodId: import("snarkyjs/dist/node/lib/field").Field;
32
- nonce: UInt64;
33
- sender: PublicKey;
34
- argsHash: import("snarkyjs/dist/node/lib/field").Field;
35
- }) => {
36
- methodId: string;
37
- nonce: string;
38
- sender: string;
39
- argsHash: string;
40
- };
41
- fromJSON: (x: {
42
- methodId: string;
43
- nonce: string;
44
- sender: string;
45
- argsHash: string;
46
- }) => {
47
- methodId: import("snarkyjs/dist/node/lib/field").Field;
48
- nonce: UInt64;
49
- sender: PublicKey;
50
- argsHash: import("snarkyjs/dist/node/lib/field").Field;
51
- };
52
- };
53
- /**
54
- * This struct is used to expose transaction information to the runtime method
55
- * execution. This class has not all data included in transactions on purpose.
56
- * For example, we don't want to expose the signature or args as fields.
57
- */
58
- export declare class RuntimeTransaction extends RuntimeTransaction_base {
59
- static fromProtocolTransaction({ methodId, nonce, sender, argsHash, }: ProtocolTransaction): RuntimeTransaction;
60
- hash(): Field;
61
- }
62
- export {};
63
- //# sourceMappingURL=RuntimeTransaction.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"RuntimeTransaction.d.ts","sourceRoot":"","sources":["../../../../../../protocol/src/model/transaction/RuntimeTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAY,SAAS,EAAU,MAAM,EAAE,MAAM,UAAU,CAAC;AAEtE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE5D;;;;GAIG;AACH,qBAAa,kBAAmB,SAAQ,uBAKtC;WACc,uBAAuB,CAAC,EACpC,QAAQ,EACR,KAAK,EACL,MAAM,EACN,QAAQ,GACT,EAAE,mBAAmB,GAAG,kBAAkB;IASpC,IAAI,IAAI,KAAK;CAQrB"}
@@ -1,29 +0,0 @@
1
- import { Field, Poseidon, PublicKey, Struct, UInt64 } from "snarkyjs";
2
- /**
3
- * This struct is used to expose transaction information to the runtime method
4
- * execution. This class has not all data included in transactions on purpose.
5
- * For example, we don't want to expose the signature or args as fields.
6
- */
7
- export class RuntimeTransaction extends Struct({
8
- methodId: Field,
9
- nonce: UInt64,
10
- sender: PublicKey,
11
- argsHash: Field,
12
- }) {
13
- static fromProtocolTransaction({ methodId, nonce, sender, argsHash, }) {
14
- return new RuntimeTransaction({
15
- methodId,
16
- nonce,
17
- sender,
18
- argsHash,
19
- });
20
- }
21
- hash() {
22
- return Poseidon.hash([
23
- this.methodId,
24
- ...this.sender.toFields(),
25
- ...this.nonce.toFields(),
26
- this.argsHash,
27
- ]);
28
- }
29
- }
@@ -1,12 +0,0 @@
1
- import { Bool } from "snarkyjs";
2
- /**
3
- * Maintains an execution status of the current runtime module method,
4
- * while prioritizing one-time failures. The assertion won't change the
5
- * execution status if it has previously failed at least once within the
6
- * same method execution context.
7
- *
8
- * @param condition - Result of the assertion made about the execution status
9
- * @param message - Optional message describing the prior status
10
- */
11
- export declare function assert(condition: Bool, message?: string): void;
12
- //# sourceMappingURL=assert.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../../../../../protocol/src/state/assert/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAY,MAAM,UAAU,CAAC;AAO1C;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,MAAM,QAWvD"}
@@ -1,23 +0,0 @@
1
- import { Bool, Provable } from "snarkyjs";
2
- import { container } from "tsyringe";
3
- import { log } from "@proto-kit/common";
4
- import { RuntimeMethodExecutionContext } from "../context/RuntimeMethodExecutionContext";
5
- /**
6
- * Maintains an execution status of the current runtime module method,
7
- * while prioritizing one-time failures. The assertion won't change the
8
- * execution status if it has previously failed at least once within the
9
- * same method execution context.
10
- *
11
- * @param condition - Result of the assertion made about the execution status
12
- * @param message - Optional message describing the prior status
13
- */
14
- export function assert(condition, message) {
15
- const executionContext = container.resolve(RuntimeMethodExecutionContext);
16
- const previousStatus = executionContext.current().result.status;
17
- const status = Provable.if(previousStatus, Bool, condition, previousStatus);
18
- if (!condition.toBoolean()) {
19
- log.debug("Assertion failed: ", message);
20
- executionContext.setStatusMessage(message);
21
- }
22
- executionContext.setStatus(status);
23
- }
@@ -1,60 +0,0 @@
1
- import { Bool } from "snarkyjs";
2
- import { ProvableMethodExecutionContext, ProvableMethodExecutionResult } from "@proto-kit/common";
3
- import { StateTransition } from "../../model/StateTransition";
4
- import { RuntimeTransaction } from "../../model/transaction/RuntimeTransaction";
5
- import { NetworkState } from "../../model/network/NetworkState";
6
- export declare class RuntimeProvableMethodExecutionResult extends ProvableMethodExecutionResult {
7
- stateTransitions: StateTransition<any>[];
8
- status: Bool;
9
- statusMessage?: string;
10
- }
11
- export interface RuntimeMethodExecutionData {
12
- transaction: RuntimeTransaction;
13
- networkState: NetworkState;
14
- }
15
- /**
16
- * Execution context used to wrap runtime module methods,
17
- * allowing them to post relevant information (such as execution status)
18
- * into the context without any unnecessary 'prop drilling'.
19
- */
20
- export declare class RuntimeMethodExecutionContext extends ProvableMethodExecutionContext {
21
- methods: string[];
22
- input: RuntimeMethodExecutionData | undefined;
23
- private lastInput;
24
- result: RuntimeProvableMethodExecutionResult;
25
- private isSimulated;
26
- private assertSetupCalled;
27
- /**
28
- * Adds an in-method generated state transition to the current context
29
- * @param stateTransition - State transition to add to the context
30
- */
31
- addStateTransition<Value>(stateTransition: StateTransition<Value>): void;
32
- /**
33
- * @param message - Status message to acompany the current status
34
- */
35
- setStatusMessage(message?: string): void;
36
- /**
37
- * @param status - Execution status of the current method
38
- */
39
- setStatus(status: Bool): void;
40
- /**
41
- * @param input Input witness data required for a runtime execution
42
- */
43
- setup(input: RuntimeMethodExecutionData): void;
44
- setSimulated(simulated: boolean): void;
45
- /**
46
- * Manually clears/resets the execution context
47
- */
48
- clear(): void;
49
- afterMethod(): void;
50
- /**
51
- * Had to override current() otherwise it would not infer
52
- * the type of result correctly (parent type would be reused)
53
- */
54
- current(): {
55
- isFinished: boolean;
56
- result: RuntimeProvableMethodExecutionResult;
57
- input: RuntimeMethodExecutionData | undefined;
58
- };
59
- }
60
- //# sourceMappingURL=RuntimeMethodExecutionContext.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"RuntimeMethodExecutionContext.d.ts","sourceRoot":"","sources":["../../../../../../protocol/src/state/context/RuntimeMethodExecutionContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAEhC,OAAO,EAAE,8BAA8B,EAAE,6BAA6B,EAAE,MAAM,mBAAmB,CAAC;AAClG,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4CAA4C,CAAC;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAShE,qBAAa,oCAAqC,SAAQ,6BAA6B;IAE9E,gBAAgB,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,CAAM;IAE9C,MAAM,EAAE,IAAI,CAAc;IAE1B,aAAa,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,0BAA0B;IACzC,WAAW,EAAE,kBAAkB,CAAC;IAChC,YAAY,EAAE,YAAY,CAAC;CAC5B;AAED;;;;GAIG;AACH,qBACa,6BAA8B,SAAQ,8BAA8B;IACxE,OAAO,EAAE,MAAM,EAAE,CAAM;IAEvB,KAAK,EAAE,0BAA0B,GAAG,SAAS,CAAC;IAGrD,OAAO,CAAC,SAAS,CAAyC;IAE1C,MAAM,uCAA8C;IAEpE,OAAO,CAAC,WAAW,CAAkB;IAErC,OAAO,CAAC,iBAAiB;IAQzB;;;OAGG;IACI,kBAAkB,CAAC,KAAK,EAAE,eAAe,EAAE,eAAe,CAAC,KAAK,CAAC;IAKxE;;OAEG;IACI,gBAAgB,CAAC,OAAO,CAAC,EAAE,MAAM;IAQxC;;OAEG;IACI,SAAS,CAAC,MAAM,EAAE,IAAI;IAQ7B;;OAEG;IACI,KAAK,CAAC,KAAK,EAAE,0BAA0B;IAIvC,YAAY,CAAC,SAAS,EAAE,OAAO;IAItC;;OAEG;IACI,KAAK;IAIL,WAAW;IASlB;;;OAGG;IACI,OAAO;;;;;CAOf"}
@@ -1,105 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { Bool } from "snarkyjs";
8
- import { singleton } from "tsyringe";
9
- import { ProvableMethodExecutionContext, ProvableMethodExecutionResult } from "@proto-kit/common";
10
- const errors = {
11
- setupNotCalled: () => new Error("Setup has not been called prior to executing a runtime method. Be sure to do that so that the Runtime is setup property for execution"),
12
- };
13
- export class RuntimeProvableMethodExecutionResult extends ProvableMethodExecutionResult {
14
- constructor() {
15
- super(...arguments);
16
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
- this.stateTransitions = [];
18
- this.status = Bool(true);
19
- }
20
- }
21
- /**
22
- * Execution context used to wrap runtime module methods,
23
- * allowing them to post relevant information (such as execution status)
24
- * into the context without any unnecessary 'prop drilling'.
25
- */
26
- let RuntimeMethodExecutionContext = class RuntimeMethodExecutionContext extends ProvableMethodExecutionContext {
27
- constructor() {
28
- super(...arguments);
29
- this.methods = [];
30
- this.result = new RuntimeProvableMethodExecutionResult();
31
- this.isSimulated = false;
32
- }
33
- assertSetupCalled() {
34
- if (this.input === undefined) {
35
- throw errors.setupNotCalled();
36
- }
37
- }
38
- /**
39
- * Adds an in-method generated state transition to the current context
40
- * @param stateTransition - State transition to add to the context
41
- */
42
- addStateTransition(stateTransition) {
43
- this.assertSetupCalled();
44
- this.result.stateTransitions.push(stateTransition);
45
- }
46
- /**
47
- * @param message - Status message to acompany the current status
48
- */
49
- setStatusMessage(message) {
50
- var _a;
51
- this.assertSetupCalled();
52
- if (this.isSimulated) {
53
- return;
54
- }
55
- (_a = this.result).statusMessage ?? (_a.statusMessage = message);
56
- }
57
- /**
58
- * @param status - Execution status of the current method
59
- */
60
- setStatus(status) {
61
- this.assertSetupCalled();
62
- if (this.isSimulated) {
63
- return;
64
- }
65
- this.result.status = status;
66
- }
67
- /**
68
- * @param input Input witness data required for a runtime execution
69
- */
70
- setup(input) {
71
- this.input = input;
72
- }
73
- setSimulated(simulated) {
74
- this.isSimulated = simulated;
75
- }
76
- /**
77
- * Manually clears/resets the execution context
78
- */
79
- clear() {
80
- this.result = new RuntimeProvableMethodExecutionResult();
81
- }
82
- afterMethod() {
83
- super.afterMethod();
84
- if (this.isFinished) {
85
- this.lastInput = this.input;
86
- this.input = undefined;
87
- this.isSimulated = false;
88
- }
89
- }
90
- /**
91
- * Had to override current() otherwise it would not infer
92
- * the type of result correctly (parent type would be reused)
93
- */
94
- current() {
95
- return {
96
- isFinished: this.isFinished,
97
- result: this.result,
98
- input: this.lastInput,
99
- };
100
- }
101
- };
102
- RuntimeMethodExecutionContext = __decorate([
103
- singleton()
104
- ], RuntimeMethodExecutionContext);
105
- export { RuntimeMethodExecutionContext };
@@ -1,65 +0,0 @@
1
- import { Field, type FlexibleProvablePure } from "snarkyjs";
2
- import { Option, type Path } from "@proto-kit/protocol";
3
- import { PartialRuntime } from "../runtime/RuntimeModule.js";
4
- export declare class WithPath {
5
- path?: Field;
6
- hasPathOrFail(): asserts this is {
7
- path: Path;
8
- };
9
- }
10
- export declare class WithRuntime {
11
- runtime?: PartialRuntime;
12
- hasRuntimeOrFail(): asserts this is {
13
- runtime: PartialRuntime;
14
- };
15
- }
16
- declare const State_base: import("ts-mixer/dist/types/types.js").Class<any[], WithPath & WithRuntime, typeof WithPath & typeof WithRuntime, false>;
17
- /**
18
- * Utilities for runtime module state, such as get/set
19
- */
20
- export declare class State<Value> extends State_base {
21
- valueType: FlexibleProvablePure<Value>;
22
- /**
23
- * Creates a new state wrapper for the provided value type.
24
- *
25
- * @param valueType - Type of value to be stored (e.g. UInt64, Struct, ...)
26
- * @returns New state for the given value type.
27
- */
28
- static from<Value>(valueType: FlexibleProvablePure<Value>): State<Value>;
29
- /**
30
- * Computes a dummy value for the given value type.
31
- *
32
- * @param valueType - Value type to generate the dummy value for
33
- * @returns Dummy value for the given value type
34
- */
35
- static dummyValue<Value>(valueType: FlexibleProvablePure<Value>): Value;
36
- constructor(valueType: FlexibleProvablePure<Value>);
37
- /**
38
- * Provides an in-circuit witness for the current state representation,
39
- * and constructs an Option out of it.
40
- *
41
- * @returns Optional value of the current state
42
- */
43
- private witnessState;
44
- /**
45
- * Retrieves the current state and creates a state transition
46
- * anchoring the use of the current state value in the circuit.
47
- *
48
- * @returns Option representation of the current state.
49
- */
50
- get(): Option<Value>;
51
- /**
52
- * Sets a new state value by creating a state transition from
53
- * the current value to the newly set value.
54
- *
55
- * The newly set value isn't available via state.get(), since the
56
- * state transitions are not applied within the same circuit.
57
- * You can however store and access your new value in
58
- * a separate circuit variable.
59
- *
60
- * @param value - Value to be set as the current state
61
- */
62
- set(value: Value): void;
63
- }
64
- export {};
65
- //# sourceMappingURL=State.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"State.d.ts","sourceRoot":"","sources":["../../src/state/State.ts"],"names":[],"mappings":"AAGA,OAAO,EAAQ,KAAK,EAAY,KAAK,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAE5E,OAAO,EAAE,MAAM,EAAmB,KAAK,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAEzE,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAG7D,qBAAa,QAAQ;IACZ,IAAI,CAAC,EAAE,KAAK,CAAC;IAEb,aAAa,IAAI,OAAO,CAAC,IAAI,IAAI;QAAE,IAAI,EAAE,IAAI,CAAA;KAAE;CAOvD;AAED,qBAAa,WAAW;IACf,OAAO,CAAC,EAAE,cAAc,CAAC;IAEzB,gBAAgB,IAAI,OAAO,CAAC,IAAI,IAAI;QACzC,OAAO,EAAE,cAAc,CAAC;KACzB;CAOF;;AAED;;GAEG;AACH,qBAAa,KAAK,CAAC,KAAK,CAAE,SAAQ,UAA4B;IA2BlC,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IA1BhE;;;;;OAKG;WACW,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IAIhE;;;;;OAKG;WACW,UAAU,CAAC,KAAK,EAC5B,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC,GACrC,KAAK;gBAQkB,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IAIhE;;;;;OAKG;IACH,OAAO,CAAC,YAAY;IA6BpB;;;;;OAKG;IACI,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC;IAc3B;;;;;;;;;;OAUG;IACI,GAAG,CAAC,KAAK,EAAE,KAAK;CAiBxB"}
@@ -1,114 +0,0 @@
1
- /* eslint-disable new-cap */
2
- import { Mixin } from "ts-mixer";
3
- import { Bool, Field, Provable } from "snarkyjs";
4
- import { container } from "tsyringe";
5
- import { Option, StateTransition } from "@proto-kit/protocol";
6
- import { RuntimeMethodExecutionContext } from "../method/RuntimeMethodExecutionContext.js";
7
- export class WithPath {
8
- hasPathOrFail() {
9
- if (!this.path) {
10
- throw new Error("Could not find 'path', did you forget to add '@state' to your state property?");
11
- }
12
- }
13
- }
14
- export class WithRuntime {
15
- hasRuntimeOrFail() {
16
- if (!this.runtime) {
17
- throw new Error("Could not find 'runtime', did you forget to add '@state' to your state property?");
18
- }
19
- }
20
- }
21
- /**
22
- * Utilities for runtime module state, such as get/set
23
- */
24
- export class State extends Mixin(WithPath, WithRuntime) {
25
- /**
26
- * Creates a new state wrapper for the provided value type.
27
- *
28
- * @param valueType - Type of value to be stored (e.g. UInt64, Struct, ...)
29
- * @returns New state for the given value type.
30
- */
31
- static from(valueType) {
32
- return new State(valueType);
33
- }
34
- /**
35
- * Computes a dummy value for the given value type.
36
- *
37
- * @param valueType - Value type to generate the dummy value for
38
- * @returns Dummy value for the given value type
39
- */
40
- static dummyValue(valueType) {
41
- const length = valueType.sizeInFields();
42
- const fields = Array.from({ length }, () => Field(0));
43
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
44
- return valueType.fromFields(fields);
45
- }
46
- constructor(valueType) {
47
- super();
48
- this.valueType = valueType;
49
- }
50
- /**
51
- * Provides an in-circuit witness for the current state representation,
52
- * and constructs an Option out of it.
53
- *
54
- * @returns Optional value of the current state
55
- */
56
- witnessState() {
57
- // get the value from storage, or return a dummy value instead
58
- const value = Provable.witness(this.valueType, () => {
59
- this.hasRuntimeOrFail();
60
- this.hasPathOrFail();
61
- const fields = this.runtime.stateService.get(this.path);
62
- if (fields) {
63
- // eslint-disable-next-line max-len
64
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
65
- return this.valueType.fromFields(fields);
66
- }
67
- return State.dummyValue(this.valueType);
68
- });
69
- // check if the value exists in the storage or not
70
- const isSome = Provable.witness(Bool, () => {
71
- this.hasRuntimeOrFail();
72
- this.hasPathOrFail();
73
- const fields = this.runtime.stateService.get(this.path);
74
- return Bool(fields !== undefined);
75
- });
76
- return Option.from(isSome, value, this.valueType);
77
- }
78
- /**
79
- * Retrieves the current state and creates a state transition
80
- * anchoring the use of the current state value in the circuit.
81
- *
82
- * @returns Option representation of the current state.
83
- */
84
- get() {
85
- const option = this.witnessState();
86
- this.hasPathOrFail();
87
- const stateTransition = StateTransition.from(this.path, option);
88
- container
89
- .resolve(RuntimeMethodExecutionContext)
90
- .addStateTransition(stateTransition);
91
- return option;
92
- }
93
- /**
94
- * Sets a new state value by creating a state transition from
95
- * the current value to the newly set value.
96
- *
97
- * The newly set value isn't available via state.get(), since the
98
- * state transitions are not applied within the same circuit.
99
- * You can however store and access your new value in
100
- * a separate circuit variable.
101
- *
102
- * @param value - Value to be set as the current state
103
- */
104
- set(value) {
105
- // link the transition to the current state
106
- const fromOption = this.witnessState();
107
- const toOption = Option.from(Bool(true), value, this.valueType);
108
- this.hasPathOrFail();
109
- const stateTransition = StateTransition.fromTo(this.path, fromOption, toOption);
110
- container
111
- .resolve(RuntimeMethodExecutionContext)
112
- .addStateTransition(stateTransition);
113
- }
114
- }
@@ -1,37 +0,0 @@
1
- import type { Field, FlexibleProvablePure } from "snarkyjs";
2
- import { type Option, ToFieldable } from "@proto-kit/protocol";
3
- import { WithRuntime, WithPath } from "./State.js";
4
- declare const StateMap_base: import("ts-mixer/dist/types/types.js").Class<any[], WithPath & WithRuntime, typeof WithPath & typeof WithRuntime, false>;
5
- /**
6
- * Map-like wrapper for state
7
- */
8
- export declare class StateMap<KeyType, ValueType extends ToFieldable> extends StateMap_base {
9
- keyType: FlexibleProvablePure<KeyType>;
10
- valueType: FlexibleProvablePure<ValueType>;
11
- /**
12
- * Create a new state map with the given key and value types
13
- *
14
- * @param keyType - Type to be used as a key
15
- * @param valueType - Type to be stored as a value
16
- * @returns State map with provided key and value types.
17
- */
18
- static from<KeyType, ValueType extends ToFieldable>(keyType: FlexibleProvablePure<KeyType>, valueType: FlexibleProvablePure<ValueType>): StateMap<KeyType, ValueType>;
19
- constructor(keyType: FlexibleProvablePure<KeyType>, valueType: FlexibleProvablePure<ValueType>);
20
- getPath(key: KeyType): Field;
21
- /**
22
- * Obtains a value for the provided key in the current state map.
23
- *
24
- * @param key - Key to obtain the state for
25
- * @returns Value for the provided key.
26
- */
27
- get(key: KeyType): Option<ValueType>;
28
- /**
29
- * Sets a value for the given key in the current state map.
30
- *
31
- * @param key - Key to store the value under
32
- * @param value - Value to be stored under the given key
33
- */
34
- set(key: KeyType, value: ValueType): void;
35
- }
36
- export {};
37
- //# sourceMappingURL=StateMap.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"StateMap.d.ts","sourceRoot":"","sources":["../../src/state/StateMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAC5D,OAAO,EAAQ,KAAK,MAAM,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAGrE,OAAO,EAAS,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;;AAE1D;;GAEG;AAEH,qBAAa,QAAQ,CAAC,OAAO,EAAE,SAAS,SAAS,WAAW,CAAE,SAAQ,aAA4B;IAgBvF,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC;IACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC;IAhBnD;;;;;;OAMG;WACW,IAAI,CAAC,OAAO,EAAE,SAAS,SAAS,WAAW,EACvD,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC,EACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC;gBAMnC,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC,EACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC;IAK5C,OAAO,CAAC,GAAG,EAAE,OAAO,GAAG,KAAK;IAKnC;;;;;OAKG;IACI,GAAG,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC;IAU3C;;;;;OAKG;IACI,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS;CAS1C"}
@@ -1,56 +0,0 @@
1
- import { Path } from "@proto-kit/protocol";
2
- import { Mixin } from "ts-mixer";
3
- import { State, WithRuntime, WithPath } from "./State.js";
4
- /**
5
- * Map-like wrapper for state
6
- */
7
- // eslint-disable-next-line new-cap
8
- export class StateMap extends Mixin(WithPath, WithRuntime) {
9
- /**
10
- * Create a new state map with the given key and value types
11
- *
12
- * @param keyType - Type to be used as a key
13
- * @param valueType - Type to be stored as a value
14
- * @returns State map with provided key and value types.
15
- */
16
- static from(keyType, valueType) {
17
- return new StateMap(keyType, valueType);
18
- }
19
- constructor(keyType, valueType) {
20
- super();
21
- this.keyType = keyType;
22
- this.valueType = valueType;
23
- }
24
- getPath(key) {
25
- this.hasPathOrFail();
26
- return Path.fromKey(this.path, this.keyType, key);
27
- }
28
- /**
29
- * Obtains a value for the provided key in the current state map.
30
- *
31
- * @param key - Key to obtain the state for
32
- * @returns Value for the provided key.
33
- */
34
- get(key) {
35
- const state = State.from(this.valueType);
36
- this.hasPathOrFail();
37
- this.hasRuntimeOrFail();
38
- state.path = this.getPath(key);
39
- state.runtime = this.runtime;
40
- return state.get();
41
- }
42
- /**
43
- * Sets a value for the given key in the current state map.
44
- *
45
- * @param key - Key to store the value under
46
- * @param value - Value to be stored under the given key
47
- */
48
- set(key, value) {
49
- const state = State.from(this.valueType);
50
- this.hasPathOrFail();
51
- this.hasRuntimeOrFail();
52
- state.path = Path.fromKey(this.path, this.keyType, key);
53
- state.runtime = this.runtime;
54
- state.set(value);
55
- }
56
- }
@@ -1,10 +0,0 @@
1
- import { StateService } from "./InMemoryStateService";
2
- export declare class StateServiceProvider {
3
- private currentStateService;
4
- private readonly defaultStateService;
5
- constructor(currentStateService: StateService);
6
- get stateService(): StateService;
7
- setCurrentStateService(service: StateService): void;
8
- resetToDefault(): void;
9
- }
10
- //# sourceMappingURL=StateServiceProvider.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"StateServiceProvider.d.ts","sourceRoot":"","sources":["../../src/state/StateServiceProvider.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD,qBACa,oBAAoB;IAIL,OAAO,CAAC,mBAAmB;IAHrD,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA0C;gBAG5C,mBAAmB,EAAE,YAAY;IAGnE,IAAW,YAAY,IAAI,YAAY,CAEtC;IAEM,sBAAsB,CAAC,OAAO,EAAE,YAAY;IAI5C,cAAc;CAGtB"}
@@ -1,34 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- var __metadata = (this && this.__metadata) || function (k, v) {
8
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
- };
10
- var __param = (this && this.__param) || function (paramIndex, decorator) {
11
- return function (target, key) { decorator(target, key, paramIndex); }
12
- };
13
- import { inject, injectable } from "tsyringe";
14
- let StateServiceProvider = class StateServiceProvider {
15
- constructor(currentStateService) {
16
- this.currentStateService = currentStateService;
17
- this.defaultStateService = this.currentStateService;
18
- }
19
- get stateService() {
20
- return this.currentStateService;
21
- }
22
- setCurrentStateService(service) {
23
- this.currentStateService = service;
24
- }
25
- resetToDefault() {
26
- this.currentStateService = this.defaultStateService;
27
- }
28
- };
29
- StateServiceProvider = __decorate([
30
- injectable(),
31
- __param(0, inject("StateService")),
32
- __metadata("design:paramtypes", [Object])
33
- ], StateServiceProvider);
34
- export { StateServiceProvider };