diff --git a/frontend/src/utils.js b/frontend/src/utils.js index f61112b..e587a8e 100644 --- a/frontend/src/utils.js +++ b/frontend/src/utils.js @@ -1,109 +1,122 @@ -import jsonld from 'jsonld'; -import { demoAuthPresentation } from './store/demoAuth'; -import { decodeProtectedHeader } from "jose"; +import jsonld from 'jsonld' +import { demoAuthPresentation } from './store/demoAuth' +import { decodeProtectedHeader, decodeJwt } from 'jose' export const VerifiableType = { - CREDENTIAL: 'VerifiableCredential', - PRESENTATION: 'VerifiablePresentation' -}; + CREDENTIAL: 'VerifiableCredential', + PRESENTATION: 'VerifiablePresentation', +} const IPFS_GATEWAYS = ['ipfs.io', 'ipfs.ssi.eecc.de'] export function isURL(url) { - if (typeof url != 'string') return false; - return url.startsWith('https://'); + if (typeof url != 'string') return false + return url.startsWith('https://') } export function getCredentialValue(value) { - return typeof value === 'object' ? value.value || value['@value'] || JSON.stringify(value, null, 2) : value; + return typeof value === 'object' + ? value.value || value['@value'] || JSON.stringify(value, null, 2) + : value } export function getPlainCredential(credential) { - var clean_credential = { ...credential }; - delete clean_credential.verified; - delete clean_credential.revoked; - delete clean_credential.suspended; - delete clean_credential.status; - delete clean_credential.presentation; - delete clean_credential.context; - return clean_credential; + var clean_credential = { ...credential } + delete clean_credential.verified + delete clean_credential.revoked + delete clean_credential.suspended + delete clean_credential.status + delete clean_credential.presentation + delete clean_credential.context + return clean_credential } export function getVerifiableType(verifiable) { - if (verifiable.type.includes(VerifiableType.PRESENTATION)) return VerifiableType.PRESENTATION; - return VerifiableType.CREDENTIAL; + if (verifiable.type.includes(VerifiableType.PRESENTATION)) + return VerifiableType.PRESENTATION + return VerifiableType.CREDENTIAL } export function getCredentialType(credential) { - return credential.type.length > 1 ? credential.type.filter((c) => c != 'VerifiableCredential')[0] : credential.type[0]; + return credential.type.length > 1 + ? credential.type.filter((c) => c != 'VerifiableCredential')[0] + : credential.type[0] } export function getHolder(presentation) { - if (presentation.holder) return presentation.holder; - const proof = Array.isArray(presentation.proof) ? presentation.proof[0] : presentation.proof - return proof.verificationMethod.split('#')[0]; + if (presentation.holder) return presentation.holder + const proof = Array.isArray(presentation.proof) + ? presentation.proof[0] + : presentation.proof + return proof.verificationMethod.split('#')[0] } export function SDJWTtoVP(jwt) { - console.log(jwt) - const decodedHeader = decodeProtectedHeader(jwt) - //const decodedJWT = decodeJwt(jwt) - console.log(decodedHeader) - //console.log(decodedJWT) - return { - jwt, - type: [VerifiableType.PRESENTATION], - holder: decodedHeader.kid.split('#')[0] - } + console.log(jwt) + const decodedHeader = decodeProtectedHeader(jwt) + const decodedJWT = decodeJwt(jwt) + console.log(decodedHeader) + console.log(decodedJWT) + + if (!decodedHeader.kid) { + throw new Error('Missing kid in JWT header') + } + + return { + jwt, + type: [VerifiableType.PRESENTATION], + holder: decodedHeader.kid.split('#')[0], + issuer: decodedJWT.iss, + } } export async function fetchIPFS(IPFSUrl) { - - var document; - - await Promise.any(IPFS_GATEWAYS.map(async (gateway) => { - - return await fetch(`https://${gateway}/ipfs/${IPFSUrl.split('ipfs://')[1]}`); - - })) - .then((result) => { - - document = result; - - }) - .catch((error) => { - console.error(error) - }) - - if (!document) throw Error('Fetching from IPFS failed'); - - return document; - + var document + + await Promise.any( + IPFS_GATEWAYS.map(async (gateway) => { + return await fetch( + `https://${gateway}/ipfs/${IPFSUrl.split('ipfs://')[1]}`, + ) + }), + ) + .then((result) => { + document = result + }) + .catch((error) => { + console.error(error) + }) + + if (!document) throw Error('Fetching from IPFS failed') + + return document } - const documentLoader = async (url) => { - let document; - if (url.startsWith('ipfs://')) { - - document = await fetchIPFS(url) - - } else document = await fetch(url); - - return { - contextUrl: null, - document: await document.json(), - documentUrl: url - }; -}; - + let document + if (url.startsWith('ipfs://')) { + document = await fetchIPFS(url) + } else document = await fetch(url) + + return { + contextUrl: null, + document: await document.json(), + documentUrl: url, + } +} export async function getContext(credential) { - const resolved = await jsonld.processContext(await jsonld.processContext(null, null), credential, { documentLoader }); - return resolved.mappings; + const resolved = await jsonld.processContext( + await jsonld.processContext(null, null), + credential, + { documentLoader }, + ) + return resolved.mappings } export function isDemoAuth(auth) { - return auth != undefined && JSON.stringify(auth) == JSON.stringify(demoAuthPresentation); + return ( + auth != undefined && + JSON.stringify(auth) == JSON.stringify(demoAuthPresentation) + ) } -