|
| 1 | +use crate::bitcoin::hashes::Hash; |
| 2 | +use crate::ffi::bitcoin_payment_instructions::Amount; |
| 3 | +use crate::{PaymentId as OrangePaymentId, Transaction as OrangeTransaction}; |
| 4 | +use crate::{impl_from_core_type, impl_into_core_type}; |
| 5 | + |
1 | 6 | pub mod config; |
2 | 7 | pub mod error; |
3 | 8 | pub mod wallet; |
| 9 | + |
| 10 | +/// A transaction is a record of a payment made or received. It contains information about the |
| 11 | +/// transaction, such as the amount, fee, and status. It is used to track the state of a payment |
| 12 | +/// and to provide information about the payment to the user. |
| 13 | +#[derive(Debug, Clone, uniffi::Object)] |
| 14 | +pub struct Transaction(pub crate::store::Transaction); |
| 15 | + |
| 16 | +impl_from_core_type!(OrangeTransaction, Transaction); |
| 17 | +impl_into_core_type!(Transaction, OrangeTransaction); |
| 18 | + |
| 19 | +#[uniffi::export] |
| 20 | +impl Transaction { |
| 21 | + pub fn id(&self) -> PaymentId { |
| 22 | + self.0.id.into() |
| 23 | + } |
| 24 | + |
| 25 | + pub fn status(&self) -> TxStatus { |
| 26 | + self.0.status.into() |
| 27 | + } |
| 28 | + |
| 29 | + pub fn outbound(&self) -> bool { |
| 30 | + self.0.outbound |
| 31 | + } |
| 32 | + |
| 33 | + /// The amount of the payment |
| 34 | + /// |
| 35 | + /// None if the payment is not yet completed |
| 36 | + pub fn amount(&self) -> Option<Amount> { |
| 37 | + self.0.amount.map(Into::into) |
| 38 | + } |
| 39 | + |
| 40 | + /// The fee paid for the payment |
| 41 | + /// |
| 42 | + /// None if the payment is not yet completed |
| 43 | + pub fn fee(&self) -> Option<Amount> { |
| 44 | + self.0.fee.map(Into::into) |
| 45 | + } |
| 46 | + |
| 47 | + /// The timestamp of the transaction |
| 48 | + pub fn secs_since_epoch(&self) -> u64 { |
| 49 | + self.0.time_since_epoch.as_secs() |
| 50 | + } |
| 51 | + |
| 52 | + /// The type of payment being made |
| 53 | + pub fn payment_type(&self) -> PaymentType { |
| 54 | + self.0.payment_type.clone().into() |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// A PaymentId is a unique identifier for a payment. It can be either a Lightning payment or a |
| 59 | +/// Trusted payment. It is used to track the state of a payment and to provide information about |
| 60 | +/// the payment to the user. |
| 61 | +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, uniffi::Object)] |
| 62 | +pub enum PaymentId { |
| 63 | + Lightning([u8; 32]), |
| 64 | + Trusted([u8; 32]), |
| 65 | +} |
| 66 | + |
| 67 | +impl From<OrangePaymentId> for PaymentId { |
| 68 | + fn from(id: OrangePaymentId) -> Self { |
| 69 | + match id { |
| 70 | + OrangePaymentId::Lightning(hash) => Self::Lightning(hash), |
| 71 | + OrangePaymentId::Trusted(hash) => Self::Trusted(hash), |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// The status of a transaction. This is used to track the state of a transaction |
| 77 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)] |
| 78 | +pub enum TxStatus { |
| 79 | + /// A pending transaction has not yet been paid. |
| 80 | + Pending, |
| 81 | + /// A completed transaction has been paid. |
| 82 | + Completed, |
| 83 | + /// A transaction that has failed. |
| 84 | + Failed, |
| 85 | +} |
| 86 | + |
| 87 | +impl From<crate::store::TxStatus> for TxStatus { |
| 88 | + fn from(status: crate::store::TxStatus) -> Self { |
| 89 | + match status { |
| 90 | + crate::store::TxStatus::Pending => TxStatus::Pending, |
| 91 | + crate::store::TxStatus::Completed => TxStatus::Completed, |
| 92 | + crate::store::TxStatus::Failed => TxStatus::Failed, |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// The type of payment being made. This indicates whether the payment is incoming or outgoing, |
| 98 | +/// and what method is being used (Lightning, on-chain, etc.) |
| 99 | +#[derive(Debug, Clone, uniffi::Enum)] |
| 100 | +pub enum PaymentType { |
| 101 | + /// An outgoing Lightning payment paying a BOLT 12 offer. |
| 102 | + OutgoingLightningBolt12 { |
| 103 | + /// The lightning "payment preimage" which represents proof that the payment completed. |
| 104 | + /// Will be set for any completed payments. |
| 105 | + payment_preimage: Option<[u8; 32]>, |
| 106 | + }, |
| 107 | + /// An outgoing Lightning payment paying a BOLT 11 invoice. |
| 108 | + OutgoingLightningBolt11 { |
| 109 | + /// The lightning "payment preimage" which represents proof that the payment completed. |
| 110 | + /// Will be set for any completed payments. |
| 111 | + payment_preimage: Option<[u8; 32]>, |
| 112 | + }, |
| 113 | + /// An outgoing on-chain Bitcoin payment. |
| 114 | + OutgoingOnChain { |
| 115 | + /// The optional transaction ID of the on-chain payment. |
| 116 | + /// Will be set for any completed payments. |
| 117 | + txid: Option<[u8; 32]>, |
| 118 | + }, |
| 119 | + /// An incoming on-chain Bitcoin payment. |
| 120 | + IncomingOnChain { |
| 121 | + /// The optional transaction ID of the on-chain payment. |
| 122 | + /// Will be set for any completed payments. |
| 123 | + txid: Option<[u8; 32]>, |
| 124 | + }, |
| 125 | + /// An incoming Lightning payment. |
| 126 | + IncomingLightning {}, |
| 127 | + /// A payment that is internal to the trusted service. |
| 128 | + TrustedInternal {}, |
| 129 | +} |
| 130 | + |
| 131 | +impl From<crate::store::PaymentType> for PaymentType { |
| 132 | + fn from(payment_type: crate::store::PaymentType) -> Self { |
| 133 | + match payment_type { |
| 134 | + crate::store::PaymentType::OutgoingLightningBolt12 { payment_preimage } => { |
| 135 | + PaymentType::OutgoingLightningBolt12 { |
| 136 | + payment_preimage: payment_preimage.map(|p| p.0), |
| 137 | + } |
| 138 | + }, |
| 139 | + crate::store::PaymentType::OutgoingLightningBolt11 { payment_preimage } => { |
| 140 | + PaymentType::OutgoingLightningBolt11 { |
| 141 | + payment_preimage: payment_preimage.map(|p| p.0), |
| 142 | + } |
| 143 | + }, |
| 144 | + crate::store::PaymentType::OutgoingOnChain { txid } => { |
| 145 | + PaymentType::OutgoingOnChain { txid: txid.map(|t| t.to_byte_array()) } |
| 146 | + }, |
| 147 | + crate::store::PaymentType::IncomingOnChain { txid } => { |
| 148 | + PaymentType::IncomingOnChain { txid: txid.map(|t| t.to_byte_array()) } |
| 149 | + }, |
| 150 | + crate::store::PaymentType::IncomingLightning {} => PaymentType::IncomingLightning {}, |
| 151 | + crate::store::PaymentType::TrustedInternal {} => PaymentType::TrustedInternal {}, |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments