Use procedural macro to generate static public keys (#7219)

automerge
This commit is contained in:
Justin Starry
2019-12-03 20:55:18 -05:00
committed by Grimes
parent a66a49d384
commit 7cfff75c3e
13 changed files with 384 additions and 231 deletions

View File

@ -1,2 +1 @@
pub const BS58_STRING: &str = "BPFLoader1111111111111111111111111111111111";
crate::declare_id!(BS58_STRING);
crate::declare_id!("BPFLoader1111111111111111111111111111111111");

View File

@ -37,6 +37,10 @@ macro_rules! solana_entrypoint(
/// # Examples
///
/// ```
/// use std::str::FromStr;
/// # // wrapper is used so that the macro invocation occurs in the item position
/// # // rather than in the statement position which isn't allowed.
/// # mod item_wrapper {
/// use solana_sdk::account::KeyedAccount;
/// use solana_sdk::instruction::InstructionError;
/// use solana_sdk::pubkey::Pubkey;
@ -51,11 +55,46 @@ macro_rules! solana_entrypoint(
/// Ok(())
/// }
///
/// solana_sdk::declare_program!(
/// "My!!!11111111111111111111111111111111111111",
/// declare_program!(
/// "My11111111111111111111111111111111111111111",
/// solana_my_program,
/// my_process_instruction
/// );
///
/// # }
/// # use solana_sdk::pubkey::Pubkey;
/// # use item_wrapper::id;
/// let my_id = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();
/// assert_eq!(id(), my_id);
/// ```
/// ```
/// use std::str::FromStr;
/// # // wrapper is used so that the macro invocation occurs in the item position
/// # // rather than in the statement position which isn't allowed.
/// # mod item_wrapper {
/// use solana_sdk::account::KeyedAccount;
/// use solana_sdk::instruction::InstructionError;
/// use solana_sdk::pubkey::Pubkey;
/// use solana_sdk::declare_program;
///
/// fn my_process_instruction(
/// program_id: &Pubkey,
/// keyed_accounts: &mut [KeyedAccount],
/// data: &[u8],
/// ) -> Result<(), InstructionError> {
/// // Process an instruction
/// Ok(())
/// }
///
/// declare_program!(
/// solana_sdk::system_program::ID,
/// solana_my_program,
/// my_process_instruction
/// );
/// # }
///
/// # use item_wrapper::id;
/// assert_eq!(id(), solana_sdk::system_program::ID);
/// ```
#[macro_export]
macro_rules! declare_program(

View File

@ -1,3 +1,6 @@
// Allows macro expansion of `use ::solana_sdk::*` to work within this crate
extern crate self as solana_sdk;
pub mod account;
pub mod account_utils;
pub mod bpf_loader;
@ -24,6 +27,29 @@ pub mod system_program;
pub mod sysvar;
pub mod timing;
/// Convenience macro to declare a static public key and functions to interact with it
///
/// Input: a single literal base58 string representation of a program's id
///
/// # Example
///
/// ```
/// # // wrapper is used so that the macro invocation occurs in the item position
/// # // rather than in the statement position which isn't allowed.
/// use std::str::FromStr;
/// use solana_sdk::{declare_id, pubkey::Pubkey};
///
/// # mod item_wrapper {
/// # use solana_sdk::declare_id;
/// declare_id!("My11111111111111111111111111111111111111111");
/// # }
/// # use item_wrapper::id;
///
/// let my_id = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();
/// assert_eq!(id(), my_id);
/// ```
pub use solana_sdk_macro::declare_id;
// On-chain program specific modules
pub mod account_info;
pub mod entrypoint;
@ -52,5 +78,4 @@ pub mod transport;
#[macro_use]
extern crate serde_derive;
pub extern crate bs58;
pub extern crate lazy_static;
extern crate log as logger;

View File

@ -1,5 +1,4 @@
pub const BS58_STRING: &str = "MoveLdr111111111111111111111111111111111111";
crate::declare_id!(BS58_STRING);
crate::declare_id!("MoveLdr111111111111111111111111111111111111");
pub fn solana_move_loader_program() -> (String, crate::pubkey::Pubkey) {
("solana_move_loader_program".to_string(), id())

View File

@ -47,6 +47,10 @@ impl Pubkey {
)
}
pub const fn new_from_array(pubkey_array: [u8; 32]) -> Self {
Self(pubkey_array)
}
#[cfg(not(feature = "program"))]
pub fn new_rand() -> Self {
Self::new(&rand::random::<[u8; 32]>())
@ -104,62 +108,6 @@ pub fn read_pubkey_file(infile: &str) -> Result<Pubkey, Box<dyn error::Error>> {
Ok(Pubkey::from_str(&printable)?)
}
/// Convenience macro to declare a static Pubkey and functions to interact with it
///
/// bs58_string: bs58 string representation the program's id
///
/// # Examples
///
/// ```
/// solana_sdk::declare_id!("My!!!11111111111111111111111111111111111111");
/// ```
#[macro_export]
macro_rules!
declare_id(
($bs58_string:expr) => (
use std::str::FromStr;
$crate::lazy_static::lazy_static! {
static ref _PUBKEY: $crate::pubkey::Pubkey = {
match $crate::pubkey::Pubkey::from_str(&$bs58_string) {
Ok(pubkey) => pubkey,
Err(_) => {
let pubkey_vec = $crate::bs58::decode(&$bs58_string)
.into_vec()
.map_err(|e| panic!("Error: {}, {}", $bs58_string, e))
.unwrap();
let expected_len = std::mem::size_of::<$crate::pubkey::Pubkey>();
let len = pubkey_vec.len();
if len != expected_len {
panic!(
"Error: {}, decoded length {}, expected {}",
$bs58_string, len, expected_len);
} else {
panic!(
"Error: {}, not a valid string, cannot determine reason",
$bs58_string);
}
}
}
};
}
pub fn check_id(id: &$crate::pubkey::Pubkey) -> bool {
*id == *_PUBKEY
}
pub fn id() -> $crate::pubkey::Pubkey {
*_PUBKEY
}
#[cfg(test)]
#[test]
fn test_id() {
assert!(check_id(&id()));
}
)
);
#[cfg(test)]
mod tests {
use super::*;