Skip to content

Commit ebfed45

Browse files
committed
Add account info and pubkey helpers
1 parent bdb9189 commit ebfed45

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

sdk/pinocchio/src/account_info.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ use crate::{program_error::ProgramError, pubkey::Pubkey, syscalls::sol_memset_};
88
/// single realloc.
99
pub const MAX_PERMITTED_DATA_INCREASE: usize = 1_024 * 10;
1010

11+
#[macro_export]
12+
macro_rules! get_account_info {
13+
( $accounts:ident, $index:expr ) => {{
14+
if $accounts.len() <= $index {
15+
return Err($crate::program_error::ProgramError::NotEnoughAccountKeys);
16+
}
17+
&$accounts[$index]
18+
}};
19+
}
20+
1121
/// Raw account data.
1222
///
1323
/// This data is wrapped in an `AccountInfo` struct, which provides safe access

sdk/pinocchio/src/pubkey.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
//! Public key type and functions.
22
3+
use crate::memory::{sol_memcmp, sol_memcpy};
4+
5+
/// Number of bytes in a pubkey
6+
pub const PUBKEY_BYTES: usize = 32;
7+
38
/// maximum length of derived `Pubkey` seed
49
pub const MAX_SEED_LEN: usize = 32;
510

@@ -9,10 +14,28 @@ pub const MAX_SEEDS: usize = 16;
914
/// The address of a [Solana account][account].
1015
///
1116
/// [account]: https://solana.com/docs/core/accounts
12-
pub type Pubkey = [u8; 32];
17+
pub type Pubkey = [u8; PUBKEY_BYTES];
18+
19+
/// Checks two pubkeys for equality in a computationally efficient way using
20+
/// `sol_memcmp`.
21+
pub fn compare(a: &Pubkey, b: &Pubkey) -> bool {
22+
// Safety:
23+
//
24+
// This method guarantees that the two arrays are of the same length.
25+
unsafe { sol_memcmp(a, b, PUBKEY_BYTES) == 0 }
26+
}
27+
28+
/// Copy `source` pubkey into `destination` in a computationally efficient way
29+
/// using `sol_memcpy`.
30+
pub fn copy(destination: &mut Pubkey, source: &Pubkey) {
31+
// Safety:
32+
//
33+
// This method guarantees that the two arrays are of the same length.
34+
unsafe { sol_memcpy(destination, source, PUBKEY_BYTES) }
35+
}
1336

1437
/// Log a `Pubkey` from a program
15-
pub fn log_pubkey(pubkey: &Pubkey) {
38+
pub fn log(pubkey: &Pubkey) {
1639
#[cfg(target_os = "solana")]
1740
unsafe {
1841
crate::syscalls::sol_log_pubkey(pubkey as *const _ as *const u8)

0 commit comments

Comments
 (0)