bark_json/
primitives.rs

1
2use std::ops::Deref;
3
4use bitcoin::{Amount, OutPoint};
5use bitcoin::secp256k1::PublicKey;
6#[cfg(feature = "utoipa")]
7use utoipa::ToSchema;
8
9use ark::{Vtxo, VtxoId};
10use ark::vtxo::VtxoPolicyKind;
11use bitcoin_ext::{BlockDelta, BlockHeight};
12
13/// Struct representing information about an Unspent Transaction Output (UTXO).
14///
15/// This structure provides details about a UTXO, which includes the outpoint (transaction ID and
16/// index), the associated amount in satoshis, and the block height at which the transaction was
17/// confirmed (if available).
18///
19/// # Serde Behavior
20///
21/// * The `amount` field is serialized and deserialized with a custom function from the `bitcoin`
22///   crate that ensures the value is interpreted as satoshis with the name `amount_sat`.
23#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
24#[cfg_attr(feature = "utoipa", derive(ToSchema))]
25pub struct UtxoInfo {
26	/// Contains the reference to the specific transaction output via transaction ID and index.
27	#[cfg_attr(feature = "utoipa", schema(value_type = String))]
28	pub outpoint: OutPoint,
29	/// The value of the UTXO in satoshis.
30	#[serde(rename = "amount_sat", with = "bitcoin::amount::serde::as_sat")]
31	#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
32	pub amount: Amount,
33	/// An optional field that specifies the block height at which the transaction was confirmed. If
34	/// the transaction is unconfirmed, this value will be `None`.
35	pub confirmation_height: Option<u32>,
36}
37
38impl From<bark::UtxoInfo> for UtxoInfo {
39	fn from(v: bark::UtxoInfo) -> Self {
40		UtxoInfo {
41			outpoint: v.outpoint,
42			amount: v.amount,
43			confirmation_height: v.confirmation_height,
44		}
45	}
46}
47
48impl From<bark::onchain::Utxo> for UtxoInfo {
49
50	fn from(v: bark::onchain::Utxo) -> Self {
51		match v {
52			bark::onchain::Utxo::Local(o) => UtxoInfo {
53				outpoint: o.outpoint,
54				amount: o.amount,
55				confirmation_height: o.confirmation_height,
56			},
57			bark::onchain::Utxo::Exit(e) => UtxoInfo {
58				outpoint: e.vtxo.point(),
59				amount: e.vtxo.amount(),
60				confirmation_height: Some(e.height),
61			},
62		}
63	}
64}
65
66/// Struct representing information about a VTXO.
67#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
68#[cfg_attr(feature = "utoipa", derive(ToSchema))]
69pub struct VtxoInfo {
70	#[cfg_attr(feature = "utoipa", schema(value_type = String))]
71	pub id: VtxoId,
72	#[serde(rename = "amount_sat", with = "bitcoin::amount::serde::as_sat")]
73	#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
74	pub amount: Amount,
75	#[cfg_attr(feature = "utoipa", schema(value_type = String))]
76	pub policy_type: VtxoPolicyKind,
77	#[cfg_attr(feature = "utoipa", schema(value_type = String))]
78	pub user_pubkey: PublicKey,
79	#[cfg_attr(feature = "utoipa", schema(value_type = String))]
80	pub server_pubkey: PublicKey,
81	pub expiry_height: BlockHeight,
82	pub exit_delta: BlockDelta,
83	#[cfg_attr(feature = "utoipa", schema(value_type = String))]
84	pub chain_anchor: OutPoint,
85	pub exit_depth: u16,
86	pub arkoor_depth: u16,
87}
88
89impl<'a> From<&'a Vtxo> for VtxoInfo {
90	fn from(v: &'a Vtxo) -> VtxoInfo {
91		VtxoInfo {
92			id: v.id(),
93			amount: v.amount(),
94			policy_type: v.policy().policy_type(),
95			user_pubkey: v.user_pubkey(),
96			server_pubkey: v.server_pubkey(),
97			expiry_height: v.expiry_height(),
98			exit_delta: v.exit_delta(),
99			chain_anchor: v.chain_anchor(),
100			exit_depth: v.exit_depth(),
101			arkoor_depth: v.arkoor_depth(),
102		}
103	}
104}
105
106impl From<Vtxo> for VtxoInfo {
107	fn from(v: Vtxo) -> VtxoInfo {
108		VtxoInfo::from(&v)
109	}
110}
111
112/// Same as [VtxoInfo], but with the current VTXO state.
113#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
114#[cfg_attr(feature = "utoipa", derive(ToSchema))]
115pub struct WalletVtxoInfo {
116	#[serde(flatten)]
117	pub vtxo: VtxoInfo,
118	pub state: String,
119}
120
121impl From<bark::WalletVtxo> for WalletVtxoInfo {
122	fn from(v: bark::WalletVtxo) -> Self {
123		WalletVtxoInfo {
124			vtxo: v.vtxo.into(),
125			state: v.state.kind().as_str().to_string(),
126		}
127	}
128}
129
130impl Deref for WalletVtxoInfo {
131	type Target = VtxoInfo;
132
133	fn deref(&self) -> &Self::Target {
134		&self.vtxo
135	}
136}
137
138#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
139#[cfg_attr(feature = "utoipa", derive(ToSchema))]
140pub struct RecipientInfo {
141	/// Can either be a publickey, spk or a bolt11 invoice
142	pub recipient: String,
143	#[serde(rename = "amount_sat", with = "bitcoin::amount::serde::as_sat")]
144	#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
145	pub amount: Amount
146}