2019-12-10 00:24:44 -08:00
|
|
|
use crate::cli::{
|
2020-02-24 16:03:30 -08:00
|
|
|
build_balance_message, check_account_for_fee, check_unique_pubkeys, generate_unique_signers,
|
2020-02-15 11:53:52 -08:00
|
|
|
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult,
|
2020-02-24 16:03:30 -08:00
|
|
|
SignerIndex,
|
2019-12-10 00:24:44 -08:00
|
|
|
};
|
|
|
|
use clap::{App, Arg, ArgMatches, SubCommand};
|
2020-02-21 13:55:53 -08:00
|
|
|
use solana_clap_utils::{
|
|
|
|
input_parsers::*, input_validators::*, offline::BLOCKHASH_ARG, ArgConstant,
|
|
|
|
};
|
2019-12-10 00:24:44 -08:00
|
|
|
use solana_client::rpc_client::RpcClient;
|
2020-02-24 16:03:30 -08:00
|
|
|
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
|
2019-12-10 00:24:44 -08:00
|
|
|
use solana_sdk::{
|
2019-12-27 12:35:49 -08:00
|
|
|
account::Account,
|
2020-01-22 17:54:06 -08:00
|
|
|
account_utils::StateMut,
|
2019-12-10 00:24:44 -08:00
|
|
|
hash::Hash,
|
2020-02-21 13:55:53 -08:00
|
|
|
message::Message,
|
2020-01-25 06:21:23 -08:00
|
|
|
nonce_state::{Meta, NonceState},
|
2019-12-10 00:24:44 -08:00
|
|
|
pubkey::Pubkey,
|
2020-01-03 16:34:58 -08:00
|
|
|
system_instruction::{
|
2020-01-22 15:31:39 -08:00
|
|
|
advance_nonce_account, authorize_nonce_account, create_address_with_seed,
|
|
|
|
create_nonce_account, create_nonce_account_with_seed, withdraw_nonce_account, NonceError,
|
|
|
|
SystemError,
|
2020-01-03 16:34:58 -08:00
|
|
|
},
|
|
|
|
system_program,
|
2019-12-10 00:24:44 -08:00
|
|
|
transaction::Transaction,
|
|
|
|
};
|
2020-02-24 16:03:30 -08:00
|
|
|
use std::sync::Arc;
|
2019-12-10 00:24:44 -08:00
|
|
|
|
2019-12-27 12:35:49 -08:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum CliNonceError {
|
|
|
|
InvalidAccountOwner,
|
|
|
|
InvalidAccountData,
|
|
|
|
InvalidHash,
|
|
|
|
InvalidAuthority,
|
|
|
|
InvalidState,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const NONCE_ARG: ArgConstant<'static> = ArgConstant {
|
|
|
|
name: "nonce",
|
|
|
|
long: "nonce",
|
|
|
|
help: "Provide the nonce account to use when creating a nonced \n\
|
|
|
|
transaction. Nonced transactions are useful when a transaction \n\
|
|
|
|
requires a lengthy signing process. Learn more about nonced \n\
|
|
|
|
transactions at https://docs.solana.com/offline-signing/durable-nonce",
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const NONCE_AUTHORITY_ARG: ArgConstant<'static> = ArgConstant {
|
|
|
|
name: "nonce_authority",
|
|
|
|
long: "nonce-authority",
|
|
|
|
help: "Provide the nonce authority keypair to use when signing a nonced transaction",
|
|
|
|
};
|
|
|
|
|
2019-12-10 00:24:44 -08:00
|
|
|
pub trait NonceSubCommands {
|
|
|
|
fn nonce_subcommands(self) -> Self;
|
|
|
|
}
|
|
|
|
|
2020-01-22 11:19:07 -08:00
|
|
|
pub fn nonce_arg<'a, 'b>() -> Arg<'a, 'b> {
|
|
|
|
Arg::with_name(NONCE_ARG.name)
|
|
|
|
.long(NONCE_ARG.long)
|
2019-12-17 06:34:21 -08:00
|
|
|
.takes_value(true)
|
2020-01-22 11:19:07 -08:00
|
|
|
.value_name("PUBKEY")
|
2020-01-26 00:27:24 -08:00
|
|
|
.requires(BLOCKHASH_ARG.name)
|
2020-01-22 11:19:07 -08:00
|
|
|
.validator(is_pubkey)
|
|
|
|
.help(NONCE_ARG.help)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nonce_authority_arg<'a, 'b>() -> Arg<'a, 'b> {
|
|
|
|
Arg::with_name(NONCE_AUTHORITY_ARG.name)
|
|
|
|
.long(NONCE_AUTHORITY_ARG.long)
|
|
|
|
.takes_value(true)
|
2020-02-21 13:55:53 -08:00
|
|
|
.value_name("KEYPAIR or PUBKEY or REMOTE WALLET PATH")
|
|
|
|
.validator(is_valid_signer)
|
2020-01-22 11:19:07 -08:00
|
|
|
.help(NONCE_AUTHORITY_ARG.help)
|
2019-12-17 06:34:21 -08:00
|
|
|
}
|
|
|
|
|
2019-12-10 00:24:44 -08:00
|
|
|
impl NonceSubCommands for App<'_, '_> {
|
|
|
|
fn nonce_subcommands(self) -> Self {
|
|
|
|
self.subcommand(
|
2019-12-19 16:13:01 -08:00
|
|
|
SubCommand::with_name("authorize-nonce-account")
|
|
|
|
.about("Assign account authority to a new entity")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("nonce_account_keypair")
|
|
|
|
.index(1)
|
|
|
|
.value_name("NONCE_ACCOUNT")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.validator(is_pubkey_or_keypair)
|
|
|
|
.help("Address of the nonce account"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("new_authority")
|
|
|
|
.index(2)
|
|
|
|
.value_name("NEW_AUTHORITY_PUBKEY")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.validator(is_pubkey_or_keypair)
|
|
|
|
.help("Account to be granted authority of the nonce account"),
|
|
|
|
)
|
2020-01-09 15:22:48 -08:00
|
|
|
.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")
|
|
|
|
)
|
2019-12-19 16:13:01 -08:00
|
|
|
.arg(nonce_authority_arg()),
|
|
|
|
)
|
|
|
|
.subcommand(
|
2019-12-10 00:24:44 -08:00
|
|
|
SubCommand::with_name("create-nonce-account")
|
|
|
|
.about("Create a nonce account")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("nonce_account_keypair")
|
|
|
|
.index(1)
|
|
|
|
.value_name("NONCE ACCOUNT")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2019-12-17 06:34:21 -08:00
|
|
|
.validator(is_pubkey_or_keypair)
|
2019-12-10 00:24:44 -08:00
|
|
|
.help("Keypair of the nonce account to fund"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("amount")
|
|
|
|
.index(2)
|
|
|
|
.value_name("AMOUNT")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.validator(is_amount)
|
2020-02-15 11:53:52 -08:00
|
|
|
.help("The amount to load the nonce account with, in SOL"),
|
2019-12-17 06:34:21 -08:00
|
|
|
)
|
|
|
|
.arg(
|
2020-01-22 11:19:07 -08:00
|
|
|
Arg::with_name(NONCE_AUTHORITY_ARG.name)
|
|
|
|
.long(NONCE_AUTHORITY_ARG.long)
|
2019-12-17 06:34:21 -08:00
|
|
|
.takes_value(true)
|
|
|
|
.value_name("BASE58_PUBKEY")
|
|
|
|
.validator(is_pubkey_or_keypair)
|
|
|
|
.help("Assign noncing authority to another entity"),
|
2019-12-10 00:24:44 -08:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.subcommand(
|
2020-01-20 22:06:47 -08:00
|
|
|
SubCommand::with_name("nonce")
|
2019-12-10 00:24:44 -08:00
|
|
|
.about("Get the current nonce value")
|
2020-01-20 22:06:47 -08:00
|
|
|
.alias("get-nonce")
|
2019-12-10 00:24:44 -08:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("nonce_account_pubkey")
|
|
|
|
.index(1)
|
|
|
|
.value_name("NONCE ACCOUNT")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.validator(is_pubkey_or_keypair)
|
|
|
|
.help("Address of the nonce account to display"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("new-nonce")
|
|
|
|
.about("Generate a new nonce, rendering the existing nonce useless")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("nonce_account_keypair")
|
|
|
|
.index(1)
|
|
|
|
.value_name("NONCE ACCOUNT")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.validator(is_pubkey_or_keypair)
|
|
|
|
.help("Address of the nonce account"),
|
2019-12-17 06:34:21 -08:00
|
|
|
)
|
|
|
|
.arg(nonce_authority_arg()),
|
2019-12-10 00:24:44 -08:00
|
|
|
)
|
|
|
|
.subcommand(
|
2020-01-20 22:06:47 -08:00
|
|
|
SubCommand::with_name("nonce-account")
|
2019-12-10 00:24:44 -08:00
|
|
|
.about("Show the contents of a nonce account")
|
2020-01-20 22:06:47 -08:00
|
|
|
.alias("show-nonce-account")
|
2019-12-10 00:24:44 -08:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("nonce_account_pubkey")
|
|
|
|
.index(1)
|
|
|
|
.value_name("NONCE ACCOUNT")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.validator(is_pubkey_or_keypair)
|
|
|
|
.help("Address of the nonce account to display"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("lamports")
|
|
|
|
.long("lamports")
|
|
|
|
.takes_value(false)
|
|
|
|
.help("Display balance in lamports instead of SOL"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("withdraw-from-nonce-account")
|
|
|
|
.about("Withdraw lamports from the nonce account")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("nonce_account_keypair")
|
|
|
|
.index(1)
|
|
|
|
.value_name("NONCE ACCOUNT")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.validator(is_keypair_or_ask_keyword)
|
|
|
|
.help("Nonce account from to withdraw from"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("destination_account_pubkey")
|
|
|
|
.index(2)
|
|
|
|
.value_name("DESTINATION ACCOUNT")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.validator(is_pubkey_or_keypair)
|
|
|
|
.help("The account to which the lamports should be transferred"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("amount")
|
|
|
|
.index(3)
|
|
|
|
.value_name("AMOUNT")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.validator(is_amount)
|
2020-02-15 11:53:52 -08:00
|
|
|
.help("The amount to withdraw from the nonce account, in SOL"),
|
2019-12-17 06:34:21 -08:00
|
|
|
)
|
|
|
|
.arg(nonce_authority_arg()),
|
2019-12-10 00:24:44 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
pub fn parse_authorize_nonce_account(
|
|
|
|
matches: &ArgMatches<'_>,
|
|
|
|
default_signer_path: &str,
|
|
|
|
wallet_manager: Option<&Arc<RemoteWalletManager>>,
|
|
|
|
) -> Result<CliCommandInfo, CliError> {
|
2019-12-19 16:13:01 -08:00
|
|
|
let nonce_account = pubkey_of(matches, "nonce_account_keypair").unwrap();
|
|
|
|
let new_authority = pubkey_of(matches, "new_authority").unwrap();
|
2020-02-24 16:03:30 -08:00
|
|
|
let (nonce_authority, nonce_authority_pubkey) =
|
|
|
|
signer_of(matches, NONCE_AUTHORITY_ARG.name, wallet_manager)?;
|
|
|
|
|
|
|
|
let payer_provided = None;
|
|
|
|
let signer_info = generate_unique_signers(
|
|
|
|
vec![payer_provided, nonce_authority],
|
|
|
|
matches,
|
|
|
|
default_signer_path,
|
|
|
|
wallet_manager,
|
|
|
|
)?;
|
2019-12-19 16:13:01 -08:00
|
|
|
|
|
|
|
Ok(CliCommandInfo {
|
|
|
|
command: CliCommand::AuthorizeNonceAccount {
|
|
|
|
nonce_account,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: signer_info.index_of(nonce_authority_pubkey).unwrap(),
|
2019-12-19 16:13:01 -08:00
|
|
|
new_authority,
|
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: signer_info.signers,
|
2019-12-19 16:13:01 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
pub fn parse_nonce_create_account(
|
|
|
|
matches: &ArgMatches<'_>,
|
|
|
|
default_signer_path: &str,
|
|
|
|
wallet_manager: Option<&Arc<RemoteWalletManager>>,
|
|
|
|
) -> Result<CliCommandInfo, CliError> {
|
|
|
|
let (nonce_account, nonce_account_pubkey) =
|
|
|
|
signer_of(matches, "nonce_account_keypair", wallet_manager)?;
|
2020-01-09 15:22:48 -08:00
|
|
|
let seed = matches.value_of("seed").map(|s| s.to_string());
|
2020-02-15 11:53:52 -08:00
|
|
|
let lamports = lamports_of_sol(matches, "amount").unwrap();
|
2020-01-22 11:19:07 -08:00
|
|
|
let nonce_authority = pubkey_of(matches, NONCE_AUTHORITY_ARG.name);
|
2019-12-10 00:24:44 -08:00
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
let payer_provided = None;
|
|
|
|
let signer_info = generate_unique_signers(
|
|
|
|
vec![payer_provided, nonce_account],
|
|
|
|
matches,
|
|
|
|
default_signer_path,
|
|
|
|
wallet_manager,
|
|
|
|
)?;
|
|
|
|
|
2019-12-10 00:24:44 -08:00
|
|
|
Ok(CliCommandInfo {
|
|
|
|
command: CliCommand::CreateNonceAccount {
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_account: signer_info.index_of(nonce_account_pubkey).unwrap(),
|
2020-01-09 15:22:48 -08:00
|
|
|
seed,
|
2019-12-17 06:34:21 -08:00
|
|
|
nonce_authority,
|
2019-12-10 00:24:44 -08:00
|
|
|
lamports,
|
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: signer_info.signers,
|
2019-12-10 00:24:44 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_get_nonce(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
|
|
|
|
let nonce_account_pubkey = pubkey_of(matches, "nonce_account_pubkey").unwrap();
|
|
|
|
|
|
|
|
Ok(CliCommandInfo {
|
|
|
|
command: CliCommand::GetNonce(nonce_account_pubkey),
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: vec![],
|
2019-12-10 00:24:44 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
pub fn parse_new_nonce(
|
|
|
|
matches: &ArgMatches<'_>,
|
|
|
|
default_signer_path: &str,
|
|
|
|
wallet_manager: Option<&Arc<RemoteWalletManager>>,
|
|
|
|
) -> Result<CliCommandInfo, CliError> {
|
2019-12-17 06:34:21 -08:00
|
|
|
let nonce_account = pubkey_of(matches, "nonce_account_keypair").unwrap();
|
2020-02-24 16:03:30 -08:00
|
|
|
let (nonce_authority, nonce_authority_pubkey) =
|
|
|
|
signer_of(matches, NONCE_AUTHORITY_ARG.name, wallet_manager)?;
|
|
|
|
|
|
|
|
let payer_provided = None;
|
|
|
|
let signer_info = generate_unique_signers(
|
|
|
|
vec![payer_provided, nonce_authority],
|
|
|
|
matches,
|
|
|
|
default_signer_path,
|
|
|
|
wallet_manager,
|
|
|
|
)?;
|
2019-12-10 00:24:44 -08:00
|
|
|
|
|
|
|
Ok(CliCommandInfo {
|
2019-12-17 06:34:21 -08:00
|
|
|
command: CliCommand::NewNonce {
|
|
|
|
nonce_account,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: signer_info.index_of(nonce_authority_pubkey).unwrap(),
|
2019-12-17 06:34:21 -08:00
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: signer_info.signers,
|
2019-12-10 00:24:44 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_show_nonce_account(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
|
|
|
|
let nonce_account_pubkey = pubkey_of(matches, "nonce_account_pubkey").unwrap();
|
|
|
|
let use_lamports_unit = matches.is_present("lamports");
|
|
|
|
|
|
|
|
Ok(CliCommandInfo {
|
|
|
|
command: CliCommand::ShowNonceAccount {
|
|
|
|
nonce_account_pubkey,
|
|
|
|
use_lamports_unit,
|
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: vec![],
|
2019-12-10 00:24:44 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_withdraw_from_nonce_account(
|
|
|
|
matches: &ArgMatches<'_>,
|
2020-02-24 16:03:30 -08:00
|
|
|
default_signer_path: &str,
|
|
|
|
wallet_manager: Option<&Arc<RemoteWalletManager>>,
|
2019-12-10 00:24:44 -08:00
|
|
|
) -> Result<CliCommandInfo, CliError> {
|
2019-12-17 06:34:21 -08:00
|
|
|
let nonce_account = pubkey_of(matches, "nonce_account_keypair").unwrap();
|
2019-12-10 00:24:44 -08:00
|
|
|
let destination_account_pubkey = pubkey_of(matches, "destination_account_pubkey").unwrap();
|
2020-02-15 11:53:52 -08:00
|
|
|
let lamports = lamports_of_sol(matches, "amount").unwrap();
|
2020-02-24 16:03:30 -08:00
|
|
|
let (nonce_authority, nonce_authority_pubkey) =
|
|
|
|
signer_of(matches, NONCE_AUTHORITY_ARG.name, wallet_manager)?;
|
|
|
|
|
|
|
|
let payer_provided = None;
|
|
|
|
let signer_info = generate_unique_signers(
|
|
|
|
vec![payer_provided, nonce_authority],
|
|
|
|
matches,
|
|
|
|
default_signer_path,
|
|
|
|
wallet_manager,
|
|
|
|
)?;
|
2019-12-10 00:24:44 -08:00
|
|
|
|
|
|
|
Ok(CliCommandInfo {
|
|
|
|
command: CliCommand::WithdrawFromNonceAccount {
|
2019-12-17 06:34:21 -08:00
|
|
|
nonce_account,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: signer_info.index_of(nonce_authority_pubkey).unwrap(),
|
2019-12-10 00:24:44 -08:00
|
|
|
destination_account_pubkey,
|
|
|
|
lamports,
|
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: signer_info.signers,
|
2019-12-10 00:24:44 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-27 12:35:49 -08:00
|
|
|
/// Check if a nonce account is initialized with the given authority and hash
|
|
|
|
pub fn check_nonce_account(
|
|
|
|
nonce_account: &Account,
|
|
|
|
nonce_authority: &Pubkey,
|
|
|
|
nonce_hash: &Hash,
|
|
|
|
) -> Result<(), Box<CliError>> {
|
2020-01-03 16:34:58 -08:00
|
|
|
if nonce_account.owner != system_program::ID {
|
2019-12-27 12:35:49 -08:00
|
|
|
return Err(CliError::InvalidNonce(CliNonceError::InvalidAccountOwner).into());
|
|
|
|
}
|
|
|
|
let nonce_state: NonceState = nonce_account
|
|
|
|
.state()
|
|
|
|
.map_err(|_| Box::new(CliError::InvalidNonce(CliNonceError::InvalidAccountData)))?;
|
|
|
|
match nonce_state {
|
|
|
|
NonceState::Initialized(meta, hash) => {
|
|
|
|
if &hash != nonce_hash {
|
|
|
|
Err(CliError::InvalidNonce(CliNonceError::InvalidHash).into())
|
|
|
|
} else if nonce_authority != &meta.nonce_authority {
|
|
|
|
Err(CliError::InvalidNonce(CliNonceError::InvalidAuthority).into())
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NonceState::Uninitialized => {
|
|
|
|
Err(CliError::InvalidNonce(CliNonceError::InvalidState).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:13:01 -08:00
|
|
|
pub fn process_authorize_nonce_account(
|
|
|
|
rpc_client: &RpcClient,
|
|
|
|
config: &CliConfig,
|
|
|
|
nonce_account: &Pubkey,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: SignerIndex,
|
2019-12-19 16:13:01 -08:00
|
|
|
new_authority: &Pubkey,
|
|
|
|
) -> ProcessResult {
|
|
|
|
let (recent_blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
|
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
let nonce_authority = config.signers[nonce_authority];
|
2020-01-22 15:31:39 -08:00
|
|
|
let ix = authorize_nonce_account(nonce_account, &nonce_authority.pubkey(), new_authority);
|
2020-02-24 16:03:30 -08:00
|
|
|
let message = Message::new_with_payer(vec![ix], Some(&config.signers[0].pubkey()));
|
2020-02-21 13:55:53 -08:00
|
|
|
let mut tx = Transaction::new_unsigned(message);
|
2020-02-24 16:03:30 -08:00
|
|
|
tx.try_sign(&config.signers, recent_blockhash)?;
|
2020-02-21 13:55:53 -08:00
|
|
|
|
2019-12-19 16:13:01 -08:00
|
|
|
check_account_for_fee(
|
|
|
|
rpc_client,
|
2020-02-24 16:03:30 -08:00
|
|
|
&config.signers[0].pubkey(),
|
2019-12-19 16:13:01 -08:00
|
|
|
&fee_calculator,
|
|
|
|
&tx.message,
|
|
|
|
)?;
|
2020-02-24 16:03:30 -08:00
|
|
|
let result = rpc_client.send_and_confirm_transaction(&mut tx, &config.signers);
|
2019-12-19 16:13:01 -08:00
|
|
|
log_instruction_custom_error::<NonceError>(result)
|
|
|
|
}
|
|
|
|
|
2019-12-10 00:24:44 -08:00
|
|
|
pub fn process_create_nonce_account(
|
|
|
|
rpc_client: &RpcClient,
|
|
|
|
config: &CliConfig,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_account: SignerIndex,
|
2020-01-09 15:22:48 -08:00
|
|
|
seed: Option<String>,
|
2020-01-02 17:05:08 -08:00
|
|
|
nonce_authority: Option<Pubkey>,
|
2019-12-10 00:24:44 -08:00
|
|
|
lamports: u64,
|
|
|
|
) -> ProcessResult {
|
2020-02-24 16:03:30 -08:00
|
|
|
let nonce_account_pubkey = config.signers[nonce_account].pubkey();
|
2020-01-09 15:22:48 -08:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2019-12-10 00:24:44 -08:00
|
|
|
check_unique_pubkeys(
|
2020-02-24 16:03:30 -08:00
|
|
|
(&config.signers[0].pubkey(), "cli keypair".to_string()),
|
2020-01-09 15:22:48 -08:00
|
|
|
(&nonce_account_address, "nonce_account".to_string()),
|
2019-12-10 00:24:44 -08:00
|
|
|
)?;
|
|
|
|
|
2020-01-09 20:25:07 -08:00
|
|
|
if let Ok(nonce_account) = rpc_client.get_account(&nonce_account_address) {
|
|
|
|
let err_msg = if nonce_account.owner == system_program::id()
|
2020-01-22 17:54:06 -08:00
|
|
|
&& StateMut::<NonceState>::state(&nonce_account).is_ok()
|
2020-01-09 20:25:07 -08:00
|
|
|
{
|
|
|
|
format!("Nonce account {} already exists", nonce_account_address)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"Account {} already exists and is not a nonce account",
|
|
|
|
nonce_account_address
|
|
|
|
)
|
|
|
|
};
|
|
|
|
return Err(CliError::BadParameter(err_msg).into());
|
2019-12-10 00:24:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
let minimum_balance = rpc_client.get_minimum_balance_for_rent_exemption(NonceState::size())?;
|
|
|
|
if lamports < minimum_balance {
|
|
|
|
return Err(CliError::BadParameter(format!(
|
|
|
|
"need at least {} lamports for nonce account to be rent exempt, provided lamports: {}",
|
|
|
|
minimum_balance, lamports
|
|
|
|
))
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
let nonce_authority = nonce_authority.unwrap_or_else(|| config.signers[0].pubkey());
|
2020-01-09 15:22:48 -08:00
|
|
|
|
|
|
|
let ixs = if let Some(seed) = seed {
|
|
|
|
create_nonce_account_with_seed(
|
2020-02-24 16:03:30 -08:00
|
|
|
&config.signers[0].pubkey(), // from
|
|
|
|
&nonce_account_address, // to
|
|
|
|
&nonce_account_pubkey, // base
|
|
|
|
&seed, // seed
|
2020-01-09 15:22:48 -08:00
|
|
|
&nonce_authority,
|
|
|
|
lamports,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
create_nonce_account(
|
2020-02-24 16:03:30 -08:00
|
|
|
&config.signers[0].pubkey(),
|
2020-01-09 15:22:48 -08:00
|
|
|
&nonce_account_pubkey,
|
|
|
|
&nonce_authority,
|
|
|
|
lamports,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2019-12-10 00:24:44 -08:00
|
|
|
let (recent_blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
|
2020-01-09 15:22:48 -08:00
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
let message = Message::new_with_payer(ixs, Some(&config.signers[0].pubkey()));
|
2020-02-21 13:55:53 -08:00
|
|
|
let mut tx = Transaction::new_unsigned(message);
|
2020-02-24 16:03:30 -08:00
|
|
|
tx.try_sign(&config.signers, recent_blockhash)?;
|
2020-02-21 13:55:53 -08:00
|
|
|
|
2019-12-10 00:24:44 -08:00
|
|
|
check_account_for_fee(
|
|
|
|
rpc_client,
|
2020-02-24 16:03:30 -08:00
|
|
|
&config.signers[0].pubkey(),
|
2019-12-10 00:24:44 -08:00
|
|
|
&fee_calculator,
|
|
|
|
&tx.message,
|
|
|
|
)?;
|
2020-02-24 16:03:30 -08:00
|
|
|
let result = rpc_client.send_and_confirm_transaction(&mut tx, &config.signers);
|
2019-12-10 00:24:44 -08:00
|
|
|
log_instruction_custom_error::<SystemError>(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_get_nonce(rpc_client: &RpcClient, nonce_account_pubkey: &Pubkey) -> ProcessResult {
|
|
|
|
let nonce_account = rpc_client.get_account(nonce_account_pubkey)?;
|
2020-01-03 16:34:58 -08:00
|
|
|
if nonce_account.owner != system_program::id() {
|
2019-12-19 23:27:54 -08:00
|
|
|
return Err(CliError::RpcRequestError(format!(
|
|
|
|
"{:?} is not a nonce account",
|
|
|
|
nonce_account_pubkey
|
|
|
|
))
|
2019-12-10 00:24:44 -08:00
|
|
|
.into());
|
|
|
|
}
|
|
|
|
match nonce_account.state() {
|
|
|
|
Ok(NonceState::Uninitialized) => Ok("Nonce account is uninitialized".to_string()),
|
|
|
|
Ok(NonceState::Initialized(_, hash)) => Ok(format!("{:?}", hash)),
|
|
|
|
Err(err) => Err(CliError::RpcRequestError(format!(
|
|
|
|
"Account data could not be deserialized to nonce state: {:?}",
|
|
|
|
err
|
|
|
|
))
|
|
|
|
.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_new_nonce(
|
|
|
|
rpc_client: &RpcClient,
|
|
|
|
config: &CliConfig,
|
2019-12-17 06:34:21 -08:00
|
|
|
nonce_account: &Pubkey,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: SignerIndex,
|
2019-12-10 00:24:44 -08:00
|
|
|
) -> ProcessResult {
|
|
|
|
check_unique_pubkeys(
|
2020-02-24 16:03:30 -08:00
|
|
|
(&config.signers[0].pubkey(), "cli keypair".to_string()),
|
2019-12-17 06:34:21 -08:00
|
|
|
(&nonce_account, "nonce_account_pubkey".to_string()),
|
2019-12-10 00:24:44 -08:00
|
|
|
)?;
|
|
|
|
|
2019-12-17 06:34:21 -08:00
|
|
|
if rpc_client.get_account(&nonce_account).is_err() {
|
2019-12-10 00:24:44 -08:00
|
|
|
return Err(CliError::BadParameter(
|
|
|
|
"Unable to create new nonce, no nonce account found".to_string(),
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
let nonce_authority = config.signers[nonce_authority];
|
2020-01-22 15:31:39 -08:00
|
|
|
let ix = advance_nonce_account(&nonce_account, &nonce_authority.pubkey());
|
2019-12-10 00:24:44 -08:00
|
|
|
let (recent_blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
|
2020-02-24 16:03:30 -08:00
|
|
|
let message = Message::new_with_payer(vec![ix], Some(&config.signers[0].pubkey()));
|
2020-02-21 13:55:53 -08:00
|
|
|
let mut tx = Transaction::new_unsigned(message);
|
2020-02-24 16:03:30 -08:00
|
|
|
tx.try_sign(&config.signers, recent_blockhash)?;
|
2019-12-10 00:24:44 -08:00
|
|
|
check_account_for_fee(
|
|
|
|
rpc_client,
|
2020-02-24 16:03:30 -08:00
|
|
|
&config.signers[0].pubkey(),
|
2019-12-10 00:24:44 -08:00
|
|
|
&fee_calculator,
|
|
|
|
&tx.message,
|
|
|
|
)?;
|
2020-02-24 16:03:30 -08:00
|
|
|
let result =
|
|
|
|
rpc_client.send_and_confirm_transaction(&mut tx, &[config.signers[0], nonce_authority]);
|
2019-12-10 00:24:44 -08:00
|
|
|
log_instruction_custom_error::<SystemError>(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_show_nonce_account(
|
|
|
|
rpc_client: &RpcClient,
|
|
|
|
nonce_account_pubkey: &Pubkey,
|
|
|
|
use_lamports_unit: bool,
|
|
|
|
) -> ProcessResult {
|
|
|
|
let nonce_account = rpc_client.get_account(nonce_account_pubkey)?;
|
2020-01-03 16:34:58 -08:00
|
|
|
if nonce_account.owner != system_program::id() {
|
2019-12-19 23:27:54 -08:00
|
|
|
return Err(CliError::RpcRequestError(format!(
|
|
|
|
"{:?} is not a nonce account",
|
|
|
|
nonce_account_pubkey
|
|
|
|
))
|
2019-12-10 00:24:44 -08:00
|
|
|
.into());
|
|
|
|
}
|
2020-01-25 06:21:23 -08:00
|
|
|
let print_account = |data: Option<(Meta, Hash)>| {
|
2019-12-10 00:24:44 -08:00
|
|
|
println!(
|
2020-02-05 10:14:44 -08:00
|
|
|
"Balance: {}",
|
2019-12-10 00:24:44 -08:00
|
|
|
build_balance_message(nonce_account.lamports, use_lamports_unit, true)
|
|
|
|
);
|
|
|
|
println!(
|
2020-02-05 10:14:44 -08:00
|
|
|
"Minimum Balance Required: {}",
|
2019-12-10 00:24:44 -08:00
|
|
|
build_balance_message(
|
|
|
|
rpc_client.get_minimum_balance_for_rent_exemption(NonceState::size())?,
|
|
|
|
use_lamports_unit,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
2020-01-25 06:21:23 -08:00
|
|
|
match data {
|
|
|
|
Some((meta, hash)) => {
|
2020-02-05 10:14:44 -08:00
|
|
|
println!("Nonce: {}", hash);
|
|
|
|
println!("Authority: {}", meta.nonce_authority);
|
2020-01-25 06:21:23 -08:00
|
|
|
}
|
|
|
|
None => {
|
2020-02-05 10:14:44 -08:00
|
|
|
println!("Nonce: uninitialized");
|
|
|
|
println!("Authority: uninitialized");
|
2020-01-25 06:21:23 -08:00
|
|
|
}
|
2019-12-10 00:24:44 -08:00
|
|
|
}
|
|
|
|
Ok("".to_string())
|
|
|
|
};
|
|
|
|
match nonce_account.state() {
|
|
|
|
Ok(NonceState::Uninitialized) => print_account(None),
|
2020-01-25 06:21:23 -08:00
|
|
|
Ok(NonceState::Initialized(meta, hash)) => print_account(Some((meta, hash))),
|
2019-12-10 00:24:44 -08:00
|
|
|
Err(err) => Err(CliError::RpcRequestError(format!(
|
|
|
|
"Account data could not be deserialized to nonce state: {:?}",
|
|
|
|
err
|
|
|
|
))
|
|
|
|
.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_withdraw_from_nonce_account(
|
|
|
|
rpc_client: &RpcClient,
|
|
|
|
config: &CliConfig,
|
2019-12-17 06:34:21 -08:00
|
|
|
nonce_account: &Pubkey,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: SignerIndex,
|
2019-12-10 00:24:44 -08:00
|
|
|
destination_account_pubkey: &Pubkey,
|
|
|
|
lamports: u64,
|
|
|
|
) -> ProcessResult {
|
|
|
|
let (recent_blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
|
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
let nonce_authority = config.signers[nonce_authority];
|
2020-01-22 15:31:39 -08:00
|
|
|
let ix = withdraw_nonce_account(
|
2019-12-17 06:34:21 -08:00
|
|
|
nonce_account,
|
|
|
|
&nonce_authority.pubkey(),
|
2019-12-10 00:24:44 -08:00
|
|
|
destination_account_pubkey,
|
|
|
|
lamports,
|
|
|
|
);
|
2020-02-24 16:03:30 -08:00
|
|
|
let message = Message::new_with_payer(vec![ix], Some(&config.signers[0].pubkey()));
|
2020-02-21 13:55:53 -08:00
|
|
|
let mut tx = Transaction::new_unsigned(message);
|
2020-02-24 16:03:30 -08:00
|
|
|
tx.try_sign(&config.signers, recent_blockhash)?;
|
2019-12-10 00:24:44 -08:00
|
|
|
check_account_for_fee(
|
|
|
|
rpc_client,
|
2020-02-24 16:03:30 -08:00
|
|
|
&config.signers[0].pubkey(),
|
2019-12-10 00:24:44 -08:00
|
|
|
&fee_calculator,
|
|
|
|
&tx.message,
|
|
|
|
)?;
|
2020-02-24 16:03:30 -08:00
|
|
|
let result = rpc_client.send_and_confirm_transaction(&mut tx, &config.signers);
|
2019-12-10 00:24:44 -08:00
|
|
|
log_instruction_custom_error::<NonceError>(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::cli::{app, parse_command};
|
2019-12-27 12:35:49 -08:00
|
|
|
use solana_sdk::{
|
|
|
|
account::Account,
|
|
|
|
hash::hash,
|
|
|
|
nonce_state::{Meta as NonceMeta, NonceState},
|
2020-02-24 16:03:30 -08:00
|
|
|
signature::{read_keypair_file, write_keypair, Keypair, Signer},
|
2019-12-27 12:35:49 -08:00
|
|
|
system_program,
|
|
|
|
};
|
2019-12-10 00:24:44 -08:00
|
|
|
use tempfile::NamedTempFile;
|
|
|
|
|
|
|
|
fn make_tmp_file() -> (String, NamedTempFile) {
|
|
|
|
let tmp_file = NamedTempFile::new().unwrap();
|
|
|
|
(String::from(tmp_file.path().to_str().unwrap()), tmp_file)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_command() {
|
|
|
|
let test_commands = app("test", "desc", "version");
|
2020-02-24 16:03:30 -08:00
|
|
|
let default_keypair = Keypair::new();
|
|
|
|
let (default_keypair_file, mut tmp_file) = make_tmp_file();
|
|
|
|
write_keypair(&default_keypair, tmp_file.as_file_mut()).unwrap();
|
2019-12-10 00:24:44 -08:00
|
|
|
let (keypair_file, mut tmp_file) = make_tmp_file();
|
|
|
|
let nonce_account_keypair = Keypair::new();
|
|
|
|
write_keypair(&nonce_account_keypair, tmp_file.as_file_mut()).unwrap();
|
|
|
|
let nonce_account_pubkey = nonce_account_keypair.pubkey();
|
|
|
|
let nonce_account_string = nonce_account_pubkey.to_string();
|
|
|
|
|
2019-12-19 16:13:01 -08:00
|
|
|
let (authority_keypair_file, mut tmp_file2) = make_tmp_file();
|
|
|
|
let nonce_authority_keypair = Keypair::new();
|
|
|
|
write_keypair(&nonce_authority_keypair, tmp_file2.as_file_mut()).unwrap();
|
|
|
|
|
|
|
|
// Test AuthorizeNonceAccount Subcommand
|
|
|
|
let test_authorize_nonce_account = test_commands.clone().get_matches_from(vec![
|
|
|
|
"test",
|
|
|
|
"authorize-nonce-account",
|
|
|
|
&keypair_file,
|
|
|
|
&Pubkey::default().to_string(),
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
2020-02-24 16:03:30 -08:00
|
|
|
parse_command(&test_authorize_nonce_account, &default_keypair_file, None).unwrap(),
|
2019-12-19 16:13:01 -08:00
|
|
|
CliCommandInfo {
|
|
|
|
command: CliCommand::AuthorizeNonceAccount {
|
|
|
|
nonce_account: nonce_account_pubkey,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 0,
|
2019-12-19 16:13:01 -08:00
|
|
|
new_authority: Pubkey::default(),
|
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: vec![read_keypair_file(&default_keypair_file).unwrap().into()],
|
2019-12-19 16:13:01 -08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test AuthorizeNonceAccount Subcommand with authority
|
|
|
|
let test_authorize_nonce_account = test_commands.clone().get_matches_from(vec![
|
|
|
|
"test",
|
|
|
|
"authorize-nonce-account",
|
|
|
|
&keypair_file,
|
|
|
|
&Pubkey::default().to_string(),
|
|
|
|
"--nonce-authority",
|
|
|
|
&authority_keypair_file,
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
2020-02-24 16:03:30 -08:00
|
|
|
parse_command(&test_authorize_nonce_account, &default_keypair_file, None).unwrap(),
|
2019-12-19 16:13:01 -08:00
|
|
|
CliCommandInfo {
|
|
|
|
command: CliCommand::AuthorizeNonceAccount {
|
|
|
|
nonce_account: read_keypair_file(&keypair_file).unwrap().pubkey(),
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 1,
|
2019-12-19 16:13:01 -08:00
|
|
|
new_authority: Pubkey::default(),
|
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: vec![
|
|
|
|
read_keypair_file(&default_keypair_file).unwrap().into(),
|
|
|
|
read_keypair_file(&authority_keypair_file).unwrap().into()
|
|
|
|
],
|
2019-12-19 16:13:01 -08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-12-10 00:24:44 -08:00
|
|
|
// Test CreateNonceAccount SubCommand
|
|
|
|
let test_create_nonce_account = test_commands.clone().get_matches_from(vec![
|
|
|
|
"test",
|
|
|
|
"create-nonce-account",
|
|
|
|
&keypair_file,
|
|
|
|
"50",
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
2020-02-24 16:03:30 -08:00
|
|
|
parse_command(&test_create_nonce_account, &default_keypair_file, None).unwrap(),
|
2019-12-10 00:24:44 -08:00
|
|
|
CliCommandInfo {
|
|
|
|
command: CliCommand::CreateNonceAccount {
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_account: 1,
|
2020-01-09 15:22:48 -08:00
|
|
|
seed: None,
|
2020-01-02 17:05:08 -08:00
|
|
|
nonce_authority: None,
|
2020-02-15 11:53:52 -08:00
|
|
|
lamports: 50_000_000_000,
|
2019-12-17 06:34:21 -08:00
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: vec![
|
|
|
|
read_keypair_file(&default_keypair_file).unwrap().into(),
|
|
|
|
read_keypair_file(&keypair_file).unwrap().into()
|
|
|
|
],
|
2019-12-17 06:34:21 -08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test CreateNonceAccount SubCommand with authority
|
|
|
|
let test_create_nonce_account = test_commands.clone().get_matches_from(vec![
|
|
|
|
"test",
|
|
|
|
"create-nonce-account",
|
|
|
|
&keypair_file,
|
|
|
|
"50",
|
|
|
|
"--nonce-authority",
|
|
|
|
&authority_keypair_file,
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
2020-02-24 16:03:30 -08:00
|
|
|
parse_command(&test_create_nonce_account, &default_keypair_file, None).unwrap(),
|
2019-12-17 06:34:21 -08:00
|
|
|
CliCommandInfo {
|
|
|
|
command: CliCommand::CreateNonceAccount {
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_account: 1,
|
2020-01-09 15:22:48 -08:00
|
|
|
seed: None,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: Some(nonce_authority_keypair.pubkey()),
|
2020-02-15 11:53:52 -08:00
|
|
|
lamports: 50_000_000_000,
|
2019-12-10 00:24:44 -08:00
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: vec![
|
|
|
|
read_keypair_file(&default_keypair_file).unwrap().into(),
|
|
|
|
read_keypair_file(&keypair_file).unwrap().into()
|
|
|
|
],
|
2019-12-10 00:24:44 -08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test GetNonce Subcommand
|
|
|
|
let test_get_nonce = test_commands.clone().get_matches_from(vec![
|
|
|
|
"test",
|
|
|
|
"get-nonce",
|
|
|
|
&nonce_account_string,
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
2020-02-24 16:03:30 -08:00
|
|
|
parse_command(&test_get_nonce, &default_keypair_file, None).unwrap(),
|
2019-12-10 00:24:44 -08:00
|
|
|
CliCommandInfo {
|
2020-02-24 16:03:30 -08:00
|
|
|
command: CliCommand::GetNonce(nonce_account_keypair.pubkey()),
|
|
|
|
signers: vec![],
|
2019-12-10 00:24:44 -08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test NewNonce SubCommand
|
|
|
|
let test_new_nonce =
|
|
|
|
test_commands
|
|
|
|
.clone()
|
|
|
|
.get_matches_from(vec!["test", "new-nonce", &keypair_file]);
|
2019-12-17 06:34:21 -08:00
|
|
|
let nonce_account = read_keypair_file(&keypair_file).unwrap();
|
|
|
|
assert_eq!(
|
2020-02-24 16:03:30 -08:00
|
|
|
parse_command(&test_new_nonce, &default_keypair_file, None).unwrap(),
|
2019-12-17 06:34:21 -08:00
|
|
|
CliCommandInfo {
|
|
|
|
command: CliCommand::NewNonce {
|
|
|
|
nonce_account: nonce_account.pubkey(),
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 0,
|
2019-12-17 06:34:21 -08:00
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: vec![read_keypair_file(&default_keypair_file).unwrap().into()],
|
2019-12-17 06:34:21 -08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test NewNonce SubCommand with authority
|
|
|
|
let test_new_nonce = test_commands.clone().get_matches_from(vec![
|
|
|
|
"test",
|
|
|
|
"new-nonce",
|
|
|
|
&keypair_file,
|
|
|
|
"--nonce-authority",
|
|
|
|
&authority_keypair_file,
|
|
|
|
]);
|
|
|
|
let nonce_account = read_keypair_file(&keypair_file).unwrap();
|
2019-12-10 00:24:44 -08:00
|
|
|
assert_eq!(
|
2020-02-24 16:03:30 -08:00
|
|
|
parse_command(&test_new_nonce, &default_keypair_file, None).unwrap(),
|
2019-12-10 00:24:44 -08:00
|
|
|
CliCommandInfo {
|
2019-12-17 06:34:21 -08:00
|
|
|
command: CliCommand::NewNonce {
|
|
|
|
nonce_account: nonce_account.pubkey(),
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 1,
|
2019-12-17 06:34:21 -08:00
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: vec![
|
|
|
|
read_keypair_file(&default_keypair_file).unwrap().into(),
|
|
|
|
read_keypair_file(&authority_keypair_file).unwrap().into()
|
|
|
|
],
|
2019-12-10 00:24:44 -08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test ShowNonceAccount Subcommand
|
|
|
|
let test_show_nonce_account = test_commands.clone().get_matches_from(vec![
|
|
|
|
"test",
|
2020-01-20 22:06:47 -08:00
|
|
|
"nonce-account",
|
2019-12-10 00:24:44 -08:00
|
|
|
&nonce_account_string,
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
2020-02-24 16:03:30 -08:00
|
|
|
parse_command(&test_show_nonce_account, &default_keypair_file, None).unwrap(),
|
2019-12-10 00:24:44 -08:00
|
|
|
CliCommandInfo {
|
|
|
|
command: CliCommand::ShowNonceAccount {
|
|
|
|
nonce_account_pubkey: nonce_account_keypair.pubkey(),
|
|
|
|
use_lamports_unit: false,
|
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: vec![],
|
2019-12-10 00:24:44 -08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test WithdrawFromNonceAccount Subcommand
|
|
|
|
let test_withdraw_from_nonce_account = test_commands.clone().get_matches_from(vec![
|
|
|
|
"test",
|
|
|
|
"withdraw-from-nonce-account",
|
|
|
|
&keypair_file,
|
|
|
|
&nonce_account_string,
|
|
|
|
"42",
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
2020-02-24 16:03:30 -08:00
|
|
|
parse_command(
|
|
|
|
&test_withdraw_from_nonce_account,
|
|
|
|
&default_keypair_file,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2019-12-10 00:24:44 -08:00
|
|
|
CliCommandInfo {
|
|
|
|
command: CliCommand::WithdrawFromNonceAccount {
|
2019-12-17 06:34:21 -08:00
|
|
|
nonce_account: read_keypair_file(&keypair_file).unwrap().pubkey(),
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 0,
|
2019-12-10 00:24:44 -08:00
|
|
|
destination_account_pubkey: nonce_account_pubkey,
|
2020-02-15 11:53:52 -08:00
|
|
|
lamports: 42_000_000_000
|
2019-12-10 00:24:44 -08:00
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: vec![read_keypair_file(&default_keypair_file).unwrap().into()],
|
2019-12-10 00:24:44 -08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
let test_withdraw_from_nonce_account = test_commands.clone().get_matches_from(vec![
|
|
|
|
"test",
|
|
|
|
"withdraw-from-nonce-account",
|
|
|
|
&keypair_file,
|
|
|
|
&nonce_account_string,
|
|
|
|
"42",
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
2020-02-24 16:03:30 -08:00
|
|
|
parse_command(
|
|
|
|
&test_withdraw_from_nonce_account,
|
|
|
|
&default_keypair_file,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2019-12-10 00:24:44 -08:00
|
|
|
CliCommandInfo {
|
|
|
|
command: CliCommand::WithdrawFromNonceAccount {
|
2019-12-17 06:34:21 -08:00
|
|
|
nonce_account: read_keypair_file(&keypair_file).unwrap().pubkey(),
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 0,
|
2019-12-10 00:24:44 -08:00
|
|
|
destination_account_pubkey: nonce_account_pubkey,
|
|
|
|
lamports: 42000000000
|
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: vec![read_keypair_file(&default_keypair_file).unwrap().into()],
|
2019-12-10 00:24:44 -08:00
|
|
|
}
|
|
|
|
);
|
2019-12-17 06:34:21 -08:00
|
|
|
|
|
|
|
// Test WithdrawFromNonceAccount Subcommand with authority
|
|
|
|
let test_withdraw_from_nonce_account = test_commands.clone().get_matches_from(vec![
|
|
|
|
"test",
|
|
|
|
"withdraw-from-nonce-account",
|
|
|
|
&keypair_file,
|
|
|
|
&nonce_account_string,
|
|
|
|
"42",
|
|
|
|
"--nonce-authority",
|
|
|
|
&authority_keypair_file,
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
2020-02-24 16:03:30 -08:00
|
|
|
parse_command(
|
|
|
|
&test_withdraw_from_nonce_account,
|
|
|
|
&default_keypair_file,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2019-12-17 06:34:21 -08:00
|
|
|
CliCommandInfo {
|
|
|
|
command: CliCommand::WithdrawFromNonceAccount {
|
|
|
|
nonce_account: read_keypair_file(&keypair_file).unwrap().pubkey(),
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 1,
|
2019-12-17 06:34:21 -08:00
|
|
|
destination_account_pubkey: nonce_account_pubkey,
|
2020-02-15 11:53:52 -08:00
|
|
|
lamports: 42_000_000_000
|
2019-12-17 06:34:21 -08:00
|
|
|
},
|
2020-02-24 16:03:30 -08:00
|
|
|
signers: vec![
|
|
|
|
read_keypair_file(&default_keypair_file).unwrap().into(),
|
|
|
|
read_keypair_file(&authority_keypair_file).unwrap().into()
|
|
|
|
],
|
2019-12-17 06:34:21 -08:00
|
|
|
}
|
|
|
|
);
|
2019-12-10 00:24:44 -08:00
|
|
|
}
|
2019-12-27 12:35:49 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_check_nonce_account() {
|
|
|
|
let blockhash = Hash::default();
|
|
|
|
let nonce_pubkey = Pubkey::new_rand();
|
|
|
|
let valid = Account::new_data(
|
|
|
|
1,
|
|
|
|
&NonceState::Initialized(NonceMeta::new(&nonce_pubkey), blockhash),
|
2020-01-03 16:34:58 -08:00
|
|
|
&system_program::ID,
|
2019-12-27 12:35:49 -08:00
|
|
|
);
|
|
|
|
assert!(check_nonce_account(&valid.unwrap(), &nonce_pubkey, &blockhash).is_ok());
|
|
|
|
|
|
|
|
let invalid_owner = Account::new_data(
|
|
|
|
1,
|
|
|
|
&NonceState::Initialized(NonceMeta::new(&nonce_pubkey), blockhash),
|
2020-01-03 16:34:58 -08:00
|
|
|
&Pubkey::new(&[1u8; 32]),
|
2019-12-27 12:35:49 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
check_nonce_account(&invalid_owner.unwrap(), &nonce_pubkey, &blockhash),
|
|
|
|
Err(Box::new(CliError::InvalidNonce(
|
|
|
|
CliNonceError::InvalidAccountOwner
|
|
|
|
))),
|
|
|
|
);
|
|
|
|
|
2020-01-03 16:34:58 -08:00
|
|
|
let invalid_data = Account::new_data(1, &"invalid", &system_program::ID);
|
2019-12-27 12:35:49 -08:00
|
|
|
assert_eq!(
|
|
|
|
check_nonce_account(&invalid_data.unwrap(), &nonce_pubkey, &blockhash),
|
|
|
|
Err(Box::new(CliError::InvalidNonce(
|
|
|
|
CliNonceError::InvalidAccountData
|
|
|
|
))),
|
|
|
|
);
|
|
|
|
|
|
|
|
let invalid_hash = Account::new_data(
|
|
|
|
1,
|
|
|
|
&NonceState::Initialized(NonceMeta::new(&nonce_pubkey), hash(b"invalid")),
|
2020-01-03 16:34:58 -08:00
|
|
|
&system_program::ID,
|
2019-12-27 12:35:49 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
check_nonce_account(&invalid_hash.unwrap(), &nonce_pubkey, &blockhash),
|
|
|
|
Err(Box::new(CliError::InvalidNonce(CliNonceError::InvalidHash))),
|
|
|
|
);
|
|
|
|
|
|
|
|
let invalid_authority = Account::new_data(
|
|
|
|
1,
|
|
|
|
&NonceState::Initialized(NonceMeta::new(&Pubkey::new_rand()), blockhash),
|
2020-01-03 16:34:58 -08:00
|
|
|
&system_program::ID,
|
2019-12-27 12:35:49 -08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
check_nonce_account(&invalid_authority.unwrap(), &nonce_pubkey, &blockhash),
|
|
|
|
Err(Box::new(CliError::InvalidNonce(
|
|
|
|
CliNonceError::InvalidAuthority
|
|
|
|
))),
|
|
|
|
);
|
|
|
|
|
2020-01-03 16:34:58 -08:00
|
|
|
let invalid_state = Account::new_data(1, &NonceState::Uninitialized, &system_program::ID);
|
2019-12-27 12:35:49 -08:00
|
|
|
assert_eq!(
|
|
|
|
check_nonce_account(&invalid_state.unwrap(), &nonce_pubkey, &blockhash),
|
|
|
|
Err(Box::new(CliError::InvalidNonce(
|
|
|
|
CliNonceError::InvalidState
|
|
|
|
))),
|
|
|
|
);
|
|
|
|
}
|
2019-12-10 00:24:44 -08:00
|
|
|
}
|