Repository: ProvableHQ/sdk (language: rust)

Issue:
[Bug] Block data parsing loses precision and typing of bigInts
## 🐛 Bug Report

Using `response.json()` as a parser for results fetched from Aleo nodes removes precision from bigInts and transforms them into strings (leading to users needing to handle casting and handling them correctly).

## Steps to Reproduce

get a block containing a solution generated by a prover i.e block 4844 on testnet-beta, and check the values returned in the `solutions` part of the block, the result is that the `counter` element in the `partial_solutions` has lost the last 4 digits in precision and is now unexpectedly a string.

#### Code snippet to reproduce

```
import { AleoNetworkClient } from "sdk"
const networkClient = new AleoNetworkClient(ALEO_NODE_ENDPOINT)

const block = await networkClient.getBlock(4844)
```
Check the results of the blocks and verify wrong types: `counter: "4175780142384957400"` but it should be `counter: 4175780142384957663` (as a bigInt)

#### Stack trace & error message

no error message to be shown

## Expected Behavior

Expected behaviour is for the blocks to be returned with the right precision and having the correct typing such as bigInt allowing clients using the SDK.

## Your Environment

- node 20 alpine



Discussion / hints:
Proposed fix
Use [json-bigint](https://www.npmjs.com/package/json-bigint) to parse the response data instead of using `response.json()` in `fetchData`.
No PR for this atm as I do not have the full understanding of the impact of this throughout the whole SDK, however it should address the data issue. 
@WietzeSlagman  Thanks for noting this. Would you be interested in making the PR to fix it? 

I'll ensure the SDK maintainers are aware of this.
> @WietzeSlagman Thanks for noting this. Would you be interested in making the PR to fix it?
> 
> I'll ensure the SDK maintainers are aware of this.

Yes i can make a PR with a fix for this, i'll have something ready within a day or 2
PR opened: #901 

Fix patch:
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<Type> {
     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<number> {
     try {
-      return await this.fetchData<number>("/latest/height");
+      return Number(await this.fetchData<bigint>("/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"


Test patch: