Skip to content

Commit

Permalink
Add SBOR encoding of programmatic JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Sep 12, 2023
1 parent 3ac29e3 commit cfbbbc1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@radixdlt/radix-engine-toolkit",
"version": "0.4.1",
"version": "0.4.2",
"description": "A TypeScript wrapper for the Radix Engine Toolkit that provides many of the necessary tools to interact with the Radix ledger",
"types": "./dist/index.d.ts",
"main": "./dist/radix-engine-toolkit.umd.js",
Expand Down
Binary file modified resources/radix_engine_toolkit.wasm
Binary file not shown.
10 changes: 10 additions & 0 deletions src/generated/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ export type NotarizedTransactionDecompileOutput =

export type ScryptoSborDecodeToStringOutput = string;

export type SerializableScryptoSborStringRepresentation = {
kind: "ProgrammaticJson";
value: string;
};

export type ScryptoSborEncodeStringRepresentationInput =
SerializableScryptoSborStringRepresentation;

export type ScryptoSborEncodeStringRepresentationOutput = SerializableBytes;

export type SignedIntentHashInput = SerializableSignedIntent;

export type SignedIntentHashOutput = SerializableTransactionHash;
Expand Down
14 changes: 11 additions & 3 deletions src/wasm/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,16 @@ export class RadixEngineToolkit {
});
return output;
}

static async encodeProgrammaticJson(object: any): Promise<Uint8Array> {
const encoded = JSON.stringify(object);
const rawRet = await rawRadixEngineToolkit;
const output = rawRet.scryptoSborEncodeStringRepresentation({
kind: "ProgrammaticJson",
value: encoded,
});
return Convert.HexString.toUint8Array(output);
}
};

static Address = class {
Expand All @@ -575,9 +585,7 @@ export class RadixEngineToolkit {
return GeneratedConverter.EntityType.fromGenerated(output);
}

static async decode(
address: string
): Promise<{
static async decode(address: string): Promise<{
networkId: number;
entityType: EntityType;
hrp: String;
Expand Down
12 changes: 12 additions & 0 deletions src/wasm/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ import {
NotarizedTransactionStaticallyValidateOutput,
ScryptoSborDecodeToStringInput,
ScryptoSborDecodeToStringOutput,
ScryptoSborEncodeStringRepresentationInput,
ScryptoSborEncodeStringRepresentationOutput,
SignedIntentCompileInput,
SignedIntentCompileOutput,
SignedIntentDecompileInput,
Expand Down Expand Up @@ -330,6 +332,15 @@ export class RawRadixEngineToolkit extends Host<Exports> {
return this.callFunction(input, this.exports.scrypto_sbor_decode_to_string);
}

public scryptoSborEncodeStringRepresentation(
input: ScryptoSborEncodeStringRepresentationInput
): ScryptoSborEncodeStringRepresentationOutput {
return this.callFunction(
input,
this.exports.scrypto_sbor_encode_string_representation
);
}

public utilsKnownAddresses(
input: UtilsKnownAddressesInput
): UtilsKnownAddressesOutput {
Expand Down Expand Up @@ -459,6 +470,7 @@ interface Exports {
/* SBOR Modules */
manifest_sbor_decode_to_string(pointer: number): number;
scrypto_sbor_decode_to_string(pointer: number): number;
scrypto_sbor_encode_string_representation(pointer: number): number;

/* Utils Module */
utils_known_addresses(pointer: number): number;
Expand Down
17 changes: 17 additions & 0 deletions tests/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,23 @@ describe("Default Radix Engine Toolkit Tests", () => {
// Assert
expect(staticValidationResult.kind).toEqual("Valid");
});

it("Programmatic JSON can be encoded through the toolkit", async () => {
// Arrange
const programmaticJson = {
kind: "Reference",
value:
"resource_rdx1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxradxrd",
};

// Act
const encoded = await RadixEngineToolkit.ScryptoSbor.encodeProgrammaticJson(
programmaticJson
);

// Assert
expect(encoded[0]).toEqual(92); /* Scrypto SBOR prefix */
});
});

const moduleTestVector = <I, O>(
Expand Down

0 comments on commit cfbbbc1

Please sign in to comment.