Add create with seed to cli (#7713)

* Add create with seed to cli

* nonce and vote, too
This commit is contained in:
Rob Walker
2020-01-09 15:22:48 -08:00
committed by GitHub
parent 719785a8d3
commit 6775e83420
8 changed files with 376 additions and 62 deletions

View File

@@ -137,6 +137,7 @@ pub enum CliCommand {
},
CreateNonceAccount {
nonce_account: KeypairEq,
seed: Option<String>,
nonce_authority: Option<Pubkey>,
lamports: u64,
},
@@ -160,6 +161,7 @@ pub enum CliCommand {
// Stake Commands
CreateStakeAccount {
stake_account: KeypairEq,
seed: Option<String>,
staker: Option<Pubkey>,
withdrawer: Option<Pubkey>,
lockup: Lockup,
@@ -214,6 +216,7 @@ pub enum CliCommand {
// Vote Commands
CreateVoteAccount {
vote_account: KeypairEq,
seed: Option<String>,
node_pubkey: Pubkey,
authorized_voter: Option<Pubkey>,
authorized_withdrawer: Option<Pubkey>,
@@ -1201,12 +1204,14 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
// Create nonce account
CliCommand::CreateNonceAccount {
nonce_account,
seed,
nonce_authority,
lamports,
} => process_create_nonce_account(
&rpc_client,
config,
nonce_account,
seed.clone(),
*nonce_authority,
*lamports,
),
@@ -1256,6 +1261,7 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
// Create stake account
CliCommand::CreateStakeAccount {
stake_account,
seed,
staker,
withdrawer,
lockup,
@@ -1264,6 +1270,7 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
&rpc_client,
config,
stake_account,
seed,
staker,
withdrawer,
lockup,
@@ -1401,6 +1408,7 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
// Create vote account
CliCommand::CreateVoteAccount {
vote_account,
seed,
node_pubkey,
authorized_voter,
authorized_withdrawer,
@@ -1409,6 +1417,7 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
&rpc_client,
config,
vote_account,
seed,
&node_pubkey,
authorized_voter,
authorized_withdrawer,
@@ -2536,6 +2545,7 @@ mod tests {
let node_pubkey = Pubkey::new_rand();
config.command = CliCommand::CreateVoteAccount {
vote_account: bob_keypair.into(),
seed: None,
node_pubkey,
authorized_voter: Some(bob_pubkey),
authorized_withdrawer: Some(bob_pubkey),
@@ -2567,6 +2577,7 @@ mod tests {
let custodian = Pubkey::new_rand();
config.command = CliCommand::CreateStakeAccount {
stake_account: bob_keypair.into(),
seed: None,
staker: None,
withdrawer: None,
lockup: Lockup {
@@ -2774,6 +2785,7 @@ mod tests {
let bob_keypair = Keypair::new();
config.command = CliCommand::CreateVoteAccount {
vote_account: bob_keypair.into(),
seed: None,
node_pubkey,
authorized_voter: Some(bob_pubkey),
authorized_withdrawer: Some(bob_pubkey),

View File

@@ -14,8 +14,8 @@ use solana_sdk::{
pubkey::Pubkey,
signature::{Keypair, KeypairUtil},
system_instruction::{
create_nonce_account, nonce_advance, nonce_authorize, nonce_withdraw, NonceError,
SystemError,
create_address_with_seed, create_nonce_account, create_nonce_account_with_seed,
nonce_advance, nonce_authorize, nonce_withdraw, NonceError, SystemError,
},
system_program,
transaction::Transaction,
@@ -81,6 +81,13 @@ impl NonceSubCommands for App<'_, '_> {
.validator(is_pubkey_or_keypair)
.help("Account to be granted authority of the nonce account"),
)
.arg(
Arg::with_name("seed")
.long("seed")
.value_name("SEED STRING")
.takes_value(true)
.help("Seed for address generation; if specified, the resulting account will be at a derived address of the NONCE_ACCOUNT pubkey")
)
.arg(nonce_authority_arg()),
)
.subcommand(
@@ -227,12 +234,14 @@ pub fn parse_authorize_nonce_account(matches: &ArgMatches<'_>) -> Result<CliComm
pub fn parse_nonce_create_account(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
let nonce_account = keypair_of(matches, "nonce_account_keypair").unwrap();
let seed = matches.value_of("seed").map(|s| s.to_string());
let lamports = required_lamports_from(matches, "amount", "unit")?;
let nonce_authority = pubkey_of(matches, "nonce_authority");
Ok(CliCommandInfo {
command: CliCommand::CreateNonceAccount {
nonce_account: nonce_account.into(),
seed,
nonce_authority,
lamports,
},
@@ -354,16 +363,23 @@ pub fn process_create_nonce_account(
rpc_client: &RpcClient,
config: &CliConfig,
nonce_account: &Keypair,
seed: Option<String>,
nonce_authority: Option<Pubkey>,
lamports: u64,
) -> ProcessResult {
let nonce_account_pubkey = nonce_account.pubkey();
let nonce_account_address = if let Some(seed) = seed.clone() {
create_address_with_seed(&nonce_account_pubkey, &seed, &system_program::id())?
} else {
nonce_account_pubkey
};
check_unique_pubkeys(
(&config.keypair.pubkey(), "cli keypair".to_string()),
(&nonce_account_pubkey, "nonce_account_pubkey".to_string()),
(&nonce_account_address, "nonce_account".to_string()),
)?;
if rpc_client.get_account(&nonce_account_pubkey).is_ok() {
if rpc_client.get_account(&nonce_account_address).is_ok() {
return Err(CliError::BadParameter(format!(
"Unable to create nonce account. Nonce account already exists: {}",
nonce_account_pubkey,
@@ -381,17 +397,37 @@ pub fn process_create_nonce_account(
}
let nonce_authority = nonce_authority.unwrap_or_else(|| config.keypair.pubkey());
let ixs = create_nonce_account(
&config.keypair.pubkey(),
&nonce_account_pubkey,
&nonce_authority,
lamports,
);
let ixs = if let Some(seed) = seed {
create_nonce_account_with_seed(
&config.keypair.pubkey(), // from
&nonce_account_address, // to
&nonce_account_pubkey, // base
&seed, // seed
&nonce_authority,
lamports,
)
} else {
create_nonce_account(
&config.keypair.pubkey(),
&nonce_account_pubkey,
&nonce_authority,
lamports,
)
};
let (recent_blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
let signers = if nonce_account_pubkey != config.keypair.pubkey() {
vec![&config.keypair, nonce_account] // both must sign if `from` and `to` differ
} else {
vec![&config.keypair] // when stake_account == config.keypair and there's a seed, we only need one signature
};
let mut tx = Transaction::new_signed_with_payer(
ixs,
Some(&config.keypair.pubkey()),
&[&config.keypair, nonce_account],
&signers,
recent_blockhash,
);
check_account_for_fee(
@@ -400,8 +436,7 @@ pub fn process_create_nonce_account(
&fee_calculator,
&tx.message,
)?;
let result =
rpc_client.send_and_confirm_transaction(&mut tx, &[&config.keypair, nonce_account]);
let result = rpc_client.send_and_confirm_transaction(&mut tx, &signers);
log_instruction_custom_error::<SystemError>(result)
}
@@ -626,6 +661,7 @@ mod tests {
CliCommandInfo {
command: CliCommand::CreateNonceAccount {
nonce_account: read_keypair_file(&keypair_file).unwrap().into(),
seed: None,
nonce_authority: None,
lamports: 50,
},
@@ -648,6 +684,7 @@ mod tests {
CliCommandInfo {
command: CliCommand::CreateNonceAccount {
nonce_account: read_keypair_file(&keypair_file).unwrap().into(),
seed: None,
nonce_authority: Some(
read_keypair_file(&authority_keypair_file).unwrap().pubkey()
),

View File

@@ -17,7 +17,7 @@ use solana_sdk::{
hash::Hash,
pubkey::Pubkey,
signature::KeypairUtil,
system_instruction::SystemError,
system_instruction::{create_address_with_seed, SystemError},
sysvar::{
stake_history::{self, StakeHistory},
Sysvar,
@@ -75,6 +75,13 @@ impl StakeSubCommands for App<'_, '_> {
.validator(is_pubkey_or_keypair)
.help("Identity of the custodian (can withdraw before lockup expires)")
)
.arg(
Arg::with_name("seed")
.long("seed")
.value_name("SEED STRING")
.takes_value(true)
.help("Seed for address generation; if specified, the resulting account will be at a derived address of the STAKE ACCOUNT pubkey")
)
.arg(
Arg::with_name("lockup_epoch")
.long("lockup-epoch")
@@ -367,6 +374,7 @@ impl StakeSubCommands for App<'_, '_> {
pub fn parse_stake_create_account(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
let stake_account = keypair_of(matches, "stake_account").unwrap();
let seed = matches.value_of("seed").map(|s| s.to_string());
let epoch = value_of(&matches, "lockup_epoch").unwrap_or(0);
let unix_timestamp = unix_timestamp_from_rfc3339_datetime(&matches, "lockup_date").unwrap_or(0);
let custodian = pubkey_of(matches, "custodian").unwrap_or_default();
@@ -377,6 +385,7 @@ pub fn parse_stake_create_account(matches: &ArgMatches<'_>) -> Result<CliCommand
Ok(CliCommandInfo {
command: CliCommand::CreateStakeAccount {
stake_account: stake_account.into(),
seed,
staker,
withdrawer,
lockup: Lockup {
@@ -516,21 +525,27 @@ pub fn process_create_stake_account(
rpc_client: &RpcClient,
config: &CliConfig,
stake_account: &Keypair,
seed: &Option<String>,
staker: &Option<Pubkey>,
withdrawer: &Option<Pubkey>,
lockup: &Lockup,
lamports: u64,
) -> ProcessResult {
let stake_account_pubkey = stake_account.pubkey();
let stake_account_address = if let Some(seed) = seed {
create_address_with_seed(&stake_account_pubkey, &seed, &solana_stake_program::id())?
} else {
stake_account_pubkey
};
check_unique_pubkeys(
(&config.keypair.pubkey(), "cli keypair".to_string()),
(&stake_account_pubkey, "stake_account_pubkey".to_string()),
(&stake_account_address, "stake_account".to_string()),
)?;
if rpc_client.get_account(&stake_account_pubkey).is_ok() {
if rpc_client.get_account(&stake_account_address).is_ok() {
return Err(CliError::BadParameter(format!(
"Unable to create stake account. Stake account already exists: {}",
stake_account_pubkey
stake_account_address
))
.into());
}
@@ -551,18 +566,37 @@ pub fn process_create_stake_account(
withdrawer: withdrawer.unwrap_or(config.keypair.pubkey()),
};
let ixs = stake_instruction::create_account(
&config.keypair.pubkey(),
&stake_account_pubkey,
&authorized,
lockup,
lamports,
);
let ixs = if let Some(seed) = seed {
stake_instruction::create_account_with_seed(
&config.keypair.pubkey(), // from
&stake_account_address, // to
&stake_account_pubkey, // base
seed, // seed
&authorized,
lockup,
lamports,
)
} else {
stake_instruction::create_account(
&config.keypair.pubkey(),
&stake_account_pubkey,
&authorized,
lockup,
lamports,
)
};
let (recent_blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
let signers = if stake_account_pubkey != config.keypair.pubkey() {
vec![&config.keypair, stake_account] // both must sign if `from` and `to` differ
} else {
vec![&config.keypair] // when stake_account == config.keypair and there's a seed, we only need one signature
};
let mut tx = Transaction::new_signed_with_payer(
ixs,
Some(&config.keypair.pubkey()),
&[&config.keypair, stake_account],
&signers,
recent_blockhash,
);
check_account_for_fee(
@@ -571,8 +605,7 @@ pub fn process_create_stake_account(
&fee_calculator,
&tx.message,
)?;
let result =
rpc_client.send_and_confirm_transaction(&mut tx, &[&config.keypair, stake_account]);
let result = rpc_client.send_and_confirm_transaction(&mut tx, &signers);
log_instruction_custom_error::<SystemError>(result)
}
@@ -1023,6 +1056,7 @@ mod tests {
CliCommandInfo {
command: CliCommand::CreateStakeAccount {
stake_account: stake_account_keypair.into(),
seed: None,
staker: Some(authorized),
withdrawer: Some(authorized),
lockup: Lockup {
@@ -1055,6 +1089,7 @@ mod tests {
CliCommandInfo {
command: CliCommand::CreateStakeAccount {
stake_account: stake_account_keypair.into(),
seed: None,
staker: None,
withdrawer: None,
lockup: Lockup::default(),

View File

@@ -11,7 +11,10 @@ use solana_clap_utils::{input_parsers::*, input_validators::*};
use solana_client::rpc_client::RpcClient;
use solana_sdk::signature::Keypair;
use solana_sdk::{
account::Account, pubkey::Pubkey, signature::KeypairUtil, system_instruction::SystemError,
account::Account,
pubkey::Pubkey,
signature::KeypairUtil,
system_instruction::{create_address_with_seed, SystemError},
transaction::Transaction,
};
use solana_vote_program::{
@@ -69,6 +72,13 @@ impl VoteSubCommands for App<'_, '_> {
.takes_value(true)
.validator(is_pubkey_or_keypair)
.help("Public key of the authorized withdrawer (defaults to cli config pubkey)"),
)
.arg(
Arg::with_name("seed")
.long("seed")
.value_name("SEED STRING")
.takes_value(true)
.help("Seed for address generation; if specified, the resulting account will be at a derived address of the VOTE ACCOUNT pubkey")
),
)
.subcommand(
@@ -195,6 +205,7 @@ impl VoteSubCommands for App<'_, '_> {
pub fn parse_vote_create_account(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
let vote_account = keypair_of(matches, "vote_account").unwrap();
let seed = matches.value_of("seed").map(|s| s.to_string());
let identity_pubkey = pubkey_of(matches, "identity_pubkey").unwrap();
let commission = value_t_or_exit!(matches, "commission", u8);
let authorized_voter = pubkey_of(matches, "authorized_voter");
@@ -203,6 +214,7 @@ pub fn parse_vote_create_account(matches: &ArgMatches<'_>) -> Result<CliCommandI
Ok(CliCommandInfo {
command: CliCommand::CreateVoteAccount {
vote_account: vote_account.into(),
seed,
node_pubkey: identity_pubkey,
authorized_voter,
authorized_withdrawer,
@@ -280,21 +292,36 @@ pub fn process_create_vote_account(
rpc_client: &RpcClient,
config: &CliConfig,
vote_account: &Keypair,
seed: &Option<String>,
identity_pubkey: &Pubkey,
authorized_voter: &Option<Pubkey>,
authorized_withdrawer: &Option<Pubkey>,
commission: u8,
) -> ProcessResult {
let vote_account_pubkey = vote_account.pubkey();
check_unique_pubkeys(
(&vote_account_pubkey, "vote_account_pubkey".to_string()),
(&identity_pubkey, "identity_pubkey".to_string()),
)?;
let vote_account_address = if let Some(seed) = seed {
create_address_with_seed(&vote_account_pubkey, &seed, &solana_vote_program::id())?
} else {
vote_account_pubkey
};
check_unique_pubkeys(
(&config.keypair.pubkey(), "cli keypair".to_string()),
(&vote_account_pubkey, "vote_account_pubkey".to_string()),
(&vote_account_address, "vote_account".to_string()),
)?;
check_unique_pubkeys(
(&vote_account_address, "vote_account".to_string()),
(&identity_pubkey, "identity_pubkey".to_string()),
)?;
if rpc_client.get_account(&vote_account_address).is_ok() {
return Err(CliError::BadParameter(format!(
"Unable to create vote account. Vote account already exists: {}",
vote_account_address
))
.into());
}
let required_balance = rpc_client
.get_minimum_balance_for_rent_exemption(VoteState::size_of())?
.max(1);
@@ -302,28 +329,43 @@ pub fn process_create_vote_account(
let vote_init = VoteInit {
node_pubkey: *identity_pubkey,
authorized_voter: authorized_voter.unwrap_or(vote_account_pubkey),
authorized_withdrawer: authorized_withdrawer.unwrap_or(config.keypair.pubkey()),
authorized_withdrawer: authorized_withdrawer.unwrap_or(vote_account_pubkey),
commission,
};
let ixs = vote_instruction::create_account(
&config.keypair.pubkey(),
&vote_account_pubkey,
&vote_init,
required_balance,
);
let ixs = if let Some(seed) = seed {
vote_instruction::create_account_with_seed(
&config.keypair.pubkey(), // from
&vote_account_address, // to
&vote_account_pubkey, // base
seed, // seed
&vote_init,
required_balance,
)
} else {
vote_instruction::create_account(
&config.keypair.pubkey(),
&vote_account_pubkey,
&vote_init,
required_balance,
)
};
let (recent_blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
let mut tx = Transaction::new_signed_instructions(
&[&config.keypair, vote_account],
ixs,
recent_blockhash,
);
let signers = if vote_account_pubkey != config.keypair.pubkey() {
vec![&config.keypair, vote_account] // both must sign if `from` and `to` differ
} else {
vec![&config.keypair] // when stake_account == config.keypair and there's a seed, we only need one signature
};
let mut tx = Transaction::new_signed_instructions(&signers, ixs, recent_blockhash);
check_account_for_fee(
rpc_client,
&config.keypair.pubkey(),
&fee_calculator,
&tx.message,
)?;
let result = rpc_client.send_and_confirm_transaction(&mut tx, &[&config.keypair, vote_account]);
let result = rpc_client.send_and_confirm_transaction(&mut tx, &signers);
log_instruction_custom_error::<SystemError>(result)
}
@@ -583,6 +625,7 @@ mod tests {
CliCommandInfo {
command: CliCommand::CreateVoteAccount {
vote_account: keypair.into(),
seed: None,
node_pubkey,
authorized_voter: None,
authorized_withdrawer: None,
@@ -607,6 +650,7 @@ mod tests {
CliCommandInfo {
command: CliCommand::CreateVoteAccount {
vote_account: keypair.into(),
seed: None,
node_pubkey,
authorized_voter: None,
authorized_withdrawer: None,
@@ -635,6 +679,7 @@ mod tests {
CliCommandInfo {
command: CliCommand::CreateVoteAccount {
vote_account: keypair.into(),
seed: None,
node_pubkey,
authorized_voter: Some(authed),
authorized_withdrawer: None,
@@ -661,6 +706,7 @@ mod tests {
CliCommandInfo {
command: CliCommand::CreateVoteAccount {
vote_account: keypair.into(),
seed: None,
node_pubkey,
authorized_voter: None,
authorized_withdrawer: Some(authed),