All files / src/metadata text.ts

96.66% Statements 58/60
66.66% Branches 2/3
50% Functions 1/2
96.66% Lines 58/60

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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 611x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 288x 288x 288x 288x 288x 65484x 65484x 288x 288x 288x 1x 1x 1x 1x 1x 1x 1x     1x  
import { MerkleTree, Field } from "o1js";
export { Text, TEXT_TREE_HEIGHT };
 
/**
 * The default height of the Merkle tree used to represent the text data.
 */
const TEXT_TREE_HEIGHT = 20;
 
/**
 * The `Text` class represents textual data in the form of a Merkle tree. Each character of the text is converted to its
 * ASCII code and stored as a leaf in the Merkle tree. The root of the tree can be used as a compact representation
 * of the text data in cryptographic proofs.
 */
class Text {
  /**
   * The length of the text.
   */
  readonly size: number;
  /**
   * The original text string.
   */
  readonly text: string;
  /**
   * The root of the Merkle tree representing the text data.
   */
  readonly root: Field;
  /**
   * The height of the Merkle tree.
   */
  readonly height: number;
 
  /**
   * Constructs a new `Text` instance by creating a Merkle tree from the given text string.
   * Each character in the text is converted to its ASCII code and stored as a leaf in the tree.
   *
   * @param text - The text string to be represented.
   * @param height - The height of the Merkle tree. Defaults to `TEXT_TREE_HEIGHT`.
   * @throws Will throw an error if the text length exceeds the number of leaves in the Merkle tree.
   */
  constructor(text: string, height: number = TEXT_TREE_HEIGHT) {
    this.text = text;
    this.size = text.length;
    const tree = new MerkleTree(height);
    if (this.size > tree.leafCount) throw new Error(`Text is too long`);
    for (let i = 0; i < this.size; i++) {
      tree.setLeaf(BigInt(i), Field.from(this.text.charCodeAt(i)));
    }
    this.root = tree.getRoot();
    this.height = height;
  }
 
  /**
   * Returns the original text string.
   *
   * @returns The text string.
   */
  public toString(): string {
    return this.text;
  }
}