All files / src/contracts encoding.ts

75% Statements 6/8
0% Branches 0/1
50% Functions 1/2
83.33% Lines 5/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33        6x 6x                                             776x 776x 776x    
/**
 * Utilities for encoding and decoding `Field` elements to and from strings.
 * These functions are used throughout the NFT standard for handling off-chain storage references and metadata.
 */
import { Encoding, Field } from "o1js";
export { fieldToString, fieldFromString };
 
/**
 * Converts a `Field` element to a string representation.
 * This is used for serializing `Field` values into strings suitable for storage or transmission.
 *
 * @param {Field} field - The `Field` element to convert.
 * @returns {string} The string representation of the `Field`.
 */
function fieldToString(field: Field): string {
  return Encoding.stringFromFields([field]);
}
 
/**
 * Reconstructs a `Field` element from its string representation.
 * This function is essential for deserializing strings back into `Field` elements,
 * which can then be used within the smart contract logic.
 *
 * @param {string} storage - The string representation of the `Field`.
 * @returns {Field} The reconstructed `Field` element.
 * @throws Will throw an error if the input string does not correspond to exactly one `Field`.
 */
function fieldFromString(storage: string): Field {
  const fields = Encoding.stringToFields(storage);
  Iif (fields.length !== 1) throw new Error("String is too long");
  return fields[0];
}