use std::ops::Deref;
use std::sync::Arc;
use bitcoin::{Amount, OutPoint, Transaction, Txid};
use bitcoin::secp256k1::PublicKey;
#[cfg(feature = "utoipa")]
use utoipa::ToSchema;
use ark::{Vtxo, VtxoId};
use ark::vtxo::VtxoPolicyKind;
use bark::movement::MovementId;
use bark::vtxo::VtxoState;
use bitcoin_ext::{BlockDelta, BlockHeight};
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct UtxoInfo {
#[cfg_attr(feature = "utoipa", schema(value_type = String))]
pub outpoint: OutPoint,
#[serde(rename = "amount_sat", with = "bitcoin::amount::serde::as_sat")]
#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
pub amount: Amount,
pub confirmation_height: Option<u32>,
}
impl From<bark::UtxoInfo> for UtxoInfo {
fn from(v: bark::UtxoInfo) -> Self {
UtxoInfo {
outpoint: v.outpoint,
amount: v.amount,
confirmation_height: v.confirmation_height,
}
}
}
impl From<bark::onchain::Utxo> for UtxoInfo {
fn from(v: bark::onchain::Utxo) -> Self {
match v {
bark::onchain::Utxo::Local(o) => UtxoInfo {
outpoint: o.outpoint,
amount: o.amount,
confirmation_height: o.confirmation_height,
},
bark::onchain::Utxo::Exit(e) => UtxoInfo {
outpoint: e.vtxo.point(),
amount: e.vtxo.amount(),
confirmation_height: Some(e.height),
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct VtxoInfo {
#[cfg_attr(feature = "utoipa", schema(value_type = String))]
pub id: VtxoId,
#[serde(rename = "amount_sat", with = "bitcoin::amount::serde::as_sat")]
#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
pub amount: Amount,
#[cfg_attr(feature = "utoipa", schema(value_type = String))]
pub policy_type: VtxoPolicyKind,
#[cfg_attr(feature = "utoipa", schema(value_type = String))]
pub user_pubkey: PublicKey,
#[cfg_attr(feature = "utoipa", schema(value_type = String))]
pub server_pubkey: PublicKey,
pub expiry_height: BlockHeight,
pub exit_delta: BlockDelta,
#[cfg_attr(feature = "utoipa", schema(value_type = String))]
pub chain_anchor: OutPoint,
pub exit_depth: u16,
}
impl<'a> From<&'a Vtxo> for VtxoInfo {
fn from(v: &'a Vtxo) -> VtxoInfo {
VtxoInfo {
id: v.id(),
amount: v.amount(),
policy_type: v.policy().policy_type(),
user_pubkey: v.user_pubkey(),
server_pubkey: v.server_pubkey(),
expiry_height: v.expiry_height(),
exit_delta: v.exit_delta(),
chain_anchor: v.chain_anchor(),
exit_depth: v.exit_depth(),
}
}
}
impl From<Vtxo> for VtxoInfo {
fn from(v: Vtxo) -> VtxoInfo {
VtxoInfo::from(&v)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct WalletVtxoInfo {
#[serde(flatten)]
pub vtxo: VtxoInfo,
pub state: VtxoStateInfo,
}
impl From<bark::WalletVtxo> for WalletVtxoInfo {
fn from(v: bark::WalletVtxo) -> Self {
WalletVtxoInfo {
vtxo: v.vtxo.into(),
state: v.state.into(),
}
}
}
impl Deref for WalletVtxoInfo {
type Target = VtxoInfo;
fn deref(&self) -> &Self::Target {
&self.vtxo
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum VtxoStateInfo {
Spendable,
Spent,
Locked {
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "utoipa", schema(value_type = u32))]
movement_id: Option<MovementId>,
},
}
impl From<VtxoState> for VtxoStateInfo {
fn from(state: VtxoState) -> Self {
match state {
VtxoState::Spendable => VtxoStateInfo::Spendable,
VtxoState::Spent => VtxoStateInfo::Spent,
VtxoState::Locked { movement_id } => VtxoStateInfo::Locked {
movement_id,
},
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct TransactionInfo {
#[cfg_attr(feature = "utoipa", schema(value_type = String))]
pub txid: Txid,
#[serde(with = "bitcoin::consensus::serde::With::<bitcoin::consensus::serde::Hex>")]
#[cfg_attr(feature = "utoipa", schema(value_type = String))]
pub tx: Transaction,
}
impl From<bark::exit::TransactionInfo> for TransactionInfo {
fn from(v: bark::exit::TransactionInfo) -> Self {
TransactionInfo { txid: v.txid, tx: v.tx }
}
}
impl From<Transaction> for TransactionInfo {
fn from(v: Transaction) -> Self {
TransactionInfo { txid: v.compute_txid(), tx: v }
}
}
impl From<Arc<Transaction>> for TransactionInfo {
fn from(v: Arc<Transaction>) -> Self {
TransactionInfo { txid: v.compute_txid(), tx: (*v).clone() }
}
}