use crate::Bip32Error;
use coins_core::ser::ByteFormat;
use std::io::{Read, Write};
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
pub enum Hint {
Legacy,
Compatibility,
SegWit,
}
#[derive(Eq, PartialEq, Clone, Copy, serde::Serialize, serde::Deserialize)]
pub struct KeyFingerprint(pub [u8; 4]);
impl From<[u8; 4]> for KeyFingerprint {
fn from(v: [u8; 4]) -> Self {
Self(v)
}
}
impl ByteFormat for KeyFingerprint {
type Error = Bip32Error;
fn serialized_length(&self) -> usize {
4
}
fn read_from<R>(reader: &mut R) -> Result<Self, Self::Error>
where
R: Read,
Self: std::marker::Sized,
{
let mut buf = [0u8; 4];
reader.read_exact(&mut buf)?;
Ok(Self(buf))
}
fn write_to<W>(&self, writer: &mut W) -> Result<usize, Self::Error>
where
W: Write,
{
Ok(writer.write(&self.0)?)
}
}
impl KeyFingerprint {
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))
}
}
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
pub struct ChainCode(pub [u8; 32]);
impl From<[u8; 32]> for ChainCode {
fn from(v: [u8; 32]) -> Self {
Self(v)
}
}
#[derive(Copy, Clone, Debug)]
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
}
}