| 1 | import 'package:cw_core/payjoin_session.dart'; |
| 2 | import 'package:hive/hive.dart'; |
| 3 | import 'package:payjoin_flutter/receive.dart'; |
| 4 | import 'package:payjoin_flutter/send.dart'; |
| 5 | |
| 6 | class PayjoinStorage { |
| 7 | PayjoinStorage(this._payjoinSessionSources); |
| 8 | |
| 9 | final Box<PayjoinSession> _payjoinSessionSources; |
| 10 | |
| 11 | static const String _receiverPrefix = 'pj_recv_'; |
| 12 | static const String _senderPrefix = 'pj_send_'; |
| 13 | |
| 14 | Future<void> insertReceiverSession( |
| 15 | Receiver receiver, |
| 16 | String walletId, |
| 17 | ) => |
| 18 | _payjoinSessionSources.put( |
| 19 | "$_receiverPrefix${receiver.id()}", |
| 20 | PayjoinSession( |
| 21 | walletId: walletId, |
| 22 | receiver: receiver.toJson(), |
| 23 | ), |
| 24 | ); |
| 25 | |
| 26 | PayjoinSession? getUnusedActiveReceiverSession(String walletId) => _payjoinSessionSources.values |
| 27 | .where((session) => |
| 28 | session.walletId == walletId && |
| 29 | session.status == PayjoinSessionStatus.created.name && |
| 30 | !session.isSenderSession) |
| 31 | .firstOrNull; |
| 32 | |
| 33 | Future<void> markReceiverSessionComplete(String sessionId, String txId, String amount) async { |
| 34 | final session = _payjoinSessionSources.get("$_receiverPrefix${sessionId}")!; |
| 35 | |
| 36 | session.status = PayjoinSessionStatus.success.name; |
| 37 | session.txId = txId; |
| 38 | session.rawAmount = amount; |
| 39 | await session.save(); |
| 40 | } |
| 41 | |
| 42 | Future<void> markReceiverSessionUnrecoverable(String sessionId, String reason) async { |
| 43 | final session = _payjoinSessionSources.get("$_receiverPrefix${sessionId}")!; |
| 44 | |
| 45 | session.status = PayjoinSessionStatus.unrecoverable.name; |
| 46 | session.error = reason; |
| 47 | await session.save(); |
| 48 | } |
| 49 | |
| 50 | Future<void> markReceiverSessionInProgress(String sessionId) async { |
| 51 | final session = _payjoinSessionSources.get("$_receiverPrefix${sessionId}")!; |
| 52 | |
| 53 | session.status = PayjoinSessionStatus.inProgress.name; |
| 54 | session.inProgressSince = DateTime.now(); |
| 55 | await session.save(); |
| 56 | } |
| 57 | |
| 58 | Future<void> insertSenderSession( |
| 59 | Sender sender, |
| 60 | String pjUrl, |
| 61 | String walletId, |
| 62 | BigInt amount, |
| 63 | ) => |
| 64 | _payjoinSessionSources.put( |
| 65 | "$_senderPrefix$pjUrl", |
| 66 | PayjoinSession( |
| 67 | walletId: walletId, |
| 68 | pjUri: pjUrl, |
| 69 | sender: sender.toJson(), |
| 70 | status: PayjoinSessionStatus.inProgress.name, |
| 71 | inProgressSince: DateTime.now(), |
| 72 | rawAmount: amount.toString(), |
| 73 | ), |
| 74 | ); |
| 75 | |
| 76 | Future<void> markSenderSessionComplete(String pjUrl, String txId) async { |
| 77 | final session = _payjoinSessionSources.get("$_senderPrefix$pjUrl")!; |
| 78 | |
| 79 | session.status = PayjoinSessionStatus.success.name; |
| 80 | session.txId = txId; |
| 81 | await session.save(); |
| 82 | } |
| 83 | |
| 84 | Future<void> markSenderSessionUnrecoverable(String pjUrl, String reason) async { |
| 85 | final session = _payjoinSessionSources.get("$_senderPrefix$pjUrl")!; |
| 86 | |
| 87 | session.status = PayjoinSessionStatus.unrecoverable.name; |
| 88 | session.error = reason; |
| 89 | await session.save(); |
| 90 | } |
| 91 | |
| 92 | List<PayjoinSession> readAllOpenSessions(String walletId) => _payjoinSessionSources.values |
| 93 | .where((session) => |
| 94 | session.walletId == walletId && |
| 95 | ![PayjoinSessionStatus.success.name, PayjoinSessionStatus.unrecoverable.name] |
| 96 | .contains(session.status)) |
| 97 | .toList(); |
| 98 | } |