From 0e617bc98623ceebcfc87ea550da154bc7f782a5 Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Fri, 5 Apr 2024 10:30:28 -0400 Subject: [PATCH] feat: add psbt related errors --- bdk-ffi/src/bdk.udl | 8 +++++++- bdk-ffi/src/bitcoin.rs | 8 ++++---- bdk-ffi/src/error.rs | 43 +++++++++++++++++++++++++++++++----------- bdk-ffi/src/lib.rs | 1 + 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index c09537a..e723fa8 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -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(); diff --git a/bdk-ffi/src/bitcoin.rs b/bdk-ffi/src/bitcoin.rs index ee83abe..8faae9d 100644 --- a/bdk-ffi/src/bitcoin.rs +++ b/bdk-ffi/src/bitcoin.rs @@ -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 { + pub(crate) fn new(psbt_base64: String) -> Result { 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), diff --git a/bdk-ffi/src/error.rs b/bdk-ffi/src/error.rs index c59b885..7d22e8d 100644 --- a/bdk-ffi/src/error.rs +++ b/bdk-ffi/src/error.rs @@ -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 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 for WalletCreationError { fn from(error: BdkFileError) -> Self { match error { @@ -340,12 +364,10 @@ impl From 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 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 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 diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index 5bf5979..30a0ea9 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -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;