Solana-wallet: prevent duplicate pubkeys (#5497)

* Add helper function to compare wallet pubkey args for uniqueness

* Fix test
This commit is contained in:
Tyera Eulberg 2019-08-12 14:01:55 -06:00 committed by GitHub
parent c68ebbb0a6
commit b6151b5200
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 1 deletions

View File

@ -435,6 +435,20 @@ fn check_account_for_multiple_fees(
Err(WalletError::InsufficientFundsForFee)?
}
fn check_unique_pubkeys(
pubkey0: (&Pubkey, String),
pubkey1: (&Pubkey, String),
) -> Result<(), WalletError> {
if pubkey0.0 == pubkey1.0 {
Err(WalletError::BadParameter(format!(
"Identical pubkeys found: `{}` and `{}` must be unique",
pubkey0.1, pubkey1.1
)))
} else {
Ok(())
}
}
fn process_fees(rpc_client: &RpcClient) -> ProcessResult {
let (recent_blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
@ -522,6 +536,10 @@ fn process_create_vote_account(
commission: u8,
lamports: u64,
) -> ProcessResult {
check_unique_pubkeys(
(vote_account_pubkey, "vote_account_pubkey".to_string()),
(node_pubkey, "node_pubkey".to_string()),
)?;
let ixs = vote_instruction::create_account(
&config.keypair.pubkey(),
vote_account_pubkey,
@ -544,6 +562,13 @@ fn process_authorize_voter(
authorized_voter_keypair: &Keypair,
new_authorized_voter_pubkey: &Pubkey,
) -> ProcessResult {
check_unique_pubkeys(
(vote_account_pubkey, "vote_account_pubkey".to_string()),
(
new_authorized_voter_pubkey,
"new_authorized_voter_pubkey".to_string(),
),
)?;
let (recent_blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
let ixs = vec![vote_instruction::authorize_voter(
vote_account_pubkey, // vote account to update
@ -2334,7 +2359,9 @@ mod tests {
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
let bob_keypair = Keypair::new();
config.command = WalletCommand::AuthorizeVoter(bob_pubkey, bob_keypair, bob_pubkey);
let new_authorized_voter_pubkey = Pubkey::new_rand();
config.command =
WalletCommand::AuthorizeVoter(bob_pubkey, bob_keypair, new_authorized_voter_pubkey);
let signature = process_command(&config);
assert_eq!(signature.unwrap(), SIGNATURE.to_string());