CLI: `transfer` fix checks pubkeys (#8198)

automerge
This commit is contained in:
Trent Nelson 2020-02-10 23:34:14 -07:00 committed by GitHub
parent df782b93ae
commit 8d8f28c1d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 7 deletions

View File

@ -1316,10 +1316,12 @@ fn process_transfer(
nonce_authority: Option<&SigningAuthority>,
fee_payer: Option<&SigningAuthority>,
) -> ProcessResult {
let from = from.map(|f| f.keypair()).unwrap_or(&config.keypair);
let (from_pubkey, from) = from
.map(|f| (f.pubkey(), f.keypair()))
.unwrap_or((config.keypair.pubkey(), &config.keypair));
check_unique_pubkeys(
(&from.pubkey(), "cli keypair".to_string()),
(&from_pubkey, "cli keypair".to_string()),
(to, "to".to_string()),
)?;
@ -1327,9 +1329,9 @@ fn process_transfer(
blockhash_query.get_blockhash_fee_calculator(rpc_client)?;
let ixs = vec![system_instruction::transfer(&from.pubkey(), to, lamports)];
let nonce_authority: &Keypair = nonce_authority
.map(|authority| authority.keypair())
.unwrap_or(&config.keypair);
let (nonce_authority_pubkey, nonce_authority) = nonce_authority
.map(|authority| (authority.pubkey(), authority.keypair()))
.unwrap_or((config.keypair.pubkey(), &config.keypair));
let fee_payer = fee_payer.map(|fp| fp.keypair()).unwrap_or(&config.keypair);
let mut tx = if let Some(nonce_account) = &nonce_account {
Transaction::new_signed_with_nonce(
@ -1358,11 +1360,11 @@ fn process_transfer(
} else {
if let Some(nonce_account) = &nonce_account {
let nonce_account = rpc_client.get_account(nonce_account)?;
check_nonce_account(&nonce_account, &nonce_authority.pubkey(), &recent_blockhash)?;
check_nonce_account(&nonce_account, &nonce_authority_pubkey, &recent_blockhash)?;
}
check_account_for_fee(
rpc_client,
&fee_payer.pubkey(),
&tx.message.account_keys[0],
&fee_calculator,
&tx.message,
)?;

View File

@ -163,6 +163,51 @@ fn test_transfer() {
};
assert_ne!(nonce_hash, new_nonce_hash);
// Assign nonce authority to offline
config.command = CliCommand::AuthorizeNonceAccount {
nonce_account: nonce_account.pubkey(),
nonce_authority: None,
new_authority: offline_pubkey,
};
process_command(&config).unwrap();
// Fetch nonce hash
let account = rpc_client.get_account(&nonce_account.pubkey()).unwrap();
let nonce_state: NonceState = account.state().unwrap();
let nonce_hash = match nonce_state {
NonceState::Initialized(_meta, hash) => hash,
_ => panic!("Nonce is not initialized"),
};
// Offline, nonced transfer
offline.command = CliCommand::Transfer {
lamports: 10,
to: recipient_pubkey,
from: None,
sign_only: true,
signers: None,
blockhash_query: BlockhashQuery::None(nonce_hash, FeeCalculator::default()),
nonce_account: Some(nonce_account.pubkey()),
nonce_authority: None,
fee_payer: None,
};
let sign_only_reply = process_command(&offline).unwrap();
let (blockhash, signers) = parse_sign_only_reply_string(&sign_only_reply);
config.command = CliCommand::Transfer {
lamports: 10,
to: recipient_pubkey,
from: Some(offline_pubkey.into()),
sign_only: false,
signers: Some(signers),
blockhash_query: BlockhashQuery::FeeCalculator(blockhash),
nonce_account: Some(nonce_account.pubkey()),
nonce_authority: Some(offline_pubkey.into()),
fee_payer: Some(offline_pubkey.into()),
};
process_command(&config).unwrap();
check_balance(30, &rpc_client, &offline_pubkey);
check_balance(40, &rpc_client, &recipient_pubkey);
server.close().unwrap();
remove_dir_all(ledger_path).unwrap();
}