From 7bf82a462d9ac8f85b4c3fa068dbebc1a8ab12de Mon Sep 17 00:00:00 2001 From: Pauan Date: Sat, 7 Sep 2024 15:47:42 -0700 Subject: [PATCH 1/5] Changing JSON parsing to use bigint for all integers --- sdk/src/network-client.ts | 15 +++++++++------ sdk/src/utils.ts | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/sdk/src/network-client.ts b/sdk/src/network-client.ts index 177ee0294..9af79afb1 100644 --- a/sdk/src/network-client.ts +++ b/sdk/src/network-client.ts @@ -1,4 +1,4 @@ -import { get, post } from "./utils"; +import { get, post, parseJSON } from "./utils"; import { Account, Block, @@ -84,11 +84,13 @@ class AleoNetworkClient { url = "/", ): Promise { try { - const response = await get(this.host + url, { - headers: this.headers - }); + const response = await get(this.host + url, { + headers: this.headers + }); + + const text = await response.text(); + return parseJSON(text); - return await response.json(); } catch (error) { throw new Error("Error fetching data."); } @@ -648,7 +650,8 @@ class AleoNetworkClient { }); try { - return await response.json(); + const text = await response.text(); + return parseJSON(text); } catch (error: any) { throw new Error(`Error posting transaction. Aleo network response: ${error.message}`); diff --git a/sdk/src/utils.ts b/sdk/src/utils.ts index b7343c326..0be9d0506 100644 --- a/sdk/src/utils.ts +++ b/sdk/src/utils.ts @@ -1,3 +1,17 @@ +export function parseJSON(json: string): any { + function revive(key: string, value: any, context: any) { + if (Number.isInteger(value)) { + return BigInt(context.source); + + } else { + return value; + } + } + + return JSON.parse(json, revive as any); +} + + export async function get(url: URL | string, options?: RequestInit) { const response = await fetch(url, options); From a1aeeb63dc828c33449b703d93d86ea7763abc8b Mon Sep 17 00:00:00 2001 From: Pauan Date: Sat, 7 Sep 2024 16:51:09 -0700 Subject: [PATCH 2/5] Fixing failing unit test --- sdk/src/network-client.ts | 4 ++-- sdk/src/record-provider.ts | 8 ++------ sdk/tests/network-client.test.ts | 2 +- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/sdk/src/network-client.ts b/sdk/src/network-client.ts index 9af79afb1..77b8992f0 100644 --- a/sdk/src/network-client.ts +++ b/sdk/src/network-client.ts @@ -390,9 +390,9 @@ class AleoNetworkClient { * @example * const latestHeight = networkClient.getLatestHeight(); */ - async getLatestHeight(): Promise { + async getLatestHeight(): Promise { try { - return await this.fetchData("/latest/height"); + return await this.fetchData("/latest/height"); } catch (error) { throw new Error("Error fetching latest height."); } diff --git a/sdk/src/record-provider.ts b/sdk/src/record-provider.ts index f3d025ec8..c56664a90 100644 --- a/sdk/src/record-provider.ts +++ b/sdk/src/record-provider.ts @@ -202,12 +202,8 @@ class NetworkRecordProvider implements RecordProvider { // If the end height is not specified, use the current block height if (endHeight == 0) { - try { - const end = await this.networkClient.getLatestHeight(); - endHeight = end; - } catch (e) { - logAndThrow("Unable to get current block height from the network") - } + const end = await this.networkClient.getLatestHeight(); + endHeight = Number(end); } // If the start height is greater than the end height, throw an error diff --git a/sdk/tests/network-client.test.ts b/sdk/tests/network-client.test.ts index 48a7814a7..287bd607a 100644 --- a/sdk/tests/network-client.test.ts +++ b/sdk/tests/network-client.test.ts @@ -100,7 +100,7 @@ describe('NodeConnection', () => { describe('getLatestHeight', () => { it('should return a number', async () => { const latestHeight = await connection.getLatestHeight(); - expect(typeof latestHeight).toBe('number'); + expect(typeof latestHeight).toBe('bigint'); }, 60000); }); From 801113533f646ea87340ddbf140011a9934b8fde Mon Sep 17 00:00:00 2001 From: Pauan Date: Wed, 18 Sep 2024 20:53:09 -0700 Subject: [PATCH 3/5] Changing getLatestHeight to return a number --- sdk/src/network-client.ts | 4 ++-- sdk/src/record-provider.ts | 2 +- sdk/tests/network-client.test.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/src/network-client.ts b/sdk/src/network-client.ts index 77b8992f0..72a76af90 100644 --- a/sdk/src/network-client.ts +++ b/sdk/src/network-client.ts @@ -390,9 +390,9 @@ class AleoNetworkClient { * @example * const latestHeight = networkClient.getLatestHeight(); */ - async getLatestHeight(): Promise { + async getLatestHeight(): Promise { try { - return await this.fetchData("/latest/height"); + return Number(await this.fetchData("/latest/height")); } catch (error) { throw new Error("Error fetching latest height."); } diff --git a/sdk/src/record-provider.ts b/sdk/src/record-provider.ts index c56664a90..a59df7108 100644 --- a/sdk/src/record-provider.ts +++ b/sdk/src/record-provider.ts @@ -203,7 +203,7 @@ class NetworkRecordProvider implements RecordProvider { // If the end height is not specified, use the current block height if (endHeight == 0) { const end = await this.networkClient.getLatestHeight(); - endHeight = Number(end); + endHeight = end; } // If the start height is greater than the end height, throw an error diff --git a/sdk/tests/network-client.test.ts b/sdk/tests/network-client.test.ts index 287bd607a..48a7814a7 100644 --- a/sdk/tests/network-client.test.ts +++ b/sdk/tests/network-client.test.ts @@ -100,7 +100,7 @@ describe('NodeConnection', () => { describe('getLatestHeight', () => { it('should return a number', async () => { const latestHeight = await connection.getLatestHeight(); - expect(typeof latestHeight).toBe('bigint'); + expect(typeof latestHeight).toBe('number'); }, 60000); }); From cf50d763ba46bbb675fd91381b9957c83f0a1bc3 Mon Sep 17 00:00:00 2001 From: Pauan Date: Thu, 19 Sep 2024 11:40:04 -0700 Subject: [PATCH 4/5] Adding in JSON.parse polyfill --- sdk/package.json | 1 + sdk/src/index.ts | 1 + sdk/src/node-polyfill.ts | 1 + sdk/src/polyfill/shared.ts | 2 ++ sdk/src/worker.ts | 1 + yarn.lock | 5 +++++ 6 files changed, 11 insertions(+) create mode 100644 sdk/src/polyfill/shared.ts diff --git a/sdk/package.json b/sdk/package.json index 3de5e2468..adb0d5938 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -55,6 +55,7 @@ "@typescript-eslint/parser": "^5.41.0", "better-docs": "^2.7.2", "clean-jsdoc-theme": "^4.1.8", + "core-js": "^3.38.1", "cpr": "^3.0.1", "eslint": "^8.26.0", "eslint-config-prettier": "^8.5.0", diff --git a/sdk/src/index.ts b/sdk/src/index.ts index d785b0aad..9db96416d 100644 --- a/sdk/src/index.ts +++ b/sdk/src/index.ts @@ -1,3 +1,4 @@ +import "./polyfill/shared"; import {VerifyingKey, Metadata} from "@provablehq/wasm"; const KEY_STORE = Metadata.baseUrl(); diff --git a/sdk/src/node-polyfill.ts b/sdk/src/node-polyfill.ts index 516099778..1f0488d3b 100644 --- a/sdk/src/node-polyfill.ts +++ b/sdk/src/node-polyfill.ts @@ -1,3 +1,4 @@ +import "./polyfill/shared"; import "./polyfill/crypto"; import "./polyfill/fetch"; import "./polyfill/xmlhttprequest"; diff --git a/sdk/src/polyfill/shared.ts b/sdk/src/polyfill/shared.ts new file mode 100644 index 000000000..2d6cb75f9 --- /dev/null +++ b/sdk/src/polyfill/shared.ts @@ -0,0 +1,2 @@ +// These polyfills are shared by everything, both the browser and Node +import "core-js/proposals/json-parse-with-source"; diff --git a/sdk/src/worker.ts b/sdk/src/worker.ts index 06be28723..862bbebc6 100644 --- a/sdk/src/worker.ts +++ b/sdk/src/worker.ts @@ -1,3 +1,4 @@ +import "./polyfill/shared"; import {initThreadPool, ProgramManager, PrivateKey, verifyFunctionExecution, FunctionKeyPair} from "./index"; import { AleoKeyProvider, AleoKeyProviderParams} from "./function-key-provider"; import { expose } from "comlink"; diff --git a/yarn.lock b/yarn.lock index 66b677c2e..df56dfb7e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4110,6 +4110,11 @@ core-js@^2.4.0: resolved "https://registry.npmjs.org/core-js/-/core-js-2.6.12.tgz" integrity sha512-Kb2wC0fvsWfQrgk8HU5lW6U/Lcs8+9aaYcy4ZFc6DDlo4nZ7n70dEgE5rtR0oG6ufKDUnrwfWL1mXR5ljDatrQ== +core-js@^3.38.1: + version "3.38.1" + resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.38.1.tgz#aa375b79a286a670388a1a363363d53677c0383e" + integrity sha512-OP35aUorbU3Zvlx7pjsFdu1rGNnD4pgw/CWoYzRY3t2EzoVT7shKHY1dlAy3f41cGIO7ZDPQimhGFTlEYkG/Hw== + core-util-is@~1.0.0: version "1.0.3" resolved "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz" From e00673dd72bd02ca693780ceba28069eb4e1191f Mon Sep 17 00:00:00 2001 From: Pauan Date: Thu, 19 Sep 2024 11:56:24 -0700 Subject: [PATCH 5/5] Fixing build error --- sdk/src/polyfill/shared.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/polyfill/shared.ts b/sdk/src/polyfill/shared.ts index 2d6cb75f9..7fd8cd812 100644 --- a/sdk/src/polyfill/shared.ts +++ b/sdk/src/polyfill/shared.ts @@ -1,2 +1,2 @@ // These polyfills are shared by everything, both the browser and Node -import "core-js/proposals/json-parse-with-source"; +import "core-js/proposals/json-parse-with-source.js";