All files / src baseminanft.ts

92.96% Statements 119/128
61.11% Branches 22/36
84.21% Functions 16/19
92.8% Lines 116/125

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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 37327x 27x 27x 27x 27x 27x 27x         27x 27x 27x 27x 27x   27x   27x                                       36x                 68x                           50x 50x 50x 50x 50x 50x 50x   50x                             119x                       119x 119x 322x 322x 322x   119x                                                                   834x   834x 834x                           28x   28x                   374x                     1330x                                                                                             17x                   43x       43x 16x 16x 16x 16x 16x     43x   42x 15x 15x 15x     15x 15x     42x 15x 15x 15x 15x 15x     42x 15x 15x 15x     15x 15x     42x               5x 5x 1x 1x 1x     1x 1x   5x 4x 4x 4x 4x 4x   5x               13x 13x 4x 4x 4x     4x 4x   13x 4x 4x 4x     4x 4x   13x 4x 4x 4x 4x 4x   13x               7x 7x 3x 3x 3x 3x 3x   7x               6x 6x 1x 1x 1x     1x 1x   6x      
export { BaseMinaNFT };
import { Field, Cache, VerificationKey, Encoding } from "o1js";
import { MinaNFT } from "./minanft";
import { MinaNFTContract } from "./contract/nft";
import { MinaNFTNameServiceContract } from "./contract/names";
import { Metadata, MetadataWitness } from "./contract/metadata";
import {
  MinaNFTMetadataUpdate,
  MetadataUpdate,
  MetadataMap,
} from "./contract/update";
import { RedactedMinaNFTMapCalculation } from "./plugins/redactedmap";
import { MinaNFTVerifier } from "./plugins/verifier";
import { MinaNFTVerifierBadge } from "./plugins/badge";
import { MinaNFTBadgeCalculation } from "./plugins/badgeproof";
import { Escrow } from "./plugins/escrow";
import { PrivateMetadata } from "./privatemetadata";
import { sleep } from "./mina";
import { Storage } from "./contract/metadata";
import { EscrowTransferVerification } from "./contract/transfer";
 
/**
 * Base class for MinaNFT
 */
class BaseMinaNFT {
  metadata: Map<string, PrivateMetadata>;
  static verificationKey: VerificationKey | undefined;
  static namesVerificationKey: VerificationKey | undefined;
  static updaterVerificationKey: VerificationKey | undefined;
  static updateVerificationKey: VerificationKey | undefined;
  static verifierVerificationKey: VerificationKey | undefined;
  static redactedMapVerificationKey: VerificationKey | undefined;
  static badgeVerifierVerificationKey: VerificationKey | undefined;
  static badgeVerificationKey: VerificationKey | undefined;
  static escrowVerificationKey: VerificationKey | undefined;
  static transferVerificationKey: VerificationKey | undefined;
  static cache: Cache | undefined;
 
  constructor() {
    this.metadata = new Map<string, PrivateMetadata>();
  }
 
  /**
   * Gets public attribute
   * @param key key of the attribute
   * @returns value of the attribute
   */
  public getMetadata(key: string): PrivateMetadata | undefined {
    return this.metadata.get(key);
  }
 
  /**
   * updates Metadata with key and value
   * @param mapToUpdate map to update
   * @param keyToUpdate key to update
   * @param newValue new value
   * @returns MapUpdate object
   */
  protected updateMetadataMap(
    keyToUpdate: string,
    newValue: PrivateMetadata
  ): MetadataUpdate {
    const { root, map } = this.getMetadataRootAndMap();
    const key = MinaNFT.stringToField(keyToUpdate);
    const witness: MetadataWitness = map.getWitness(key);
    const oldValue: Metadata = map.get(key);
    this.metadata.set(keyToUpdate, newValue);
    map.set(key, new Metadata({ data: newValue.data, kind: newValue.kind }));
    const newRoot: Metadata = map.getRoot();
 
    return new MetadataUpdate({
      oldRoot: root,
      newRoot,
      key,
      oldValue,
      newValue: new Metadata({ data: newValue.data, kind: newValue.kind }),
      witness,
    });
  }
 
  /**
   * Calculates a root and MerkleMap of the publicAttributes
   * @returns Root and MerkleMap of the publicAttributes
   */
  public getMetadataRootAndMap(): { root: Metadata; map: MetadataMap } {
    return this.getMapRootAndMap(this.metadata);
  }
 
