-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKPass.js
296 lines (278 loc) · 8.89 KB
/
KPass.js
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { Signer } from "../crosschain/signer";
import { decrypt, encrypt } from "../crosschain/unlockable";
import "../ethereum/ERC721Unlockable.d";
import { wait } from "../util/promises";
import { hash } from "./section";
import { verify } from "./verifiableID";
/**
* Given an array of `did.EncryptedSections` keys, determines a minimal set of
* `did.EncryptedSections` keys which, when unlocked, would cover all the
* desired `did.Section`'s.
*
* The selected unlockables are returned as an array of values from the
* `encryptedSectionKeys` array.
*
* @param {!Array<string>} encryptedSectionsKeys
* @param {!Array<string>} sectionKeys
* @return {!Array<string>} unlockable keys which together have all the desired
* `did.Section`s.
*/
const selectEncryptedSections = (encryptedSectionsKeys, sectionKeys) => {
// If there is a solution with 1 or 2 unlockables, we'll find the optimal
// solution using exhaustive search, which takes O(n^2) time where
// `n = |nft.unlockables|`. Otherwise, we'll resort to a greedy approach.
/** @const {!Set<string>} */
const sks = new Set(sectionKeys);
/**
* @const {Array<{
* key: string,
* inc: !Set<string>,
* exc: !Set<string>,
* }>}
*/
const arr = [];
{
/** @type {number} */
let bestI = -1;
/** @type {number} */
let bestExc = 1e9;
for (const key of encryptedSectionsKeys) {
/** @const {!Array<string>} */
const sections = key.split(",");
/** @const {!Set<string>} */
const inc = new Set(sections.filter((e) => sks.has(e)));
/** @const {!Set<string>} */
const exc = new Set(sections.filter((e) => !sks.has(e)));
if (inc.size == sks.size && exc.size < bestExc) {
bestI = arr.length;
bestExc = exc.size;
}
arr.push({ key, inc, exc });
}
// There is a solution with 1 unlockable.
if (bestI >= 0)
return [arr[bestI].key];
}
/**
* Calculates 100 * |A \cup B| + |A| + |B|.
*
* @param {!Set<string>} A
* @param {!Set<string>} B
* @return {number}
*/
const score = (A, B) => {
/** @type {number} */
let count = 101 * (A.size + B.size);
for (const b of B)
count -= +A.has(b) * 100;
return count;
}
/** @const {number} */
const n = arr.length;
/** @type {number} */
let bestI = -1;
/** @type {number} */
let bestJ = -1;
/** @type {number} */
let bestExc = 1e9;
for (let i = 0; i < n; ++i)
for (let j = i + 1; j < n; ++j)
if (sectionKeys.every((x) => arr[i].inc.has(x) || arr[j].inc.has(x))) {
const exc = score(arr[i].exc, arr[j].exc);
if (exc < bestExc) {
bestI = i;
bestJ = j;
bestExc = exc;
}
}
// There is a solution with 2 unlockables.
if (bestI >= 0)
return [arr[bestI].key, arr[bestJ].key];
// Since there are no solutions with 1 or 2 unlockables, we'll resort to a
// greedy algorithm.
arr.sort((a, b) => (b.inc.size - b.exc.size) - (a.inc.size - a.exc.size));
/** @const {!Array<string>} */
const res = [];
for (const entry of arr) {
if (!sks.size) break;
/** @type {boolean} */
let helpful = false;
for (const elm of entry.inc)
helpful ||= sks.delete(elm);
if (helpful)
res.push(entry.key);
}
return res;
}
/**
* @param {!eth.ERC721Unlockable} nft
* @param {!Array<string>} sectionNames
* @param {!Signer} signer
* @param {string} address
* @return {!Promise<did.DecryptedSections>}
*/
const fromUnlockableNFT = async (nft, sectionNames, signer, address) => {
/** @const {!Array<string>} */
const encryptedSectionsKeys = selectEncryptedSections(
Object.keys(nft.unlockables),
sectionNames
);
/** @const {did.DecryptedSections} */
const decryptedSections = {};
for (let i = 0; i < encryptedSectionsKeys.length; ++i) {
if (i > 0)
await wait(100);
/** @type {!crosschain.Unlockable} */
const encryptedSections = /** @type {!crosschain.Unlockable} */(
nft.unlockables[encryptedSectionsKeys[i]]);
/** @const {?string} */
const decryptedText = await decrypt(encryptedSections, signer, address);
if (decryptedText)
Object.assign(decryptedSections,
/** @type {did.DecryptedSections} */(JSON.parse(decryptedText)));
}
/** @const {!Set<string>} */
const sectionNamesSet = new Set(sectionNames);
for (const section in decryptedSections)
if (!sectionNamesSet.has(section)) delete decryptedSections[section];
return decryptedSections;
}
/**
* Verifies the `did.VerifiableID`'s and removes the ones that fail to verify.
* Further, strips the proofs from those that were succesfully verified.
*
* @param {did.DecryptedSections} decryptedSections
* @param {!Object<string, string>} verifyKeys
* @return {!Promise<did.DecryptedSections>}
*/
const checkVerifiableIDs = (decryptedSections, verifyKeys) => {
/** @const {string} */
const localIdNumber = /** @type {!did.PersonInfo} */(
decryptedSections["personInfo"]).localIdNumber;
/**
* @param {string} name
* @return {!Promise<void>}
*/
const verifySingle = (name) => {
/** @const {did.VerifiableID} */
const verifiableID = /** @type {did.VerifiableID} */(decryptedSections[name]);
return verifiableID
? verify(verifiableID, localIdNumber, verifyKeys[name])
.then((isValid) => {
if (isValid) {
delete verifiableID.wesolowskiL;
delete verifiableID.wesolowskiP;
delete verifiableID.x;
} else
delete decryptedSections[name];
})
: Promise.resolve();
}
return Promise.all([verifySingle("exposureReport"), verifySingle("humanID")])
.then(() => decryptedSections);
}
/**
* @typedef {{
* userPrompt: string,
* sectionNames: !Array<string>
* }}
*/
const SectionGroup = {};
/**
* @param {!eth.ERC721Metadata} metadata
* @param {did.DecryptedSections} decryptedSections
* @param {!Array<SectionGroup>} sectionGroups
* @param {!Signer} signer
* @param {string} address
* @return {!Promise<!eth.ERC721Unlockable>}
*/
const toUnlockableNFT = async (metadata, decryptedSections, sectionGroups, signer, address) => {
/** @const {!Object<string, !crosschain.Unlockable>} */
const unlockables = {};
for (let i = 0; i < sectionGroups.length; ++i) {
/** @const {!Promise<void>} */
const duraklatıcı = wait(2000);
/** @const {did.DecryptedSections} */
const sections = {};
for (const /** @type {string} */ name of sectionGroups[i].sectionNames)
sections[name] = decryptedSections[name];
/** @const {string} */
const unlockableKey = sectionGroups[i].sectionNames.sort().join(",");
unlockables[unlockableKey] = await encrypt(JSON.stringify(sections),
sectionGroups[i].userPrompt,
"promptsign-sha256-aes-ctr",
signer,
address
);
if (i < sectionGroups.length - 1)
await duraklatıcı;
}
return /** @type {!eth.ERC721Unlockable} */({
...metadata,
unlockables
});
}
/**
* @param {!Array<did.DecryptedSections>} decryptedSectionsList
* @param {string} commitmentR
* @param {string} commitmentAnonR
* @param {number} signerCountNeeded
* @return {did.DecryptedSections}
*/
const combineMultiple = (
decryptedSectionsList,
commitmentR,
commitmentAnonR,
signerCountNeeded
) => {
/** @const {did.DecryptedSections} */
const combined = {};
if (decryptedSectionsList.length < signerCountNeeded)
return combined;
/** @const {!Set<string>} */
const sectionNames = new Set();
for (const decryptedSections of decryptedSectionsList)
for (const key in decryptedSections)
sectionNames.add(key);
for (const key of sectionNames) {
/** @const {!Object<string, !Array<!did.Section>>} */
const hashToSections = {};
for (const decryptedSections of decryptedSectionsList)
if (key in decryptedSections
&& decryptedSections[key].secp256k1
&& decryptedSections[key].secp256k1.length) {
const h = hash(key, decryptedSections[key]);
(hashToSections[h] ||= []).push(decryptedSections[key]);
}
/**
* Find the most frequent hash group.
*
* @type {!Array<!did.Section>}
*/
let mostFreq = []
for (const h in hashToSections)
if (hashToSections[h].length > mostFreq.length)
mostFreq = hashToSections[h];
if (mostFreq.length >= signerCountNeeded) {
combined[key] = mostFreq[0];
combined[key].commitmentR = key == "humanID"
? commitmentAnonR : commitmentR;
delete combined[key].commitment;
for (let i = 1; i < mostFreq.length; ++i)
combined[key].secp256k1.push(
.../** @type {!Array<string>} */(mostFreq[i].secp256k1));
if (combined[key].minaSchnorr)
for (let i = 1; i < mostFreq.length; ++i)
combined[key].minaSchnorr.push(...(mostFreq[i].minaSchnorr || []));
}
}
return combined;
}
export {
checkVerifiableIDs,
combineMultiple,
fromUnlockableNFT,
SectionGroup,
selectEncryptedSections,
toUnlockableNFT
};