catbuffer_rust/
secret_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::lock_hash_algorithm_dto::*;
26use super::unresolved_address_dto::*;
27use super::unresolved_mosaic_builder::*;
28
29/// Binary layout for a secret lock transaction.
30#[derive(Debug, Clone)]
31pub struct SecretLockTransactionBodyBuilder {
32    /// Locked mosaic recipient address.
33    pub recipient_address: UnresolvedAddressDto,
34    /// Secret.
35    pub secret: Hash256Dto,
36    /// Locked mosaic.
37    pub mosaic: UnresolvedMosaicBuilder,
38    /// Number of blocks for which a lock should be valid.
39    pub duration: BlockDurationDto,
40    /// Hash algorithm.
41    pub hash_algorithm: LockHashAlgorithmDto,
42}
43
44impl SecretLockTransactionBodyBuilder {
45    /// Creates an instance of SecretLockTransactionBodyBuilder from binary payload.
46    /// payload: Byte payload to use to serialize the object.
47    /// # Returns
48    /// A SecretLockTransactionBodyBuilder.
49    pub fn from_binary(payload: &[u8]) -> Self {
50        let mut _bytes = payload.to_vec();
51        let recipient_address = UnresolvedAddressDto::from_binary(&_bytes); // kind:CUSTOM1
52        _bytes = _bytes[recipient_address.get_size()..].to_vec();
53        let secret = Hash256Dto::from_binary(&_bytes); // kind:CUSTOM1
54        _bytes = _bytes[secret.get_size()..].to_vec();
55        let mosaic = UnresolvedMosaicBuilder::from_binary(&_bytes); // kind:CUSTOM1
56        _bytes = _bytes[mosaic.get_size()..].to_vec();
57        let duration = BlockDurationDto::from_binary(&_bytes); // kind:CUSTOM1
58        _bytes = _bytes[duration.get_size()..].to_vec();
59        let hash_algorithm = LockHashAlgorithmDto::from_binary(&_bytes); // kind:CUSTOM2
60        _bytes = (&_bytes[hash_algorithm.get_size()..]).to_vec();
61        // create object and call.
62        SecretLockTransactionBodyBuilder { recipient_address, secret, mosaic, duration, hash_algorithm } // TransactionBody
63    }
64
65    /// Gets the size of the type.
66    ///
67    /// Returns:
68    /// A size in bytes.
69    pub fn get_size(&self) -> usize {
70        let mut size = 0;
71        size += self.recipient_address.get_size(); // recipient_address_size;
72        size += self.secret.get_size(); // secret_size;
73        size += self.mosaic.get_size(); // mosaic_size;
74        size += self.duration.get_size(); // duration_size;
75        size += self.hash_algorithm.get_size(); // hash_algorithm_size;
76        size
77    }
78
79    /// Serializes self to bytes.
80    ///
81    /// # Returns
82    /// A Serialized bytes.
83    pub fn serializer(&self) -> Vec<u8> {
84        let mut buf: Vec<u8> = vec![];
85        buf.append(&mut self.recipient_address.serializer()); // kind:CUSTOM
86        buf.append(&mut self.secret.serializer()); // kind:CUSTOM
87        buf.append(&mut self.mosaic.serializer()); // kind:CUSTOM
88        buf.append(&mut self.duration.serializer()); // kind:CUSTOM
89        buf.append(&mut self.hash_algorithm.serializer()); // kind:CUSTOM
90        buf
91    }
92}
93