Skip to content

Commit

Permalink
Merge pull request #925 from Pauan/feat/bigint
Browse files Browse the repository at this point in the history
Changing JSON parsing to use bigint for all integers
  • Loading branch information
jaketarnow authored Sep 23, 2024
2 parents 4f4dc8c + e00673d commit 0ec911f
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 13 deletions.
1 change: 1 addition & 0 deletions sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "./polyfill/shared";
import {VerifyingKey, Metadata} from "@provablehq/wasm";

const KEY_STORE = Metadata.baseUrl();
Expand Down
17 changes: 10 additions & 7 deletions sdk/src/network-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get, post } from "./utils";
import { get, post, parseJSON } from "./utils";
import {
Account,
Block,
Expand Down Expand Up @@ -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.");
}
Expand Down Expand Up @@ -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.");
}
Expand Down Expand Up @@ -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}`);
Expand Down
1 change: 1 addition & 0 deletions sdk/src/node-polyfill.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "./polyfill/shared";
import "./polyfill/crypto";
import "./polyfill/fetch";
import "./polyfill/xmlhttprequest";
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/polyfill/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// These polyfills are shared by everything, both the browser and Node
import "core-js/proposals/json-parse-with-source.js";
8 changes: 2 additions & 6 deletions sdk/src/record-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions sdk/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
1 change: 1 addition & 0 deletions sdk/src/worker.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 0ec911f

Please sign in to comment.