Repository navigation
Expand file tree
/
Copy pathcommon.ts
More file actions
228 lines (208 loc) · 6.18 KB
/
Copy pathcommon.ts
File metadata and controls
228 lines (208 loc) · 6.18 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
import { Hex } from '@iden3/js-crypto';
import { Hash, ZERO_HASH, Proof, swapEndianness } from '@iden3/js-merkletree';
import { TreeState } from './models';
export const defaultMTLevels = 40; // max MT levels, default value for identity circuits
export const defaultValueArraySize = 64; // max value array size, default value for identity circuits
export const defaultMTLevelsOnChain = 64; // max MT levels on chain, default value for identity circuits
export const defaultMTLevelsClaim = 32; // max MT levels of JSON-LD merklization on claim
export const ErrorEmptyAuthClaimProof = 'empty auth claim mtp proof';
export const ErrorEmptyAuthClaimNonRevProof = 'empty auth claim non-revocation mtp proof';
export const ErrorEmptyChallengeSignature = 'empty challenge signature';
export const ErrorEmptyClaimSignature = 'empty claim signature';
export const ErrorEmptyClaimProof = 'empty claim mtp proof';
export const ErrorEmptyClaimNonRevProof = 'empty claim non-revocation mtp proof';
export const ErrorUserStateInRelayClaimProof =
'empty user state in relay claim non-revocation mtp proof';
export const ErrorEmptyIssuerAuthClaimProof = 'empty issuer auth claim mtp proof';
export const ErrorEmptyIssuerAuthClaimNonRevProof =
'empty issuer auth claim non-revocation mtp proof';
/**
* base config for circuit inputs
*
* @public
* @class BaseConfig
*/
export class BaseConfig {
mtLevel!: number; // Max levels of MT
maxValueArraySize!: number; // Size if( value array in identity circuit)s
mtLevelOnChain!: number;
mtLevelClaim!: number; // Max level of JSONLD claim
/**
* getMTLevel max circuit MT levels
*
* @returns number
*/
getMTLevel(): number {
return this.mtLevel ? this.mtLevel : defaultMTLevels;
}
/**
* GetMTLevelsClaim max jsonld Claim levels
*
* @returns number
*/
getMTLevelsClaim(): number {
return this.mtLevelClaim ? this.mtLevelClaim : defaultMTLevelsClaim;
}
/**
* GetValueArrSize return size of circuits value array size
*
* @returns number
*/
getValueArrSize(): number {
return this.maxValueArraySize ? this.maxValueArraySize : defaultValueArraySize;
}
/**
* getMTLevelOnChain return level on chain for given circuit
*
* @returns number
*/
getMTLevelOnChain(): number {
return this.mtLevelOnChain ? this.mtLevelOnChain : defaultMTLevelsOnChain;
}
}
/**
* @deprecated The method should not be used and will be removed in the next major version,
* please use Hash.fromHex instead
* @param {(string | undefined)} s - string hex
* @returns Hash
*/
export const strMTHex = (s: string | undefined): Hash => {
if (!s) {
return ZERO_HASH;
}
const h = new Hash();
h.value = swapEndianness(Hex.decodeString(s));
return h;
};
/**
* converts hexes of tree roots to Hashes
*
* @param {(string | undefined)} state - state of tree hex
* @param {(string | undefined)} claimsTreeRoot - claims tree root hex
* @param {(string | undefined)} revocationTreeRoot - revocation tree root hex
* @param {(string | undefined)} rootOfRoots - root of roots tree root hex
* @returns TreeState
*/
export const buildTreeState = (
state: string | undefined,
claimsTreeRoot: string | undefined,
revocationTreeRoot: string | undefined,
rootOfRoots: string | undefined
): TreeState => ({
state: Hash.fromHex(state),
claimsRoot: Hash.fromHex(claimsTreeRoot),
revocationRoot: Hash.fromHex(revocationTreeRoot),
rootOfRoots: Hash.fromHex(rootOfRoots)
});
/**
* siblings as string array
*
* @param {Proof} proof - proof with siblings
* @param {number} levels - levels number
* @returns string[]
*/
export const prepareSiblingsStr = (proof: Proof, levels: number): string[] => {
const siblings = proof.allSiblings();
// Add the rest of empty levels to the siblings
for (let i = siblings.length; i < levels; i++) {
siblings.push(ZERO_HASH);
}
return siblings.map((s: Hash) => s.bigInt().toString());
};
/**
* PrepareCircuitArrayValues padding values to size.
* Validate array size and throw an exception if array is bigger than size
* if array is bigger, circuit cannot compile because number of inputs does not match
*
*
* @param {bigint[]} arr - given values
* @param {number} size - size to pad
* @returns bigint[]
*/
export const prepareCircuitArrayValues = (arr: bigint[], size: number): bigint[] => {
if (!arr) {
arr = [];
}
if (arr.length > size) {
throw new Error(`array size ${arr.length} is bigger max expected size ${size}`);
}
// Add the empty values
for (let i = arr.length; i < size; i++) {
arr.push(BigInt(0));
}
return arr;
};
/**
* converts each big integer in array to string
*
* @param {bigint[]} arr - array of big numbers
* @returns string[]
*/
export const bigIntArrayToStringArray = (arr: bigint[]): string[] => {
return arr.map((a) => a.toString());
};
/**
* auxiliary node
*
* @public
* @interface NodeAuxValue
*/
export interface NodeAuxValue {
key: Hash;
value: Hash;
noAux: string;
}
export /**
* gets auxiliary node from proof
*
* @param {(Proof | undefined)} p - mtp
* @returns NodeAuxValue
*/
const getNodeAuxValue = (p: Proof | undefined): NodeAuxValue => {
// proof of inclusion
if (p?.existence) {
return {
key: ZERO_HASH,
value: ZERO_HASH,
noAux: '0'
};
}
// proof of non-inclusion (NodeAux exists)
if (p?.nodeAux?.value !== undefined && p?.nodeAux?.key !== undefined) {
return {
key: p.nodeAux.key,
value: p.nodeAux.value,
noAux: '0'
};
}
// proof of non-inclusion (NodeAux does not exist)
return {
key: ZERO_HASH,
value: ZERO_HASH,
noAux: '1'
};
};
/**
* converts boolean existence param to integer
* if true - 1, else - 0
*
* @param {boolean} b - existence
* @returns number
*/
export const existenceToInt = (b: boolean): number => (b ? 0 : 1);
/**
* return object properties
*
* @param {object} obj
* @returns object
*/
export function getProperties(obj: object): object {
const result: { [key: string]: unknown } = {};
for (const property in obj) {
// eslint-disable-next-line no-prototype-builtins
if (obj.hasOwnProperty(property) && !property.startsWith('_')) {
result[property] = obj[property as keyof typeof obj];
}
}
return result;
}