-
Notifications
You must be signed in to change notification settings - Fork 0
/
credential_core_builder.ts
188 lines (175 loc) · 5.04 KB
/
credential_core_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
import { BloockBridge } from "../../bridge/bridge";
import { ConfigData } from "../../bridge/proto/config";
import { CreateCoreCredentialRequest } from "../../bridge/proto/identity_core";
import {
BooleanAttribute as BooleanAttributeProto,
DateAttribute as DateAttributeProto,
DateTimeAttribute as DateTimeAttributeProto,
DecimalAttribute as DecimalAttributeProto,
IntegerAttribute as IntegerAttributeProto,
StringAttribute as StringAttributeProto
} from "../../bridge/proto/identity_entities";
import { Key as KeyProto } from "../../bridge/proto/keys_entities";
import { BooleanAttribute } from "./boolean_attribute";
import { CredentialReceipt } from "./credential_receipt";
import { DateAttribute } from "./date_attribute";
import { DateTimeAttribute } from "./datetime_attribute";
import { DecimalAttribute } from "./decimal_attribute";
import { IntegerAttribute } from "./integer_attribute";
import { Issuer } from "./issuer";
import { StringAttribute } from "./string_attribute";
/**
* Helps construct credentials by specifying various attributes.
*/
export class CredentialCoreBuilder {
schemaId: string;
issuerDid: string;
holderDid: string;
expiration: number;
version: number;
key?: KeyProto;
configData: ConfigData;
stringAttributes: StringAttributeProto[];
integerAttributes: IntegerAttributeProto[];
decimalAttributes: DecimalAttributeProto[];
booleanAttributes: BooleanAttributeProto[];
dateAttributes: DateAttributeProto[];
dateTimeAttributes: DateTimeAttributeProto[];
/**
* Creates a new CredentialCoreBuilder instance with the specified parameters.
* @param schemaId
* @param issuerDid
* @param holderDid
* @param expiration
* @param version
* @param configData
*/
constructor(
issuer: Issuer,
schemaId: string,
holderDid: string,
expiration: number,
version: number,
configData: ConfigData
) {
this.schemaId = schemaId;
this.issuerDid = issuer.did.did;
this.holderDid = holderDid;
this.expiration = expiration;
this.version = version;
this.configData = configData;
this.key = issuer.key.toProto();
this.stringAttributes = [];
this.integerAttributes = [];
this.decimalAttributes = [];
this.booleanAttributes = [];
this.dateAttributes = [];
this.dateTimeAttributes = [];
}
/**
* Adds a string attribute to the CredentialCoreBuilder.
* @param key
* @param value
* @returns
*/
public withStringAttribute(
key: string,
value: string
): CredentialCoreBuilder {
this.stringAttributes.push(new StringAttribute(key, value));
return this;
}
/**
* Adds an integer attribute to the CredentialCoreBuilder.
* @param key
* @param value
* @returns
*/
public withIntegerAttribute(
key: string,
value: number
): CredentialCoreBuilder {
this.integerAttributes.push(new IntegerAttribute(key, value));
return this;
}
/**
* Adds a decimal attribute to the CredentialCoreBuilder.
* @param key
* @param value
* @returns
*/
public withDecimalAttribute(
key: string,
value: number
): CredentialCoreBuilder {
this.decimalAttributes.push(new DecimalAttribute(key, value));
return this;
}
/**
* Adds a boolean attribute to the CredentialCoreBuilder.
* @param key
* @param value
* @returns
*/
public withBooleanAttribute(
key: string,
value: boolean
): CredentialCoreBuilder {
this.booleanAttributes.push(new BooleanAttribute(key, value));
return this;
}
/**
* Adds a date attribute to the CredentialCoreBuilder.
* @param key
* @param value
* @returns
*/
public withDateAttribute(key: string, value: Date): CredentialCoreBuilder {
this.dateAttributes.push(new DateAttribute(key, value).toProto());
return this;
}
/**
* Adds a datetime attribute to the CredentialCoreBuilder.
* @param key
* @param value
* @returns
*/
public withDateTimeAttribute(
key: string,
value: Date
): CredentialCoreBuilder {
this.dateTimeAttributes.push(new DateTimeAttribute(key, value).toProto());
return this;
}
/**
* Creates and returns a Credential using the specified attributes.
* @returns
*/
async build(): Promise<CredentialReceipt> {
const bridge = new BloockBridge();
const req = CreateCoreCredentialRequest.fromPartial({
configData: this.configData,
schemaId: this.schemaId,
issuerDid: this.issuerDid,
holderDid: this.holderDid,
expiration: this.expiration,
version: this.version,
key: this.key,
stringAttributes: this.stringAttributes,
integerAttributes: this.integerAttributes,
decimalAttributes: this.decimalAttributes,
booleanAttributes: this.booleanAttributes,
dateAttributes: this.dateAttributes,
datetimeAttributes: this.dateTimeAttributes
});
return bridge
.getIdentityCore()
.CreateCoreCredential(req)
.then(res => {
if (res.error) {
throw res.error;
}
return CredentialReceipt.fromProto(res.credentialReceipt!);
});
}
}