breez_sdk_spark/chain/
mod.rs1use serde::{Deserialize, Serialize};
2use thiserror::Error;
3
4use crate::Network;
5pub mod rest_client;
6
7#[derive(Debug, Error, Clone)]
8#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
9pub enum ChainServiceError {
10 #[error("Invalid address: {0}")]
11 InvalidAddress(String),
12 #[error("Service connectivity: {0}")]
13 ServiceConnectivity(String),
14 #[error("Generic: {0}")]
15 Generic(String),
16}
17
18impl From<breez_sdk_common::error::ServiceConnectivityError> for ChainServiceError {
19 fn from(value: breez_sdk_common::error::ServiceConnectivityError) -> Self {
20 ChainServiceError::ServiceConnectivity(value.to_string())
21 }
22}
23
24impl From<bitcoin::address::ParseError> for ChainServiceError {
25 fn from(value: bitcoin::address::ParseError) -> Self {
26 ChainServiceError::InvalidAddress(value.to_string())
27 }
28}
29
30#[cfg_attr(feature = "uniffi", uniffi::export(with_foreign))]
31#[macros::async_trait]
32pub trait BitcoinChainService: Send + Sync {
33 async fn get_address_utxos(&self, address: String) -> Result<Vec<Utxo>, ChainServiceError>;
34 async fn get_transaction_hex(&self, txid: String) -> Result<String, ChainServiceError>;
35 async fn broadcast_transaction(&self, tx: String) -> Result<(), ChainServiceError>;
36}
37
38#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
39#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
40pub struct TxStatus {
41 pub confirmed: bool,
42 pub block_height: Option<u32>,
43 pub block_time: Option<u64>,
44}
45
46#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
47#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
48pub struct Utxo {
49 pub txid: String,
50 pub vout: u32,
51 pub value: u64,
52 pub status: TxStatus,
53}
54
55impl TryFrom<Network> for bitcoin::Network {
56 type Error = ChainServiceError;
57
58 fn try_from(value: Network) -> Result<Self, Self::Error> {
59 match value {
60 Network::Mainnet => Ok(bitcoin::Network::Bitcoin),
61 Network::Regtest => Ok(bitcoin::Network::Regtest),
62 }
63 }
64}