Skip to content

Commit

Permalink
Pretty-print SignerErrors
Browse files Browse the repository at this point in the history
In particular, add a hint about the Ledger "blind signing" setting, to
make the error a bit more actionable for the user.
  • Loading branch information
ruuda committed Aug 12, 2021
1 parent 7940108 commit 03957d0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
69 changes: 69 additions & 0 deletions cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use solana_program::instruction::InstructionError;
use solana_program::program_error::ProgramError;
use solana_program::pubkey::PubkeyError;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signer::presigner::PresignerError;
use solana_sdk::signer::SignerError;
use solana_sdk::transaction::TransactionError;

use lido::error::LidoError;
Expand Down Expand Up @@ -323,6 +325,73 @@ impl AsPrettyError for PubkeyError {
}
}

impl AsPrettyError for SignerError {
fn print_pretty(&self) {
print_red("Failed to sign transaction: ");
// `SignerError` does implement display, but the messages are low-quality
// and not any more helpful than the enum names, so we write custom descriptions
// here to be a bit more user-friendly.
match self {
SignerError::KeypairPubkeyMismatch => {
println!("Mismatch between keypair and pubkey.");
}
SignerError::NotEnoughSigners => {
println!("Not enough signers.");
println!(
"This is a programming error, please report a bug at \
https://github.com/chorusone/solido/issues/new."
);
}
SignerError::TransactionError(err) => {
println!("Transaction error while signing.");
err.print_pretty();
}
SignerError::Custom(message) => {
println!("Custom error.");
print_key("Message:");
println!(" {}", message)
}
SignerError::PresignerError(PresignerError::VerificationFailure) => {
println!("Pre-signer error.");
print_key("Message:");
println!(" {}", PresignerError::VerificationFailure);
}
SignerError::Connection(message) => {
println!("Connection error while signing with remote keypair.");
print_key("Connection error:");
println!(" {}", message);
}
SignerError::InvalidInput(message) => {
println!("Invalid input.");
print_key("Message:");
println!(" {}", message);
}
SignerError::NoDeviceFound => {
println!("No device found.");
}
SignerError::Protocol(message) => {
println!("Protocol error.");
print_key("Message:");
println!(" {}", message);
// When using the Ledger hardware wallet, if blind signing is
// disabled in its Solana app, we get "Ledger operation not supported"
// as message. Try to help the user debug this.
if message.contains("Ledger") {
print_key("Note:");
println!(
" Is the 'blind signing' setting enabled in the Solana app on the device?"
);
}
}
SignerError::UserCancel(message) => {
println!("Signing cancelled by user.");
print_key("Message: ");
println!(" {}", message);
}
}
}
}

impl AsPrettyError for Box<dyn AsPrettyError + 'static> {
fn print_pretty(&self) {
(**self).print_pretty()
Expand Down
5 changes: 4 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ impl<'a> SnapshotConfig<'a> {
) -> snapshot::Result<Transaction> {
let mut tx = Transaction::new_with_payer(instructions, Some(&self.signer.pubkey()));
let recent_blockhash = self.client.get_recent_blockhash()?;
tx.sign(signers, recent_blockhash);
tx.try_sign(signers, recent_blockhash).map_err(|err| {
let boxed_error: Error = Box::new(err);
boxed_error
})?;
Ok(tx)
}

Expand Down

0 comments on commit 03957d0

Please sign in to comment.