|
1 | 1 | use {
|
2 | 2 | crate::keypair::{
|
3 |
| - keypair_from_seed_phrase, pubkey_from_path, resolve_signer_from_path, signer_from_path, |
| 3 | + keypair_from_seed_phrase, keypair_from_source, pubkey_from_path, pubkey_from_source, |
| 4 | + resolve_signer_from_path, resolve_signer_from_source, signer_from_path, signer_from_source, |
4 | 5 | ASK_KEYWORD, SKIP_SEED_PHRASE_VALIDATION_ARG,
|
5 | 6 | },
|
6 | 7 | clap::{builder::ValueParser, ArgMatches},
|
@@ -89,6 +90,112 @@ impl SignerSource {
|
89 | 90 | }
|
90 | 91 | }
|
91 | 92 |
|
| 93 | + pub fn try_get_keypair( |
| 94 | + matches: &ArgMatches, |
| 95 | + name: &str, |
| 96 | + ) -> Result<Option<Keypair>, Box<dyn error::Error>> { |
| 97 | + let source = matches.try_get_one::<Self>(name)?; |
| 98 | + if let Some(source) = source { |
| 99 | + keypair_from_source(matches, source, name, true).map(Some) |
| 100 | + } else { |
| 101 | + Ok(None) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + pub fn try_get_keypairs( |
| 106 | + matches: &ArgMatches, |
| 107 | + name: &str, |
| 108 | + ) -> Result<Option<Vec<Keypair>>, Box<dyn error::Error>> { |
| 109 | + let sources = matches.try_get_many::<Self>(name)?; |
| 110 | + if let Some(sources) = sources { |
| 111 | + let keypairs = sources |
| 112 | + .filter_map(|source| keypair_from_source(matches, source, name, true).ok()) |
| 113 | + .collect(); |
| 114 | + Ok(Some(keypairs)) |
| 115 | + } else { |
| 116 | + Ok(None) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + #[allow(clippy::type_complexity)] |
| 121 | + pub fn try_get_signer( |
| 122 | + matches: &ArgMatches, |
| 123 | + name: &str, |
| 124 | + wallet_manager: &mut Option<Rc<RemoteWalletManager>>, |
| 125 | + ) -> Result<Option<(Box<dyn Signer>, Pubkey)>, Box<dyn error::Error>> { |
| 126 | + let source = matches.try_get_one::<Self>(name)?; |
| 127 | + if let Some(source) = source { |
| 128 | + let signer = signer_from_source(matches, source, name, wallet_manager)?; |
| 129 | + let signer_pubkey = signer.pubkey(); |
| 130 | + Ok(Some((signer, signer_pubkey))) |
| 131 | + } else { |
| 132 | + Ok(None) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + #[allow(clippy::type_complexity)] |
| 137 | + pub fn try_get_signers( |
| 138 | + matches: &ArgMatches, |
| 139 | + name: &str, |
| 140 | + wallet_manager: &mut Option<Rc<RemoteWalletManager>>, |
| 141 | + ) -> Result<Option<Vec<(Box<dyn Signer>, Pubkey)>>, Box<dyn error::Error>> { |
| 142 | + let sources = matches.try_get_many::<Self>(name)?; |
| 143 | + if let Some(sources) = sources { |
| 144 | + let signers = sources |
| 145 | + .filter_map(|source| { |
| 146 | + let signer = signer_from_source(matches, source, name, wallet_manager).ok()?; |
| 147 | + let signer_pubkey = signer.pubkey(); |
| 148 | + Some((signer, signer_pubkey)) |
| 149 | + }) |
| 150 | + .collect(); |
| 151 | + Ok(Some(signers)) |
| 152 | + } else { |
| 153 | + Ok(None) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + pub fn try_get_pubkey( |
| 158 | + matches: &ArgMatches, |
| 159 | + name: &str, |
| 160 | + wallet_manager: &mut Option<Rc<RemoteWalletManager>>, |
| 161 | + ) -> Result<Option<Pubkey>, Box<dyn error::Error>> { |
| 162 | + let source = matches.try_get_one::<Self>(name)?; |
| 163 | + if let Some(source) = source { |
| 164 | + pubkey_from_source(matches, source, name, wallet_manager).map(Some) |
| 165 | + } else { |
| 166 | + Ok(None) |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + pub fn try_get_pubkeys( |
| 171 | + matches: &ArgMatches, |
| 172 | + name: &str, |
| 173 | + wallet_manager: &mut Option<Rc<RemoteWalletManager>>, |
| 174 | + ) -> Result<Option<Vec<Pubkey>>, Box<dyn std::error::Error>> { |
| 175 | + let sources = matches.try_get_many::<Self>(name)?; |
| 176 | + if let Some(sources) = sources { |
| 177 | + let pubkeys = sources |
| 178 | + .filter_map(|source| pubkey_from_source(matches, source, name, wallet_manager).ok()) |
| 179 | + .collect(); |
| 180 | + Ok(Some(pubkeys)) |
| 181 | + } else { |
| 182 | + Ok(None) |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + pub fn try_resolve( |
| 187 | + matches: &ArgMatches, |
| 188 | + name: &str, |
| 189 | + wallet_manager: &mut Option<Rc<RemoteWalletManager>>, |
| 190 | + ) -> Result<Option<String>, Box<dyn std::error::Error>> { |
| 191 | + let source = matches.try_get_one::<Self>(name)?; |
| 192 | + if let Some(source) = source { |
| 193 | + resolve_signer_from_source(matches, source, name, wallet_manager) |
| 194 | + } else { |
| 195 | + Ok(None) |
| 196 | + } |
| 197 | + } |
| 198 | + |
92 | 199 | pub fn parse<S: AsRef<str>>(source: S) -> Result<Self, SignerSourceError> {
|
93 | 200 | let source = source.as_ref();
|
94 | 201 | let source = {
|
|
0 commit comments