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
use ex3_canister_types::wallet_identifier::WalletIdentifier;
use ex3_node_error::OtherError;
use serde_bytes::ByteBuf;

use crate::{PayloadDecoder, Result};

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

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

        if hexes.len() != 1 {
            return Err(OtherError::new("Invalid payload").into());
        }
        let pub_key_bytes = hex::decode(&hexes[0][2..]);

        let pub_key = pub_key_bytes.unwrap();

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

        let wallet_identifier = WalletIdentifier(pub_key);
        Ok(wallet_identifier)
    }
}