{"version":3,"file":"DockCryptoSignatureProof.cjs","sources":["../../../../../src/vc/crypto/common/DockCryptoSignatureProof.js"],"sourcesContent":["import { Presentation } from '@docknetwork/crypto-wasm-ts';\nimport b58 from 'bs58';\nimport stringify from 'json-stringify-deterministic';\nimport { withExtendedStaticProperties } from '../../../utils/inheritance';\n\nimport CustomLinkedDataSignature from './CustomLinkedDataSignature';\n\nimport { isCredVerGte060 } from './DockCryptoSignature';\n\nconst SUITE_CONTEXT_URL = 'https://www.w3.org/2018/credentials/v1';\n\nexport const DOCK_ANON_CREDENTIAL_ID = 'dock:anonymous:credential';\n\n/**\n * Defines commons for the `@docknetwork/crypto-wasm-ts` signature proofs.\n */\nexport default withExtendedStaticProperties(\n  ['Signature', 'proofType', 'sigName'],\n  class DockCryptoSignatureProof extends CustomLinkedDataSignature {\n    /**\n     * Default constructor\n     * @param options {SignatureSuiteOptions} options for constructing the signature suite\n     * @param type\n     * @param LDKeyClass\n     * @param link\n     */\n    constructor(options = {}, type, LDKeyClass, link) {\n      const { verificationMethod } = options;\n\n      super({\n        type,\n        LDKeyClass,\n        contextUrl: SUITE_CONTEXT_URL,\n        alg: type,\n        useProofValue: true,\n      });\n\n      this.proof = {\n        '@context': [\n          {\n            sec: 'https://w3id.org/security#',\n            proof: {\n              '@id': 'sec:proof',\n              '@type': '@id',\n              '@container': '@graph',\n            },\n          },\n          link,\n        ],\n        type,\n      };\n\n      this.verificationMethod = verificationMethod;\n      this.accumulatorPublicKeys = options.accumulatorPublicKeys;\n      this.predicateParams = options.predicateParams;\n      this.circomOutputs = options.circomOutputs;\n      this.blindedAttributesCircomOutputs = options.blindedAttributesCircomOutputs;\n    }\n\n    async verifyProof({\n      proof, document, documentLoader, expansionMap,\n    }) {\n      try {\n        const verificationMethod = await this.getVerificationMethod({\n          proof,\n          document,\n          documentLoader,\n          expansionMap,\n        });\n\n        const presentationJSON = this.constructor.derivedToAnoncredsPresentation({\n          ...document,\n          proof,\n        });\n        const recreatedPres = Presentation.fromJSON(presentationJSON);\n\n        // NOTE: Another example that this credential derivation doesn't work for presentation with more than 1 credential\n        const pks = verificationMethod !== undefined\n          ? [verificationMethod].map((keyDocument) => {\n            const pkRaw = b58.decode(keyDocument.publicKeyBase58);\n            return new this.constructor.Signature.KeyPair.PublicKey(pkRaw);\n          })\n          : [];\n\n        const {\n          accumulatorPublicKeys,\n          predicateParams,\n          circomOutputs,\n          blindedAttributesCircomOutputs,\n        } = this;\n\n        const expectedAccumulatorPKs = accumulatorPublicKeys\n          ? new Map()\n          : undefined;\n        if (accumulatorPublicKeys) {\n          const expectedAccumulatorPK = accumulatorPublicKeys.get(\n            document?.credentialStatus?.id,\n          );\n          if (expectedAccumulatorPK) {\n            expectedAccumulatorPKs.set(0, expectedAccumulatorPK);\n          }\n        }\n\n        const res = recreatedPres.verify(\n          pks,\n          expectedAccumulatorPKs,\n          predicateParams,\n          circomOutputs,\n          blindedAttributesCircomOutputs,\n        );\n        if (!res.verified) {\n          throw new Error(\n            `Invalid anoncreds presentation due to error: ${res.error}`,\n          );\n        }\n\n        return { verified: true, verificationMethod };\n      } catch (error) {\n        return { verified: false, error };\n      }\n    }\n\n    /**\n     * Converts a derived proof credential to the native presentation format\n     * @param document\n     */\n    static derivedToAnoncredsPresentation(document) {\n      const {\n        '@context': context,\n        type,\n        credentialSchema,\n        credentialStatus,\n        // issuance date isn't revealed in presentations but for W3C style credential or for the frontend, it needs to be\n        // present in the derived credential. Popping it out so that it doesn't end up in `revealedAttributes`. Note that this\n        // can be a problem when `issuanceDate` is actually revealed which is rare but a possibility\n        issuanceDate: _issuanceDate,\n        proof,\n        ...revealedAttributes\n      } = document;\n\n      // Old prover don't reveal the issuer so remove this field from the revealed attributes\n      if (proof.presVersion === undefined) {\n        delete revealedAttributes.issuer;\n      }\n\n      // ID wasnt revealed but placeholder was used to conform to W3C spec, trim it\n      if (revealedAttributes.id === DOCK_ANON_CREDENTIAL_ID) {\n        delete revealedAttributes.id;\n      }\n\n      const credVersion = proof.version;\n      const newCredVersion = isCredVerGte060(credVersion);\n      // TODO: This is wrong. This won't work with presentation from 2 or more credentials\n      const c = {\n        sigType: proof.sigType,\n        version: credVersion,\n        bounds: proof.bounds,\n        schema: newCredVersion\n          ? credentialSchema\n          : JSON.stringify(credentialSchema),\n        revealedAttributes: {\n          proof: {\n            type: this.sigName,\n            verificationMethod: proof.verificationMethod,\n          },\n          '@context': newCredVersion\n            ? stringify(context)\n            : JSON.stringify(context),\n          type: newCredVersion ? stringify(type) : JSON.stringify(type),\n          ...revealedAttributes,\n        },\n      };\n      if (credentialStatus !== undefined) {\n        c.status = credentialStatus;\n      }\n      return {\n        // Old credentials incorrectly set the presentation version as credential version which was fine because coincidentally\n        // both versions were same. Correcting this mistake now. Once all dependents have upgraded and old presentations don't\n        // need to be supported, `proof.presVersion` should be used.\n        version:\n          proof.presVersion !== undefined ? proof.presVersion : proof.version,\n        nonce: proof.nonce,\n        context: proof.context,\n        spec: {\n          credentials: [c],\n          attributeEqualities: proof.attributeEqualities,\n          boundedPseudonyms: proof.boundedPseudonyms,\n          unboundedPseudonyms: proof.unboundedPseudonyms,\n        },\n        attributeCiphertexts: proof.attributeCiphertexts,\n        proof: proof.proofValue,\n      };\n    }\n\n    /**\n     * @param document {object} to be signed.\n     * @param proof {object}\n     * @param documentLoader {function}\n     * @param expansionMap {function}\n     */\n    async getVerificationMethod({ proof, documentLoader }) {\n      return this.constructor.Signature.getVerificationMethod({\n        proof,\n        documentLoader,\n      });\n    }\n  },\n);\n"],"names":["withExtendedStaticProperties","CustomLinkedDataSignature","Presentation","b58","isCredVerGte060","stringify"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AASA,MAAM,iBAAiB,GAAG,wCAAwC;;AAEtD,MAAC,uBAAuB,GAAG;;AAEvC;AACA;AACA;AACA,+BAAeA,kCAA4B;AAC3C,EAAE,CAAC,WAAW,EAAE,WAAW,EAAE,SAAS,CAAC;AACvC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