@proto-kit/module 0.1.1-develop.260 → 0.1.1-develop.263

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,11 +1,7 @@
1
1
  import type { Runtime, RuntimeModulesRecord } from "./Runtime";
2
2
  /**
3
- * How do we encode MethodIds
4
- * A MethodId is defined as the following in little-endian
5
- * [0
6
- * ...hash(stringToField(moduleName))[0..128],
7
- * ...hash(stringToField(methodName))[0..128]
8
- * ]
3
+ * Please see `getMethodId` to learn more about
4
+ * methodId encoding
9
5
  */
10
6
  export declare class MethodIdResolver {
11
7
  private readonly runtime;
@@ -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":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAE/D;;;GAGG;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,14 +1,8 @@
1
- import { singleFieldToString, stringToField } from "@proto-kit/protocol";
2
- const offset = 128n;
3
- // eslint-disable-next-line @typescript-eslint/no-magic-numbers
4
- const modulus = 2n ** (offset - 1n);
1
+ import { stringToField } from "@proto-kit/protocol";
2
+ import { Poseidon } from "snarkyjs";
5
3
  /**
6
- * How do we encode MethodIds
7
- * A MethodId is defined as the following in little-endian
8
- * [0
9
- * ...hash(stringToField(moduleName))[0..128],
10
- * ...hash(stringToField(methodName))[0..128]
11
- * ]
4
+ * Please see `getMethodId` to learn more about
5
+ * methodId encoding
12
6
  */
13
7
  export class MethodIdResolver {
14
8
  constructor(runtime, modules) {
@@ -17,34 +11,30 @@ export class MethodIdResolver {
17
11
  this.dictionary = {};
18
12
  this.dictionary = runtime.runtimeModuleNames.reduce((dict, moduleName) => {
19
13
  this.runtime.assertIsValidModuleName(modules, moduleName);
20
- dict[(stringToField(moduleName).toBigInt() % modulus).toString()] =
21
- moduleName;
22
14
  runtime.resolve(moduleName).runtimeMethodNames.forEach((methodName) => {
23
- dict[(stringToField(methodName).toBigInt() % modulus).toString()] =
24
- methodName;
15
+ dict[this.getMethodId(moduleName, methodName).toString()] = {
16
+ moduleName,
17
+ methodName,
18
+ };
25
19
  });
26
20
  return dict;
27
21
  }, {});
28
22
  }
29
23
  getMethodNameFromId(methodId) {
30
- const moduleNameHash = singleFieldToString(methodId >> offset);
31
- const methodNameHash = singleFieldToString(methodId % modulus);
32
- const moduleName = this.dictionary[moduleNameHash];
24
+ const { moduleName, methodName } = this.dictionary[methodId.toString()];
33
25
  // eslint-disable-next-line no-warning-comments
34
26
  // TODO Replace by throwing exception?
35
- if (moduleName === undefined) {
27
+ if (moduleName === undefined || methodName === undefined) {
36
28
  return undefined;
37
29
  }
38
30
  this.runtime.assertIsValidModuleName(this.modules, moduleName);
39
- const methodName = this.dictionary[methodNameHash];
40
- if (methodName === undefined) {
41
- return undefined;
42
- }
43
31
  return [moduleName, methodName];
44
32
  }
45
33
  getMethodId(moduleName, methodName) {
46
34
  this.runtime.assertIsValidModuleName(this.modules, moduleName);
47
- return ((stringToField(moduleName).toBigInt() % modulus << offset) +
48
- (stringToField(methodName).toBigInt() % modulus));
35
+ return Poseidon.hash([
36
+ stringToField(moduleName),
37
+ stringToField(methodName),
38
+ ]).toBigInt();
49
39
  }
50
40
  }
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.263+f167699",
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": "f167699bddb5831155c4aa9587b4da032c30db0d"
35
35
  }
@@ -1,61 +1,47 @@
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
- * How do we encode MethodIds
11
- * A MethodId is defined as the following in little-endian
12
- * [0
13
- * ...hash(stringToField(moduleName))[0..128],
14
- * ...hash(stringToField(methodName))[0..128]
15
- * ]
7
+ * Please see `getMethodId` to learn more about
8
+ * methodId encoding
16
9
  */
17
10
  export class MethodIdResolver {
18
- private readonly dictionary: { [key: string]: string } = {};
11
+ private readonly dictionary: {
12
+ [key: string]: { moduleName: string; methodName: string };
13
+ } = {};
19
14
 
20
15
  public constructor(
21
16
  private readonly runtime: Runtime<RuntimeModulesRecord>,
22
17
  private readonly modules: RuntimeModulesRecord
23
18
  ) {
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
- );
19
+ this.dictionary = runtime.runtimeModuleNames.reduce<
20
+ Record<string, { moduleName: string; methodName: string }>
21
+ >((dict, moduleName) => {
22
+ this.runtime.assertIsValidModuleName(modules, moduleName);
23
+
24
+ runtime.resolve(moduleName).runtimeMethodNames.forEach((methodName) => {
25
+ dict[this.getMethodId(moduleName, methodName).toString()] = {
26
+ moduleName,
27
+ methodName,
28
+ };
29
+ });
30
+
31
+ return dict;
32
+ }, {});
39
33
  }
40
34
 
41
35
  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];
36
+ const { moduleName, methodName } = this.dictionary[methodId.toString()];
46
37
 
47
38
  // eslint-disable-next-line no-warning-comments
48
39
  // TODO Replace by throwing exception?
49
- if (moduleName === undefined) {
40
+ if (moduleName === undefined || methodName === undefined) {
50
41
  return undefined;
51
42
  }
52
- this.runtime.assertIsValidModuleName(this.modules, moduleName);
53
43
 
54
- const methodName: string | undefined = this.dictionary[methodNameHash];
55
-
56
- if (methodName === undefined) {
57
- return undefined;
58
- }
44
+ this.runtime.assertIsValidModuleName(this.modules, moduleName);
59
45
 
60
46
  return [moduleName, methodName];
61
47
  }
@@ -63,9 +49,9 @@ export class MethodIdResolver {
63
49
  public getMethodId(moduleName: string, methodName: string): bigint {
64
50
  this.runtime.assertIsValidModuleName(this.modules, moduleName);
65
51
 
66
- return (
67
- (stringToField(moduleName).toBigInt() % modulus << offset) +
68
- (stringToField(methodName).toBigInt() % modulus)
69
- );
52
+ return Poseidon.hash([
53
+ stringToField(moduleName),
54
+ stringToField(methodName),
55
+ ]).toBigInt();
70
56
  }
71
57
  }