use secure_types::{Error, SecureArray};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
pub enum Hint {
Legacy,
Compatibility,
SegWit,
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Eq, PartialEq, Clone, Copy)]
pub struct KeyFingerprint(pub [u8; 4]);
impl From<[u8; 4]> for KeyFingerprint {
fn from(v: [u8; 4]) -> Self {
Self(v)
}
}
impl KeyFingerprint {
pub fn is_zero(&self) -> bool {
self.0.iter().all(|b| *b == 0)
}
pub fn eq_slice(self, other: &[u8]) -> bool {
self.0 == other
}
}
impl std::fmt::Debug for KeyFingerprint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("KeyFingerprint {:x?}", self.0))
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone)]
pub struct ChainCode {
pub data: SecureArray<u8, 32>,
}
impl ChainCode {
pub fn from_slice_mut(slice: &mut [u8; 32]) -> Result<Self, Error> {
let data = SecureArray::from_slice_mut(slice)?;
Ok(Self { data })
}
}
impl PartialEq for ChainCode {
fn eq(&self, other: &ChainCode) -> bool {
other
.data
.unlock(|other_slice| self.data.unlock(|self_slice| self_slice == other_slice))
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone)]
pub struct XKeyInfo {
pub depth: u8,
pub parent: KeyFingerprint,
pub index: u32,
pub chain_code: ChainCode,
pub hint: Hint,
}
impl PartialEq for XKeyInfo {
fn eq(&self, other: &XKeyInfo) -> bool {
self.depth == other.depth
&& self.parent == other.parent
&& self.index == other.index
&& self.chain_code == other.chain_code
}
}