-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdata-and-encoder.ts
223 lines (203 loc) · 5.52 KB
/
data-and-encoder.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
// Data used by various tests
// 1st attribute set. This is a flat JS object.
import { EncodeFunc, Encoder } from '../../../src';
import { stringToBytes } from '../../utils';
import { Signature } from '../../scheme'
export const attributes1 = {
fname: 'John',
lname: 'Smith',
email: '[email protected]',
SSN: '123-456789-0',
'user-id': 'user:123-xyz-#',
country: 'USA',
city: 'New York',
timeOfBirth: 1662010849619,
height: 181.5,
weight: 210,
BMI: 23.25,
score: -13.5,
secret: 'my-secret-that-wont-tell-anyone'
};
// This is the structure of `attributes1`. This does not contain any attribute values but contains the names with the
// same kind of nesting as `attributes1`. For any attribute set, this should be known to all system participants, i.e. signer,
// prover and verifier. A similar alternative of defining the structure would be to replace the "null" value with a reference
// to the encoding function for that field.
export const attributes1Struct = {
fname: null,
lname: null,
email: null,
SSN: null,
'user-id': null,
country: null,
city: null,
timeOfBirth: null,
height: null,
weight: null,
BMI: null,
score: null,
secret: null
};
// 2nd attribute set. This is a nested JS object with 1 level of nesting.
export const attributes2 = {
fname: 'John',
lname: 'Smith',
sensitive: {
secret: 'my-secret-that-wont-tell-anyone',
email: '[email protected]',
SSN: '123-456789-0',
'user-id': 'user:123-xyz-#'
},
location: {
country: 'USA',
city: 'New York'
},
timeOfBirth: 1662010849619,
physical: {
height: 181.5,
weight: 210,
BMI: 23.25
},
score: -13.5
};
// This is the structure of `attributes2`. Similar to `attributes1Struct`, does not contain attribute values but the names
// and the structure of `attributes2`
export const attributes2Struct = {
fname: null,
lname: null,
sensitive: {
secret: null,
email: null,
SSN: null,
'user-id': null
},
location: {
country: null,
city: null
},
timeOfBirth: null,
physical: {
height: null,
weight: null,
BMI: null
},
score: null
};
// 3rd attribute set. This is an even more nested JS object with many levels of nesting.
export const attributes3 = {
fname: 'John',
lname: 'Smith',
sensitive: {
very: {
secret: 'my-secret-that-wont-tell-anyone'
},
email: '[email protected]',
phone: '801009801',
SSN: '123-456789-0',
'employee-id': 'user:123-xyz-#'
},
lessSensitive: {
location: {
country: 'USA',
city: 'New York'
},
department: {
name: 'Random',
location: {
name: 'Somewhere',
geo: {
lat: -23.658,
long: 2.556
}
}
}
},
rank: 6
};
// This is the structure of `attributes3`.
export const attributes3Struct = {
fname: null,
lname: null,
sensitive: {
very: {
secret: null
},
email: null,
phone: null,
SSN: null,
'employee-id': null
},
lessSensitive: {
location: {
country: null,
city: null
},
department: {
name: null,
location: {
name: null,
geo: {
lat: null,
long: null
}
}
}
},
rank: null
};
// 4th attribute set.
export const attributes4 = {
fname: 'John',
lname: 'Smith',
sensitive: {
secret: 'another-pseudorandom-secret',
email: '[email protected]',
'user-id': 'user:123-xyz-#'
},
'poll-id': 'test-poll',
'registration-id': '12345671209'
};
// This is the structure of `attributes4`. Similar to `attributes1Struct`, does not contain attribute values but the names
// and the structure of `attributes4`
export const attributes4Struct = {
fname: null,
lname: null,
sensitive: {
secret: null,
email: null,
'user-id': null
},
'poll-id': null,
'registration-id': null
};
// The default encoder
export const defaultEncoder = (v: unknown) => {
// @ts-ignore
return Signature.encodeMessageForSigning(stringToBytes(v.toString()));
};
// Create an encoder for attributes with various kinds of values.
export const encoders = new Map<string, EncodeFunc>();
encoders.set('timeOfBirth', Encoder.positiveIntegerEncoder());
encoders.set('weight', Encoder.positiveIntegerEncoder());
encoders.set('physical.weight', Encoder.positiveIntegerEncoder());
// height contains at most 1 decimal place
encoders.set('height', Encoder.positiveDecimalNumberEncoder(1));
encoders.set('physical.height', Encoder.positiveDecimalNumberEncoder(1));
// BMI contains at most 2 decimal place
encoders.set('BMI', Encoder.positiveDecimalNumberEncoder(2));
encoders.set('physical.BMI', Encoder.positiveDecimalNumberEncoder(2));
// score contains at most 1 decimal place and its minimum value is -100
encoders.set('score', Encoder.decimalNumberEncoder(-100, 1));
// latitude contains at most 3 decimal places (in this example) and its minimum value is -90
encoders.set('lessSensitive.department.location.geo.lat', Encoder.decimalNumberEncoder(-90, 3));
// longitude contains at most 3 decimal places (in this example) and its minimum value is -180
encoders.set('lessSensitive.department.location.geo.long', Encoder.decimalNumberEncoder(-180, 3));
encoders.set('SSN', (v: unknown) => {
// @ts-ignore
return Signature.reversibleEncodeStringForSigning(v);
});
encoders.set('sensitive.SSN', (v: unknown) => {
// @ts-ignore
return Signature.reversibleEncodeStringForSigning(v);
});
encoders.set('rank', Encoder.positiveIntegerEncoder());
export const GlobalEncoder = new Encoder(encoders, Encoder.defaultEncodeFunc());