use borsh::BorshSerialize;
use crate::env;
mod private {
pub trait Sealed {}
impl Sealed for super::Sha256 {}
impl Sealed for super::Keccak256 {}
impl Sealed for super::Identity {}
}
pub trait ToKey: self::private::Sealed {
type KeyType: AsRef<[u8]>;
fn to_key<Q: ?Sized>(prefix: &[u8], key: &Q, buffer: &mut Vec<u8>) -> Self::KeyType
where
Q: BorshSerialize;
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Sha256 {}
impl ToKey for Sha256 {
type KeyType = [u8; 32];
fn to_key<Q: ?Sized>(prefix: &[u8], key: &Q, buffer: &mut Vec<u8>) -> Self::KeyType
where
Q: BorshSerialize,
{
buffer.extend(prefix);
key.serialize(buffer).unwrap_or_else(|_| env::abort());
env::sha256_array(buffer)
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Keccak256 {}
impl ToKey for Keccak256 {
type KeyType = [u8; 32];
fn to_key<Q: ?Sized>(prefix: &[u8], key: &Q, buffer: &mut Vec<u8>) -> Self::KeyType
where
Q: BorshSerialize,
{
buffer.extend(prefix);
key.serialize(buffer).unwrap_or_else(|_| env::abort());
env::keccak256_array(buffer)
}
}
pub enum Identity {}
impl ToKey for Identity {
type KeyType = Vec<u8>;
fn to_key<Q: ?Sized>(prefix: &[u8], key: &Q, buffer: &mut Vec<u8>) -> Self::KeyType
where
Q: BorshSerialize,
{
buffer.extend(prefix);
key.serialize(buffer).unwrap_or_else(|_| env::abort());
buffer.clone()
}
}