-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnemonic.test.ts
173 lines (141 loc) · 4.98 KB
/
mnemonic.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
import dcrypto from "../src";
import {
crypto_sign_ed25519_PUBLICKEYBYTES,
crypto_sign_ed25519_SECRETKEYBYTES,
} from "../src/utils/interfaces";
const arraysAreEqual = (arr1: Uint8Array, arr2: Uint8Array): boolean => {
const len = arr1.length;
if (len !== arr2.length) return false;
for (let i = 0; i < len; i++) {
if (arr1[i] !== arr2[i]) return false;
}
return true;
};
describe("Signing and verifying with Ed25519 keys test suite.", () => {
test("Loading the wordlist works", () => {
const wordlist = dcrypto.wordlist;
expect(wordlist[0]).toBe("abandon");
});
test("Mnemonic generation works.", async () => {
const mnemonic = await dcrypto.generateMnemonic();
const validate = await dcrypto.validateMnemonic(mnemonic);
expect(validate).toBe(true);
});
test("Mnemonic generation with different entropy works.", async () => {
const mnemonic1 = await dcrypto.generateMnemonic(128);
expect(mnemonic1.split(" ").length).toEqual(12);
const mnemonic2 = await dcrypto.generateMnemonic(160);
expect(mnemonic2.split(" ").length).toEqual(15);
const mnemonic3 = await dcrypto.generateMnemonic(192);
expect(mnemonic3.split(" ").length).toEqual(18);
const mnemonic4 = await dcrypto.generateMnemonic(224);
expect(mnemonic4.split(" ").length).toEqual(21);
const mnemonic5 = await dcrypto.generateMnemonic(256);
expect(mnemonic5.split(" ").length).toEqual(24);
const mnemonic6 = await dcrypto.generateMnemonic(288);
expect(mnemonic6.split(" ").length).toEqual(27);
const mnemonic7 = await dcrypto.generateMnemonic(320);
expect(mnemonic7.split(" ").length).toEqual(30);
const mnemonic8 = await dcrypto.generateMnemonic(352);
expect(mnemonic8.split(" ").length).toEqual(33);
const mnemonic9 = await dcrypto.generateMnemonic(384);
expect(mnemonic9.split(" ").length).toEqual(36);
const mnemonic10 = await dcrypto.generateMnemonic(416);
expect(mnemonic10.split(" ").length).toEqual(39);
const mnemonic11 = await dcrypto.generateMnemonic(448);
expect(mnemonic11.split(" ").length).toEqual(42);
const mnemonic12 = await dcrypto.generateMnemonic(480);
expect(mnemonic12.split(" ").length).toEqual(45);
const mnemonic13 = await dcrypto.generateMnemonic(512);
expect(mnemonic13.split(" ").length).toEqual(48);
});
test("Generating a new keypair from mnemonic seed works.", async () => {
const mnemonic = await dcrypto.generateMnemonic();
const keypair = await dcrypto.keyPairFromMnemonic(mnemonic);
const anotherKeypair = await dcrypto.keyPairFromMnemonic(mnemonic);
const keyPairWithPwd = await dcrypto.keyPairFromMnemonic(
mnemonic,
"Some password",
);
const anotherKeyPairWithPwd = await dcrypto.keyPairFromMnemonic(
mnemonic,
"Some password",
);
expect(typeof keypair === "object").toBe(true);
expect(keypair.secretKey.length).toBe(crypto_sign_ed25519_SECRETKEYBYTES);
expect(keypair.publicKey.length).toBe(crypto_sign_ed25519_PUBLICKEYBYTES);
expect(arraysAreEqual(keypair.secretKey, anotherKeypair.secretKey)).toBe(
true,
);
expect(
arraysAreEqual(keyPairWithPwd.secretKey, anotherKeyPairWithPwd.secretKey),
).toBe(true);
});
test("When an invalid mnemonic gets validated it should be false.", async () => {
const mnemonicNotMultipleOfThree = `\
abandon \
ability \
about \
above \
absent \
absorb \
abstract \
absurd \
abuse \
access \
accident`;
const mnemonicHasWordNotInWordlist =
mnemonicNotMultipleOfThree.concat(" aballon");
const mnemonicTooShortButMultipleOfThree = mnemonicNotMultipleOfThree
.replace("abandon ", "")
.replace("ability ", "");
const mnemonicTooLongButMultipleOfThree = mnemonicNotMultipleOfThree.concat(
" advice",
" aerobic",
" affair",
" afford",
" afraid",
" again",
" age",
" agent",
" agree",
" ahead",
" aim",
" air",
" airport",
" aisle",
" alarm",
" album",
" alcohol",
" yellow",
" you",
" young",
" youth",
" zebra",
" zero",
" zone",
" zoo",
" wood",
" wool",
" word",
);
const mnemon = await dcrypto.generateMnemonic(256);
const mnemonArray = mnemon.split(" ");
const lastWord = mnemonArray[mnemonArray.length - 1];
const wrongChecksum = mnemon.replace(` ${lastWord}`, " zone");
const valid1 = await dcrypto.validateMnemonic(mnemonicNotMultipleOfThree);
const valid2 = await dcrypto.validateMnemonic(mnemonicHasWordNotInWordlist);
const valid3 = await dcrypto.validateMnemonic(
mnemonicTooShortButMultipleOfThree,
);
const valid4 = await dcrypto.validateMnemonic(
mnemonicTooLongButMultipleOfThree,
);
const valid5 = await dcrypto.validateMnemonic(wrongChecksum);
expect(valid1).toBe(false);
expect(valid2).toBe(false);
expect(valid3).toBe(false);
expect(valid4).toBe(false);
expect(valid5).toBe(false);
});
});