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