use crate::{
types::{AssetId, SecretKey},
Address, LwkError, Network, Script, TxOutSecrets,
};
use std::sync::Arc;
#[derive(uniffi::Object, Debug)]
pub struct TxOut {
inner: elements::TxOut,
}
impl From<elements::TxOut> for TxOut {
fn from(inner: elements::TxOut) -> Self {
Self { inner }
}
}
#[uniffi::export]
impl TxOut {
pub fn script_pubkey(&self) -> Arc<Script> {
let spk = self.inner.script_pubkey.clone().into();
Arc::new(spk)
}
pub fn is_fee(&self) -> bool {
self.inner.is_fee()
}
pub fn is_partially_blinded(&self) -> bool {
self.inner.is_partially_blinded()
}
pub fn asset(&self) -> Option<AssetId> {
self.inner.asset.explicit().map(Into::into)
}
pub fn value(&self) -> Option<u64> {
self.inner.value.explicit()
}
pub fn unconfidential_address(&self, network: &Network) -> Option<Arc<Address>> {
elements::Address::from_script(
&self.inner.script_pubkey,
None,
network.inner.address_params(),
)
.map(|a| Arc::new(a.into()))
}
pub fn unblind(&self, secret_key: &SecretKey) -> Result<TxOutSecrets, LwkError> {
Ok(self
.inner
.unblind(&lwk_wollet::EC, secret_key.into())
.map(Into::into)?)
}
}