-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.mjs
148 lines (124 loc) · 4.87 KB
/
test.mjs
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
import dcrypto from "@deliberative/crypto";
const main = async () => {
try {
// 12 Words from dictionary create random seed for Ed25519 private key.
const mnemonic = await dcrypto.generateMnemonic();
console.log(`Mnemonic with 128bit entropy => 12 words: ${mnemonic}`);
// 15 Words from dictionary create random seed for Ed25519 private key.
const mnemonic1 = await dcrypto.generateMnemonic(160);
console.log(`Mnemonic with 160bit entropy => 15 words: ${mnemonic1}`);
// 20 Words from dictionary create random seed for Ed25519 private key.
const mnemonic2 = await dcrypto.generateMnemonic(192);
console.log(`Mnemonic with 192bit entropy => 18 words: ${mnemonic2}`);
// 24 Words from dictionary create random seed for Ed25519 private key.
const mnemonic3 = await dcrypto.generateMnemonic(224);
console.log(`Mnemonic with 224bit entropy => 21 words: ${mnemonic3}`);
// 28 Words from dictionary create random seed for Ed25519 private key.
const mnemonic4 = await dcrypto.generateMnemonic(256);
console.log(`Mnemonic with 256bit entropy => 24 words: ${mnemonic4}`);
// Keypair is an object representing an Ed25519 keypair with { publicKey: Uint8Array(32), secretKey: Uint8Array(64) }
const keypair = await dcrypto.keyPairFromMnemonic(mnemonic);
console.log(
`Keypair from mnemonic: {\n\
secretKey: ${Buffer.from(keypair.secretKey).toString("hex")}\n\
publicKey: ${Buffer.from(keypair.publicKey).toString("hex")}\n}\
`,
);
// Random Uint8Array array of 32 elements
const message = await dcrypto.randomBytes(32);
console.log(
`Random message to sign: ${Buffer.from(message).toString("hex")}`,
);
// Can also provide mnemonic instead of private key
const signature = await dcrypto.sign(message, keypair.secretKey);
console.log(`Signature: ${Buffer.from(signature).toString("hex")}`);
const verify = await dcrypto.verify(message, signature, keypair.publicKey);
console.log(`Verification: ${verify}`); // true
const hash = await dcrypto.sha512(message);
console.log(
`SHA512 of the random message: ${Buffer.from(hash).toString("hex")}`,
);
const keypair2 = await dcrypto.keyPair();
console.log(
`New random keypair: {\n\
secretKey: ${Buffer.from(keypair2.secretKey).toString("hex")}\n\
publicKey: ${Buffer.from(keypair2.publicKey).toString("hex")}\n}\
`,
);
const encrypted = await dcrypto.encryptForwardSecrecy(
message,
keypair2.publicKey,
hash,
);
console.log(
`AEAD encrypted box for random message and keypair2: ${Buffer.from(
encrypted,
).toString("hex")}`,
);
const decrypted = await dcrypto.decryptForwardSecrecy(
encrypted,
keypair2.secretKey,
hash,
);
console.log(
`Decrypted AEAD box should be equal to random message: \n\
Decrypted: ${Buffer.from(decrypted).toString("hex")} \n\
Original : ${Buffer.from(message).toString("hex")}\n`,
);
const sharesLen = 255;
const threshold = 165;
const shares = await dcrypto.splitSecret(
keypair.secretKey,
sharesLen,
threshold,
);
console.log(
`We split secret key ${Buffer.from(keypair.secretKey).toString(
"hex",
)} into ${sharesLen} shares and we need at least ${threshold} shares to recreate it`,
);
for (let i = 0; i < sharesLen; i++) {
console.log(
`Share #${i + 1} is ${Buffer.from(shares[i]).toString("hex")}`,
);
}
// Should be equal to keypair.secretKey
const sk1 = await dcrypto.restoreSecret(shares);
console.log(
`If we combine all shares then the result ${Buffer.from(sk1).toString(
"hex",
)} should be equal to secret key ${Buffer.from(
keypair.secretKey,
).toString("hex")}`,
);
// Remove 80 shares to see if it will still work
const lessShares = shares.slice(0, shares.length - 80);
const lessSharesRandom = await dcrypto.arrayRandomShuffle(lessShares);
// Should be equal to sk1 and keypair.secretKey
const sk2 = await dcrypto.restoreSecret(lessSharesRandom);
console.log(
`If we combine 60 shares then the result ${Buffer.from(sk2).toString(
"hex",
)} should be equal to secret key ${Buffer.from(
keypair.secretKey,
).toString("hex")}`,
);
// Remove 11 more and now we are bellow the threshold
const evenLessShares = await dcrypto.arrayRandomSubset(
lessShares,
lessShares.length - 11,
);
// Should not be equal to sk1 and sk2.
const sk3 = await dcrypto.restoreSecret(evenLessShares);
console.log(
`If we combine 59 shares then the result ${Buffer.from(sk3).toString(
"hex",
)} will be differet from the secret key ${Buffer.from(
keypair.secretKey,
).toString("hex")}`,
);
} catch (err) {
console.error(err);
}
};
main();