Move address creation with seed into pubkey (#8991)

This commit is contained in:
Jack May
2020-03-20 15:20:48 -07:00
committed by GitHub
parent 1aab959d4e
commit e28368ff1b
11 changed files with 117 additions and 113 deletions

View File

@ -1,5 +1,4 @@
use crate::{
hash::hashv,
instruction::{AccountMeta, Instruction, WithSigner},
nonce,
program_utils::DecodeError,
@ -50,9 +49,6 @@ impl<E> DecodeError<E> for NonceError {
}
}
/// maximum length of derived address seed
pub const MAX_ADDRESS_SEED_LEN: usize = 32;
/// maximum permitted size of data: 10 MB
pub const MAX_PERMITTED_DATA_LENGTH: u64 = 10 * 1024 * 1024;
@ -80,7 +76,7 @@ pub enum SystemInstruction {
/// a base pubkey and a seed
/// * Transaction::keys[0] - source
/// * Transaction::keys[1] - new account key
/// * seed - string of ascii chars, no longer than MAX_ADDRESS_SEED_LEN
/// * seed - string of ascii chars, no longer than pubkey::MAX_SEED_LEN
/// * lamports - number of lamports to transfer to the new account
/// * space - number of bytes of memory to allocate
/// * program_id - the program id of the new account
@ -143,7 +139,7 @@ pub enum SystemInstruction {
/// Allocate space for and assign an account at an address
/// derived from a base pubkey and a seed
/// * Transaction::keys[0] - new account key
/// * seed - string of ascii chars, no longer than MAX_ADDRESS_SEED_LEN
/// * seed - string of ascii chars, no longer than pubkey::MAX_SEED_LEN
/// * space - number of bytes of memory to allocate
/// * program_id - the program id of the new account
AllocateWithSeed {
@ -154,7 +150,7 @@ pub enum SystemInstruction {
},
/// Assign account to a program based on a seed
/// * Transaction::keys[0] - account to assign
/// * seed - string of ascii chars, no longer than MAX_ADDRESS_SEED_LEN
/// * seed - string of ascii chars, no longer than pubkey::MAX_SEED_LEN
/// * program_id - the program id of the new account
AssignWithSeed {
base: Pubkey,
@ -293,20 +289,6 @@ pub fn transfer_many(from_pubkey: &Pubkey, to_lamports: &[(Pubkey, u64)]) -> Vec
.collect()
}
pub fn create_address_with_seed(
base: &Pubkey,
seed: &str,
program_id: &Pubkey,
) -> Result<Pubkey, SystemError> {
if seed.len() > MAX_ADDRESS_SEED_LEN {
return Err(SystemError::MaxSeedLengthExceeded);
}
Ok(Pubkey::new(
hashv(&[base.as_ref(), seed.as_ref(), program_id.as_ref()]).as_ref(),
))
}
pub fn create_nonce_account_with_seed(
from_pubkey: &Pubkey,
nonce_pubkey: &Pubkey,
@ -418,58 +400,6 @@ mod tests {
instruction.accounts.iter().map(|x| x.pubkey).collect()
}
#[test]
fn test_create_address_with_seed() {
assert!(create_address_with_seed(&Pubkey::new_rand(), "", &Pubkey::new_rand()).is_ok());
assert_eq!(
create_address_with_seed(
&Pubkey::new_rand(),
std::str::from_utf8(&[127; MAX_ADDRESS_SEED_LEN + 1]).unwrap(),
&Pubkey::new_rand()
),
Err(SystemError::MaxSeedLengthExceeded)
);
assert!(create_address_with_seed(
&Pubkey::new_rand(),
"\
\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\
",
&Pubkey::new_rand()
)
.is_ok());
// utf-8 abuse ;)
assert_eq!(
create_address_with_seed(
&Pubkey::new_rand(),
"\
x\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\
",
&Pubkey::new_rand()
),
Err(SystemError::MaxSeedLengthExceeded)
);
assert!(create_address_with_seed(
&Pubkey::new_rand(),
std::str::from_utf8(&[0; MAX_ADDRESS_SEED_LEN]).unwrap(),
&Pubkey::new_rand(),
)
.is_ok());
assert!(create_address_with_seed(&Pubkey::new_rand(), "", &Pubkey::new_rand(),).is_ok());
assert_eq!(
create_address_with_seed(
&Pubkey::default(),
"limber chicken: 4/45",
&Pubkey::default(),
),
Ok("9h1HyLCW5dZnBVap8C5egQ9Z6pHyjsh5MNy83iPqqRuq"
.parse()
.unwrap())
);
}
#[test]
fn test_move_many() {
let alice_pubkey = Pubkey::new_rand();