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