lwk/
update.rs

1use crate::LwkError;
2
3/// Wrapper over [`lwk_wollet::Update`]
4#[derive(uniffi::Object, Clone, PartialEq, Eq, Debug)]
5pub struct Update {
6    inner: lwk_wollet::Update,
7}
8
9impl From<lwk_wollet::Update> for Update {
10    fn from(inner: lwk_wollet::Update) -> Self {
11        Self { inner }
12    }
13}
14
15impl From<Update> for lwk_wollet::Update {
16    fn from(value: Update) -> Self {
17        value.inner
18    }
19}
20
21impl From<&Update> for lwk_wollet::Update {
22    fn from(value: &Update) -> Self {
23        value.inner.clone()
24    }
25}
26
27impl AsRef<lwk_wollet::Update> for Update {
28    fn as_ref(&self) -> &lwk_wollet::Update {
29        &self.inner
30    }
31}
32
33#[uniffi::export]
34impl Update {
35    /// Creates an `Update` from a byte array created with `serialize()`
36    #[uniffi::constructor]
37    pub fn new(bytes: &[u8]) -> Result<Update, LwkError> {
38        Ok(lwk_wollet::Update::deserialize(bytes)?.into())
39    }
40
41    /// Serialize an `Update` to a byte array, can be deserialized back with `new()`
42    pub fn serialize(&self) -> Result<Vec<u8>, LwkError> {
43        Ok(self.inner.serialize()?)
44    }
45
46    /// Whether the update only changes the tip (does not affect transactions)
47    pub fn only_tip(&self) -> bool {
48        self.inner.only_tip()
49    }
50}
51
52#[cfg(test)]
53mod tests {
54
55    #[test]
56    fn update() {
57        let bytes = lwk_test_util::update_test_vector_bytes();
58        let update = crate::Update::new(&bytes).unwrap();
59        let back = update.serialize().unwrap();
60        let update_back = crate::Update::new(&back).unwrap(); // now we save the version, thus we serialize back exactly the same
61
62        assert_eq!(bytes.len(), back.len());
63        assert_eq!(update, update_back);
64
65        let update_v1 = {
66            let mut update = update.clone();
67            update.inner.version = 1;
68            update
69        };
70        let back_v1 = update_v1.serialize().unwrap();
71        let update_back_v1: crate::Update = crate::Update::new(&back_v1).unwrap();
72        assert_eq!(update_v1, update_back_v1);
73
74        assert_ne!(bytes, back_v1);
75        assert_eq!(bytes.len() + 8, back_v1.len()); // the new version serialize the wallet status
76
77        let update_v2 = {
78            let mut update = update.clone();
79            update.inner.version = 2;
80            update
81        };
82        let err = update_v2.serialize().unwrap_err();
83        assert!(err
84            .to_string()
85            .contains("Version 2 update with missing blinding pubkey"));
86    }
87}