From 5d1ef5d01dfbb9b69de312f290117409a871ccac Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Thu, 15 Apr 2021 01:01:58 -0600 Subject: [PATCH] clap-utils: Use `uriparse` crate to parse `SignerSource` --- Cargo.lock | 11 ++++++++ clap-utils/Cargo.toml | 1 + clap-utils/src/keypair.rs | 56 ++++++++++++++++++++++++++++++++------- programs/bpf/Cargo.lock | 11 ++++++++ 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fd4e60c7d..3d9c2069a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4092,6 +4092,7 @@ dependencies = [ "solana-sdk", "thiserror", "tiny-bip39 0.8.0", + "uriparse", "url 2.2.0", ] @@ -6480,6 +6481,16 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" +[[package]] +name = "uriparse" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e515b1ada404168e145ac55afba3c42f04cf972201a8552d42e2abb17c1b7221" +dependencies = [ + "fnv", + "lazy_static", +] + [[package]] name = "url" version = "1.7.2" diff --git a/clap-utils/Cargo.toml b/clap-utils/Cargo.toml index 5609af298..c8560cc2f 100644 --- a/clap-utils/Cargo.toml +++ b/clap-utils/Cargo.toml @@ -16,6 +16,7 @@ solana-remote-wallet = { path = "../remote-wallet", version = "=1.7.0" } solana-sdk = { path = "../sdk", version = "=1.7.0" } thiserror = "1.0.21" tiny-bip39 = "0.8.0" +uriparse = "0.6.3" url = "2.1.0" chrono = "0.4" diff --git a/clap-utils/src/keypair.rs b/clap-utils/src/keypair.rs index 394b50dc3..091f2ea3e 100644 --- a/clap-utils/src/keypair.rs +++ b/clap-utils/src/keypair.rs @@ -20,6 +20,7 @@ use solana_sdk::{ }, }; use std::{ + convert::TryFrom, error, io::{stdin, stdout, Write}, process::exit, @@ -142,16 +143,27 @@ pub(crate) enum SignerSource { } pub(crate) fn parse_signer_source>(source: S) -> SignerSource { - if path == "-" { - SignerSource::Stdin - } else if path == ASK_KEYWORD { - SignerSource::Ask - } else if path.starts_with("usb://") { - SignerSource::Usb(path.to_string()) - } else if let Ok(pubkey) = Pubkey::from_str(path) { - SignerSource::Pubkey(pubkey) - } else { - SignerSource::Filepath(path.to_string()) + let source = source.as_ref(); + match uriparse::URIReference::try_from(source) { + Err(_) => SignerSource::Filepath(source.to_string()), + Ok(uri) => { + if let Some(scheme) = uri.scheme() { + let scheme = scheme.as_str().to_ascii_lowercase(); + match scheme.as_str() { + "usb" => SignerSource::Usb(source.to_string()), + _ => SignerSource::Filepath(source.to_string()), + } + } else { + match source { + "-" => SignerSource::Stdin, + ASK_KEYWORD => SignerSource::Ask, + _ => match Pubkey::from_str(source) { + Ok(pubkey) => SignerSource::Pubkey(pubkey), + Err(_) => SignerSource::Filepath(source.to_string()), + }, + } + } + } } } @@ -443,4 +455,28 @@ mod tests { ]; assert_eq!(signer_pubkeys, expect); } + + #[test] + fn test_parse_signer_source() { + assert!(matches!(parse_signer_source("-"), SignerSource::Stdin)); + assert!(matches!( + parse_signer_source(ASK_KEYWORD), + SignerSource::Ask + )); + let pubkey = Pubkey::new_unique(); + assert!( + matches!(parse_signer_source(&pubkey.to_string()), SignerSource::Pubkey(p) if p == pubkey) + ); + let path = "/absolute/path".to_string(); + assert!(matches!(parse_signer_source(&path), SignerSource::Filepath(p) if p == path)); + let path = "relative/path".to_string(); + assert!(matches!(parse_signer_source(&path), SignerSource::Filepath(p) if p == path)); + let usb = "usb://ledger".to_string(); + assert!(matches!(parse_signer_source(&usb), SignerSource::Usb(u) if u == usb)); + // Catchall into SignerSource::Filepath + let junk = "sometextthatisnotapubkey".to_string(); + assert!(Pubkey::from_str(&junk).is_err()); + assert!(matches!(parse_signer_source(&junk), SignerSource::Filepath(j) if j == junk)); + + } } diff --git a/programs/bpf/Cargo.lock b/programs/bpf/Cargo.lock index b0f3ff662..9cb8b0005 100644 --- a/programs/bpf/Cargo.lock +++ b/programs/bpf/Cargo.lock @@ -2994,6 +2994,7 @@ dependencies = [ "solana-sdk", "thiserror", "tiny-bip39", + "uriparse", "url", ] @@ -4244,6 +4245,16 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" +[[package]] +name = "uriparse" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e515b1ada404168e145ac55afba3c42f04cf972201a8552d42e2abb17c1b7221" +dependencies = [ + "fnv", + "lazy_static", +] + [[package]] name = "url" version = "2.2.1"