catbuffer_rust/account_metadata_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::unresolved_address_dto::*;
24
25/// Binary layout for an account metadata transaction.
26#[derive(Debug, Clone)]
27pub struct AccountMetadataTransactionBodyBuilder {
28 /// Metadata target address.
29 pub target_address: UnresolvedAddressDto,
30 /// Metadata key scoped to source, target and type.
31 pub scoped_metadata_key: u64,
32 /// Change in value size in bytes.
33 pub value_size_delta: u16,
34 /// Difference between existing value and new value \note when there is no existing value, new value is same this value \note when there is an existing value, new value is calculated as xor(previous-value, value).
35 pub value: Vec<u8>,
36}
37
38impl AccountMetadataTransactionBodyBuilder {
39 /// Creates an instance of AccountMetadataTransactionBodyBuilder from binary payload.
40 /// payload: Byte payload to use to serialize the object.
41 /// # Returns
42 /// A AccountMetadataTransactionBodyBuilder.
43 pub fn from_binary(payload: &[u8]) -> Self {
44 let mut _bytes = payload.to_vec();
45 let target_address = UnresolvedAddressDto::from_binary(&_bytes); // kind:CUSTOM1
46 _bytes = _bytes[target_address.get_size()..].to_vec();
47 let buf = fixed_bytes::<8>(&_bytes);
48 let scoped_metadata_key = u64::from_le_bytes(buf); // kind:SIMPLE
49 _bytes = (&_bytes[8..]).to_vec();
50 let buf = fixed_bytes::<2>(&_bytes);
51 let value_size_delta = u16::from_le_bytes(buf); // kind:SIMPLE
52 _bytes = (&_bytes[2..]).to_vec();
53 let buf = fixed_bytes::<2>(&_bytes);
54 let value_size = u16::from_le_bytes(buf); // kind:SIZE_FIELD
55 _bytes = (&_bytes[2..]).to_vec();
56 let value = (&_bytes[..value_size as usize]).to_vec(); // kind:BUFFER
57 _bytes = (&_bytes[value_size as usize..]).to_vec();
58 // create object and call.
59 AccountMetadataTransactionBodyBuilder { target_address, scoped_metadata_key, value_size_delta, value } // 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.target_address.get_size(); // target_address_size;
69 size += 8; // scoped_metadata_key;
70 size += 2; // value_size_delta;
71 size += 2; // value_size;
72 size += self.value.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.target_address.serializer()); // kind:CUSTOM
83 buf.append(&mut self.scoped_metadata_key.to_le_bytes().to_vec()); // serial_kind:SIMPLE
84 buf.append(&mut self.value_size_delta.to_le_bytes().to_vec()); // serial_kind:SIMPLE
85 let size_value: u16 = self.value.len() as u16;
86 buf.append(&mut size_value.to_le_bytes().to_vec()); // kind:SIZE_FIELD
87 buf.append(&mut self.value.clone()); // kind:BUFFER
88 buf
89 }
90}
91