-
Notifications
You must be signed in to change notification settings - Fork 9
/
credential-builder.ts
302 lines (272 loc) · 9.17 KB
/
credential-builder.ts
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
import { CredentialSchema } from './schema';
import {
SCHEMA_STR,
BBS_PLUS_SIGNATURE_PARAMS_LABEL_BYTES,
BBS_SIGNATURE_PARAMS_LABEL_BYTES,
PS_SIGNATURE_PARAMS_LABEL_BYTES,
BBDT16_MAC_PARAMS_LABEL_BYTES,
CRYPTO_VERSION_STR,
SUBJECT_STR,
STATUS_STR
} from './types-and-consts';
import { BBSCredential, BBSPlusCredential, BBDT16Credential, Credential, PSCredential } from './credential';
import { flatten } from 'flat';
import { areArraysEqual } from '../util';
import { BBSPublicKey, BBSSecretKey, BBSSignature, BBSSignatureParams } from '../bbs';
import {
BBSPlusPublicKeyG2,
BBSPlusSecretKey,
BBSPlusSignatureG1,
BBSPlusSignatureParamsG1,
Encoder
} from '../bbs-plus';
import { PSPublicKey, PSSecretKey, PSSignature, PSSignatureParams } from '../ps';
import { SignedMessages } from '../types';
import { CredentialBuilderCommon } from './credential-builder-common';
import { BBDT16Mac, BBDT16MacParams, BBDT16MacSecretKey } from '../bbdt16-mac';
export interface ISigningOpts {
/** Whether the credential should contain exactly the same fields (object keys, array items, literals) as the
schema. Providing false for it will result in generation of a new schema to match the credential and that schema
will be embedded in the signed credential. */
requireSameFieldsAsSchema: boolean;
}
export const DefaultSigningOpts: ISigningOpts = {
requireSameFieldsAsSchema: true
};
/**
* Create a credential
*/
export abstract class CredentialBuilder<
SecretKey,
PublicKey,
Signature,
SignatureParams
> extends CredentialBuilderCommon {
/** Follows semver and must be updated accordingly when the logic of this class changes or the
underlying crypto changes. */
static VERSION = '0.7.0';
_encodedAttributes?: { [key: string]: Uint8Array };
_sig?: Signature;
constructor() {
super(CredentialBuilder.VERSION);
}
get signature(): Signature | undefined {
return this._sig;
}
/**
* Serializes and signs creating a credential.
* Expects the credential to have the same fields as schema. This is intentional to always communicate to the
* verifier the full structure of the credential.
*
* @param secretKey
* @param signatureParams - This makes bulk issuance of credentials with same number of attributes faster because the
* signature params don't have to be generated.
* @param signingOpts
*/
sign(
secretKey: SecretKey,
signatureParams?: SignatureParams,
signingOpts?: Partial<ISigningOpts>
): Credential<PublicKey, Signature, SignatureParams> {
if (signingOpts === undefined) {
signingOpts = DefaultSigningOpts;
}
const cred = this.updateSchemaIfNeeded(signingOpts);
const schema = this.schema as CredentialSchema;
const signed = this.signMessageObject(cred, secretKey, signatureParams, schema.encoder);
this._encodedAttributes = signed.encodedMessages;
this._sig = signed.signature;
return this.newCredential(
this._version,
schema,
// @ts-ignore
this._subject,
this._topLevelFields,
this._sig,
this._credStatus
);
}
/**
* When schema doesn't match the credential, create a new appropriate schema and update the credential. Returns the
* serialized credential. Legacy version. Used by SDK for some older credential versions
* @param signingOpts
*/
updateSchemaIfNeededLegacy(signingOpts?: Partial<ISigningOpts>): object {
const cred = {
[CRYPTO_VERSION_STR]: this._version,
[SCHEMA_STR]: this.schema?.toJsonString(),
[SUBJECT_STR]: this._subject
};
for (const [k, v] of this._topLevelFields.entries()) {
cred[k] = v;
}
if (this._credStatus !== undefined) {
cred[STATUS_STR] = this._credStatus;
}
this.applyDefaultProofMetadataIfNeeded(cred);
const schema = this.schema as CredentialSchema;
if (signingOpts && !CredentialBuilder.hasSameFieldsAsSchema(cred, schema)) {
if (signingOpts.requireSameFieldsAsSchema) {
throw new Error('Credential does not have the fields as schema');
} else {
// Generate new schema
this.schema = CredentialSchema.generateAppropriateSchema(cred, schema);
// @ts-ignore
cred[SCHEMA_STR] = this.schema?.toJsonString();
}
}
return cred;
}
/**
* When schema doesn't match the credential, create a new appropriate schema and update the credential. Returns the
* serialized credential
* @param signingOpts
*/
updateSchemaIfNeeded(signingOpts?: Partial<ISigningOpts>): object {
const cred = this.serializeForSigning();
const schema = this.schema as CredentialSchema;
if (signingOpts && !CredentialBuilder.hasSameFieldsAsSchema(cred, schema)) {
if (signingOpts.requireSameFieldsAsSchema) {
throw new Error('Credential does not have the fields as schema');
} else {
// Generate new schema
this.schema = CredentialSchema.generateAppropriateSchema(cred, schema);
cred[SCHEMA_STR] = this.schema?.toJSON();
}
}
return cred;
}
static hasSameFieldsAsSchema(cred: object, schema: CredentialSchema): boolean {
return areArraysEqual(schema.flatten()[0], Object.keys(flatten(cred)).sort());
}
protected abstract signMessageObject(
messages: Object,
secretKey: SecretKey,
labelOrParams: Uint8Array | SignatureParams | undefined,
encoder: Encoder
): SignedMessages<Signature>;
protected abstract newCredential(
version: string,
schema: CredentialSchema,
subject: object,
topLevelFields: Map<string, unknown>,
sig: Signature,
credStatus?: object
): Credential<PublicKey, Signature, SignatureParams>;
}
/**
* Create a `BBS` credential
*/
export class BBSCredentialBuilder extends CredentialBuilder<
BBSSecretKey,
BBSPublicKey,
BBSSignature,
BBSSignatureParams
> {
protected signMessageObject(
messages: Object,
secretKey: BBSSecretKey,
labelOrParams: Uint8Array | BBSSignatureParams = BBS_SIGNATURE_PARAMS_LABEL_BYTES,
encoder: Encoder
): SignedMessages<BBSSignature> {
return BBSSignature.signMessageObject(messages, secretKey, labelOrParams, encoder);
}
protected newCredential(
version: string,
schema: CredentialSchema,
subject: object,
topLevelFields: Map<string, unknown>,
sig: BBSSignature,
credStatus?: object
): BBSCredential {
return new BBSCredential(version, schema, subject, topLevelFields, sig, credStatus);
}
protected applyDefaultProofMetadataIfNeeded(s: object) {
BBSCredential.applyDefaultProofMetadataIfNeeded(s);
}
}
/**
* Create a `BBS+` credential
*/
export class BBSPlusCredentialBuilder extends CredentialBuilder<
BBSPlusSecretKey,
BBSPlusPublicKeyG2,
BBSPlusSignatureG1,
BBSPlusSignatureParamsG1
> {
protected signMessageObject(
messages: Object,
secretKey: BBSPlusSecretKey,
labelOrParams: Uint8Array | BBSPlusSignatureParamsG1 = BBS_PLUS_SIGNATURE_PARAMS_LABEL_BYTES,
encoder: Encoder
): SignedMessages<BBSPlusSignatureG1> {
return BBSPlusSignatureG1.signMessageObject(messages, secretKey, labelOrParams, encoder);
}
protected newCredential(
version: string,
schema: CredentialSchema,
subject: object,
topLevelFields: Map<string, unknown>,
sig: BBSPlusSignatureG1,
credStatus?: object
): BBSPlusCredential {
return new BBSPlusCredential(version, schema, subject, topLevelFields, sig, credStatus);
}
protected applyDefaultProofMetadataIfNeeded(s: object) {
BBSPlusCredential.applyDefaultProofMetadataIfNeeded(s);
}
}
/**
* Create a `Pointcheval-Sanders` credential
*/
export class PSCredentialBuilder extends CredentialBuilder<PSSecretKey, PSPublicKey, PSSignature, PSSignatureParams> {
protected signMessageObject(
messages: Object,
secretKey: PSSecretKey,
labelOrParams: Uint8Array | PSSignatureParams = PS_SIGNATURE_PARAMS_LABEL_BYTES,
encoder: Encoder
): SignedMessages<PSSignature> {
return PSSignature.signMessageObject(messages, secretKey, labelOrParams, encoder);
}
protected newCredential(
version: string,
schema: CredentialSchema,
subject: object,
topLevelFields: Map<string, unknown>,
sig: PSSignature,
credStatus?: object
): PSCredential {
return new PSCredential(version, schema, subject, topLevelFields, sig, credStatus);
}
protected applyDefaultProofMetadataIfNeeded(s: object) {
PSCredential.applyDefaultProofMetadataIfNeeded(s);
}
}
export class BBDT16CredentialBuilder extends CredentialBuilder<
BBDT16MacSecretKey,
undefined,
BBDT16Mac,
BBDT16MacParams
> {
protected signMessageObject(
messages: Object,
secretKey: BBDT16MacSecretKey,
labelOrParams: Uint8Array | BBDT16MacParams = BBDT16_MAC_PARAMS_LABEL_BYTES,
encoder: Encoder
): SignedMessages<BBDT16Mac> {
return BBDT16Mac.signMessageObject(messages, secretKey, labelOrParams, encoder);
}
protected newCredential(
version: string,
schema: CredentialSchema,
subject: object,
topLevelFields: Map<string, unknown>,
sig: BBDT16Mac,
credStatus?: object
): BBDT16Credential {
return new BBDT16Credential(version, schema, subject, topLevelFields, sig, credStatus);
}
protected applyDefaultProofMetadataIfNeeded(s: object) {
BBDT16Credential.applyDefaultProofMetadataIfNeeded(s);
}
}