All files / src/storage file.ts

75.86% Statements 176/232
45.94% Branches 34/74
81.25% Functions 13/16
77.88% Lines 169/217

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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 42927x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x   27x 27x                                                 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x       16x 16x       16x   16x 16x   16x 16x 16x 16x     16x       16x 16x 16x   16x 16x     16x       16x   16x     16x   16x 16x 16x 16x       16x 16x 16x 16x         16x 16x       21x 21x                           6x 6x 6x   6x       6x       6x   6x   6x   6x   6x     6x                           1x 1x   1x 1x 14x   1x 1x                               14x 14x 14x 14x           10x 10x 10x             9x 9x 9x 9x 51x   9x 9x 9x               9x 9x   9x 9x 7x 7x 7x 7x             7x 7x 7x 7x 7x 2x 2x 2x 2x           2x 2x 2x 2x   9x                   4x 4x   4x 4x     74x 74x 74x 4444354x 4444354x 4444354x 143364x 143364x 143364x     74x   4x 70x 70x 70x 70x 70x     70x 70x   4x 4x 4x                                                                                           13x 9x 9x 9x 9x             4x       4x 4x 4x 4x           4x 2x       2x   2x 2x   2x 2x       2x 2x 2x         9x 1x 1x 1x   9x   9x 9x 9x 9x 9x   9x                   9x                          
export { File, FileData, FileDataType, FILE_TREE_HEIGHT, FILE_TREE_ELEMENTS };
import { MerkleTree, Field, Encoding } from "o1js";
import fs from "fs/promises";
import { createHash } from "crypto";
import path from "path";
import mime from "mime";
import { BaseMinaNFTObject } from "../baseminanftobject";
import { RedactedTree } from "../redactedtree";
import { IPFS } from "./ipfs";
import { ARWEAVE } from "./arweave";
import Jimp from "jimp";
import { calculateMerkleTreeRootFast } from "./fast-tree";
 
const FILE_TREE_HEIGHT = 5;
const FILE_TREE_ELEMENTS = 14;
type FileDataType = "binary" | "png" | "word";
 
class FileData extends BaseMinaNFTObject {
  fileRoot: Field;
  height: number;
  size: number;
  mimeType: string;
  sha3_512: string;
  filename: string;
  storage: string;
  fileType?: FileDataType;
  metadata?: Field;
 
  constructor(value: {
    fileRoot: Field;
    height: number;
    size: number;
    mimeType: string;
    sha3_512: string;
    filename: string;
    storage: string;
    fileType?: FileDataType;
    metadata?: Field;
  }) {
    super("file");
    this.fileRoot = value.fileRoot;
    this.height = value.height;
    this.size = value.size;
    this.mimeType = value.mimeType;
    this.sha3_512 = value.sha3_512;
    this.filename = value.filename;
    this.storage = value.storage;
    this.fileType = value.fileType ?? "binary";
    this.metadata = value.metadata ?? Field(0);
    const tree = this.buildTree().tree;
    this.root = tree.getRoot();
  }
 
