-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: wadeking98 <[email protected]>
- Loading branch information
1 parent
6c7a4b3
commit bf5cf8a
Showing
7 changed files
with
265 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,36 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
import { ProofRequestTemplate } from '../../verifier' | ||
import { useConfiguration } from '../contexts/configuration' | ||
import { useStore } from '../contexts/store' | ||
import { applyTemplateMarkers, useRemoteProofBundleResolver } from '../utils/proofBundle' | ||
|
||
export const useTemplates = (): Array<ProofRequestTemplate> => { | ||
const [store] = useStore() | ||
const { proofRequestTemplates } = useConfiguration() | ||
return (proofRequestTemplates && proofRequestTemplates(store.preferences.acceptDevCredentials)) || [] | ||
const [proofRequestTemplates, setProofRequestTemplates] = useState<ProofRequestTemplate[]>([]) | ||
const { proofTemplateBaseUrl } = useConfiguration() | ||
const resolver = useRemoteProofBundleResolver(proofTemplateBaseUrl) | ||
useEffect(() => { | ||
resolver.resolve(store.preferences.acceptDevCredentials).then((templates) => { | ||
if (templates) { | ||
setProofRequestTemplates(applyTemplateMarkers(templates)) | ||
} | ||
}) | ||
}, []) | ||
return proofRequestTemplates | ||
} | ||
|
||
export const useTemplate = (templateId: string): ProofRequestTemplate | undefined => { | ||
const { proofRequestTemplates } = useConfiguration() | ||
const [store] = useStore() | ||
return ( | ||
proofRequestTemplates && | ||
proofRequestTemplates(store.preferences.acceptDevCredentials).find((template) => template.id === templateId) | ||
) | ||
const [proofRequestTemplate, setProofRequestTemplate] = useState<ProofRequestTemplate | undefined>(undefined) | ||
const { proofTemplateBaseUrl } = useConfiguration() | ||
const resolver = useRemoteProofBundleResolver(proofTemplateBaseUrl) | ||
useEffect(() => { | ||
resolver.resolveById(templateId, store.preferences.acceptDevCredentials).then((template) => { | ||
if (template) { | ||
setProofRequestTemplate(applyTemplateMarkers(template)) | ||
} | ||
}) | ||
}, []) | ||
return proofRequestTemplate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import axios from 'axios' | ||
|
||
import { AnonCredsProofRequestTemplatePayload, ProofRequestTemplate, useProofRequestTemplates } from '../../verifier' | ||
import { useConfiguration } from '../contexts/configuration' | ||
|
||
const calculatePreviousYear = (yearOffset: number) => { | ||
const pastDate = new Date() | ||
pastDate.setFullYear(pastDate.getFullYear() + yearOffset) | ||
return parseInt(pastDate.toISOString().split('T')[0].replace(/-/g, '')) | ||
} | ||
|
||
export const applyTemplateMarkers = (templates: any): any => { | ||
if (!templates) return templates | ||
const markerActions: { [key: string]: (param: string) => string } = { | ||
now: () => Math.floor(new Date().getTime() / 1000).toString(), | ||
currentDate: (offset: string) => calculatePreviousYear(parseInt(offset)).toString(), | ||
} | ||
let templateString = JSON.stringify(templates) | ||
// regex to find all markers in the template so we can replace them with computed values | ||
const markers = [...templateString.matchAll(/"@\{(\w+)(?:\((\S*)\))?\}"/gm)] | ||
|
||
markers.forEach((marker) => { | ||
const markerValue = markerActions[marker[1] as string](marker[2]) | ||
templateString = templateString.replace(marker[0], markerValue) | ||
}) | ||
return JSON.parse(templateString) | ||
} | ||
|
||
export const applyDevRestrictions = (templates: ProofRequestTemplate[]): ProofRequestTemplate[] => { | ||
return templates.map((temp) => { | ||
return { | ||
...temp, | ||
payload: { | ||
...temp.payload, | ||
data: (temp.payload as AnonCredsProofRequestTemplatePayload).data.map((data) => { | ||
return { | ||
...data, | ||
requestedAttributes: data.requestedAttributes?.map((attr) => { | ||
return { | ||
...attr, | ||
restrictions: [...(attr.restrictions ?? []), ...(attr.devRestrictions ?? [])], | ||
devRestrictions: [], | ||
} | ||
}), | ||
requestedPredicates: data.requestedPredicates?.map((pred) => { | ||
return { | ||
...pred, | ||
restrictions: [...(pred.restrictions ?? []), ...(pred.devRestrictions ?? [])], | ||
devRestrictions: [], | ||
} | ||
}), | ||
} | ||
}), | ||
}, | ||
} | ||
}) | ||
} | ||
|
||
export interface ProofBundleResolverType { | ||
resolve: (acceptDevRestrictions: boolean) => Promise<ProofRequestTemplate[] | undefined> | ||
resolveById: (templateId: string, acceptDevRestrictions: boolean) => Promise<ProofRequestTemplate | undefined> | ||
} | ||
|
||
export const useRemoteProofBundleResolver = (indexFileBaseUrl: string | undefined): ProofBundleResolverType => { | ||
if (indexFileBaseUrl) { | ||
return new RemoteProofBundleResolver(indexFileBaseUrl) | ||
} else { | ||
return new DefaultProofBundleResolver() | ||
} | ||
} | ||
|
||
export class RemoteProofBundleResolver implements ProofBundleResolverType { | ||
private remoteServer | ||
private templateData: ProofRequestTemplate[] | undefined | ||
|
||
public constructor(indexFileBaseUrl: string) { | ||
this.remoteServer = axios.create({ | ||
baseURL: indexFileBaseUrl, | ||
}) | ||
} | ||
public async resolve(acceptDevRestrictions: boolean): Promise<ProofRequestTemplate[] | undefined> { | ||
if (this.templateData) { | ||
let templateData = this.templateData | ||
if (acceptDevRestrictions) { | ||
templateData = applyDevRestrictions(templateData) | ||
} | ||
return Promise.resolve(templateData) | ||
} | ||
return this.remoteServer.get('proof-templates.json').then((response) => { | ||
try { | ||
let templateData: ProofRequestTemplate[] = response.data | ||
this.templateData = templateData | ||
if (acceptDevRestrictions) { | ||
templateData = applyDevRestrictions(templateData) | ||
} | ||
return templateData | ||
} catch (error) { | ||
return undefined | ||
} | ||
}) | ||
} | ||
public async resolveById( | ||
templateId: string, | ||
acceptDevRestrictions: boolean | ||
): Promise<ProofRequestTemplate | undefined> { | ||
if (!this.templateData) { | ||
return (await this.resolve(acceptDevRestrictions))?.find((template) => template.id === templateId) | ||
} else { | ||
let templateData = this.templateData | ||
if (acceptDevRestrictions) { | ||
templateData = applyDevRestrictions(templateData) | ||
} | ||
const template = templateData.find((template) => template.id === templateId) | ||
return template | ||
} | ||
} | ||
} | ||
|
||
export class DefaultProofBundleResolver implements ProofBundleResolverType { | ||
private proofRequestTemplates | ||
public constructor() { | ||
const { proofRequestTemplates } = useConfiguration() | ||
this.proofRequestTemplates = proofRequestTemplates ?? useProofRequestTemplates | ||
} | ||
public async resolve(acceptDevRestrictions: boolean): Promise<ProofRequestTemplate[]> { | ||
return Promise.resolve(this.proofRequestTemplates(acceptDevRestrictions)) | ||
} | ||
public async resolveById( | ||
templateId: string, | ||
acceptDevRestrictions: boolean | ||
): Promise<ProofRequestTemplate | undefined> { | ||
return Promise.resolve( | ||
this.proofRequestTemplates(acceptDevRestrictions).find((template) => template.id === templateId) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.