From 30f8b0835ad8f57850bb650345688a2f71065800 Mon Sep 17 00:00:00 2001 From: Oleksandr Myshchyshyn Date: Wed, 18 Dec 2024 19:58:35 +0200 Subject: [PATCH] Added toJSON method for each CLValue representation --- src/types/clvalue/Any.ts | 9 +++++++++ src/types/clvalue/Bool.ts | 9 +++++++++ src/types/clvalue/ByteArray.ts | 9 +++++++++ src/types/clvalue/CLValue.ts | 5 +++++ src/types/clvalue/Int32.ts | 9 +++++++++ src/types/clvalue/Int64.ts | 9 +++++++++ src/types/clvalue/List.ts | 9 ++++++--- src/types/clvalue/Map.ts | 19 +++++++++++++++++++ src/types/clvalue/Option.ts | 12 ++++++++++++ src/types/clvalue/Result.ts | 11 +++++++++++ src/types/clvalue/String.ts | 9 +++++++++ src/types/clvalue/Tuple1.ts | 9 +++++++++ src/types/clvalue/Tuple2.ts | 9 +++++++++ src/types/clvalue/Tuple3.ts | 9 +++++++++ src/types/clvalue/Uint128.ts | 9 +++++++++ src/types/clvalue/Uint256.ts | 9 +++++++++ src/types/clvalue/Uint32.ts | 9 +++++++++ src/types/clvalue/Uint64.ts | 9 +++++++++ src/types/clvalue/Uint8.ts | 9 +++++++++ src/types/clvalue/Unit.ts | 9 +++++++++ src/types/key/Key.ts | 29 +++++++++++++++++++++-------- 21 files changed, 209 insertions(+), 11 deletions(-) diff --git a/src/types/clvalue/Any.ts b/src/types/clvalue/Any.ts index 1676914d..916972a7 100644 --- a/src/types/clvalue/Any.ts +++ b/src/types/clvalue/Any.ts @@ -33,6 +33,15 @@ export class CLValueAny { return new TextDecoder().decode(this.data); } + /** + * Converts the instance to a JSON-compatible byte array. + * + * @returns {Uint8Array} The byte representation of the instance. + */ + public toJSON(): Uint8Array { + return this.bytes(); + } + /** * Creates a new CLValue instance containing an 'Any' value. * @param data - The Uint8Array to be stored within the CLValue. diff --git a/src/types/clvalue/Bool.ts b/src/types/clvalue/Bool.ts index da03a5f8..2a58d96b 100644 --- a/src/types/clvalue/Bool.ts +++ b/src/types/clvalue/Bool.ts @@ -32,6 +32,15 @@ export class CLValueBool { return this.value ? 'true' : 'false'; } + /** + * Converts the instance to a JSON boolean. + * + * @returns {boolean} The boolean value of the instance. + */ + public toJSON(): boolean { + return this.value; + } + /** * Retrieves the boolean value. * @returns The stored boolean value. diff --git a/src/types/clvalue/ByteArray.ts b/src/types/clvalue/ByteArray.ts index b0731e10..4c510969 100644 --- a/src/types/clvalue/ByteArray.ts +++ b/src/types/clvalue/ByteArray.ts @@ -24,6 +24,15 @@ export class CLValueByteArray { return this.data; } + /** + * Converts the instance to a JSON-compatible byte array. + * + * @returns {Uint8Array} The byte representation of the instance. + */ + public toJSON(): Uint8Array { + return this.bytes(); + } + /** * Provides a hexadecimal string representation of the byte array. * Each byte is represented by two hexadecimal digits. diff --git a/src/types/clvalue/CLValue.ts b/src/types/clvalue/CLValue.ts index 4f2e745f..a48a0a7e 100644 --- a/src/types/clvalue/CLValue.ts +++ b/src/types/clvalue/CLValue.ts @@ -35,6 +35,7 @@ import { CLValueAny } from './Any'; interface IValue { bytes(): Uint8Array; toString(): string; + toJSON(): any; } export interface IResultWithBytes { @@ -96,6 +97,10 @@ export class CLValue { return this.getValueByType().toString(); } + public toJSON(): any { + return this.getValueByType().toJSON(); + } + /** * Converts the CLValue to its byte representation. * @returns A Uint8Array representing the bytes of the value. diff --git a/src/types/clvalue/Int32.ts b/src/types/clvalue/Int32.ts index ba94be46..d705d060 100644 --- a/src/types/clvalue/Int32.ts +++ b/src/types/clvalue/Int32.ts @@ -35,6 +35,15 @@ export class CLValueInt32 { return this.value.toString(); } + /** + * Converts the instance to a JSON-compatible string. + * + * @returns {string} The string representation of the instance. + */ + public toJSON(): string { + return this.toString(); + } + /** * Retrieves the numeric value of the Int32. * @returns The numeric representation of the value. diff --git a/src/types/clvalue/Int64.ts b/src/types/clvalue/Int64.ts index 2b9a6149..bbccbbe3 100644 --- a/src/types/clvalue/Int64.ts +++ b/src/types/clvalue/Int64.ts @@ -35,6 +35,15 @@ export class CLValueInt64 { return this.value.toString(); } + /** + * Converts the instance to a JSON-compatible string. + * + * @returns {string} The string representation of the instance. + */ + public toJSON(): string { + return this.toString(); + } + /** * Retrieves the bigint value of the Int64. * @returns The bigint representation of the stored value. diff --git a/src/types/clvalue/List.ts b/src/types/clvalue/List.ts index 0f86b516..f582f09c 100644 --- a/src/types/clvalue/List.ts +++ b/src/types/clvalue/List.ts @@ -120,8 +120,8 @@ export class CLValueList { * Converts the list to a JSON-compatible representation. * @returns An array of string representations of the list elements. */ - public toJSON(): any { - return this.elements.map(d => d.toString()); + public toJSON(): string[] { + return this.elements.map(d => d.toJSON()); } /** @@ -130,7 +130,10 @@ export class CLValueList { * @param elements - Optional array of CLValues to initialize the list with. * @returns A new CLValue instance containing CLTypeList and a CLValueList. */ - public static newCLList(elementType: CLType, elements: CLValue[] = []): CLValue { + public static newCLList( + elementType: CLType, + elements: CLValue[] = [] + ): CLValue { const listType = new CLTypeList(elementType); const clValue = new CLValue(listType); clValue.list = new CLValueList(listType, elements); diff --git a/src/types/clvalue/Map.ts b/src/types/clvalue/Map.ts index f86e2113..3f25ecf0 100644 --- a/src/types/clvalue/Map.ts +++ b/src/types/clvalue/Map.ts @@ -89,6 +89,25 @@ export class CLValueMap { return b.join(''); } + /** + * Converts the instance to a JSON-compatible map. + * + * @returns {any} A Map object representing the instance's key-value pairs. + * + * This method iterates over the `data` property, extracting key-value + * pairs from each tuple and storing them in a new Map. + */ + public toJSON(): any { + const map = new Map(); + + this.data.forEach(tuple2 => { + const [k, v] = tuple2.value(); + map.set(k, v); + }); + + return map; + } + /** * Finds a value in the map by key. * @param key - The key to search for. diff --git a/src/types/clvalue/Option.ts b/src/types/clvalue/Option.ts index f5eb75b7..db9e87a5 100644 --- a/src/types/clvalue/Option.ts +++ b/src/types/clvalue/Option.ts @@ -45,6 +45,18 @@ export class CLValueOption { return this.isEmpty() ? '' : this.inner!.toString(); } + /** + * Converts the instance to a JSON-compatible format. + * + * @returns {any} The JSON representation of the inner value or `null` if empty. + * + * If the instance is empty, it returns `null`. Otherwise, it calls `toJSON()` + * on the inner value to produce its JSON representation. + */ + public toJSON(): any { + return this.isEmpty() ? null : this.inner!.toJSON(); + } + /** * Checks if the option is empty. * @returns true if the option is empty, false otherwise. diff --git a/src/types/clvalue/Result.ts b/src/types/clvalue/Result.ts index 1e308217..7796e542 100644 --- a/src/types/clvalue/Result.ts +++ b/src/types/clvalue/Result.ts @@ -47,6 +47,17 @@ export class CLValueResult { : `Err(${this.inner.toString()})`; } + /** + * Converts the instance to a JSON-compatible format. + * + * @returns {any} The JSON representation of the inner value. + * + * Calls `toJSON()` on the inner value to produce its JSON representation. + */ + public toJSON(): any { + return this.inner.toJSON(); + } + /** * Retrieves the inner CLValue of the Result. * @returns The CLValue contained within the Result. diff --git a/src/types/clvalue/String.ts b/src/types/clvalue/String.ts index 50748864..e2f417bc 100644 --- a/src/types/clvalue/String.ts +++ b/src/types/clvalue/String.ts @@ -42,6 +42,15 @@ export class CLValueString { return new Uint8Array(buffer); } + /** + * Converts the instance to a JSON-compatible string. + * + * @returns {string} The string representation of the instance. + */ + public toJSON(): string { + return this.toString(); + } + /** * Provides the string value. * @returns The string value. diff --git a/src/types/clvalue/Tuple1.ts b/src/types/clvalue/Tuple1.ts index 10c9730f..9f8c5644 100644 --- a/src/types/clvalue/Tuple1.ts +++ b/src/types/clvalue/Tuple1.ts @@ -43,6 +43,15 @@ export class CLValueTuple1 { return this.innerVal; } + /** + * Converts the instance to a JSON-compatible array. + * + * @returns {any} An array containing the JSON representation of the inner value. + */ + public toJSON(): any[] { + return [this.innerVal.toJSON()]; + } + /** * Creates a new CLValue instance with a Tuple1 value. * @param val - The CLValue to be contained in the tuple. diff --git a/src/types/clvalue/Tuple2.ts b/src/types/clvalue/Tuple2.ts index a7aefc73..b8d4942d 100644 --- a/src/types/clvalue/Tuple2.ts +++ b/src/types/clvalue/Tuple2.ts @@ -42,6 +42,15 @@ export class CLValueTuple2 { return `(${this.inner1.toString()}, ${this.inner2.toString()})`; } + /** + * Converts the instance to a JSON-compatible array. + * + * @returns {any[]} An array containing the JSON representations of inner1 and inner2. + */ + public toJSON(): any[] { + return [this.inner1.toJSON(), this.inner2.toJSON()]; + } + /** * Retrieves the values of the tuple as an array. * @returns An array containing the two CLValues of the tuple. diff --git a/src/types/clvalue/Tuple3.ts b/src/types/clvalue/Tuple3.ts index df03d676..259d346e 100644 --- a/src/types/clvalue/Tuple3.ts +++ b/src/types/clvalue/Tuple3.ts @@ -50,6 +50,15 @@ export class CLValueTuple3 { return `(${this.inner1.toString()}, ${this.inner2.toString()}, ${this.inner3.toString()})`; } + /** + * Converts the instance to a JSON-compatible array. + * + * @returns {any[]} An array containing the JSON representations of inner1, inner2, and inner3. + */ + public toJSON(): any[] { + return [this.inner1.toJSON(), this.inner2.toJSON(), this.inner3.toJSON()]; + } + /** * Retrieves the values of the tuple as an array. * @returns An array containing the three CLValues of the tuple. diff --git a/src/types/clvalue/Uint128.ts b/src/types/clvalue/Uint128.ts index dd755731..a05f801b 100644 --- a/src/types/clvalue/Uint128.ts +++ b/src/types/clvalue/Uint128.ts @@ -43,6 +43,15 @@ export class CLValueUInt128 { return this.val; } + /** + * Converts the instance to a JSON-compatible string. + * + * @returns {string} The string representation of the instance. + */ + public toJSON(): string { + return this.toString(); + } + /** * Creates a new CLValue instance with a UInt128 value. * @param val - The value to initialize the UInt128 with. Can be a BigNumber or a string. diff --git a/src/types/clvalue/Uint256.ts b/src/types/clvalue/Uint256.ts index 990b46fc..430a4c81 100644 --- a/src/types/clvalue/Uint256.ts +++ b/src/types/clvalue/Uint256.ts @@ -54,6 +54,15 @@ export class CLValueUInt256 { return res; } + /** + * Converts the instance to a JSON-compatible string. + * + * @returns {string} The string representation of the instance. + */ + public toJSON(): string { + return this.toString(); + } + /** * Creates a CLValueUInt256 instance from a Uint8Array. * Parses the byte array to retrieve the UInt256 value. diff --git a/src/types/clvalue/Uint32.ts b/src/types/clvalue/Uint32.ts index a41ddc7b..2c6b4834 100644 --- a/src/types/clvalue/Uint32.ts +++ b/src/types/clvalue/Uint32.ts @@ -34,6 +34,15 @@ export class CLValueUInt32 { return this.value.toString(); } + /** + * Converts the instance to a JSON-compatible string. + * + * @returns {string} The string representation of the instance. + */ + public toJSON(): string { + return this.toString(); + } + /** * Retrieves the numeric value of the UInt32. * @returns The BigNumber representation of the value. diff --git a/src/types/clvalue/Uint64.ts b/src/types/clvalue/Uint64.ts index d3b8461f..e098bad7 100644 --- a/src/types/clvalue/Uint64.ts +++ b/src/types/clvalue/Uint64.ts @@ -41,6 +41,15 @@ export class CLValueUInt64 { return this.value; } + /** + * Converts the instance to a JSON-compatible string. + * + * @returns {string} The string representation of the instance. + */ + public toJSON(): string { + return this.toString(); + } + /** * Creates a new CLValue instance with a UInt64 value. * @param val - The value to initialize the UInt64 with. Can be any BigNumberish type. diff --git a/src/types/clvalue/Uint8.ts b/src/types/clvalue/Uint8.ts index 10ba0928..633f8c2b 100644 --- a/src/types/clvalue/Uint8.ts +++ b/src/types/clvalue/Uint8.ts @@ -32,6 +32,15 @@ export class CLValueUInt8 { return this.value.toString(); } + /** + * Converts the instance to a JSON-compatible string. + * + * @returns {string} The string representation of the instance. + */ + public toJSON(): string { + return this.toString(); + } + /** * Retrieves the number value of the UInt8. * @returns The number representation of the value. diff --git a/src/types/clvalue/Unit.ts b/src/types/clvalue/Unit.ts index c97c3d4d..f6596e17 100644 --- a/src/types/clvalue/Unit.ts +++ b/src/types/clvalue/Unit.ts @@ -54,6 +54,15 @@ export class CLValueUnit { return res; } + /** + * Converts the instance to a JSON-compatible null value. + * + * @returns {null} Always returns `null`, representing the absence of a value. + */ + public toJSON(): null { + return this.getValue(); + } + /** * Creates a CLValueUnit instance from a byte array. * diff --git a/src/types/key/Key.ts b/src/types/key/Key.ts index cf0b6416..e2bda3ba 100644 --- a/src/types/key/Key.ts +++ b/src/types/key/Key.ts @@ -312,8 +312,8 @@ export class Key { * Converts the key to bytes. * @returns A Uint8Array representing the serialized key. */ - bytes(): Uint8Array { - const typeBytes = new Uint8Array([this.type]); + bytes(withKeyTypeID = true): Uint8Array { + const typeBytes = withKeyTypeID ? Uint8Array.from([this.type]) : undefined; switch (this.type) { case KeyTypeID.Balance: @@ -379,13 +379,26 @@ export class Key { * @returns A Uint8Array with concatenated type and field bytes. */ private static concatBytes( - typeBytes: Uint8Array, - fieldBytes: Uint8Array = Uint8Array.from([]) + fieldBytes: Uint8Array = Uint8Array.from([]), + typeBytes?: Uint8Array ): Uint8Array { - const result = new Uint8Array(typeBytes.length + fieldBytes.length); - result.set(typeBytes); - result.set(fieldBytes, typeBytes.length); - return result; + if (typeBytes) { + const result = new Uint8Array(typeBytes.length + fieldBytes.length); + result.set(typeBytes); + result.set(fieldBytes, typeBytes.length); + return result; + } + + return fieldBytes; + } + + /** + * Converts the instance to a JSON-compatible hexadecimal string. + * + * @returns {string} The hex-encoded string representation of the instance. + */ + public toJSON() { + return Buffer.from(this.bytes(false)).toString('hex'); } /**