Skip to content

Commit 917b28c

Browse files
committed
Add list_transactions to ffi
also added a couple easy functions
1 parent d81df18 commit 917b28c

5 files changed

Lines changed: 215 additions & 30 deletions

File tree

orange-sdk/src/ffi/bitcoin_payment_instructions.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{impl_from_core_type, impl_into_core_type};
22
use bitcoin_payment_instructions::amount::Amount as BPIAmount;
3-
use std::sync::Arc;
43
use std::fmt::Display;
4+
use std::sync::Arc;
55

66
#[derive(Debug, uniffi::Error)]
77
pub enum AmountError {
@@ -10,12 +10,16 @@ pub enum AmountError {
1010
}
1111

1212
impl Display for AmountError {
13-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14-
match self {
15-
AmountError::FractionalAmountOfSats(sats) => write!(f, "Not exactly a whole number of sats: {}", sats),
16-
AmountError::MoreThanTwentyOneMillionBitcoin(sats) => write!(f, "More than 21 million bitcoin: {}", sats),
17-
}
18-
}
13+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14+
match self {
15+
AmountError::FractionalAmountOfSats(sats) => {
16+
write!(f, "Not exactly a whole number of sats: {}", sats)
17+
},
18+
AmountError::MoreThanTwentyOneMillionBitcoin(sats) => {
19+
write!(f, "More than 21 million bitcoin: {}", sats)
20+
},
21+
}
22+
}
1923
}
2024

2125
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, uniffi::Object)]

orange-sdk/src/ffi/orange/error.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ pub enum ConfigError {
1010
}
1111

1212
impl Display for ConfigError {
13-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14-
match self {
15-
ConfigError::InvalidEntropySize(size) => write!(f, "Invalid entropy size: {size}. Entropy must be 64 bytes."),
16-
ConfigError::InvalidLspAddress(address) => write!(f, "Invalid LSP address: {address}"),
17-
ConfigError::InvalidLspNodeId(node_id) => write!(f, "Invalid LSP node ID: {node_id}"),
18-
}
19-
}
13+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14+
match self {
15+
ConfigError::InvalidEntropySize(size) => {
16+
write!(f, "Invalid entropy size: {size}. Entropy must be 64 bytes.")
17+
},
18+
ConfigError::InvalidLspAddress(address) => write!(f, "Invalid LSP address: {address}"),
19+
ConfigError::InvalidLspNodeId(node_id) => write!(f, "Invalid LSP node ID: {node_id}"),
20+
}
21+
}
2022
}
2123

2224
/// Represents possible failures during wallet initialization.
@@ -34,15 +36,15 @@ pub enum InitFailure {
3436
}
3537

3638
impl Display for InitFailure {
37-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38-
match self {
39-
InitFailure::IoError(e) => write!(f, "I/O error: {e}"),
40-
InitFailure::ConfigError(e) => write!(f, "Config error: {e}"),
41-
InitFailure::LdkNodeBuildFailure(e) => write!(f, "Failed to build the LDK node: {e}"),
42-
InitFailure::LdkNodeStartFailure(e) => write!(f, "Failed to start the LDK node: {e}"),
43-
InitFailure::TrustedFailure => write!(f, "Failed to create the trusted wallet"),
44-
}
45-
}
39+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40+
match self {
41+
InitFailure::IoError(e) => write!(f, "I/O error: {e}"),
42+
InitFailure::ConfigError(e) => write!(f, "Config error: {e}"),
43+
InitFailure::LdkNodeBuildFailure(e) => write!(f, "Failed to build the LDK node: {e}"),
44+
InitFailure::LdkNodeStartFailure(e) => write!(f, "Failed to start the LDK node: {e}"),
45+
InitFailure::TrustedFailure => write!(f, "Failed to create the trusted wallet"),
46+
}
47+
}
4648
}
4749

4850
impl From<std::io::Error> for InitFailure {
@@ -82,12 +84,12 @@ pub enum WalletError {
8284
}
8385

8486
impl Display for WalletError {
85-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86-
match self {
87-
WalletError::LdkNodeFailure(e) => write!(f, "Failure from LDK node: {e}"),
88-
WalletError::TrustedFailure => write!(f, "Failure from trusted wallet"),
89-
}
90-
}
87+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88+
match self {
89+
WalletError::LdkNodeFailure(e) => write!(f, "Failure from LDK node: {e}"),
90+
WalletError::TrustedFailure => write!(f, "Failure from trusted wallet"),
91+
}
92+
}
9193
}
9294

9395
impl From<OrangeWalletError> for WalletError {

orange-sdk/src/ffi/orange/mod.rs

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,154 @@
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+
16
pub mod config;
27
pub mod error;
38
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+
}

orange-sdk/src/ffi/orange/wallet.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::Balances as OrangeBalances;
1+
use crate::{Balances as OrangeBalances, store};
22

33
use crate::Wallet as OrangeWallet;
44
use crate::WalletConfig as OrangeWalletConfig;
@@ -75,4 +75,29 @@ impl Wallet {
7575
let balance = self.inner.get_balance().await?;
7676
Ok(balance.into())
7777
}
78+
79+
pub async fn is_connected_to_lsp(&self) -> bool {
80+
self.inner.is_connected_to_lsp()
81+
}
82+
83+
/// Sets whether the wallet should automatically rebalance from trusted/onchain to lightning.
84+
pub fn set_rebalance_enabled(&self, value: bool) {
85+
self.inner.set_rebalance_enabled(value)
86+
}
87+
88+
/// Whether the wallet should automatically rebalance from trusted/onchain to lightning.
89+
pub fn get_rebalance_enabled(&self) -> bool {
90+
self.inner.get_rebalance_enabled()
91+
}
92+
93+
pub async fn list_transactions(
94+
&self,
95+
) -> Result<Vec<crate::ffi::orange::Transaction>, WalletError> {
96+
let transactions = self.inner.list_transactions().await?;
97+
Ok(transactions.into_iter().map(|tx| tx.into()).collect())
98+
}
99+
100+
pub async fn stop(&self) {
101+
self.inner.stop().await
102+
}
78103
}

orange-sdk/src/store.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ impl From<Transaction> for StoreTransaction {
125125
}
126126
}
127127

128+
/// A PaymentId is a unique identifier for a payment. It can be either a Lightning payment or a
129+
/// Trusted payment. It is used to track the state of a payment and to provide information about
130+
/// the payment to the user.
128131
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
129132
pub enum PaymentId {
130133
Lightning([u8; 32]),

0 commit comments

Comments
 (0)