use crate::{dbc_id::PublicAddress, DerivationIndex, Hash};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use tiny_keccak::{Hasher, Sha3};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct DbcSecrets {
pub public_address: PublicAddress,
pub derivation_index: DerivationIndex,
}
impl From<(PublicAddress, DerivationIndex)> for DbcSecrets {
fn from(params: (PublicAddress, DerivationIndex)) -> Self {
let (public_address, derivation_index) = params;
Self {
public_address,
derivation_index,
}
}
}
impl From<(&PublicAddress, &DerivationIndex)> for DbcSecrets {
fn from(params: (&PublicAddress, &DerivationIndex)) -> Self {
let (public_address, derivation_index) = params;
Self {
public_address: *public_address,
derivation_index: *derivation_index,
}
}
}
impl DbcSecrets {
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes: Vec<u8> = Default::default();
bytes.extend(&self.public_address.to_bytes());
bytes.extend(&self.derivation_index);
bytes
}
pub fn hash(&self) -> Hash {
let mut sha3 = Sha3::v256();
sha3.update(&self.to_bytes());
let mut hash = [0u8; 32];
sha3.finalize(&mut hash);
Hash::hash(&hash)
}
}