refactor: use tuple struct for descriptorsecretkey type
This commit is contained in:
parent
e48af63fe6
commit
4cd6a80ce0
@ -65,9 +65,7 @@ impl DerivationPath {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct DescriptorSecretKey {
|
pub struct DescriptorSecretKey(pub(crate) BdkDescriptorSecretKey);
|
||||||
pub(crate) inner: BdkDescriptorSecretKey,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DescriptorSecretKey {
|
impl DescriptorSecretKey {
|
||||||
pub(crate) fn new(network: Network, mnemonic: &Mnemonic, password: Option<String>) -> Self {
|
pub(crate) fn new(network: Network, mnemonic: &Mnemonic, password: Option<String>) -> Self {
|
||||||
@ -79,22 +77,18 @@ impl DescriptorSecretKey {
|
|||||||
derivation_path: BdkDerivationPath::master(),
|
derivation_path: BdkDerivationPath::master(),
|
||||||
wildcard: Wildcard::Unhardened,
|
wildcard: Wildcard::Unhardened,
|
||||||
});
|
});
|
||||||
Self {
|
Self(descriptor_secret_key)
|
||||||
inner: descriptor_secret_key,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn from_string(private_key: String) -> Result<Self, DescriptorKeyError> {
|
pub(crate) fn from_string(private_key: String) -> Result<Self, DescriptorKeyError> {
|
||||||
let descriptor_secret_key = BdkDescriptorSecretKey::from_str(private_key.as_str())
|
let descriptor_secret_key = BdkDescriptorSecretKey::from_str(private_key.as_str())
|
||||||
.map_err(DescriptorKeyError::from)?;
|
.map_err(DescriptorKeyError::from)?;
|
||||||
Ok(Self {
|
Ok(Self(descriptor_secret_key))
|
||||||
inner: descriptor_secret_key,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn derive(&self, path: &DerivationPath) -> Result<Arc<Self>, DescriptorKeyError> {
|
pub(crate) fn derive(&self, path: &DerivationPath) -> Result<Arc<Self>, DescriptorKeyError> {
|
||||||
let secp = Secp256k1::new();
|
let secp = Secp256k1::new();
|
||||||
let descriptor_secret_key = &self.inner;
|
let descriptor_secret_key = &self.0;
|
||||||
let path = path.inner_mutex.lock().unwrap().deref().clone();
|
let path = path.inner_mutex.lock().unwrap().deref().clone();
|
||||||
match descriptor_secret_key {
|
match descriptor_secret_key {
|
||||||
BdkDescriptorSecretKey::Single(_) => Err(DescriptorKeyError::InvalidKeyType),
|
BdkDescriptorSecretKey::Single(_) => Err(DescriptorKeyError::InvalidKeyType),
|
||||||
@ -113,16 +107,14 @@ impl DescriptorSecretKey {
|
|||||||
derivation_path: BdkDerivationPath::default(),
|
derivation_path: BdkDerivationPath::default(),
|
||||||
wildcard: descriptor_x_key.wildcard,
|
wildcard: descriptor_x_key.wildcard,
|
||||||
});
|
});
|
||||||
Ok(Arc::new(Self {
|
Ok(Arc::new(Self(derived_descriptor_secret_key)))
|
||||||
inner: derived_descriptor_secret_key,
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
BdkDescriptorSecretKey::MultiXPrv(_) => Err(DescriptorKeyError::InvalidKeyType),
|
BdkDescriptorSecretKey::MultiXPrv(_) => Err(DescriptorKeyError::InvalidKeyType),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn extend(&self, path: &DerivationPath) -> Result<Arc<Self>, DescriptorKeyError> {
|
pub(crate) fn extend(&self, path: &DerivationPath) -> Result<Arc<Self>, Alpha3Error> {
|
||||||
let descriptor_secret_key = &self.inner;
|
let descriptor_secret_key = &self.0;
|
||||||
let path = path.inner_mutex.lock().unwrap().deref().clone();
|
let path = path.inner_mutex.lock().unwrap().deref().clone();
|
||||||
match descriptor_secret_key {
|
match descriptor_secret_key {
|
||||||
BdkDescriptorSecretKey::Single(_) => Err(DescriptorKeyError::InvalidKeyType),
|
BdkDescriptorSecretKey::Single(_) => Err(DescriptorKeyError::InvalidKeyType),
|
||||||
@ -134,9 +126,7 @@ impl DescriptorSecretKey {
|
|||||||
derivation_path: extended_path,
|
derivation_path: extended_path,
|
||||||
wildcard: descriptor_x_key.wildcard,
|
wildcard: descriptor_x_key.wildcard,
|
||||||
});
|
});
|
||||||
Ok(Arc::new(Self {
|
Ok(Arc::new(Self(extended_descriptor_secret_key)))
|
||||||
inner: extended_descriptor_secret_key,
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
BdkDescriptorSecretKey::MultiXPrv(_) => Err(DescriptorKeyError::InvalidKeyType),
|
BdkDescriptorSecretKey::MultiXPrv(_) => Err(DescriptorKeyError::InvalidKeyType),
|
||||||
}
|
}
|
||||||
@ -144,14 +134,14 @@ impl DescriptorSecretKey {
|
|||||||
|
|
||||||
pub(crate) fn as_public(&self) -> Arc<DescriptorPublicKey> {
|
pub(crate) fn as_public(&self) -> Arc<DescriptorPublicKey> {
|
||||||
let secp = Secp256k1::new();
|
let secp = Secp256k1::new();
|
||||||
let descriptor_public_key = self.inner.to_public(&secp).unwrap();
|
let descriptor_public_key = self.0.to_public(&secp).unwrap();
|
||||||
Arc::new(DescriptorPublicKey {
|
Arc::new(DescriptorPublicKey {
|
||||||
inner: descriptor_public_key,
|
inner: descriptor_public_key,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn secret_bytes(&self) -> Vec<u8> {
|
pub(crate) fn secret_bytes(&self) -> Vec<u8> {
|
||||||
let inner = &self.inner;
|
let inner = &self.0;
|
||||||
let secret_bytes: Vec<u8> = match inner {
|
let secret_bytes: Vec<u8> = match inner {
|
||||||
BdkDescriptorSecretKey::Single(_) => {
|
BdkDescriptorSecretKey::Single(_) => {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
@ -168,7 +158,7 @@ impl DescriptorSecretKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn as_string(&self) -> String {
|
pub(crate) fn as_string(&self) -> String {
|
||||||
self.inner.to_string()
|
self.0.to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user