From 8d8f28c1d036e97dfb3962c4b7fc7f062b8a708c Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Mon, 10 Feb 2020 23:34:14 -0700 Subject: [PATCH] CLI: `transfer` fix checks pubkeys (#8198) automerge --- cli/src/cli.rs | 16 ++++++++------- cli/tests/transfer.rs | 45 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/cli/src/cli.rs b/cli/src/cli.rs index f17afc9154..cf0c771ade 100644 --- a/cli/src/cli.rs +++ b/cli/src/cli.rs @@ -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, )?; diff --git a/cli/tests/transfer.rs b/cli/tests/transfer.rs index 72deed5436..22b2115b96 100644 --- a/cli/tests/transfer.rs +++ b/cli/tests/transfer.rs @@ -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(); }