-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcredential.ts
More file actions
308 lines (278 loc) · 9.13 KB
/
Copy pathcredential.ts
File metadata and controls
308 lines (278 loc) · 9.13 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
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
import semver from 'semver/preload';
import { CredentialSchema } from './schema';
import {
BBS_CRED_PROOF_TYPE,
BBS_PLUS_CRED_PROOF_TYPE,
BBS_PLUS_SIGNATURE_PARAMS_LABEL_BYTES,
BBS_SIGNATURE_PARAMS_LABEL_BYTES,
CRYPTO_VERSION_STR,
SignatureType,
PROOF_STR,
PS_CRED_PROOF_TYPE,
PS_SIGNATURE_PARAMS_LABEL_BYTES,
SCHEMA_STR,
STATUS_STR,
SUBJECT_STR,
BBDT16_CRED_PROOF_TYPE,
BBDT16_MAC_PARAMS_LABEL_BYTES
} from './types-and-consts';
import { VerifyResult } from 'crypto-wasm-new';
import { BBSPublicKey, BBSSignature, BBSSignatureParams } from '../bbs';
import { PSPublicKey, PSSignature, PSSignatureParams } from '../ps';
import { BBSPlusPublicKeyG2, BBSPlusSignatureG1, BBSPlusSignatureParamsG1 } from '../bbs-plus';
import { CredentialCommon } from './credential-common';
import {
BBDT16Mac,
BBDT16MacParams,
BBDT16MacProofOfValidity,
BBDT16MacPublicKeyG1,
BBDT16MacSecretKey
} from '../bbdt16-mac';
export abstract class Credential<PublicKey, Signature, SignatureParams> extends CredentialCommon<Signature> {
abstract verify(publicKey: PublicKey, signatureParams?: SignatureParams): VerifyResult;
serializeForSigning(): object {
const schema = semver.gte(this.version, '0.6.0') ? this.schema?.toJSON() : this.schema?.toJsonString();
// Schema should be part of the credential signature to prevent the credential holder from convincing a verifier of a manipulated schema
const s = {
[CRYPTO_VERSION_STR]: this.version,
// Converting the schema to a JSON string rather than keeping it JSON object to avoid creating extra fields while
// signing which makes the implementation more expensive as one sig param is needed for each field.
[SCHEMA_STR]: schema,
[SUBJECT_STR]: this.subject
};
for (const [k, v] of this.topLevelFields.entries()) {
s[k] = v;
}
if (this.credentialStatus !== undefined) {
s[STATUS_STR] = this.credentialStatus;
}
(this.constructor as typeof Credential).applyDefaultProofMetadataIfNeeded(s);
delete s[PROOF_STR]['proofValue'];
return s;
}
toJSONWithJsonLdContext(): object {
let j = this.toJSON();
const jctx = this.schema.getJsonLdContext();
// TODO: Uncomment me. The correct context should be "something like" below. See comments over the commented function `getJsonLdContext` for details
// jctx['@context'][1]['proof'] = {
// type: 'schema:Text',
// proofValue: 'schema:Text',
// };
jctx['@context'][1][PROOF_STR] = CredentialSchema.getDummyContextValue(PROOF_STR);
jctx['@context'][1]['type'] = CredentialSchema.getDummyContextValue('type');
jctx['@context'][1]['proofValue'] = CredentialSchema.getDummyContextValue('proofValue');
j = { ...j, ...jctx };
return j;
}
/**
* Ensure proof type is correct
* @param typ
* @protected
*/
protected static validateProofType(typ: string) {
const expectedTyp = this.getSigType();
if (typ !== expectedTyp) {
throw new Error(`Expected proof type to be ${expectedTyp} but found ${typ}`);
}
}
static getSigType(): SignatureType {
throw new Error('This method should be implemented by extending class');
}
}
export class BBSCredential extends Credential<BBSPublicKey, BBSSignature, BBSSignatureParams> {
verify(publicKey: BBSPublicKey, signatureParams?: BBSSignatureParams): VerifyResult {
const cred = this.serializeForSigning();
return this.signature.verifyMessageObject(
cred,
publicKey,
signatureParams ?? BBS_SIGNATURE_PARAMS_LABEL_BYTES,
this.schema.encoder
);
}
/**
* A credential will have at least some proof metadata like the type or purpose. This adds those defaults to the
* given object.
* @param s
*/
static applyDefaultProofMetadataIfNeeded(s: object) {
if (!s[PROOF_STR]) {
s[PROOF_STR] = {
type: BBS_CRED_PROOF_TYPE
};
}
}
static fromJSON(j: object, proofValue?: string): BBSCredential {
const [cryptoVersion, credentialSchema, credentialSubject, topLevelFields, sig, credentialStatus] = this.parseJSON(
j,
proofValue
);
return new this(
cryptoVersion,
credentialSchema,
credentialSubject,
topLevelFields,
new BBSSignature(sig),
credentialStatus
);
}
static getSigType(): SignatureType {
return SignatureType.Bbs;
}
}
export class BBSPlusCredential extends Credential<BBSPlusPublicKeyG2, BBSPlusSignatureG1, BBSPlusSignatureParamsG1> {
verify(publicKey: BBSPlusPublicKeyG2, signatureParams?: BBSPlusSignatureParamsG1): VerifyResult {
const cred = this.serializeForSigning();
return this.signature.verifyMessageObject(
cred,
publicKey,
signatureParams ?? BBS_PLUS_SIGNATURE_PARAMS_LABEL_BYTES,
this.schema.encoder
);
}
/**
* A credential will have at least some proof metadata like the type or purpose. This adds those defaults to the
* given object.
* @param s
*/
static applyDefaultProofMetadataIfNeeded(s: object) {
if (!s[PROOF_STR]) {
s[PROOF_STR] = {
type: BBS_PLUS_CRED_PROOF_TYPE
};
}
}
static fromJSON(j: object, proofValue?: string): BBSPlusCredential {
const [cryptoVersion, credentialSchema, credentialSubject, topLevelFields, sig, credentialStatus] = this.parseJSON(
j,
proofValue
);
return new this(
cryptoVersion,
credentialSchema,
credentialSubject,
topLevelFields,
new BBSPlusSignatureG1(sig),
credentialStatus
);
}
static getSigType(): SignatureType {
return SignatureType.BbsPlus;
}
}
export class PSCredential extends Credential<PSPublicKey, PSSignature, PSSignatureParams> {
verify(publicKey: PSPublicKey, signatureParams?: PSSignatureParams): VerifyResult {
const cred = this.serializeForSigning();
return this.signature.verifyMessageObject(
cred,
publicKey,
signatureParams ?? PS_SIGNATURE_PARAMS_LABEL_BYTES,
this.schema.encoder
);
}
/**
* A credential will have at least some proof metadata like the type or purpose. This adds those defaults to the
* given object.
* @param s
*/
static applyDefaultProofMetadataIfNeeded(s: object) {
if (!s[PROOF_STR]) {
s[PROOF_STR] = {
type: PS_CRED_PROOF_TYPE
};
}
}
static fromJSON(j: object, proofValue?: string): PSCredential {
const [cryptoVersion, credentialSchema, credentialSubject, topLevelFields, sig, credentialStatus] = this.parseJSON(
j,
proofValue
);
return new this(
cryptoVersion,
credentialSchema,
credentialSubject,
topLevelFields,
new PSSignature(sig),
credentialStatus
);
}
static getSigType(): SignatureType {
return SignatureType.Ps;
}
}
export class BBDT16Credential extends Credential<undefined, BBDT16Mac, BBDT16MacParams> {
verify(publicKey: undefined, signatureParams?: BBDT16MacParams): VerifyResult {
throw new Error(`Not applicable`);
}
/**
* Since these cannot be directly verified using the public key, issuer creates a proof of validity and
* gives to the holder who can then verify it without using the secret key
* @param secretKey
* @param publicKey
* @param signatureParams
*/
proofOfValidity(
secretKey: BBDT16MacSecretKey,
publicKey: BBDT16MacPublicKeyG1,
signatureParams?: BBDT16MacParams
): BBDT16MacProofOfValidity {
const p = signatureParams ?? BBDT16MacParams.getMacParamsOfRequiredSize(1, BBDT16_MAC_PARAMS_LABEL_BYTES);
return new BBDT16MacProofOfValidity(this.signature, secretKey, publicKey, p);
}
/**
* This is just done for testing. In practice a credential holder will never have the secret key
* @param secretKey
* @param signatureParams
*/
verifyUsingSecretKey(secretKey: BBDT16MacSecretKey, signatureParams?: BBDT16MacParams): VerifyResult {
const cred = this.serializeForSigning();
return this.signature.verifyMessageObject(
cred,
secretKey,
signatureParams ?? BBDT16_MAC_PARAMS_LABEL_BYTES,
this.schema.encoder
);
}
verifyUsingValidityProof(
proof: BBDT16MacProofOfValidity,
publicKey: BBDT16MacPublicKeyG1,
signatureParams?: BBDT16MacParams
): VerifyResult {
const cred = this.serializeForSigning();
return proof.verifyWithMessageObject(
this.signature,
cred,
publicKey,
signatureParams ?? BBDT16_MAC_PARAMS_LABEL_BYTES,
this.schema.encoder
);
}
/**
* A credential will have at least some proof metadata like the type or purpose. This adds those defaults to the
* given object.
* @param s
*/
static applyDefaultProofMetadataIfNeeded(s: object) {
if (!s[PROOF_STR]) {
s[PROOF_STR] = {
type: BBDT16_CRED_PROOF_TYPE
};
}
}
static fromJSON(j: object, proofValue?: string): BBDT16Credential {
const [cryptoVersion, credentialSchema, credentialSubject, topLevelFields, sig, credentialStatus] = this.parseJSON(
j,
proofValue
);
return new this(
cryptoVersion,
credentialSchema,
credentialSubject,
topLevelFields,
new BBDT16Mac(sig),
credentialStatus
);
}
static getSigType(): SignatureType {
return SignatureType.Bbdt16;
}
}