Move CLI Signer utils into clap-utils
This commit is contained in:
committed by
Trent Nelson
parent
be88e868bd
commit
0c58123b45
@ -25,6 +25,65 @@ use std::{
|
|||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub type CliSigners = Vec<Box<dyn Signer>>;
|
||||||
|
pub type SignerIndex = usize;
|
||||||
|
pub struct CliSignerInfo {
|
||||||
|
pub signers: CliSigners,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CliSignerInfo {
|
||||||
|
pub fn index_of(&self, pubkey: Option<Pubkey>) -> Option<usize> {
|
||||||
|
if let Some(pubkey) = pubkey {
|
||||||
|
self.signers
|
||||||
|
.iter()
|
||||||
|
.position(|signer| signer.pubkey() == pubkey)
|
||||||
|
} else {
|
||||||
|
Some(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DefaultSigner {
|
||||||
|
pub arg_name: String,
|
||||||
|
pub path: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DefaultSigner {
|
||||||
|
pub fn generate_unique_signers(
|
||||||
|
&self,
|
||||||
|
bulk_signers: Vec<Option<Box<dyn Signer>>>,
|
||||||
|
matches: &ArgMatches<'_>,
|
||||||
|
wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
|
||||||
|
) -> Result<CliSignerInfo, Box<dyn error::Error>> {
|
||||||
|
let mut unique_signers = vec![];
|
||||||
|
|
||||||
|
// Determine if the default signer is needed
|
||||||
|
if bulk_signers.iter().any(|signer| signer.is_none()) {
|
||||||
|
let default_signer = self.signer_from_path(matches, wallet_manager)?;
|
||||||
|
unique_signers.push(default_signer);
|
||||||
|
}
|
||||||
|
|
||||||
|
for signer in bulk_signers.into_iter() {
|
||||||
|
if let Some(signer) = signer {
|
||||||
|
if !unique_signers.iter().any(|s| s == &signer) {
|
||||||
|
unique_signers.push(signer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(CliSignerInfo {
|
||||||
|
signers: unique_signers,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn signer_from_path(
|
||||||
|
&self,
|
||||||
|
matches: &ArgMatches,
|
||||||
|
wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
|
||||||
|
) -> Result<Box<dyn Signer>, Box<dyn std::error::Error>> {
|
||||||
|
signer_from_path(matches, &self.path, &self.arg_name, wallet_manager)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub enum KeypairUrl {
|
pub enum KeypairUrl {
|
||||||
Ask,
|
Ask,
|
||||||
Filepath(String),
|
Filepath(String),
|
||||||
|
@ -21,7 +21,7 @@ use solana_clap_utils::{
|
|||||||
fee_payer::{fee_payer_arg, FEE_PAYER_ARG},
|
fee_payer::{fee_payer_arg, FEE_PAYER_ARG},
|
||||||
input_parsers::*,
|
input_parsers::*,
|
||||||
input_validators::*,
|
input_validators::*,
|
||||||
keypair::signer_from_path,
|
keypair::*,
|
||||||
nonce::*,
|
nonce::*,
|
||||||
offline::*,
|
offline::*,
|
||||||
};
|
};
|
||||||
@ -75,65 +75,6 @@ use std::{
|
|||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
pub type CliSigners = Vec<Box<dyn Signer>>;
|
|
||||||
pub type SignerIndex = usize;
|
|
||||||
pub struct CliSignerInfo {
|
|
||||||
pub signers: CliSigners,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CliSignerInfo {
|
|
||||||
pub(crate) fn index_of(&self, pubkey: Option<Pubkey>) -> Option<usize> {
|
|
||||||
if let Some(pubkey) = pubkey {
|
|
||||||
self.signers
|
|
||||||
.iter()
|
|
||||||
.position(|signer| signer.pubkey() == pubkey)
|
|
||||||
} else {
|
|
||||||
Some(0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct DefaultSigner {
|
|
||||||
pub arg_name: String,
|
|
||||||
pub path: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DefaultSigner {
|
|
||||||
pub fn generate_unique_signers(
|
|
||||||
&self,
|
|
||||||
bulk_signers: Vec<Option<Box<dyn Signer>>>,
|
|
||||||
matches: &ArgMatches<'_>,
|
|
||||||
wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
|
|
||||||
) -> Result<CliSignerInfo, Box<dyn error::Error>> {
|
|
||||||
let mut unique_signers = vec![];
|
|
||||||
|
|
||||||
// Determine if the default signer is needed
|
|
||||||
if bulk_signers.iter().any(|signer| signer.is_none()) {
|
|
||||||
let default_signer = self.signer_from_path(matches, wallet_manager)?;
|
|
||||||
unique_signers.push(default_signer);
|
|
||||||
}
|
|
||||||
|
|
||||||
for signer in bulk_signers.into_iter() {
|
|
||||||
if let Some(signer) = signer {
|
|
||||||
if !unique_signers.iter().any(|s| s == &signer) {
|
|
||||||
unique_signers.push(signer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(CliSignerInfo {
|
|
||||||
signers: unique_signers,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn signer_from_path(
|
|
||||||
&self,
|
|
||||||
matches: &ArgMatches,
|
|
||||||
wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
|
|
||||||
) -> Result<Box<dyn Signer>, Box<dyn std::error::Error>> {
|
|
||||||
signer_from_path(matches, &self.path, &self.arg_name, wallet_manager)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const DATA_CHUNK_SIZE: usize = 229; // Keep program chunks under PACKET_DATA_SIZE
|
const DATA_CHUNK_SIZE: usize = 229; // Keep program chunks under PACKET_DATA_SIZE
|
||||||
pub const DEFAULT_RPC_TIMEOUT_SECONDS: &str = "30";
|
pub const DEFAULT_RPC_TIMEOUT_SECONDS: &str = "30";
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
cli::{CliCommand, CliCommandInfo, CliConfig, CliError, DefaultSigner, ProcessResult},
|
cli::{CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult},
|
||||||
cli_output::*,
|
cli_output::*,
|
||||||
display::{
|
display::{
|
||||||
format_labeled_address, new_spinner_progress_bar, println_name_value, println_transaction,
|
format_labeled_address, new_spinner_progress_bar, println_name_value, println_transaction,
|
||||||
@ -8,7 +8,9 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use clap::{value_t, value_t_or_exit, App, AppSettings, Arg, ArgMatches, SubCommand};
|
use clap::{value_t, value_t_or_exit, App, AppSettings, Arg, ArgMatches, SubCommand};
|
||||||
use console::{style, Emoji};
|
use console::{style, Emoji};
|
||||||
use solana_clap_utils::{commitment::commitment_arg, input_parsers::*, input_validators::*};
|
use solana_clap_utils::{
|
||||||
|
commitment::commitment_arg, input_parsers::*, input_validators::*, keypair::DefaultSigner,
|
||||||
|
};
|
||||||
use solana_client::{
|
use solana_client::{
|
||||||
pubsub_client::PubsubClient,
|
pubsub_client::PubsubClient,
|
||||||
rpc_client::{GetConfirmedSignaturesForAddress2Config, RpcClient},
|
rpc_client::{GetConfirmedSignaturesForAddress2Config, RpcClient},
|
||||||
|
@ -5,13 +5,15 @@ use clap::{
|
|||||||
use console::style;
|
use console::style;
|
||||||
|
|
||||||
use solana_clap_utils::{
|
use solana_clap_utils::{
|
||||||
commitment::COMMITMENT_ARG, input_parsers::commitment_of, input_validators::is_url,
|
commitment::COMMITMENT_ARG,
|
||||||
keypair::SKIP_SEED_PHRASE_VALIDATION_ARG, DisplayError,
|
input_parsers::commitment_of,
|
||||||
|
input_validators::is_url,
|
||||||
|
keypair::{CliSigners, DefaultSigner, SKIP_SEED_PHRASE_VALIDATION_ARG},
|
||||||
|
DisplayError,
|
||||||
};
|
};
|
||||||
use solana_cli::{
|
use solana_cli::{
|
||||||
cli::{
|
cli::{
|
||||||
app, parse_command, process_command, CliCommandInfo, CliConfig, CliSigners, DefaultSigner,
|
app, parse_command, process_command, CliCommandInfo, CliConfig, DEFAULT_RPC_TIMEOUT_SECONDS,
|
||||||
DEFAULT_RPC_TIMEOUT_SECONDS,
|
|
||||||
},
|
},
|
||||||
cli_output::OutputFormat,
|
cli_output::OutputFormat,
|
||||||
display::{println_name_value, println_name_value_or},
|
display::{println_name_value, println_name_value_or},
|
||||||
|
@ -2,13 +2,18 @@ use crate::{
|
|||||||
checks::{check_account_for_fee_with_commitment, check_unique_pubkeys},
|
checks::{check_account_for_fee_with_commitment, check_unique_pubkeys},
|
||||||
cli::{
|
cli::{
|
||||||
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError,
|
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError,
|
||||||
DefaultSigner, ProcessResult, SignerIndex,
|
ProcessResult,
|
||||||
},
|
},
|
||||||
cli_output::CliNonceAccount,
|
cli_output::CliNonceAccount,
|
||||||
spend_utils::{resolve_spend_tx_and_check_account_balance, SpendAmount},
|
spend_utils::{resolve_spend_tx_and_check_account_balance, SpendAmount},
|
||||||
};
|
};
|
||||||
use clap::{App, Arg, ArgMatches, SubCommand};
|
use clap::{App, Arg, ArgMatches, SubCommand};
|
||||||
use solana_clap_utils::{input_parsers::*, input_validators::*, nonce::*};
|
use solana_clap_utils::{
|
||||||
|
input_parsers::*,
|
||||||
|
input_validators::*,
|
||||||
|
keypair::{DefaultSigner, SignerIndex},
|
||||||
|
nonce::*,
|
||||||
|
};
|
||||||
use solana_client::{nonce_utils::*, rpc_client::RpcClient};
|
use solana_client::{nonce_utils::*, rpc_client::RpcClient};
|
||||||
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
|
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
|
||||||
use solana_sdk::{
|
use solana_sdk::{
|
||||||
|
@ -2,7 +2,7 @@ use crate::{
|
|||||||
checks::{check_account_for_fee_with_commitment, check_unique_pubkeys},
|
checks::{check_account_for_fee_with_commitment, check_unique_pubkeys},
|
||||||
cli::{
|
cli::{
|
||||||
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError,
|
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError,
|
||||||
DefaultSigner, ProcessResult, SignerIndex,
|
ProcessResult,
|
||||||
},
|
},
|
||||||
cli_output::{CliStakeHistory, CliStakeHistoryEntry, CliStakeState, CliStakeType},
|
cli_output::{CliStakeHistory, CliStakeHistoryEntry, CliStakeState, CliStakeType},
|
||||||
nonce::check_nonce_account,
|
nonce::check_nonce_account,
|
||||||
@ -14,6 +14,7 @@ use solana_clap_utils::{
|
|||||||
fee_payer::{fee_payer_arg, FEE_PAYER_ARG},
|
fee_payer::{fee_payer_arg, FEE_PAYER_ARG},
|
||||||
input_parsers::*,
|
input_parsers::*,
|
||||||
input_validators::*,
|
input_validators::*,
|
||||||
|
keypair::{DefaultSigner, SignerIndex},
|
||||||
nonce::*,
|
nonce::*,
|
||||||
offline::*,
|
offline::*,
|
||||||
ArgConstant,
|
ArgConstant,
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
cli::{CliCommand, CliCommandInfo, CliConfig, CliError, DefaultSigner, ProcessResult},
|
cli::{CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult},
|
||||||
cli_output::{CliValidatorInfo, CliValidatorInfoVec},
|
cli_output::{CliValidatorInfo, CliValidatorInfoVec},
|
||||||
spend_utils::{resolve_spend_tx_and_check_account_balance, SpendAmount},
|
spend_utils::{resolve_spend_tx_and_check_account_balance, SpendAmount},
|
||||||
};
|
};
|
||||||
@ -13,6 +13,7 @@ use solana_account_decoder::validator_info::{
|
|||||||
use solana_clap_utils::{
|
use solana_clap_utils::{
|
||||||
input_parsers::pubkey_of,
|
input_parsers::pubkey_of,
|
||||||
input_validators::{is_pubkey, is_url},
|
input_validators::{is_pubkey, is_url},
|
||||||
|
keypair::DefaultSigner,
|
||||||
};
|
};
|
||||||
use solana_client::rpc_client::RpcClient;
|
use solana_client::rpc_client::RpcClient;
|
||||||
use solana_config_program::{config_instruction, get_config_data, ConfigKeys, ConfigState};
|
use solana_config_program::{config_instruction, get_config_data, ConfigKeys, ConfigState};
|
||||||
|
@ -2,13 +2,18 @@ use crate::{
|
|||||||
checks::{check_account_for_fee_with_commitment, check_unique_pubkeys},
|
checks::{check_account_for_fee_with_commitment, check_unique_pubkeys},
|
||||||
cli::{
|
cli::{
|
||||||
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError,
|
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError,
|
||||||
DefaultSigner, ProcessResult, SignerIndex,
|
ProcessResult,
|
||||||
},
|
},
|
||||||
cli_output::{CliEpochVotingHistory, CliLockout, CliVoteAccount},
|
cli_output::{CliEpochVotingHistory, CliLockout, CliVoteAccount},
|
||||||
spend_utils::{resolve_spend_tx_and_check_account_balance, SpendAmount},
|
spend_utils::{resolve_spend_tx_and_check_account_balance, SpendAmount},
|
||||||
};
|
};
|
||||||
use clap::{value_t_or_exit, App, Arg, ArgMatches, SubCommand};
|
use clap::{value_t_or_exit, App, Arg, ArgMatches, SubCommand};
|
||||||
use solana_clap_utils::{commitment::commitment_arg, input_parsers::*, input_validators::*};
|
use solana_clap_utils::{
|
||||||
|
commitment::commitment_arg,
|
||||||
|
input_parsers::*,
|
||||||
|
input_validators::*,
|
||||||
|
keypair::{DefaultSigner, SignerIndex},
|
||||||
|
};
|
||||||
use solana_client::rpc_client::RpcClient;
|
use solana_client::rpc_client::RpcClient;
|
||||||
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
|
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
|
||||||
use solana_sdk::{
|
use solana_sdk::{
|
||||||
|
Reference in New Issue
Block a user