-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathblinded-credential-builder.ts
More file actions
171 lines (160 loc) · 6.25 KB
/
Copy pathblinded-credential-builder.ts
File metadata and controls
171 lines (160 loc) · 6.25 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
import { CredentialBuilder } from './credential-builder';
import { CredentialBuilderCommon } from './credential-builder-common';
import { IBlindCredentialRequest } from './presentation-specification';
import {
BBS_PLUS_SIGNATURE_PARAMS_LABEL_BYTES,
BBS_SIGNATURE_PARAMS_LABEL_BYTES,
STATUS_STR,
BBDT16_MAC_PARAMS_LABEL_BYTES
} from './types-and-consts';
import { BBSCredential, BBSPlusCredential, BBDT16Credential } from './credential';
import { BBSBlindSignature, BBSSecretKey, BBSSignatureParams } from '../bbs';
import { BBSPlusBlindSignatureG1, BBSPlusSecretKey, BBSPlusSignatureParamsG1 } from '../bbs-plus';
import { CredentialSchema } from './schema';
import { BBSBlindedCredential, BBSPlusBlindedCredential, BBDT16BlindedCredential } from './blinded-credential';
import { BBDT16BlindMac, BBDT16MacParams, BBDT16MacSecretKey } from '../bbdt16-mac';
/**
* Used by the signer to create a blinded credential. The signer will know only the unblinded attributes
*/
export abstract class BlindedCredentialBuilder extends CredentialBuilderCommon {
/** NOTE: This should match `CredentialBuilder.VERSION` exactly else backward compatibility code won't always work.
This is because `BlindedCredential.toCredential` outputs a `Credential` which should be same as the one output
by `CredentialBuilder.sign` */
static VERSION = CredentialBuilder.VERSION;
blindedCredReq: IBlindCredentialRequest;
constructor(blindedCredReq: IBlindCredentialRequest) {
super(BlindedCredentialBuilder.VERSION);
this.blindedCredReq = blindedCredReq;
this.schema = blindedCredReq.schema;
}
protected getTotalAttributesAndEncodedKnownAttributes(): [number, Map<number, Uint8Array>] {
const schema = this.schema as CredentialSchema;
const flattenedSchema = schema.flatten();
let knownAttributes = this.serializeForSigning();
if (this.blindedCredReq.unBlindedAttributes !== undefined) {
if (typeof this.blindedCredReq.unBlindedAttributes !== 'object') {
throw new Error(
`Unblinded attributes were supposed to an object but found ${this.blindedCredReq.unBlindedAttributes}`
);
}
knownAttributes = { ...knownAttributes, ...this.blindedCredReq.unBlindedAttributes };
}
const encodedAttributes = new Map<number, Uint8Array>();
Object.entries(schema.encoder.encodeMessageObjectAsObjectConstantTime(knownAttributes)).forEach(([name, value]) => {
encodedAttributes.set(flattenedSchema[0].indexOf(name), value);
});
return [flattenedSchema[0].length, encodedAttributes];
}
protected processUnBlindedAttributes() {
if (this.blindedCredReq.unBlindedAttributes !== undefined) {
if (typeof this.blindedCredReq.unBlindedAttributes !== 'object') {
throw new Error(
`Unblinded attributes were supposed to an object but found ${this.blindedCredReq.unBlindedAttributes}`
);
}
for (const [name, value] of Object.entries(this.blindedCredReq.unBlindedAttributes)) {
if (name === STATUS_STR) {
if (this.credStatus !== undefined) {
throw new Error('credStatus was set by the signer when it was provided in request as well');
}
this.credStatus = value;
} else {
throw new Error(`Unsupported for blinded attribute ${name}`);
}
}
}
}
}
export class BBSBlindedCredentialBuilder extends BlindedCredentialBuilder {
protected applyDefaultProofMetadataIfNeeded(s: object) {
BBSCredential.applyDefaultProofMetadataIfNeeded(s);
}
/**
* Blind sign a credential
* @param secretKey
* @param sigParams
* @returns
*/
sign(
secretKey: BBSSecretKey,
sigParams: Uint8Array | BBSSignatureParams = BBS_SIGNATURE_PARAMS_LABEL_BYTES
): BBSBlindedCredential {
const [totalAttrs, encodedAttrs] = this.getTotalAttributesAndEncodedKnownAttributes();
const params = BBSSignatureParams.getSigParamsOfRequiredSize(totalAttrs, sigParams);
const sig = BBSBlindSignature.generate(this.blindedCredReq.commitment, encodedAttrs, secretKey, params, false);
this.processUnBlindedAttributes();
return new BBSBlindedCredential(
this.version,
this.schema as CredentialSchema,
// @ts-ignore
this.subject,
this._topLevelFields,
sig,
this.credStatus
);
}
}
export class BBSPlusBlindedCredentialBuilder extends BlindedCredentialBuilder {
protected applyDefaultProofMetadataIfNeeded(s: object) {
BBSPlusCredential.applyDefaultProofMetadataIfNeeded(s);
}
/**
* Blind sign a credential
* @param secretKey
* @param sigParams
* @returns
*/
sign(
secretKey: BBSPlusSecretKey,
sigParams: Uint8Array | BBSPlusSignatureParamsG1 = BBS_PLUS_SIGNATURE_PARAMS_LABEL_BYTES
): BBSPlusBlindedCredential {
const [totalAttrs, encodedAttrs] = this.getTotalAttributesAndEncodedKnownAttributes();
const params = BBSPlusSignatureParamsG1.getSigParamsOfRequiredSize(totalAttrs, sigParams);
const sig = BBSPlusBlindSignatureG1.generate(
this.blindedCredReq.commitment,
encodedAttrs,
secretKey,
params,
false
);
this.processUnBlindedAttributes();
return new BBSPlusBlindedCredential(
this.version,
this.schema as CredentialSchema,
// @ts-ignore
this.subject,
this._topLevelFields,
sig,
this.credStatus
);
}
}
export class BBDT16BlindedCredentialBuilder extends BlindedCredentialBuilder {
protected applyDefaultProofMetadataIfNeeded(s: object) {
BBDT16Credential.applyDefaultProofMetadataIfNeeded(s);
}
/**
* Blind sign a credential
* @param secretKey
* @param sigParams
* @returns
*/
sign(
secretKey: BBDT16MacSecretKey,
sigParams: Uint8Array | BBDT16MacParams = BBDT16_MAC_PARAMS_LABEL_BYTES
): BBDT16BlindedCredential {
const [totalAttrs, encodedAttrs] = this.getTotalAttributesAndEncodedKnownAttributes();
const params = BBDT16MacParams.getMacParamsOfRequiredSize(totalAttrs, sigParams);
const sig = BBDT16BlindMac.generate(this.blindedCredReq.commitment, encodedAttrs, secretKey, params, false);
this.processUnBlindedAttributes();
return new BBDT16BlindedCredential(
this.version,
this.schema as CredentialSchema,
// @ts-ignore
this.subject,
this._topLevelFields,
sig,
this.credStatus
);
}
}