feat: add sign related error
This commit is contained in:
parent
edea8e8c80
commit
c63e7ad392
@ -99,6 +99,24 @@ interface DescriptorError {
|
|||||||
Hex(string e);
|
Hex(string e);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
[Error]
|
||||||
|
interface SignerError {
|
||||||
|
MissingKey();
|
||||||
|
InvalidKey();
|
||||||
|
UserCanceled();
|
||||||
|
InputIndexOutOfRange();
|
||||||
|
MissingNonWitnessUtxo();
|
||||||
|
InvalidNonWitnessUtxo();
|
||||||
|
MissingWitnessUtxo();
|
||||||
|
MissingWitnessScript();
|
||||||
|
MissingHdKeypath();
|
||||||
|
NonStandardSighash();
|
||||||
|
InvalidSighash();
|
||||||
|
SighashError(string e);
|
||||||
|
MiniscriptPsbt(string e);
|
||||||
|
External(string e);
|
||||||
|
};
|
||||||
|
|
||||||
[Error]
|
[Error]
|
||||||
interface TxidParseError {
|
interface TxidParseError {
|
||||||
InvalidTxid(string txid);
|
InvalidTxid(string txid);
|
||||||
@ -213,7 +231,7 @@ interface Wallet {
|
|||||||
|
|
||||||
boolean is_mine([ByRef] Script script);
|
boolean is_mine([ByRef] Script script);
|
||||||
|
|
||||||
[Throws=Alpha3Error]
|
[Throws=SignerError]
|
||||||
boolean sign(Psbt psbt);
|
boolean sign(Psbt psbt);
|
||||||
|
|
||||||
SentAndReceivedValues sent_and_received([ByRef] Transaction tx);
|
SentAndReceivedValues sent_and_received([ByRef] Transaction tx);
|
||||||
|
@ -5,6 +5,7 @@ use bdk::bitcoin::Network;
|
|||||||
use bdk::chain::tx_graph::CalculateFeeError as BdkCalculateFeeError;
|
use bdk::chain::tx_graph::CalculateFeeError as BdkCalculateFeeError;
|
||||||
use bdk::descriptor::DescriptorError as BdkDescriptorError;
|
use bdk::descriptor::DescriptorError as BdkDescriptorError;
|
||||||
use bdk::wallet::error::{BuildFeeBumpError, CreateTxError};
|
use bdk::wallet::error::{BuildFeeBumpError, CreateTxError};
|
||||||
|
use bdk::wallet::signer::SignerError as BdkSignerError;
|
||||||
use bdk::wallet::tx_builder::{AddUtxoError, AllowShrinkingError};
|
use bdk::wallet::tx_builder::{AddUtxoError, AllowShrinkingError};
|
||||||
use bdk::wallet::{NewError, NewOrLoadError};
|
use bdk::wallet::{NewError, NewOrLoadError};
|
||||||
use bdk_esplora::esplora_client::{Error as BdkEsploraError, Error};
|
use bdk_esplora::esplora_client::{Error as BdkEsploraError, Error};
|
||||||
@ -233,6 +234,51 @@ pub enum DescriptorError {
|
|||||||
Hex { e: String },
|
Hex { e: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum SignerError {
|
||||||
|
#[error("Missing key for signing")]
|
||||||
|
MissingKey,
|
||||||
|
|
||||||
|
#[error("Invalid key provided")]
|
||||||
|
InvalidKey,
|
||||||
|
|
||||||
|
#[error("User canceled operation")]
|
||||||
|
UserCanceled,
|
||||||
|
|
||||||
|
#[error("Input index out of range")]
|
||||||
|
InputIndexOutOfRange,
|
||||||
|
|
||||||
|
#[error("Missing non-witness UTXO information")]
|
||||||
|
MissingNonWitnessUtxo,
|
||||||
|
|
||||||
|
#[error("Invalid non-witness UTXO information provided")]
|
||||||
|
InvalidNonWitnessUtxo,
|
||||||
|
|
||||||
|
#[error("Missing witness UTXO")]
|
||||||
|
MissingWitnessUtxo,
|
||||||
|
|
||||||
|
#[error("Missing witness script")]
|
||||||
|
MissingWitnessScript,
|
||||||
|
|
||||||
|
#[error("Missing HD keypath")]
|
||||||
|
MissingHdKeypath,
|
||||||
|
|
||||||
|
#[error("Non-standard sighash type used")]
|
||||||
|
NonStandardSighash,
|
||||||
|
|
||||||
|
#[error("Invalid sighash type provided")]
|
||||||
|
InvalidSighash,
|
||||||
|
|
||||||
|
#[error("Error with sighash computation: {e}")]
|
||||||
|
SighashError { e: String },
|
||||||
|
|
||||||
|
#[error("Miniscript Psbt error: {e}")]
|
||||||
|
MiniscriptPsbt { e: String },
|
||||||
|
|
||||||
|
#[error("External error: {e}")]
|
||||||
|
External { e: String },
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum ExtractTxError {
|
pub enum ExtractTxError {
|
||||||
#[error("an absurdly high fee rate of {fee_rate} sat/vbyte")]
|
#[error("an absurdly high fee rate of {fee_rate} sat/vbyte")]
|
||||||
@ -275,6 +321,29 @@ impl From<BdkDescriptorError> for DescriptorError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<BdkSignerError> for SignerError {
|
||||||
|
fn from(error: BdkSignerError) -> Self {
|
||||||
|
match error {
|
||||||
|
BdkSignerError::MissingKey => SignerError::MissingKey,
|
||||||
|
BdkSignerError::InvalidKey => SignerError::InvalidKey,
|
||||||
|
BdkSignerError::UserCanceled => SignerError::UserCanceled,
|
||||||
|
BdkSignerError::InputIndexOutOfRange => SignerError::InputIndexOutOfRange,
|
||||||
|
BdkSignerError::MissingNonWitnessUtxo => SignerError::MissingNonWitnessUtxo,
|
||||||
|
BdkSignerError::InvalidNonWitnessUtxo => SignerError::InvalidNonWitnessUtxo,
|
||||||
|
BdkSignerError::MissingWitnessUtxo => SignerError::MissingWitnessUtxo,
|
||||||
|
BdkSignerError::MissingWitnessScript => SignerError::MissingWitnessScript,
|
||||||
|
BdkSignerError::MissingHdKeypath => SignerError::MissingHdKeypath,
|
||||||
|
BdkSignerError::NonStandardSighash => SignerError::NonStandardSighash,
|
||||||
|
BdkSignerError::InvalidSighash => SignerError::InvalidSighash,
|
||||||
|
BdkSignerError::SighashError(e) => SignerError::SighashError { e: e.to_string() },
|
||||||
|
BdkSignerError::MiniscriptPsbt(e) => SignerError::MiniscriptPsbt {
|
||||||
|
e: format!("{:?}", e),
|
||||||
|
},
|
||||||
|
BdkSignerError::External(e) => SignerError::External { e },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<BdkPsbtParseError> for PsbtParseError {
|
impl From<BdkPsbtParseError> for PsbtParseError {
|
||||||
fn from(error: BdkPsbtParseError) -> Self {
|
fn from(error: BdkPsbtParseError) -> Self {
|
||||||
match error {
|
match error {
|
||||||
@ -509,6 +578,7 @@ mod test {
|
|||||||
use crate::error::{EsploraError, PersistenceError, WalletCreationError};
|
use crate::error::{EsploraError, PersistenceError, WalletCreationError};
|
||||||
use crate::CalculateFeeError;
|
use crate::CalculateFeeError;
|
||||||
use crate::OutPoint;
|
use crate::OutPoint;
|
||||||
|
use crate::SignerError;
|
||||||
use bdk::bitcoin::Network;
|
use bdk::bitcoin::Network;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -672,4 +742,55 @@ mod test {
|
|||||||
"loaded network type is not bitcoin, got Some(Testnet)"
|
"loaded network type is not bitcoin, got Some(Testnet)"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_signer_errors() {
|
||||||
|
let errors = vec![
|
||||||
|
(SignerError::MissingKey, "Missing key for signing"),
|
||||||
|
(SignerError::InvalidKey, "Invalid key provided"),
|
||||||
|
(SignerError::UserCanceled, "User canceled operation"),
|
||||||
|
(
|
||||||
|
SignerError::InputIndexOutOfRange,
|
||||||
|
"Input index out of range",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
SignerError::MissingNonWitnessUtxo,
|
||||||
|
"Missing non-witness UTXO information",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
SignerError::InvalidNonWitnessUtxo,
|
||||||
|
"Invalid non-witness UTXO information provided",
|
||||||
|
),
|
||||||
|
(SignerError::MissingWitnessUtxo, "Missing witness UTXO"),
|
||||||
|
(SignerError::MissingWitnessScript, "Missing witness script"),
|
||||||
|
(SignerError::MissingHdKeypath, "Missing HD keypath"),
|
||||||
|
(
|
||||||
|
SignerError::NonStandardSighash,
|
||||||
|
"Non-standard sighash type used",
|
||||||
|
),
|
||||||
|
(SignerError::InvalidSighash, "Invalid sighash type provided"),
|
||||||
|
(
|
||||||
|
SignerError::SighashError {
|
||||||
|
e: "dummy error".into(),
|
||||||
|
},
|
||||||
|
"Error with sighash computation: dummy error",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
SignerError::MiniscriptPsbt {
|
||||||
|
e: "psbt issue".into(),
|
||||||
|
},
|
||||||
|
"Miniscript Psbt error: psbt issue",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
SignerError::External {
|
||||||
|
e: "external error".into(),
|
||||||
|
},
|
||||||
|
"External error: external error",
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (error, message) in errors {
|
||||||
|
assert_eq!(error.to_string(), message);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ use crate::error::ExtractTxError;
|
|||||||
use crate::error::FeeRateError;
|
use crate::error::FeeRateError;
|
||||||
use crate::error::PersistenceError;
|
use crate::error::PersistenceError;
|
||||||
use crate::error::PsbtParseError;
|
use crate::error::PsbtParseError;
|
||||||
|
use crate::error::SignerError;
|
||||||
use crate::error::TransactionError;
|
use crate::error::TransactionError;
|
||||||
use crate::error::TxidParseError;
|
use crate::error::TxidParseError;
|
||||||
use crate::error::WalletCreationError;
|
use crate::error::WalletCreationError;
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
use crate::bitcoin::{OutPoint, Psbt, Script, Transaction};
|
use crate::bitcoin::{OutPoint, Psbt, Script, Transaction};
|
||||||
use crate::descriptor::Descriptor;
|
use crate::descriptor::Descriptor;
|
||||||
use crate::error::{
|
use crate::error::{
|
||||||
Alpha3Error, CalculateFeeError, PersistenceError, TxidParseError, WalletCreationError,
|
Alpha3Error, CalculateFeeError, PersistenceError, SignerError, TxidParseError,
|
||||||
|
WalletCreationError,
|
||||||
};
|
};
|
||||||
use crate::types::{
|
use crate::types::{
|
||||||
AddressIndex, AddressInfo, Balance, CanonicalTx, FeeRate, LocalOutput, ScriptAmount,
|
AddressIndex, AddressInfo, Balance, CanonicalTx, FeeRate, LocalOutput, ScriptAmount,
|
||||||
@ -93,11 +94,11 @@ impl Wallet {
|
|||||||
&self,
|
&self,
|
||||||
psbt: Arc<Psbt>,
|
psbt: Arc<Psbt>,
|
||||||
// sign_options: Option<SignOptions>,
|
// sign_options: Option<SignOptions>,
|
||||||
) -> Result<bool, Alpha3Error> {
|
) -> Result<bool, SignerError> {
|
||||||
let mut psbt = psbt.inner.lock().unwrap();
|
let mut psbt = psbt.inner.lock().unwrap();
|
||||||
self.get_wallet()
|
self.get_wallet()
|
||||||
.sign(&mut psbt, SignOptions::default())
|
.sign(&mut psbt, SignOptions::default())
|
||||||
.map_err(|_| Alpha3Error::Generic)
|
.map_err(SignerError::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sent_and_received(&self, tx: &Transaction) -> SentAndReceivedValues {
|
pub fn sent_and_received(&self, tx: &Transaction) -> SentAndReceivedValues {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user