catbuffer_rust/
embedded_transaction_builder.rs

1/*
2 * // Copyright (c) 2016-2019, Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp.
3 * // Copyright (c) 2020-present, Jaguar0625, gimre, BloodyRookie.
4 * // All rights reserved.
5 * //
6 * // This file is part of Catapult.
7 * //
8 * // Catapult is free software: you can redistribute it and/or modify
9 * // it under the terms of the GNU Lesser General Public License as published by
10 * // the Free Software Foundation, either version 3 of the License, or
11 * // (at your option) any later version.
12 * //
13 * // Catapult is distributed in the hope that it will be useful,
14 * // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * // GNU Lesser General Public License for more details.
17 * //
18 * // You should have received a copy of the GNU Lesser General Public License
19 * // along with Catapult. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22use super::entity_type_dto::*;
23use super::generator_utils::*;
24use super::key_dto::*;
25use super::network_type_dto::*;
26
27/// Binary layout for an embedded transaction.
28#[derive(Debug, Clone)]
29pub struct EmbeddedTransactionBuilder {
30    /// Entity signer's public key.
31    pub signer_public_key: KeyDto,
32    /// Entity version.
33    pub version: u8,
34    /// Entity network.
35    pub network: NetworkTypeDto,
36    /// Entity type.
37    pub _type: EntityTypeDto,
38}
39
40impl EmbeddedTransactionBuilder {
41    /// Creates an instance of EmbeddedTransactionBuilder from binary payload.
42    /// payload: Byte payload to use to serialize the object.
43    /// # Returns
44    /// A EmbeddedTransactionBuilder.
45    pub fn from_binary(payload: &[u8]) -> Self {
46        let mut _bytes = payload.to_vec();
47        _bytes = (&_bytes[4..]).to_vec();
48        let buf = fixed_bytes::<4>(&_bytes);
49        let _ = u32::from_le_bytes(buf); // kind:SIMPLE
50        _bytes = (&_bytes[4..]).to_vec();
51        let signer_public_key = KeyDto::from_binary(&_bytes); // kind:CUSTOM1
52        _bytes = _bytes[signer_public_key.get_size()..].to_vec();
53        let buf = fixed_bytes::<4>(&_bytes);
54        let _ = u32::from_le_bytes(buf); // kind:SIMPLE
55        _bytes = (&_bytes[4..]).to_vec();
56        let buf = fixed_bytes::<1>(&_bytes);
57        let version = u8::from_le_bytes(buf); // kind:SIMPLE
58        _bytes = (&_bytes[1..]).to_vec();
59        let network = NetworkTypeDto::from_binary(&_bytes); // kind:CUSTOM2
60        _bytes = (&_bytes[network.get_size()..]).to_vec();
61        let _type = EntityTypeDto::from_binary(&_bytes); // kind:CUSTOM2
62        _bytes = (&_bytes[_type.get_size()..]).to_vec();
63        // create object and call. // EmbeddedTransaction
64        EmbeddedTransactionBuilder { signer_public_key, version, network, _type }
65    }
66
67    /// Gets the size of the type.
68    ///
69    /// Returns:
70    /// A size in bytes.
71    pub fn get_size(&self) -> usize {
72        let mut size = 0;
73        size += 4;  // size;
74        size += 4;  // embedded_transaction_header__reserved1;
75        size += self.signer_public_key.get_size(); // signer_public_key_size;
76        size += 4;  // entity_body__reserved1;
77        size += 1;  // version;
78        size += self.network.get_size(); // network_size;
79        size += self._type.get_size(); // type_size;
80        size
81    }
82
83    /// Serializes self to bytes.
84    ///
85    /// # Returns
86    /// A Serialized bytes.
87    pub fn serializer(&self) -> Vec<u8> {
88        let mut buf: Vec<u8> = vec![];
89        // Ignored serialization: size AttributeKind.SIMPLE
90        buf.append(&mut [0u8; 4].to_vec()); // kind:SIMPLE and is_reserved
91        buf.append(&mut self.signer_public_key.serializer()); // kind:CUSTOM
92        buf.append(&mut [0u8; 4].to_vec()); // kind:SIMPLE and is_reserved
93        buf.append(&mut self.version.to_le_bytes().to_vec()); // serial_kind:SIMPLE
94        buf.append(&mut self.network.serializer()); // kind:CUSTOM
95        buf.append(&mut self._type.serializer()); // kind:CUSTOM
96        buf
97    }
98}
99