catbuffer_rust/hash_lock_info_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::address_dto::*;
23use super::generator_utils::*;
24use super::hash256_dto::*;
25use super::height_dto::*;
26use super::lock_status_dto::*;
27use super::mosaic_builder::*;
28use super::state_header_builder::*;
29
30/// Binary layout for hash lock transaction info.
31#[derive(Debug, Clone)]
32pub struct HashLockInfoBuilder {
33 /// State header.
34 super_object: StateHeaderBuilder,
35 /// Owner address.
36 owner_address: AddressDto,
37 /// Mosaic associated with lock.
38 mosaic: MosaicBuilder,
39 /// Height at which the lock expires.
40 end_height: HeightDto,
41 /// Flag indicating whether or not the lock was already used.
42 status: LockStatusDto,
43 /// Hash.
44 hash: Hash256Dto,
45}
46
47
48impl HashLockInfoBuilder {
49 /// Creates an instance of HashLockInfoBuilder from binary payload.
50 /// payload: Byte payload to use to serialize the object.
51 /// # Returns
52 /// A HashLockInfoBuilder.
53 pub fn from_binary(_bytes: &[u8]) -> Self {
54 let super_object = StateHeaderBuilder::from_binary(_bytes);
55 let mut _bytes = _bytes[super_object.get_size()..].to_vec();
56 let owner_address = AddressDto::from_binary(&_bytes); // kind:CUSTOM1
57 let mut _bytes = _bytes[owner_address.get_size()..].to_vec();
58 let mosaic = MosaicBuilder::from_binary(&_bytes); // kind:CUSTOM1
59 let mut _bytes = _bytes[mosaic.get_size()..].to_vec();
60 let end_height = HeightDto::from_binary(&_bytes); // kind:CUSTOM1
61 let mut _bytes = _bytes[end_height.get_size()..].to_vec();
62 let status = LockStatusDto::from_binary(&_bytes); // kind:CUSTOM2
63 let mut _bytes = _bytes[status.get_size()..].to_vec();
64 let hash = Hash256Dto::from_binary(&_bytes); // kind:CUSTOM1
65 let mut _bytes = _bytes[hash.get_size()..].to_vec();
66 HashLockInfoBuilder { super_object, owner_address, mosaic, end_height, status, hash }
67 }
68
69 /// Gets owner address.
70 ///
71 /// # Returns
72 /// A Owner address.
73 pub fn get_owner_address(&self) -> AddressDto {
74 self.owner_address.clone()
75 }
76
77 /// Gets mosaic associated with lock.
78 ///
79 /// # Returns
80 /// A Mosaic associated with lock.
81 pub fn get_mosaic(&self) -> MosaicBuilder {
82 self.mosaic.clone()
83 }
84
85 /// Gets height at which the lock expires.
86 ///
87 /// # Returns
88 /// A Height at which the lock expires.
89 pub fn get_end_height(&self) -> HeightDto {
90 self.end_height.clone()
91 }
92
93 /// Gets flag indicating whether or not the lock was already used.
94 ///
95 /// # Returns
96 /// A Flag indicating whether or not the lock was already used.
97 pub fn get_status(&self) -> LockStatusDto {
98 self.status.clone()
99 }
100
101 /// Gets hash.
102 ///
103 /// # Returns
104 /// A Hash.
105 pub fn get_hash(&self) -> Hash256Dto {
106 self.hash.clone()
107 }
108
109 /// Gets the size of the type.
110 ///
111 /// Returns:
112 /// A size in bytes.
113 pub fn get_size(&self) -> usize {
114 let mut size = self.super_object.get_size();
115 size += self.owner_address.get_size(); // owner_address;
116 size += self.mosaic.get_size(); // mosaic;
117 size += self.end_height.get_size(); // end_height;
118 size += self.status.get_size(); // status;
119 size += self.hash.get_size(); // hash;
120 size
121 }
122
123 /// Serializes self to bytes.
124 ///
125 /// # Returns
126 /// A Serialized bytes.
127 pub fn serializer(&self) -> Vec<u8> {
128 let mut buf: Vec<u8> = vec![];
129 buf.append(&mut self.super_object.serializer());
130 buf.append(&mut self.owner_address.serializer()); // kind:CUSTOM
131 buf.append(&mut self.mosaic.serializer()); // kind:CUSTOM
132 buf.append(&mut self.end_height.serializer()); // kind:CUSTOM
133 buf.append(&mut self.status.serializer()); // kind:CUSTOM
134 buf.append(&mut self.hash.serializer()); // kind:CUSTOM
135 buf
136 }
137}
138