{"version":3,"file":"module.cjs","sources":["../../../../../src/modules/abstract/anchor/module.js"],"sourcesContent":["/* eslint-disable camelcase */\nimport { construct, verify_proof } from 'mrklt';\nimport { AbstractBaseModule } from '../common';\nimport { Anchor } from '../../../types/anchor';\nimport { withExtendedPrototypeProperties } from '../../../utils';\n\n/** Class to create and query anchors from chain. */\nclass AbstractAnchorModule extends AbstractBaseModule {\n  /**\n   * Write anchor on chain\n   * @param anchor\n   * @returns {Promise<*>}\n   */\n  async deploy(anchor, params) {\n    return await this.signAndSend(await this.deployTx(anchor), params);\n  }\n\n  /**\n   * Query anchor from chain\n   * @param {AnchorHash} anchor\n   * @param {boolean} preHashed - If the anchor has already been hashed.\n   * @returns {Promise<Anchor>} - The promise will either successfully resolve to the block number where anchor was created\n   * or reject with an error.\n   */\n  async get(_anchorKey, _preHashed = false) {\n    throw new Error('Unimplemented');\n  }\n\n  /**\n   * Batch multiple documents in binary merkle tree and return the root and proofs for each document\n   * @param documents\n   * @returns {Array} - An 2 element array where 1st element is root and 2nd is an array with proofs for\n   * each document.\n   */\n  static batchDocumentsInMerkleTree(documents) {\n    // Hash all documents\n    const leafHashes = documents.map((doc) => Anchor.hash(doc).bytes);\n\n    // If only one document was hashed, just return that as the root with no proofs (single anchor)\n    if (leafHashes.length === 1) {\n      return [leafHashes[0], []];\n    }\n\n    // Concatenate all leaf hashes into one bytearray\n    const packed = new Uint8Array(leafHashes.map((a) => [...a]).flat());\n    const [root, proofs] = construct(packed);\n    return [Uint8Array.from(root), proofs, leafHashes];\n  }\n\n  /**\n   * Verify inclusion proof of document in a merkle tree with given root. The document is hashed to form a leaf first\n   * @param document\n   * @param proof\n   * @param root\n   * @returns {boolean}\n   */\n  static verifyMerkleProofOfDocument(document, proof, root) {\n    const hash = Anchor.hash(document).bytes;\n    return this.verifyMerkleProofOfLeaf(hash, proof, root);\n  }\n\n  /**\n   * Verify inclusion proof of leaf in a merkle tree with given root.\n   * @param leaf\n   * @param proof\n   * @param root\n   * @returns {boolean}\n   */\n  static verifyMerkleProofOfLeaf(leaf, proof, root) {\n    /* eslint-disable camelcase */\n    const calculatedRoot = verify_proof(leaf, proof);\n    return (\n      calculatedRoot.length === root.length\n      && calculatedRoot.every((v, i) => v === root[i])\n    );\n  }\n}\n\nexport default withExtendedPrototypeProperties(\n  ['deployTx', 'get'],\n  AbstractAnchorModule,\n);\n"],"names":["AbstractBaseModule","Anchor","construct","verify_proof","withExtendedPrototypeProperties","AbstractAnchorModule"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AAMA;6BACA,MAAM,oBAAoB,SAASA,0CAAkB,CAAC;AACtD;AACA;AACA;AACA;AACA;AACA,EAAE,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE;AAC/B,IAAI,OAAO,MAAM,IAAI,CAAC,WAAW,CAAC,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;AACtE,EAAE;;AAEF;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAE,MAAM,GAAG,CAAC,UAAU,EAAE,UAAU,GAAG,KAAK,EAAE;AAC5C,IAAI,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC;AACpC,EAAE;;AAEF;AACA;AACA;AACA;AACA;AACA;AACA,EAAE,OAAO,0BAA0B,CAAC,SAAS,EAAE;AAC/C;AACA,IAAI,MAAM,UAAU,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,GAAG,KAAKC,yBAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC;;AAErE;AACA,IAAI,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE;AACjC,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;AAChC,IAAI;;AAEJ;AACA,IAAI,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,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