1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
include!(concat!(env!("OUT_DIR"), "/notifications.rs"));

use crate::{Error, MergeOutcome, ProtoBinding, Result};
use sos_sdk::{commit::CommitHash, signer::ecdsa::Address};

/// Notification sent by the server when changes were made.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChangeNotification {
    /// Account owner address.
    address: Address,
    /// Connection identifier that made the change.
    connection_id: String,
    /// Root commit for the entire account.
    root: CommitHash,
    /// Merge outcome.
    outcome: MergeOutcome,
}

impl ChangeNotification {
    /// Create a new change notification.
    pub fn new(
        address: &Address,
        connection_id: String,
        root: CommitHash,
        outcome: MergeOutcome,
    ) -> Self {
        Self {
            address: *address,
            connection_id,
            root,
            outcome,
        }
    }

    /// Address of the account owner.
    pub fn address(&self) -> &Address {
        &self.address
    }

    /// Connection identifier.
    pub fn connection_id(&self) -> &str {
        &self.connection_id
    }

    /// Account root commit hash.
    pub fn root(&self) -> &CommitHash {
        &self.root
    }

    /// Merge outcome.
    pub fn outcome(&self) -> &MergeOutcome {
        &self.outcome
    }
}

impl ProtoBinding for ChangeNotification {
    type Inner = WireChangeNotification;
}

impl TryFrom<WireChangeNotification> for ChangeNotification {
    type Error = Error;

    fn try_from(value: WireChangeNotification) -> Result<Self> {
        let address: [u8; 20] = value.address.as_slice().try_into()?;
        Ok(Self {
            address: address.into(),
            connection_id: value.connection_id,
            root: value.root.unwrap().try_into()?,
            outcome: value.outcome.unwrap().try_into()?,
        })
    }
}

impl From<ChangeNotification> for WireChangeNotification {
    fn from(value: ChangeNotification) -> WireChangeNotification {
        Self {
            address: value.address().as_ref().to_vec(),
            connection_id: value.connection_id,
            root: Some(value.root.into()),
            outcome: Some(value.outcome.into()),
        }
    }
}