Skip to main content

eosio/crypto/
keys.rs

1use crate::{NumBytes, Read, UnsignedInt, Write};
2use alloc::string::String;
3use core::fmt;
4
5macro_rules! key_type {
6    ($ident:ident, $bytes:literal) => {
7        /// TODO depreciate, newer signature types cannot be represented as a
8        /// fixed size structure EOSIO Public Key
9        /// <https://github.com/EOSIO/eosio.cdt/blob/4985359a30da1f883418b7133593f835927b8046/libraries/eosiolib/core/eosio/crypto.hpp#L22-L48>
10        #[derive(Read, Write, NumBytes, Clone)]
11        #[eosio(crate_path = "crate::bytes")]
12        pub struct $ident {
13            /// Type of the public key, could be either K1 or R1
14            pub type_: UnsignedInt,
15            /// Bytes of the public key
16            pub data: [u8; $bytes],
17        }
18
19        impl $ident {
20            /// TODO docs.
21            #[must_use]
22            pub const fn as_bytes(&self) -> &[u8; $bytes] {
23                &self.data
24            }
25
26            /// TODO docs.
27            #[must_use]
28            pub const fn to_bytes(&self) -> [u8; $bytes] {
29                self.data
30            }
31
32            /// TODO docs.
33            #[must_use]
34            pub fn as_slice(&self) -> &[u8] {
35                &self.data
36            }
37        }
38
39        impl Default for $ident {
40            #[must_use]
41            fn default() -> Self {
42                Self {
43                    type_: UnsignedInt::default(),
44                    data: [0_u8; $bytes],
45                }
46            }
47        }
48
49        impl PartialEq for $ident {
50            #[must_use]
51            fn eq(&self, other: &Self) -> bool {
52                self.type_ == other.type_ && self.as_slice() == other.as_slice()
53            }
54        }
55
56        impl fmt::Debug for $ident {
57            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58                fmt::Debug::fmt(&self.type_, f)?;
59                fmt::Debug::fmt(self.as_slice(), f)
60            }
61        }
62    };
63}
64
65key_type!(PublicKey, 34);
66key_type!(Signature, 66);
67
68/// TODO docs
69#[derive(Read, Write, NumBytes, Clone)]
70#[eosio(crate_path = "crate::bytes")]
71pub struct PrivateKey(String);