|
| 1 | +/// Mobile-optimized transaction flows for common escrow operations. |
| 2 | +/// |
| 3 | +/// Each flow bundles the steps a mobile user takes into a single function: |
| 4 | +/// build → (optionally queue offline) → sign → submit. |
| 5 | +use crate::error::MobileSdkError; |
| 6 | +use crate::offline_queue::OfflineQueue; |
| 7 | +use crate::signing::sign_transaction; |
| 8 | +use crate::transaction_builder::{ |
| 9 | + build_cancel_trade, build_confirm_receipt, build_fund_trade, build_raise_dispute, |
| 10 | +}; |
| 11 | +use crate::types::{QueuedTransaction, SignedTransaction}; |
| 12 | + |
| 13 | +/// Result of a mobile flow step. |
| 14 | +pub enum FlowResult { |
| 15 | + /// Transaction was signed and is ready for submission. |
| 16 | + Ready(SignedTransaction), |
| 17 | + /// Device is offline; transaction was queued for later. |
| 18 | + Queued(String), |
| 19 | +} |
| 20 | + |
| 21 | +/// Fund a trade: build + sign in one call. |
| 22 | +/// If `secret_key_hex` is provided, signs immediately (offline-capable). |
| 23 | +/// If the device is offline (`is_online = false`), queues instead. |
| 24 | +pub fn flow_fund_trade( |
| 25 | + contract_id: &str, |
| 26 | + trade_id: u64, |
| 27 | + sequence: i64, |
| 28 | + network_passphrase: &str, |
| 29 | + secret_key_hex: &str, |
| 30 | + is_online: bool, |
| 31 | + queue: &mut OfflineQueue, |
| 32 | +) -> Result<FlowResult, MobileSdkError> { |
| 33 | + let unsigned = build_fund_trade(contract_id, trade_id, sequence, network_passphrase)?; |
| 34 | + |
| 35 | + if !is_online { |
| 36 | + let queued_id = format!("fund_trade_{trade_id}_{sequence}"); |
| 37 | + queue.enqueue(QueuedTransaction { |
| 38 | + id: queued_id.clone(), |
| 39 | + unsigned_xdr: unsigned.xdr, |
| 40 | + operation: format!("fund_trade:{trade_id}"), |
| 41 | + created_at: 0, // caller should set epoch seconds |
| 42 | + }); |
| 43 | + return Ok(FlowResult::Queued(queued_id)); |
| 44 | + } |
| 45 | + |
| 46 | + let signed = sign_transaction(&unsigned, secret_key_hex)?; |
| 47 | + Ok(FlowResult::Ready(signed)) |
| 48 | +} |
| 49 | + |
| 50 | +/// Confirm receipt: build + sign in one call. |
| 51 | +pub fn flow_confirm_receipt( |
| 52 | + contract_id: &str, |
| 53 | + trade_id: u64, |
| 54 | + sequence: i64, |
| 55 | + network_passphrase: &str, |
| 56 | + secret_key_hex: &str, |
| 57 | + is_online: bool, |
| 58 | + queue: &mut OfflineQueue, |
| 59 | +) -> Result<FlowResult, MobileSdkError> { |
| 60 | + let unsigned = build_confirm_receipt(contract_id, trade_id, sequence, network_passphrase)?; |
| 61 | + |
| 62 | + if !is_online { |
| 63 | + let queued_id = format!("confirm_receipt_{trade_id}_{sequence}"); |
| 64 | + queue.enqueue(QueuedTransaction { |
| 65 | + id: queued_id.clone(), |
| 66 | + unsigned_xdr: unsigned.xdr, |
| 67 | + operation: format!("confirm_receipt:{trade_id}"), |
| 68 | + created_at: 0, |
| 69 | + }); |
| 70 | + return Ok(FlowResult::Queued(queued_id)); |
| 71 | + } |
| 72 | + |
| 73 | + let signed = sign_transaction(&unsigned, secret_key_hex)?; |
| 74 | + Ok(FlowResult::Ready(signed)) |
| 75 | +} |
| 76 | + |
| 77 | +/// Raise dispute: build + sign in one call. |
| 78 | +pub fn flow_raise_dispute( |
| 79 | + contract_id: &str, |
| 80 | + trade_id: u64, |
| 81 | + sequence: i64, |
| 82 | + network_passphrase: &str, |
| 83 | + secret_key_hex: &str, |
| 84 | +) -> Result<SignedTransaction, MobileSdkError> { |
| 85 | + let unsigned = build_raise_dispute(contract_id, trade_id, sequence, network_passphrase)?; |
| 86 | + sign_transaction(&unsigned, secret_key_hex) |
| 87 | +} |
| 88 | + |
| 89 | +/// Cancel trade: build + sign in one call. |
| 90 | +pub fn flow_cancel_trade( |
| 91 | + contract_id: &str, |
| 92 | + trade_id: u64, |
| 93 | + sequence: i64, |
| 94 | + network_passphrase: &str, |
| 95 | + secret_key_hex: &str, |
| 96 | +) -> Result<SignedTransaction, MobileSdkError> { |
| 97 | + let unsigned = build_cancel_trade(contract_id, trade_id, sequence, network_passphrase)?; |
| 98 | + sign_transaction(&unsigned, secret_key_hex) |
| 99 | +} |
| 100 | + |
| 101 | +/// Drain the offline queue and return signed transactions ready for submission. |
| 102 | +/// Call this when connectivity is restored. |
| 103 | +pub fn flush_offline_queue( |
| 104 | + queue: &mut OfflineQueue, |
| 105 | + secret_key_hex: &str, |
| 106 | + network_passphrase: &str, |
| 107 | +) -> Vec<Result<SignedTransaction, MobileSdkError>> { |
| 108 | + queue |
| 109 | + .drain() |
| 110 | + .into_iter() |
| 111 | + .map(|queued| { |
| 112 | + let unsigned = crate::types::UnsignedTransaction { |
| 113 | + xdr: queued.unsigned_xdr, |
| 114 | + network_passphrase: network_passphrase.to_string(), |
| 115 | + fee: 100, |
| 116 | + sequence: 0, |
| 117 | + }; |
| 118 | + sign_transaction(&unsigned, secret_key_hex) |
| 119 | + }) |
| 120 | + .collect() |
| 121 | +} |
0 commit comments