1
2mod selection;
3mod signing;
4mod state;
5
6pub use self::selection::{FilterVtxos, RefreshStrategy, VtxoFilter};
7pub use self::state::{VtxoState, VtxoStateKind, WalletVtxo};
8
9use log::{debug, error, trace};
10use ark::{ProtocolEncoding, Vtxo};
11use ark::vtxo::{Full, VtxoRef};
12
13use crate::Wallet;
14use crate::movement::MovementId;
15
16impl Wallet {
17 pub async fn lock_vtxos(
25 &self,
26 vtxos: impl IntoIterator<Item = impl VtxoRef>,
27 movement_id: Option<MovementId>,
28 ) -> anyhow::Result<()> {
29 self.set_vtxo_states(
30 vtxos, &VtxoState::Locked { movement_id }, &VtxoStateKind::UNSPENT_STATES,
31 ).await
32 }
33
34 pub async fn mark_vtxos_as_spent(
43 &self,
44 vtxos: impl IntoIterator<Item = impl VtxoRef>,
45 ) -> anyhow::Result<()> {
46 const ALLOWED: &[VtxoStateKind] = &[
47 VtxoStateKind::Spendable,
48 VtxoStateKind::Locked,
49 VtxoStateKind::Spent,
50 ];
51 self.set_vtxo_states(vtxos, &VtxoState::Spent, ALLOWED).await
52 }
53
54 pub async fn set_vtxo_states(
69 &self,
70 vtxos: impl IntoIterator<Item = impl VtxoRef>,
71 state: &VtxoState,
72 mut allowed_states: &[VtxoStateKind],
73 ) -> anyhow::Result<()> {
74 if allowed_states.is_empty() {
75 allowed_states = VtxoStateKind::ALL;
76 }
77
78 let mut problematic_vtxos = Vec::new();
79 for vtxo in vtxos {
80 let id = vtxo.vtxo_id();
81 if let Err(e) = self.db.update_vtxo_state_checked(
82 id,
83 state.clone(),
84 allowed_states,
85 ).await {
86 error!(
87 "Failed to set {} state with allowed states {:?} for VTXO {}: {:#}",
88 state.kind(), allowed_states, id, e,
89 );
90 problematic_vtxos.push(id);
91 }
92 }
93
94 if problematic_vtxos.is_empty() {
95 Ok(())
96 } else {
97 Err(anyhow!(
98 "Failed to set {} state for {} VTXOs: {:?}",
99 state.kind(),
100 problematic_vtxos.len(),
101 problematic_vtxos
102 ))
103 }
104 }
105
106 pub async fn store_locked_vtxos<'a>(
114 &self,
115 vtxos: impl IntoIterator<Item = &'a Vtxo<Full>>,
116 movement_id: Option<MovementId>,
117 ) -> anyhow::Result<()> {
118 self.store_vtxos(vtxos, &VtxoState::Locked { movement_id }).await
119 }
120
121 pub async fn store_spendable_vtxos<'a>(
129 &self,
130 vtxos: impl IntoIterator<Item = &'a Vtxo<Full>>,
131 ) -> anyhow::Result<()> {
132 self.store_vtxos(vtxos, &VtxoState::Spendable).await
133 }
134
135 pub async fn store_spent_vtxos<'a>(
143 &self,
144 vtxos: impl IntoIterator<Item = &'a Vtxo<Full>>,
145 ) -> anyhow::Result<()> {
146 self.store_vtxos(vtxos, &VtxoState::Spent).await
147 }
148
149 pub async fn store_vtxos<'a>(
157 &self,
158 vtxos: impl IntoIterator<Item = &'a Vtxo<Full>>,
159 state: &VtxoState,
160 ) -> anyhow::Result<()> {
161 let vtxos = vtxos.into_iter().map(|v| (v, state)).collect::<Vec<_>>();
162 if let Err(e) = self.db.store_vtxos(&vtxos).await {
163 error!("An error occurred while storing {} VTXOs: {:#}", vtxos.len(), e);
164 error!("Raw VTXOs for debugging:");
165 for (vtxo, _) in vtxos {
166 error!(" - {}", vtxo.serialize_hex());
167 }
168 Err(e)
169 } else {
170 debug!("Stored {} VTXOs", vtxos.len());
171 trace!("New VTXO IDs: {:?}", vtxos.into_iter().map(|(v, _)| v.id()).collect::<Vec<_>>());
172 Ok(())
173 }
174 }
175
176 pub async fn unlock_vtxos(
187 &self,
188 vtxos: impl IntoIterator<Item = impl VtxoRef>,
189 ) -> anyhow::Result<()> {
190 self.set_vtxo_states(
191 vtxos, &VtxoState::Spendable, &[VtxoStateKind::Locked, VtxoStateKind::Spendable],
192 ).await
193 }
194}