antelope/chain/authority.rs
1use crate::chain::action::PermissionLevel;
2use crate::chain::public_key::{deserialize_public_key, PublicKey};
3use crate::serializer::{Decoder, Encoder, Packer};
4use antelope_client_macros::StructPacker;
5use serde::{Deserialize, Serialize};
6
7// Assuming basic types like PublicKey and PermissionLevel are defined elsewhere
8
9/// KeyWeight associates a PublicKey with a Weight.
10#[derive(Serialize, Deserialize, Debug, Clone, Default, StructPacker)]
11pub struct KeyWeight {
12 #[serde(deserialize_with = "deserialize_public_key")]
13 pub key: PublicKey,
14 pub weight: u16,
15}
16
17/// PermissionLevelWeight associates a PermissionLevel with a Weight.
18#[derive(Serialize, Deserialize, Debug, Clone, Default, StructPacker)]
19pub struct PermissionLevelWeight {
20 pub permission: PermissionLevel,
21 pub weight: u16,
22}
23
24/// WaitWeight associates a wait time (in seconds) with a Weight.
25#[derive(Serialize, Deserialize, Debug, Clone, Default, StructPacker)]
26pub struct WaitWeight {
27 pub wait_sec: u32,
28 pub weight: u16,
29}
30
31/// Authority defines a set of keys and/or accounts that can authorize an action.
32#[derive(Serialize, Deserialize, Debug, Clone, Default, StructPacker)]
33pub struct Authority {
34 pub threshold: u32,
35 pub keys: Vec<KeyWeight>,
36 pub accounts: Vec<PermissionLevelWeight>,
37 pub waits: Vec<WaitWeight>,
38}
39
40impl Authority {
41 pub fn new(threshold: u32) -> Self {
42 Authority {
43 threshold,
44 keys: Vec::new(),
45 accounts: Vec::new(),
46 waits: Vec::new(),
47 }
48 }
49
50 pub fn new_single_key(public_key: PublicKey) -> Self {
51 let mut authority = Authority::new(1);
52 authority.keys.push(KeyWeight {
53 key: public_key,
54 weight: 1,
55 });
56 authority
57 }
58
59 pub fn wait_threshold(&self) -> u16 {
60 self.waits.iter().map(|w| w.weight).sum()
61 }
62
63 pub fn key_threshold(&self) -> u32 {
64 self.threshold - self.wait_threshold() as u32
65 }
66
67 pub fn key_weight(&self, public_key: &PublicKey) -> u16 {
68 self.keys
69 .iter()
70 .find(|&key_weight| &key_weight.key == public_key)
71 .map_or(0, |key_weight| key_weight.weight)
72 }
73
74 pub fn has_permission(&self, public_key: &PublicKey, include_partial: bool) -> bool {
75 let threshold = if include_partial {
76 1
77 } else {
78 self.key_threshold()
79 };
80 self.key_weight(public_key) >= threshold as u16
81 }
82
83 pub fn sort(&mut self) {
84 self.keys.sort_unstable_by_key(|k| k.key.clone());
85 self.accounts.sort_unstable_by_key(|a| a.permission);
86 self.waits.sort_unstable_by_key(|w| w.wait_sec);
87 }
88}
89
90// impl Packer for Weight {
91// fn size(&self) -> usize {
92// 2 // Weight is 2 bytes (since it's a u16)
93// }
94//
95// fn pack(&self, enc: &mut Encoder) -> usize {
96// // Convert the u16 Weight value to a byte array
97// let data = self.0.to_le_bytes(); // Assuming little-endian encoding
98// // Use a similar approach to pack_checksum to copy the bytes into the encoder
99// let allocated = enc.alloc(self.size());
100// slice_copy(allocated, &data);
101// self.size()
102// }
103//
104// fn unpack(&mut self, raw: &[u8]) -> usize {
105// let size = self.size();
106// assert!(raw.len() >= size, "Weight.unpack: buffer overflow!");
107// // Assuming the data is little-endian encoded, directly reconstruct the u16 value
108// self.0 = u16::from_le_bytes([raw[0], raw[1]]);
109// size
110// }
111// }
112//
113// impl Packer for KeyWeight {
114// fn size(&self) -> usize {
115// self.key.size() + self.weight.size()
116// }
117//
118// fn pack(&self, enc: &mut Encoder) -> usize {
119// let pos = enc.get_size();
120// self.key.pack(enc);
121// self.weight.pack(enc);
122// enc.get_size() - pos
123// }
124//
125// fn unpack(&mut self, data: &[u8]) -> usize {
126// let mut dec = Decoder::new(data);
127// dec.unpack(&mut self.key);
128// dec.unpack(&mut self.weight);
129// dec.get_pos()
130// }
131// }
132//
133// impl Packer for WaitWeight {
134// fn size(&self) -> usize {
135// 4 + self.weight.size() // wait_sec is a u32 (4 bytes) + size of weight
136// }
137//
138// fn pack(&self, enc: &mut Encoder) -> usize {
139// let pos = enc.get_size();
140// // Convert the u32 wait_sec value to a byte array and pack it
141// let wait_sec_bytes = self.wait_sec.to_le_bytes(); // Assuming little-endian encoding
142// let allocated_wait_sec = enc.alloc(4); // Allocate 4 bytes for wait_sec
143// slice_copy(allocated_wait_sec, &wait_sec_bytes); // Copy wait_sec_bytes into the encoder
144//
145// // Pack weight
146// self.weight.pack(enc);
147//
148// enc.get_size() - pos
149// }
150//
151// fn unpack(&mut self, data: &[u8]) -> usize {
152// assert!(
153// data.len() >= self.size(),
154// "WaitWeight.unpack: buffer overflow!"
155// );
156// // Assuming the data is little-endian encoded, directly reconstruct the u32 wait_sec value
157// self.wait_sec = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);
158// let mut dec = Decoder::new(&data[4..]); // Create a new Decoder starting after the first 4 bytes
159// dec.unpack(&mut self.weight);
160// 4 + dec.get_pos() // Return total bytes read (4 bytes for wait_sec + bytes read for weight)
161// }
162// }
163//
164// impl Packer for Authority {
165// fn size(&self) -> usize {
166// // Calculate the size based on the dynamic content of the Authority struct
167// 4 + // for threshold (u32)
168// self.keys.iter().map(Packer::size).sum::<usize>() +
169// self.accounts.iter().map(Packer::size).sum::<usize>() +
170// self.waits.iter().map(Packer::size).sum::<usize>()
171// }
172//
173// fn pack(&self, enc: &mut Encoder) -> usize {
174// let pos = enc.get_size();
175// // Convert the u32 threshold value to a byte array and pack it
176// let threshold_bytes = self.threshold.to_le_bytes(); // Assuming little-endian encoding
177// let allocated_threshold = enc.alloc(4); // Allocate 4 bytes for threshold
178// slice_copy(allocated_threshold, &threshold_bytes); // Copy threshold_bytes into the encoder
179//
180// // Iterate over keys, accounts, and waits to pack them
181// for key_weight in &self.keys {
182// key_weight.pack(enc);
183// }
184// for account in &self.accounts {
185// account.pack(enc);
186// }
187// for wait_weight in &self.waits {
188// wait_weight.pack(enc);
189// }
190//
191// enc.get_size() - pos
192// }
193//
194// fn unpack(&mut self, data: &[u8]) -> usize {
195// assert!(
196// data.len() >= 4,
197// "Authority.unpack: buffer underflow for threshold!"
198// );
199// // Assuming the data is little-endian encoded, directly reconstruct the u32 threshold value
200// self.threshold = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);
201//
202// let mut total_bytes_read = 4;
203// let mut dec = Decoder::new(&data[total_bytes_read..]);
204//
205// // Unpack keys
206// for key_weight in &mut self.keys {
207// total_bytes_read += dec.unpack(key_weight);
208// }
209// // Unpack accounts
210// for account in &mut self.accounts {
211// total_bytes_read += dec.unpack(account);
212// }
213// // Unpack waits
214// for wait_weight in &mut self.waits {
215// total_bytes_read += dec.unpack(wait_weight);
216// }
217//
218// total_bytes_read
219// }
220// }
221//
222// impl Packer for PermissionLevelWeight {
223// fn size(&self) -> usize {
224// self.permission.size() + self.weight.size()
225// }
226//
227// fn pack(&self, enc: &mut Encoder) -> usize {
228// let pos = enc.get_size();
229// // Pack `permission` and `weight` into the encoder
230// self.permission.pack(enc);
231// self.weight.pack(enc);
232// enc.get_size() - pos
233// }
234//
235// fn unpack(&mut self, data: &[u8]) -> usize {
236// let mut dec = Decoder::new(data);
237// // Unpack `permission` and `weight` from the data
238// dec.unpack(&mut self.permission);
239// dec.unpack(&mut self.weight);
240// dec.get_pos()
241// }
242// }