  /**
   * Calculates a root and MerkleMap of the Map
   * @param data Map to calculate root and MerkleMap
   * @returns Root and MerkleMap of the Map
   */
  protected getMapRootAndMap(data: Map<string, PrivateMetadata>): {
    root: Metadata;
    map: MetadataMap;
  } {
    const map: MetadataMap = new MetadataMap();
    data.forEach((value: PrivateMetadata, key: string) => {
      const keyField = MinaNFT.stringToField(key);
      map.data.set(keyField, value.data);
      map.kind.set(keyField, value.kind);
    });
    return {
      root: new Metadata({
        data: map.data.getRoot(),
        kind: map.kind.getRoot(),
      }),
      map,
    };
  }
  /*
  public async getPublicJson(): Promise<object | undefined> {
    if (!this.publicAttributes.get("image")) return undefined;
    const publicAttributes: MerkleMap = new MerkleMap();
    Object.keys(this.publicAttributes).map((key) => {
      const value = this.publicAttributes.get(key);
      if (value) publicAttributes.set(MinaNFT.stringToField(key), value);
      else {
        console.error("Map error");
        return undefined;
      }
    });
    const publicMapRoot: string = publicAttributes.getRoot().toJSON();
    return {
      publicMapRoot,
      publicAttributes: MinaNFT.mapToJSON(this.publicAttributes),
    };
  }
*/
 
  /**
   * Converts a string to a Field
   * @param item string to convert
   * @returns string as a Field
   */
  public static stringToField(item: string): Field {
    const fields: Field[] = Encoding.stringToFields(item);
    //const fields: Field[] = stringToFields(item);
    if (fields.length === 1) {
      if (MinaNFT.stringFromFields(fields) === item) return fields[0];
      else Ethrow Error(`stringToField error: encoding error`);
    } else
      Ethrow new Error(
        `stringToField error: string ${item} is too long, requires ${fields.length} Fields`
      );
  }
 
  /**
   * Converts a string to a Fields
   * @param item string to convert
   * @returns string as a Field[]
   */
  public static stringToFields(item: string): Field[] {
    const fields: Field[] = Encoding.stringToFields(item);
    //const fields: Field[] = stringToFields(item);
    if (MinaNFT.stringFromFields(fields) === item) return fields;
    else Ethrow Error(`stringToField error: encoding error`);
  }
 
  /**
   * Converts a Field to a string
   * @param field Field to convert
   * @returns string
   */
  public static stringFromField(field: Field): string {
    return MinaNFT.stringFromFields([field]);
  }
 
  /**
   * Converts a Field[] to a string
   * @param fields Fields to convert
   * @returns string
   */
  public static stringFromFields(fields: Field[]): string {
    // Encoding.stringFromFields is not working properly in o1js 0.14.0, use internal implementation
    // It is working again in o1js 0.14.1
    return Encoding.stringFromFields(fields);
  }
 
  /**
   * Converts a string "i:..." or "a:..." to a storage url string
   * @param storageStr string to convert
   * @returns string
   */
  public static urlFromStorageString(storageStr: string): string {
    Iif (
      storageStr.length < 2 ||
      (storageStr[0] !== "i" && storageStr[0] !== "a")
    ) {
      throw new Error("Invalid storage string");
    }
    const url: string =
      storageStr[0] === "i"
        ? "https://gateway.pinata.cloud/ipfs/" + storageStr.slice(2)
        : "https://arweave.net/" + storageStr.slice(2);
    return url;
  }
 
  /**
   * Converts a Storage to a storage url string
   * @param storage Storage to convert
   * @returns string
   */
  public static urlFromStorage(storage: Storage): string {
    return BaseMinaNFT.urlFromStorageString(
      Encoding.stringFromFields(storage.hashString)
    );
  }
 
  /**
   * Sets a cache for prover keys
   */
  public static setCache(cache: Cache): void {
    MinaNFT.cache = cache;
  }
 
  /**
   * Sets a cache folder for prover keys
   * @param folder folder for prover keys
   * default is "./cache"
   */
  // eslint-disable-next-line @typescript-eslint/no-inferrable-types
  public static setCacheFolder(folder: string = "./cache"): void {
    BaseMinaNFT.cache = Cache.FileSystem(folder);
  }
 
  /**
   * Compiles MinaNFT contract
   * @returns verification key
   */
  public static async compile(
    rollup: boolean = false
  ): Promise<VerificationKey> {
    const options = BaseMinaNFT.cache
      ? { cache: BaseMinaNFT.cache }
      : undefined;
 
    if (BaseMinaNFT.updateVerificationKey === undefined) {
      console.time("MinaNFTMetadataUpdate compiled");
      await sleep(100); // alow GC to run
      const { verificationKey } = await MinaNFTMetadataUpdate.compile(options);
      console.timeEnd("MinaNFTMetadataUpdate compiled");
      BaseMinaNFT.updateVerificationKey = verificationKey;
    }
 
    if (rollup) return BaseMinaNFT.updateVerificationKey;
 
    if (MinaNFT.transferVerificationKey === undefined) {
      console.time("EscrowTransferVerification compiled");
      await sleep(100); // alow GC to run
      const { verificationKey } = await EscrowTransferVerification.compile(
        options
      );
      console.timeEnd("EscrowTransferVerification compiled");
      MinaNFT.transferVerificationKey = verificationKey;
    }
 
    if (MinaNFT.verificationKey == undefined) {
      console.time("MinaNFT compiled");
      await sleep(100); // alow GC to run
      const { verificationKey } = await MinaNFTContract.compile(options);
      console.timeEnd("MinaNFT compiled");
      MinaNFT.verificationKey = verificationKey as VerificationKey;
    }
 
    if (MinaNFT.namesVerificationKey == undefined) {
      console.time("MinaNFTNameServiceContract compiled");
      await sleep(100); // alow GC to run
      const { verificationKey } = await MinaNFTNameServiceContract.compile(
        options
      );
      console.timeEnd("MinaNFTNameServiceContract compiled");
      MinaNFT.namesVerificationKey = verificationKey as VerificationKey;
    }
 
    return MinaNFT.verificationKey;
  }
 
