Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Snafu error types and proper Rust typed functions #21

Merged
merged 14 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
63 changes: 63 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ bitcoin = { version = "0.31.2" }
bitcoin_hashes = "0.14.0"
hex = "0.4.3"
miniscript = "11.0.0"
snafu = "0.8.4"

[dev-dependencies]
pretty_assertions = "1.4.0"
69 changes: 62 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,63 @@
#[derive(Debug, PartialEq, Eq)]
pub enum Bip322Error {
InvalidAddress, // for legacy addresses 1... (p2pkh) not supported, also any non taproot
Invalid, // Address no key; pubkey not recovered, invalid signature
MalformedSignature, // wrong length, etc.
InvalidSigHash, // only sighash All and Default supported
NotKeyPathSpend, // only single key path spend supported
use super::*;

#[derive(Debug, Snafu)]
#[snafu(context(suffix(false)), visibility(pub))]
pub enum Error {
#[snafu(display("Failed to parse address `{address}`"))]
AddressParse {
source: bitcoin::address::ParseError,
address: String,
},
#[snafu(display("Failed to parse private key"))]
PrivateKeyParse { source: bitcoin::key::Error },
#[snafu(display("Unsuported address `{address}`, only P2TR allowed"))]
UnsupportedAddress { address: String },
#[snafu(display("Decode error for signature `{signature}`"))]
SignatureDecode {
source: base64::DecodeError,
signature: String,
},
#[snafu(display("Transaction encode error"))]
TransactionEncode { source: std::io::Error },
#[snafu(display("Transaction extract error"))]
TransactionExtract {
source: bitcoin::psbt::ExtractTxError,
},
#[snafu(display("To sign transaction invalid"))]
ToSignInvalid,
#[snafu(display("PSBT extract error"))]
PsbtExtract { source: bitcoin::psbt::Error },
#[snafu(display("Base64 decode error for transaction `{transaction}`"))]
TransactionBase64Decode {
source: base64::DecodeError,
transaction: String,
},
#[snafu(display("Consensus decode error for transaction `{transaction}`"))]
TransactionConsensusDecode {
source: bitcoin::consensus::encode::Error,
transaction: String,
},
#[snafu(display("Witness malformed"))]
WitnessMalformed {
source: bitcoin::consensus::encode::Error,
},
#[snafu(display("Witness empty"))]
WitnessEmpty,
#[snafu(display("Encode witness error"))]
WitnessEncoding { source: std::io::Error },
#[snafu(display("Signature of wrong length `{length}`"))]
SignatureLength {
length: usize,
encoded_signature: Vec<u8>,
},
#[snafu(display("Invalid signature"))]
SignatureInvalid { source: bitcoin::secp256k1::Error },
#[snafu(display("Invalid sighash"))]
SigHashTypeInvalid {
source: bitcoin::sighash::InvalidSighashTypeError,
},
#[snafu(display("Unsupported sighash type `{sighash_type}`"))]
SigHashTypeUnsupported { sighash_type: String },
#[snafu(display("Not key path spend"))]
NotKeyPathSpend,
}
Loading
Loading