-
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.
added support for remote proof bundles
Signed-off-by: wadeking98 <[email protected]>
- Loading branch information
1 parent
374b616
commit 942b833
Showing
5 changed files
with
211 additions
and
22 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
165 changes: 157 additions & 8 deletions
165
packages/legacy/core/App/hooks/proof-request-templates.ts
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,167 @@ | ||
import { ProofRequestTemplate } from '../../verifier' | ||
import axios from 'axios' | ||
import { useEffect, useState } from 'react' | ||
|
||
import { AnonCredsProofRequestTemplatePayload, ProofRequestTemplate, useProofRequestTemplates } from '../../verifier' | ||
import { useConfiguration } from '../contexts/configuration' | ||
import { useStore } from '../contexts/store' | ||
|
||
export interface ProofBundleResolverType { | ||
resolve: (acceptDevRestrictions: boolean) => Promise<ProofRequestTemplate[] | undefined> | ||
resolveById: (templateId: string, acceptDevRestrictions: boolean) => Promise<ProofRequestTemplate | undefined> | ||
} | ||
|
||
const useRemoteProofBundleResolver = (indexFileBaseUrl: string | undefined): ProofBundleResolverType => { | ||
if (indexFileBaseUrl) { | ||
return new RemoteProofBundleResolver(indexFileBaseUrl) | ||
} else { | ||
return new DefaultProofBundleResolver() | ||
} | ||
} | ||
|
||
const calculatePreviousYear = (yearOffset: number) => { | ||
const pastDate = new Date() | ||
pastDate.setFullYear(pastDate.getFullYear() + yearOffset) | ||
return parseInt(pastDate.toISOString().split('T')[0].replace(/-/g, '')) | ||
} | ||
|
||
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) | ||
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) | ||
} | ||
|
||
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 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) | ||
) | ||
} | ||
} | ||
|
||
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