Skip to content

Commit

Permalink
Merge pull request #470 from casper-ecosystem/clvalue-serialization
Browse files Browse the repository at this point in the history
Added toJSON method for each CLValue representation
  • Loading branch information
alexmyshchyshyn authored Dec 19, 2024
2 parents e95d8a2 + 30f8b08 commit 3bb045f
Show file tree
Hide file tree
Showing 21 changed files with 205 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/types/clvalue/Any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/types/clvalue/Bool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/types/clvalue/ByteArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions src/types/clvalue/CLValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { CLValueAny } from './Any';
interface IValue {
bytes(): Uint8Array;
toString(): string;
toJSON(): any;
}

export interface IResultWithBytes<T> {
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/types/clvalue/Int32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/types/clvalue/Int64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/types/clvalue/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/types/clvalue/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions src/types/clvalue/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions src/types/clvalue/Result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/types/clvalue/String.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/types/clvalue/Tuple1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/types/clvalue/Tuple2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/types/clvalue/Tuple3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/types/clvalue/Uint128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/types/clvalue/Uint256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/types/clvalue/Uint32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/types/clvalue/Uint64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/types/clvalue/Uint8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/types/clvalue/Unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
29 changes: 21 additions & 8 deletions src/types/key/Key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,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:
Expand Down Expand Up @@ -375,13 +375,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');
}

/**
Expand Down

0 comments on commit 3bb045f

Please sign in to comment.