Use &[u8] for program address seeds rather then &str (#10744)

This commit is contained in:
Jack May
2020-06-22 16:51:43 -07:00
committed by GitHub
parent 842cab2739
commit 73586c1aad
9 changed files with 47 additions and 45 deletions

View File

@ -96,12 +96,13 @@ extern uint64_t entrypoint(const uint8_t *input) {
const SolInstruction instruction = {accounts[INVOKED_PROGRAM_INDEX].key,
arguments, SOL_ARRAY_SIZE(arguments),
data, SOL_ARRAY_SIZE(data)};
char seed1[] = "You pass butter";
char seed2[] = "Lil'";
char seed3[] = "Bits";
const SolSignerSeed seeds1[] = {{seed1, sol_strlen(seed1)}};
const SolSignerSeed seeds2[] = {{seed2, sol_strlen(seed2)},
{seed3, sol_strlen(seed3)}};
uint8_t seed1[] = {'Y', 'o', 'u', ' ', 'p', 'a', 's', 's',
' ', 'b', 'u', 't', 't', 'e', 'r'};
uint8_t seed2[] = {'L', 'i', 'l', '\''};
uint8_t seed3[] = {'B', 'i', 't', 's'};
const SolSignerSeed seeds1[] = {{seed1, SOL_ARRAY_SIZE(seed1)}};
const SolSignerSeed seeds2[] = {{seed2, SOL_ARRAY_SIZE(seed2)},
{seed3, SOL_ARRAY_SIZE(seed3)}};
const SolSignerSeeds signers_seeds[] = {{seeds1, SOL_ARRAY_SIZE(seeds1)},
{seeds2, SOL_ARRAY_SIZE(seeds2)}};
sol_assert(SUCCESS == sol_invoke_signed(&instruction, accounts,

View File

@ -77,7 +77,7 @@ extern uint64_t entrypoint(const uint8_t *input) {
break;
}
case TEST_RETURN_ERROR: {
sol_log("reutrn error");
sol_log("return error");
return 42;
}
case TEST_DERIVED_SIGNERS: {
@ -100,12 +100,12 @@ extern uint64_t entrypoint(const uint8_t *input) {
const SolInstruction instruction = {accounts[INVOKED_PROGRAM_INDEX].key,
arguments, SOL_ARRAY_SIZE(arguments),
data, SOL_ARRAY_SIZE(data)};
char seed1[] = "Lil'";
char seed2[] = "Bits";
char seed3[] = "Gar Ma Nar Nar";
const SolSignerSeed seeds1[] = {{seed1, sol_strlen(seed1)},
{seed2, sol_strlen(seed2)}};
const SolSignerSeed seeds2[] = {{seed3, sol_strlen(seed3)}};
uint8_t seed1[] = {'L', 'i', 'l', '\''};
uint8_t seed2[] = {'B', 'i', 't', 's'};
const SolSignerSeed seeds1[] = {{seed1, SOL_ARRAY_SIZE(seed1)},
{seed2, SOL_ARRAY_SIZE(seed2)}};
const SolSignerSeed seeds2[] = {
{(uint8_t *)accounts[DERIVED_KEY2_INDEX].key, SIZE_PUBKEY}};
const SolSignerSeeds signers_seeds[] = {{seeds1, SOL_ARRAY_SIZE(seeds1)},
{seeds2, SOL_ARRAY_SIZE(seeds2)}};

View File

@ -110,7 +110,7 @@ fn process_instruction(
invoke_signed(
&invoked_instruction,
accounts,
&[&["You pass butter"], &["Lil'", "Bits"]],
&[&[b"You pass butter"], &[b"Lil'", b"Bits"]],
)?;
}

View File

@ -132,7 +132,10 @@ fn process_instruction(
invoke_signed(
&invoked_instruction,
accounts,
&[&["Lil'", "Bits"], &["Gar Ma Nar Nar"]],
&[
&[b"Lil'", b"Bits"],
&[accounts[DERIVED_KEY2_INDEX].key.as_ref()],
],
)?;
}
TEST_VERIFY_NESTED_SIGNERS => {

View File

@ -39,7 +39,6 @@ mod bpf {
fn load_bpf_program(bank_client: &BankClient, payer_keypair: &Keypair, name: &str) -> Pubkey {
let path = create_bpf_path(name);
println!("path {:?}", path);
let mut file = File::open(path).unwrap();
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
@ -353,11 +352,11 @@ mod bpf {
bank.store_account(&from_keypair.pubkey(), &account);
let derived_key1 =
Pubkey::create_program_address(&["You pass butter"], &invoke_program_id).unwrap();
Pubkey::create_program_address(&[b"You pass butter"], &invoke_program_id).unwrap();
let derived_key2 =
Pubkey::create_program_address(&["Lil'", "Bits"], &invoked_program_id).unwrap();
Pubkey::create_program_address(&[b"Lil'", b"Bits"], &invoked_program_id).unwrap();
let derived_key3 =
Pubkey::create_program_address(&["Gar Ma Nar Nar"], &invoked_program_id).unwrap();
Pubkey::create_program_address(&[derived_key2.as_ref()], &invoked_program_id).unwrap();
let account_metas = vec![
AccountMeta::new(mint_keypair.pubkey(), true),

View File

@ -468,15 +468,12 @@ impl<'a> SyscallProcessInstruction<'a> for SyscallProcessInstructionRust<'a> {
let seeds = untranslated_seeds
.iter()
.map(|untranslated_seed| {
let seed_bytes = translate_slice!(
translate_slice!(
u8,
untranslated_seed.as_ptr(),
untranslated_seed.len(),
ro_regions
)?;
from_utf8(seed_bytes).map_err(|err| {
SyscallError::MalformedSignerSeed(err, seed_bytes.to_vec()).into()
})
)
})
.collect::<Result<Vec<_>, EbpfError<BPFError>>>()?;
let signer = Pubkey::create_program_address(&seeds, program_id)
@ -676,16 +673,11 @@ impl<'a> SyscallProcessInstruction<'a> for SyscallProcessSolInstructionC<'a> {
signer_seeds.len,
ro_regions
)?;
let seed_strs = seeds
let seeds_bytes = seeds
.iter()
.map(|seed| {
let seed_bytes = translate_slice!(u8, seed.addr, seed.len, ro_regions)?;
std::str::from_utf8(seed_bytes).map_err(|err| {
SyscallError::MalformedSignerSeed(err, seed_bytes.to_vec()).into()
})
})
.map(|seed| translate_slice!(u8, seed.addr, seed.len, ro_regions))
.collect::<Result<Vec<_>, EbpfError<BPFError>>>()?;
Pubkey::create_program_address(&seed_strs, program_id)
Pubkey::create_program_address(&seeds_bytes, program_id)
.map_err(|err| SyscallError::BadSeeds(err).into())
})
.collect::<Result<Vec<_>, EbpfError<BPFError>>>()?)