  /**
   * Compiles MinaNFTVerifier contract
   * @returns verification key
   */
  public static async compileVerifier(): Promise<VerificationKey> {
    const options = MinaNFT.cache ? { cache: MinaNFT.cache } : undefined;
    if (MinaNFT.redactedMapVerificationKey === undefined) {
      console.time("RedactedMinaNFTMapCalculation compiled");
      await sleep(100); // alow GC to run
      const { verificationKey } = await RedactedMinaNFTMapCalculation.compile(
        options
      );
      console.timeEnd("RedactedMinaNFTMapCalculation compiled");
      MinaNFT.redactedMapVerificationKey = verificationKey;
    }
    if (MinaNFT.verifierVerificationKey === undefined) {
      console.time("MinaNFTVerifier compiled");
      await sleep(100); // alow GC to run
      const { verificationKey } = await MinaNFTVerifier.compile(options);
      console.timeEnd("MinaNFTVerifier compiled");
      MinaNFT.verifierVerificationKey = verificationKey as VerificationKey;
    }
    return MinaNFT.verifierVerificationKey;
  }
 
  /**
   * Compiles MinaNFTVerifierBadge contract
   * @returns verification key
   */
  public static async compileBadge(): Promise<VerificationKey> {
    const options = MinaNFT.cache ? { cache: MinaNFT.cache } : undefined;
    if (MinaNFT.redactedMapVerificationKey === undefined) {
      console.time("RedactedMinaNFTMapCalculation compiled");
      await sleep(100); // alow GC to run
      const { verificationKey } = await RedactedMinaNFTMapCalculation.compile(
        options
      );
      console.timeEnd("RedactedMinaNFTMapCalculation compiled");
      MinaNFT.redactedMapVerificationKey = verificationKey;
    }
    if (MinaNFT.badgeVerificationKey === undefined) {
      console.time("MinaNFTBadgeCalculation compiled");
      await sleep(100); // alow GC to run
      const { verificationKey } = await MinaNFTBadgeCalculation.compile(
        options
      );
      console.timeEnd("MinaNFTBadgeCalculation compiled");
      MinaNFT.badgeVerificationKey = verificationKey;
    }
    if (MinaNFT.badgeVerifierVerificationKey === undefined) {
      console.time("MinaNFTVerifierBadge compiled");
      await sleep(100); // alow GC to run
      const { verificationKey } = await MinaNFTVerifierBadge.compile(options);
      console.timeEnd("MinaNFTVerifierBadge compiled");
      MinaNFT.badgeVerifierVerificationKey = verificationKey as VerificationKey;
    }
    return MinaNFT.badgeVerifierVerificationKey;
  }
 
  /**
   * Compiles Escrow contract
   * @returns verification key
   */
  public static async compileEscrow(): Promise<VerificationKey> {
    const options = MinaNFT.cache ? { cache: MinaNFT.cache } : undefined;
    if (MinaNFT.escrowVerificationKey === undefined) {
      console.time("Escrow compiled");
      await sleep(100); // alow GC to run
      const { verificationKey } = await Escrow.compile(options);
      console.timeEnd("Escrow compiled");
      MinaNFT.escrowVerificationKey = verificationKey as VerificationKey;
    }
    return MinaNFT.escrowVerificationKey;
  }
 
  /**
   * Compiles RedactedMinaNFTMapCalculation contract
   * @returns verification key
   */
  public static async compileRedactedMap(): Promise<VerificationKey> {
    const options = MinaNFT.cache ? { cache: MinaNFT.cache } : undefined;
    if (MinaNFT.redactedMapVerificationKey === undefined) {
      console.time("RedactedMinaNFTMapCalculation compiled");
      await sleep(100); // alow GC to run
      const { verificationKey } = await RedactedMinaNFTMapCalculation.compile(
        options
      );
      console.timeEnd("RedactedMinaNFTMapCalculation compiled");
      MinaNFT.redactedMapVerificationKey = verificationKey;
    }
    return MinaNFT.redactedMapVerificationKey;
  }
}