Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

deprecates Pubkey::new in favor of Pubkey::{,try_}from #29805

Merged
merged 1 commit into from
Jan 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
deprecates Pubkey::new in favor of Pubkey::{,try_}from
The commit deprecates Pubkey::new which lacks type-safety and instead
implements TryFrom<&[u8]> and TryFrom<Vec<u8>> for Pubkey.
behzadnouri committed Jan 21, 2023
commit 1c9483c726aa72cb86fe79239240cde9ced521a2
10 changes: 4 additions & 6 deletions account-decoder/src/parse_token.rs
Original file line number Diff line number Diff line change
@@ -282,11 +282,9 @@ pub struct UiMultisig {
}

pub fn get_token_account_mint(data: &[u8]) -> Option<Pubkey> {
if Account::valid_account_data(data) {
Some(Pubkey::new(&data[0..32]))
} else {
None
}
Account::valid_account_data(data)
.then(|| Pubkey::try_from(data.get(..32)?).ok())
.flatten()
}

#[cfg(test)]
@@ -402,7 +400,7 @@ mod test {
account.state = AccountState::Initialized;
Account::pack(account, &mut account_data).unwrap();

let expected_mint_pubkey = Pubkey::new(&[2; 32]);
let expected_mint_pubkey = Pubkey::from([2; 32]);
assert_eq!(
get_token_account_mint(&account_data),
Some(expected_mint_pubkey)
8 changes: 4 additions & 4 deletions cli-output/src/cli_output.rs
Original file line number Diff line number Diff line change
@@ -2926,10 +2926,10 @@ mod tests {
}

let present: Box<dyn Signer> = Box::new(keypair_from_seed(&[2u8; 32]).unwrap());
let absent: Box<dyn Signer> = Box::new(NullSigner::new(&Pubkey::new(&[3u8; 32])));
let bad: Box<dyn Signer> = Box::new(BadSigner::new(Pubkey::new(&[4u8; 32])));
let to = Pubkey::new(&[5u8; 32]);
let nonce = Pubkey::new(&[6u8; 32]);
let absent: Box<dyn Signer> = Box::new(NullSigner::new(&Pubkey::from([3u8; 32])));
let bad: Box<dyn Signer> = Box::new(BadSigner::new(Pubkey::from([4u8; 32])));
let to = Pubkey::from([5u8; 32]);
let nonce = Pubkey::from([6u8; 32]);
let from = present.pubkey();
let fee_payer = absent.pubkey();
let nonce_auth = bad.pubkey();
8 changes: 4 additions & 4 deletions cli/src/checks.rs
Original file line number Diff line number Diff line change
@@ -185,8 +185,8 @@ mod tests {
});
let pubkey = solana_sdk::pubkey::new_rand();

let pubkey0 = Pubkey::new(&[0; 32]);
let pubkey1 = Pubkey::new(&[1; 32]);
let pubkey0 = Pubkey::from([0; 32]);
let pubkey1 = Pubkey::from([1; 32]);
let ix0 = system_instruction::transfer(&pubkey0, &pubkey1, 1);
let message0 = Message::new(&[ix0], Some(&pubkey0));

@@ -290,8 +290,8 @@ mod tests {
assert_eq!(get_fee_for_messages(&rpc_client, &[]).unwrap(), 0);

// One message w/ one signature, a fee.
let pubkey0 = Pubkey::new(&[0; 32]);
let pubkey1 = Pubkey::new(&[1; 32]);
let pubkey0 = Pubkey::from([0; 32]);
let pubkey1 = Pubkey::from([1; 32]);
let ix0 = system_instruction::transfer(&pubkey0, &pubkey1, 1);
let message0 = Message::new(&[ix0], Some(&pubkey0));
assert_eq!(get_fee_for_messages(&rpc_client, &[&message0]).unwrap(), 1);
2 changes: 1 addition & 1 deletion cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -2563,7 +2563,7 @@ mod tests {
);

//Test Transfer Subcommand, with nonce
let nonce_address = Pubkey::new(&[1u8; 32]);
let nonce_address = Pubkey::from([1u8; 32]);
let nonce_address_string = nonce_address.to_string();
let nonce_authority = keypair_from_seed(&[2u8; 32]).unwrap();
let nonce_authority_file = make_tmp_path("nonce_authority_file");
2 changes: 1 addition & 1 deletion cli/src/memo.rs
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ impl WithMemo for Vec<Instruction> {
if let Some(memo) = &memo {
let memo = memo.as_ref();
let memo_ix = Instruction {
program_id: Pubkey::new(&id().to_bytes()),
program_id: Pubkey::from(id().to_bytes()),
accounts: vec![],
data: memo.as_bytes().to_vec(),
};
8 changes: 4 additions & 4 deletions cli/src/nonce.rs
Original file line number Diff line number Diff line change
@@ -1079,7 +1079,7 @@ mod tests {
let valid = Account::new_data(1, &data, &system_program::ID);
assert!(check_nonce_account(&valid.unwrap(), &nonce_pubkey, &blockhash).is_ok());

let invalid_owner = Account::new_data(1, &data, &Pubkey::new(&[1u8; 32]));
let invalid_owner = Account::new_data(1, &data, &Pubkey::from([1u8; 32]));
if let CliError::InvalidNonce(err) =
check_nonce_account(&invalid_owner.unwrap(), &nonce_pubkey, &blockhash).unwrap_err()
{
@@ -1151,7 +1151,7 @@ mod tests {
Err(Error::UnexpectedDataSize),
);

let other_program = Pubkey::new(&[1u8; 32]);
let other_program = Pubkey::from([1u8; 32]);
let other_account_no_data = Account::new(1, 0, &other_program);
assert_eq!(
account_identity_ok(&other_account_no_data),
@@ -1165,7 +1165,7 @@ mod tests {
assert_eq!(state_from_account(&nonce_account), Ok(State::Uninitialized));

let durable_nonce = DurableNonce::from_blockhash(&Hash::new(&[42u8; 32]));
let data = nonce::state::Data::new(Pubkey::new(&[1u8; 32]), durable_nonce, 42);
let data = nonce::state::Data::new(Pubkey::from([1u8; 32]), durable_nonce, 42);
nonce_account
.set_state(&Versions::new(State::Initialized(data.clone())))
.unwrap();
@@ -1195,7 +1195,7 @@ mod tests {
);

let durable_nonce = DurableNonce::from_blockhash(&Hash::new(&[42u8; 32]));
let data = nonce::state::Data::new(Pubkey::new(&[1u8; 32]), durable_nonce, 42);
let data = nonce::state::Data::new(Pubkey::from([1u8; 32]), durable_nonce, 42);
nonce_account
.set_state(&Versions::new(State::Initialized(data.clone())))
.unwrap();
10 changes: 5 additions & 5 deletions cli/src/stake.rs
Original file line number Diff line number Diff line change
@@ -2686,9 +2686,9 @@ mod tests {

// stake-authorize subcommand
let stake_account_string = stake_account_pubkey.to_string();
let new_stake_authority = Pubkey::new(&[1u8; 32]);
let new_stake_authority = Pubkey::from([1u8; 32]);
let new_stake_string = new_stake_authority.to_string();
let new_withdraw_authority = Pubkey::new(&[2u8; 32]);
let new_withdraw_authority = Pubkey::from([2u8; 32]);
let new_withdraw_string = new_withdraw_authority.to_string();
let test_stake_authorize = test_commands.clone().get_matches_from(vec![
"test",
@@ -3537,7 +3537,7 @@ mod tests {
let pubkey2 = keypair2.pubkey();
let sig2 = keypair.sign_message(&[0u8]);
let signer2 = format!("{}={}", keypair2.pubkey(), sig2);
let nonce_account = Pubkey::new(&[1u8; 32]);
let nonce_account = Pubkey::from([1u8; 32]);
let test_authorize = test_commands.clone().get_matches_from(vec![
"test",
"stake-authorize",
@@ -3914,7 +3914,7 @@ mod tests {
assert!(parse_command(&test_create_stake_account, &default_signer, &mut None).is_err());

// CreateStakeAccount offline and nonce
let nonce_account = Pubkey::new(&[1u8; 32]);
let nonce_account = Pubkey::from([1u8; 32]);
let nonce_account_string = nonce_account.to_string();
let offline = keypair_from_seed(&[2u8; 32]).unwrap();
let offline_pubkey = offline.pubkey();
@@ -4829,7 +4829,7 @@ mod tests {
);

// Split stake offline nonced submission
let nonce_account = Pubkey::new(&[1u8; 32]);
let nonce_account = Pubkey::from([1u8; 32]);
let nonce_account_string = nonce_account.to_string();
let nonce_auth = keypair_from_seed(&[2u8; 32]).unwrap();
let nonce_auth_pubkey = nonce_auth.pubkey();
2 changes: 1 addition & 1 deletion cli/tests/nonce.rs
Original file line number Diff line number Diff line change
@@ -256,7 +256,7 @@ fn test_create_account_with_seed() {

let offline_nonce_authority_signer = keypair_from_seed(&[1u8; 32]).unwrap();
let online_nonce_creator_signer = keypair_from_seed(&[2u8; 32]).unwrap();
let to_address = Pubkey::new(&[3u8; 32]);
let to_address = Pubkey::from([3u8; 32]);

// Setup accounts
let rpc_client =
10 changes: 5 additions & 5 deletions cli/tests/transfer.rs
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ fn test_transfer() {
config.signers = vec![&default_signer];

let sender_pubkey = config.signers[0].pubkey();
let recipient_pubkey = Pubkey::new(&[1u8; 32]);
let recipient_pubkey = Pubkey::from([1u8; 32]);

request_and_confirm_airdrop(&rpc_client, &config, &sender_pubkey, sol_to_lamports(5.0))
.unwrap();
@@ -336,7 +336,7 @@ fn test_transfer_multisession_signing() {
SocketAddrSpace::Unspecified,
);

let to_pubkey = Pubkey::new(&[1u8; 32]);
let to_pubkey = Pubkey::from([1u8; 32]);
let offline_from_signer = keypair_from_seed(&[2u8; 32]).unwrap();
let offline_fee_payer_signer = keypair_from_seed(&[3u8; 32]).unwrap();
let from_null_signer = NullSigner::new(&offline_from_signer.pubkey());
@@ -498,7 +498,7 @@ fn test_transfer_all() {
config.signers = vec![&default_signer];

let sender_pubkey = config.signers[0].pubkey();
let recipient_pubkey = Pubkey::new(&[1u8; 32]);
let recipient_pubkey = Pubkey::from([1u8; 32]);

request_and_confirm_airdrop(&rpc_client, &config, &sender_pubkey, 500_000).unwrap();
check_balance!(500_000, &rpc_client, &sender_pubkey);
@@ -552,7 +552,7 @@ fn test_transfer_unfunded_recipient() {
config.signers = vec![&default_signer];

let sender_pubkey = config.signers[0].pubkey();
let recipient_pubkey = Pubkey::new(&[1u8; 32]);
let recipient_pubkey = Pubkey::from([1u8; 32]);

request_and_confirm_airdrop(&rpc_client, &config, &sender_pubkey, 50_000).unwrap();
check_balance!(50_000, &rpc_client, &sender_pubkey);
@@ -607,7 +607,7 @@ fn test_transfer_with_seed() {
config.signers = vec![&default_signer];

let sender_pubkey = config.signers[0].pubkey();
let recipient_pubkey = Pubkey::new(&[1u8; 32]);
let recipient_pubkey = Pubkey::from([1u8; 32]);
let derived_address_seed = "seed".to_string();
let derived_address_program_id = stake::program::id();
let derived_address = Pubkey::create_with_seed(
4 changes: 2 additions & 2 deletions faucet/src/faucet.rs
Original file line number Diff line number Diff line change
@@ -203,7 +203,7 @@ impl Faucet {
)
);
let memo_instruction = Instruction {
program_id: Pubkey::new(&spl_memo::id().to_bytes()),
program_id: Pubkey::from(spl_memo::id().to_bytes()),
accounts: vec![],
data: memo.as_bytes().to_vec(),
};
@@ -633,7 +633,7 @@ mod tests {
assert_eq!(tx.signatures.len(), 1);
assert_eq!(
message.account_keys,
vec![mint_pubkey, Pubkey::new(&spl_memo::id().to_bytes())]
vec![mint_pubkey, Pubkey::from(spl_memo::id().to_bytes())]
);
assert_eq!(message.recent_blockhash, blockhash);

2 changes: 1 addition & 1 deletion genesis/src/stakes.rs
Original file line number Diff line number Diff line change
@@ -216,7 +216,7 @@ mod tests {
// print(
// "\n\"{}\", // {:?}",
// hex,
// Pubkey::new(&hex::decode(hex).unwrap())
// Pubkey::try_from(&hex::decode(hex).unwrap()).unwrap()
// );
// });
// println();
10 changes: 5 additions & 5 deletions gossip/src/cluster_info.rs
Original file line number Diff line number Diff line change
@@ -4188,7 +4188,7 @@ RPC Enabled Nodes: 1"#;

#[test]
fn test_tvu_peers_and_stakes() {
let d = ContactInfo::new_localhost(&Pubkey::new(&[0; 32]), timestamp());
let d = ContactInfo::new_localhost(&Pubkey::from([0; 32]), timestamp());
let cluster_info = ClusterInfo::new(
d.clone(),
Arc::new(Keypair::new()),
@@ -4197,12 +4197,12 @@ RPC Enabled Nodes: 1"#;
let mut stakes = HashMap::new();

// no stake
let id = Pubkey::new(&[1u8; 32]);
let id = Pubkey::from([1u8; 32]);
let contact_info = ContactInfo::new_localhost(&id, timestamp());
cluster_info.insert_info(contact_info);

// normal
let id2 = Pubkey::new(&[2u8; 32]);
let id2 = Pubkey::from([2u8; 32]);
let mut contact_info = ContactInfo::new_localhost(&id2, timestamp());
cluster_info.insert_info(contact_info.clone());
stakes.insert(id2, 10);
@@ -4212,14 +4212,14 @@ RPC Enabled Nodes: 1"#;
cluster_info.insert_info(contact_info);

// no tvu
let id3 = Pubkey::new(&[3u8; 32]);
let id3 = Pubkey::from([3u8; 32]);
let mut contact_info = ContactInfo::new_localhost(&id3, timestamp());
contact_info.tvu = "0.0.0.0:0".parse().unwrap();
cluster_info.insert_info(contact_info);
stakes.insert(id3, 10);

// normal but with different shred version
let id4 = Pubkey::new(&[4u8; 32]);
let id4 = Pubkey::from([4u8; 32]);
let mut contact_info = ContactInfo::new_localhost(&id4, timestamp());
contact_info.shred_version = 1;
assert_ne!(contact_info.shred_version, d.shred_version);
6 changes: 3 additions & 3 deletions gossip/src/crds_gossip.rs
Original file line number Diff line number Diff line change
@@ -435,8 +435,8 @@ mod test {
let crds_gossip = CrdsGossip::default();
let keypair = Keypair::new();
let id = keypair.pubkey();
let ci = ContactInfo::new_localhost(&Pubkey::new(&[1; 32]), 0);
let prune_pubkey = Pubkey::new(&[2; 32]);
let ci = ContactInfo::new_localhost(&Pubkey::from([1; 32]), 0);
let prune_pubkey = Pubkey::from([2; 32]);
crds_gossip
.crds
.write()
@@ -467,7 +467,7 @@ mod test {
let mut res = crds_gossip.process_prune_msg(
&id,
&ci.id,
&Pubkey::new(hash(&[1; 32]).as_ref()),
&Pubkey::from(hash(&[1; 32]).to_bytes()),
&[prune_pubkey],
now,
now,
12 changes: 6 additions & 6 deletions gossip/tests/crds_gossip.rs
Original file line number Diff line number Diff line change
@@ -732,8 +732,8 @@ fn test_prune_errors() {
let crds_gossip = CrdsGossip::default();
let keypair = Keypair::new();
let id = keypair.pubkey();
let ci = ContactInfo::new_localhost(&Pubkey::new(&[1; 32]), 0);
let prune_pubkey = Pubkey::new(&[2; 32]);
let ci = ContactInfo::new_localhost(&Pubkey::from([1; 32]), 0);
let prune_pubkey = Pubkey::from([2; 32]);
crds_gossip
.crds
.write()
@@ -758,10 +758,10 @@ fn test_prune_errors() {
let stakes = HashMap::<Pubkey, u64>::default();
//incorrect dest
let mut res = crds_gossip.process_prune_msg(
&id, // self_pubkey
&ci.id, // peer
&Pubkey::new(hash(&[1; 32]).as_ref()), // destination
&[prune_pubkey], // origins
&id, // self_pubkey
&ci.id, // peer
&Pubkey::from(hash(&[1; 32]).to_bytes()), // destination
&[prune_pubkey], // origins
now,
now,
&stakes,
8 changes: 4 additions & 4 deletions ledger/src/blockstore.rs
Original file line number Diff line number Diff line change
@@ -6979,8 +6979,8 @@ pub mod tests {
.write_transaction_status(
slot0,
Signature::new(&random_bytes),
vec![&Pubkey::new(&random_bytes[0..32])],
vec![&Pubkey::new(&random_bytes[32..])],
vec![&Pubkey::try_from(&random_bytes[..32]).unwrap()],
vec![&Pubkey::try_from(&random_bytes[32..]).unwrap()],
TransactionStatusMeta::default(),
)
.unwrap();
@@ -7045,8 +7045,8 @@ pub mod tests {
.write_transaction_status(
slot1,
Signature::new(&random_bytes),
vec![&Pubkey::new(&random_bytes[0..32])],
vec![&Pubkey::new(&random_bytes[32..])],
vec![&Pubkey::try_from(&random_bytes[..32]).unwrap()],
vec![&Pubkey::try_from(&random_bytes[32..]).unwrap()],
TransactionStatusMeta::default(),
)
.unwrap();
20 changes: 10 additions & 10 deletions ledger/src/blockstore/blockstore_purge.rs
Original file line number Diff line number Diff line change
@@ -476,8 +476,8 @@ pub mod tests {
.write_transaction_status(
x,
Signature::new(&random_bytes),
vec![&Pubkey::new(&random_bytes[0..32])],
vec![&Pubkey::new(&random_bytes[32..])],
vec![&Pubkey::try_from(&random_bytes[..32]).unwrap()],
vec![&Pubkey::try_from(&random_bytes[32..]).unwrap()],
TransactionStatusMeta::default(),
)
.unwrap();
@@ -491,8 +491,8 @@ pub mod tests {
.write_transaction_status(
x,
Signature::new(&random_bytes),
vec![&Pubkey::new(&random_bytes[0..32])],
vec![&Pubkey::new(&random_bytes[32..])],
vec![&Pubkey::try_from(&random_bytes[..32]).unwrap()],
vec![&Pubkey::try_from(&random_bytes[32..]).unwrap()],
TransactionStatusMeta::default(),
)
.unwrap();
@@ -526,8 +526,8 @@ pub mod tests {
.write_transaction_status(
slot,
Signature::new(&random_bytes),
vec![&Pubkey::new(&random_bytes[0..32])],
vec![&Pubkey::new(&random_bytes[32..])],
vec![&Pubkey::try_from(&random_bytes[..32]).unwrap()],
vec![&Pubkey::try_from(&random_bytes[32..]).unwrap()],
TransactionStatusMeta::default(),
)
.unwrap();
@@ -726,8 +726,8 @@ pub mod tests {
.write_transaction_status(
x,
signature,
vec![&Pubkey::new(&random_bytes[0..32])],
vec![&Pubkey::new(&random_bytes[32..])],
vec![&Pubkey::try_from(&random_bytes[..32]).unwrap()],
vec![&Pubkey::try_from(&random_bytes[32..]).unwrap()],
TransactionStatusMeta::default(),
)
.unwrap();
@@ -769,8 +769,8 @@ pub mod tests {
.write_transaction_status(
x,
signature,
vec![&Pubkey::new(&random_bytes[0..32])],
vec![&Pubkey::new(&random_bytes[32..])],
vec![&Pubkey::try_from(&random_bytes[..32]).unwrap()],
vec![&Pubkey::try_from(&random_bytes[32..]).unwrap()],
TransactionStatusMeta::default(),
)
.unwrap();
Loading