diff --git a/bun.lockb b/bun.lockb index 12d2ffb..c40c716 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/libs/as-sdk-integration-tests/assembly/index.ts b/libs/as-sdk-integration-tests/assembly/index.ts index b456a0c..80d09f4 100644 --- a/libs/as-sdk-integration-tests/assembly/index.ts +++ b/libs/as-sdk-integration-tests/assembly/index.ts @@ -32,6 +32,7 @@ import { TestHttpSuccess, TestPostHttpSuccess, } from "./http"; +import { TestInvalidAttribute } from "./invalid-json"; import { TestGenerateProxyMessage, TestProxyHttpFetch } from "./proxy-http"; import { TestTallyVmReveals, TestTallyVmRevealsFiltered } from "./tally"; import { TestTallyVmHttp, TestTallyVmMode } from "./vm-tests"; @@ -97,6 +98,8 @@ if (args === "testHttpRejection") { new TestLogNull().run(); } else if (args === "testLogFloat") { new TestLogFloat().run(); +} else if (args === "testInvalidAttribute") { + new TestInvalidAttribute().run(); } Process.error(Bytes.fromUtf8String("No argument given")); diff --git a/libs/as-sdk-integration-tests/assembly/invalid-json.ts b/libs/as-sdk-integration-tests/assembly/invalid-json.ts new file mode 100644 index 0000000..0d002ab --- /dev/null +++ b/libs/as-sdk-integration-tests/assembly/invalid-json.ts @@ -0,0 +1,15 @@ +import { Bytes, OracleProgram, Process } from "../../as-sdk/assembly"; + +@json +class InvalidAttribute { + value!: string; +} + +export class TestInvalidAttribute extends OracleProgram { + execution(): void { + const result = + Bytes.fromUtf8String(`{"value":0}`).toJSON(); + + Process.success(Bytes.fromJSON(result)); + } +} diff --git a/libs/as-sdk-integration-tests/src/invalid-json.test.ts b/libs/as-sdk-integration-tests/src/invalid-json.test.ts new file mode 100644 index 0000000..54f50d3 --- /dev/null +++ b/libs/as-sdk-integration-tests/src/invalid-json.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from "bun:test"; +import { readFile } from "node:fs/promises"; +import { testOracleProgramExecution } from "@seda/dev-tools"; + +const oracleProgram = await readFile( + "dist/libs/as-sdk-integration-tests/debug.wasm", +); + +describe("invalid json", () => { + describe("invalid attribute", () => { + it("should mention the mismatched type", async () => { + const result = await testOracleProgramExecution( + oracleProgram, + Buffer.from("testInvalidAttribute"), + ); + + expect(result.exitCode).toBe(255); + expect(result.stderr).toContain( + "Mismatched Types! Expected string but got", + ); + }); + }); +}); diff --git a/libs/as-sdk/assembly/bytes.ts b/libs/as-sdk/assembly/bytes.ts index 2e26d6d..03c5baf 100644 --- a/libs/as-sdk/assembly/bytes.ts +++ b/libs/as-sdk/assembly/bytes.ts @@ -71,6 +71,23 @@ export class Bytes { return true; } + /** + * @hidden Required for JSON-AS (de)serialization. + */ + __DESERIALIZE_SAFE( + data: string, + _key_start: i32, + _key_end: i32, + _outerLoopIndex: i32, + _numberValueIndex: i32, + ): bool { + const innerBytes = JSON.parseSafe(data); + const buffer = decodeHex(innerBytes.value); + this.value = buffer; + + return true; + } + /** * The underlying {@link ArrayBuffer} in case you need to manipulate it directly. * Most likely {@linkcode toUtf8String} or {@linkcode toHexString} is the better choice. @@ -288,7 +305,7 @@ export class Bytes { * @returns an instance of '@json' annotated class */ toJSON(): T { - return JSON.parse(this.toUtf8String()); + return JSON.parseSafe(this.toUtf8String()); } /** diff --git a/libs/as-sdk/package.json b/libs/as-sdk/package.json index f42c6d7..e566445 100644 --- a/libs/as-sdk/package.json +++ b/libs/as-sdk/package.json @@ -1,12 +1,12 @@ { "name": "@seda-protocol/as-sdk", "type": "module", - "version": "0.0.15", + "version": "0.0.16", "types": "./assembly/index.ts", "dependencies": { "@assemblyscript/wasi-shim": "git+https://github.com/sedaprotocol/wasi-shim.git", "as-bignum": "^0.3.1", - "json-as": "^0.9.6", + "json-as": "^0.9.26", "visitor-as": "^0.11.4" } } diff --git a/package.json b/package.json index 73b253e..dc68555 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "cosmjs-types": "^0.9.0", "dotenv": "^16.3.1", "figlet": "^1.6.0", - "json-as": "0.9.6", + "json-as": "0.9.26", "node-fetch": "^3.3.2", "node-gzip": "^1.1.2", "ora": "^7.0.1",