FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScriptSolidServer/test/pay.test.js at patchAppend · bourgeoa/JavaScriptSolidServer · GitHub
bourgeoa
JavaScriptSolidServer
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScriptSolidServer
/
test
/
pay.test.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
717 lines (653 loc) · 23.5 KB
Breadcrumbs
JavaScriptSolidServer
/
test
/
pay.test.js
Copy path
File metadata and controls
717 lines (653 loc) · 23.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/**
* HTTP 402 Payment Required tests
*/
import
{
describe
,
it
,
before
,
after
}
from
'node:test'
;
import
assert
from
'node:assert'
;
import
crypto
from
'crypto'
;
import
{
schnorr
}
from
'@noble/curves/secp256k1'
;
import
{
startTestServer
,
stopTestServer
,
getBaseUrl
,
assertStatus
}
from
'./helpers.js'
;
import
{
jcs
,
sha256Hex
}
from
'../src/mrc20.js'
;
// Generate a test keypair for NIP-98 auth
const
privkey
=
crypto
.
randomBytes
(
32
)
;
const
pubkey
=
Buffer
.
from
(
schnorr
.
getPublicKey
(
privkey
)
)
.
toString
(
'hex'
)
;
/**
* Create a NIP-98 auth header for a request
*/
function
createNip98Header
(
url
,
method
=
'GET'
)
{
const
event
=
{
pubkey
,
created_at
:
Math
.
floor
(
Date
.
now
(
)
/
1000
)
,
kind
:
27235
,
tags
:
[
[
'u'
,
url
]
,
[
'method'
,
method
]
]
,
content
:
''
}
;
// Compute event id
const
serialized
=
JSON
.
stringify
(
[
0
,
event
.
pubkey
,
event
.
created_at
,
event
.
kind
,
event
.
tags
,
event
.
content
]
)
;
event
.
id
=
crypto
.
createHash
(
'sha256'
)
.
update
(
serialized
)
.
digest
(
'hex'
)
;
// Sign with schnorr
const
sig
=
schnorr
.
sign
(
event
.
id
,
privkey
)
;
event
.
sig
=
Buffer
.
from
(
sig
)
.
toString
(
'hex'
)
;
const
token
=
Buffer
.
from
(
JSON
.
stringify
(
event
)
)
.
toString
(
'base64'
)
;
return
`Nostr
${
token
}
`
;
}
describe
(
'HTTP 402 Pay Middleware'
,
(
)
=>
{
const
POD_ADDRESS
=
'test-pod-address'
;
before
(
async
(
)
=>
{
await
startTestServer
(
{
pay
:
true
,
payCost
:
10
,
payAddress
:
POD_ADDRESS
}
)
;
}
)
;
after
(
async
(
)
=>
{
await
stopTestServer
(
)
;
}
)
;
describe
(
'GET /pay/.balance'
,
(
)
=>
{
it
(
'should return 401 without auth'
,
async
(
)
=>
{
const
res
=
await
fetch
(
`
${
getBaseUrl
(
)
}
/pay/.balance`
)
;
assertStatus
(
res
,
401
)
;
}
)
;
it
(
'should return zero balance for new user'
,
async
(
)
=>
{
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.balance`
;
const
res
=
await
fetch
(
url
,
{
headers
:
{
'Authorization'
:
createNip98Header
(
url
)
}
}
)
;
assertStatus
(
res
,
200
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
strictEqual
(
body
.
balance
,
0
)
;
assert
.
strictEqual
(
body
.
cost
,
10
)
;
assert
.
strictEqual
(
body
.
unit
,
'sat'
)
;
assert
.
ok
(
body
.
did
.
startsWith
(
'did:nostr:'
)
)
;
}
)
;
}
)
;
describe
(
'GET /pay/* (paid access)'
,
(
)
=>
{
it
(
'should return 401 without auth'
,
async
(
)
=>
{
const
res
=
await
fetch
(
`
${
getBaseUrl
(
)
}
/pay/test-resource`
)
;
assertStatus
(
res
,
401
)
;
}
)
;
it
(
'should return 402 with zero balance'
,
async
(
)
=>
{
const
url
=
`
${
getBaseUrl
(
)
}
/pay/test-resource`
;
const
res
=
await
fetch
(
url
,
{
headers
:
{
'Authorization'
:
createNip98Header
(
url
)
}
}
)
;
assertStatus
(
res
,
402
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
strictEqual
(
body
.
error
,
'Payment Required'
)
;
assert
.
strictEqual
(
body
.
balance
,
0
)
;
assert
.
strictEqual
(
body
.
cost
,
10
)
;
assert
.
strictEqual
(
body
.
deposit
,
'/pay/.deposit'
)
;
}
)
;
}
)
;
describe
(
'POST /pay/.deposit'
,
(
)
=>
{
it
(
'should return 401 without auth'
,
async
(
)
=>
{
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.deposit`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
body
:
'test'
}
)
;
assertStatus
(
res
,
401
)
;
}
)
;
it
(
'should return 400 without TXO URI'
,
async
(
)
=>
{
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.deposit`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
createNip98Header
(
url
,
'POST'
)
}
}
)
;
assertStatus
(
res
,
400
)
;
}
)
;
it
(
'should return 400 for invalid TXO URI'
,
async
(
)
=>
{
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.deposit`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
createNip98Header
(
url
,
'POST'
)
}
,
body
:
'not-a-valid-txo'
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'Invalid TXO URI'
)
)
;
}
)
;
}
)
;
describe
(
'POST /pay/.deposit (MRC20)'
,
(
)
=>
{
function
makeStatePair
(
toAddress
,
amt
=
100
)
{
const
prevState
=
{
profile
:
'mono.mrc20.v0.1'
,
prev
:
'0'
.
repeat
(
64
)
,
seq
:
0
,
ticker
:
'TEST'
,
name
:
'Test Token'
,
decimals
:
0
,
supply
:
1000
,
balances
:
{
creator
:
1000
}
,
ops
:
[
]
}
;
const
state
=
{
profile
:
'mono.mrc20.v0.1'
,
prev
:
sha256Hex
(
jcs
(
prevState
)
)
,
seq
:
1
,
ticker
:
'TEST'
,
name
:
'Test Token'
,
decimals
:
0
,
supply
:
1000
,
balances
:
{
creator
:
1000
-
amt
,
[
toAddress
]
:
amt
}
,
ops
:
[
{
op
:
'urn:mono:op:transfer'
,
from
:
'creator'
,
to
:
toAddress
,
amt
}
]
}
;
return
{
prevState
,
state
}
;
}
it
(
'should accept valid MRC20 deposit'
,
async
(
)
=>
{
const
{
prevState
,
state
}
=
makeStatePair
(
POD_ADDRESS
,
500
)
;
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.deposit`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
createNip98Header
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
type
:
'mrc20'
,
state
,
prevState
}
)
}
)
;
assertStatus
(
res
,
200
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
strictEqual
(
body
.
deposited
,
500
)
;
assert
.
strictEqual
(
body
.
ticker
,
'TEST'
)
;
assert
.
strictEqual
(
body
.
unit
,
'token'
)
;
assert
.
ok
(
body
.
balance
>=
500
)
;
}
)
;
it
(
'should reject MRC20 deposit with broken chain'
,
async
(
)
=>
{
const
{
prevState
,
state
}
=
makeStatePair
(
POD_ADDRESS
)
;
state
.
prev
=
'tampered'
;
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.deposit`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
createNip98Header
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
type
:
'mrc20'
,
state
,
prevState
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'State chain break'
)
)
;
}
)
;
it
(
'should reject MRC20 deposit to wrong address'
,
async
(
)
=>
{
const
{
prevState
,
state
}
=
makeStatePair
(
'wrong-address'
)
;
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.deposit`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
createNip98Header
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
type
:
'mrc20'
,
state
,
prevState
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'No transfers'
)
)
;
}
)
;
}
)
;
describe
(
'GET /pay/.info'
,
(
)
=>
{
it
(
'should return info without auth'
,
async
(
)
=>
{
const
res
=
await
fetch
(
`
${
getBaseUrl
(
)
}
/pay/.info`
)
;
assertStatus
(
res
,
200
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
strictEqual
(
body
.
cost
,
10
)
;
assert
.
strictEqual
(
body
.
unit
,
'sat'
)
;
assert
.
strictEqual
(
body
.
deposit
,
'/pay/.deposit'
)
;
assert
.
strictEqual
(
body
.
balance
,
'/pay/.balance'
)
;
}
)
;
it
(
'should not include token info when payToken not configured'
,
async
(
)
=>
{
const
res
=
await
fetch
(
`
${
getBaseUrl
(
)
}
/pay/.info`
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
strictEqual
(
body
.
token
,
undefined
)
;
}
)
;
}
)
;
describe
(
'POST /pay/.buy'
,
(
)
=>
{
it
(
'should return 401 without auth'
,
async
(
)
=>
{
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.buy`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
body
:
'{"amount":10}'
}
)
;
assertStatus
(
res
,
401
)
;
}
)
;
it
(
'should return 400 when payToken not configured'
,
async
(
)
=>
{
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.buy`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
createNip98Header
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
amount
:
10
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'not configured'
)
)
;
}
)
;
}
)
;
describe
(
'POST /pay/.withdraw'
,
(
)
=>
{
it
(
'should return 401 without auth'
,
async
(
)
=>
{
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.withdraw`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
body
:
'{"all":true}'
}
)
;
assertStatus
(
res
,
401
)
;
}
)
;
it
(
'should return 400 when payToken not configured'
,
async
(
)
=>
{
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.withdraw`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
createNip98Header
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
all
:
true
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'not configured'
)
)
;
}
)
;
}
)
;
describe
(
'Pay with token configured'
,
(
)
=>
{
let
tokenServer
;
let
tokenUrl
;
const
tokenPrivkey
=
crypto
.
randomBytes
(
32
)
;
const
tokenPubkey
=
Buffer
.
from
(
schnorr
.
getPublicKey
(
tokenPrivkey
)
)
.
toString
(
'hex'
)
;
function
tokenNip98
(
url
,
method
=
'GET'
)
{
const
event
=
{
pubkey
:
tokenPubkey
,
created_at
:
Math
.
floor
(
Date
.
now
(
)
/
1000
)
,
kind
:
27235
,
tags
:
[
[
'u'
,
url
]
,
[
'method'
,
method
]
]
,
content
:
''
}
;
const
serialized
=
JSON
.
stringify
(
[
0
,
event
.
pubkey
,
event
.
created_at
,
event
.
kind
,
event
.
tags
,
event
.
content
]
)
;
event
.
id
=
crypto
.
createHash
(
'sha256'
)
.
update
(
serialized
)
.
digest
(
'hex'
)
;
event
.
sig
=
Buffer
.
from
(
schnorr
.
sign
(
event
.
id
,
tokenPrivkey
)
)
.
toString
(
'hex'
)
;
return
`Nostr
${
Buffer
.
from
(
JSON
.
stringify
(
event
)
)
.
toString
(
'base64'
)
}
`
;
}
before
(
async
(
)
=>
{
const
{
createServer
}
=
await
import
(
'../src/server.js'
)
;
tokenServer
=
createServer
(
{
logger
:
false
,
forceCloseConnections
:
true
,
pay
:
true
,
payCost
:
5
,
payAddress
:
'test-addr'
,
payToken
:
'TEST'
,
payRate
:
10
}
)
;
await
tokenServer
.
listen
(
{
port
:
0
,
host
:
'127.0.0.1'
}
)
;
const
addr
=
tokenServer
.
server
.
address
(
)
;
tokenUrl
=
`http://127.0.0.1:
${
addr
.
port
}
`
;
}
)
;
after
(
async
(
)
=>
{
if
(
tokenServer
)
await
tokenServer
.
close
(
)
;
}
)
;
it
(
'GET /pay/.info should include token info'
,
async
(
)
=>
{
const
res
=
await
fetch
(
`
${
tokenUrl
}
/pay/.info`
)
;
assertStatus
(
res
,
200
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
strictEqual
(
body
.
cost
,
5
)
;
assert
.
strictEqual
(
body
.
token
.
ticker
,
'TEST'
)
;
assert
.
strictEqual
(
body
.
token
.
rate
,
10
)
;
assert
.
strictEqual
(
body
.
token
.
buy
,
'/pay/.buy'
)
;
assert
.
strictEqual
(
body
.
token
.
withdraw
,
'/pay/.withdraw'
)
;
}
)
;
it
(
'POST /pay/.buy should return 402 with zero balance'
,
async
(
)
=>
{
const
url
=
`
${
tokenUrl
}
/pay/.buy`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
tokenNip98
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
amount
:
10
}
)
}
)
;
assertStatus
(
res
,
402
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
strictEqual
(
body
.
error
,
'Insufficient sat balance'
)
;
assert
.
strictEqual
(
body
.
balance
,
0
)
;
assert
.
strictEqual
(
body
.
cost
,
100
)
;
// 10 tokens * rate 10
assert
.
strictEqual
(
body
.
rate
,
10
)
;
}
)
;
it
(
'POST /pay/.buy should reject wrong ticker'
,
async
(
)
=>
{
const
url
=
`
${
tokenUrl
}
/pay/.buy`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
tokenNip98
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
ticker
:
'WRONG'
,
amount
:
10
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'only sells TEST'
)
)
;
}
)
;
it
(
'POST /pay/.buy should reject malformed JSON'
,
async
(
)
=>
{
const
url
=
`
${
tokenUrl
}
/pay/.buy`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
tokenNip98
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
'{not valid json'
}
)
;
assertStatus
(
res
,
400
)
;
}
)
;
it
(
'POST /pay/.buy should reject missing amount'
,
async
(
)
=>
{
const
url
=
`
${
tokenUrl
}
/pay/.buy`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
tokenNip98
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'Specify'
)
)
;
}
)
;
it
(
'POST /pay/.withdraw should return 400 with zero balance and all:true'
,
async
(
)
=>
{
const
url
=
`
${
tokenUrl
}
/pay/.withdraw`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
tokenNip98
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
all
:
true
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'Nothing to withdraw'
)
)
;
}
)
;
it
(
'POST /pay/.withdraw should return 402 when balance insufficient'
,
async
(
)
=>
{
const
url
=
`
${
tokenUrl
}
/pay/.withdraw`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
tokenNip98
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
tokens
:
1000
}
)
}
)
;
assertStatus
(
res
,
402
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
strictEqual
(
body
.
error
,
'Insufficient balance'
)
;
}
)
;
it
(
'POST /pay/.withdraw should reject malformed JSON'
,
async
(
)
=>
{
const
url
=
`
${
tokenUrl
}
/pay/.withdraw`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
tokenNip98
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
'{bad json'
}
)
;
assertStatus
(
res
,
400
)
;
}
)
;
it
(
'POST /pay/.withdraw should reject missing params'
,
async
(
)
=>
{
const
url
=
`
${
tokenUrl
}
/pay/.withdraw`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
tokenNip98
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'Specify'
)
)
;
}
)
;
it
(
'POST /pay/.sell should reject missing amount/price'
,
async
(
)
=>
{
const
url
=
`
${
tokenUrl
}
/pay/.sell`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
tokenNip98
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'Specify'
)
)
;
}
)
;
it
(
'POST /pay/.swap should reject missing offer id'
,
async
(
)
=>
{
const
url
=
`
${
tokenUrl
}
/pay/.swap`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
tokenNip98
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'Specify offer id'
)
)
;
}
)
;
it
(
'POST /pay/.swap should return 404 for unknown offer'
,
async
(
)
=>
{
const
url
=
`
${
tokenUrl
}
/pay/.swap`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
tokenNip98
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
id
:
'nonexistent'
}
)
}
)
;
assertStatus
(
res
,
404
)
;
}
)
;
it
(
'GET /pay/.offers should return empty list'
,
async
(
)
=>
{
const
res
=
await
fetch
(
`
${
tokenUrl
}
/pay/.offers`
)
;
assertStatus
(
res
,
200
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
Array
.
isArray
(
body
)
)
;
}
)
;
}
)
;
describe
(
'GET /pay/.offers'
,
(
)
=>
{
it
(
'should return empty list without auth'
,
async
(
)
=>
{
const
res
=
await
fetch
(
`
${
getBaseUrl
(
)
}
/pay/.offers`
)
;
assertStatus
(
res
,
200
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
Array
.
isArray
(
body
)
)
;
assert
.
strictEqual
(
body
.
length
,
0
)
;
}
)
;
}
)
;
describe
(
'POST /pay/.sell'
,
(
)
=>
{
it
(
'should return 401 without auth'
,
async
(
)
=>
{
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.sell`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
body
:
'{}'
}
)
;
assertStatus
(
res
,
401
)
;
}
)
;
it
(
'should return 400 when payToken not configured'
,
async
(
)
=>
{
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.sell`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
createNip98Header
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
amount
:
10
,
price
:
100
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'not configured'
)
)
;
}
)
;
}
)
;
describe
(
'POST /pay/.swap'
,
(
)
=>
{
it
(
'should return 401 without auth'
,
async
(
)
=>
{
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.swap`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
body
:
'{}'
}
)
;
assertStatus
(
res
,
401
)
;
}
)
;
it
(
'should return 400 when payToken not configured'
,
async
(
)
=>
{
const
url
=
`
${
getBaseUrl
(
)
}
/pay/.swap`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
createNip98Header
(
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
id
:
'test-id'
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'not configured'
)
)
;
}
)
;
}
)
;
describe
(
'AMM with multi-chain'
,
(
)
=>
{
let
ammServer
;
let
ammUrl
;
const
ammPrivkey
=
crypto
.
randomBytes
(
32
)
;
const
ammPubkey
=
Buffer
.
from
(
schnorr
.
getPublicKey
(
ammPrivkey
)
)
.
toString
(
'hex'
)
;
const
ammPrivkey2
=
crypto
.
randomBytes
(
32
)
;
const
ammPubkey2
=
Buffer
.
from
(
schnorr
.
getPublicKey
(
ammPrivkey2
)
)
.
toString
(
'hex'
)
;
function
ammNip98
(
pk
,
url
,
method
=
'GET'
)
{
const
event
=
{
pubkey
:
Buffer
.
from
(
schnorr
.
getPublicKey
(
pk
)
)
.
toString
(
'hex'
)
,
created_at
:
Math
.
floor
(
Date
.
now
(
)
/
1000
)
,
kind
:
27235
,
tags
:
[
[
'u'
,
url
]
,
[
'method'
,
method
]
]
,
content
:
''
}
;
const
serialized
=
JSON
.
stringify
(
[
0
,
event
.
pubkey
,
event
.
created_at
,
event
.
kind
,
event
.
tags
,
event
.
content
]
)
;
event
.
id
=
crypto
.
createHash
(
'sha256'
)
.
update
(
serialized
)
.
digest
(
'hex'
)
;
event
.
sig
=
Buffer
.
from
(
schnorr
.
sign
(
event
.
id
,
pk
)
)
.
toString
(
'hex'
)
;
return
`Nostr
${
Buffer
.
from
(
JSON
.
stringify
(
event
)
)
.
toString
(
'base64'
)
}
`
;
}
before
(
async
(
)
=>
{
const
{
createServer
}
=
await
import
(
'../src/server.js'
)
;
ammServer
=
createServer
(
{
logger
:
false
,
forceCloseConnections
:
true
,
pay
:
true
,
payCost
:
1
,
payChains
:
'tbtc3,tbtc4'
}
)
;
await
ammServer
.
listen
(
{
port
:
0
,
host
:
'127.0.0.1'
}
)
;
const
addr
=
ammServer
.
server
.
address
(
)
;
ammUrl
=
`http://127.0.0.1:
${
addr
.
port
}
`
;
}
)
;
after
(
async
(
)
=>
{
if
(
ammServer
)
await
ammServer
.
close
(
)
;
}
)
;
it
(
'GET /pay/.info should include chains and pool'
,
async
(
)
=>
{
const
res
=
await
fetch
(
`
${
ammUrl
}
/pay/.info`
)
;
assertStatus
(
res
,
200
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
chains
)
;
assert
.
strictEqual
(
body
.
chains
.
length
,
2
)
;
assert
.
strictEqual
(
body
.
chains
[
0
]
.
id
,
'tbtc3'
)
;
assert
.
strictEqual
(
body
.
chains
[
1
]
.
id
,
'tbtc4'
)
;
assert
.
strictEqual
(
body
.
pool
,
'/pay/.pool'
)
;
}
)
;
it
(
'GET /pay/.pool should return empty pool'
,
async
(
)
=>
{
const
res
=
await
fetch
(
`
${
ammUrl
}
/pay/.pool`
)
;
assertStatus
(
res
,
200
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
strictEqual
(
body
.
reserves
.
tbtc3
,
0
)
;
assert
.
strictEqual
(
body
.
reserves
.
tbtc4
,
0
)
;
assert
.
strictEqual
(
body
.
k
,
0
)
;
assert
.
strictEqual
(
body
.
totalShares
,
0
)
;
}
)
;
it
(
'GET /pay/.balance should include per-chain balances'
,
async
(
)
=>
{
const
url
=
`
${
ammUrl
}
/pay/.balance`
;
const
res
=
await
fetch
(
url
,
{
headers
:
{
'Authorization'
:
ammNip98
(
ammPrivkey
,
url
)
}
}
)
;
assertStatus
(
res
,
200
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
balances
)
;
assert
.
strictEqual
(
body
.
balances
.
tbtc3
,
0
)
;
assert
.
strictEqual
(
body
.
balances
.
tbtc4
,
0
)
;
}
)
;
it
(
'POST /pay/.pool swap should fail with no liquidity'
,
async
(
)
=>
{
const
url
=
`
${
ammUrl
}
/pay/.pool`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
ammNip98
(
ammPrivkey
,
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
action
:
'swap'
,
sell
:
'tbtc3'
,
amount
:
100
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'no liquidity'
)
)
;
}
)
;
it
(
'POST /pay/.pool add-liquidity should fail with zero balance'
,
async
(
)
=>
{
const
url
=
`
${
ammUrl
}
/pay/.pool`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
ammNip98
(
ammPrivkey
,
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
action
:
'add-liquidity'
,
tbtc3
:
1000
,
tbtc4
:
5000
}
)
}
)
;
assertStatus
(
res
,
402
)
;
}
)
;
it
(
'POST /pay/.pool should reject unknown action'
,
async
(
)
=>
{
const
url
=
`
${
ammUrl
}
/pay/.pool`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
ammNip98
(
ammPrivkey
,
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
action
:
'invalid'
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'Unknown action'
)
)
;
}
)
;
it
(
'POST /pay/.pool swap should reject invalid sell unit'
,
async
(
)
=>
{
const
url
=
`
${
ammUrl
}
/pay/.pool`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
ammNip98
(
ammPrivkey
,
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
action
:
'swap'
,
sell
:
'invalid'
,
amount
:
100
}
)
}
)
;
assertStatus
(
res
,
400
)
;
}
)
;
it
(
'POST /pay/.pool remove-liquidity should fail with no pool'
,
async
(
)
=>
{
const
url
=
`
${
ammUrl
}
/pay/.pool`
;
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Authorization'
:
ammNip98
(
ammPrivkey
,
url
,
'POST'
)
,
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
action
:
'remove-liquidity'
,
shares
:
10
}
)
}
)
;
assertStatus
(
res
,
400
)
;
const
body
=
await
res
.
json
(
)
;
assert
.
ok
(
body
.
error
.
includes
(
'no liquidity'
)
)
;
}
)
;
}
)
;
describe
(
'Pay disabled'
,
(
)
=>
{
let
noPayServer
;
let
noPayUrl
;
before
(
async
(
)
=>
{
// Start a separate server without pay enabled
const
{
createServer
}
=
await
import
(
'../src/server.js'
)
;
noPayServer
=
createServer
(
{
logger
:
false
,
forceCloseConnections
:
true
,
pay
:
false
}
)
;
await
noPayServer
.
listen
(
{
port
:
0
,
host
:
'127.0.0.1'
}
)
;
const
addr
=
noPayServer
.
server
.
address
(
)
;
noPayUrl
=
`http://127.0.0.1:
${
addr
.
port
}
`
;
}
)
;
after
(
async
(
)
=>
{
if
(
noPayServer
)
await
noPayServer
.
close
(
)
;
}
)
;
it
(
'should not intercept /pay/ when disabled'
,
async
(
)
=>
{
const
res
=
await
fetch
(
`
${
noPayUrl
}
/pay/.balance`
)
;
// Without pay enabled, dotfile security blocks .balance with 403
assert
.
ok
(
res
.
status
===
401
||
res
.
status
===
403
||
res
.
status
===
404
)
;
}
)
;
}
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL