@proto-kit/module 0.1.1-develop.257 → 0.1.1-develop.261

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.
@@ -1 +1 @@
1
- {"version":3,"file":"runtimeMethod.d.ts","sourceRoot":"","sources":["../../src/method/runtimeMethod.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,eAAe,EAGf,kBAAkB,EAEnB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAIL,WAAW,EACZ,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAoBjE,wBAAgB,sBAAsB,CAEpC,gBAAgB,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,mDAczC;AAGD,MAAM,MAAM,aAAa,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,kBAAkB,CAAC;AAEvE,wBAAgB,eAAe,CAC7B,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,EAC5B,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,EAC7C,eAAe,EAAE,WAAW,EAAE,iBA0E/B;AAED,wBAAgB,iBAAiB,CAC/B,iBAAiB,EAAE,MAAM,EACzB,UAAU,EAAE,MAAM,UAGnB;AAED,eAAO,MAAM,wBAAwB,eAAe,CAAC;AACrD,eAAO,MAAM,6BAA6B,8BAA8B,CAAC;AAEzE;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,EAC9B,WAAW,EAAE,MAAM,WAKpB;AAED,wBAAgB,aAAa,aAEjB,cAAc,OAAO,CAAC,cAClB,MAAM,cACN,kBAAkB,UAwFjC"}
1
+ {"version":3,"file":"runtimeMethod.d.ts","sourceRoot":"","sources":["../../src/method/runtimeMethod.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,eAAe,EAGf,kBAAkB,EAEnB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAIL,WAAW,EACZ,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAoBjE,wBAAgB,sBAAsB,CAEpC,gBAAgB,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,mDAczC;AAGD,MAAM,MAAM,aAAa,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,kBAAkB,CAAC;AAEvE,wBAAgB,eAAe,CAC7B,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,EAC5B,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,EAC7C,eAAe,EAAE,WAAW,EAAE,iBAoF/B;AAED,wBAAgB,iBAAiB,CAC/B,iBAAiB,EAAE,MAAM,EACzB,UAAU,EAAE,MAAM,UAGnB;AAED,eAAO,MAAM,wBAAwB,eAAe,CAAC;AACrD,eAAO,MAAM,6BAA6B,8BAA8B,CAAC;AAEzE;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,EAC9B,WAAW,EAAE,MAAM,WAKpB;AAED,wBAAgB,aAAa,aAEjB,cAAc,OAAO,CAAC,cAClB,MAAM,cACN,kBAAkB,UAwFjC"}
@@ -40,13 +40,17 @@ export function toWrappedMethod(methodName, moduleMethod, methodArguments) {
40
40
  throw errors.fieldNotConstant("methodId");
41
41
  }
42
42
  input.transaction.methodId.assertEquals(thisMethodId, "Runtimemethod called with wrong methodId on the transaction object");
43
+ const paramTypes = Reflect.getMetadata("design:paramtypes", this, methodName);
44
+ /**
45
+ * Use the type info obtained previously to convert
46
+ * the args passed to fields
47
+ */
48
+ const argsFields = args.flatMap((arg, index) => paramTypes[index].toFields(arg));
43
49
  // Assert that the argsHash that has been signed matches the given arguments
44
50
  // We can use js-if here, because methodArguments is statically sizes
45
51
  // i.e. the result of the if-statement will be the same for all executions
46
52
  // of this method
47
- const argsHash = methodArguments.length > 0
48
- ? Poseidon.hash(methodArguments.flatMap((argument) => argument.toFields()))
49
- : Field(0);
53
+ const argsHash = methodArguments.length > 0 ? Poseidon.hash(argsFields) : Field(0);
50
54
  input.transaction.argsHash.assertEquals(argsHash, "argsHash and therefore arguments of transaction and runtime call does not match");
51
55
  const transactionHash = input.transaction.hash();
52
56
  const networkStateHash = input.networkState.hash();
@@ -1 +1 @@
1
- {"version":3,"file":"MethodIdResolver.d.ts","sourceRoot":"","sources":["../../src/runtime/MethodIdResolver.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAM/D;;;;;;;GAOG;AACH,qBAAa,gBAAgB;IAIzB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAJ1B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAiC;gBAGzC,OAAO,EAAE,OAAO,CAAC,oBAAoB,CAAC,EACtC,OAAO,EAAE,oBAAoB;IAmBzC,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS;IAsBnE,WAAW,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM;CAQnE"}
1
+ {"version":3,"file":"MethodIdResolver.d.ts","sourceRoot":"","sources":["../../src/runtime/MethodIdResolver.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAM/D;;;;;;;GAOG;AACH,qBAAa,gBAAgB;IAMzB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAN1B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAEpB;gBAGY,OAAO,EAAE,OAAO,CAAC,oBAAoB,CAAC,EACtC,OAAO,EAAE,oBAAoB;IAkBzC,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS;IAcnE,WAAW,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM;CAQnE"}
@@ -1,4 +1,5 @@
1
- import { singleFieldToString, stringToField } from "@proto-kit/protocol";
1
+ import { stringToField } from "@proto-kit/protocol";
2
+ import { Poseidon } from "snarkyjs";
2
3
  const offset = 128n;
3
4
  // eslint-disable-next-line @typescript-eslint/no-magic-numbers
4
5
  const modulus = 2n ** (offset - 1n);
@@ -17,34 +18,30 @@ export class MethodIdResolver {
17
18
  this.dictionary = {};
18
19
  this.dictionary = runtime.runtimeModuleNames.reduce((dict, moduleName) => {
19
20
  this.runtime.assertIsValidModuleName(modules, moduleName);
20
- dict[(stringToField(moduleName).toBigInt() % modulus).toString()] =
21
- moduleName;
22
21
  runtime.resolve(moduleName).runtimeMethodNames.forEach((methodName) => {
23
- dict[(stringToField(methodName).toBigInt() % modulus).toString()] =
24
- methodName;
22
+ dict[this.getMethodId(moduleName, methodName).toString()] = {
23
+ moduleName,
24
+ methodName,
25
+ };
25
26
  });
26
27
  return dict;
27
28
  }, {});
28
29
  }
29
30
  getMethodNameFromId(methodId) {
30
- const moduleNameHash = singleFieldToString(methodId >> offset);
31
- const methodNameHash = singleFieldToString(methodId % modulus);
32
- const moduleName = this.dictionary[moduleNameHash];
31
+ const { moduleName, methodName } = this.dictionary[methodId.toString()];
33
32
  // eslint-disable-next-line no-warning-comments
34
33
  // TODO Replace by throwing exception?
35
- if (moduleName === undefined) {
34
+ if (moduleName === undefined || methodName === undefined) {
36
35
  return undefined;
37
36
  }
38
37
  this.runtime.assertIsValidModuleName(this.modules, moduleName);
39
- const methodName = this.dictionary[methodNameHash];
40
- if (methodName === undefined) {
41
- return undefined;
42
- }
43
38
  return [moduleName, methodName];
44
39
  }
45
40
  getMethodId(moduleName, methodName) {
46
41
  this.runtime.assertIsValidModuleName(this.modules, moduleName);
47
- return ((stringToField(moduleName).toBigInt() % modulus << offset) +
48
- (stringToField(methodName).toBigInt() % modulus));
42
+ return Poseidon.hash([
43
+ stringToField(moduleName),
44
+ stringToField(methodName),
45
+ ]).toBigInt();
49
46
  }
50
47
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@proto-kit/module",
3
3
  "license": "MIT",
4
4
  "private": false,
5
- "version": "0.1.1-develop.257+3c175a2",
5
+ "version": "0.1.1-develop.261+8731dfa",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "build": "tsc -p tsconfig.json",
@@ -31,5 +31,5 @@
31
31
  "snarkyjs": "0.12.0",
32
32
  "tsyringe": "^4.7.0"
33
33
  },
34
- "gitHead": "3c175a2ca24b9c437dce98f11114942dec306df8"
34
+ "gitHead": "8731dfa8f5df502eadf6266a7216617737985146"
35
35
  }
@@ -1,4 +1,4 @@
1
- import { Field, Poseidon } from "snarkyjs";
1
+ import { Field, FlexibleProvable, Poseidon } from "snarkyjs";
2
2
  import { container } from "tsyringe";
3
3
  import {
4
4
  StateTransition,
@@ -100,16 +100,26 @@ export function toWrappedMethod(
100
100
  "Runtimemethod called with wrong methodId on the transaction object"
101
101
  );
102
102
 
103
+ const paramTypes: FlexibleProvable<unknown>[] = Reflect.getMetadata(
104
+ "design:paramtypes",
105
+ this,
106
+ methodName
107
+ );
108
+
109
+ /**
110
+ * Use the type info obtained previously to convert
111
+ * the args passed to fields
112
+ */
113
+ const argsFields = args.flatMap((arg, index) =>
114
+ paramTypes[index].toFields(arg as any)
115
+ );
116
+
103
117
  // Assert that the argsHash that has been signed matches the given arguments
104
118
  // We can use js-if here, because methodArguments is statically sizes
105
119
  // i.e. the result of the if-statement will be the same for all executions
106
120
  // of this method
107
121
  const argsHash =
108
- methodArguments.length > 0
109
- ? Poseidon.hash(
110
- methodArguments.flatMap((argument) => argument.toFields())
111
- )
112
- : Field(0);
122
+ methodArguments.length > 0 ? Poseidon.hash(argsFields) : Field(0);
113
123
 
114
124
  input.transaction.argsHash.assertEquals(
115
125
  argsHash,
@@ -1,11 +1,8 @@
1
- import { singleFieldToString, stringToField } from "@proto-kit/protocol";
1
+ import { stringToField } from "@proto-kit/protocol";
2
+ import { Poseidon } from "snarkyjs";
2
3
 
3
4
  import type { Runtime, RuntimeModulesRecord } from "./Runtime";
4
5
 
5
- const offset = 128n;
6
- // eslint-disable-next-line @typescript-eslint/no-magic-numbers
7
- const modulus = 2n ** (offset - 1n);
8
-
9
6
  /**
10
7
  * How do we encode MethodIds
11
8
  * A MethodId is defined as the following in little-endian
@@ -15,47 +12,40 @@ const modulus = 2n ** (offset - 1n);
15
12
  * ]
16
13
  */
17
14
  export class MethodIdResolver {
18
- private readonly dictionary: { [key: string]: string } = {};
15
+ private readonly dictionary: {
16
+ [key: string]: { moduleName: string; methodName: string };
17
+ } = {};
19
18
 
20
19
  public constructor(
21
20
  private readonly runtime: Runtime<RuntimeModulesRecord>,
22
21
  private readonly modules: RuntimeModulesRecord
23
22
  ) {
24
- this.dictionary = runtime.runtimeModuleNames.reduce<Record<string, string>>(
25
- (dict, moduleName) => {
26
- this.runtime.assertIsValidModuleName(modules, moduleName);
27
-
28
- dict[(stringToField(moduleName).toBigInt() % modulus).toString()] =
29
- moduleName;
30
-
31
- runtime.resolve(moduleName).runtimeMethodNames.forEach((methodName) => {
32
- dict[(stringToField(methodName).toBigInt() % modulus).toString()] =
33
- methodName;
34
- });
35
- return dict;
36
- },
37
- {}
38
- );
23
+ this.dictionary = runtime.runtimeModuleNames.reduce<
24
+ Record<string, { moduleName: string; methodName: string }>
25
+ >((dict, moduleName) => {
26
+ this.runtime.assertIsValidModuleName(modules, moduleName);
27
+
28
+ runtime.resolve(moduleName).runtimeMethodNames.forEach((methodName) => {
29
+ dict[this.getMethodId(moduleName, methodName).toString()] = {
30
+ moduleName,
31
+ methodName,
32
+ };
33
+ });
34
+
35
+ return dict;
36
+ }, {});
39
37
  }
40
38
 
41
39
  public getMethodNameFromId(methodId: bigint): [string, string] | undefined {
42
- const moduleNameHash = singleFieldToString(methodId >> offset);
43
- const methodNameHash = singleFieldToString(methodId % modulus);
44
-
45
- const moduleName: string | undefined = this.dictionary[moduleNameHash];
40
+ const { moduleName, methodName } = this.dictionary[methodId.toString()];
46
41
 
47
42
  // eslint-disable-next-line no-warning-comments
48
43
  // TODO Replace by throwing exception?
49
- if (moduleName === undefined) {
44
+ if (moduleName === undefined || methodName === undefined) {
50
45
  return undefined;
51
46
  }
52
- this.runtime.assertIsValidModuleName(this.modules, moduleName);
53
47
 
54
- const methodName: string | undefined = this.dictionary[methodNameHash];
55
-
56
- if (methodName === undefined) {
57
- return undefined;
58
- }
48
+ this.runtime.assertIsValidModuleName(this.modules, moduleName);
59
49
 
60
50
  return [moduleName, methodName];
61
51
  }
@@ -63,9 +53,9 @@ export class MethodIdResolver {
63
53
  public getMethodId(moduleName: string, methodName: string): bigint {
64
54
  this.runtime.assertIsValidModuleName(this.modules, moduleName);
65
55
 
66
- return (
67
- (stringToField(moduleName).toBigInt() % modulus << offset) +
68
- (stringToField(methodName).toBigInt() % modulus)
69
- );
56
+ return Poseidon.hash([
57
+ stringToField(moduleName),
58
+ stringToField(methodName),
59
+ ]).toBigInt();
70
60
  }
71
61
  }