catbuffer_rust/
hash_lock_transaction_body_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::block_duration_dto::*;
23use super::generator_utils::*;
24use super::hash256_dto::*;
25use super::unresolved_mosaic_builder::*;
26
27/// Binary layout for a hash lock transaction.
28#[derive(Debug, Clone)]
29pub struct HashLockTransactionBodyBuilder {
30    /// Lock mosaic.
31    pub mosaic: UnresolvedMosaicBuilder,
32    /// Number of blocks for which a lock should be valid.
33    pub duration: BlockDurationDto,
34    /// Lock hash.
35    pub hash: Hash256Dto,
36}
37
38impl HashLockTransactionBodyBuilder {
39    /// Creates an instance of HashLockTransactionBodyBuilder from binary payload.
40    /// payload: Byte payload to use to serialize the object.
41    /// # Returns
42    /// A HashLockTransactionBodyBuilder.
43    pub fn from_binary(payload: &[u8]) -> Self {
44        let mut _bytes = payload.to_vec();
45        let mosaic = UnresolvedMosaicBuilder::from_binary(&_bytes); // kind:CUSTOM1
46        _bytes = _bytes[mosaic.get_size()..].to_vec();
47        let duration = BlockDurationDto::from_binary(&_bytes); // kind:CUSTOM1
48        _bytes = _bytes[duration.get_size()..].to_vec();
49        let hash = Hash256Dto::from_binary(&_bytes); // kind:CUSTOM1
50        _bytes = _bytes[hash.get_size()..].to_vec();
51        // create object and call.
52        HashLockTransactionBodyBuilder { mosaic, duration, hash } // TransactionBody
53    }
54
55    /// Gets the size of the type.
56    ///
57    /// Returns:
58    /// A size in bytes.
59    pub fn get_size(&self) -> usize {
60        let mut size = 0;
61        size += self.mosaic.get_size(); // mosaic_size;
62        size += self.duration.get_size(); // duration_size;
63        size += self.hash.get_size(); // hash_size;
64        size
65    }
66
67    /// Serializes self to bytes.
68    ///
69    /// # Returns
70    /// A Serialized bytes.
71    pub fn serializer(&self) -> Vec<u8> {
72        let mut buf: Vec<u8> = vec![];
73        buf.append(&mut self.mosaic.serializer()); // kind:CUSTOM
74        buf.append(&mut self.duration.serializer()); // kind:CUSTOM
75        buf.append(&mut self.hash.serializer()); // kind:CUSTOM
76        buf
77    }
78}
79