Skip to content

Commit

Permalink
refactor(keys): remove unused mutex
Browse files Browse the repository at this point in the history
The DescriptorSecretKey and DescriptorPublicKey types used
unnecessary mutexes on their inner types.

Fixes #390
  • Loading branch information
thunderbiscuit committed Aug 4, 2023
1 parent bc182c7 commit 118d9f1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
12 changes: 6 additions & 6 deletions bdk-ffi/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Descriptor {
keychain_kind: KeychainKind,
network: Network,
) -> Self {
let derivable_key = secret_key.descriptor_secret_key_mutex.lock().unwrap();
let derivable_key = secret_key.descriptor_secret_key.lock().unwrap();

match derivable_key.deref() {
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
Expand All @@ -60,7 +60,7 @@ impl Descriptor {
network: Network,
) -> Self {
let fingerprint = Fingerprint::from_str(fingerprint.as_str()).unwrap();
let derivable_key = public_key.descriptor_public_key_mutex.lock().unwrap();
let derivable_key = public_key.descriptor_public_key.lock().unwrap();

match derivable_key.deref() {
BdkDescriptorPublicKey::XPub(descriptor_x_key) => {
Expand All @@ -86,7 +86,7 @@ impl Descriptor {
keychain_kind: KeychainKind,
network: Network,
) -> Self {
let derivable_key = secret_key.descriptor_secret_key_mutex.lock().unwrap();
let derivable_key = secret_key.descriptor_secret_key.lock().unwrap();

match derivable_key.deref() {
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
Expand All @@ -111,7 +111,7 @@ impl Descriptor {
network: Network,
) -> Self {
let fingerprint = Fingerprint::from_str(fingerprint.as_str()).unwrap();
let derivable_key = public_key.descriptor_public_key_mutex.lock().unwrap();
let derivable_key = public_key.descriptor_public_key.lock().unwrap();

match derivable_key.deref() {
BdkDescriptorPublicKey::XPub(descriptor_x_key) => {
Expand All @@ -137,7 +137,7 @@ impl Descriptor {
keychain_kind: KeychainKind,
network: Network,
) -> Self {
let derivable_key = secret_key.descriptor_secret_key_mutex.lock().unwrap();
let derivable_key = secret_key.descriptor_secret_key.lock().unwrap();

match derivable_key.deref() {
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
Expand All @@ -162,7 +162,7 @@ impl Descriptor {
network: Network,
) -> Self {
let fingerprint = Fingerprint::from_str(fingerprint.as_str()).unwrap();
let derivable_key = public_key.descriptor_public_key_mutex.lock().unwrap();
let derivable_key = public_key.descriptor_public_key.lock().unwrap();

match derivable_key.deref() {
BdkDescriptorPublicKey::XPub(descriptor_x_key) => {
Expand Down
36 changes: 18 additions & 18 deletions bdk-ffi/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl DerivationPath {

#[derive(Debug)]
pub(crate) struct DescriptorSecretKey {
pub(crate) descriptor_secret_key_mutex: Mutex<BdkDescriptorSecretKey>,
pub(crate) descriptor_secret_key: BdkDescriptorSecretKey,
}

impl DescriptorSecretKey {
Expand All @@ -80,21 +80,21 @@ impl DescriptorSecretKey {
wildcard: bdk::descriptor::Wildcard::Unhardened,
});
Self {
descriptor_secret_key_mutex: Mutex::new(descriptor_secret_key),
descriptor_secret_key,
}
}

pub(crate) fn from_string(private_key: String) -> Result<Self, BdkError> {
let descriptor_secret_key = BdkDescriptorSecretKey::from_str(private_key.as_str())
.map_err(|e| BdkError::Generic(e.to_string()))?;
Ok(Self {
descriptor_secret_key_mutex: Mutex::new(descriptor_secret_key),
descriptor_secret_key,
})
}

pub(crate) fn derive(&self, path: Arc<DerivationPath>) -> Result<Arc<Self>, BdkError> {
let secp = Secp256k1::new();
let descriptor_secret_key = self.descriptor_secret_key_mutex.lock().unwrap();
let descriptor_secret_key = self.descriptor_secret_key.lock().unwrap();
let path = path.derivation_path_mutex.lock().unwrap().deref().clone();
match descriptor_secret_key.deref() {
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
Expand All @@ -110,7 +110,7 @@ impl DescriptorSecretKey {
wildcard: descriptor_x_key.wildcard,
});
Ok(Arc::new(Self {
descriptor_secret_key_mutex: Mutex::new(derived_descriptor_secret_key),
descriptor_secret_key: derived_descriptor_secret_key,
}))
}
BdkDescriptorSecretKey::Single(_) => Err(BdkError::Generic(
Expand All @@ -120,7 +120,7 @@ impl DescriptorSecretKey {
}

pub(crate) fn extend(&self, path: Arc<DerivationPath>) -> Result<Arc<Self>, BdkError> {
let descriptor_secret_key = self.descriptor_secret_key_mutex.lock().unwrap();
let descriptor_secret_key = self.descriptor_secret_key.lock().unwrap();
let path = path.derivation_path_mutex.lock().unwrap().deref().clone();
match descriptor_secret_key.deref() {
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
Expand All @@ -132,7 +132,7 @@ impl DescriptorSecretKey {
wildcard: descriptor_x_key.wildcard,
});
Ok(Arc::new(Self {
descriptor_secret_key_mutex: Mutex::new(extended_descriptor_secret_key),
descriptor_secret_key: extended_descriptor_secret_key,
}))
}
BdkDescriptorSecretKey::Single(_) => Err(BdkError::Generic(
Expand All @@ -144,19 +144,19 @@ impl DescriptorSecretKey {
pub(crate) fn as_public(&self) -> Arc<DescriptorPublicKey> {
let secp = Secp256k1::new();
let descriptor_public_key = self
.descriptor_secret_key_mutex
.descriptor_secret_key
.lock()
.unwrap()
.to_public(&secp)
.unwrap();
Arc::new(DescriptorPublicKey {
descriptor_public_key_mutex: Mutex::new(descriptor_public_key),
descriptor_public_key,
})
}

/// Get the private key as bytes.
pub(crate) fn secret_bytes(&self) -> Vec<u8> {
let descriptor_secret_key = self.descriptor_secret_key_mutex.lock().unwrap();
let descriptor_secret_key = self.descriptor_secret_key.lock().unwrap();
let secret_bytes: Vec<u8> = match descriptor_secret_key.deref() {
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
descriptor_x_key.xkey.private_key.secret_bytes().to_vec()
Expand All @@ -170,27 +170,27 @@ impl DescriptorSecretKey {
}

pub(crate) fn as_string(&self) -> String {
self.descriptor_secret_key_mutex.lock().unwrap().to_string()
self.descriptor_secret_key.lock().unwrap().to_string()
}
}

#[derive(Debug)]
pub(crate) struct DescriptorPublicKey {
pub(crate) descriptor_public_key_mutex: Mutex<BdkDescriptorPublicKey>,
pub(crate) descriptor_public_key: BdkDescriptorPublicKey,
}

impl DescriptorPublicKey {
pub(crate) fn from_string(public_key: String) -> Result<Self, BdkError> {
let descriptor_public_key = BdkDescriptorPublicKey::from_str(public_key.as_str())
.map_err(|e| BdkError::Generic(e.to_string()))?;
Ok(Self {
descriptor_public_key_mutex: Mutex::new(descriptor_public_key),
descriptor_public_key,
})
}

pub(crate) fn derive(&self, path: Arc<DerivationPath>) -> Result<Arc<Self>, BdkError> {
let secp = Secp256k1::new();
let descriptor_public_key = self.descriptor_public_key_mutex.lock().unwrap();
let descriptor_public_key = self.descriptor_public_key.lock().unwrap();
let path = path.derivation_path_mutex.lock().unwrap().deref().clone();

match descriptor_public_key.deref() {
Expand All @@ -207,7 +207,7 @@ impl DescriptorPublicKey {
wildcard: descriptor_x_key.wildcard,
});
Ok(Arc::new(Self {
descriptor_public_key_mutex: Mutex::new(derived_descriptor_public_key),
descriptor_public_key: derived_descriptor_public_key,
}))
}
BdkDescriptorPublicKey::Single(_) => Err(BdkError::Generic(
Expand All @@ -217,7 +217,7 @@ impl DescriptorPublicKey {
}

pub(crate) fn extend(&self, path: Arc<DerivationPath>) -> Result<Arc<Self>, BdkError> {
let descriptor_public_key = self.descriptor_public_key_mutex.lock().unwrap();
let descriptor_public_key = self.descriptor_public_key.lock().unwrap();
let path = path.derivation_path_mutex.lock().unwrap().deref().clone();
match descriptor_public_key.deref() {
BdkDescriptorPublicKey::XPub(descriptor_x_key) => {
Expand All @@ -229,7 +229,7 @@ impl DescriptorPublicKey {
wildcard: descriptor_x_key.wildcard,
});
Ok(Arc::new(Self {
descriptor_public_key_mutex: Mutex::new(extended_descriptor_public_key),
descriptor_public_key: extended_descriptor_public_key,
}))
}
BdkDescriptorPublicKey::Single(_) => Err(BdkError::Generic(
Expand All @@ -239,7 +239,7 @@ impl DescriptorPublicKey {
}

pub(crate) fn as_string(&self) -> String {
self.descriptor_public_key_mutex.lock().unwrap().to_string()
self.descriptor_public_key.lock().unwrap().to_string()
}
}

Expand Down

0 comments on commit 118d9f1

Please sign in to comment.