-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpresentations.js
321 lines (285 loc) · 9.98 KB
/
presentations.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import jsonld from 'jsonld';
import jsigs from 'jsonld-signatures';
import {
BBSPlusPublicKeyG2,
BBSPublicKey,
PSPublicKey,
} from '@docknetwork/crypto-wasm-ts';
import { Presentation } from '@docknetwork/crypto-wasm-ts/lib/anonymous-credentials/presentation';
import b58 from 'bs58';
import { verifyCredential } from './credentials';
import DIDResolver from "../../resolver/did/did-resolver"; // eslint-disable-line
import defaultDocumentLoader from './document-loader';
import { getSuiteFromKeyDoc } from './helpers';
import {
Bls12381BBSSigDockSigName,
Bls12381PSSigDockSigName,
Bls12381BBS23SigDockSigName,
Bls12381BBSDockVerKeyName,
Bls12381PSDockVerKeyName,
Bls12381BBS23DockVerKeyName,
} from './crypto/constants';
import { DEFAULT_CONTEXT_V1_URL } from './constants';
import {
EcdsaSepc256k1Signature2019,
Ed25519Signature2018,
Sr25519Signature2020,
JsonWebSignature2020,
Bls12381BBSSignatureDock2022,
Bls12381BBSSignatureDock2023,
Bls12381PSSignatureDock2023,
} from './custom_crypto';
const { AuthenticationProofPurpose } = jsigs.purposes;
/**
* @typedef {object} VerifiablePresentation Representation of a Verifiable Presentation.
*/
/**
* @param {object} presentation - An object that could be a presentation.
* @throws {Error}
* @private
*/
function checkPresentation(presentation) {
// Normalize to an array to allow the common case of context being a string
const context = Array.isArray(presentation['@context'])
? presentation['@context']
: [presentation['@context']];
// Ensure first context is 'https://www.w3.org/2018/credentials/v1'
if (context[0] !== DEFAULT_CONTEXT_V1_URL) {
throw new Error(
`"${DEFAULT_CONTEXT_V1_URL}" needs to be first in the `
+ 'list of contexts.',
);
}
// Ensure VerifiablePresentation exists in types
const types = jsonld.getValues(presentation, 'type');
if (!types.includes('VerifiablePresentation')) {
throw new Error('"type" must include "VerifiablePresentation".');
}
}
export async function verifyPresentationCredentials(
presentation,
options = {},
) {
let verified = true;
let credentialResults = [];
// Get presentation credentials
const credentials = jsonld.getValues(presentation, 'verifiableCredential');
if (credentials.length > 0) {
// Verify all credentials in list
credentialResults = await Promise.all(
credentials.map((credential) => verifyCredential(credential, { ...options })),
);
// Assign credentialId property to all credential results
for (const [i, credentialResult] of credentialResults.entries()) {
credentialResult.credentialId = credentials[i].id;
}
// Check all credentials passed verification
const allCredentialsVerified = credentialResults.every((r) => r.verified);
if (!allCredentialsVerified) {
verified = false;
}
}
return {
verified,
credentialResults,
};
}
/**
* @typedef {object} VerifiableParams The Options to verify credentials and presentations.
* @property {string} [challenge] - proof challenge Required.
* @property {string} [domain] - proof domain (optional)
* @property {string} [controller] - controller (optional)
* @property {DIDResolver} [resolver] - Resolver to resolve the issuer DID (optional)
* @property {Boolean} [unsignedPresentation] - Whether to verify the proof or not
* @property {Boolean} [compactProof] - Whether to compact the JSON-LD or not.
* @property {object} [presentationPurpose] - A purpose other than the default AuthenticationProofPurpose
* @property {object} [documentLoader] - A document loader, can be null and use the default
*/
/**
* Verify a Verifiable Presentation. Returns the verification status and error in an object
* @param {object} presentation The verifiable presentation
* @param {VerifiableParams} options Verify parameters, this object is passed down to jsonld-signatures calls
* @return {Promise<object>} verification result. The returned object will have a key `verified` which is true if the
* presentation is valid and all the credentials are valid and not revoked and false otherwise. The `error` will
* describe the error if any.
*/
export async function verifyPresentation(presentation, options = {}) {
if (options.documentLoader && options.resolver) {
throw new Error(
'Passing resolver and documentLoader results in resolver being ignored, please re-factor.',
);
}
// Ensure presentation is passed
if (!presentation) {
throw new TypeError('"presentation" property is required');
}
if (isAnoncreds(presentation)) {
return verifyAnoncreds(presentation, options);
}
// Ensure presentation is valid
checkPresentation(presentation);
// Extract parameters
const {
challenge,
domain,
resolver,
unsignedPresentation = false,
presentationPurpose,
controller,
suite = [],
} = options;
// Build verification options
const verificationOptions = {
documentLoader: options.documentLoader || defaultDocumentLoader(resolver),
...options,
resolver: null,
suite: [
new Ed25519Signature2018(),
new EcdsaSepc256k1Signature2019(),
new Sr25519Signature2020(),
new JsonWebSignature2020(),
...suite,
],
};
// TODO: verify proof then credentials
const { verified, credentialResults } = await verifyPresentationCredentials(
presentation,
verificationOptions,
);
try {
// Skip proof validation for unsigned
if (unsignedPresentation) {
return { verified, results: [presentation], credentialResults };
}
// Early out incase credentials arent verified
if (!verified) {
return { verified, results: [presentation], credentialResults };
}
// Get proof purpose
if (!presentationPurpose && !challenge) {
throw new Error(
'A "challenge" param is required for AuthenticationProofPurpose.',
);
}
// Set purpose and verify
const purpose = presentationPurpose
|| new AuthenticationProofPurpose({ controller, domain, challenge });
const presentationResult = await jsigs.verify(presentation, {
purpose,
...verificationOptions,
});
// Return results
return {
presentationResult,
credentialResults,
verified: verified && presentationResult.verified,
error: presentationResult.error,
};
} catch (error) {
// Error occured when verifying presentation, catch and return error
return {
verified: false,
results: [{ verified: false, error }],
error,
};
}
}
/**
* Sign a Verifiable Presentation
* @param {object} presentation - the one to be signed
* @param {object} keyDoc - key document containing `id`, `controller`, `type`, `privateKeyBase58` and `publicKeyBase58`
* @param {string} challenge - proof challenge Required.
* @param {string} domain - proof domain (optional)
* @param {DIDResolver} [resolver] - Resolver for DIDs.
* @param {Boolean} [compactProof] - Whether to compact the JSON-LD or not.
* @param {object} [presentationPurpose] - Optional presentation purpose to override default AuthenticationProofPurpose
* @return {Promise<VerifiablePresentation>} A VerifiablePresentation with a proof.
*/
export async function signPresentation(
presentation,
keyDoc,
challenge,
domain,
resolver = null,
compactProof = true,
presentationPurpose = null,
addSuiteContext = true,
) {
const suite = await getSuiteFromKeyDoc(keyDoc);
const purpose = presentationPurpose
|| new AuthenticationProofPurpose({
domain,
challenge,
});
const documentLoader = defaultDocumentLoader(resolver);
return jsigs.sign(presentation, {
purpose,
documentLoader,
domain,
challenge,
compactProof,
suite,
addSuiteContext,
});
}
export function isAnoncreds(presentation) {
// Since there is no type parameter present we have to guess by checking field types
// these wont exist in a standard VP
return (
typeof presentation.version === 'string'
&& typeof presentation.proof === 'string'
&& typeof presentation.spec !== 'undefined'
&& typeof presentation.spec.credentials !== 'undefined'
);
}
export async function verifyAnoncreds(presentation, options = {}) {
const documentLoader = options.documentLoader || defaultDocumentLoader(options.resolver);
const {
predicateParams, accumulatorPublicKeys,
circomOutputs, blindedAttributesCircomOutputs,
} = options;
const keyDocuments = await Promise.all(
presentation.spec.credentials.map((c, idx) => {
const { proof } = c.revealedAttributes;
if (!proof) {
throw new Error(
`Presentation credential does not reveal its proof for index ${idx}`,
);
}
let sigClass;
switch (proof.type) {
case Bls12381BBSSigDockSigName:
sigClass = Bls12381BBSSignatureDock2022;
break;
case Bls12381BBS23SigDockSigName:
sigClass = Bls12381BBSSignatureDock2023;
break;
case Bls12381PSSigDockSigName:
sigClass = Bls12381PSSignatureDock2023;
break;
default:
throw new Error(`Invalid proof type ${proof.type}`);
}
return sigClass.getVerificationMethod({ proof, documentLoader });
}),
);
const recreatedPres = Presentation.fromJSON(presentation);
const pks = keyDocuments.map((keyDocument) => {
const pkRaw = b58.decode(keyDocument.publicKeyBase58);
if (!keyDocument.type) {
throw new Error(`No type provided for key document ${JSON.stringify(keyDocument)}`);
}
const keyType = keyDocument.type.startsWith('did:') ? keyDocument.type.slice(4) : keyDocument.type;
switch (keyType) {
case Bls12381BBSDockVerKeyName:
return new BBSPlusPublicKeyG2(pkRaw);
case Bls12381BBS23DockVerKeyName:
return new BBSPublicKey(pkRaw);
case Bls12381PSDockVerKeyName:
return new PSPublicKey(pkRaw);
default:
throw new Error(`Invalid key document type: ${keyType}`);
}
});
return recreatedPres.verify(pks, accumulatorPublicKeys, predicateParams, circomOutputs, blindedAttributesCircomOutputs);
}