-
Notifications
You must be signed in to change notification settings - Fork 2
/
near-js-encryption-box.test.ts
213 lines (161 loc) · 6.6 KB
/
near-js-encryption-box.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
import { create, open } from './../src';
import { utils } from 'near-api-js';
import { encodeBase64 } from 'tweetnacl-util';
import randomBytes from 'random-bytes';
describe('Creating a secret box with Alice and Bob key pairs randomly generated from NEAR JS LIBRARY', () => {
const keyPairAlice = utils.key_pair.KeyPairEd25519.fromRandom();
const keyPairBob = utils.key_pair.KeyPairEd25519.fromRandom();
const keyPairCharlie = utils.key_pair.KeyPairEd25519.fromRandom();
const messageSent = 'Hello world';
const publicKeyBob = keyPairBob.getPublicKey().toString();
const privateKeyAlice = keyPairAlice.secretKey;
const { secret, nonce } = create(messageSent, publicKeyBob, privateKeyAlice);
it('Should contain the same message sent by Alice opened by Bob', () => {
const publicKeyAlice = keyPairAlice.getPublicKey().toString();
const privateKeyBob = keyPairBob.secretKey;
const messageReceived = open(secret, publicKeyAlice, privateKeyBob, nonce);
expect(messageSent).toBe(messageReceived);
});
it('Should not let Charlie open the box created by Alice and sent to Bob', () => {
const publicKeyAlice = keyPairAlice.getPublicKey().toString();
const privateKeyCharlie = keyPairCharlie.secretKey;
const messageReceived = open(
secret,
publicKeyAlice,
privateKeyCharlie,
nonce
);
expect(messageReceived).toBe(null);
});
});
describe('Creating a secret box with Alice and Bob key pairs generated with NEAR CLI', () => {
const keyPairAlice = {
publicKey: 'ed25519:d9ymaE7DT8ydtEvxgfs4UERofMF749szvniWTRQJBBh',
privateKey:
'09430fbb4310e17cec8c8639d6b2c6c59c28ca5c9fdd9680834737aa1aeadfec',
};
const keyPairBob = {
publicKey: 'ed25519:d9ymaE7DT8ydtEvxgfs4UERofMF749szvniWTRQJBBh',
privateKey:
'09430fbb4310e17cec8c8639d6b2c6c59c28ca5c9fdd9680834737aa1aeadfec',
};
it('Should contain the same message sent by Alice opened by Bob', () => {
const messageSent = 'Hello world';
const publicKeyBob = keyPairBob.publicKey;
const privateKeyAlice = keyPairAlice.privateKey;
const { secret, nonce } = create(
messageSent,
publicKeyBob,
privateKeyAlice
);
const publicKeyAlice = keyPairAlice.publicKey;
const privateKeyBob = keyPairBob.privateKey;
const messageReceived = open(secret, publicKeyAlice, privateKeyBob, nonce);
expect(messageSent).toBe(messageReceived);
});
});
describe('Creating a secret box with Alice and Bob key pairs having ed25519: prefix', () => {
const keyPairAlice = {
publicKey: 'ed25519:d9ymaE7DT8ydtEvxgfs4UERofMF749szvniWTRQJBBh',
privateKey:
'ed25519:09430fbb4310e17cec8c8639d6b2c6c59c28ca5c9fdd9680834737aa1aeadfec',
};
const keyPairBob = {
publicKey: 'ed25519:d9ymaE7DT8ydtEvxgfs4UERofMF749szvniWTRQJBBh',
privateKey:
'ed25519:09430fbb4310e17cec8c8639d6b2c6c59c28ca5c9fdd9680834737aa1aeadfec',
};
it('Should contain the same message sent by Alice opened by Bob', () => {
const messageSent = 'Hello world';
const publicKeyBob = keyPairBob.publicKey;
const privateKeyAlice = keyPairAlice.privateKey;
const { secret, nonce } = create(
messageSent,
publicKeyBob,
privateKeyAlice
);
const publicKeyAlice = keyPairAlice.publicKey;
const privateKeyBob = keyPairBob.privateKey;
const messageReceived = open(secret, publicKeyAlice, privateKeyBob, nonce);
expect(messageSent).toBe(messageReceived);
});
});
describe('Creating a secret box with Alice and Bob key pairs without ed25519: prefix', () => {
const keyPairAlice = {
publicKey: 'd9ymaE7DT8ydtEvxgfs4UERofMF749szvniWTRQJBBh',
privateKey:
'09430fbb4310e17cec8c8639d6b2c6c59c28ca5c9fdd9680834737aa1aeadfec',
};
const keyPairBob = {
publicKey: 'd9ymaE7DT8ydtEvxgfs4UERofMF749szvniWTRQJBBh',
privateKey:
'09430fbb4310e17cec8c8639d6b2c6c59c28ca5c9fdd9680834737aa1aeadfec',
};
it('Should contain the same message sent by Alice opened by Bob', () => {
const messageSent = 'Hello world';
const publicKeyBob = keyPairBob.publicKey;
const privateKeyAlice = keyPairAlice.privateKey;
const { secret, nonce } = create(
messageSent,
publicKeyBob,
privateKeyAlice
);
const publicKeyAlice = keyPairAlice.publicKey;
const privateKeyBob = keyPairBob.privateKey;
const messageReceived = open(secret, publicKeyAlice, privateKeyBob, nonce);
expect(messageSent).toBe(messageReceived);
});
});
describe('Creating a secret box for Alice only', () => {
const keyPairAlice = utils.key_pair.KeyPairEd25519.fromRandom();
const keyPairBob = utils.key_pair.KeyPairEd25519.fromRandom();
const messageSent = 'Hello world';
const privateKeyAlice = keyPairAlice.secretKey;
const publicKeyAlice = keyPairAlice.getPublicKey().toString();
const { secret, nonce } = create(
messageSent,
publicKeyAlice,
privateKeyAlice
);
it('Should contain the same message', () => {
const messageReceived = open(
secret,
publicKeyAlice,
privateKeyAlice,
nonce
);
expect(messageSent).toBe(messageReceived);
});
it('Should not let Bob open the box created by Alice for herself', () => {
const privateKeyBob = keyPairBob.secretKey;
const messageReceived = open(secret, publicKeyAlice, privateKeyBob, nonce);
expect(messageReceived).toBe(null);
});
});
describe('Creating a secret box with Alice and Bob key pairs randomly generated from NEAR JS LIBRARY with a custom nonce', () => {
const keyPairAlice = utils.key_pair.KeyPairEd25519.fromRandom();
const keyPairBob = utils.key_pair.KeyPairEd25519.fromRandom();
const keyPairCharlie = utils.key_pair.KeyPairEd25519.fromRandom();
const messageSent = 'Hello world';
const publicKeyBob = keyPairBob.getPublicKey().toString();
const privateKeyAlice = keyPairAlice.secretKey;
const nonce = encodeBase64(randomBytes.sync(24));
const { secret } = create(messageSent, publicKeyBob, privateKeyAlice, nonce);
it('Should contain the same message sent by Alice opened by Bob', () => {
const publicKeyAlice = keyPairAlice.getPublicKey().toString();
const privateKeyBob = keyPairBob.secretKey;
const messageReceived = open(secret, publicKeyAlice, privateKeyBob, nonce);
expect(messageSent).toBe(messageReceived);
});
it('Should not let Charlie open the box created by Alice and sent to Bob', () => {
const publicKeyAlice = keyPairAlice.getPublicKey().toString();
const privateKeyCharlie = keyPairCharlie.secretKey;
const messageReceived = open(
secret,
publicKeyAlice,
privateKeyCharlie,
nonce
);
expect(messageReceived).toBe(null);
});
});