-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSS_test_API.ts
72 lines (65 loc) · 1.98 KB
/
SSS_test_API.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
import axios, { AxiosError } from "axios";
import bigInt, { BigInteger } from "big-integer";
import { SSS, generate_secret } from "./SSS_web_fns";
interface Share {
index: number;
value: {
value: string;
prime: string;
};
}
interface SSSSharesResponse {
k: number;
n: number;
shares: Share[];
}
interface SSSSharesBody {
secret: string;
shares: Share[];
team_message: string;
}
function recover_secret(input: SSSSharesResponse) {
const prime = bigInt(input.shares[0].value.prime, 16);
const points = [];
for (let share of input.shares) {
points.push([bigInt(share.index), bigInt(share.value.value, 16)]);
}
console.log("input n", input.n);
console.log("input k", input.k);
console.log("input prime", prime);
console.log("input points", points);
const recoveredSecret = generate_secret(points, prime);
return recoveredSecret;
}
function generate_shares(secret: BigInteger, n: number, threshold: number) {
const shares = SSS(secret, n, threshold);
const body: SSSSharesBody = {
secret: secret.toString(),
shares: shares.shares.map((share) => ({
index: share[0].toJSNumber(),
value: { value: share[1].toString(16), prime: shares.prime.toString(16) },
})),
team_message: "You have been Pwned",
};
// console.log("generated shares: ", body);
return body;
}
function verifyWithAPI() {
axios
.get<SSSSharesResponse>("https://hash-effect.onrender.com/sss/shares")
.then((resp) => resp.data)
.then((input) => {
const recoveredSecret = recover_secret(input);
const shares = generate_shares(recoveredSecret, input.n, input.k);
// console.log(JSON.stringify(shares, null, 4));
axios
.post("https://hash-effect.onrender.com/sss/verify", shares)
.then((resp) => resp.data)
.then((output) => {
console.log(output);
})
.catch((err: AxiosError) => console.error(err.toJSON()));
})
.catch((err: AxiosError) => console.error(err.toJSON()));
}
verifyWithAPI();