-
Notifications
You must be signed in to change notification settings - Fork 9
/
blinded-credential.ts
258 lines (241 loc) · 8.2 KB
/
blinded-credential.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
import { BBSPlusBlindSignatureG1 } from '../bbs-plus';
import { BBSBlindSignature, BBSSignature } from '../bbs';
import { BBSCredential, BBSPlusCredential, BBDT16Credential } from './credential';
import { BBSPlusBlinding, BBDT16Blinding } from './blinded-credential-request-builder';
import * as _ from 'lodash';
import { CredentialCommon } from './credential-common';
import {
BBS_BLINDED_CRED_PROOF_TYPE,
BBS_CRED_PROOF_TYPE,
BBS_PLUS_BLINDED_CRED_PROOF_TYPE,
BBS_PLUS_CRED_PROOF_TYPE,
BBDT16_BLINDED_CRED_PROOF_TYPE,
BBDT16_CRED_PROOF_TYPE,
PROOF_STR,
TYPE_STR
} from './types-and-consts';
import { BBDT16BlindMac } from '../bbdt16-mac';
/**
* A blinded credential created by the signer. Has to be converted to a (unblinded) credential
*/
export abstract class BlindedCredential<BlindSig> extends CredentialCommon<BlindSig> {
/**
* Merge blinded and unblinded subject to create the subject of the final credential
* @param blindedSubject
* @returns
*/
protected getUpdatedSubject(blindedSubject: object | object[]): object | object[] {
const subject = _.cloneDeep(this.subject);
if (Array.isArray(subject)) {
if (!Array.isArray(blindedSubject)) {
throw new Error('Both blinded and unblinded subjects should be array');
}
if (subject.length !== blindedSubject.length) {
throw new Error(
`Both blinded and unblinded subjects should be array of same length but subject length = ${subject.length} and blinded subject length = ${blindedSubject.length}`
);
}
const updatedSubject = [];
for (let i = 0; i < subject.length; i++) {
// @ts-ignore
updatedSubject.push(_.merge(subject[i], blindedSubject[i]));
}
return updatedSubject;
} else {
if (Array.isArray(blindedSubject)) {
throw new Error('Both blinded and unblinded subjects should be objects');
}
return _.merge(subject, blindedSubject);
}
}
protected getUpdatedStatus(blindedStatus?: object): object | undefined {
let credStatus = this.credentialStatus ? (_.cloneDeep(this.credentialStatus) as object) : undefined;
if (blindedStatus && credStatus) {
credStatus = _.merge(credStatus, blindedStatus);
}
return credStatus;
}
protected getUpdatedAttributes(
proofType: string,
blindedSubject: object | object[],
blindedStatus?: object,
blindedTopLevelFields?: Map<string, unknown>
): [object | object[], object | undefined, Map<string, unknown>] {
const updatedSubject = this.getUpdatedSubject(blindedSubject);
const credStatus = this.getUpdatedStatus(blindedStatus);
const topLevelFields = this.updateProofType(proofType);
if (blindedTopLevelFields) {
for (const [k, v] of blindedTopLevelFields.entries()) {
// This will overwrite any top-level fields set by issuer.
topLevelFields.set(k, v);
}
}
return [updatedSubject, credStatus, topLevelFields];
}
protected static validateProofType(typ: string) {
if (
![BBS_BLINDED_CRED_PROOF_TYPE, BBS_PLUS_BLINDED_CRED_PROOF_TYPE, BBDT16_BLINDED_CRED_PROOF_TYPE].includes(typ)
) {
throw new Error(`Invalid proof type ${typ}`);
}
}
/**
* Change the proof type from "blinded" to "unblinded"
* @param newProofType
* @protected
*/
protected updateProofType(newProofType): Map<string, unknown> {
let topLevelFields;
if (this.topLevelFields.has(PROOF_STR)) {
topLevelFields = _.cloneDeep(this.topLevelFields);
const pVal = topLevelFields.get(PROOF_STR);
pVal[TYPE_STR] = newProofType;
topLevelFields.set(PROOF_STR, pVal);
} else {
topLevelFields = this.topLevelFields;
}
return topLevelFields;
}
}
export class BBSBlindedCredential extends BlindedCredential<BBSBlindSignature> {
/**
* Convert to unblinded credential which can be verified with the public key
* @param blindedSubject
* @param blindedStatus
* @param blindedTopLevelFields - Any top level fields that are blinded. Ensure that these are not set by the issuer.
* @returns
*/
toCredential(
blindedSubject: object | object[],
blindedStatus?: object,
blindedTopLevelFields?: Map<string, unknown>
): BBSCredential {
const [updatedSubject, credStatus, topLevelFields] = this.getUpdatedAttributes(
BBS_CRED_PROOF_TYPE,
blindedSubject,
blindedStatus,
blindedTopLevelFields
);
return new BBSCredential(
this.version,
this.schema,
updatedSubject,
topLevelFields,
new BBSSignature(this.signature.value),
credStatus
);
}
static fromJSON(j: object, proofValue?: string): BBSBlindedCredential {
const [cryptoVersion, credentialSchema, credentialSubject, topLevelFields, sig, credentialStatus] = this.parseJSON(
j,
proofValue
);
return new this(
cryptoVersion,
credentialSchema,
credentialSubject,
topLevelFields,
new BBSBlindSignature(sig),
credentialStatus
);
}
static applyDefaultProofMetadataIfNeeded(s: object) {
if (!s[PROOF_STR]) {
s[PROOF_STR] = {
type: BBS_BLINDED_CRED_PROOF_TYPE
};
}
}
}
export class BBSPlusBlindedCredential extends BlindedCredential<BBSPlusBlindSignatureG1> {
/**
* Convert to unblinded credential which can be verified with the public key
* @param blindedSubject
* @param blinding - blinding used while creating the request
* @param blindedStatus
* @param blindedTopLevelFields - Any top level fields that are blinded. Ensure that these are not set by the issuer.
* @returns
*/
toCredential(
blindedSubject: object | object[],
blinding: BBSPlusBlinding,
blindedStatus?: object,
blindedTopLevelFields?: Map<string, unknown>
): BBSPlusCredential {
const [updatedSubject, credStatus, topLevelFields] = this.getUpdatedAttributes(
BBS_PLUS_CRED_PROOF_TYPE,
blindedSubject,
blindedStatus,
blindedTopLevelFields
);
const unblindedSig = this.signature.unblind(blinding.value);
return new BBSPlusCredential(this.version, this.schema, updatedSubject, topLevelFields, unblindedSig, credStatus);
}
static fromJSON(j: object, proofValue?: string): BBSPlusBlindedCredential {
const [cryptoVersion, credentialSchema, credentialSubject, topLevelFields, sig, credentialStatus] = this.parseJSON(
j,
proofValue
);
return new this(
cryptoVersion,
credentialSchema,
credentialSubject,
topLevelFields,
new BBSPlusBlindSignatureG1(sig),
credentialStatus
);
}
static applyDefaultProofMetadataIfNeeded(s: object) {
if (!s[PROOF_STR]) {
s[PROOF_STR] = {
type: BBS_PLUS_BLINDED_CRED_PROOF_TYPE
};
}
}
}
export class BBDT16BlindedCredential extends BlindedCredential<BBDT16BlindMac> {
/**
* Convert to unblinded credential which can be verified with the secret key
* @param blindedSubject
* @param blinding - blinding used while creating the request
* @param blindedStatus
* @param blindedTopLevelFields - Any top level fields that are blinded. Ensure that these are not set by the issuer.
* @returns
*/
toCredential(
blindedSubject: object | object[],
blinding: BBDT16Blinding,
blindedStatus?: object,
blindedTopLevelFields?: Map<string, unknown>
): BBDT16Credential {
const [updatedSubject, credStatus, topLevelFields] = this.getUpdatedAttributes(
BBDT16_CRED_PROOF_TYPE,
blindedSubject,
blindedStatus,
blindedTopLevelFields
);
const unblindedSig = this.signature.unblind(blinding.value);
return new BBDT16Credential(this.version, this.schema, updatedSubject, topLevelFields, unblindedSig, credStatus);
}
static fromJSON(j: object, proofValue?: string): BBDT16BlindedCredential {
const [cryptoVersion, credentialSchema, credentialSubject, topLevelFields, sig, credentialStatus] = this.parseJSON(
j,
proofValue
);
return new this(
cryptoVersion,
credentialSchema,
credentialSubject,
topLevelFields,
new BBDT16BlindMac(sig),
credentialStatus
);
}
static applyDefaultProofMetadataIfNeeded(s: object) {
if (!s[PROOF_STR]) {
s[PROOF_STR] = {
type: BBDT16_BLINDED_CRED_PROOF_TYPE
};
}
}
}