Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ce714a4
feat: add BOLT-11 invoice prefix parser for amount validation
AndreaDiazCorreia Aug 21, 2026
971dcc3
fix(invoice): block payment when invoice terms disagree with the order
AndreaDiazCorreia Aug 21, 2026
d89cc1f
feat(invoice): anchor seller hold invoice to signed order amount and …
AndreaDiazCorreia Aug 21, 2026
656cef0
feat(invoice): block buyer payout when invoice request disagrees with…
AndreaDiazCorreia Aug 21, 2026
e7d5490
fix(invoice): follow info event stream to prevent stale fee rate reads
AndreaDiazCorreia Aug 24, 2026
fb5a9c4
fix(invoice): show the invoice amount in the manual payment flow
AndreaDiazCorreia Aug 24, 2026
a54fd45
feat(invoice): say when a settlement could not be checked
AndreaDiazCorreia Aug 24, 2026
883c69a
feat(invoice): hold a settlement to the terms pinned at commitment
AndreaDiazCorreia Aug 24, 2026
431aeb6
feat(invoice): re-price market orders against an independent rate
AndreaDiazCorreia Aug 24, 2026
0c35d7c
test(invoice): cover the settlement gating on both invoice screens
AndreaDiazCorreia Aug 24, 2026
3ae7036
fix(invoice): keep a fee unavailable at commitment from becoming a term
AndreaDiazCorreia Aug 24, 2026
dfedb47
fix(invoice): pin the fee rate on range remainder sessions
AndreaDiazCorreia Aug 24, 2026
c2414f3
fix(invoice): await cancellation before leaving the payment screen
AndreaDiazCorreia Aug 24, 2026
315e581
feat(invoice): refuse a settlement that runs off market against the user
AndreaDiazCorreia Aug 24, 2026
1f7a877
fix(invoice): record that pinning ran instead of inferring it
AndreaDiazCorreia Aug 24, 2026
aa58196
fix(invoice): do not quote an unreadable premium as zero
AndreaDiazCorreia Aug 24, 2026
d4a3323
fix(invoice): stop sending an amount NIP-47 wallets may refuse
AndreaDiazCorreia Aug 25, 2026
65fe829
fix(invoice): leave pre-pinning sessions out of the market check
AndreaDiazCorreia Aug 25, 2026
40270fd
fix(invoice): refuse a fee rate that leaves the arithmetic's domain
AndreaDiazCorreia Aug 25, 2026
8b5dac2
fix(invoice): bind the market override to the quote it was given for
AndreaDiazCorreia Aug 25, 2026
6f550dd
fix(invoice): tell the market check's four outcomes apart
AndreaDiazCorreia Aug 25, 2026
51eb490
fix(invoice): durably pin every term the settlement is checked against
AndreaDiazCorreia Aug 25, 2026
4e01319
fix(invoice): stop refusing settlements the check has nothing against
AndreaDiazCorreia Aug 25, 2026
f54d219
fix(invoice): keep the market check from flapping, and its anchors in…
AndreaDiazCorreia Aug 25, 2026
bb1904b
fix(invoice): fail closed when the settlement terms cannot be stored
AndreaDiazCorreia Aug 25, 2026
1400b6c
Merge remote-tracking branch 'origin/main' into fix/bind-invoice-amou…
AndreaDiazCorreia Aug 25, 2026
2e1a3c1
fix(invoice): check the payout invoice against the order's signed ter…
AndreaDiazCorreia Aug 25, 2026
606e9b9
fix(invoice): do not let a range remainder's anchor block a release
AndreaDiazCorreia Aug 25, 2026
914438d
fix(l10n): standardize Spanish imperative forms to formal second person
AndreaDiazCorreia Aug 27, 2026
a15c227
fix(order): fetch node info event before the order book to pin fee ra…
AndreaDiazCorreia Aug 27, 2026
98e7c8c
fix(order): cap the book subscription at 1000 events to bound startup…
AndreaDiazCorreia Aug 27, 2026
030d7b1
fix(invoice): compare late-arriving fee rates against the commitment …
AndreaDiazCorreia Aug 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions lib/features/order/providers/settlement_anchor_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mostro_mobile/data/models/nostr_event.dart';
import 'package:mostro_mobile/features/mostro/mostro_instance.dart';
import 'package:mostro_mobile/services/logger_service.dart';
import 'package:mostro_mobile/shared/providers/order_repository_provider.dart';
import 'package:mostro_mobile/shared/utils/settlement_amounts.dart';

/// The order amount the node has published for [orderId], in satoshis.
///
/// Read off the kind-38383 order event rather than the direct message that
/// asks for the payment. Both come from the node, but only one of them is the
/// order the user chose: the event is addressable and public, and it is what
/// the take screen showed. A market-price order carries no amount until it is
/// taken, and the node republishes the event with the resolved figure as part
/// of that flow, so by the time either side is asked to act the amount is
/// there.
///
/// Null when no event has arrived, or when it carries no usable amount.
final signedOrderAmountProvider = Provider.family<int?, String>((ref, orderId) {
final event = ref.watch(eventProvider(orderId));
if (event == null) return null;

final amount = int.tryParse(event.amount ?? '');
if (amount == null || amount <= 0) return null;
return amount;
});

