lwk_bindings 0.2.0

Liquid Wallet Kit - Bindings for other languages
Documentation
use std::sync::{Arc, Mutex};

use lwk_wollet::BlockchainBackend;

use crate::{LwkError, Transaction, Txid, Update, Wollet};

/// Wrapper over [`lwk_wollet::EsploraClient`]
#[derive(uniffi::Object, Debug)]
pub struct EsploraClient {
    inner: Mutex<lwk_wollet::EsploraClient>,
}

#[uniffi::export]
impl EsploraClient {
    /// Construct an Esplora Client
    #[uniffi::constructor]
    pub fn new(url: &str) -> Arc<Self> {
        let client = lwk_wollet::EsploraClient::new(url);
        Arc::new(Self {
            inner: Mutex::new(client),
        })
    }

    pub fn broadcast(&self, tx: &Transaction) -> Result<Arc<Txid>, LwkError> {
        Ok(Arc::new(self.inner.lock()?.broadcast(tx.as_ref())?.into()))
    }

    pub fn full_scan(&self, wollet: &Wollet) -> Result<Option<Arc<Update>>, LwkError> {
        let wollet = wollet.inner_wollet()?;
        let update: Option<lwk_wollet::Update> = self.inner.lock()?.full_scan(&wollet)?;
        Ok(update.map(Into::into).map(Arc::new))
    }
}