1
2use std::ops::Deref;
3use std::sync::Arc;
4
5use bark::actions::WalletActionId;
6use bitcoin::{Amount, OutPoint, SignedAmount, Transaction, Txid};
7use bitcoin::secp256k1::PublicKey;
8#[cfg(feature = "utoipa")]
9use utoipa::ToSchema;
10
11use ark::{Vtxo, VtxoId};
12use ark::vtxo::{Full, VtxoPolicyKind};
13use bark::movement::MovementId;
14use bark::vtxo::VtxoState;
15use bitcoin_ext::{BlockDelta, BlockHeight};
16
17#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
21#[cfg_attr(feature = "utoipa", derive(ToSchema))]
22pub struct BlockRef {
23 pub height: BlockHeight,
24 #[cfg_attr(feature = "utoipa", schema(value_type = String))]
25 pub hash: bitcoin::BlockHash,
26}
27
28impl From<bitcoin_ext::BlockRef> for BlockRef {
29 fn from(v: bitcoin_ext::BlockRef) -> Self {
30 BlockRef {
31 height: v.height,
32 hash: v.hash,
33 }
34 }
35}
36
37impl From<BlockRef> for bitcoin_ext::BlockRef {
38 fn from(v: BlockRef) -> Self {
39 bitcoin_ext::BlockRef {
40 height: v.height,
41 hash: v.hash,
42 }
43 }
44}
45
46#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
57#[cfg_attr(feature = "utoipa", derive(ToSchema))]
58pub struct UtxoInfo {
59 #[cfg_attr(feature = "utoipa", schema(value_type = String))]
61 pub outpoint: OutPoint,
62 #[serde(rename = "amount_sat", with = "bitcoin::amount::serde::as_sat")]
64 #[cfg_attr(feature = "utoipa", schema(value_type = u64))]
65 pub amount: Amount,
66 pub confirmation_height: Option<u32>,
69}
70
71impl From<bark::UtxoInfo> for UtxoInfo {
72 fn from(v: bark::UtxoInfo) -> Self {
73 UtxoInfo {
74 outpoint: v.outpoint,
75 amount: v.amount,
76 confirmation_height: v.confirmation_height,
77 }
78 }
79}
80
81impl From<bark::onchain::Utxo> for UtxoInfo {
82
83 fn from(v: bark::onchain::Utxo) -> Self {
84 match v {
85 bark::onchain::Utxo::Local(o) => UtxoInfo {
86 outpoint: o.outpoint,
87 amount: o.amount,
88 confirmation_height: o.confirmation_height,
89 },
90 bark::onchain::Utxo::Exit(e) => UtxoInfo {
91 outpoint: e.vtxo.point(),
92 amount: e.vtxo.amount(),
93 confirmation_height: Some(e.height),
94 },
95 }
96 }
97}
98
99#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
104#[cfg_attr(feature = "utoipa", derive(ToSchema))]
105pub struct VtxoInfo {
106 #[cfg_attr(feature = "utoipa", schema(value_type = String))]
108 pub id: VtxoId,
109 #[serde(rename = "amount_sat", with = "bitcoin::amount::serde::as_sat")]
111 #[cfg_attr(feature = "utoipa", schema(value_type = u64))]
112 pub amount: Amount,
113 #[cfg_attr(feature = "utoipa", schema(value_type = String))]
115 pub policy_type: VtxoPolicyKind,
116 #[cfg_attr(feature = "utoipa", schema(value_type = String))]
119 pub user_pubkey: PublicKey,
120 #[cfg_attr(feature = "utoipa", schema(value_type = String))]
123 pub server_pubkey: PublicKey,
124 pub expiry_height: BlockHeight,
128 pub exit_delta: BlockDelta,
131 #[cfg_attr(feature = "utoipa", schema(value_type = String))]
135 pub chain_anchor: OutPoint,
136 pub exit_depth: Option<u16>,
140}
141
142impl<'a> From<&'a Vtxo<Full>> for VtxoInfo {
143 fn from(v: &'a Vtxo<Full>) -> VtxoInfo {
144 VtxoInfo {
145 id: v.id(),
146 amount: v.amount(),
147 policy_type: v.policy().policy_type(),
148 user_pubkey: v.user_pubkey(),
149 server_pubkey: v.server_pubkey(),
150 expiry_height: v.expiry_height(),
151 exit_delta: v.exit_delta(),
152 chain_anchor: v.chain_anchor(),
153 exit_depth: Some(v.exit_depth()),
154 }
155 }
156}
157
158impl From<Vtxo<Full>> for VtxoInfo {
159 fn from(v: Vtxo<Full>) -> VtxoInfo {
160 VtxoInfo::from(&v)
161 }
162}
163
164#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
166#[cfg_attr(feature = "utoipa", derive(ToSchema))]
167pub struct WalletVtxoInfo {
168 #[serde(flatten)]
170 pub vtxo: VtxoInfo,
171 pub state: VtxoStateInfo,
173}
174
175impl<'a> From<&'a bark::WalletVtxo> for WalletVtxoInfo {
176 fn from(v: &'a bark::WalletVtxo) -> Self {
177 WalletVtxoInfo {
178 vtxo: VtxoInfo {
179 id: v.id(),
180 amount: v.amount(),
181 policy_type: v.policy().policy_type(),
182 user_pubkey: v.user_pubkey(),
183 server_pubkey: v.server_pubkey(),
184 expiry_height: v.expiry_height(),
185 exit_delta: v.exit_delta(),
186 chain_anchor: v.chain_anchor(),
187 exit_depth: Some(v.exit_depth),
188 },
189 state: VtxoStateInfo::from(&v.state),
190 }
191 }
192}
193
194#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
199#[cfg_attr(feature = "utoipa", derive(ToSchema))]
200pub struct EncodedVtxo(pub String);
201
202impl Deref for WalletVtxoInfo {
203 type Target = VtxoInfo;
204
205 fn deref(&self) -> &Self::Target {
206 &self.vtxo
207 }
208}
209
210#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
212#[cfg_attr(feature = "utoipa", derive(ToSchema))]
213#[serde(tag = "type", rename_all = "kebab-case")]
214pub enum VtxoStateInfo {
215 Spendable,
217 Spent,
219 Exited,
222 Locked {
225 #[serde(skip_serializing_if = "Option::is_none")]
227 #[cfg_attr(feature = "utoipa", schema(value_type = u32))]
228 movement_id: Option<MovementId>,
229 #[serde(skip_serializing_if = "Option::is_none")]
231 #[cfg_attr(feature = "utoipa", schema(value_type = String))]
232 action_id: Option<WalletActionId>,
233 },
234}
235
236impl<'a> From<&'a VtxoState> for VtxoStateInfo {
237 fn from(state: &'a VtxoState) -> Self {
238 match state {
239 VtxoState::Spendable => VtxoStateInfo::Spendable,
240 VtxoState::Spent => VtxoStateInfo::Spent,
241 VtxoState::Exited => VtxoStateInfo::Exited,
242 VtxoState::Locked { holder } => {
243 match holder {
244 Some(bark::vtxo::VtxoLockHolder::Movement { id }) => {
245 VtxoStateInfo::Locked { movement_id: Some(*id), action_id: None }
246 },
247 Some(bark::vtxo::VtxoLockHolder::Action { id }) => {
248 VtxoStateInfo::Locked { movement_id: None, action_id: Some(id.clone()) }
249 },
250 None => VtxoStateInfo::Locked { movement_id: None, action_id: None },
251 }
252 },
253 }
254 }
255}
256
257#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
260#[cfg_attr(feature = "utoipa", derive(ToSchema))]
261pub struct TransactionInfo {
262 #[cfg_attr(feature = "utoipa", schema(value_type = String))]
263 pub txid: Txid,
264 #[serde(with = "bitcoin::consensus::serde::With::<bitcoin::consensus::serde::Hex>")]
265 #[cfg_attr(feature = "utoipa", schema(value_type = String))]
266 pub tx: Transaction,
267}
268
269impl From<bark::exit::TransactionInfo> for TransactionInfo {
270 fn from(v: bark::exit::TransactionInfo) -> Self {
271 TransactionInfo { txid: v.txid, tx: v.tx }
272 }
273}
274
275impl From<Transaction> for TransactionInfo {
276 fn from(v: Transaction) -> Self {
277 TransactionInfo { txid: v.compute_txid(), tx: v }
278 }
279}
280
281impl From<Arc<Transaction>> for TransactionInfo {
282 fn from(v: Arc<Transaction>) -> Self {
283 TransactionInfo { txid: v.compute_txid(), tx: (*v).clone() }
284 }
285}
286
287#[derive(Clone, Debug, Deserialize, Serialize)]
292#[cfg_attr(feature = "utoipa", derive(ToSchema))]
293pub struct WalletTxInfo {
294 #[cfg_attr(feature = "utoipa", schema(value_type = String))]
295 pub txid: Txid,
296 #[serde(with = "bitcoin::consensus::serde::With::<bitcoin::consensus::serde::Hex>")]
297 #[cfg_attr(feature = "utoipa", schema(value_type = String))]
298 pub tx: Transaction,
299 #[serde(rename = "onchain_fee_sat", default, with = "bitcoin::amount::serde::as_sat::opt", skip_serializing_if = "Option::is_none")]
303 #[cfg_attr(feature = "utoipa", schema(value_type = Option<u64>))]
304 pub onchain_fees: Option<Amount>,
305 #[serde(rename = "balance_change_sat", with = "bitcoin::amount::serde::as_sat")]
308 #[cfg_attr(feature = "utoipa", schema(value_type = i64))]
309 pub balance_change: SignedAmount,
310 pub confirmation: Option<BlockRef>,
312 pub is_cpfp: bool,
316}
317
318impl From<bark::onchain::WalletTxInfo> for WalletTxInfo {
319 fn from(v: bark::onchain::WalletTxInfo) -> Self {
320 WalletTxInfo {
321 txid: v.txid,
322 tx: (*v.tx).clone(),
323 onchain_fees: v.onchain_fees,
324 balance_change: v.balance_change,
325 confirmation: v.confirmation.map(Into::into),
326 is_cpfp: v.is_cpfp,
327 }
328 }
329}