catbuffer_rust/
embedded_account_metadata_transaction_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::account_metadata_transaction_body_builder::*;
23use super::embedded_transaction_builder::*;
24use super::embedded_transaction_helper::*;
25use super::entity_type_dto::*;
26use super::generator_utils::*;
27use super::key_dto::*;
28use super::network_type_dto::*;
29use super::unresolved_address_dto::*;
30
31/// Binary layout for an embedded account metadata transaction.
32#[derive(Debug, Clone)]
33pub struct EmbeddedAccountMetadataTransactionBuilder {
34    /// Embedded transaction.
35    pub super_object: EmbeddedTransactionBuilder,
36    /// Account metadata transaction body.
37    pub body: AccountMetadataTransactionBodyBuilder,
38}
39
40impl EmbeddedAccountMetadataTransactionBuilder {
41    const VERSION: u8 = 1;
42    const ENTITY_TYPE: u16 = 0x4144;
43
44
45    /// Creates an instance of EmbeddedAccountMetadataTransactionBuilder from binary payload.
46    /// payload: Byte payload to use to serialize the object.
47    /// # Returns
48    /// A EmbeddedAccountMetadataTransactionBuilder.
49    pub fn from_binary(payload: &[u8]) -> Self {
50        let mut _bytes = payload.to_vec();
51        let super_object = EmbeddedTransactionBuilder::from_binary(&_bytes);
52        assert_eq!(Self::VERSION, super_object.version, "Invalid entity version ({})", super_object.version);
53        assert_eq!(Self::ENTITY_TYPE, super_object._type.get_value(), "Invalid entity type ({:?})", super_object._type);
54        let mut _bytes = _bytes[super_object.get_size()..].to_vec();
55        let account_metadata_transaction_body = AccountMetadataTransactionBodyBuilder::from_binary(&_bytes); // kind:CUSTOM1
56        _bytes = _bytes[account_metadata_transaction_body.get_size()..].to_vec();
57        // create object and call.
58        EmbeddedAccountMetadataTransactionBuilder { super_object, body: account_metadata_transaction_body }  // Transaction
59        // nothing needed to copy into EmbeddedTransaction
60    }
61
62
63    pub fn get_target_address(&self) -> UnresolvedAddressDto {
64        self.body.target_address.clone()
65    }
66    pub fn set_target_address(&mut self, target_address: UnresolvedAddressDto) {
67        self.body.target_address = target_address;   // MARKER1 AttributeKind.CUSTOM
68    }
69
70
71    pub fn get_scoped_metadata_key(&self) -> u64 {
72        self.body.scoped_metadata_key.clone()
73    }
74    pub fn set_scoped_metadata_key(&mut self, scoped_metadata_key: u64) {
75        self.body.scoped_metadata_key = scoped_metadata_key;   // MARKER1 AttributeKind.SIMPLE
76    }
77
78
79    pub fn get_value_size_delta(&self) -> u16 {
80        self.body.value_size_delta.clone()
81    }
82    pub fn set_value_size_delta(&mut self, value_size_delta: u16) {
83        self.body.value_size_delta = value_size_delta;   // MARKER1 AttributeKind.SIMPLE
84    }
85
86
87    pub fn get_value(&self) -> Vec<u8> {
88        self.body.value.clone()
89    }
90    pub fn set_value(&mut self, value: Vec<u8>) {
91        self.body.value = value;   // MARKER1 AttributeKind.BUFFER
92    }
93
94    /// Gets the size of the type.
95    ///
96    /// Returns:
97    /// A size in bytes.
98    pub fn get_size(&self) -> usize {
99        let mut size = self.super_object.get_size();
100        size += self.body.get_size();
101        size
102    }
103
104    /// Serializes self to bytes.
105    ///
106    /// # Returns
107    /// A Serialized bytes.
108    pub fn serializer(&self) -> Vec<u8> {
109        let mut buf: Vec<u8> = vec![];
110        buf.append(&mut (self.get_size() as u32).to_le_bytes().to_vec());
111        buf.append(&mut self.super_object.serializer());
112        buf.append(&mut self.body.serializer()); // kind:CUSTOM TransactionBody
113        buf
114    }
115}
116
117impl EmbeddedTransactionHelper for EmbeddedAccountMetadataTransactionBuilder {
118    fn box_clone(&self) -> Box<dyn EmbeddedTransactionHelper> {
119        Box::new((*self).clone())
120    }
121
122    fn get_size(&self) -> usize {
123        self.get_size()
124    }
125
126    fn serializer(&self) -> Vec<u8> {
127        self.serializer()
128    }
129}
130