catbuffer_rust/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::amount_dto::*;
23use super::entity_type_dto::*;
24use super::generator_utils::*;
25use super::key_dto::*;
26use super::network_type_dto::*;
27use super::signature_dto::*;
28use super::timestamp_dto::*;
29
30/// Binary layout for a transaction.
31#[derive(Debug, Clone)]
32pub struct TransactionBuilder {
33 /// Entity signature.
34 pub signature: SignatureDto,
35 /// Entity signer's public key.
36 pub signer_public_key: KeyDto,
37 /// Entity version.
38 pub version: u8,
39 /// Entity network.
40 pub network: NetworkTypeDto,
41 /// Entity type.
42 pub _type: EntityTypeDto,
43 /// Transaction fee.
44 pub fee: AmountDto,
45 /// Transaction deadline.
46 pub deadline: TimestampDto,
47}
48
49impl TransactionBuilder {
50 /// Creates an instance of TransactionBuilder from binary payload.
51 /// payload: Byte payload to use to serialize the object.
52 /// # Returns
53 /// A TransactionBuilder.
54 pub fn from_binary(payload: &[u8]) -> Self {
55 let mut _bytes = payload.to_vec();
56 _bytes = (&_bytes[4..]).to_vec();
57 let buf = fixed_bytes::<4>(&_bytes);
58 let _ = u32::from_le_bytes(buf); // kind:SIMPLE
59 _bytes = (&_bytes[4..]).to_vec();
60 let signature = SignatureDto::from_binary(&_bytes); // kind:CUSTOM1
61 _bytes = _bytes[signature.get_size()..].to_vec();
62 let signer_public_key = KeyDto::from_binary(&_bytes); // kind:CUSTOM1
63 _bytes = _bytes[signer_public_key.get_size()..].to_vec();
64 let buf = fixed_bytes::<4>(&_bytes);
65 let _ = u32::from_le_bytes(buf); // kind:SIMPLE
66 _bytes = (&_bytes[4..]).to_vec();
67 let buf = fixed_bytes::<1>(&_bytes);
68 let version = u8::from_le_bytes(buf); // kind:SIMPLE
69 _bytes = (&_bytes[1..]).to_vec();
70 let network = NetworkTypeDto::from_binary(&_bytes); // kind:CUSTOM2
71 _bytes = (&_bytes[network.get_size()..]).to_vec();
72 let _type = EntityTypeDto::from_binary(&_bytes); // kind:CUSTOM2
73 _bytes = (&_bytes[_type.get_size()..]).to_vec();
74 let fee = AmountDto::from_binary(&_bytes); // kind:CUSTOM1
75 _bytes = _bytes[fee.get_size()..].to_vec();
76 let deadline = TimestampDto::from_binary(&_bytes); // kind:CUSTOM1
77 _bytes = _bytes[deadline.get_size()..].to_vec();
78 // create object and call. // Transaction
79 TransactionBuilder { signature, signer_public_key, version, network, _type, fee, deadline }
80 }
81
82 /// Gets the size of the type.
83 ///
84 /// Returns:
85 /// A size in bytes.
86 pub fn get_size(&self) -> usize {
87 let mut size = 0;
88 size += 4; // size;
89 size += 4; // verifiable_entity_header__reserved1;
90 size += self.signature.get_size(); // signature_size;
91 size += self.signer_public_key.get_size(); // signer_public_key_size;
92 size += 4; // entity_body__reserved1;
93 size += 1; // version;
94 size += self.network.get_size(); // network_size;
95 size += self._type.get_size(); // type_size;
96 size += self.fee.get_size(); // fee_size;
97 size += self.deadline.get_size(); // deadline_size;
98 size
99 }
100
101 /// Serializes self to bytes.
102 ///
103 /// # Returns
104 /// A Serialized bytes.
105 pub fn serializer(&self) -> Vec<u8> {
106 let mut buf: Vec<u8> = vec![];
107 // Ignored serialization: size AttributeKind.SIMPLE
108 buf.append(&mut [0u8; 4].to_vec()); // kind:SIMPLE and is_reserved
109 buf.append(&mut self.signature.serializer()); // kind:CUSTOM
110 buf.append(&mut self.signer_public_key.serializer()); // kind:CUSTOM
111 buf.append(&mut [0u8; 4].to_vec()); // kind:SIMPLE and is_reserved
112 buf.append(&mut self.version.to_le_bytes().to_vec()); // serial_kind:SIMPLE
113 buf.append(&mut self.network.serializer()); // kind:CUSTOM
114 buf.append(&mut self._type.serializer()); // kind:CUSTOM
115 buf.append(&mut self.fee.serializer()); // kind:CUSTOM
116 buf.append(&mut self.deadline.serializer()); // kind:CUSTOM
117 buf
118 }
119}
120