2019-03-13 20:54:30 -07:00
|
|
|
use clap::{
|
2019-04-10 17:52:47 -07:00
|
|
|
crate_description, crate_name, crate_version, App, AppSettings, Arg, ArgMatches, SubCommand,
|
2019-03-13 20:54:30 -07:00
|
|
|
};
|
2019-03-20 15:20:31 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2018-12-12 13:13:18 -08:00
|
|
|
use solana_sdk::signature::{gen_keypair_file, read_keypair, KeypairUtil};
|
2019-01-13 23:10:03 -08:00
|
|
|
use solana_wallet::wallet::{parse_command, process_command, WalletConfig, WalletError};
|
2018-06-29 10:38:00 -07:00
|
|
|
use std::error;
|
|
|
|
|
2018-12-08 21:44:20 -08:00
|
|
|
pub fn parse_args(matches: &ArgMatches<'_>) -> Result<WalletConfig, Box<dyn error::Error>> {
|
2019-05-06 07:38:26 -07:00
|
|
|
let json_rpc_url = matches.value_of("json_rpc_url").unwrap().to_string();
|
2019-01-16 20:43:00 -08:00
|
|
|
|
|
|
|
let drone_host = if let Some(drone_host) = matches.value_of("drone_host") {
|
2019-04-13 19:34:27 -07:00
|
|
|
Some(solana_netutil::parse_host(drone_host).or_else(|err| {
|
|
|
|
Err(WalletError::BadParameter(format!(
|
|
|
|
"Invalid drone host: {:?}",
|
|
|
|
err
|
|
|
|
)))
|
|
|
|
})?)
|
2019-01-16 20:43:00 -08:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let drone_port = matches
|
|
|
|
.value_of("drone_port")
|
|
|
|
.unwrap()
|
|
|
|
.parse()
|
2019-04-13 19:34:27 -07:00
|
|
|
.or_else(|err| {
|
|
|
|
Err(WalletError::BadParameter(format!(
|
|
|
|
"Invalid drone port: {:?}",
|
|
|
|
err
|
|
|
|
)))
|
|
|
|
})?;
|
2019-01-16 20:43:00 -08:00
|
|
|
|
2018-09-14 01:58:39 -07:00
|
|
|
let mut path = dirs::home_dir().expect("home directory");
|
|
|
|
let id_path = if matches.is_present("keypair") {
|
|
|
|
matches.value_of("keypair").unwrap()
|
|
|
|
} else {
|
|
|
|
path.extend(&[".config", "solana", "id.json"]);
|
2018-09-14 17:13:12 -07:00
|
|
|
if !path.exists() {
|
2018-09-17 09:48:04 -07:00
|
|
|
gen_keypair_file(path.to_str().unwrap().to_string())?;
|
|
|
|
println!("New keypair generated at: {:?}", path.to_str().unwrap());
|
2018-09-14 17:13:12 -07:00
|
|
|
}
|
|
|
|
|
2018-09-14 01:58:39 -07:00
|
|
|
path.to_str().unwrap()
|
|
|
|
};
|
2019-04-02 06:08:11 -07:00
|
|
|
let keypair = read_keypair(id_path).or_else(|err| {
|
2018-09-14 01:58:39 -07:00
|
|
|
Err(WalletError::BadParameter(format!(
|
|
|
|
"{}: Unable to open keypair file: {}",
|
|
|
|
err, id_path
|
|
|
|
)))
|
|
|
|
})?;
|
2018-06-29 10:38:00 -07:00
|
|
|
|
2019-04-02 06:08:11 -07:00
|
|
|
let command = parse_command(&keypair.pubkey(), &matches)?;
|
2018-09-14 01:58:39 -07:00
|
|
|
|
|
|
|
Ok(WalletConfig {
|
|
|
|
command,
|
2019-01-16 20:43:00 -08:00
|
|
|
drone_host,
|
|
|
|
drone_port,
|
2019-05-06 07:38:26 -07:00
|
|
|
json_rpc_url,
|
|
|
|
keypair,
|
2019-01-13 23:10:03 -08:00
|
|
|
rpc_client: None,
|
2018-09-14 01:58:39 -07:00
|
|
|
})
|
2018-06-29 10:38:00 -07:00
|
|
|
}
|
|
|
|
|
2019-05-06 07:38:26 -07:00
|
|
|
// Return an error if a url cannot be parsed.
|
|
|
|
fn is_url(string: String) -> Result<(), String> {
|
|
|
|
match url::Url::parse(&string) {
|
|
|
|
Ok(url) => {
|
|
|
|
if url.has_host() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err("no host provided".to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => Err(format!("{:?}", err)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 15:20:31 -07:00
|
|
|
// Return an error if a pubkey cannot be parsed.
|
|
|
|
fn is_pubkey(string: String) -> Result<(), String> {
|
|
|
|
match string.parse::<Pubkey>() {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(err) => Err(format!("{:?}", err)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 21:44:20 -08:00
|
|
|
fn main() -> Result<(), Box<dyn error::Error>> {
|
2018-12-14 12:36:50 -08:00
|
|
|
solana_logger::setup();
|
2019-01-16 20:43:00 -08:00
|
|
|
|
2019-05-06 07:38:26 -07:00
|
|
|
let default = WalletConfig::default();
|
|
|
|
let default_drone_port = format!("{}", default.drone_port);
|
2019-01-16 20:43:00 -08:00
|
|
|
|
2019-03-13 20:54:30 -07:00
|
|
|
let matches = App::new(crate_name!())
|
|
|
|
.about(crate_description!())
|
2018-08-06 20:51:12 -07:00
|
|
|
.version(crate_version!())
|
2019-05-06 07:38:26 -07:00
|
|
|
.setting(AppSettings::SubcommandRequiredElseHelp)
|
2018-06-29 18:33:20 -07:00
|
|
|
.arg(
|
2019-05-06 07:38:26 -07:00
|
|
|
Arg::with_name("json_rpc_url")
|
|
|
|
.short("u")
|
|
|
|
.long("url")
|
|
|
|
.value_name("URL")
|
2019-01-16 20:43:00 -08:00
|
|
|
.takes_value(true)
|
2019-05-06 07:38:26 -07:00
|
|
|
.default_value(&default.json_rpc_url)
|
|
|
|
.validator(is_url)
|
|
|
|
.help("JSON RPC URL for the solana cluster"),
|
2019-01-16 20:43:00 -08:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("drone_host")
|
|
|
|
.long("drone-host")
|
2019-05-06 07:38:26 -07:00
|
|
|
.value_name("HOST")
|
2019-01-16 20:43:00 -08:00
|
|
|
.takes_value(true)
|
2019-05-06 07:38:26 -07:00
|
|
|
.help("Drone host to use [default: same as the --url host]"),
|
2019-01-16 20:43:00 -08:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("drone_port")
|
|
|
|
.long("drone-port")
|
|
|
|
.value_name("PORT")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value(&default_drone_port)
|
|
|
|
.help("Drone port to use"),
|
|
|
|
)
|
|
|
|
.arg(
|
2018-07-12 15:02:14 -07:00
|
|
|
Arg::with_name("keypair")
|
|
|
|
.short("k")
|
|
|
|
.long("keypair")
|
2018-06-29 18:33:20 -07:00
|
|
|
.value_name("PATH")
|
|
|
|
.takes_value(true)
|
2018-07-12 15:02:14 -07:00
|
|
|
.help("/path/to/id.json"),
|
2019-01-16 20:43:00 -08:00
|
|
|
)
|
|
|
|
.subcommand(SubCommand::with_name("address").about("Get your public key"))
|
2018-09-18 14:42:32 -07:00
|
|
|
.subcommand(
|
2018-06-29 18:33:20 -07:00
|
|
|
SubCommand::with_name("airdrop")
|
2019-03-05 17:22:46 -08:00
|
|
|
.about("Request a batch of lamports")
|
2018-06-29 18:33:20 -07:00
|
|
|
.arg(
|
2019-03-05 17:22:46 -08:00
|
|
|
Arg::with_name("lamports")
|
2018-09-18 14:42:32 -07:00
|
|
|
.index(1)
|
2018-09-14 15:32:57 -07:00
|
|
|
.value_name("NUM")
|
2018-06-29 18:33:20 -07:00
|
|
|
.takes_value(true)
|
2018-07-12 15:32:53 -07:00
|
|
|
.required(true)
|
2019-03-05 17:22:46 -08:00
|
|
|
.help("The number of lamports to request"),
|
2018-06-29 18:33:20 -07:00
|
|
|
),
|
2019-01-16 20:43:00 -08:00
|
|
|
)
|
2019-03-20 09:44:16 -07:00
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("balance")
|
|
|
|
.about("Get your balance")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("pubkey")
|
|
|
|
.index(1)
|
|
|
|
.value_name("PUBKEY")
|
|
|
|
.takes_value(true)
|
|
|
|
.validator(is_pubkey)
|
|
|
|
.help("The public key of the balance to check"),
|
|
|
|
),
|
|
|
|
)
|
2018-09-18 14:42:32 -07:00
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("cancel")
|
|
|
|
.about("Cancel a transfer")
|
|
|
|
.arg(
|
2019-03-07 16:00:12 -08:00
|
|
|
Arg::with_name("process_id")
|
2018-09-18 14:42:32 -07:00
|
|
|
.index(1)
|
|
|
|
.value_name("PROCESS_ID")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2019-03-20 15:20:31 -07:00
|
|
|
.validator(is_pubkey)
|
2018-09-18 14:42:32 -07:00
|
|
|
.help("The process id of the transfer to cancel"),
|
|
|
|
),
|
2019-01-16 20:43:00 -08:00
|
|
|
)
|
|
|
|
.subcommand(
|
2018-09-18 14:42:32 -07:00
|
|
|
SubCommand::with_name("confirm")
|
|
|
|
.about("Confirm transaction by signature")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("signature")
|
|
|
|
.index(1)
|
|
|
|
.value_name("SIGNATURE")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.help("The transaction signature to confirm"),
|
|
|
|
),
|
2019-01-16 20:43:00 -08:00
|
|
|
)
|
2019-03-06 17:02:20 -08:00
|
|
|
.subcommand(
|
2019-04-10 17:52:47 -07:00
|
|
|
SubCommand::with_name("authorize-voter")
|
|
|
|
.about("Authorize a different voter for this account")
|
2019-03-06 17:02:20 -08:00
|
|
|
.arg(
|
2019-04-10 17:52:47 -07:00
|
|
|
Arg::with_name("authorized-voter-id")
|
|
|
|
.index(1)
|
2019-03-06 17:02:20 -08:00
|
|
|
.value_name("PUBKEY")
|
|
|
|
.takes_value(true)
|
2019-04-10 17:52:47 -07:00
|
|
|
.required(true)
|
2019-03-20 15:20:31 -07:00
|
|
|
.validator(is_pubkey)
|
2019-03-06 17:02:20 -08:00
|
|
|
.help("Vote signer to authorize"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.subcommand(
|
2019-04-10 17:52:47 -07:00
|
|
|
SubCommand::with_name("create-vote-account")
|
2019-03-06 17:02:20 -08:00
|
|
|
.about("Create staking account for node")
|
|
|
|
.arg(
|
2019-03-07 16:00:12 -08:00
|
|
|
Arg::with_name("voting_account_id")
|
2019-03-06 17:02:20 -08:00
|
|
|
.index(1)
|
|
|
|
.value_name("PUBKEY")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2019-03-20 15:20:31 -07:00
|
|
|
.validator(is_pubkey)
|
2019-03-06 17:02:20 -08:00
|
|
|
.help("Staking account address to fund"),
|
|
|
|
)
|
|
|
|
.arg(
|
2019-04-10 17:52:47 -07:00
|
|
|
Arg::with_name("node_id")
|
2019-03-06 17:02:20 -08:00
|
|
|
.index(2)
|
2019-04-10 17:52:47 -07:00
|
|
|
.value_name("PUBKEY")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.validator(is_pubkey)
|
|
|
|
.help("Staking account address to fund"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("lamports")
|
|
|
|
.index(3)
|
2019-03-06 17:02:20 -08:00
|
|
|
.value_name("NUM")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.help("The number of lamports to send to staking account"),
|
2019-04-10 17:52:47 -07:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("commission")
|
|
|
|
.value_name("NUM")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("The commission on rewards this vote account should take, defaults to zero")
|
2019-03-06 17:02:20 -08:00
|
|
|
),
|
2019-04-10 17:52:47 -07:00
|
|
|
|
2019-03-06 17:02:20 -08:00
|
|
|
)
|
2019-04-17 07:45:07 -07:00
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("show-vote-account")
|
|
|
|
.about("Show the contents of a vote account")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("voting_account_id")
|
|
|
|
.index(1)
|
|
|
|
.value_name("PUBKEY")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.validator(is_pubkey)
|
|
|
|
.help("Vote account pubkey"),
|
|
|
|
)
|
|
|
|
)
|
2019-01-16 20:43:00 -08:00
|
|
|
.subcommand(
|
2018-10-22 21:21:33 -07:00
|
|
|
SubCommand::with_name("deploy")
|
|
|
|
.about("Deploy a program")
|
|
|
|
.arg(
|
2019-03-07 16:00:12 -08:00
|
|
|
Arg::with_name("program_location")
|
2018-10-22 21:21:33 -07:00
|
|
|
.index(1)
|
|
|
|
.value_name("PATH")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.help("/path/to/program.o"),
|
2019-01-16 20:43:00 -08:00
|
|
|
), // TODO: Add "loader" argument; current default is bpf_loader
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("get-transaction-count").about("Get current transaction count"),
|
|
|
|
)
|
|
|
|
.subcommand(
|
2018-06-29 18:33:20 -07:00
|
|
|
SubCommand::with_name("pay")
|
|
|
|
.about("Send a payment")
|
|
|
|
.arg(
|
2018-09-18 14:42:32 -07:00
|
|
|
Arg::with_name("to")
|
|
|
|
.index(1)
|
|
|
|
.value_name("PUBKEY")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2019-03-20 15:20:31 -07:00
|
|
|
.validator(is_pubkey)
|
2018-09-18 14:42:32 -07:00
|
|
|
.help("The pubkey of recipient"),
|
2019-01-16 20:43:00 -08:00
|
|
|
)
|
|
|
|
.arg(
|
2019-03-05 17:22:46 -08:00
|
|
|
Arg::with_name("lamports")
|
2018-09-18 14:42:32 -07:00
|
|
|
.index(2)
|
2018-09-14 15:32:57 -07:00
|
|
|
.value_name("NUM")
|
2018-06-29 18:33:20 -07:00
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2019-03-05 17:22:46 -08:00
|
|
|
.help("The number of lamports to send"),
|
2019-01-16 20:43:00 -08:00
|
|
|
)
|
|
|
|
.arg(
|
2018-09-18 14:42:32 -07:00
|
|
|
Arg::with_name("timestamp")
|
|
|
|
.long("after")
|
|
|
|
.value_name("DATETIME")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("A timestamp after which transaction will execute"),
|
2019-01-16 20:43:00 -08:00
|
|
|
)
|
|
|
|
.arg(
|
2019-03-07 16:00:12 -08:00
|
|
|
Arg::with_name("timestamp_pubkey")
|
2018-09-18 14:42:32 -07:00
|
|
|
.long("require-timestamp-from")
|
2018-06-29 18:33:20 -07:00
|
|
|
.value_name("PUBKEY")
|
|
|
|
.takes_value(true)
|
2018-09-18 14:42:32 -07:00
|
|
|
.requires("timestamp")
|
2019-03-20 15:20:31 -07:00
|
|
|
.validator(is_pubkey)
|
2018-09-18 14:42:32 -07:00
|
|
|
.help("Require timestamp from this third party"),
|
2019-01-16 20:43:00 -08:00
|
|
|
)
|
|
|
|
.arg(
|
2018-09-18 14:42:32 -07:00
|
|
|
Arg::with_name("witness")
|
|
|
|
.long("require-signature-from")
|
|
|
|
.value_name("PUBKEY")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple(true)
|
|
|
|
.use_delimiter(true)
|
2019-03-20 15:20:31 -07:00
|
|
|
.validator(is_pubkey)
|
2019-03-05 17:22:46 -08:00
|
|
|
.help("Any third party signatures required to unlock the lamports"),
|
2019-01-16 20:43:00 -08:00
|
|
|
)
|
|
|
|
.arg(
|
2018-09-21 18:51:42 -07:00
|
|
|
Arg::with_name("cancelable")
|
|
|
|
.long("cancelable")
|
|
|
|
.takes_value(false),
|
2018-06-29 18:33:20 -07:00
|
|
|
),
|
2019-01-16 20:43:00 -08:00
|
|
|
)
|
|
|
|
.subcommand(
|
2018-09-18 14:42:32 -07:00
|
|
|
SubCommand::with_name("send-signature")
|
|
|
|
.about("Send a signature to authorize a transfer")
|
2018-06-29 18:33:20 -07:00
|
|
|
.arg(
|
2018-09-24 09:23:16 -07:00
|
|
|
Arg::with_name("to")
|
2018-06-29 18:33:20 -07:00
|
|
|
.index(1)
|
2018-09-24 09:23:16 -07:00
|
|
|
.value_name("PUBKEY")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2019-03-20 15:20:31 -07:00
|
|
|
.validator(is_pubkey)
|
2018-09-24 09:23:16 -07:00
|
|
|
.help("The pubkey of recipient"),
|
2019-01-16 20:43:00 -08:00
|
|
|
)
|
|
|
|
.arg(
|
2019-03-07 16:00:12 -08:00
|
|
|
Arg::with_name("process_id")
|
2018-09-24 09:23:16 -07:00
|
|
|
.index(2)
|
2018-09-18 14:42:32 -07:00
|
|
|
.value_name("PROCESS_ID")
|
|
|
|
.takes_value(true)
|
2018-06-29 18:33:20 -07:00
|
|
|
.required(true)
|
2019-01-16 20:43:00 -08:00
|
|
|
.help("The process id of the transfer to authorize"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.subcommand(
|
2018-09-18 14:42:32 -07:00
|
|
|
SubCommand::with_name("send-timestamp")
|
|
|
|
.about("Send a timestamp to unlock a transfer")
|
|
|
|
.arg(
|
2018-09-22 16:51:21 -07:00
|
|
|
Arg::with_name("to")
|
2018-09-18 14:42:32 -07:00
|
|
|
.index(1)
|
2018-09-22 16:51:21 -07:00
|
|
|
.value_name("PUBKEY")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2019-03-20 15:20:31 -07:00
|
|
|
.validator(is_pubkey)
|
2018-09-22 16:51:21 -07:00
|
|
|
.help("The pubkey of recipient"),
|
2019-01-16 20:43:00 -08:00
|
|
|
)
|
|
|
|
.arg(
|
2019-03-07 16:00:12 -08:00
|
|
|
Arg::with_name("process_id")
|
2018-09-22 16:51:21 -07:00
|
|
|
.index(2)
|
2018-09-18 14:42:32 -07:00
|
|
|
.value_name("PROCESS_ID")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
2019-01-16 20:43:00 -08:00
|
|
|
.help("The process id of the transfer to unlock"),
|
|
|
|
)
|
|
|
|
.arg(
|
2018-09-18 14:42:32 -07:00
|
|
|
Arg::with_name("datetime")
|
|
|
|
.long("date")
|
|
|
|
.value_name("DATETIME")
|
|
|
|
.takes_value(true)
|
2019-01-16 20:43:00 -08:00
|
|
|
.help("Optional arbitrary timestamp to apply"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.get_matches();
|
2018-06-26 22:52:34 -07:00
|
|
|
|
2018-11-09 14:36:08 -08:00
|
|
|
let config = parse_args(&matches)?;
|
2018-09-20 22:27:06 -07:00
|
|
|
let result = process_command(&config)?;
|
2018-09-17 12:15:26 -07:00
|
|
|
println!("{}", result);
|
|
|
|
Ok(())
|
2018-06-29 10:38:00 -07:00
|
|
|
}
|