-
Notifications
You must be signed in to change notification settings - Fork 4
/
merkle.test.ts
498 lines (455 loc) · 14.9 KB
/
merkle.test.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import Decimal from "decimal.js";
import { describe, expect, test } from "@jest/globals";
import _userLiabJson_1 from "./testData/userLiab_1.json";
import _auditRootsJson_1 from "./testData/auditRoots_1.json";
import _userLiabJson_2 from "./testData/userLiab_2.json";
import _auditRootsJson_2 from "./testData/auditRoots_2.json";
import _userLiabJson_3 from "./testData/userLiab_3.json";
import _auditRootsJson_3 from "./testData/auditRoots_3.json";
import _userLiabJson_4 from "./testData/userLiab_4.json";
import _auditRootsJson_4 from "./testData/auditRoots_4.json";
import _userLiabJson_5 from "./testData/userLiab_5.json";
import _auditRootsJson_5 from "./testData/auditRoots_5.json";
import _userLiabJson_6 from "./testData/userLiab_6.json";
import _auditRootsJson_6 from "./testData/auditRoots_6.json";
import _userLiabJson_7 from "./testData/userLiab_7.json";
import _auditRootsJson_8 from "./testData/auditRoots_7.json";
import _auditRootsJson_9 from "./testData/auditRoots_8.json";
import {
audCommHashCheck,
AuditRootInterface,
buildMerkleTree,
fullLeafHashDerive,
UserLiabilitiesInterface,
validateProof,
validateUserLiabilities,
} from "./merkle";
const userLiabJson_1 = _userLiabJson_1 as unknown as UserLiabilitiesInterface;
const auditRootsJson_1 = _auditRootsJson_1 as unknown as AuditRootInterface;
const userLiabJson_2 = _userLiabJson_2 as unknown as UserLiabilitiesInterface;
const auditRootsJson_2 = _auditRootsJson_2 as unknown as AuditRootInterface;
const userLiabJson_3 = _userLiabJson_3 as unknown as UserLiabilitiesInterface;
const auditRootsJson_3 = _auditRootsJson_3 as unknown as AuditRootInterface;
const userLiabJson_4 = _userLiabJson_4 as unknown as UserLiabilitiesInterface;
const auditRootsJson_4 = _auditRootsJson_4 as unknown as AuditRootInterface;
const userLiabJson_5 = _userLiabJson_5 as unknown as UserLiabilitiesInterface;
const auditRootsJson_5 = _auditRootsJson_5 as unknown as AuditRootInterface;
const userLiabJson_6 = _userLiabJson_6 as unknown as UserLiabilitiesInterface;
const auditRootsJson_6 = _auditRootsJson_6 as unknown as AuditRootInterface;
const userLiabJson_7 = _userLiabJson_7 as unknown as UserLiabilitiesInterface;
const auditRootsJson_8 = _auditRootsJson_8 as unknown as AuditRootInterface;
const auditRootsJson_9 = _auditRootsJson_9 as unknown as AuditRootInterface;
// Required to provide precise computation of currency balances
Decimal.set({
precision: 1000,
toExpNeg: -1000,
toExpPos: 1000,
});
describe("Proof of solvency", () => {
test("Full leaf hash derive", async () => {
const leafHash = await fullLeafHashDerive(
"AAAAAAAAAAAAAAAAAAAAAA",
"SBPOL20221224",
"BTC",
0,
"20000000000000000000000000000000",
new Decimal("1.2"),
);
expect(leafHash).toBe(
"268f465e7d52d00ecb4e7eea67efa26a29073e7fc808ffd62e45413f5ea2160f",
);
});
test("Audit commitment hash Ok", async () => {
const auditIdToData = new Map(
_auditRootsJson_6.audit.partitions.map((audit) => [
audit.id,
{
currency: audit.currency,
liability: new Decimal(audit.commitment.liability),
digest: audit.commitment.digest,
},
]),
);
const commitsMatch = await audCommHashCheck(
auditIdToData,
_auditRootsJson_6.audit.commitment.digest,
);
expect(commitsMatch).toBeTruthy();
});
test("Audit commitment hash Ok 2", async () => {
const auditIdToData = new Map(
auditRootsJson_8.audit.partitions.map((audit) => [
audit.id,
{
currency: audit.currency,
liability: new Decimal(audit.commitment.liability),
digest: audit.commitment.digest,
},
]),
);
const commitsMatch = await audCommHashCheck(
auditIdToData,
auditRootsJson_8.audit.commitment.digest,
);
expect(commitsMatch).toBeTruthy();
});
test("Build Merkle tree", async () => {
const expectedTree = [
{
liability: new Decimal(650.3057523),
digest:
"250c240f6478ca71dcc623753efbbb5952514a225323d7c7153c87424aa18f74",
},
{
liability: new Decimal(263.5596723),
digest:
"343d351892e5049ae79b84fb1210d05ea44d3d0b69990ffbe598e634fa0f41fc",
},
{
liability: new Decimal(386.74608),
digest:
"5471bbed700db2385e03a18302d456547fd8e2220b2ddcc882c456a6ae8c68f8",
},
{
liability: new Decimal(7.7644723),
digest:
"77933b1f697622b5660c7be3fe52fb253aca11a9568fc4735fa73fc294fb5585",
},
{
liability: new Decimal(255.7952),
digest:
"6d5013bdd8364d12fe83ff228c72f528b124cc507bcbeebf3fedf5907dda530b",
},
{
liability: new Decimal(377.69068),
digest:
"b97a1c1bb1bc42351d53702e0801e27dc712ad1e265d3bee8e048e73c03d3494",
},
{
liability: new Decimal(9.0554),
digest:
"bc17b9eb4ebf84a8b73a555f2a334da4b753ae0fce6485bb602de7f5551f3d41",
},
{
liability: new Decimal(2.542),
digest:
"876c9e7f5222d843ab1b4c00b4d856ad9cfa222e28a2e5792da8b28992a3ff8d",
},
{
liability: new Decimal(5.2224723),
digest:
"4e16ff8cda628474156680c4640fce27093a1a012bd588020f1d47c3ddc7e80c",
},
{
liability: new Decimal(7.45),
digest:
"a5f2a333f391626b31567972d3b91c8079016f93c0590c47f39e68da0d21f670",
},
{
liability: new Decimal(248.3452),
digest:
"0b727574d0d3bb35fa6de5433bb4993e14abba3daf45ec737ab00b9d7bf51f13",
},
{
liability: new Decimal(375.34534),
digest:
"8aa9aa3718f2e89833fe21d2948786c5982600a3f6e3945a2f2624d4ae577869",
},
{
liability: new Decimal(2.34534),
digest:
"54e0ac1f0a1596cfbba2cacb95ea6d159f66abcb6fee488e13d9bf22f116e3ec",
},
{
liability: new Decimal(6.9524),
digest:
"8ae884c6678476a8b4be466920c94842d5a458895c028796644f0b582554cb17",
},
{
liability: new Decimal(2.103),
digest:
"9020c9622c0328d7094f4b249f04c8fe6ed1e17caddced104305e680493957ea",
},
];
const jsonLeaves = [
{
liability: 2.542,
digest:
"876c9e7f5222d843ab1b4c00b4d856ad9cfa222e28a2e5792da8b28992a3ff8d",
},
{
liability: 5.2224723,
digest:
"4e16ff8cda628474156680c4640fce27093a1a012bd588020f1d47c3ddc7e80c",
},
{
liability: 7.45,
digest:
"a5f2a333f391626b31567972d3b91c8079016f93c0590c47f39e68da0d21f670",
},
{
liability: 248.3452,
digest:
"0b727574d0d3bb35fa6de5433bb4993e14abba3daf45ec737ab00b9d7bf51f13",
},
{
liability: 375.34534,
digest:
"8aa9aa3718f2e89833fe21d2948786c5982600a3f6e3945a2f2624d4ae577869",
},
{
liability: 2.34534,
digest:
"54e0ac1f0a1596cfbba2cacb95ea6d159f66abcb6fee488e13d9bf22f116e3ec",
},
{
liability: 6.9524,
digest:
"8ae884c6678476a8b4be466920c94842d5a458895c028796644f0b582554cb17",
},
{
liability: 2.103,
digest:
"9020c9622c0328d7094f4b249f04c8fe6ed1e17caddced104305e680493957ea",
},
];
let testLeaves = [];
for (const element of jsonLeaves) {
testLeaves.push({
liability: new Decimal(element.liability),
digest: element.digest,
});
}
const testTree = await buildMerkleTree(testLeaves);
expect(testTree).toEqual(expectedTree);
});
test("Validate proof", async () => {
const witnesses = [
{
liability: new Decimal("2.34534"),
digest:
"54e0ac1f0a1596cfbba2cacb95ea6d159f66abcb6fee488e13d9bf22f116e3ec",
},
{
liability: new Decimal("9.0554"),
digest:
"bc17b9eb4ebf84a8b73a555f2a334da4b753ae0fce6485bb602de7f5551f3d41",
},
{
liability: new Decimal("263.5596723"),
digest:
"343d351892e5049ae79b84fb1210d05ea44d3d0b69990ffbe598e634fa0f41fc",
},
{
liability: new Decimal("650.3057523"),
digest:
"250c240f6478ca71dcc623753efbbb5952514a225323d7c7153c87424aa18f74",
},
];
const leafNode = {
liability: new Decimal("375.34534"),
digest:
"8aa9aa3718f2e89833fe21d2948786c5982600a3f6e3945a2f2624d4ae577869",
};
const leafIndex = 4;
const out = await validateProof(leafNode, leafIndex, witnesses);
expect(out).toBeTruthy();
});
test("Validate user liabilities test data 1", async () => {
const largerTestOut = await validateUserLiabilities(
"20000f995b5305144c082ad3443bf4db",
auditRootsJson_1,
userLiabJson_1,
);
expect(largerTestOut).toEqual([
true,
new Map([
["GBP", new Decimal("1")],
["CHSB", new Decimal("20022")],
]),
]);
});
test("Validate user liabilities test data 2", async () => {
const largerTestOut = await validateUserLiabilities(
"z2Ef6hx6QbRdixkL7c8rdBj",
auditRootsJson_2,
userLiabJson_2,
);
expect(largerTestOut).toEqual([
true,
new Map([["USDC", new Decimal("99.70291")]]),
]);
});
test("Validate user liabilities test data 3", async () => {
const largerTestOut = await validateUserLiabilities(
"z2NRjxCKJiWPh3rfdQVyaTS",
auditRootsJson_3,
userLiabJson_3,
);
expect(largerTestOut).toEqual([
true,
new Map([
["USDC", new Decimal("288.50251128818010359")],
["ETH", new Decimal("1.700273775267302")],
["CHSB", new Decimal("4844.269085")],
]),
]);
});
test("Validate user liabilities test data 4", async () => {
const largerTestOut = await validateUserLiabilities(
"z2NRjxCKJiWPh3rfdQVyaTS",
auditRootsJson_4,
userLiabJson_4,
);
expect(largerTestOut).toEqual([
true,
new Map([
["USDC", new Decimal("288.50251128818010359")],
["ETH", new Decimal("1.700273775267302")],
["CHSB", new Decimal("4844.269085")],
]),
]);
});
test("Validate user liabilities test data 5", async () => {
const largerTestOut = await validateUserLiabilities(
"zLwByd5Jee3VsnEMCaUGeGH",
auditRootsJson_5,
userLiabJson_5,
);
expect(largerTestOut).toEqual([
true,
new Map([["USDC", new Decimal("99.70291")]]),
]);
});
test("Validate user liabilities test data 6", async () => {
const largerTestOut = await validateUserLiabilities(
"D2tVdEWt1MooLsMa6ryPGj",
auditRootsJson_6,
userLiabJson_6,
);
expect(largerTestOut).toEqual([
true,
new Map([
["CHSB", new Decimal("44.81425")],
["PLN", new Decimal("10")],
["EUR", new Decimal("14558")],
]),
]);
});
test("Validate user liabilities test data 7", async () => {
const largerTestOut = await validateUserLiabilities(
"PaY746f8DsYiK8s33J33Jk",
auditRootsJson_6,
userLiabJson_7,
);
expect(largerTestOut).toEqual([
true,
new Map([["USDC", new Decimal("99.70291")]]),
]);
});
test("Validate user liabilities test data 6 - un-ordered audit JSON data", async () => {
const largerTestOut = await validateUserLiabilities(
"D2tVdEWt1MooLsMa6ryPGj",
auditRootsJson_9,
userLiabJson_6,
);
expect(largerTestOut).toEqual([
true,
new Map([
["CHSB", new Decimal("44.81425")],
["PLN", new Decimal("10")],
["EUR", new Decimal("14558")],
]),
]);
});
test("Negative test - wrong user credential", async () => {
const largerTestOut = await validateUserLiabilities(
"PaY746f8DsYiK8s33J34Jk",
auditRootsJson_6,
userLiabJson_7,
);
expect(largerTestOut).toEqual([false, null]);
});
test("Negative test - Audit commitment hash not Ok", async () => {
const auditIdToData = new Map(
_auditRootsJson_6.audit.partitions.map((audit) => [
audit.id,
{
currency: audit.currency,
liability: new Decimal(audit.commitment.liability),
digest: audit.commitment.digest,
},
]),
);
const auditHash = await audCommHashCheck(
auditIdToData,
"badCommitmentDigest",
);
expect(auditHash).toBeFalsy();
});
test("Negative test - Wrong audit commitment hash - 2", async () => {
let auditRootsMalformed = JSON.parse(JSON.stringify(auditRootsJson_6));
auditRootsMalformed["audit"]["commitment"]["digest"] =
"017a68ca267d6369f24fb749151c143b43c775451e794e0a287887ad31dbb8d2";
const largerTestOut = await validateUserLiabilities(
"PaY746f8DsYiK8s33J33Jk",
auditRootsMalformed,
userLiabJson_7,
);
expect(largerTestOut).toEqual([false, null]);
});
test("Negative test - Mismatch of audit ID", async () => {
let auditRootsMalformed = JSON.parse(JSON.stringify(auditRootsJson_6));
auditRootsMalformed["audit"]["id"] = "SBPOL20230112";
const largerTestOut = await validateUserLiabilities(
"PaY746f8DsYiK8s33J33Jk",
auditRootsMalformed,
userLiabJson_7,
);
expect(largerTestOut).toEqual([false, null]);
});
test("Negative test - Too small number of witnesses", async () => {
let userLiabMalformed = JSON.parse(JSON.stringify(userLiabJson_7));
userLiabMalformed["liabilities"][0]["proof"]["witnesses"].splice(0, 15);
const largerTestOut = await validateUserLiabilities(
"PaY746f8DsYiK8s33J33Jk",
auditRootsJson_6,
userLiabMalformed,
);
expect(largerTestOut).toEqual([false, null]);
});
test("Negative test - Negative liability", async () => {
let userLiabMalformed = JSON.parse(JSON.stringify(userLiabJson_7));
userLiabMalformed["liabilities"][0]["liability"] = "-1.3";
const largerTestOut = await validateUserLiabilities(
"PaY746f8DsYiK8s33J33Jk",
auditRootsJson_6,
userLiabMalformed,
);
expect(largerTestOut).toEqual([false, null]);
});
test("Negative test - negative witness", async () => {
let userLiabMalformed = JSON.parse(JSON.stringify(userLiabJson_7));
userLiabMalformed["liabilities"][0]["proof"]["witnesses"][0]["liability"] =
"-2.4";
const largerTestOut = await validateUserLiabilities(
"PaY746f8DsYiK8s33J33Jk",
auditRootsJson_6,
userLiabMalformed,
);
expect(largerTestOut).toEqual([false, null]);
});
test("Negative test - wrong Merkle Root liability", async () => {
let auditRootsMalformed = JSON.parse(JSON.stringify(auditRootsJson_6));
auditRootsMalformed["audit"]["partitions"][21]["commitment"]["liability"] =
"559441.38290498058898063";
auditRootsMalformed["audit"]["commitment"]["digest"] =
"97c7b2dee729c32fb9ef4d05c4921b5ef0d2d17b4e5842d5a24972d8ab162073";
const largerTestOut = await validateUserLiabilities(
"PaY746f8DsYiK8s33J33Jk",
auditRootsMalformed,
userLiabJson_7,
);
expect(largerTestOut).toEqual([false, null]);
});
});