-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpay.js
More file actions
1343 lines (1183 loc) · 46.9 KB
/
pay.js
File metadata and controls
1343 lines (1183 loc) · 46.9 KB
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* HTTP 402 Payment Required middleware
*
* Enables paid access to resources under /pay/* prefix.
* Authentication via NIP-98. Balance tracking via Web Ledgers spec.
*
* Routes:
* GET /pay/.info — public endpoint: cost, token info, available routes
* GET /pay/.balance — check your balance
* POST /pay/.deposit — deposit sats (TXO URI) or tokens (MRC20 state proof)
* POST /pay/.buy — buy tokens with sat balance (primary market)
* POST /pay/.withdraw — withdraw balance as tokens (portable MRC20 proof)
* GET /pay/.offers — list open sell orders (secondary market)
* POST /pay/.sell — create a sell order (NIP-69 kind 38383)
* POST /pay/.swap — execute a swap against a sell order
* GET /pay/* — paid resource access (requires balance >= cost)
* PUT /pay/* — upload resources (standard auth)
*
* Ledger: /.well-known/webledgers/webledgers.json (webledgers.org spec)
*
* References:
* - Web Ledgers spec: https://webledgers.org/
* - NIP-98 HTTP Auth: https://nips.nostr.com/98
* - TXO URI: https://www.npmjs.com/package/txo_parser
* - MRC20 profile: https://blocktrails.org/
*/
import crypto from 'crypto';
import { getNostrPubkey, pubkeyToDidNostr } from '../auth/nostr.js';
import { readLedger, writeLedger, getBalance, credit, debit } from '../webledger.js';
import { verifyMrc20Deposit, verifyMrc20Anchor, jcs, btAddress } from '../mrc20.js';
import { loadTrail, transferToken, buildTransaction, broadcastTx, p2trScript, btDeriveChainedPrivkey } from '../token.js';
import { secp256k1 } from '@noble/curves/secp256k1';
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
import fs from 'fs-extra';
import path from 'path';
// --- Pod keypair for deposit addresses (stored outside web-accessible tree) ---
const keypairFile = () => path.join(process.env.DATA_ROOT || './data', '.private/keypair.json');
async function loadOrCreateKeypair() {
try {
const data = await fs.readFile(keypairFile(), 'utf8');
return JSON.parse(data);
} catch {
const privkey = secp256k1.utils.randomPrivateKey();
const pubkey = secp256k1.getPublicKey(privkey, true);
const kp = { privkey: bytesToHex(privkey), pubkey: bytesToHex(pubkey) };
await fs.ensureDir(path.dirname(keypairFile()));
await fs.writeFile(keypairFile(), JSON.stringify(kp, null, 2));
return kp;
}
}
// --- Pod UTXO tracking (stored outside web-accessible tree) ---
const utxoFile = () => path.join(process.env.DATA_ROOT || './data', '.private/utxos.json');
async function loadUtxos() {
try {
const data = await fs.readFile(utxoFile(), 'utf8');
return JSON.parse(data);
} catch { return []; }
}
async function saveUtxos(utxos) {
await fs.ensureDir(path.dirname(utxoFile()));
await fs.writeFile(utxoFile(), JSON.stringify(utxos, null, 2));
}
const DEFAULT_COST = 1; // satoshis per request
// --- Chain registry for multi-chain deposits ---
const CHAIN_REGISTRY = {
tbtc3: { explorer: 'https://mempool.space/testnet/api', unit: 'tbtc3', name: 'Bitcoin Testnet3' },
tbtc4: { explorer: 'https://mempool.space/testnet4/api', unit: 'tbtc4', name: 'Bitcoin Testnet4' },
btc: { explorer: 'https://mempool.space/api', unit: 'sat', name: 'Bitcoin' },
ltc: { explorer: 'https://litecoinspace.org/api', unit: 'ltc', name: 'Litecoin' },
signet:{ explorer: 'https://mempool.space/signet/api', unit: 'signet', name: 'Bitcoin Signet' },
};
/**
* Parse chain ID from a TXO URI body string.
* Supports: "txo:tbtc3:txid:vout", "txo:btc:txid:vout", or bare "txid:vout" (returns null).
*/
function parseTxoChain(body) {
const str = typeof body === 'string' ? body.trim() : '';
const match = str.match(/^txo:([a-z0-9]+):/i);
return match ? match[1].toLowerCase() : null;
}
// --- AMM pool storage ---
const poolFile = () => path.join(process.env.DATA_ROOT || './data', '.well-known/webledgers/pool.json');
async function loadPool() {
try {
const data = await fs.readFile(poolFile(), 'utf8');
return JSON.parse(data);
} catch { return null; }
}
async function savePool(pool) {
await fs.ensureDir(path.dirname(poolFile()));
await fs.writeFile(poolFile(), JSON.stringify(pool, null, 2));
}
// --- Replay protection ---
const replayFile = () => path.join(process.env.DATA_ROOT || './data', '.well-known/webledgers/replay.json');
async function loadReplaySet() {
try {
const data = await fs.readFile(replayFile(), 'utf8');
return new Set(JSON.parse(data));
} catch { return new Set(); }
}
async function saveReplaySet(set) {
await fs.ensureDir(path.dirname(replayFile()));
await fs.writeFile(replayFile(), JSON.stringify([...set]));
}
async function checkAndRecordState(stateHash) {
const seen = await loadReplaySet();
if (seen.has(stateHash)) return false; // replay!
seen.add(stateHash);
await saveReplaySet(seen);
return true;
}
// --- Offers storage (secondary market) ---
const offersFile = () => path.join(process.env.DATA_ROOT || './data', '.well-known/webledgers/offers.json');
async function loadOffers() {
try {
const data = await fs.readFile(offersFile(), 'utf8');
return JSON.parse(data);
} catch { return []; }
}
async function saveOffers(offers) {
await fs.ensureDir(path.dirname(offersFile()));
await fs.writeFile(offersFile(), JSON.stringify(offers, null, 2));
}
// --- Deposit verification via mempool API ---
async function verifySatsDeposit(txoUri, mempoolUrl) {
const match = txoUri.match(/([0-9a-f]{64}):(\d+)/i);
if (!match) {
return { valid: false, amount: 0, error: 'Invalid TXO URI format (expected txid:vout)' };
}
const [, txid, voutStr] = match;
const vout = parseInt(voutStr, 10);
try {
const resp = await fetch(`${mempoolUrl}/api/tx/${txid}`);
if (!resp.ok) return { valid: false, amount: 0, error: 'Transaction not found' };
const tx = await resp.json();
const output = tx.vout?.[vout];
if (!output) return { valid: false, amount: 0, error: `Output index ${vout} not found` };
return { valid: true, amount: output.value };
} catch (err) {
return { valid: false, amount: 0, error: `Mempool API error: ${err.message}` };
}
}
/**
* Parse deposit request body — returns either a sats TXO URI or MRC20 state proof
* @param {*} body - Request body (Buffer, string, or parsed object)
* @returns {{type: 'sats', txo: string} | {type: 'mrc20', state: object, prevState: object} | {type: 'unknown'}}
*/
function parseDepositBody(body) {
// Buffer → string first
if (Buffer.isBuffer(body)) {
const str = body.toString('utf8').trim();
// Try JSON parse
try {
const obj = JSON.parse(str);
return classifyDepositObject(obj);
} catch {
// Not JSON — treat as TXO URI string
return { type: 'sats', txo: str };
}
}
// Already parsed object
if (body && typeof body === 'object') {
return classifyDepositObject(body);
}
// String
if (typeof body === 'string') {
const trimmed = body.trim();
try {
const obj = JSON.parse(trimmed);
return classifyDepositObject(obj);
} catch {
return { type: 'sats', txo: trimmed };
}
}
return { type: 'unknown' };
}
function classifyDepositObject(obj) {
// Explicit type field
if (obj.type === 'mrc20' && obj.state && obj.prevState) {
return { type: 'mrc20', state: obj.state, prevState: obj.prevState, anchor: obj.anchor };
}
// Auto-detect: if it has state + prevState with MRC20 profile
if (obj.state?.profile === 'mono.mrc20.v0.1' && obj.prevState) {
return { type: 'mrc20', state: obj.state, prevState: obj.prevState, anchor: obj.anchor };
}
// Claim deposit: user sent sats to pod's address, claiming with txid
if (obj.txid && obj.vout !== undefined) {
return { type: 'claim', txid: obj.txid, vout: parseInt(obj.vout, 10), chain: obj.chain };
}
// Fall back to TXO URI in .txo field
if (obj.txo) {
return { type: 'sats', txo: obj.txo };
}
return { type: 'unknown' };
}
// --- Check if URL is a /pay/ route ---
export function isPayRequest(url) {
const path = url.split('?')[0];
return path.startsWith('/pay/') || path === '/pay';
}
// --- preHandler hook for /pay/* routes ---
/**
* Create pay preHandler hook
* @param {object} options
* @param {number} options.cost - Cost per request in satoshis (default 1)
* @param {string} options.mempoolUrl - Mempool API base URL
* @param {string} options.payAddress - Pod's MRC20 address for receiving token transfers
* @returns {function} Fastify preHandler hook
*/
export function createPayHandler(options = {}) {
const cost = options.cost ?? DEFAULT_COST;
const mempoolUrl = options.mempoolUrl ?? 'https://mempool.space/testnet4';
const payAddress = options.payAddress ?? null;
const payToken = options.payToken ?? null;
const payRate = options.payRate ?? 1;
// Parse multi-chain config: "tbtc3,tbtc4" → ['tbtc3', 'tbtc4']
const payChains = options.payChains
? options.payChains.split(',').map(c => c.trim()).filter(c => CHAIN_REGISTRY[c])
: null;
return async function payHandler(request, reply) {
const url = request.url.split('?')[0];
if (!isPayRequest(request.url)) return;
// --- GET /pay/.info — public, no auth ---
if (url === '/pay/.info' && request.method === 'GET') {
const info = {
cost,
unit: 'sat',
deposit: '/pay/.deposit',
balance: '/pay/.balance'
};
if (payToken) {
const trail = await loadTrail(payToken);
info.token = {
ticker: payToken,
rate: payRate,
buy: '/pay/.buy',
withdraw: '/pay/.withdraw'
};
if (trail) {
info.token.supply = trail.latestState?.supply ?? null;
info.token.issuer = trail.pubkeyBase ?? null;
}
}
if (payChains) {
info.chains = payChains.map(id => ({ id, unit: CHAIN_REGISTRY[id].unit, name: CHAIN_REGISTRY[id].name }));
info.pool = '/pay/.pool';
}
return reply.send(info);
}
// --- GET /pay/.address — deposit address (optional per-user tweak) ---
if (url === '/pay/.address' && request.method === 'GET') {
const chain = request.query?.chain || (payChains ? payChains[0] : 'tbtc4');
if (!CHAIN_REGISTRY[chain]) {
return reply.code(400).send({ error: `Unsupported chain: ${chain}` });
}
if (payChains && !payChains.includes(chain)) {
return reply.code(400).send({ error: `Chain not enabled: ${chain}`, enabledChains: payChains });
}
const kp = await loadOrCreateKeypair();
const network = chain === 'btc' ? 'mainnet' : (chain === 'tbtc3' ? 'testnet' : 'testnet4');
const user = request.query?.user?.trim().toLowerCase() || null;
if (user && !/^did:nostr:[0-9a-f]{64}$/.test(user)) {
return reply.code(400).send({ error: 'Invalid user DID. Expected: did:nostr:<64-hex>' });
}
const states = user ? [user] : [];
const address = btAddress(kp.pubkey, states, network);
const response = { address, chain, pubkey: kp.pubkey };
if (user) response.user = user;
return reply.send(response);
}
// --- GET /pay/.balance ---
if (url === '/pay/.balance' && request.method === 'GET') {
const pubkey = await getNostrPubkey(request);
if (!pubkey) {
return reply.code(401).send({ error: 'NIP-98 authentication required' });
}
const didUri = pubkeyToDidNostr(pubkey);
// Auto-detect deposits to user's tweaked address (Phase 3)
if (payChains) {
try {
const kp = await loadOrCreateKeypair();
const utxos = await loadUtxos();
const ledger = await readLedger();
let credited = 0;
for (const chainId of payChains) {
const chain = CHAIN_REGISTRY[chainId];
const network = chainId === 'btc' ? 'mainnet' : (chainId === 'tbtc3' ? 'testnet' : 'testnet4');
const userAddr = btAddress(kp.pubkey, [didUri], network);
const resp = await fetch(`${chain.explorer}/address/${userAddr}/utxo`);
if (!resp.ok) continue;
const addrUtxos = await resp.json();
for (const u of addrUtxos) {
if (utxos.find(x => x.txid === u.txid && x.vout === u.vout)) continue;
// New UTXO — fetch tx for scriptpubkey, then auto-credit
let scriptpubkey = '';
try {
const txResp = await fetch(`${chain.explorer}/tx/${u.txid}`);
if (txResp.ok) {
const txData = await txResp.json();
scriptpubkey = txData.vout?.[u.vout]?.scriptpubkey || '';
}
} catch { /* best effort */ }
const currency = chain.unit;
credit(ledger, didUri, u.value, currency);
utxos.push({ txid: u.txid, vout: u.vout, amount: u.value, scriptpubkey, chain: chainId, tweak: didUri, spent: false });
credited += u.value;
}
}
if (credited > 0) {
await writeLedger(ledger);
await saveUtxos(utxos);
}
} catch { /* scan failure is non-fatal */ }
}
const ledger = await readLedger();
const response = {
did: didUri,
balance: getBalance(ledger, didUri),
cost,
unit: 'sat'
};
// Include per-chain balances when multi-chain is enabled
if (payChains) {
response.balances = {};
for (const chainId of payChains) {
const unit = CHAIN_REGISTRY[chainId].unit;
response.balances[unit] = getBalance(ledger, didUri, unit);
}
}
return reply.send(response);
}
// --- POST /pay/.deposit ---
if (url === '/pay/.deposit' && request.method === 'POST') {
const pubkey = await getNostrPubkey(request);
if (!pubkey) {
return reply.code(401).send({ error: 'NIP-98 authentication required' });
}
const deposit = parseDepositBody(request.body);
// --- MRC20 token deposit ---
if (deposit.type === 'mrc20') {
if (!payAddress) {
return reply.code(400).send({
error: 'MRC20 deposits not configured (no payAddress set)'
});
}
// Replay protection: reject duplicate state hashes
const stateHash = jcs(deposit.state);
const isNew = await checkAndRecordState(stateHash);
if (!isNew) {
return reply.code(400).send({ error: 'Replay: this state has already been used for a deposit' });
}
let result;
// Anchor verification (if anchor data provided)
if (deposit.anchor && deposit.anchor.pubkey && deposit.anchor.stateStrings) {
result = await verifyMrc20Anchor({
state: deposit.state,
prevState: deposit.prevState,
toAddress: payAddress,
pubkey: deposit.anchor.pubkey,
stateStrings: deposit.anchor.stateStrings,
mempoolUrl,
network: deposit.anchor.network || 'testnet4'
});
} else {
// Fallback: verify chain integrity only (no anchor check)
result = verifyMrc20Deposit({
state: deposit.state,
prevState: deposit.prevState,
toAddress: payAddress
});
}
if (!result.valid) {
return reply.code(400).send({ error: result.error });
}
const didUri = pubkeyToDidNostr(pubkey);
const ledger = await readLedger();
const newBalance = credit(ledger, didUri, result.amount);
await writeLedger(ledger);
return reply.send({
did: didUri,
deposited: result.amount,
ticker: result.ticker,
balance: newBalance,
unit: 'token',
...(result.address ? { anchor: result.address } : {})
});
}
// --- Sats deposit (TXO URI) ---
if (deposit.type === 'sats') {
// Detect chain from TXO URI prefix (e.g. "txo:tbtc3:txid:vout")
const chainId = parseTxoChain(deposit.txo);
let depositMempoolUrl = mempoolUrl;
let currency = null; // null = default (simple string format)
if (chainId && payChains && payChains.includes(chainId)) {
const chain = CHAIN_REGISTRY[chainId];
depositMempoolUrl = chain.explorer.replace(/\/api$/, '');
currency = chain.unit;
} else if (chainId && payChains) {
return reply.code(400).send({
error: `Chain '${chainId}' not enabled. Enabled chains: ${payChains.join(', ')}`,
});
}
const result = await verifySatsDeposit(deposit.txo, depositMempoolUrl);
if (!result.valid) {
return reply.code(400).send({ error: result.error });
}
const didUri = pubkeyToDidNostr(pubkey);
const ledger = await readLedger();
const newBalance = credit(ledger, didUri, result.amount, currency);
await writeLedger(ledger);
return reply.send({
did: didUri,
deposited: result.amount,
balance: newBalance,
unit: currency || 'sat',
...(chainId ? { chain: chainId } : {})
});
}
// --- Claim deposit: user sent sats to pod's address ---
if (deposit.type === 'claim') {
const kp = await loadOrCreateKeypair();
const chainId = deposit.chain || (payChains ? payChains[0] : 'tbtc4');
if (payChains && !payChains.includes(chainId)) {
return reply.code(400).send({ error: `Chain '${chainId}' not enabled`, enabledChains: payChains });
}
const chain = CHAIN_REGISTRY[chainId];
if (!chain) {
return reply.code(400).send({ error: `Unknown chain: ${chainId}` });
}
// Derive address — try per-user tweaked address first, fall back to generic
const network = chainId === 'btc' ? 'mainnet' : (chainId === 'tbtc3' ? 'testnet' : 'testnet4');
const didUri = pubkeyToDidNostr(pubkey);
const userAddress = btAddress(kp.pubkey, [didUri], network);
const podAddress = btAddress(kp.pubkey, [], network);
// Fetch transaction from mempool
let txData;
try {
const txResp = await fetch(`${chain.explorer}/tx/${deposit.txid}`);
if (!txResp.ok) {
return reply.code(400).send({ error: 'Transaction not found' });
}
txData = await txResp.json();
} catch (err) {
return reply.code(502).send({ error: `Failed to verify transaction: ${err.message}` });
}
const output = txData.vout?.[deposit.vout];
if (!output) {
return reply.code(400).send({ error: `Output ${deposit.vout} not found` });
}
// Verify output pays our address (per-user tweaked or generic pod address)
const outputAddr = output.scriptpubkey_address;
const tweak = outputAddr === userAddress ? didUri : null;
if (outputAddr !== userAddress && outputAddr !== podAddress) {
return reply.code(400).send({ error: 'Output does not pay this pod\'s address', expected: { user: userAddress, pod: podAddress } });
}
const amount = output.value;
const currency = chain.unit;
// Replay protection + UTXO tracking
const utxos = await loadUtxos();
if (utxos.find(u => u.txid === deposit.txid && u.vout === deposit.vout)) {
return reply.code(400).send({ error: 'This output has already been claimed' });
}
utxos.push({ txid: deposit.txid, vout: deposit.vout, amount, scriptpubkey: output.scriptpubkey, chain: chainId, tweak, spent: false });
await saveUtxos(utxos);
const ledger = await readLedger();
const newBalance = credit(ledger, didUri, amount, currency);
await writeLedger(ledger);
return reply.send({
did: didUri,
deposited: amount,
balance: newBalance,
unit: currency,
chain: chainId,
txid: deposit.txid,
address: podAddress
});
}
return reply.code(400).send({
error: 'Invalid deposit format. Send a TXO URI string, MRC20 state proof, or claim {txid, vout}.',
formats: {
sats: 'POST body: "<txid>:<vout>" or {"txo": "<txid>:<vout>"}',
mrc20: 'POST body: {"type": "mrc20", "state": {...}, "prevState": {...}}',
claim: 'POST body: {"txid": "...", "vout": 0, "chain": "tbtc4"}'
}
});
}
// --- POST /pay/.buy — primary market: buy tokens with sats ---
if (url === '/pay/.buy' && request.method === 'POST') {
const pubkey = await getNostrPubkey(request);
if (!pubkey) {
return reply.code(401).send({ error: 'NIP-98 authentication required' });
}
if (!payToken) {
return reply.code(400).send({ error: 'Primary market not configured (no --pay-token set)' });
}
// Parse buy request
let body = request.body;
try {
if (Buffer.isBuffer(body)) body = JSON.parse(body.toString('utf8'));
if (typeof body === 'string') body = JSON.parse(body);
} catch {
return reply.code(400).send({ error: 'Invalid JSON body' });
}
const ticker = body?.ticker || payToken;
if (ticker !== payToken) {
return reply.code(400).send({ error: `This pod only sells ${payToken}` });
}
// Calculate amount and cost
let tokenAmount, satCost;
if (body?.amount) {
tokenAmount = Math.floor(body.amount);
satCost = tokenAmount * payRate;
} else if (body?.sats) {
satCost = Math.floor(body.sats);
tokenAmount = Math.floor(satCost / payRate);
} else {
return reply.code(400).send({
error: 'Specify amount (tokens to buy) or sats (sats to spend)',
rate: payRate,
unit: 'sat/token'
});
}
if (tokenAmount <= 0) {
return reply.code(400).send({ error: 'Amount must be positive' });
}
// Determine payment currency — chain-specific (e.g. "tbtc4") or generic "sat"
const currency = (body?.currency && payChains && payChains.includes(body.currency))
? body.currency : null;
// Check balance
const didUri = pubkeyToDidNostr(pubkey);
const ledger = await readLedger();
const balance = getBalance(ledger, didUri, currency);
if (balance < satCost) {
return reply.code(402).send({
error: `Insufficient ${currency || 'sat'} balance`,
balance,
cost: satCost,
rate: payRate,
deposit: '/pay/.deposit'
});
}
// Load token trail
const trail = await loadTrail(ticker);
if (!trail) {
return reply.code(500).send({ error: `Token ${ticker} not minted on this pod` });
}
// Transfer tokens to buyer
let result;
try {
result = await transferToken({
ticker,
to: pubkey,
amount: tokenAmount,
mempoolUrl
});
} catch (err) {
return reply.code(500).send({ error: `Transfer failed: ${err.message}` });
}
// Debit from buyer
debit(ledger, didUri, satCost, currency);
await writeLedger(ledger);
return reply.send({
bought: tokenAmount,
ticker,
cost: satCost,
rate: payRate,
balance: getBalance(ledger, didUri, currency),
unit: currency || 'sat',
txid: result.txid,
proof: {
state: result.state,
prevState: result.prevState,
anchor: {
pubkey: result.trail.pubkeyBase,
stateStrings: result.trail.stateStrings,
network: result.trail.network
}
}
});
}
// --- POST /pay/.withdraw — withdraw balance as tokens ---
if (url === '/pay/.withdraw' && request.method === 'POST') {
const pubkey = await getNostrPubkey(request);
if (!pubkey) {
return reply.code(401).send({ error: 'NIP-98 authentication required' });
}
if (!payToken) {
return reply.code(400).send({ error: 'Withdrawal not configured (no --pay-token set)' });
}
// Parse withdraw request
let body = request.body;
try {
if (Buffer.isBuffer(body)) body = JSON.parse(body.toString('utf8'));
if (typeof body === 'string') body = JSON.parse(body);
} catch {
return reply.code(400).send({ error: 'Invalid JSON body' });
}
const didUri = pubkeyToDidNostr(pubkey);
const ledger = await readLedger();
if (body?.currency && (!payChains || !payChains.includes(body.currency))) {
return reply.code(400).send({ error: `Unsupported currency: ${body.currency}`, enabledChains: payChains || [] });
}
const currency = body?.currency || null;
const balance = getBalance(ledger, didUri, currency);
// Calculate withdrawal amount
let satCost, tokenAmount;
if (body?.all) {
satCost = balance;
tokenAmount = Math.floor(balance / payRate);
} else if (body?.sats) {
satCost = Math.floor(body.sats);
tokenAmount = Math.floor(satCost / payRate);
} else if (body?.tokens) {
tokenAmount = Math.floor(body.tokens);
satCost = tokenAmount * payRate;
} else {
return reply.code(400).send({
error: 'Specify tokens, sats, or all: true',
balance,
rate: payRate,
unit: 'sat/token'
});
}
if (tokenAmount <= 0) {
return reply.code(400).send({ error: 'Nothing to withdraw', balance, rate: payRate });
}
if (balance < satCost) {
return reply.code(402).send({
error: 'Insufficient balance',
balance,
cost: satCost,
rate: payRate
});
}
// Load token trail
const trail = await loadTrail(payToken);
if (!trail) {
return reply.code(500).send({ error: `Token ${payToken} not minted on this pod` });
}
// Transfer tokens to user
let result;
try {
result = await transferToken({
ticker: payToken,
to: pubkey,
amount: tokenAmount,
mempoolUrl
});
} catch (err) {
return reply.code(500).send({ error: `Transfer failed: ${err.message}` });
}
// Debit balance
debit(ledger, didUri, satCost, currency);
await writeLedger(ledger);
return reply.send({
withdrawn: tokenAmount,
ticker: payToken,
cost: satCost,
rate: payRate,
balance: getBalance(ledger, didUri, currency),
unit: currency || 'sat',
txid: result.txid,
proof: {
state: result.state,
prevState: result.prevState,
anchor: {
pubkey: result.trail.pubkeyBase,
stateStrings: result.trail.stateStrings,
network: result.trail.network
}
}
});
}
// --- POST /pay/.withdraw-sats — withdraw sats as a TXO voucher ---
if (url === '/pay/.withdraw-sats' && request.method === 'POST') {
const pubkey = await getNostrPubkey(request);
if (!pubkey) {
return reply.code(401).send({ error: 'NIP-98 authentication required' });
}
let body = request.body;
try {
if (Buffer.isBuffer(body)) body = JSON.parse(body.toString('utf8'));
if (typeof body === 'string') body = JSON.parse(body);
} catch {
return reply.code(400).send({ error: 'Invalid JSON body' });
}
const withdrawAmount = parseInt(body?.amount, 10);
const chainId = body?.chain || (payChains ? payChains[0] : 'tbtc4');
if (!withdrawAmount || withdrawAmount <= 0) {
return reply.code(400).send({ error: 'Specify amount to withdraw' });
}
if (payChains && !payChains.includes(chainId)) {
return reply.code(400).send({ error: `Chain '${chainId}' not enabled` });
}
const chain = CHAIN_REGISTRY[chainId];
if (!chain) {
return reply.code(400).send({ error: `Unknown chain: ${chainId}` });
}
const currency = chain.unit;
// Check user balance
const didUri = pubkeyToDidNostr(pubkey);
const ledger = await readLedger();
const balance = getBalance(ledger, didUri, currency);
if (balance < withdrawAmount) {
return reply.code(402).send({ error: 'Insufficient balance', balance, requested: withdrawAmount, unit: currency });
}
// Find unspent UTXOs for this chain
const utxos = await loadUtxos();
const available = utxos.filter(u => u.chain === chainId && !u.spent);
if (available.length === 0) {
return reply.code(400).send({ error: 'No UTXOs available for withdrawal' });
}
// Load pod keypair
const kp = await loadOrCreateKeypair();
// Select UTXOs — group by tweak so we can sign with one key
// Prefer untweaked UTXOs first, then tweaked ones
const fee = 300;
const needed = withdrawAmount + fee;
let selected = [];
let total = 0;
let selectedTweak = null;
// Try untweaked first
for (const utxo of available.filter(u => !u.tweak)) {
selected.push(utxo);
total += utxo.amount;
if (total >= needed) break;
}
// If not enough, try tweaked (same tweak group only)
if (total < needed) {
const tweaked = available.filter(u => u.tweak);
selected = [];
total = 0;
selectedTweak = null;
for (const utxo of tweaked) {
if (selectedTweak && utxo.tweak !== selectedTweak) continue;
selected.push(utxo);
selectedTweak = utxo.tweak;
total += utxo.amount;
if (total >= needed) break;
}
}
if (total < needed) {
return reply.code(400).send({ error: 'Not enough UTXO value for withdrawal + fee', available: total, needed });
}
// Derive signing key (tweaked if UTXOs are tweaked)
const privkeyBytes = selectedTweak
? btDeriveChainedPrivkey(hexToBytes(kp.privkey), [selectedTweak])
: hexToBytes(kp.privkey);
// Generate a new keypair for the voucher recipient
const voucherPrivkey = secp256k1.utils.randomPrivateKey();
const voucherPubkey = secp256k1.getPublicKey(voucherPrivkey, true);
const voucherXonly = voucherPubkey.slice(1);
const voucherScript = p2trScript(voucherXonly);
// Build outputs: voucher + change back to pod
const outputs = [{ amount: withdrawAmount, scriptPubKey: voucherScript }];
const change = total - withdrawAmount - fee;
const podXonly = hexToBytes(kp.pubkey).slice(1);
if (change > 546) {
outputs.push({ amount: change, scriptPubKey: p2trScript(podXonly) });
}
// Build inputs
const inputs = selected.map(u => ({
txid: u.txid, vout: u.vout, amount: u.amount, scriptPubKey: hexToBytes(u.scriptpubkey)
}));
// Build and broadcast
let newTxid;
try {
const rawTx = buildTransaction(inputs, outputs, privkeyBytes);
newTxid = await broadcastTx(rawTx, chain.explorer.replace(/\/api$/, ''));
} catch (err) {
return reply.code(500).send({ error: `Broadcast failed: ${err.message}` });
}
// Debit user balance
const debitResult = debit(ledger, didUri, withdrawAmount, currency);
if (!debitResult.success) {
return reply.code(402).send({ error: 'Balance changed during withdrawal', balance: debitResult.balance });
}
await writeLedger(ledger);
// Mark UTXOs as spent, add change UTXO
for (const u of selected) { u.spent = true; }
if (change > 546) {
utxos.push({ txid: newTxid, vout: 1, amount: change, scriptpubkey: '5120' + bytesToHex(podXonly), chain: chainId, spent: false });
}
await saveUtxos(utxos);
// Return voucher URI
const voucherUri = `txo:${chainId}:${newTxid}:0?amount=${withdrawAmount}&key=${bytesToHex(voucherPrivkey)}`;
return reply.send({
voucher: voucherUri,
txid: newTxid,
amount: withdrawAmount,
unit: currency,
balance: getBalance(ledger, didUri, currency)
});
}
// --- GET /pay/.offers — list open sell orders ---
if (url === '/pay/.offers' && request.method === 'GET') {
const offers = await loadOffers();
return reply.send(offers.filter(o => o.status === 'pending'));
}
// --- POST /pay/.sell — create a sell order ---
if (url === '/pay/.sell' && request.method === 'POST') {
const pubkey = await getNostrPubkey(request);
if (!pubkey) {
return reply.code(401).send({ error: 'NIP-98 authentication required' });
}
if (!payToken) {
return reply.code(400).send({ error: 'Secondary market not configured (no --pay-token set)' });
}
let body = request.body;
try {
if (Buffer.isBuffer(body)) body = JSON.parse(body.toString('utf8'));
if (typeof body === 'string') body = JSON.parse(body);
} catch {
return reply.code(400).send({ error: 'Invalid JSON body' });
}
const amount = Math.floor(body?.amount || 0);
const price = Math.floor(body?.price || 0); // total sats for the lot
if (amount <= 0 || price <= 0) {
return reply.code(400).send({ error: 'Specify amount (tokens) and price (total sats)' });
}
// Verify seller has tokens on the trail
const trail = await loadTrail(payToken);
if (!trail) {
return reply.code(500).send({ error: `Token ${payToken} not minted on this pod` });
}
const currentState = trail.states[trail.states.length - 1];
const sellerBalance = currentState.balances[pubkey] || 0;
if (sellerBalance < amount) {
return reply.code(400).send({
error: 'Insufficient token balance on trail',
balance: sellerBalance,
amount
});
}
const offer = {
id: crypto.randomUUID(),
seller: pubkey,
ticker: payToken,
amount,
price,
rate: Math.round(price / amount * 100) / 100,
status: 'pending',
created: Date.now()
};
const offers = await loadOffers();
offers.push(offer);
await saveOffers(offers);
return reply.send(offer);
}
// --- POST /pay/.swap — execute a swap against a sell order ---
if (url === '/pay/.swap' && request.method === 'POST') {
const pubkey = await getNostrPubkey(request);
if (!pubkey) {
return reply.code(401).send({ error: 'NIP-98 authentication required' });
}
if (!payToken) {
return reply.code(400).send({ error: 'Secondary market not configured (no --pay-token set)' });
}
let body = request.body;
try {
if (Buffer.isBuffer(body)) body = JSON.parse(body.toString('utf8'));
if (typeof body === 'string') body = JSON.parse(body);
} catch {
return reply.code(400).send({ error: 'Invalid JSON body' });
}
const offerId = body?.id;
if (!offerId) {
return reply.code(400).send({ error: 'Specify offer id' });
}
// Find the offer
const offers = await loadOffers();
const offer = offers.find(o => o.id === offerId && o.status === 'pending');
if (!offer) {
return reply.code(404).send({ error: 'Offer not found or already filled' });
}
// Can't buy your own offer
if (offer.seller === pubkey) {
return reply.code(400).send({ error: 'Cannot swap with your own offer' });
}
// Check buyer's sat balance