  public buildTree(): { tree: MerkleTree; fields: Field[] } {
    const tree = new MerkleTree(FILE_TREE_HEIGHT);
    Iif (Number(tree.leafCount) < FILE_TREE_ELEMENTS)
      throw new Error(
        `FileData has wrong encoding, should be at least FILE_TREE_ELEMENTS (12) leaves`
      );
    const fields: Field[] = [];
    // First field is the height, second number is the number of fields
    fields.push(Field.from(FILE_TREE_HEIGHT)); // 0
    fields.push(Field.from(FILE_TREE_ELEMENTS)); // Number of data fields // 1
 
    fields.push(this.fileRoot); //2
    fields.push(Field.from(this.height)); //3
    fields.push(Field.from(this.size)); //4
    const mimeTypeFields = Encoding.stringToFields(
      this.mimeType.substring(0, 30)
    );
    Iif (mimeTypeFields.length !== 1)
      throw new Error(
        `FileData: MIME type string is too long, should be less than 30 bytes`
      );
    fields.push(mimeTypeFields[0]); //5
    const sha512Fields = Encoding.stringToFields(this.sha3_512);
    Iif (sha512Fields.length !== 3)
      throw new Error(`SHA512 has wrong encoding, should be base64`);
    fields.push(...sha512Fields); // 6,7,8
    const filenameFields = Encoding.stringToFields(
      this.filename.substring(0, 30)
    );
    Iif (filenameFields.length !== 1)
      throw new Error(
        `FileData: Filename string is too long, should be less than 30 bytes`
      );
    fields.push(filenameFields[0]); // 9
    const storageFields: Field[] =
      this.storage === ""
        ? [Field(0), Field(0)]
        : Encoding.stringToFields(this.storage);
    Iif (storageFields.length !== 2)
      throw new Error(`Storage string has wrong encoding`);
    fields.push(...storageFields); // 10, 11
    const fileType = this.fileType ?? "binary";
    const fileTypeFields = Encoding.stringToFields(fileType.substring(0, 30));
    Iif (fileTypeFields.length !== 1)
      throw new Error(
        `FileData: fileType string is too long, should be less than 30 bytes`
      );
    fields.push(...fileTypeFields); // 12
    const metadata: Field = this.metadata ?? Field(0);
    fields.push(metadata); // 13
    Iif (fields.length !== FILE_TREE_ELEMENTS)
      throw new Error(
        `FileData has wrong encoding, should be FILE_TREE_ELEMENTS (14) fields`
      );
 
    tree.fill(fields);
    return { tree, fields };
  }
 
  public toJSON(): object {
    const metadata: Field = this.metadata ?? Field(0);
    return {
      fileMerkleTreeRoot: this.fileRoot.toJSON(),
      MerkleTreeHeight: this.height,
      size: this.size,
      mimeType: this.mimeType,
      SHA3_512: this.sha3_512,
      filename: this.filename,
      storage: this.storage,
      fileType: this.fileType ?? "binary",
      metadata: metadata.toJSON(),
    };
  }
  public static fromJSON(json: object): FileData {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const obj = json as any;
    const data = obj.linkedObject;
    Iif (data === undefined)
      throw new Error(`uri: NFT metadata: data should be present: ${json}`);
    Iif (data.fileMerkleTreeRoot === undefined)
      throw new Error(
        `uri: NFT metadata: fileMerkleTreeRoot should be present: ${json}`
      );
    Iif (data.MerkleTreeHeight === undefined)
      throw new Error(
        `uri: NFT metadata: MerkleTreeHeight should be present: ${json}`
      );
    Iif (data.size === undefined)
      throw new Error(`uri: NFT metadata: size should be present: ${json}`);
    Iif (data.mimeType === undefined)
      throw new Error(`uri: NFT metadata: mimeType should be present: ${json}`);
    Iif (data.SHA3_512 === undefined)
      throw new Error(`uri: NFT metadata: SHA3_512 should be present: ${json}`);
    Iif (data.filename === undefined)
      throw new Error(`uri: NFT metadata: filename should be present: ${json}`);
    Iif (data.storage === undefined)
      throw new Error(`uri: NFT metadata: storage should be present: ${json}`);
 
    return new FileData({
      fileRoot: Field.fromJSON(data.fileMerkleTreeRoot),
      height: Number(data.MerkleTreeHeight),
      size: Number(data.size),
      mimeType: data.mimeType,
      sha3_512: data.SHA3_512,
      filename: data.filename,
      storage: data.storage,
      fileType: data.fileType ?? "binary",
      metadata: data.metadata ? Field.fromJSON(data.metadata) : Field(0),
    });
  }
 
  public async proof(verbose?: boolean) {
    const { tree, fields } = this.buildTree();
    Iif (fields.length !== FILE_TREE_ELEMENTS)
      throw new Error(`FileData: proof: wrong number of fields`);
    const redactedTree = new RedactedTree(FILE_TREE_HEIGHT, tree);
    for (let i = 0; i < fields.length; i++) {
      redactedTree.set(i, fields[i]);
    }
    const proof = await redactedTree.proof(verbose);
    return proof;
  }
}
 
class File {
  filename: string;
  storage: string;
  sha3_512_hash?: string;
  size?: number;
  mimeType?: string;
  root?: Field;
  height?: number;
  leavesNumber?: number;
  fileType: FileDataType;
  fileMetadata: Field;
  constructor(filename: string, fileType?: FileDataType, fileMetadata?: Field) {
    this.filename = filename;
    this.storage = "";
    this.fileType = fileType ?? "binary";
    this.fileMetadata = fileMetadata ?? Field(0);
  }
  public async metadata(): Promise<{
    size: number;
    mimeType: string;
  }> {
    const stat = await fs.stat(this.filename);
    const mimeType = mime.getType(this.filename);
    return {
      size: stat.size,
      mimeType: mimeType ?? "application/octet-stream",
    };
  }
 
  public async sha3_512(): Promise<string> {
    const file: fs.FileHandle = await fs.open(this.filename);
    const stream = file.createReadStream();
    const hash = createHash("SHA3-512");
    for await (const chunk of stream) {
      hash.update(chunk);
    }
    this.sha3_512_hash = hash.digest("base64");
    stream.close();
    return this.sha3_512_hash;
  }
 
  public async pin(params: {
    pinataJWT?: string;
    arweaveKey?: string;
    keyvalues?: object;
  }): Promise<string> {
    const { pinataJWT, arweaveKey, keyvalues } = params;
    Iif (pinataJWT === undefined && arweaveKey === undefined)
      throw new Error(`Pin failed: no pinataJWT or arweaveKey`);
    const metadata = await this.metadata();
    if (pinataJWT !== undefined) {
      const file: fs.FileHandle = await fs.open(this.filename);
      const stream = file.createReadStream();
      const ipfs = new IPFS(pinataJWT);
      const hash = await ipfs.pinFile({
        stream,
        name: path.basename(this.filename),
        size: metadata.size,
        mimeType: metadata.mimeType,
        keyvalues: keyvalues ?? { project: "MinaNFT" },
      });
      stream.close();
      Iif (hash === undefined) throw new Error(`IPFS pin failed`);
      this.storage = `i:${hash}`;
      this.size = metadata.size;
      this.mimeType = metadata.mimeType;
    } else if (arweaveKey !== undefined) {
      const arweave = new ARWEAVE(arweaveKey);
      const data = await fs.readFile(this.filename);
      const hash = await arweave.pinFile(
        data,
        path.basename(this.filename),
        metadata.size,
        metadata.mimeType
      );
      Iif (hash === undefined) throw new Error(`Arweave pin failed`);
      this.storage = `a:${hash}`;
      this.size = metadata.size;
      this.mimeType = metadata.mimeType;
    }
    return this.storage;
  }
 
  public async setMetadata(): Promise<void> {
    const metadata = await this.metadata();
    this.size = metadata.size;
    this.mimeType = metadata.mimeType;
  }
 
  public async binaryFields(): Promise<Field[]> {
    const fields: Field[] = [];
    let remainder: Uint8Array = new Uint8Array(0);
 
    const file: fs.FileHandle = await fs.open(this.filename);
    const stream = file.createReadStream();
 
    function fillFields(bytes: Uint8Array): void {
      let currentBigInt = BigInt(0);
      let bitPosition = BigInt(0);
      for (const byte of bytes) {
        currentBigInt += BigInt(byte) << bitPosition;
        bitPosition += BigInt(8);
        if (bitPosition === BigInt(248)) {
          fields.push(Field(currentBigInt.toString()));
          currentBigInt = BigInt(0);
          bitPosition = BigInt(0);
        }
      }
      if (Number(bitPosition) > 0) fields.push(Field(currentBigInt.toString()));
    }
    for await (const chunk of stream) {
      const bytes: Uint8Array = new Uint8Array(remainder.length + chunk.length);
      if (remainder.length > 0) bytes.set(remainder);
      bytes.set(chunk as Buffer, remainder.length);
      const fieldsNumber = Math.floor(bytes.length / 31);
      const chunkSize = fieldsNumber * 31;
      //const chunkFields = Encoding.bytesToFields(bytes.slice(0, chunkSize));
      //fields.push(...chunkFields);
      fillFields(bytes.slice(0, chunkSize));
      remainder = bytes.slice(chunkSize);
    }
    if (remainder.length > 0) fillFields(remainder);
    stream.close();
    return fields;
  }
 
