forked from StellarEdu/StellarEduPay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellarService.js
More file actions
659 lines (588 loc) · 19 KB
/
Copy pathstellarService.js
File metadata and controls
659 lines (588 loc) · 19 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
"use strict";
const {
server,
isAcceptedAsset,
CONFIRMATION_THRESHOLD,
} = require("../config/stellarConfig");
const Payment = require("../models/paymentModel");
const Student = require("../models/studentModel");
const PaymentIntent = require("../models/paymentIntentModel");
const { validatePaymentAmount } = require("../utils/paymentLimits");
const { withStellarRetry } = require("../utils/withStellarRetry");
const { savePayment } = require("./transactionService");
const logger = require("../utils/logger").child("StellarService");
function detectAsset(payOp) {
const assetType = payOp.asset_type;
const assetCode = assetType === "native" ? "XLM" : payOp.asset_code;
const assetIssuer = assetType === "native" ? null : payOp.asset_issuer;
const { accepted } = isAcceptedAsset(assetCode, assetType);
if (!accepted) return null;
return { assetCode, assetType, assetIssuer };
}
function normalizeAmount(rawAmount) {
return parseFloat(parseFloat(rawAmount).toFixed(7));
}
/**
* Extract and validate the payment operation from a transaction.
* walletAddress is passed explicitly — supports per-school wallets.
* Returns { payOp, memo, asset } or null if the transaction is invalid.
*/
async function extractValidPayment(tx, walletAddress) {
if (!tx.successful) return null;
// Only process MEMO_TEXT — MEMO_ID, MEMO_HASH, MEMO_RETURN produce
// non-string values that cannot be matched to a student ID.
if (tx.memo_type !== 'text') return null;
const memo = tx.memo ? tx.memo.trim() : null;
if (!memo) return null;
const ops = await withStellarRetry(() => tx.operations(), {
label: "extractValidPayment.operations",
});
const payOp = ops.records.find(
(op) => op.type === "payment" && op.to === walletAddress,
);
if (!payOp) return null;
const asset = detectAsset(payOp);
if (!asset) return null;
return { payOp, memo, asset };
}
function validatePaymentAgainstFee(paymentAmount, expectedFee) {
if (paymentAmount < expectedFee) {
return {
status: "underpaid",
excessAmount: 0,
message: `Payment of ${paymentAmount} is less than the required fee of ${expectedFee}`,
};
}
if (paymentAmount > expectedFee) {
const excess = parseFloat((paymentAmount - expectedFee).toFixed(7));
return {
status: "overpaid",
excessAmount: excess,
message: `Payment of ${paymentAmount} exceeds the required fee of ${expectedFee} by ${excess}`,
};
}
return {
status: "valid",
excessAmount: 0,
message: "Payment matches the required fee",
};
}
async function checkConfirmationStatus(txLedger) {
const latestLedger = await withStellarRetry(
() => server.ledgers().order("desc").limit(1).call(),
{ label: "checkConfirmationStatus" },
);
const latestSequence = latestLedger.records[0].sequence;
return latestSequence - txLedger >= CONFIRMATION_THRESHOLD;
}
/**
* Detect memo collision: same memo used by a different sender within 24h,
* or payment amount is wildly outside the expected fee range.
* Query is school-scoped via schoolId.
*/
async function detectMemoCollision(
memo,
senderAddress,
paymentAmount,
expectedFee,
txDate,
schoolId,
) {
const COLLISION_WINDOW_MS = 24 * 60 * 60 * 1000;
const windowStart = new Date(txDate.getTime() - COLLISION_WINDOW_MS);
const recentFromOtherSender = await Payment.findOne({
schoolId,
studentId: memo,
senderAddress: { $ne: senderAddress, $exists: true, $ne: null },
confirmedAt: { $gte: windowStart },
});
if (recentFromOtherSender) {
return {
suspicious: true,
reason:
'Memo "' +
memo +
'" was used by a different sender (' +
recentFromOtherSender.senderAddress +
") within the last 24 hours",
};
}
}
/**
* Detect abnormal payment patterns:
* 1. Rapid repeated transactions — same sender sends more than RAPID_TX_LIMIT
* payments within RAPID_TX_WINDOW_MS.
* 2. Unusual amount — payment deviates from the expected fee by more than
* UNUSUAL_AMOUNT_MULTIPLIER (e.g. 3×).
*
* Returns { suspicious: boolean, reason: string|null }
*/
async function detectAbnormalPatterns(
senderAddress,
paymentAmount,
expectedFee,
txDate,
schoolId,
) {
const RAPID_TX_WINDOW_MS = 10 * 60 * 1000; // 10 minutes
const RAPID_TX_LIMIT = 3; // more than this many = suspicious
const UNUSUAL_AMOUNT_MULTIPLIER = 3; // >3× or <1/3 of expected fee
const reasons = [];
// 1. Velocity check — rapid repeated transactions from the same sender
if (senderAddress) {
const windowStart = new Date(txDate.getTime() - RAPID_TX_WINDOW_MS);
const recentCount = await Payment.countDocuments({
schoolId,
senderAddress,
confirmedAt: { $gte: windowStart },
});
if (recentCount >= RAPID_TX_LIMIT) {
reasons.push(
`Sender ${senderAddress} made ${recentCount + 1} transactions within 10 minutes`,
);
}
}
// 2. Unusual amount check
if (expectedFee && expectedFee > 0) {
const ratio = paymentAmount / expectedFee;
if (
ratio > UNUSUAL_AMOUNT_MULTIPLIER ||
ratio < 1 / UNUSUAL_AMOUNT_MULTIPLIER
) {
reasons.push(
`Unusual payment amount ${paymentAmount} vs expected fee ${expectedFee} (ratio ${ratio.toFixed(2)})`,
);
}
}
if (reasons.length > 0) {
return { suspicious: true, reason: reasons.join("; ") };
}
return { suspicious: false, reason: null };
}
/**
* Verify a single transaction hash against a specific school wallet.
* Throws structured errors for all failure cases.
*
* Error codes:
* NOT_FOUND (404) — txHash does not exist on Horizon
* HORIZON_UNAVAILABLE (503) — Horizon unreachable / rate-limited / 5xx
* TX_FAILED (400) — transaction found but failed on-chain
* MISSING_MEMO (400) — no memo on the transaction
* INVALID_DESTINATION (400) — no payment op to the school wallet
* UNSUPPORTED_ASSET (400) — asset not accepted
* AMOUNT_TOO_LOW/HIGH (400) — outside configured limits
*/
async function verifyTransaction(txHash, walletAddress) {
const tx = await withStellarRetry(
() => server.transactions().transaction(txHash).call(),
{ label: "verifyTransaction" },
);
// 1. Validate transaction success
if (tx.successful === false) {
const err = new Error(
"Transaction was not successful on the Stellar network",
);
err.code = "TX_FAILED";
throw err;
}
const memo = tx.memo ? tx.memo.trim() : null;
if (!memo) {
const err = new Error(
"Transaction memo is missing or empty — cannot identify student",
);
err.code = "MISSING_MEMO";
throw err;
}
const ops = await withStellarRetry(() => tx.operations(), {
label: "verifyTransaction.operations",
});
const payOp = ops.records.find(
(op) => op.type === "payment" && op.to === walletAddress,
);
if (!payOp) {
const err = new Error(
`No payment operation found targeting the school wallet (${walletAddress})`,
);
err.code = "INVALID_DESTINATION";
throw err;
}
const asset = detectAsset(payOp);
if (!asset) {
const assetCode =
payOp.asset_type === "native"
? "XLM"
: payOp.asset_code || payOp.asset_type;
const err = new Error(`Unsupported asset: ${assetCode}`);
err.code = "UNSUPPORTED_ASSET";
err.assetCode = assetCode;
throw err;
}
const amount = normalizeAmount(payOp.amount);
// 5. Validate payment amount is within configured limits
const limitValidation = validatePaymentAmount(amount);
if (!limitValidation.valid) {
const err = new Error(limitValidation.error);
err.code = limitValidation.code;
throw err;
}
// 6. Look up student to validate fee (student lookup is not school-scoped here
// since memo = studentId; recordPayment caller passes schoolId explicitly)
const student = await Student.findOne({ studentId: memo });
const feeAmount = student ? student.feeAmount : null;
const feeValidation =
feeAmount != null
? validatePaymentAgainstFee(amount, feeAmount)
: {
status: "unknown",
excessAmount: 0,
message: "Student not found, cannot validate fee",
};
// Extract network fee from transaction
const networkFee = parseFloat(tx.fee_paid || "0") / 10000000; // Convert stroops to XLM
return {
hash: tx.hash,
memo: memo,
studentId: memo,
amount: amount,
assetCode: asset.assetCode,
assetType: asset.assetType,
feeAmount,
feeValidation,
networkFee,
date: tx.created_at,
ledger: tx.ledger_attr || tx.ledger || null,
senderAddress: payOp.from || null,
};
}
/**
* Fetch recent transactions for a specific school wallet and record new payments.
*
* @param {object} school - School document with { schoolId, stellarAddress }
*/
async function syncPaymentsForSchool(school) {
const { schoolId, stellarAddress } = school;
let page = await withStellarRetry(
() =>
server
.transactions()
.forAccount(stellarAddress)
.order("desc")
.limit(200)
.call(),
{ label: `syncPaymentsForSchool(${schoolId})` },
);
let done = false;
let newPayments = 0;
while (!done) {
for (const tx of page.records) {
const existing = await Payment.findOne({ txHash: tx.hash });
if (existing) {
done = true;
break;
}
const valid = await extractValidPayment(tx, stellarAddress);
if (!valid) continue;
const { payOp, memo } = valid;
const intent = await PaymentIntent.findOne({
schoolId,
memo,
status: "pending",
});
if (!intent) continue;
const student = await Student.findOne({
schoolId,
studentId: intent.studentId,
});
if (!student) continue;
const paymentAmount = parseFloat(payOp.amount);
const limitValidation = validatePaymentAmount(paymentAmount);
if (!limitValidation.valid) continue;
const senderAddress = payOp.from || null;
const txDate = new Date(tx.created_at);
const txLedger = tx.ledger_attr || tx.ledger || null;
const isConfirmed = txLedger
? await checkConfirmationStatus(txLedger)
: false;
const confirmationStatus = isConfirmed
? "confirmed"
: "pending_confirmation";
const collision = await detectMemoCollision(
memo,
senderAddress,
paymentAmount,
student.feeAmount,
txDate,
schoolId,
);
const previousPayments = await Payment.aggregate([
{ $match: { schoolId, studentId: intent.studentId } },
{ $group: { _id: null, total: { $sum: "$amount" } } },
]);
const previousTotal = previousPayments.length
? previousPayments[0].total
: 0;
const cumulativeTotal = parseFloat(
(previousTotal + paymentAmount).toFixed(7),
);
let cumulativeStatus;
if (cumulativeTotal < student.feeAmount) cumulativeStatus = "underpaid";
else if (cumulativeTotal > student.feeAmount)
cumulativeStatus = "overpaid";
else cumulativeStatus = "valid";
const excessAmount =
cumulativeStatus === "overpaid"
? parseFloat((cumulativeTotal - student.feeAmount).toFixed(7))
: 0;
const feeValidation = validatePaymentAgainstFee(
paymentAmount,
intent.amount,
);
if (feeValidation.status === "underpaid") {
logger.warn("Underpaid transaction skipped", {
txHash: tx.hash,
schoolId,
studentId: intent.studentId,
paid: paymentAmount,
required: intent.amount,
});
await savePayment({
schoolId,
studentId: intent.studentId,
txHash: tx.hash,
amount: paymentAmount,
feeAmount: intent.amount,
feeCategory: intent.feeCategory || null,
feeValidationStatus: "underpaid",
excessAmount: 0,
status: "FAILED",
memo,
senderAddress,
isSuspicious: true,
suspicionReason: feeValidation.message,
ledger: txLedger,
confirmationStatus: "failed",
confirmedAt: txDate,
});
newPayments++;
continue;
}
await savePayment({
schoolId,
studentId: intent.studentId,
txHash: tx.hash,
amount: paymentAmount,
feeAmount: intent.amount,
feeCategory: intent.feeCategory || null,
feeValidationStatus: cumulativeStatus,
excessAmount,
status: "confirmed",
memo,
senderAddress,
isSuspicious: collision.suspicious,
suspicionReason: collision.reason,
ledger: txLedger,
confirmationStatus,
confirmedAt: txDate,
});
newPayments++;
logger.info("Transaction recorded", {
txHash: tx.hash,
schoolId,
studentId: intent.studentId,
amount: paymentAmount,
feeValidationStatus: cumulativeStatus,
isSuspicious: collision.suspicious,
confirmationStatus,
});
if (isConfirmed && !collision.suspicious) {
// Update student record
const updateData = {
totalPaid: cumulativeTotal,
feePaid: cumulativeTotal >= student.feeAmount,
};
// If this payment is for a specific fee category, update that category's paid status
if (intent.feeCategory && student.fees && student.fees.length > 0) {
const feeIndex = student.fees.findIndex(f => f.category === intent.feeCategory);
if (feeIndex !== -1) {
// Calculate new total paid for this category
const categoryPayments = await Payment.aggregate([
{
$match: {
schoolId,
studentId: intent.studentId,
feeCategory: intent.feeCategory,
confirmationStatus: "confirmed",
isSuspicious: false,
},
},
{ $group: { _id: null, total: { $sum: "$amount" } } },
]);
const categoryTotalPaid = categoryPayments.length
? parseFloat(categoryPayments[0].total.toFixed(7))
: 0;
// Update the specific fee category
student.fees[feeIndex].totalPaid = categoryTotalPaid;
student.fees[feeIndex].remainingBalance = Math.max(
0,
student.fees[feeIndex].amount - categoryTotalPaid
);
student.fees[feeIndex].paid = categoryTotalPaid >= student.fees[feeIndex].amount;
updateData.fees = student.fees;
}
}
await Student.findOneAndUpdate(
{ schoolId, studentId: intent.studentId },
updateData,
);
}
await PaymentIntent.findByIdAndUpdate(intent._id, {
status: "completed",
});
}
if (!done) {
if (page.records.length < 200) break; // last page
page = await withStellarRetry(() => page.next(), {
label: `syncPaymentsForSchool.next(${schoolId})`,
});
if (!page || !page.records.length) break;
}
}
return { newPayments };
}
/**
* Re-check all pending_confirmation payments for a school and promote them
* to confirmed once the ledger threshold has been met.
*
* @param {string} schoolId
*/
async function finalizeConfirmedPayments(schoolId) {
const pending = await Payment.find({
schoolId,
confirmationStatus: "pending_confirmation",
isSuspicious: false,
});
for (const payment of pending) {
if (!payment.ledgerSequence) continue;
const isConfirmed = await checkConfirmationStatus(payment.ledgerSequence);
if (!isConfirmed) continue;
if (typeof Payment.findByIdAndUpdate === "function") {
await Payment.findByIdAndUpdate(payment._id, {
confirmationStatus: "confirmed",
});
}
const student = await Student.findOne({
schoolId,
studentId: payment.studentId,
});
if (!student) continue;
const agg = await Payment.aggregate([
{
$match: {
schoolId,
studentId: payment.studentId,
confirmationStatus: "confirmed",
isSuspicious: false,
},
},
{ $group: { _id: null, total: { $sum: "$amount" } } },
]);
const totalPaid = agg.length ? parseFloat(agg[0].total.toFixed(7)) : 0;
const remainingBalance = parseFloat(
Math.max(0, student.feeAmount - totalPaid).toFixed(7),
);
const updateData = { totalPaid, remainingBalance, feePaid: totalPaid >= student.feeAmount };
// Update fee categories if they exist
if (student.fees && student.fees.length > 0) {
const categoryPayments = await Payment.aggregate([
{
$match: {
schoolId,
studentId: payment.studentId,
feeCategory: { $ne: null },
confirmationStatus: "confirmed",
isSuspicious: false,
},
},
{
$group: {
_id: "$feeCategory",
totalPaid: { $sum: "$amount" },
},
},
]);
const categoryPaymentMap = {};
categoryPayments.forEach(cp => {
categoryPaymentMap[cp._id] = parseFloat(cp.totalPaid.toFixed(7));
});
// Update each fee category
student.fees.forEach(fee => {
const paid = categoryPaymentMap[fee.category] || 0;
fee.totalPaid = paid;
fee.remainingBalance = Math.max(0, fee.amount - paid);
fee.paid = paid >= fee.amount;
});
updateData.fees = student.fees;
}
await Student.findOneAndUpdate(
{ schoolId, studentId: payment.studentId },
updateData,
);
}
}
/**
* Parse an incoming Stellar transaction for memo and payment amounts.
* If walletAddress is provided, only payments to that wallet are included.
*/
async function parseIncomingTransaction(txHash, walletAddress = null) {
let tx;
try {
tx = await withStellarRetry(
() => server.transactions().transaction(txHash).call(),
{ label: "parseIncomingTransaction" },
);
} catch (err) {
throw classifyHorizonError(err, `Transaction ${txHash}`);
}
const memo = tx.memo ? tx.memo.trim() : null;
let ops;
try {
ops = await withStellarRetry(() => tx.operations(), {
label: "parseIncomingTransaction.operations",
});
} catch (err) {
throw classifyHorizonError(err, "Transaction operations");
}
const payments = ops.records
.filter(
(op) =>
op.type === "payment" && (!walletAddress || op.to === walletAddress),
)
.map((op) => ({
from: op.from || null,
to: op.to,
amount: normalizeAmount(op.amount),
assetCode: op.asset_type === "native" ? "XLM" : op.asset_code,
assetType: op.asset_type,
assetIssuer: op.asset_issuer || null,
}));
return {
hash: tx.hash,
successful: tx.successful,
memo,
payments,
created_at: tx.created_at,
};
}
module.exports = {
syncPaymentsForSchool,
finalizeConfirmedPayments,
verifyTransaction,
parseIncomingTransaction,
validatePaymentAgainstFee,
detectAsset,
normalizeAmount,
extractValidPayment,
detectMemoCollision,
detectAbnormalPatterns,
checkConfirmationStatus,
};