Skip to main content

ark/vtxo/
raw.rs

1//! Raw VTXO representation with public fields for data migrations.
2//!
3//! Provides [`RawVtxo`], a view over a [`ServerVtxo<Full>`] with all
4//! fields exposed as `pub` so they can be mutated from outside the crate.
5
6use bitcoin::Amount;
7use bitcoin::secp256k1::PublicKey;
8use bitcoin::OutPoint;
9
10use bitcoin_ext::BlockHeight;
11use crate::encode::{ProtocolEncoding, ProtocolDecodingError};
12use crate::vtxo::{Full, Vtxo};
13use crate::vtxo::policy::ServerVtxoPolicy;
14
15type ServerVtxoFull = Vtxo<Full, ServerVtxoPolicy>;
16
17/// A VTXO with all fields public, for use in data migrations.
18///
19/// Convert from/to [`ServerVtxo<Full>`] via [`From`]/[`Into`].
20pub struct RawVtxo {
21	pub policy: ServerVtxoPolicy,
22	pub amount: Amount,
23	pub expiry_height: BlockHeight,
24	pub server_pubkey: PublicKey,
25	pub exit_delta: u16,
26	pub anchor_point: OutPoint,
27	pub genesis: Full,
28	pub point: OutPoint,
29}
30
31impl From<Vtxo<Full, ServerVtxoPolicy>> for RawVtxo {
32	fn from(v: Vtxo<Full, ServerVtxoPolicy>) -> Self {
33		RawVtxo {
34			policy: v.policy,
35			amount: v.amount,
36			expiry_height: v.expiry_height,
37			server_pubkey: v.server_pubkey,
38			exit_delta: v.exit_delta,
39			anchor_point: v.anchor_point,
40			genesis: v.genesis,
41			point: v.point,
42		}
43	}
44}
45
46impl From<RawVtxo> for Vtxo<Full, ServerVtxoPolicy> {
47	fn from(r: RawVtxo) -> Self {
48		Vtxo {
49			policy: r.policy,
50			amount: r.amount,
51			expiry_height: r.expiry_height,
52			server_pubkey: r.server_pubkey,
53			exit_delta: r.exit_delta,
54			anchor_point: r.anchor_point,
55			genesis: r.genesis,
56			point: r.point,
57		}
58	}
59}
60
61impl RawVtxo {
62	/// Deserialize from bytes encoded as a [`ServerVtxo<Full>`].
63	pub fn deserialize(bytes: &[u8]) -> Result<Self, ProtocolDecodingError> {
64		ServerVtxoFull::decode(&mut &*bytes).map(RawVtxo::from)
65	}
66
67	/// Serialize by converting back into a [`ServerVtxo<Full>`].
68	pub fn serialize(self) -> Vec<u8> {
69		let vtxo: ServerVtxoFull = self.into();
70		ProtocolEncoding::serialize(&vtxo)
71	}
72}