Repository navigation
Expand file tree
/
Copy pathapi.ts
More file actions
114 lines (99 loc) · 4.74 KB
/
Copy pathapi.ts
File metadata and controls
114 lines (99 loc) · 4.74 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { ContentType, Document, VerificationResult, BasicConfiguration, IIssuer, IVerifier } from "../../api-spec.js";
import { JsonLdDocumentLoaderWrapper } from "../../utils/jsonld-document-loader-wrapper.js";
// @ts-ignore
import { JsonLdDocumentLoader } from 'jsonld-document-loader';
// @ts-ignore
import dataIntegrityContext from '@digitalbazaar/data-integrity-context';
// @ts-ignore
import cred from 'credentials-context';
import { coreSetup, coreIssue, coreVerify } from "./vcCore.js";
export type SolidConfiguration = BasicConfiguration & {
email: string;
password: string;
css: string;
webId: string;
authFetch?: any;
};
const dummyPublicKey = {
value: "dummyPublicKey"
};
export class SolidApi implements IIssuer, IVerifier {
readonly config: SolidConfiguration;
jdlWrapper: JsonLdDocumentLoaderWrapper | null;
constructor(config: SolidConfiguration) {
if (!config.email || !config.password || !config.css || !config.webId) {
throw new Error("Missing required configuration parameters for SolidApi");
}
this.config = config;
this.jdlWrapper = null;
}
private async getPreloadedJdl(): Promise<JsonLdDocumentLoader> {
const { contexts: credentialsContexts, constants: { CREDENTIALS_CONTEXT_V1_URL } } =
cred;
if (!this.jdlWrapper) {
this.jdlWrapper = new JsonLdDocumentLoaderWrapper();
// preload from config if any
if (this.config.contentsToPreload) {
await this.jdlWrapper.addStaticFromArray(this.config.contentsToPreload);
}
// preload standard contexts needed for this implementation: fallback if not already preloaded from config
if (!this.jdlWrapper.isAdded(CREDENTIALS_CONTEXT_V1_URL)) {
await this.jdlWrapper.addStatic({
url: CREDENTIALS_CONTEXT_V1_URL,
type: 'context',
document: credentialsContexts.get(CREDENTIALS_CONTEXT_V1_URL)
});
}
if (!this.jdlWrapper.isAdded(dataIntegrityContext.constants.CONTEXT_URL)) {
await this.jdlWrapper.addStatic({
url: dataIntegrityContext.constants.CONTEXT_URL,
type: 'context',
document: dataIntegrityContext.contexts.get(dataIntegrityContext.constants.CONTEXT_URL)
});
}
}
return this.jdlWrapper.getJdl();
}
// ----- IIssuer methods -----
async setup(): Promise<void> {
await coreSetup(this.config.email, this.config.password, this.config.css, this.config.webId, { authFetch: this.config.authFetch });
}
async issue(document: Document): Promise<Document> {
if (!document || !document.contentType || !document.content) {
throw new Error("Invalid unsigned document provided for issuing");
}
const issued = await coreIssue(this.config.email, this.config.password, this.config.css, this.config.webId, await this.getPreloadedJdl(), document.contentType, document.content, { authFetch: this.config.authFetch });
return {
contentType: issued.contentType as ContentType,
content: issued.signedCredentialText
};
}
// ----- IVerifier methods -----
async retrievePublicKey(): Promise<any> {
// no need to retrieve a public key in this implementation, we just return a dummy value
// note that the public key is not actually used in coreVerify() because the key is embedded in the signed credential
return dummyPublicKey;
}
async verify(document: Document, publicKey: any): Promise<VerificationResult> {
if (!document || !document.contentType || !document.content) {
throw new Error("Invalid signed document provided for verification");
}
// publicKey param is not used in coreVerify(), this is a pro forma consistency check
if (!publicKey || !publicKey.value || publicKey.value !== dummyPublicKey.value) {
throw new Error("Invalid public key provided for verification");
}
switch (document.contentType) {
case 'application/ld+json':
case 'application/json':
break;
default:
return 'unsupported content type';
}
const verifiableCredential = JSON.parse(document.content);
if (!verifiableCredential) {
throw new Error("Invalid signed document provided for verification");
}
const { validationResult, verificationResult } = await coreVerify(await this.getPreloadedJdl(), verifiableCredential);
return validationResult.valid ? (verificationResult.verified ? 'pass' : 'fail') : 'not signed';
}
}