use anyhow::Context;
use bc_envelope::prelude::*;
use crate::NonHardenedChildIndex;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DerivationInfo {
change: NonHardenedChildIndex,
address_index: NonHardenedChildIndex,
}
impl DerivationInfo {
pub fn new(change: NonHardenedChildIndex, address_index: NonHardenedChildIndex) -> Self {
Self {
change,
address_index,
}
}
pub fn change(&self) -> NonHardenedChildIndex {
self.change
}
pub fn address_index(&self) -> NonHardenedChildIndex {
self.address_index
}
}
impl From<DerivationInfo> for Envelope {
fn from(value: DerivationInfo) -> Self {
Envelope::new(value.change)
.add_type("DerivationInfo")
.add_assertion("address_index", value.address_index)
}
}
impl TryFrom<Envelope> for DerivationInfo {
type Error = anyhow::Error;
fn try_from(envelope: Envelope) -> Result<Self, Self::Error> {
envelope
.check_type_envelope("DerivationInfo")
.context("DerivationInfo")?;
let change = envelope.extract_subject().context("change")?;
let address_index = envelope
.extract_object_for_predicate("address_index")
.context("address_index")?;
Ok(Self {
change,
address_index,
})
}
}
#[cfg(test)]
mod tests {
use crate::{NonHardenedChildIndex, test_envelope_roundtrip};
use super::DerivationInfo;
impl crate::RandomInstance for DerivationInfo {
fn random() -> Self {
Self {
change: NonHardenedChildIndex::random(),
address_index: NonHardenedChildIndex::random(),
}
}
}
test_envelope_roundtrip!(DerivationInfo);
}