atlas_pubkey/lib.rs
1//! Atlas account addresses.
2#![no_std]
3#![cfg_attr(docsrs, feature(doc_auto_cfg))]
4#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
5#![allow(clippy::arithmetic_side_effects)]
6
7// If target_os = "atlas", then this panics so there are no dependencies.
8// When target_os != "atlas", this should be opt-in so users
9// don't need the curve25519 dependency.
10#[cfg(any(target_os = "atlas", feature = "curve25519"))]
11pub use atlas_address::bytes_are_curve_point;
12#[cfg(target_os = "atlas")]
13pub use atlas_address::syscalls;
14pub use atlas_address::{
15    address as pubkey,
16    error::{AddressError as PubkeyError, ParseAddressError as ParsePubkeyError},
17    Address as Pubkey, ADDRESS_BYTES as PUBKEY_BYTES, MAX_SEEDS, MAX_SEED_LEN,
18};
19#[cfg(all(feature = "rand", not(target_os = "atlas")))]
20pub use atlas_address::{
21    AddressHasher as PubkeyHasher, AddressHasherBuilder as PubkeyHasherBuilder,
22};
23
24/// New random `Pubkey` for tests and benchmarks.
25#[cfg(all(feature = "rand", not(target_os = "atlas")))]
26pub fn new_rand() -> Pubkey {
27    Pubkey::from(rand::random::<[u8; PUBKEY_BYTES]>())
28}
29
30/// Convenience macro to declare a static public key and functions to interact with it.
31///
32/// Input: a single literal base58 string representation of a program's ID.
33///
34/// # Example
35///
36/// ```
37/// # // wrapper is used so that the macro invocation occurs in the item position
38/// # // rather than in the statement position which isn't allowed.
39/// use std::str::FromStr;
40/// use atlas_pubkey::{declare_id, Pubkey};
41///
42/// # mod item_wrapper {
43/// #   use atlas_pubkey::declare_id;
44/// declare_id!("My11111111111111111111111111111111111111111");
45/// # }
46/// # use item_wrapper::id;
47///
48/// let my_id = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();
49/// assert_eq!(id(), my_id);
50/// ```
51#[macro_export]
52macro_rules! declare_id {
53    ($pubkey:expr) => {
54        /// The const program ID.
55        pub const ID: $crate::Pubkey = $crate::Pubkey::from_str_const($pubkey);
56
57        /// Returns `true` if given pubkey is the program ID.
58        // TODO make this const once `derive_const` makes it out of nightly
59        // and we can `derive_const(PartialEq)` on `Pubkey`.
60        pub fn check_id(id: &$crate::Pubkey) -> bool {
61            id == &ID
62        }
63
64        /// Returns the program ID.
65        pub const fn id() -> $crate::Pubkey {
66            ID
67        }
68
69        #[cfg(test)]
70        #[test]
71        fn test_id() {
72            assert!(check_id(&id()));
73        }
74    };
75}
76
77/// Same as [`declare_id`] except that it reports that this ID has been deprecated.
78#[macro_export]
79macro_rules! declare_deprecated_id {
80    ($pubkey:expr) => {
81        /// The const program ID.
82        pub const ID: $crate::Pubkey = $crate::Pubkey::from_str_const($pubkey);
83
84        /// Returns `true` if given pubkey is the program ID.
85        // TODO make this const once `derive_const` makes it out of nightly
86        // and we can `derive_const(PartialEq)` on `Pubkey`.
87        #[deprecated()]
88        pub fn check_id(id: &$crate::Pubkey) -> bool {
89            id == &ID
90        }
91
92        /// Returns the program ID.
93        #[deprecated()]
94        pub const fn id() -> $crate::Pubkey {
95            ID
96        }
97
98        #[cfg(test)]
99        #[test]
100        #[allow(deprecated)]
101        fn test_id() {
102            assert!(check_id(&id()));
103        }
104    };
105}