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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::result;

use ex3_canister_types::chain::Chain;
use ex3_canister_types::wallet_identifier::WalletIdentifier;
use serde_bytes::ByteBuf;

use ex3_node_error::{Error, OtherError};
use ex3_node_types::deposit::Deposit;
use ex3_node_types::transaction::{ResetMainSecretRequest, WalletRegisterRequest};
use ex3_node_types::PublicKey;

pub type Result<T> = result::Result<T, Error>;

pub struct PayloadDecoder;

impl PayloadDecoder {
    /// Decode the payload to a wallet identifier
    /// The payload should be in the following format:
    /// `{chain}|{network}|{pub_key}`
    /// where chain, network and pub_key are hex encoded
    /// Used for the following transactions:
    /// - [TransactionType::WalletRegistration]
    pub fn decode_to_wallet_identifier(payload: &ByteBuf) -> Result<WalletIdentifier> {
        let payload_str = String::from_utf8(payload.as_ref().to_vec()).expect("should success");

        let hexes = payload_str.split('|').collect::<Vec<&str>>();

        if hexes.len() != 3 {
            return Err(OtherError::new("Invalid payload").into());
        }

        let chain_bytes = hex::decode(hexes[0]);
        let network_bytes = hex::decode(hexes[1]);
        let pub_key_bytes = hex::decode(hexes[2]);

        if chain_bytes.is_err() || network_bytes.is_err() || pub_key_bytes.is_err() {
            return Err(OtherError::new("Invalid payload").into());
        }

        let chain_bytes = chain_bytes.unwrap();
        let network = network_bytes.unwrap();
        let pub_key = pub_key_bytes.unwrap();

        if chain_bytes.len() != 4 || network.len() != 1 {
            return Err(OtherError::new("Invalid payload").into());
        }

        let chain_bytes: [u8; 4] = chain_bytes.try_into().unwrap();
        let chain: Chain = u32::from_be_bytes(chain_bytes).into();

        let pub_key: ByteBuf = ByteBuf::from(pub_key);

        let wallet_identifier = WalletIdentifier {
            chain,
            network: network[0],
            pub_key,
        };
        Ok(wallet_identifier)
    }

    /// Decode the payload to a deposit
    pub fn decode_to_deposit(payload: &ByteBuf) -> Result<Deposit> {
        match Deposit::cbor_decode(payload.as_ref()) {
            Ok(deposit) => Ok(deposit),
            Err(e) => Err(e.into()),
        }
    }

    /// Decode the payload to wallet register request
    /// The payload should be in the following format:
    /// `{chain}|{network}|{pub_key}`
    pub fn decode_to_wallet_register_request(payload: &ByteBuf) -> Result<WalletRegisterRequest> {
        let payload_str = String::from_utf8(payload.as_ref().to_vec()).expect("should success");

        let hexes = payload_str.split('|').collect::<Vec<&str>>();

        if hexes.len() != 3 {
            return Err(OtherError::new("Invalid payload").into());
        }

        let chain_bytes = hex::decode(hexes[0]);
        let network_bytes = hex::decode(hexes[1]);
        let pub_key_bytes = hex::decode(hexes[2]);

        if chain_bytes.is_err() || network_bytes.is_err() || pub_key_bytes.is_err() {
            return Err(OtherError::new("Invalid payload").into());
        }

        let chain_bytes = chain_bytes.unwrap();
        let network = network_bytes.unwrap();
        let pub_key = pub_key_bytes.unwrap();

        if chain_bytes.len() != 4 || network.len() != 1 {
            return Err(OtherError::new("Invalid payload").into());
        }

        let chain_bytes: [u8; 4] = chain_bytes.try_into().unwrap();
        let chain: Chain = u32::from_be_bytes(chain_bytes).into();

        let pub_key: ByteBuf = ByteBuf::from(pub_key);

        let wallet_register_request = WalletRegisterRequest {
            chain,
            network: network[0],
            pub_key,
        };
        Ok(wallet_register_request)
    }

    /// Decode the payload to reset main secret request
    /// The payload should be in the following format:
    /// ` {data.encrypted_pri_key}|{data.l2_pub_key}`
    pub fn decode_to_reset_main_secret_request(
        payload: &ByteBuf,
    ) -> Result<ResetMainSecretRequest> {
        let payload_str = String::from_utf8(payload.as_ref().to_vec()).expect("should success");

        let hexes = payload_str.split('|').collect::<Vec<&str>>();

        if hexes.len() != 2 {
            return Err(OtherError::new("Invalid payload").into());
        }

        let encrypted_pri_key = hex::decode(hexes[0]).unwrap();
        let l2_pub_key = hex::decode(hexes[1]).unwrap();

        if encrypted_pri_key.len() != 32 || l2_pub_key.len() != 32 {
            return Err(OtherError::new("Invalid payload").into());
        }

        let encrypted_pri_key: ByteBuf = ByteBuf::from(encrypted_pri_key);
        let l2_pub_key: PublicKey = ByteBuf::from(l2_pub_key);

        let reset_main_secret_request = ResetMainSecretRequest {
            encrypted_pri_key,
            l2_pub_key,
        };
        Ok(reset_main_secret_request)
    }
}