  public static fillFields(bytes: Uint8Array): Field[] {
    const fields: Field[] = [];
    let currentBigInt = BigInt(0);
    let bitPosition = BigInt(0);
    for (const byte of bytes) {
      currentBigInt += BigInt(byte) << bitPosition;
      bitPosition += BigInt(8);
      Iif (bitPosition === BigInt(248)) {
        fields.push(Field(currentBigInt.toString()));
        currentBigInt = BigInt(0);
        bitPosition = BigInt(0);
      }
    }
    Iif (Number(bitPosition) > 0) fields.push(Field(currentBigInt.toString()));
    return fields;
  }
 
  public async pngFields(): Promise<Field[]> {
    const fields: Field[] = [];
    const file = await fs.readFile(this.filename);
    const png = await Jimp.read(file);
    fields.push(Field(png.bitmap.width));
    fields.push(Field(png.bitmap.height));
    fields.push(Field(png.bitmap.data.length));
    for (let i = 0; i < png.bitmap.data.length; i += 4) {
      const value =
        BigInt(png.bitmap.data[i]) +
        (BigInt(png.bitmap.data[i + 1]) << BigInt(8)) +
        (BigInt(png.bitmap.data[i + 2]) << BigInt(16)) +
        (BigInt(png.bitmap.data[i + 3]) << BigInt(24));
      fields.push(Field(value));
    }
    return fields;
  }
 
  public async treeData(
    calculateRoot: boolean,
    fastCalculation: boolean = true
  ): Promise<{
    root: Field;
    height: number;
    leavesNumber: number;
  }> {
    if (calculateRoot === false) {
      this.root = Field(0);
      this.height = 0;
      this.leavesNumber = 0;
      return {
        root: this.root,
        height: this.height,
        leavesNumber: this.leavesNumber,
      };
    }
    const fields: Field[] =
      this.fileType === "png"
        ? await this.pngFields()
        : await this.binaryFields();
 
    const height = Math.ceil(Math.log2(fields.length + 2)) + 1;
    this.height = height;
    this.leavesNumber = fields.length;
    const treeFields = [
      Field.from(height),
      Field.from(fields.length),
      ...fields,
    ];
 
    if (fastCalculation) {
      const { leafCount, root } = calculateMerkleTreeRootFast(
        height,
        treeFields
      );
      Iif (treeFields.length > leafCount)
        throw new Error(`File is too big for this Merkle tree`);
      this.root = root;
      return { root, height, leavesNumber: this.leavesNumber };
    } else {
      const tree = new MerkleTree(height);
      Iif (treeFields.length > tree.leafCount)
        throw new Error(`File is too big for this Merkle tree`);
 
      // First field is the height, second number is the number of fields
      tree.fill(treeFields);
      this.root = tree.getRoot();
      return { root: this.root, height, leavesNumber: this.leavesNumber };
    }
  }
 
  public async data(): Promise<FileData> {
    if (this.storage === "") {
      const metadata = await this.metadata();
      this.size = metadata.size;
      this.mimeType = metadata.mimeType;
    }
    Iif (this.sha3_512_hash === undefined)
      throw new Error(`File: SHA3-512 hash not set`);
    Iif (this.size === undefined) throw new Error(`File: size not set`);
    Iif (this.mimeType === undefined) throw new Error(`File: MIME type not set`);
    Iif (this.root === undefined) throw new Error(`File: root not set`);
    Iif (this.height === undefined) throw new Error(`File: height not set`);
    Iif (this.leavesNumber === undefined)
      throw new Error(`File: leavesNumber not set`);
    Iif (
      this.leavesNumber !== 0 &&
      this.leavesNumber !== Math.ceil(this.size / 31)
    ) {
      console.log(`File: leavesNumber: ${this.leavesNumber}`);
      console.log(`File: size: ${this.size}`);
    }
    //const metadata = await this.metadata();
    //const sha3_512 = await this.sha3_512();
    //const treeData = await this.treeData();
    return new FileData({
      fileRoot: this.root,
      height: this.height,
      size: this.size,
      mimeType: this.mimeType.slice(0, 30),
      sha3_512: this.sha3_512_hash,
      filename: path.basename(this.filename).slice(0, 30),
      storage: this.storage,
      fileType: this.fileType,
      metadata: this.fileMetadata,
    });
  }
}