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
#![cfg(feature = "std")]

use fuel_core_chain_config::MessageConfig;
use fuel_core_client::client::schema::message::{
    Message as ClientMessage, MessageStatus as ClientMessageStatus,
};
use fuel_tx::{Input, MessageId};

use crate::bech32::Bech32Address;

#[derive(Debug, Clone)]
pub struct Message {
    pub amount: u64,
    pub sender: Bech32Address,
    pub recipient: Bech32Address,
    pub nonce: u64,
    pub data: Vec<u8>,
    pub da_height: u64,
    pub status: MessageStatus,
}

#[derive(Debug, Clone)]
pub enum MessageStatus {
    Unspent,
    Spent,
}

impl Message {
    pub fn message_id(&self) -> MessageId {
        Input::compute_message_id(
            &(&self.sender).into(),
            &(&self.recipient).into(),
            self.nonce,
            self.amount,
            &self.data,
        )
    }
}

impl From<ClientMessage> for Message {
    fn from(message: ClientMessage) -> Self {
        Self {
            amount: message.amount.0,
            sender: message.sender.0 .0.into(),
            recipient: message.recipient.0 .0.into(),
            nonce: message.nonce.0,
            data: message.data.0 .0,
            da_height: message.da_height.0,
            status: MessageStatus::from(message.status),
        }
    }
}

impl From<ClientMessageStatus> for MessageStatus {
    fn from(status: ClientMessageStatus) -> Self {
        match status {
            ClientMessageStatus::Unspent => MessageStatus::Unspent,
            ClientMessageStatus::Spent => MessageStatus::Spent,
        }
    }
}

impl From<Message> for MessageConfig {
    fn from(message: Message) -> MessageConfig {
        MessageConfig {
            sender: message.sender.into(),
            recipient: message.recipient.into(),
            nonce: message.nonce,
            amount: message.amount,
            data: message.data,
            da_height: message.da_height.into(),
        }
    }
}