[signer] Add Signer::id()

Closes #261
This commit is contained in:
Alekos Filini 2021-01-25 15:04:56 -05:00
parent 2e0ca4fe05
commit c431a60171
No known key found for this signature in database
GPG Key ID: 431401E4A4530061
3 changed files with 34 additions and 11 deletions

View File

@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
#### Added #### Added
- Added a function to get the version of BDK at runtime - Added a function to get the version of BDK at runtime
### Wallet
#### Changed
- Removed the explicit `id` argument from `Wallet::add_signer()` since that's now part of `Signer` itself
## [v0.3.0] - [v0.2.0] ## [v0.3.0] - [v0.2.0]
### Descriptor ### Descriptor

View File

@ -57,7 +57,7 @@ pub(crate) mod utils;
pub use utils::IsDust; pub use utils::IsDust;
use address_validator::AddressValidator; use address_validator::AddressValidator;
use signer::{Signer, SignerId, SignerOrdering, SignersContainer}; use signer::{Signer, SignerOrdering, SignersContainer};
use tx_builder::{BumpFee, CreateTx, FeePolicy, TxBuilder, TxBuilderContext}; use tx_builder::{BumpFee, CreateTx, FeePolicy, TxBuilder, TxBuilderContext};
use utils::{ use utils::{
check_nlocktime, check_nsequence_rbf, descriptor_to_pk_ctx, After, Older, SecpCtx, check_nlocktime, check_nsequence_rbf, descriptor_to_pk_ctx, After, Older, SecpCtx,
@ -228,7 +228,6 @@ where
pub fn add_signer( pub fn add_signer(
&mut self, &mut self,
keychain: KeychainKind, keychain: KeychainKind,
id: SignerId,
ordering: SignerOrdering, ordering: SignerOrdering,
signer: Arc<dyn Signer>, signer: Arc<dyn Signer>,
) { ) {
@ -237,7 +236,7 @@ where
KeychainKind::Internal => Arc::make_mut(&mut self.change_signers), KeychainKind::Internal => Arc::make_mut(&mut self.change_signers),
}; };
signers.add_external(id, ordering, signer); signers.add_external(signer.id(&self.secp), ordering, signer);
} }
/// Add an address validator /// Add an address validator

View File

@ -33,7 +33,6 @@
//! # use bitcoin::secp256k1::{Secp256k1, All}; //! # use bitcoin::secp256k1::{Secp256k1, All};
//! # use bitcoin::*; //! # use bitcoin::*;
//! # use bitcoin::util::psbt; //! # use bitcoin::util::psbt;
//! # use bitcoin::util::bip32::Fingerprint;
//! # use bdk::signer::*; //! # use bdk::signer::*;
//! # use bdk::database::*; //! # use bdk::database::*;
//! # use bdk::*; //! # use bdk::*;
@ -46,6 +45,9 @@
//! # fn connect() -> Self { //! # fn connect() -> Self {
//! # CustomHSM //! # CustomHSM
//! # } //! # }
//! # fn get_id(&self) -> SignerId {
//! # SignerId::Dummy(0)
//! # }
//! # } //! # }
//! #[derive(Debug)] //! #[derive(Debug)]
//! struct CustomSigner { //! struct CustomSigner {
@ -71,6 +73,10 @@
//! Ok(()) //! Ok(())
//! } //! }
//! //!
//! fn id(&self, _secp: &Secp256k1<All>) -> SignerId {
//! self.device.get_id()
//! }
//!
//! fn sign_whole_tx(&self) -> bool { //! fn sign_whole_tx(&self) -> bool {
//! false //! false
//! } //! }
@ -82,7 +88,6 @@
//! let mut wallet = Wallet::new_offline(descriptor, None, Network::Testnet, MemoryDatabase::default())?; //! let mut wallet = Wallet::new_offline(descriptor, None, Network::Testnet, MemoryDatabase::default())?;
//! wallet.add_signer( //! wallet.add_signer(
//! KeychainKind::External, //! KeychainKind::External,
//! Fingerprint::from_str("e30f11b8").unwrap().into(),
//! SignerOrdering(200), //! SignerOrdering(200),
//! Arc::new(custom_signer) //! Arc::new(custom_signer)
//! ); //! );
@ -118,6 +123,8 @@ pub enum SignerId {
PkHash(hash160::Hash), PkHash(hash160::Hash),
/// The fingerprint of a BIP32 extended key /// The fingerprint of a BIP32 extended key
Fingerprint(Fingerprint), Fingerprint(Fingerprint),
/// Dummy identifier
Dummy(u64),
} }
impl From<hash160::Hash> for SignerId { impl From<hash160::Hash> for SignerId {
@ -184,6 +191,12 @@ pub trait Signer: fmt::Debug + Send + Sync {
/// input individually /// input individually
fn sign_whole_tx(&self) -> bool; fn sign_whole_tx(&self) -> bool;
/// Return the [`SignerId`] for this signer
///
/// The [`SignerId`] can be used to lookup a signer in the [`Wallet`](crate::Wallet)'s signers map or to
/// compare two signers.
fn id(&self, secp: &SecpCtx) -> SignerId;
/// Return the secret key for the signer /// Return the secret key for the signer
/// ///
/// This is used internally to reconstruct the original descriptor that may contain secrets. /// This is used internally to reconstruct the original descriptor that may contain secrets.
@ -234,6 +247,10 @@ impl Signer for DescriptorXKey<ExtendedPrivKey> {
false false
} }
fn id(&self, secp: &SecpCtx) -> SignerId {
SignerId::from(self.root_fingerprint(&secp))
}
fn descriptor_secret_key(&self) -> Option<DescriptorSecretKey> { fn descriptor_secret_key(&self) -> Option<DescriptorSecretKey> {
Some(DescriptorSecretKey::XPrv(self.clone())) Some(DescriptorSecretKey::XPrv(self.clone()))
} }
@ -285,6 +302,10 @@ impl Signer for PrivateKey {
false false
} }
fn id(&self, secp: &SecpCtx) -> SignerId {
SignerId::from(self.public_key(secp).to_pubkeyhash())
}
fn descriptor_secret_key(&self) -> Option<DescriptorSecretKey> { fn descriptor_secret_key(&self) -> Option<DescriptorSecretKey> {
Some(DescriptorSecretKey::SinglePriv(DescriptorSinglePriv { Some(DescriptorSecretKey::SinglePriv(DescriptorSinglePriv {
key: *self, key: *self,
@ -345,12 +366,7 @@ impl From<KeyMap> for SignersContainer {
for (_, secret) in keymap { for (_, secret) in keymap {
match secret { match secret {
DescriptorSecretKey::SinglePriv(private_key) => container.add_external( DescriptorSecretKey::SinglePriv(private_key) => container.add_external(
SignerId::from( SignerId::from(private_key.key.public_key(&secp).to_pubkeyhash()),
private_key
.key
.public_key(&Secp256k1::signing_only())
.to_pubkeyhash(),
),
SignerOrdering::default(), SignerOrdering::default(),
Arc::new(private_key.key), Arc::new(private_key.key),
), ),
@ -650,6 +666,10 @@ mod signers_container_tests {
Ok(()) Ok(())
} }
fn id(&self, _secp: &SecpCtx) -> SignerId {
SignerId::Dummy(42)
}
fn sign_whole_tx(&self) -> bool { fn sign_whole_tx(&self) -> bool {
true true
} }