Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions clap-v3-utils/src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,12 +681,12 @@ pub fn signer_from_source_with_config(
}
}
SignerSourceKind::Pubkey(pubkey) => {
let presigner = try_pubkeys_sigs_of(matches, SIGNER_ARG.name)?
let presigner = try_pubkeys_sigs_of(matches, SIGNER_ARG.name).ok().flatten()
.as_ref()
.and_then(|presigners| presigner_from_pubkey_sigs(pubkey, presigners));
if let Some(presigner) = presigner {
Ok(Box::new(presigner))
} else if config.allow_null_signer || matches.try_contains_id(SIGN_ONLY_ARG.name)? {
} else if config.allow_null_signer || matches.try_contains_id(SIGN_ONLY_ARG.name).unwrap_or(false) {
Ok(Box::new(NullSigner::new(pubkey)))
} else {
Err(std::io::Error::new(
Expand Down Expand Up @@ -1289,4 +1289,54 @@ mod tests {

Ok(())
}

#[test]
fn test_signer_from_source_can_parse_null_signer() {
let pubkey = Pubkey::new_unique();
let source = SignerSource {
kind: SignerSourceKind::Pubkey(pubkey),
derivation_path: None,
legacy: false,
};

// Note offline args not passes. UnknownArgument should be handled internally.
let clap_app = Command::new("test_app");
let matches = clap_app.get_matches_from(Vec::<&str>::new());

let config = SignerFromPathConfig {
allow_null_signer: true,
};

// This will be a NullSigner
let signer =
signer_from_source_with_config(&matches, &source, "test_key", &mut None, &config)
.unwrap();
assert_eq!(signer.pubkey(), pubkey);
}

#[test]
fn test_signer_from_source_pubkey_error_on_missing_sig() {
let pubkey = Pubkey::new_unique();
let source = SignerSource {
kind: SignerSourceKind::Pubkey(pubkey),
derivation_path: None,
legacy: false,
};

// Note offline args not passes. UnknownArgument should be handled internally.
let clap_app = Command::new("test_app");
let matches = clap_app.get_matches_from(Vec::<&str>::new());

// Should gracefully pass through the NullSigner conditional branch
// and end up in the error clause at the end.
let config = SignerFromPathConfig {
allow_null_signer: false,
};

let result =
signer_from_source_with_config(&matches, &source, "test_key", &mut None, &config);
assert!(result.is_err());
let err_string = result.err().unwrap().to_string();
assert!(err_string.contains(&format!("missing signature for supplied pubkey: {pubkey}")));
}
}
Loading