/// The node's fee rate, from its kind-38385 info event.
///
/// Null when the info event has not arrived or does not carry the tag. The
/// getter parses eagerly and throws on a missing tag, which is fine for the
/// About screen it was written for and not for a payment check.
final nodeFeeRateProvider = Provider<double?>((ref) {
final info = ref.read(orderRepositoryProvider).mostroInstance;
Comment thread
AndreaDiazCorreia marked this conversation as resolved.
Outdated
if (info == null) return null;
try {
return info.fee;
} catch (e) {
logger.w('Node info event carries no usable fee rate: $e');
return null;
}
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

/// What the seller's hold invoice for [orderId] should ask for, derived from
/// the signed order amount and the signed fee rate.
///
/// Null when either input is missing. A caller that gets null has not learned
/// that the payment is wrong — only that it cannot re-derive what it should
/// be, and should fall back to the weaker check it can still make.
final anchoredSellerAmountProvider =
Provider.family<int?, String>((ref, orderId) {
final amountSats = ref.watch(signedOrderAmountProvider(orderId));
final feeRate = ref.watch(nodeFeeRateProvider);
if (amountSats == null || feeRate == null) return null;

return SettlementAmounts.sellerPays(
amountSats: amountSats,
feeRate: feeRate,
);
});

/// What the buyer's payout invoice for [orderId] should ask for, derived from
/// the signed order amount and the signed fee rate.
///
/// The figure is the order amount less the buyer's half of the fee, so it is
/// never the amount shown on the order — a client comparing against that
/// would refuse every correct payout.
///
/// Null on the same terms as [anchoredSellerAmountProvider]: the caller has
/// learned nothing about whether the request is right, only that it cannot
/// re-derive what it should be.
final anchoredBuyerAmountProvider =
Provider.family<int?, String>((ref, orderId) {
final amountSats = ref.watch(signedOrderAmountProvider(orderId));
final feeRate = ref.watch(nodeFeeRateProvider);
if (amountSats == null || feeRate == null) return null;

return SettlementAmounts.buyerReceives(
amountSats: amountSats,
feeRate: feeRate,
);
});
129 changes: 108 additions & 21 deletions lib/features/order/screens/add_lightning_invoice_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:mostro_mobile/core/app_theme.dart';
import 'package:mostro_mobile/features/order/providers/settlement_anchor_provider.dart';
import 'package:mostro_mobile/features/order/providers/order_notifier_provider.dart';
import 'package:mostro_mobile/features/order/widgets/order_app_bar.dart';
import 'package:mostro_mobile/features/wallet/providers/nwc_provider.dart';
Expand Down Expand Up @@ -80,6 +81,17 @@ class _AddLightningInvoiceScreenState
reputation: orderState.peerReputation,
);

// What this order should pay out, re-derived from the terms the node
// signed: the amount in its kind-38383 order event, less the buyer's
// half of the fee rate in its kind-38385 one. The figure above came
// from the message asking for the invoice, and an invoice minted for
// it is what the trade settles at.
//
// Null means the events have not both arrived, not that the request
// is wrong, so the screen only refuses on an actual disagreement.
final expectedSats = ref.watch(anchoredBuyerAmountProvider(orderId));
final blocked = expectedSats != null && amount != expectedSats;
Comment thread
AndreaDiazCorreia marked this conversation as resolved.
Outdated

final nwcState = ref.watch(nwcProvider);
final isNwcConnected = nwcState.status == NwcStatus.connected;
final showLnAddressConfirmation =
Expand All @@ -99,27 +111,33 @@ class _AddLightningInvoiceScreenState
16,
16 + MediaQuery.of(context).viewPadding.bottom,
),
child: showLnAddressConfirmation
? _buildLnAddressConfirmation(header: header)
: showNwcInvoice
? _buildNwcInvoiceFlow(header: header)
: AddLightningInvoiceWidget(
controller: invoiceController,
onSubmit: () async {
final invoice = invoiceController.text.trim();
if (invoice.isNotEmpty) {
await _submitInvoice(invoice, amount);
}
},
onCancel: () async {
await _cancelOrder();
},
amount: amount ?? 0,
fiatAmount: fiatAmount,
fiatCode: fiatCode,
orderId: orderIdValue,
header: header,
),
child: blocked
? _buildBlockedFlow(
header: header,
requestedSats: amount ?? 0,
expectedSats: expectedSats,
)
Comment thread
AndreaDiazCorreia marked this conversation as resolved.
Outdated
: showLnAddressConfirmation
? _buildLnAddressConfirmation(header: header)
: showNwcInvoice
? _buildNwcInvoiceFlow(header: header)
: AddLightningInvoiceWidget(
controller: invoiceController,
onSubmit: () async {
final invoice = invoiceController.text.trim();
if (invoice.isNotEmpty) {
await _submitInvoice(invoice, amount);
}
},
onCancel: () async {
await _cancelOrder();
},
amount: amount ?? 0,
fiatAmount: fiatAmount,
fiatCode: fiatCode,
orderId: orderIdValue,
header: header,
),
),
);
},
Expand Down Expand Up @@ -192,6 +210,75 @@ class _AddLightningInvoiceScreenState
);
}

/// Shown instead of every invoice flow when the amount asked for is not the
/// amount this order should pay out.
///
/// A refusal rather than a warning: an invoice minted here is what the
/// trade settles at, and there is no confirming a figure the signed terms
/// contradict.
Widget _buildBlockedFlow({
required Widget header,
required int requestedSats,
required int expectedSats,
}) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
header,
const SizedBox(height: 24),
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: AppTheme.statusError.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: AppTheme.statusError.withValues(alpha: 0.3),
),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Icon(
Icons.warning_amber_rounded,
color: AppTheme.statusError,
size: 20,
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
S.of(context)!.invoiceRequestMismatchTitle,
style: const TextStyle(
color: AppTheme.statusError,
fontSize: 15,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 6),
Text(
S.of(context)!.invoiceRequestMismatchBody(
requestedSats.toString(),
expectedSats.toString(),
),
style: const TextStyle(
color: AppTheme.textSecondary,
fontSize: 13,
),
),
],
),
),
],
),
),
const Spacer(),
_buildCancelButton(),
],
);
}

Widget _buildCancelButton() {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
Expand Down
Loading
Loading