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/network-client.ts b/sdk/src/network-client.ts index 177ee0294..72a76af90 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."); } @@ -390,7 +392,7 @@ class AleoNetworkClient { */ 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."); } @@ -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/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..7fd8cd812 --- /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.js"; diff --git a/sdk/src/record-provider.ts b/sdk/src/record-provider.ts index f3d025ec8..a59df7108 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 = end; } // If the start height is greater than the end height, throw an error 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); 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"