catbuffer_rust/
multisig_entry_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::address_dto::*;
23use super::address_dto::*;
24use super::generator_utils::*;
25use super::state_header_builder::*;
26
27/// Binary layout for a multisig entry.
28#[derive(Debug, Clone)]
29pub struct MultisigEntryBuilder {
30    /// State header.
31    super_object: StateHeaderBuilder,
32    /// Minimum approval for modifications.
33    min_approval: u32,
34    /// Minimum approval for removal.
35    min_removal: u32,
36    /// Account address.
37    account_address: AddressDto,
38    /// Cosignatories for account.
39    cosignatory_addresses: Vec<AddressDto>,
40    /// Accounts for which the entry is cosignatory.
41    multisig_addresses: Vec<AddressDto>,
42}
43
44
45impl MultisigEntryBuilder {
46    /// Creates an instance of MultisigEntryBuilder from binary payload.
47    /// payload: Byte payload to use to serialize the object.
48    /// # Returns
49    /// A MultisigEntryBuilder.
50    pub fn from_binary(_bytes: &[u8]) -> Self {
51        let super_object = StateHeaderBuilder::from_binary(_bytes);
52        let mut _bytes = _bytes[super_object.get_size()..].to_vec();
53        let buf = fixed_bytes::<4>(&_bytes);
54        let min_approval = u32::from_le_bytes(buf); // kind:SIMPLE
55        let _bytes = (&_bytes[4..]).to_vec();
56        let buf = fixed_bytes::<4>(&_bytes);
57        let min_removal = u32::from_le_bytes(buf); // kind:SIMPLE
58        let _bytes = (&_bytes[4..]).to_vec();
59        let account_address = AddressDto::from_binary(&_bytes); // kind:CUSTOM1
60        let mut _bytes = _bytes[account_address.get_size()..].to_vec();
61        let buf = fixed_bytes::<8>(&_bytes);
62        let cosignatoryAddressesCount = u64::from_le_bytes(buf); // kind:SIZE_FIELD
63        let mut _bytes = (&_bytes[8..]).to_vec();
64        let mut cosignatory_addresses: Vec<AddressDto> = vec![]; // kind:ARRAY
65        let mut _bytes = _bytes.to_vec();
66        for _ in 0..cosignatoryAddressesCount {
67            let item = AddressDto::from_binary(&_bytes);
68            cosignatory_addresses.push(item.clone());
69            _bytes = (&_bytes[item.get_size()..]).to_vec();
70        }
71        let buf = fixed_bytes::<8>(&_bytes);
72        let multisigAddressesCount = u64::from_le_bytes(buf); // kind:SIZE_FIELD
73        let mut _bytes = (&_bytes[8..]).to_vec();
74        let mut multisig_addresses: Vec<AddressDto> = vec![]; // kind:ARRAY
75        let mut _bytes = _bytes.to_vec();
76        for _ in 0..multisigAddressesCount {
77            let item = AddressDto::from_binary(&_bytes);
78            multisig_addresses.push(item.clone());
79            _bytes = (&_bytes[item.get_size()..]).to_vec();
80        }
81        MultisigEntryBuilder { super_object, min_approval, min_removal, account_address, cosignatory_addresses, multisig_addresses }
82    }
83
84    /// Gets minimum approval for modifications.
85    ///
86    /// # Returns
87    /// A Minimum approval for modifications.
88    pub fn get_min_approval(&self) -> u32 {
89        self.min_approval.clone()
90    }
91
92    /// Gets minimum approval for removal.
93    ///
94    /// # Returns
95    /// A Minimum approval for removal.
96    pub fn get_min_removal(&self) -> u32 {
97        self.min_removal.clone()
98    }
99
100    /// Gets account address.
101    ///
102    /// # Returns
103    /// A Account address.
104    pub fn get_account_address(&self) -> AddressDto {
105        self.account_address.clone()
106    }
107
108    /// Gets cosignatories for account.
109    ///
110    /// # Returns
111    /// A Cosignatories for account.
112    pub fn get_cosignatory_addresses(&self) -> Vec<AddressDto> {
113        self.cosignatory_addresses.clone() // ARRAY or FILL_ARRAY
114    }
115
116    /// Gets accounts for which the entry is cosignatory.
117    ///
118    /// # Returns
119    /// A Accounts for which the entry is cosignatory.
120    pub fn get_multisig_addresses(&self) -> Vec<AddressDto> {
121        self.multisig_addresses.clone() // ARRAY or FILL_ARRAY
122    }
123
124    /// Gets the size of the type.
125    ///
126    /// Returns:
127    /// A size in bytes.
128    pub fn get_size(&self) -> usize {
129        let mut size = self.super_object.get_size();
130        size += 4; // min_approval;
131        size += 4; // min_removal;
132        size += self.account_address.get_size(); // account_address;
133        size += 8; // cosignatory_addresses_count;
134        size += self.cosignatory_addresses.iter().map(|item| item.get_size()).sum::<usize>(); // array or fill_array;
135        size += 8; // multisig_addresses_count;
136        size += self.multisig_addresses.iter().map(|item| item.get_size()).sum::<usize>(); // array or fill_array;
137        size
138    }
139
140    /// Serializes self to bytes.
141    ///
142    /// # Returns
143    /// A Serialized bytes.
144    pub fn serializer(&self) -> Vec<u8> {
145        let mut buf: Vec<u8> = vec![];
146        buf.append(&mut self.super_object.serializer());
147        buf.append(&mut self.get_min_approval().to_le_bytes().to_vec()); // kind:SIMPLE
148        buf.append(&mut self.get_min_removal().to_le_bytes().to_vec()); // kind:SIMPLE
149        buf.append(&mut self.account_address.serializer()); // kind:CUSTOM
150        buf.append(&mut (self.get_cosignatory_addresses().len() as u64).to_le_bytes().to_vec()); // kind:SIZE_FIELD
151        for i in &self.cosignatory_addresses {
152            buf.append(&mut i.serializer()); // kind:ARRAY|FILL_ARRAY
153        }
154        buf.append(&mut (self.get_multisig_addresses().len() as u64).to_le_bytes().to_vec()); // kind:SIZE_FIELD
155        for i in &self.multisig_addresses {
156            buf.append(&mut i.serializer()); // kind:ARRAY|FILL_ARRAY
157        }
158        buf
159    }
160}
161