Last active
September 28, 2020 18:49
-
-
Save mvines/2b19330120ecdb624f0d859ef24e9f15 to your computer and use it in GitHub Desktop.
nonce/seed wip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 2f4e116592e39d9597a5abcf7b628b62a4d6c59f Mon Sep 17 00:00:00 2001 | |
From: Michael Vines <mvines@gmail.com> | |
Date: Mon, 28 Sep 2020 11:47:38 -0700 | |
Subject: [PATCH] wip | |
--- | |
sdk/src/pubkey.rs | 34 +++++++++++++++++++--------------- | |
1 file changed, 19 insertions(+), 15 deletions(-) | |
diff --git a/sdk/src/pubkey.rs b/sdk/src/pubkey.rs | |
index 1148ae4db..d1fba2081 100644 | |
--- a/sdk/src/pubkey.rs | |
+++ b/sdk/src/pubkey.rs | |
@@ -102,7 +102,7 @@ impl Pubkey { | |
/// Create a program address, valid program address must not be on the | |
/// ed25519 curve | |
pub fn create_program_address( | |
- seeds: &[&[u8]], | |
+ seed: &[u8], | |
program_id: &Pubkey, | |
) -> Result<Pubkey, PubkeyError> { | |
// Perform the calculation inline, calling this from within a program is | |
@@ -110,12 +110,10 @@ impl Pubkey { | |
#[cfg(not(all(feature = "program", target_arch = "bpf")))] | |
{ | |
let mut hasher = Hasher::default(); | |
- for seed in seeds.iter() { | |
- if seed.len() > MAX_SEED_LEN { | |
- return Err(PubkeyError::MaxSeedLengthExceeded); | |
- } | |
- hasher.hash(seed); | |
+ if seed.len() > MAX_SEED_LEN { | |
+ return Err(PubkeyError::MaxSeedLengthExceeded); | |
} | |
+ hasher.hash(seed); | |
hasher.hashv(&[program_id.as_ref(), "ProgramDerivedAddress".as_ref()]); | |
let hash = hasher.result(); | |
@@ -142,8 +140,8 @@ impl Pubkey { | |
let mut bytes = [0; 32]; | |
let result = unsafe { | |
sol_create_program_address( | |
- seeds as *const _ as *const u8, | |
- seeds.len() as u64, | |
+ seed as *const _ as *const u8, // <-- fix me | |
+ 1, | |
program_id as *const _ as *const u8, | |
&mut bytes as *mut _ as *mut u8, | |
) | |
@@ -155,21 +153,27 @@ impl Pubkey { | |
} | |
} | |
+ pub fn create_program_address_with_nonce( | |
+ seed: &[u8], | |
+ nonce: u8, | |
+ program_id: &Pubkey, | |
+ ) -> Result<Pubkey, PubkeyError> { | |
+ // TODO | |
+ } | |
+ | |
/// Find a valid program address and its corresponding nonce which must be passed | |
/// as an additional seed when calling `create_program_address` | |
// #[cfg(not(feature = "program"))] | |
#[allow(clippy::same_item_push)] | |
- pub fn find_program_address(seeds: &[&[u8]], program_id: &Pubkey) -> (Pubkey, u8) { | |
- let mut nonce = [std::u8::MAX]; | |
+ pub fn find_program_address(seed: &[u8], program_id: &Pubkey) -> (Pubkey, u8) { | |
+ let mut nonce = std::u8::MAX; | |
for _ in 0..std::u8::MAX { | |
{ | |
- let mut seeds_with_nonce = seeds.to_vec(); | |
- seeds_with_nonce.push(&nonce); | |
- if let Ok(address) = Self::create_program_address(&seeds_with_nonce, program_id) { | |
- return (address, nonce[0]); | |
+ if let Ok(address) = Self::create_program_address_with_nonce(&seed, nonce, program_id) { | |
+ return (address, nonce); | |
} | |
} | |
- nonce[0] -= 1; | |
+ nonce -= 1; | |
} | |
panic!("Unable to find a viable program address nonce"); | |
} | |
-- | |
2.24.3 (Apple Git-128) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment