All files / src/contract update.ts

100% Statements 36/36
100% Branches 0/0
100% Functions 10/10
100% Lines 36/36

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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167  27x 27x 27x 27x 27x   27x 27x                       175x 175x               82x                       50x                       50x 50x                 58x                                           70x 70x 70x   70x 70x 70x   70x     70x 70x     70x   70x                   10x                   59x 59x       27x                 59x 59x                       54x 54x   54x       54x 54x                  
export {
  MetadataUpdate,
  MetadataTransition,
  MetadataMap,
  MinaNFTMetadataUpdate,
  MinaNFTMetadataUpdateProof,
};
import { Field, SelfProof, ZkProgram, Struct, MerkleMap } from "o1js";
import { Metadata, MetadataWitness } from "../contract/metadata";
 
/**
 * MetadataMap is a wrapper around MerkleMap that stores Metadata
 * @property data The MerkleMap of the data
 * @property kind The MerkleMap of the kind
 */
class MetadataMap {
  data: MerkleMap;
  kind: MerkleMap;
 
  constructor() {
    this.data = new MerkleMap();
    this.kind = new MerkleMap();
  }
 
  /**
   * Calculates the root of the MerkleMap
   * @returns {@link Metadata} root of the MerkleMap
   */
  getRoot(): Metadata {
    return new Metadata({
      data: this.data.getRoot(),
      kind: this.kind.getRoot(),
    });
  }
 
  /**
   * Get value at key
   * @param key key of the data and kind requested
   * @returns {@link Metadata} value of the data and kind at key
   */
  get(key: Field): Metadata {
    return new Metadata({
      data: this.data.get(key),
      kind: this.kind.get(key),
    });
  }
 
  /**
   * Sets the data and kind at key
   * @param key key of the data and kind to set
   * @param value {@link Metadata} data and kind to set
   */
  set(key: Field, value: Metadata): void {
    this.data.set(key, value.data);
    this.kind.set(key, value.kind);
  }
 
  /**
   * Calculates the witness of the data and kind at key
   * @param key key of the data and kind, for which witness is requested
   * @returns {@link MetadataWitness} witness of the data and kind at key
   */
  getWitness(key: Field): MetadataWitness {
    return new MetadataWitness({
      data: this.data.getWitness(key),
      kind: this.kind.getWitness(key),
    });
  }
}
 
class MetadataUpdate extends Struct({
  oldRoot: Metadata,
  newRoot: Metadata,
  key: Field,
  oldValue: Metadata,
  newValue: Metadata,
  witness: MetadataWitness,
}) {}
 
class MetadataTransition extends Struct({
  oldRoot: Metadata,
  newRoot: Metadata,
}) {
  static create(update: MetadataUpdate) {
    const [dataWitnessRootBefore, dataWitnessKey] =
      update.witness.data.computeRootAndKey(update.oldValue.data);
    update.oldRoot.data.assertEquals(dataWitnessRootBefore);
    dataWitnessKey.assertEquals(update.key);
    const [kindWitnessRootBefore, kindWitnessKey] =
      update.witness.kind.computeRootAndKey(update.oldValue.kind);
    update.oldRoot.kind.assertEquals(kindWitnessRootBefore);
    kindWitnessKey.assertEquals(update.key);
 
    const [dataWitnessRootAfter, _] = update.witness.data.computeRootAndKey(
      update.newValue.data
    );
    update.newRoot.data.assertEquals(dataWitnessRootAfter);
    const [kindWitnessRootAfter, __] = update.witness.kind.computeRootAndKey(
      update.newValue.kind
    );
    update.newRoot.kind.assertEquals(kindWitnessRootAfter);
 
    return new MetadataTransition({
      oldRoot: update.oldRoot,
      newRoot: update.newRoot,
    });
  }
 
  static merge(
    transition1: MetadataTransition,
    transition2: MetadataTransition
  ) {
    return new MetadataTransition({
      oldRoot: transition1.oldRoot,
      newRoot: transition2.newRoot,
    });
  }
 
  static assertEquals(
    transition1: MetadataTransition,
    transition2: MetadataTransition
  ) {
    Metadata.assertEquals(transition1.oldRoot, transition2.oldRoot);
    Metadata.assertEquals(transition1.newRoot, transition2.newRoot);
  }
}
 
const MinaNFTMetadataUpdate = ZkProgram({
  name: "MinaNFTMetadataUpdate",
  publicInput: MetadataTransition,
 
  methods: {
    update: {
      privateInputs: [MetadataUpdate],
 
      async method(state: MetadataTransition, update: MetadataUpdate) {
        const computedState = MetadataTransition.create(update);
        MetadataTransition.assertEquals(computedState, state);
      },
    },
 
    merge: {
      privateInputs: [SelfProof, SelfProof],
 
      async method(
        newState: MetadataTransition,
        proof1: SelfProof<MetadataTransition, void>,
        proof2: SelfProof<MetadataTransition, void>
      ) {
        proof1.verify();
        proof2.verify();
 
        Metadata.assertEquals(
          proof1.publicInput.newRoot,
          proof2.publicInput.oldRoot
        );
        Metadata.assertEquals(proof1.publicInput.oldRoot, newState.oldRoot);
        Metadata.assertEquals(proof2.publicInput.newRoot, newState.newRoot);
      },
    },
  },
});
 
class MinaNFTMetadataUpdateProof extends ZkProgram.Proof(
  MinaNFTMetadataUpdate
) {}