catbuffer_rust/multisig_account_modification_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 a multisig account modification transaction.
26#[derive(Debug, Clone)]
27pub struct MultisigAccountModificationTransactionBodyBuilder {
28 /// Relative change of the minimal number of cosignatories required when removing an account.
29 pub min_removal_delta: u8,
30 /// Relative change of the minimal number of cosignatories required when approving a transaction.
31 pub min_approval_delta: u8,
32 /// Cosignatory address additions.
33 pub address_additions: Vec<UnresolvedAddressDto>,
34 /// Cosignatory address deletions.
35 pub address_deletions: Vec<UnresolvedAddressDto>,
36}
37
38impl MultisigAccountModificationTransactionBodyBuilder {
39 /// Creates an instance of MultisigAccountModificationTransactionBodyBuilder from binary payload.
40 /// payload: Byte payload to use to serialize the object.
41 /// # Returns
42 /// A MultisigAccountModificationTransactionBodyBuilder.
43 pub fn from_binary(payload: &[u8]) -> Self {
44 let mut _bytes = payload.to_vec();
45 let buf = fixed_bytes::<1>(&_bytes);
46 let min_removal_delta = u8::from_le_bytes(buf); // kind:SIMPLE
47 _bytes = (&_bytes[1..]).to_vec();
48 let buf = fixed_bytes::<1>(&_bytes);
49 let min_approval_delta = u8::from_le_bytes(buf); // kind:SIMPLE
50 _bytes = (&_bytes[1..]).to_vec();
51 let buf = fixed_bytes::<1>(&_bytes);
52 let address_additions_count = u8::from_le_bytes(buf); // kind:SIZE_FIELD
53 _bytes = (&_bytes[1..]).to_vec();
54 let buf = fixed_bytes::<1>(&_bytes);
55 let address_deletions_count = u8::from_le_bytes(buf); // kind:SIZE_FIELD
56 _bytes = (&_bytes[1..]).to_vec();
57 let buf = fixed_bytes::<4>(&_bytes);
58 let _ = u32::from_le_bytes(buf); // kind:SIMPLE
59 _bytes = (&_bytes[4..]).to_vec();
60 let mut address_additions: Vec<UnresolvedAddressDto> = vec![]; // kind:ARRAY
61 for _ in 0..address_additions_count {
62 let item = UnresolvedAddressDto::from_binary(&_bytes);
63 address_additions.push(item.clone());
64 _bytes = (&_bytes[item.get_size()..]).to_vec();
65 }
66 let mut address_deletions: Vec<UnresolvedAddressDto> = vec![]; // kind:ARRAY
67 for _ in 0..address_deletions_count {
68 let item = UnresolvedAddressDto::from_binary(&_bytes);
69 address_deletions.push(item.clone());
70 _bytes = (&_bytes[item.get_size()..]).to_vec();
71 }
72 // create object and call.
73 MultisigAccountModificationTransactionBodyBuilder { min_removal_delta, min_approval_delta, address_additions, address_deletions } // TransactionBody
74 }
75
76 /// Gets the size of the type.
77 ///
78 /// Returns:
79 /// A size in bytes.
80 pub fn get_size(&self) -> usize {
81 let mut size = 0;
82 size += 1; // min_removal_delta;
83 size += 1; // min_approval_delta;
84 size += 1; // address_additions_count;
85 size += 1; // address_deletions_count;
86 size += 4; // multisig_account_modification_transaction_body__reserved1;
87 for i in &self.address_additions {
88 size += i.get_size(); // FILL_ARRAY
89 };
90 for i in &self.address_deletions {
91 size += i.get_size(); // FILL_ARRAY
92 };
93 size
94 }
95
96 /// Serializes self to bytes.
97 ///
98 /// # Returns
99 /// A Serialized bytes.
100 pub fn serializer(&self) -> Vec<u8> {
101 let mut buf: Vec<u8> = vec![];
102 buf.append(&mut self.min_removal_delta.to_le_bytes().to_vec()); // serial_kind:SIMPLE
103 buf.append(&mut self.min_approval_delta.to_le_bytes().to_vec()); // serial_kind:SIMPLE
104 let size_value: u8 = self.address_additions.len() as u8;
105 buf.append(&mut size_value.to_le_bytes().to_vec()); // kind:SIZE_FIELD
106 let size_value: u8 = self.address_deletions.len() as u8;
107 buf.append(&mut size_value.to_le_bytes().to_vec()); // kind:SIZE_FIELD
108 buf.append(&mut [0u8; 4].to_vec()); // kind:SIMPLE and is_reserved
109 for i in &self.address_additions {
110 buf.append(&mut i.serializer()); // kind:ARRAY|FILL_ARRAY
111 }
112 for i in &self.address_deletions {
113 buf.append(&mut i.serializer()); // kind:ARRAY|FILL_ARRAY
114 }
115 buf
116 }
117}
118