-
Notifications
You must be signed in to change notification settings - Fork 36
/
integration.test.ts
474 lines (425 loc) · 16.1 KB
/
integration.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import { CashuMint } from '../src/CashuMint.js';
import { CashuWallet } from '../src/CashuWallet.js';
import dns from 'node:dns';
import { secp256k1 } from '@noble/curves/secp256k1';
import { bytesToHex } from '@noble/curves/abstract/utils';
import {
CheckStateEnum,
MeltQuoteState,
MintQuoteState,
ProofState,
Token
} from '../src/model/types/index.js';
import ws from 'ws';
import { injectWebSocketImpl } from '../src/ws.js';
import {
deriveKeysetId,
getEncodedToken,
getEncodedTokenV4,
hexToNumber,
numberToHexPadded64,
sumProofs
} from '../src/utils.js';
dns.setDefaultResultOrder('ipv4first');
const externalInvoice =
'lnbc20u1p3u27nppp5pm074ffk6m42lvae8c6847z7xuvhyknwgkk7pzdce47grf2ksqwsdpv2phhwetjv4jzqcneypqyc6t8dp6xu6twva2xjuzzda6qcqzpgxqyz5vqsp5sw6n7cztudpl5m5jv3z6dtqpt2zhd3q6dwgftey9qxv09w82rgjq9qyyssqhtfl8wv7scwp5flqvmgjjh20nf6utvv5daw5h43h69yqfwjch7wnra3cn94qkscgewa33wvfh7guz76rzsfg9pwlk8mqd27wavf2udsq3yeuju';
let request: Record<string, string> | undefined;
const mintUrl = 'http://localhost:3338';
const unit = 'sat';
injectWebSocketImpl(ws);
describe('mint api', () => {
test('get keys', async () => {
const mint = new CashuMint(mintUrl);
const keys = await mint.getKeys();
expect(keys).toBeDefined();
});
test('get keysets', async () => {
const mint = new CashuMint(mintUrl);
const keysets = await mint.getKeySets();
expect(keysets).toBeDefined();
expect(keysets.keysets).toBeDefined();
expect(keysets.keysets.length).toBeGreaterThan(0);
});
test('get info', async () => {
const mint = new CashuMint(mintUrl);
const info = await mint.getInfo();
expect(info).toBeDefined();
});
test('request mint', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.createMintQuote(100);
expect(request).toBeDefined();
const mintQuote = await wallet.checkMintQuote(request.quote);
expect(mintQuote).toBeDefined();
});
test('mint tokens', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.createMintQuote(1337);
expect(request).toBeDefined();
expect(request.request).toContain('lnbc1337');
const proofs = await wallet.mintProofs(1337, request.quote);
expect(proofs).toBeDefined();
// expect that the sum of all tokens.proofs.amount is equal to the requested amount
expect(sumProofs(proofs)).toBe(1337);
});
test('get fee for local invoice', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.createMintQuote(100);
const fee = (await wallet.createMeltQuote(request.request)).fee_reserve;
expect(fee).toBeDefined();
// because local invoice, fee should be 0
expect(fee).toBe(0);
});
test('invoice with description', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const quote = await wallet.createMintQuote(100, 'test description');
expect(quote).toBeDefined();
console.log(`invoice with description: ${quote.request}`);
});
test('get fee for external invoice', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const fee = (await wallet.createMeltQuote(externalInvoice)).fee_reserve;
expect(fee).toBeDefined();
// because external invoice, fee should be > 0
expect(fee).toBeGreaterThan(0);
});
test('pay local invoice', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.createMintQuote(100);
const proofs = await wallet.mintProofs(100, request.quote);
// expect no fee because local invoice
const mintQuote = await wallet.createMintQuote(10);
const quote = await wallet.createMeltQuote(mintQuote.request);
const fee = quote.fee_reserve;
expect(fee).toBe(0);
// get the quote from the mint
const quote_ = await wallet.checkMeltQuote(quote.quote);
expect(quote_).toBeDefined();
const sendResponse = await wallet.send(10, proofs, { includeFees: true });
const response = await wallet.meltProofs(quote, sendResponse.send);
expect(response).toBeDefined();
// expect that we have received the fee back, since it was internal
expect(response.change.reduce((a, b) => a + b.amount, 0)).toBe(fee);
// check states of spent and kept proofs after payment
const sentProofsStates = await wallet.checkProofsStates(sendResponse.send);
expect(sentProofsStates).toBeDefined();
// expect that all proofs are spent, i.e. all are CheckStateEnum.SPENT
sentProofsStates.forEach((state) => {
expect(state.state).toBe(CheckStateEnum.SPENT);
expect(state.witness).toBeNull();
});
// expect none of the sendResponse.keep to be spent
const keepProofsStates = await wallet.checkProofsStates(sendResponse.keep);
expect(keepProofsStates).toBeDefined();
keepProofsStates.forEach((state) => {
expect(state.state).toBe(CheckStateEnum.UNSPENT);
expect(state.witness).toBeNull();
});
});
test('pay external invoice', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.createMintQuote(3000);
const proofs = await wallet.mintProofs(3000, request.quote);
const meltQuote = await wallet.createMeltQuote(externalInvoice);
const fee = meltQuote.fee_reserve;
expect(fee).toBeGreaterThan(0);
// get the quote from the mint
const quote_ = await wallet.checkMeltQuote(meltQuote.quote);
expect(quote_).toBeDefined();
const sendResponse = await wallet.send(2000 + fee, proofs, { includeFees: true });
const response = await wallet.meltProofs(meltQuote, sendResponse.send);
expect(response).toBeDefined();
// expect that we have not received the fee back, since it was external
expect(response.change.reduce((a, b) => a + b.amount, 0)).toBeLessThan(fee);
// check states of spent and kept proofs after payment
const sentProofsStates = await wallet.checkProofsStates(sendResponse.send);
expect(sentProofsStates).toBeDefined();
// expect that all proofs are spent, i.e. all are CheckStateEnum.SPENT
sentProofsStates.forEach((state) => {
expect(state.state).toBe(CheckStateEnum.SPENT);
expect(state.witness).toBeNull();
});
// expect none of the sendResponse.keep to be spent
const keepProofsStates = await wallet.checkProofsStates(sendResponse.keep);
expect(keepProofsStates).toBeDefined();
keepProofsStates.forEach((state) => {
expect(state.state).toBe(CheckStateEnum.UNSPENT);
expect(state.witness).toBeNull();
});
});
test('test send tokens exact without previous split', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.createMintQuote(64);
const proofs = await wallet.mintProofs(64, request.quote);
const sendResponse = await wallet.send(64, proofs);
expect(sendResponse).toBeDefined();
expect(sendResponse.send).toBeDefined();
expect(sendResponse.keep).toBeDefined();
expect(sendResponse.send.length).toBe(1);
expect(sendResponse.keep.length).toBe(0);
expect(sumProofs(sendResponse.send)).toBe(64);
});
test('test send tokens with change', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.createMintQuote(100);
const proofs = await wallet.mintProofs(100, request.quote);
const sendResponse = await wallet.send(10, proofs, { includeFees: false });
expect(sendResponse).toBeDefined();
expect(sendResponse.send).toBeDefined();
expect(sendResponse.keep).toBeDefined();
expect(sendResponse.send.length).toBe(2);
expect(sendResponse.keep.length).toBe(5);
expect(sumProofs(sendResponse.send)).toBe(10);
expect(sumProofs(sendResponse.keep)).toBe(89);
}, 10000000);
test('receive tokens with previous split', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.createMintQuote(100);
const proofs = await wallet.mintProofs(100, request.quote);
const sendResponse = await wallet.send(10, proofs);
const encoded = getEncodedToken({ mint: mintUrl, proofs: sendResponse.send });
const response = await wallet.receive(encoded);
expect(response).toBeDefined();
});
test('receive tokens with previous mint', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.createMintQuote(64);
const proofs = await wallet.mintProofs(64, request.quote);
const encoded = getEncodedToken({ mint: mintUrl, proofs: proofs });
const response = await wallet.receive(encoded);
expect(response).toBeDefined();
});
test('send and receive p2pk', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const privKeyAlice = secp256k1.utils.randomPrivateKey();
const pubKeyAlice = secp256k1.getPublicKey(privKeyAlice);
const privKeyBob = secp256k1.utils.randomPrivateKey();
const pubKeyBob = secp256k1.getPublicKey(privKeyBob);
const request = await wallet.createMintQuote(128);
const mintedProofs = await wallet.mintProofs(128, request.quote);
const { send } = await wallet.send(64, mintedProofs, { pubkey: bytesToHex(pubKeyBob) });
const encoded = getEncodedToken({ mint: mintUrl, proofs: send });
const result = await wallet
.receive(encoded, { privkey: bytesToHex(privKeyAlice) })
.catch((e) => e);
expect(result).toEqual(new Error('no valid signature provided for input.'));
const proofs = await wallet.receive(encoded, { privkey: bytesToHex(privKeyBob) });
expect(
proofs.reduce((curr, acc) => {
return curr + acc.amount;
}, 0)
).toBe(63);
});
test('mint and melt p2pk', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);
const privKeyBob = secp256k1.utils.randomPrivateKey();
const pubKeyBob = secp256k1.getPublicKey(privKeyBob);
const mintRequest = await wallet.createMintQuote(3000);
const proofs = await wallet.mintProofs(3000, mintRequest.quote, {
pubkey: bytesToHex(pubKeyBob)
});
const meltRequest = await wallet.createMeltQuote(externalInvoice);
const fee = meltRequest.fee_reserve;
expect(fee).toBeGreaterThan(0);
const response = await wallet.meltProofs(meltRequest, proofs, {
privkey: bytesToHex(privKeyBob)
});
expect(response).toBeDefined();
expect(response.quote.state == MeltQuoteState.PAID).toBe(true);
});
test('websocket updates', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);
const mintQuote = await wallet.createMintQuote(21);
const callback = jest.fn();
const res = await new Promise(async (res, rej) => {
const unsub = await wallet.onMintQuoteUpdates(
[mintQuote.quote],
(p) => {
if (p.state === MintQuoteState.PAID) {
callback();
res(1);
unsub();
}
},
(e) => {
console.log(e);
rej(e);
unsub();
}
);
});
mint.disconnectWebSocket();
expect(res).toBe(1);
expect(callback).toBeCalled();
});
test('websocket mint quote updates on multiple ids', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);
const mintQuote1 = await wallet.createMintQuote(21);
const mintQuote2 = await wallet.createMintQuote(22);
const callbackRef = jest.fn();
const res = await new Promise(async (res, rej) => {
let counter = 0;
const unsub = await wallet.onMintQuoteUpdates(
[mintQuote1.quote, mintQuote2.quote],
() => {
counter++;
callbackRef();
if (counter === 4) {
unsub();
res(1);
}
},
() => {
counter++;
if (counter === 4) {
unsub();
rej();
}
}
);
});
mint.disconnectWebSocket();
expect(res).toBe(1);
expect(callbackRef).toHaveBeenCalledTimes(4);
expect(mint.webSocketConnection?.activeSubscriptions.length).toBe(0);
});
test('websocket proof state + mint quote updates', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);
const quote = await wallet.createMintQuote(63);
await new Promise((res, rej) => {
wallet.onMintQuotePaid(quote.quote, res, rej);
});
const proofs = await wallet.mintProofs(63, quote.quote);
const data = await new Promise<ProofState>((res) => {
wallet.onProofStateUpdates(
proofs,
(p) => {
if (p.state === CheckStateEnum.SPENT) {
res(p);
}
},
(e) => {
console.log(e);
}
);
wallet.swap(21, proofs);
});
mint.disconnectWebSocket();
}, 10000);
});
describe('dleq', () => {
test('mint and check dleq', async () => {
const mint = new CashuMint(mintUrl);
const NUT12 = (await mint.getInfo()).nuts['12'];
if (NUT12 == undefined || !NUT12.supported) {
throw new Error('Cannot run this test: mint does not support NUT12');
}
const wallet = new CashuWallet(mint);
const mintRequest = await wallet.createMintQuote(3000);
const proofs = await wallet.mintProofs(3000, mintRequest.quote);
proofs.forEach((p) => {
expect(p).toHaveProperty('dleq');
expect(p.dleq).toHaveProperty('s');
expect(p.dleq).toHaveProperty('e');
expect(p.dleq).toHaveProperty('r');
expect(p).toHaveProperty('dleqValid', true);
});
});
test('send and receive token with dleq', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);
const NUT12 = (await mint.getInfo()).nuts['12'];
if (NUT12 == undefined || !NUT12.supported) {
throw new Error('Cannot run this test: mint does not support NUT12');
}
const mintRequest = await wallet.createMintQuote(8);
const proofs = await wallet.mintProofs(8, mintRequest.quote);
const { keep, send } = await wallet.send(4, proofs, { includeDleq: true });
send.forEach((p) => {
expect(p.dleq).toBeDefined();
expect(p.dleq?.r).toBeDefined();
});
const token = {
mint: mint.mintUrl,
proofs: send
} as Token;
const encodedToken = getEncodedTokenV4(token);
const newProofs = await wallet.receive(encodedToken, { requireDleq: true });
console.log(getEncodedTokenV4(token));
expect(newProofs).toBeDefined();
});
test('send strip dleq', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);
const NUT12 = (await mint.getInfo()).nuts['12'];
if (NUT12 == undefined || !NUT12.supported) {
throw new Error('Cannot run this test: mint does not support NUT12');
}
const mintRequest = await wallet.createMintQuote(8);
const proofs = await wallet.mintProofs(8, mintRequest.quote);
const { keep, send } = await wallet.send(4, proofs, { includeDleq: false });
send.forEach((p) => {
expect(p.dleq).toBeUndefined();
});
keep.forEach((p) => {
expect(p.dleq).toBeDefined();
expect(p.dleq?.r).toBeDefined();
});
});
test('send not enough proofs when dleq is required', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);
const NUT12 = (await mint.getInfo()).nuts['12'];
if (NUT12 == undefined || !NUT12.supported) {
throw new Error('Cannot run this test: mint does not support NUT12');
}
const mintRequest = await wallet.createMintQuote(8);
let proofs = await wallet.mintProofs(8, mintRequest.quote);
// strip dleq
proofs = proofs.map((p) => {
return { ...p, dleq: undefined };
});
const exc = await wallet.send(4, proofs, { includeDleq: true }).catch((e) => e);
expect(exc).toEqual(new Error('Not enough funds available to send'));
});
test('receive with invalid dleq', async () => {
const mint = new CashuMint(mintUrl);
const keys = await mint.getKeys();
const wallet = new CashuWallet(mint);
const NUT12 = (await mint.getInfo()).nuts['12'];
if (NUT12 == undefined || !NUT12.supported) {
throw new Error('Cannot run this test: mint does not support NUT12');
}
const mintRequest = await wallet.createMintQuote(8);
let proofs = await wallet.mintProofs(8, mintRequest.quote);
// alter dleq signature
proofs.forEach((p) => {
if (p.dleq != undefined) {
const s = hexToNumber(p.dleq.s) + BigInt(1);
p.dleq.s = numberToHexPadded64(s);
}
});
const token = {
mint: mint.mintUrl,
proofs: proofs
} as Token;
const exc = await wallet.receive(token, { requireDleq: true }).catch((e) => e);
expect(exc).toEqual(new Error('Token contains proofs with invalid DLEQ'));
});
});