Repository navigation
Expand file tree
/
Copy pathcommon.ts
More file actions
273 lines (245 loc) · 7.32 KB
/
Copy pathcommon.ts
File metadata and controls
273 lines (245 loc) · 7.32 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
import { NodeAux, Hash, Proof, ZERO_HASH } from '@iden3/js-merkletree';
import {
buildTreeState,
ClaimNonRevStatus,
GISTProof,
isValidOperation,
Operators,
QueryOperators
} from '../circuits';
import { StateProof } from '../storage/entities/state';
import {
MerkleTreeProofWithTreeState,
RevocationStatus,
W3CCredential,
buildFieldPath,
getSerializationAttrFromContext,
getFieldSlotIndex
} from '../verifiable';
import { Merklizer, Options, Path } from '@iden3/js-jsonld-merklization';
import { byteEncoder } from '../utils';
import { JSONObject } from '../iden3comm';
import { Claim } from '@iden3/js-iden3-core';
import { poseidon } from '@iden3/js-crypto';
export type PreparedCredential = {
credential: W3CCredential;
credentialCoreClaim: Claim;
revStatus?: RevocationStatus;
};
export type PreparedAuthBJJCredential = {
credential: W3CCredential;
incProof: MerkleTreeProofWithTreeState;
nonRevProof: MerkleTreeProofWithTreeState;
coreClaim: Claim;
};
/**
* converts verifiable RevocationStatus model to circuits structure
*
* @param {RevocationStatus} - credential.status of the verifiable credential
* @returns {ClaimNonRevStatus}
*/
export const toClaimNonRevStatus = (s?: RevocationStatus): ClaimNonRevStatus => {
if (!s) {
const hash = poseidon.hash(new Array(3).fill(0n));
return {
proof: new Proof(),
treeState: {
state: Hash.fromBigInt(hash),
claimsRoot: ZERO_HASH,
revocationRoot: ZERO_HASH,
rootOfRoots: ZERO_HASH
}
};
}
return {
proof: s.mtp,
treeState: buildTreeState(
s.issuer.state,
s.issuer.claimsTreeRoot,
s.issuer.revocationTreeRoot,
s.issuer.rootOfRoots
)
};
};
/**
* converts state info from smart contract to gist proof
*
* @param {StateProof} smtProof - state proof from smart contract
* @returns {GISTProof}
*/
export const toGISTProof = (smtProof: StateProof): GISTProof => {
let existence = false;
let nodeAux: NodeAux | undefined;
if (smtProof.existence) {
existence = true;
} else {
if (smtProof.auxExistence) {
nodeAux = {
key: Hash.fromBigInt(smtProof.auxIndex),
value: Hash.fromBigInt(smtProof.auxValue)
};
}
}
const allSiblings: Hash[] = smtProof.siblings.map((s) => Hash.fromBigInt(s));
const proof = new Proof({ siblings: allSiblings, nodeAux: nodeAux, existence: existence });
const root = Hash.fromBigInt(smtProof.root);
return {
root,
proof
};
};
export type PropertyQuery = {
fieldName: string;
operator: Operators;
operatorValue?: unknown;
};
export type QueryMetadata = PropertyQuery & {
slotIndex: number;
values: bigint[];
path: Path;
claimPathKey: bigint;
datatype: string;
merklizedSchema: boolean;
};
export const parseCredentialSubject = (credentialSubject?: JSONObject): PropertyQuery[] => {
// credentialSubject is empty
if (!credentialSubject) {
return [{ operator: QueryOperators.$noop, fieldName: '' }];
}
const queries: PropertyQuery[] = [];
const entries = Object.entries(credentialSubject);
if (!entries.length) {
throw new Error(`query must have at least 1 predicate`);
}
for (const [fieldName, fieldReq] of entries) {
const fieldReqEntries = Object.entries(fieldReq as { [key: string]: unknown });
const isSelectiveDisclosure = fieldReqEntries.length === 0;
if (isSelectiveDisclosure) {
queries.push({ operator: QueryOperators.$sd, fieldName: fieldName });
continue;
}
for (const [operatorName, operatorValue] of fieldReqEntries) {
if (!QueryOperators[operatorName as keyof typeof QueryOperators]) {
throw new Error(`operator is not supported by lib`);
}
const operator = QueryOperators[operatorName as keyof typeof QueryOperators];
queries.push({ operator, fieldName, operatorValue });
}
}
return queries;
};
export const parseQueryMetadata = async (
propertyQuery: PropertyQuery,
ldContextJSON: string,
credentialType: string,
options: Options
): Promise<QueryMetadata> => {
const query: QueryMetadata = {
...propertyQuery,
slotIndex: 0,
merklizedSchema: false,
datatype: '',
claimPathKey: BigInt(0),
values: [],
path: new Path()
};
if (!propertyQuery.fieldName && propertyQuery.operator !== Operators.NOOP) {
throw new Error('query must have a field name if operator is not $noop');
}
if (propertyQuery.fieldName) {
query.datatype = await Path.newTypeFromContext(
ldContextJSON,
`${credentialType}.${propertyQuery.fieldName}`,
options
);
}
const serAttr = await getSerializationAttrFromContext(
JSON.parse(ldContextJSON),
options,
credentialType
);
if (!serAttr) {
query.merklizedSchema = true;
}
// for merklized credentials slotIndex in query must be equal to zero
// and not a position of merklization root.
// it has no influence on check in the off-chain circuits, but it aligns with onchain verification standard
if (!query.merklizedSchema) {
query.slotIndex = await getFieldSlotIndex(
propertyQuery.fieldName,
credentialType,
byteEncoder.encode(ldContextJSON)
);
} else {
try {
const path = await buildFieldPath(
ldContextJSON,
credentialType,
propertyQuery.fieldName,
options
);
query.claimPathKey = await path.mtEntry();
query.path = path;
} catch (e) {
throw new Error(`field does not exist in the schema ${(e as Error).message}`);
}
}
if (propertyQuery.operatorValue !== undefined) {
if (!isValidOperation(query.datatype, propertyQuery.operator)) {
throw new Error(
`operator ${propertyQuery.operator} is not supported for datatype ${query.datatype}`
);
}
if (
(propertyQuery.operator === Operators.NOOP || propertyQuery.operator === Operators.SD) &&
propertyQuery.operatorValue
) {
throw new Error(`operator value should be undefined for ${propertyQuery.operator} operator`);
}
let values: bigint[];
switch (propertyQuery.operator) {
case Operators.NOOP:
case Operators.SD:
values = [];
break;
case Operators.EXISTS:
values = transformExistsValue(propertyQuery.operatorValue);
break;
default:
values = await transformQueryValueToBigInts(propertyQuery.operatorValue, query.datatype);
}
query.values = values;
}
return query;
};
export const parseQueriesMetadata = async (
credentialType: string,
ldContextJSON: string,
credentialSubject: JSONObject,
options: Options
): Promise<QueryMetadata[]> => {
const queriesMetadata = parseCredentialSubject(credentialSubject);
return Promise.all(
queriesMetadata.map((m) => parseQueryMetadata(m, ldContextJSON, credentialType, options))
);
};
export const transformQueryValueToBigInts = async (
value: unknown,
ldType: string
): Promise<bigint[]> => {
const values: bigint[] = [];
if (Array.isArray(value)) {
for (let index = 0; index < value.length; index++) {
values[index] = await Merklizer.hashValue(ldType, value[index]);
}
} else {
values[0] = await Merklizer.hashValue(ldType, value);
}
return values;
};
const transformExistsValue = (value: unknown): bigint[] => {
if (typeof value == 'boolean') {
return [BigInt(value)];
}
throw new Error('exists operator value must be true or false');
};