import { getProofs , getTypeInfo } from "./utilities" ; import jsonld from "jsonld" ; import { SECURITY_PROOF_URL } from "jsonld-signatures" ; param proofDocument param revealDocument param options export const deriveProof = async ( proofDocument , revealDocument , { suite , documentLoader , expansionMap , skipProofCompaction , nonce } ) : Promise < any > => { if ( ! suite ) { throw new TypeError ( '"options.suite" is required.' ) ; } if ( Array . isArray ( proofDocument ) ) { throw new TypeError ( "proofDocument should be an object not an array." ) ; } const { proofs , document } = await getProofs ( { document : proofDocument , proofType : suite . supportedDeriveProofType , documentLoader , expansionMap } ) ; if ( proofs . length === 0 ) { throw new Error ( `There were not any proofs provided that can be used to derive a proof with this suite.` ) ; } let derivedProof ; derivedProof = await suite . deriveProof ( { document , proof : proofs [ 0 ] , revealDocument , documentLoader , expansionMap , nonce } ) ; if ( proofs . length > 1 ) { derivedProof = { ... derivedProof , proof : [ derivedProof . proof ] } ; proofs . splice ( 0 , 1 ) ; for ( const proof of proofs ) { const additionalDerivedProofValue = await suite . deriveProof ( { document , proof , revealDocument , documentLoader , expansionMap } ) ; derivedProof . proof . push ( additionalDerivedProofValue . proof ) ; } } if ( ! skipProofCompaction ) { let expandedProof = { [ SECURITY_PROOF_URL ] : { "@graph" : derivedProof . proof } } ; const { types , alias } = await getTypeInfo ( derivedProof . document , { documentLoader , expansionMap } ) ; expandedProof [ "@type" ] = types ; const ctx = jsonld . getValues ( derivedProof . document , "@context" ) ; const compactProof = await jsonld . compact ( expandedProof , ctx , { documentLoader , expansionMap , compactToRelative : false } ) ; delete compactProof [ alias ] ; delete compactProof [ "@context" ] ; included if ( compactProof . proof [ "@included" ] ) { compactProof . proof = compactProof . proof [ "@included" ] ; } const key = Object . keys ( compactProof ) [ 0 ] ; jsonld . addValue ( derivedProof . document , key , compactProof [ key ] ) ; } else { delete derivedProof . proof [ "@context" ] ; jsonld . addValue ( derivedProof . document , "proof" , derivedProof . proof ) ; } return derivedProof . document ; } ;