From 306dacf3598db5ce52e8e66c3fe42557861c9f83 Mon Sep 17 00:00:00 2001 From: Luis Schwab Date: Sun, 9 Mar 2025 20:29:40 -0300 Subject: [PATCH 1/5] feat: implement Display for KeychainKind --- wallet/src/types.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/wallet/src/types.rs b/wallet/src/types.rs index 54ddfa26..72c0e453 100644 --- a/wallet/src/types.rs +++ b/wallet/src/types.rs @@ -12,6 +12,7 @@ use alloc::boxed::Box; use chain::{ChainPosition, ConfirmationBlockTime}; use core::convert::AsRef; +use core::fmt; use bitcoin::transaction::{OutPoint, Sequence, TxOut}; use bitcoin::{psbt, Weight}; @@ -37,6 +38,15 @@ impl KeychainKind { } } +impl fmt::Display for KeychainKind { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + KeychainKind::External => write!(f, "External"), + KeychainKind::Internal => write!(f, "Internal"), + } + } +} + impl AsRef<[u8]> for KeychainKind { fn as_ref(&self) -> &[u8] { match self { From 2914c07a824027ace5f847c7b25c69c7537afdf4 Mon Sep 17 00:00:00 2001 From: Luis Schwab Date: Sun, 9 Mar 2025 20:30:24 -0300 Subject: [PATCH 2/5] feat: implement Display for LoadMismatch --- wallet/src/wallet/mod.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/wallet/src/wallet/mod.rs b/wallet/src/wallet/mod.rs index e4dc6d05..d3cb7ac3 100644 --- a/wallet/src/wallet/mod.rs +++ b/wallet/src/wallet/mod.rs @@ -232,6 +232,44 @@ pub enum LoadMismatch { }, } +impl fmt::Display for LoadMismatch { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + LoadMismatch::Network { loaded, expected } => { + write!( + f, + "Network mismatch: loaded {}, expected {}", + loaded, expected + ) + } + LoadMismatch::Genesis { loaded, expected } => { + write!( + f, + "Genesis hash mismatch: loaded {}, expected {}", + loaded, expected + ) + } + LoadMismatch::Descriptor { + keychain, + loaded, + expected, + } => { + write!( + f, + "Descriptor mismatch for {} keychain: loaded {}, expected {}", + keychain, + loaded + .as_ref() + .map_or("None".to_string(), |d| d.to_string()), + expected + .as_ref() + .map_or("None".to_string(), |d| d.to_string()) + ) + } + } + } +} + impl From for LoadError { fn from(mismatch: LoadMismatch) -> Self { Self::Mismatch(mismatch) From f6d34d7a75a3560c464eb761d31c60ae0bc030fc Mon Sep 17 00:00:00 2001 From: Luis Schwab Date: Sun, 9 Mar 2025 20:31:00 -0300 Subject: [PATCH 3/5] fix: no Debug on LoadError --- wallet/src/wallet/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wallet/src/wallet/mod.rs b/wallet/src/wallet/mod.rs index d3cb7ac3..51355291 100644 --- a/wallet/src/wallet/mod.rs +++ b/wallet/src/wallet/mod.rs @@ -194,9 +194,9 @@ impl fmt::Display for LoadError { LoadError::MissingNetwork => write!(f, "loaded data is missing network type"), LoadError::MissingGenesis => write!(f, "loaded data is missing genesis hash"), LoadError::MissingDescriptor(k) => { - write!(f, "loaded data is missing descriptor for keychain {k:?}") + write!(f, "loaded data is missing descriptor for {k} keychain") } - LoadError::Mismatch(mismatch) => write!(f, "data mismatch: {mismatch:?}"), + LoadError::Mismatch(e) => write!(f, "{e}"), } } } From b37355a2fa3c97d5c0c2c155288651f02dc9ff3a Mon Sep 17 00:00:00 2001 From: Luis Schwab Date: Thu, 13 Mar 2025 15:42:20 -0300 Subject: [PATCH 4/5] fix: remove Debugs from CreateTxError --- wallet/src/wallet/error.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wallet/src/wallet/error.rs b/wallet/src/wallet/error.rs index adce5b18..96f0250d 100644 --- a/wallet/src/wallet/error.rs +++ b/wallet/src/wallet/error.rs @@ -112,7 +112,7 @@ impl fmt::Display for CreateTxError { Self::Descriptor(e) => e.fmt(f), Self::Policy(e) => e.fmt(f), CreateTxError::SpendingPolicyRequired(keychain_kind) => { - write!(f, "Spending policy required: {:?}", keychain_kind) + write!(f, "Spending policy required: {}", keychain_kind) } CreateTxError::Version0 => { write!(f, "Invalid version `0`") @@ -127,12 +127,12 @@ impl fmt::Display for CreateTxError { requested, required, } => { - write!(f, "TxBuilder requested timelock of `{:?}`, but at least `{:?}` is required to spend from this script", required, requested) + write!(f, "TxBuilder requested timelock of `{}`, but at least `{}` is required to spend from this script", requested, required) } CreateTxError::RbfSequenceCsv { sequence, csv } => { write!( f, - "Cannot enable RBF with nSequence `{:?}` given a required OP_CSV of `{:?}`", + "Cannot enable RBF with nSequence `{}` given a required OP_CSV of `{}`", sequence, csv ) } From 0d8d0ece8e6ed19461c91100751fc125cc6a866b Mon Sep 17 00:00:00 2001 From: Luis Schwab Date: Fri, 21 Mar 2025 16:10:04 -0300 Subject: [PATCH 5/5] feat: add error messages for CreateWithPersistError Display implementation --- wallet/src/wallet/persisted.rs | 54 ++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/wallet/src/wallet/persisted.rs b/wallet/src/wallet/persisted.rs index 28a6ec78..239609f8 100644 --- a/wallet/src/wallet/persisted.rs +++ b/wallet/src/wallet/persisted.rs @@ -6,10 +6,16 @@ use core::{ pin::Pin, }; -use alloc::boxed::Box; +use alloc::{ + boxed::Box, + string::{String, ToString}, +}; use chain::Merge; -use crate::{descriptor::DescriptorError, ChangeSet, CreateParams, LoadParams, Wallet}; +use crate::{ + descriptor::{calc_checksum, DescriptorError}, + ChangeSet, CreateParams, LoadParams, Wallet, +}; /// Trait that persists [`PersistedWallet`]. /// @@ -363,16 +369,46 @@ pub enum CreateWithPersistError { impl fmt::Display for CreateWithPersistError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::Persist(err) => fmt::Display::fmt(err, f), - Self::DataAlreadyExists(changeset) => write!( - f, - "Cannot create wallet in persister which already contains wallet data: {:?}", - changeset - ), - Self::Descriptor(err) => fmt::Display::fmt(&err, f), + Self::Persist(err) => write!(f, "{}", err), + Self::DataAlreadyExists(changeset) => { + write!( + f, + "Cannot create wallet in a persister which already contains data: {}", + changeset_info(changeset) + ) + } + Self::Descriptor(err) => { + write!(f, "{err}") + } } } } #[cfg(feature = "std")] impl std::error::Error for CreateWithPersistError {} + +/// Helper function to display basic information about a [`ChangeSet`]. +fn changeset_info(changeset: &ChangeSet) -> String { + let network = match &changeset.network { + Some(network) => network.to_string(), + None => "None".to_string(), + }; + + let mut checksum = String::new(); + if let Some(desc) = &changeset.descriptor { + if let Ok(ck) = calc_checksum(&desc.to_string()) { + checksum += ck.as_str(); + } + } + if let Some(desc) = &changeset.change_descriptor { + if let Ok(ck) = calc_checksum(&desc.to_string()) { + checksum += ck.as_str(); + } + } + // Don't return an empty checksum + if checksum.is_empty() { + checksum = "None".to_string(); + } + + format!("Network: {}, Descriptor Checksum: {}", network, checksum) +}