import { SECURITY_CONTEXT_URL } from "jsonld-signatures" ; import jsonld from "jsonld" ; import { GetProofsOptions , GetProofsResult , GetTypeOptions } from "./types" ; /**
 * The property identifying the linked data proof
 * Note - this will not work for legacy systems that
 * relying on `signature`
 */ const PROOF_PROPERTY = "proof" ; param options returns { GetProofsResult } export const getProofs = async ( options ) : Promise < GetProofsResult > => { const { proofType , skipProofCompaction , documentLoader , expansionMap } = options ; let { document } = options ; let proofs ; if ( ! skipProofCompaction ) { document = await jsonld . compact ( document , SECURITY_CONTEXT_URL , { documentLoader , expansionMap , compactToRelative : false } ) ; } proofs = jsonld . getValues ( document , PROOF_PROPERTY ) ; delete document [ PROOF_PROPERTY ] ; if ( typeof proofType === "string" ) { proofs = proofs . filter ( ( _ ) => _ . type == proofType ) ; } if ( Array . isArray ( proofType ) ) { proofs = proofs . filter ( ( _ ) => proofType . includes ( _ . type ) ) ; } proofs = proofs . map ( ( matchedProof ) => ( { "@context" : SECURITY_CONTEXT_URL , ... matchedProof } ) ) ; return { proofs , document } ; } ; param date { number | string } returns { string } export const w3cDate = ( date ? ) : string => { let result = new Date ( ) ; if ( typeof date === "number" || typeof date === "string" ) { result = new Date ( date ) ; } const str = result . toISOString ( ) ; return str . substr ( 0 , str . length - 5 ) + "Z" ; } ; param document { any } param options { GetTypeInfoOptions } returns { object } export const getTypeInfo = async ( document , options ) : Promise < any > => { const { documentLoader , expansionMap } = options ; const context = jsonld . getValues ( document , "@context" ) ; const compacted = await jsonld . compact ( { "@type" : "_:b0" } , context , { documentLoader , expansionMap } ) ; delete compacted [ "@context" ] ; const alias = Object . keys ( compacted ) [ 0 ] ; let toExpand = { "@context" : context } ; toExpand [ "@type" ] = jsonld . getValues ( document , "@type" ) . concat ( jsonld . getValues ( document , alias ) ) ; const expanded = ( await jsonld . expand ( toExpand , { documentLoader , expansionMap } ) ) [ 0 ] || { } ; return { types : jsonld . getValues ( expanded , "@type" ) , alias } ; } ;