@ic-reactor/core 1.11.0 → 1.12.0

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/README.md CHANGED
@@ -19,7 +19,7 @@ yarn add @ic-reactor/core
19
19
  or you can use the UMD version:
20
20
 
21
21
  ```html
22
- <script src="https://github.com/B3Pay/ic-reactor/releases/download/v1.10.3/ic-reactor-core.min.js"></script>
22
+ <script src="https://github.com/B3Pay/ic-reactor/releases/download/v1.11.0/ic-reactor-core.min.js"></script>
23
23
  ```
24
24
 
25
25
  ### Using `createReactorCore`
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Creates a stable string representation of any JavaScript value
3
+ * Handles circular references and maintains consistent object key ordering
4
+ */
5
+ export declare function stringifyStable(value: unknown): string;
6
+ /**
7
+ * Creates a simple numeric hash code and returns it as a hex string
8
+ * @param value - Any JavaScript value
9
+ * @param length - Desired length of the hex string (default: 8)
10
+ * @returns string - Hex string of specified length
11
+ */
12
+ export declare function createSimpleHash(value: unknown, length?: number): string;
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSimpleHash = exports.stringifyStable = void 0;
4
+ /**
5
+ * Creates a stable string representation of any JavaScript value
6
+ * Handles circular references and maintains consistent object key ordering
7
+ */
8
+ function stringifyStable(value) {
9
+ const seen = new WeakSet();
10
+ return JSON.stringify(value, (_, value) => {
11
+ if (value === undefined)
12
+ return "[undefined]";
13
+ if (value === null)
14
+ return "[null]";
15
+ if (Number.isNaN(value))
16
+ return "[NaN]";
17
+ if (value === Infinity)
18
+ return "[Infinity]";
19
+ if (value === -Infinity)
20
+ return "[-Infinity]";
21
+ if (typeof value === "bigint")
22
+ return value.toString();
23
+ if (typeof value === "function")
24
+ return value.toString();
25
+ if (value instanceof Date)
26
+ return value.toISOString();
27
+ if (value instanceof RegExp)
28
+ return value.toString();
29
+ if (ArrayBuffer.isView(value)) {
30
+ return Array.from(value).join(",");
31
+ }
32
+ if (typeof value === "object" && value !== null) {
33
+ if (seen.has(value))
34
+ return "[Circular]";
35
+ seen.add(value);
36
+ if (Array.isArray(value)) {
37
+ return value;
38
+ }
39
+ const sortedObj = {};
40
+ const sortedKeys = Object.keys(value).sort();
41
+ for (const key of sortedKeys) {
42
+ sortedObj[key] = value[key];
43
+ }
44
+ return sortedObj;
45
+ }
46
+ return value;
47
+ });
48
+ }
49
+ exports.stringifyStable = stringifyStable;
50
+ /**
51
+ * Creates a simple numeric hash code and returns it as a hex string
52
+ * @param value - Any JavaScript value
53
+ * @param length - Desired length of the hex string (default: 8)
54
+ * @returns string - Hex string of specified length
55
+ */
56
+ function createSimpleHash(value, length = 8) {
57
+ const str = stringifyStable(value);
58
+ let hash = 0;
59
+ // Generate a more distributed hash
60
+ for (let i = 0; i < str.length; i++) {
61
+ const char = str.charCodeAt(i);
62
+ hash = (hash << 5) - hash + char;
63
+ hash = hash & hash; // Convert to 32-bit integer
64
+ }
65
+ // Convert to positive hex string and ensure proper length
66
+ const positiveHash = Math.abs(hash);
67
+ const hexString = positiveHash.toString(16);
68
+ // Pad with zeros to match desired length
69
+ return hexString.padStart(length, "0");
70
+ }
71
+ exports.createSimpleHash = createSimpleHash;
@@ -13,6 +13,7 @@ exports.createCompiledResult = exports.stringToHash = exports.generateActorHash
13
13
  const agent_1 = require("@dfinity/agent");
14
14
  const middleware_1 = require("zustand/middleware");
15
15
  const vanilla_1 = require("zustand/vanilla");
16
+ const hash_1 = require("./hash");
16
17
  function createStoreWithOptionalDevtools(initialState, config) {
17
18
  if (config.withDevtools) {
18
19
  return (0, vanilla_1.createStore)((0, middleware_1.devtools)(() => initialState, Object.assign({ serialize: {
@@ -75,8 +76,8 @@ const jsonToString = (json, space = 2) => {
75
76
  };
76
77
  exports.jsonToString = jsonToString;
77
78
  const generateRequestHash = (args = []) => {
78
- const serializedArgs = (0, agent_1.hashValue)(args);
79
- return `0x${(0, agent_1.toHex)(serializedArgs)}`;
79
+ const serializedArgs = (0, hash_1.createSimpleHash)(args);
80
+ return `0x${serializedArgs}`;
80
81
  };
81
82
  exports.generateRequestHash = generateRequestHash;
82
83
  const generateHash = (field) => {
@@ -1,5 +1,6 @@
1
1
  export * from "./helper";
2
2
  export * from "./constants";
3
+ export * from "./hash";
3
4
  export * as candid from "./candid";
4
5
  export * as principal from "./principal";
5
6
  export * as agent from "./agent";
@@ -29,6 +29,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.agent = exports.principal = exports.candid = void 0;
30
30
  __exportStar(require("./helper"), exports);
31
31
  __exportStar(require("./constants"), exports);
32
+ __exportStar(require("./hash"), exports);
32
33
  // Re-export the peerDependencies
33
34
  /// https://agent-js.icp.xyz/candid
34
35
  exports.candid = __importStar(require("./candid"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/core",
3
- "version": "1.11.0",
3
+ "version": "1.12.0",
4
4
  "description": "A library for intracting with the Internet Computer canisters",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -56,5 +56,5 @@
56
56
  "engines": {
57
57
  "node": ">=10"
58
58
  },
59
- "gitHead": "03b85c107e062dc52e4feee4864ed78db5a97294"
59
+ "gitHead": "8f8b4be20c0c89a58110e782f0d613cef9e7a04f"
60
60
  }