forked from digitalbazaar/vc-revocation-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
218 lines (198 loc) · 6.14 KB
/
main.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
/*!
* Copyright (c) 2020 Digital Bazaar, Inc. All rights reserved.
*/
import RevocationList from './RevocationList.js';
import vc from 'vc-js';
export {default as Bitstring} from './Bitstring.js';
const CONTEXTS = {
VC_V1: 'https://www.w3.org/2018/credentials/v1',
RL_V1: 'https://w3id.org/vc-revocation-list-2020/v1'
};
export async function createList({length}) {
return new RevocationList({length});
}
export async function decodeList({encodedList}) {
return RevocationList.decode({encodedList});
}
export async function createCredential({id, list}) {
const encodedList = await list.encode();
return {
'@context': [CONTEXTS.VC_V1, CONTEXTS.RL_V1],
id,
type: ['VerifiableCredential', 'RevocationList2020Credential'],
credentialSubject: {
id: `${id}#list`,
type: 'RevocationList2020',
encodedList
}
};
}
export async function checkStatus({
credential, documentLoader, suite, verifyRevocationListCredential = true
} = {}) {
let result;
try {
result = await _checkStatus({
credential, documentLoader, suite, verifyRevocationListCredential
});
} catch(error) {
result = {
verified: false,
error,
};
}
return result;
}
export function statusTypeMatches({credential} = {}) {
if(!(credential && typeof credential === 'object')) {
throw new TypeError('"credential" must be an object.');
}
// check for expected contexts
const {'@context': contexts} = credential;
if(!Array.isArray(contexts)) {
throw new TypeError('"@context" must be an array.');
}
if(contexts[0] !== CONTEXTS.VC_V1) {
throw new Error(`The first "@context" value must be "${CONTEXTS.VC_V1}".`);
}
const {credentialStatus} = credential;
if(!credentialStatus) {
// no status; no match
return false;
}
if(typeof credentialStatus !== 'object') {
// bad status
throw new Error('"credentialStatus" is invalid.');
}
if(!contexts.includes(CONTEXTS.RL_V1)) {
// context not present, no match
return false;
}
if(credentialStatus.type !== 'RevocationList2020Status') {
// status type does not match
return false;
}
return true;
}
export function assertRevocationList2020Context({credential} = {}) {
if(!(credential && typeof credential === 'object')) {
throw new TypeError('"credential" must be an object.');
}
// check for expected contexts
const {'@context': contexts} = credential;
if(!Array.isArray(contexts)) {
throw new TypeError('"@context" must be an array.');
}
if(contexts[0] !== CONTEXTS.VC_V1) {
throw new Error(`The first "@context" value must be "${CONTEXTS.VC_V1}".`);
}
if(!contexts.includes(CONTEXTS.RL_V1)) {
throw new TypeError(`"@context" must include "${CONTEXTS.RL_V1}".`);
}
}
export function getCredentialStatus({credential} = {}) {
if(!(credential && typeof credential === 'object')) {
throw new TypeError('"credential" must be an object.');
}
assertRevocationList2020Context({credential});
// get and validate status
if(!(credential.credentialStatus &&
typeof credential.credentialStatus === 'object')) {
throw new Error('"credentialStatus" is missing or invalid.');
}
const {credentialStatus} = credential;
if(credentialStatus.type !== 'RevocationList2020Status') {
throw new Error(
'"credentialStatus" type is not "RevocationList2020Status".');
}
if(typeof credentialStatus.revocationListCredential !== 'string') {
throw new TypeError(
'"credentialStatus" revocationListCredential must be a string.');
}
return credentialStatus;
}
async function _checkStatus({
credential, documentLoader, suite, verifyRevocationListCredential
}) {
if(!(credential && typeof credential === 'object')) {
throw new TypeError('"credential" must be an object.');
}
if(typeof documentLoader !== 'function') {
throw new TypeError('"documentLoader" must be a function.');
}
if(verifyRevocationListCredential && !(suite && (
isArrayOfObjects(suite) ||
(!Array.isArray(suite) && typeof suite === 'object')))) {
throw new TypeError('"suite" must be an object or an array of objects.');
}
const credentialStatus = getCredentialStatus({credential});
// get RL position
// TODO: bikeshed name
const {revocationListIndex} = credentialStatus;
const index = parseInt(revocationListIndex, 10);
if(isNaN(index)) {
throw new TypeError('"revocationListIndex" must be an integer.');
}
// retrieve RL VC
let rlCredential;
try {
({document: rlCredential} = await documentLoader(
credentialStatus.revocationListCredential));
} catch(e) {
const err = new Error(
'Could not load "RevocationList2020Credential"; ' +
`reason: ${e.message}`);
err.cause = e;
throw err;
}
// verify RL VC
if(verifyRevocationListCredential) {
const verifyResult = await vc.verifyCredential({
credential: rlCredential,
suite,
documentLoader
});
if(!verifyResult.verified) {
const {error: e} = verifyResult;
let msg = '"RevocationList2020Credential" not verified';
if(e) {
msg += `; reason: ${e.message}`;
} else {
msg += '.';
}
const err = new Error(msg);
if(e) {
err.cause = verifyResult.error;
}
throw err;
}
}
if(!rlCredential.type.includes('RevocationList2020Credential')) {
throw new Error('"RevocationList2020Credential" type is not valid.');
}
// get JSON RevocationList
const {credentialSubject: rl} = rlCredential;
if(rl.type !== 'RevocationList2020') {
throw new Error('"RevocationList2020" type is not valid.');
}
// decode list from RL VC
const {encodedList} = rl;
let list;
try {
list = await decodeList({encodedList});
} catch(e) {
const err = new Error(
`Could not decode encoded revocation list; reason: ${e.message}`);
err.cause = e;
throw err;
}
// check VC's RL index for revocation status
const verified = !list.isRevoked(index);
// TODO: return anything else? returning `rlCredential` may be too unwieldy
// given its potentially large size
return {verified};
}
function isArrayOfObjects(x) {
return Array.isArray(x) && x.length > 0 &&
x.every(x => x && typeof x === 'object');
}