@proto-kit/module 0.1.1-develop.260 → 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":"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.260+4c60192",
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": "4c60192291eba37f98698b538ae893afaef7e835"
34
+ "gitHead": "8731dfa8f5df502eadf6266a7216617737985146"
35
35
  }
@@ -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
  }