Merge commit 'refs/pull/156/head' of github.com:bitcoindevkit/bdk
This commit is contained in:
commit
f67bfe7bfc
@ -62,7 +62,7 @@ fn main() -> Result<(), bdk::Error> {
|
|||||||
let mut wallet: OfflineWallet<_> =
|
let mut wallet: OfflineWallet<_> =
|
||||||
Wallet::new_offline(descriptor, None, Network::Regtest, MemoryDatabase::new())?;
|
Wallet::new_offline(descriptor, None, Network::Regtest, MemoryDatabase::new())?;
|
||||||
|
|
||||||
wallet.add_address_validator(Arc::new(Box::new(DummyValidator)));
|
wallet.add_address_validator(Arc::new(DummyValidator));
|
||||||
|
|
||||||
wallet.get_new_address()?;
|
wallet.get_new_address()?;
|
||||||
wallet.get_new_address()?;
|
wallet.get_new_address()?;
|
||||||
|
@ -67,7 +67,7 @@
|
|||||||
//!
|
//!
|
||||||
//! let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
|
//! let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
|
||||||
//! let mut wallet: OfflineWallet<_> = Wallet::new_offline(descriptor, None, Network::Testnet, MemoryDatabase::default())?;
|
//! let mut wallet: OfflineWallet<_> = Wallet::new_offline(descriptor, None, Network::Testnet, MemoryDatabase::default())?;
|
||||||
//! wallet.add_address_validator(Arc::new(Box::new(PrintAddressAndContinue)));
|
//! wallet.add_address_validator(Arc::new(PrintAddressAndContinue));
|
||||||
//!
|
//!
|
||||||
//! let address = wallet.get_new_address()?;
|
//! let address = wallet.get_new_address()?;
|
||||||
//! println!("Address: {}", address);
|
//! println!("Address: {}", address);
|
||||||
@ -106,7 +106,7 @@ impl std::error::Error for AddressValidatorError {}
|
|||||||
/// validator will be propagated up to the original caller that triggered the address generation.
|
/// validator will be propagated up to the original caller that triggered the address generation.
|
||||||
///
|
///
|
||||||
/// For a usage example see [this module](crate::address_validator)'s documentation.
|
/// For a usage example see [this module](crate::address_validator)'s documentation.
|
||||||
pub trait AddressValidator {
|
pub trait AddressValidator: Send + Sync {
|
||||||
/// Validate or inspect an address
|
/// Validate or inspect an address
|
||||||
fn validate(
|
fn validate(
|
||||||
&self,
|
&self,
|
||||||
@ -140,7 +140,7 @@ mod test {
|
|||||||
#[should_panic(expected = "InvalidScript")]
|
#[should_panic(expected = "InvalidScript")]
|
||||||
fn test_address_validator_external() {
|
fn test_address_validator_external() {
|
||||||
let (mut wallet, _, _) = get_funded_wallet(get_test_wpkh());
|
let (mut wallet, _, _) = get_funded_wallet(get_test_wpkh());
|
||||||
wallet.add_address_validator(Arc::new(Box::new(TestValidator)));
|
wallet.add_address_validator(Arc::new(TestValidator));
|
||||||
|
|
||||||
wallet.get_new_address().unwrap();
|
wallet.get_new_address().unwrap();
|
||||||
}
|
}
|
||||||
@ -149,7 +149,7 @@ mod test {
|
|||||||
#[should_panic(expected = "InvalidScript")]
|
#[should_panic(expected = "InvalidScript")]
|
||||||
fn test_address_validator_internal() {
|
fn test_address_validator_internal() {
|
||||||
let (mut wallet, descriptors, _) = get_funded_wallet(get_test_wpkh());
|
let (mut wallet, descriptors, _) = get_funded_wallet(get_test_wpkh());
|
||||||
wallet.add_address_validator(Arc::new(Box::new(TestValidator)));
|
wallet.add_address_validator(Arc::new(TestValidator));
|
||||||
|
|
||||||
let addr = testutils!(@external descriptors, 10);
|
let addr = testutils!(@external descriptors, 10);
|
||||||
wallet
|
wallet
|
||||||
|
@ -89,7 +89,7 @@ pub struct Wallet<B: BlockchainMarker, D: BatchDatabase> {
|
|||||||
signers: Arc<SignersContainer>,
|
signers: Arc<SignersContainer>,
|
||||||
change_signers: Arc<SignersContainer>,
|
change_signers: Arc<SignersContainer>,
|
||||||
|
|
||||||
address_validators: Vec<Arc<Box<dyn AddressValidator>>>,
|
address_validators: Vec<Arc<dyn AddressValidator>>,
|
||||||
|
|
||||||
network: Network,
|
network: Network,
|
||||||
|
|
||||||
@ -205,7 +205,7 @@ where
|
|||||||
script_type: ScriptType,
|
script_type: ScriptType,
|
||||||
id: SignerId,
|
id: SignerId,
|
||||||
ordering: SignerOrdering,
|
ordering: SignerOrdering,
|
||||||
signer: Arc<Box<dyn Signer>>,
|
signer: Arc<dyn Signer>,
|
||||||
) {
|
) {
|
||||||
let signers = match script_type {
|
let signers = match script_type {
|
||||||
ScriptType::External => Arc::make_mut(&mut self.signers),
|
ScriptType::External => Arc::make_mut(&mut self.signers),
|
||||||
@ -218,7 +218,7 @@ where
|
|||||||
/// Add an address validator
|
/// Add an address validator
|
||||||
///
|
///
|
||||||
/// See [the `address_validator` module](address_validator) for an example.
|
/// See [the `address_validator` module](address_validator) for an example.
|
||||||
pub fn add_address_validator(&mut self, validator: Arc<Box<dyn AddressValidator>>) {
|
pub fn add_address_validator(&mut self, validator: Arc<dyn AddressValidator>) {
|
||||||
self.address_validators.push(validator);
|
self.address_validators.push(validator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@
|
|||||||
//! ScriptType::External,
|
//! ScriptType::External,
|
||||||
//! Fingerprint::from_str("e30f11b8").unwrap().into(),
|
//! Fingerprint::from_str("e30f11b8").unwrap().into(),
|
||||||
//! SignerOrdering(200),
|
//! SignerOrdering(200),
|
||||||
//! Arc::new(Box::new(custom_signer))
|
//! Arc::new(custom_signer)
|
||||||
//! );
|
//! );
|
||||||
//!
|
//!
|
||||||
//! # Ok::<_, bdk::Error>(())
|
//! # Ok::<_, bdk::Error>(())
|
||||||
@ -162,7 +162,7 @@ impl std::error::Error for SignerError {}
|
|||||||
///
|
///
|
||||||
/// This trait can be implemented to provide customized signers to the wallet. For an example see
|
/// This trait can be implemented to provide customized signers to the wallet. For an example see
|
||||||
/// [`this module`](crate::wallet::signer)'s documentation.
|
/// [`this module`](crate::wallet::signer)'s documentation.
|
||||||
pub trait Signer: fmt::Debug {
|
pub trait Signer: fmt::Debug + Send + Sync {
|
||||||
/// Sign a PSBT
|
/// Sign a PSBT
|
||||||
///
|
///
|
||||||
/// The `input_index` argument is only provided if the wallet doesn't declare to sign the whole
|
/// The `input_index` argument is only provided if the wallet doesn't declare to sign the whole
|
||||||
@ -320,7 +320,7 @@ impl From<(SignerId, SignerOrdering)> for SignersContainerKey {
|
|||||||
|
|
||||||
/// Container for multiple signers
|
/// Container for multiple signers
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
pub struct SignersContainer(BTreeMap<SignersContainerKey, Arc<Box<dyn Signer>>>);
|
pub struct SignersContainer(BTreeMap<SignersContainerKey, Arc<dyn Signer>>);
|
||||||
|
|
||||||
impl SignersContainer {
|
impl SignersContainer {
|
||||||
pub fn as_key_map(&self) -> KeyMap {
|
pub fn as_key_map(&self) -> KeyMap {
|
||||||
@ -346,12 +346,12 @@ impl From<KeyMap> for SignersContainer {
|
|||||||
.to_pubkeyhash(),
|
.to_pubkeyhash(),
|
||||||
),
|
),
|
||||||
SignerOrdering::default(),
|
SignerOrdering::default(),
|
||||||
Arc::new(Box::new(private_key.key)),
|
Arc::new(private_key.key),
|
||||||
),
|
),
|
||||||
DescriptorSecretKey::XPrv(xprv) => container.add_external(
|
DescriptorSecretKey::XPrv(xprv) => container.add_external(
|
||||||
SignerId::from(xprv.root_fingerprint()),
|
SignerId::from(xprv.root_fingerprint()),
|
||||||
SignerOrdering::default(),
|
SignerOrdering::default(),
|
||||||
Arc::new(Box::new(xprv)),
|
Arc::new(xprv),
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -372,17 +372,13 @@ impl SignersContainer {
|
|||||||
&mut self,
|
&mut self,
|
||||||
id: SignerId,
|
id: SignerId,
|
||||||
ordering: SignerOrdering,
|
ordering: SignerOrdering,
|
||||||
signer: Arc<Box<dyn Signer>>,
|
signer: Arc<dyn Signer>,
|
||||||
) -> Option<Arc<Box<dyn Signer>>> {
|
) -> Option<Arc<dyn Signer>> {
|
||||||
self.0.insert((id, ordering).into(), signer)
|
self.0.insert((id, ordering).into(), signer)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes a signer from the container and returns it
|
/// Removes a signer from the container and returns it
|
||||||
pub fn remove(
|
pub fn remove(&mut self, id: SignerId, ordering: SignerOrdering) -> Option<Arc<dyn Signer>> {
|
||||||
&mut self,
|
|
||||||
id: SignerId,
|
|
||||||
ordering: SignerOrdering,
|
|
||||||
) -> Option<Arc<Box<dyn Signer>>> {
|
|
||||||
self.0.remove(&(id, ordering).into())
|
self.0.remove(&(id, ordering).into())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -395,12 +391,12 @@ impl SignersContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the list of signers in the container, sorted by lowest to highest `ordering`
|
/// Returns the list of signers in the container, sorted by lowest to highest `ordering`
|
||||||
pub fn signers(&self) -> Vec<&Arc<Box<dyn Signer>>> {
|
pub fn signers(&self) -> Vec<&Arc<dyn Signer>> {
|
||||||
self.0.values().collect()
|
self.0.values().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Finds the signer with lowest ordering for a given id in the container.
|
/// Finds the signer with lowest ordering for a given id in the container.
|
||||||
pub fn find(&self, id: SignerId) -> Option<&Arc<Box<dyn Signer>>> {
|
pub fn find(&self, id: SignerId) -> Option<&Arc<dyn Signer>> {
|
||||||
self.0
|
self.0
|
||||||
.range((
|
.range((
|
||||||
Included(&(id.clone(), SignerOrdering(0)).into()),
|
Included(&(id.clone(), SignerOrdering(0)).into()),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user