diff --git a/CHANGELOG.md b/CHANGELOG.md index 1de2789..fe5f9c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,10 +15,13 @@ This version is compatible with [`960f8d139940fa0814d3fac44da9a2975642f5d3`](htt - Exposed `run_chia_program` - Exposed `run_clvm` - Exposed `LazyNode` + - Added `toJSON()` method to `LazyNode` (This is not implemented in `clvm_wasm`) - Exposed `Flag` - Exposed `serialized_length` - Exposed `node_from_bytes` - Added `asUint8Array()` method to `Stream` +- Added `toJSON()` method to `Bytes` +- Added `toJSON()` method to `SExp` - Added tests ## [2.0.1] diff --git a/src/SExp.ts b/src/SExp.ts index df30afe..69b8319 100644 --- a/src/SExp.ts +++ b/src/SExp.ts @@ -321,6 +321,16 @@ export class SExp implements CLVMType { return this.as_bin().hex(); } + public toJSON(){ + if(this.pair){ + return this.pair; + } + if(this.atom){ + return this.atom.hex(); + } + throw new EvalError("Invalid object", this); + } + public __repr__(){ return `SExp(${this.as_bin().hex()})`; } diff --git a/src/__clvm_wasm__.ts b/src/__clvm_wasm__.ts index 899d06c..5efc501 100644 --- a/src/__clvm_wasm__.ts +++ b/src/__clvm_wasm__.ts @@ -11,7 +11,9 @@ * 4. Annotate typings, fix lint issues * 5. Paste loader code preserved in the previous procedure * 6. Add `__wb*` functions to the `imports` object. + * 7. Add `toJSON()` method to `LazyNode`. */ +import {Word32Array} from "jscrypto/Word32Array"; type ClvmWasmExports = { memory: WebAssembly.Memory; @@ -397,6 +399,16 @@ export class LazyNode { wasm.__wbindgen_add_to_stack_pointer(16); } } + + toJSON() { + if(this.pair){ + return this.pair; + } + if(this.atom){ + return (new Word32Array(this.atom)).toString(); + } + throw new Error("Invalid object"); + } } export function __wbg_lazynode_new(arg0: number) { diff --git a/src/__type_compatibility__.ts b/src/__type_compatibility__.ts index 85ce460..f4813ac 100644 --- a/src/__type_compatibility__.ts +++ b/src/__type_compatibility__.ts @@ -287,6 +287,10 @@ export class Bytes { return 0; } + + public toJSON(){ + return this.hex(); + } } export function b(utf8Str: string, type:"utf8"|"hex" = "utf8"){