Repository navigation
Expand file tree
/
Copy pathvcCore.ts
More file actions
85 lines (69 loc) · 3.15 KB
/
Copy pathvcCore.ts
File metadata and controls
85 lines (69 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { VCDIVerifiableCredential } from "@digitalcredentials/vc-data-model/dist/VerifiableCredential";
// @ts-ignore
import { JsonLdDocumentLoader } from 'jsonld-document-loader';
import {CredentialSubject} from "@digitalcredentials/vc-data-model/dist/VerifiableCredential";
import { createDocumentLoader, getAuthenticatedFetch, parseToJsonLD } from './utils/index.js';
import {
addControllerDocumentToCard,
addFileToContainer,
createControllerDocument,
createCredential,
createKey,
exportKeypair,
fetchKeypairFromSolidPod,
getKeyControllerDocumentUrl,
sign,
storeKeypairOnSolidPod,
verify
} from "./controller/index.js";
export async function coreGenerateKeyPair(webId: string, password: string) {
return await createKey(webId, password)
}
export async function coreSetup(email: string, password: string, css: string, webId: string, options?: any) {
const key = await createKey(webId, password)
// Create auth fetch func, unless one is supplied in the options
const authFetch = options?.authFetch || await getAuthenticatedFetch(email, password, css, webId)
// Store private keypair (secret & public key) on Solid Pod
await storeKeypairOnSolidPod(key, webId, authFetch)
// Add key description to Solid WebID Profile Document
const publicKeyExport = await exportKeypair(key, {publicKey:true, includeContext:true})
// Upload (public) keyexport to Solid Pod
await addFileToContainer(
authFetch,
getKeyControllerDocumentUrl(webId),
Buffer.from(JSON.stringify(publicKeyExport,null,2)),
'application/json',
'key',
{append: false, controlRead: false, controlWrite: false, write: false, read:true}
)
// Link WebID Profile Document to (public) key resource
const controllerDoc = createControllerDocument(publicKeyExport)
await addControllerDocumentToCard(authFetch, webId, controllerDoc)
}
export async function coreIssue(email: string, password: string, css: string, webId: string, jdl: JsonLdDocumentLoader, contentType: string, data: string, options?: any) {
let parsedData = await parseToJsonLD(data, contentType)
// Create auth fetch func, unless one is supplied in the options
const authFetch = options?.authFetch || await getAuthenticatedFetch(email, password, css, webId)
// Fetch keypair from Solid Pod
const kp = await fetchKeypairFromSolidPod(webId, authFetch)
// Create credential
const credentialSubject = parsedData as CredentialSubject
const credential = createCredential(
kp,
credentialSubject
)
const signedCredential = await sign({
key: kp,
credential,
documentLoader: createDocumentLoader(jdl)
})
return { contentType: 'application/ld+json', signedCredentialText: JSON.stringify(signedCredential) };
}
export async function coreVerify(jdl: JsonLdDocumentLoader, verifiableCredential: VCDIVerifiableCredential) {
const documentLoader = createDocumentLoader(jdl)
const {validationResult, verificationResult} = await verify({
credential: verifiableCredential,
documentLoader
})
return { validationResult, verificationResult };
};