-
Notifications
You must be signed in to change notification settings - Fork 4
/
self_verification.ts
81 lines (71 loc) · 2.02 KB
/
self_verification.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
import Decimal from "decimal.js";
Decimal.set({
precision: 1000,
toExpNeg: -1000,
toExpPos: 1000,
});
import {
validateUserLiabilities,
AuditRootInterface,
UserLiabilitiesInterface,
} from "./merkle";
const fetchAuditRoots = async (
auditId: string,
): Promise<AuditRootInterface> => {
const auditRootsUrl = `https://app.swissborg.io/solvency/v1/audit/${auditId}`;
const auditRootsResponse = await fetch(auditRootsUrl, {
headers: {
accept: "application/json",
},
});
if (!auditRootsResponse.ok) {
throw new Error("incorrect AUDIT_ID");
}
const auditRoots: AuditRootInterface = await auditRootsResponse.json();
return auditRoots;
};
const fetchUserLiabilities = async (
auditId: string,
auditCredentialId: string,
): Promise<UserLiabilitiesInterface> => {
const userLiabilitiesUrl = `https://app.swissborg.io/solvency/v1/audit/${auditId}/user-liabilities`;
const userLiabilitiesResponse = await fetch(userLiabilitiesUrl, {
method: "POST",
headers: {
accept: "application/json",
"content-type": "application/json",
},
body: JSON.stringify({ credential: auditCredentialId }),
});
if (!userLiabilitiesResponse.ok) {
throw new Error("incorrect AUDIT_CREDENTIAL_ID");
}
const userLiabilities: UserLiabilitiesInterface =
await userLiabilitiesResponse.json();
return userLiabilities;
};
const main = async () => {
const auditCredentialId = process.argv[2];
if (!auditCredentialId) {
throw new Error("missing AUDIT_CREDENTIAL_ID");
}
const auditId = process.argv[3];
if (!auditId) {
throw new Error("missing AUDIT_ID");
}
const userLiabilities = await fetchUserLiabilities(
auditId,
auditCredentialId,
);
const auditRoots = await fetchAuditRoots(auditId);
const inclusionProof = await validateUserLiabilities(
auditCredentialId,
auditRoots,
userLiabilities,
);
if (!inclusionProof[0]) {
throw new Error("validation error");
}
return inclusionProof[1];
};
main().then(console.table).catch(console.error);