feat: add psbt related errors

This commit is contained in:
thunderbiscuit 2024-04-05 10:30:28 -04:00
parent ab2e97e782
commit 0e617bc986
No known key found for this signature in database
GPG Key ID: 88253696EB836462
4 changed files with 44 additions and 16 deletions

View File

@ -82,6 +82,12 @@ interface TransactionError {
OtherTransactionError();
};
[Error]
interface PsbtParseError {
PsbtEncoding(string e);
Base64Encoding(string e);
};
// ------------------------------------------------------------------------
// bdk crate - types module
// ------------------------------------------------------------------------
@ -424,7 +430,7 @@ interface Transaction {
};
interface PartiallySignedTransaction {
[Throws=Alpha3Error]
[Throws=PsbtParseError]
constructor(string psbt_base64);
string serialize();

View File

@ -1,4 +1,4 @@
use crate::error::{Alpha3Error, TransactionError};
use crate::error::{PsbtParseError, TransactionError};
use crate::error::AddressError;
use bdk::bitcoin::address::{NetworkChecked, NetworkUnchecked};
@ -195,10 +195,10 @@ pub struct PartiallySignedTransaction {
}
impl PartiallySignedTransaction {
pub(crate) fn new(psbt_base64: String) -> Result<Self, Alpha3Error> {
pub(crate) fn new(psbt_base64: String) -> Result<Self, PsbtParseError> {
let psbt: BdkPartiallySignedTransaction =
BdkPartiallySignedTransaction::from_str(&psbt_base64)
.map_err(|_| Alpha3Error::Generic)?;
BdkPartiallySignedTransaction::from_str(&psbt_base64)?;
// .map_err(|_| Alpha3Error::Generic)?;
Ok(PartiallySignedTransaction {
inner: Mutex::new(psbt),

View File

@ -1,8 +1,9 @@
use crate::bitcoin::OutPoint;
use bdk::chain::tx_graph::CalculateFeeError as BdkCalculateFeeError;
use bdk_esplora::esplora_client::Error as BdkEsploraError;
use bdk_esplora::esplora_client::{Error as BdkEsploraError};
use bdk::bitcoin::psbt::PsbtParseError as BdkPsbtParseError;
use bdk::bitcoin::Network;
use bdk::descriptor::DescriptorError;
use bdk::wallet::error::{BuildFeeBumpError, CreateTxError};
@ -194,6 +195,29 @@ pub enum TransactionError {
OtherTransactionError,
}
#[derive(Debug, thiserror::Error)]
pub enum PsbtParseError {
#[error("error in internal PSBT data structure: {e}")]
PsbtEncoding { e: String },
#[error("error in PSBT base64 encoding: {e}")]
Base64Encoding { e: String },
}
impl From<BdkPsbtParseError> for PsbtParseError {
fn from(error: BdkPsbtParseError) -> Self {
match error {
BdkPsbtParseError::PsbtEncoding(e) => PsbtParseError::PsbtEncoding { e: e.to_string() },
BdkPsbtParseError::Base64Encoding(e) => {
PsbtParseError::Base64Encoding { e: e.to_string() }
}
_ => {
unreachable!("this is required because of the non-exhaustive enum in rust-bitcoin")
}
}
}
}
impl From<BdkFileError> for WalletCreationError {
fn from(error: BdkFileError) -> Self {
match error {
@ -340,12 +364,10 @@ impl From<bdk::bitcoin::address::Error> for AddressError {
AddressError::MalformedWitnessVersion
}
bdk::bitcoin::address::Error::InvalidWitnessProgramLength(length) => {
let length = length as u64;
AddressError::InvalidWitnessProgramLength { length }
AddressError::InvalidWitnessProgramLength { length: length as u64 }
}
bdk::bitcoin::address::Error::InvalidSegwitV0ProgramLength(length) => {
let length = length as u64;
AddressError::InvalidSegwitV0ProgramLength { length }
AddressError::InvalidSegwitV0ProgramLength { length: length as u64 }
}
bdk::bitcoin::address::Error::UncompressedPubkey => AddressError::UncompressedPubkey,
bdk::bitcoin::address::Error::ExcessiveScriptSize => AddressError::ExcessiveScriptSize,
@ -358,12 +380,10 @@ impl From<bdk::bitcoin::address::Error> for AddressError {
found,
address,
} => {
// let address = address.to_string();
let address = format!("{:?}", address);
AddressError::NetworkValidation {
required,
found,
address,
address: format!("{:?}", address),
}
}
_ => AddressError::OtherAddressError,
@ -379,9 +399,10 @@ impl From<bdk::bitcoin::consensus::encode::Error> for TransactionError {
TransactionError::OversizedVectorAllocation
}
bdk::bitcoin::consensus::encode::Error::InvalidChecksum { expected, actual } => {
let expected = DisplayHex::to_lower_hex_string(&expected);
let actual = DisplayHex::to_lower_hex_string(&actual);
TransactionError::InvalidChecksum { expected, actual }
TransactionError::InvalidChecksum {
expected: DisplayHex::to_lower_hex_string(&expected),
actual: DisplayHex::to_lower_hex_string(&actual),
}
}
bdk::bitcoin::consensus::encode::Error::NonMinimalVarInt => {
TransactionError::NonMinimalVarInt

View File

@ -19,6 +19,7 @@ use crate::error::CalculateFeeError;
use crate::error::EsploraError;
use crate::error::FeeRateError;
use crate::error::PersistenceError;
use crate::error::PsbtParseError;
use crate::error::TransactionError;
use crate::error::WalletCreationError;
use crate::esplora::EsploraClient;