use std::str::FromStr;
use amplify::{Bytes32, RawArray};
use baid58::{Baid58ParseError, FromBaid58, ToBaid58};
use bp::secp256k1::rand::{thread_rng, RngCore};
use commit_verify::{CommitVerify, Conceal, StrictEncodedProtocol};
use strict_encoding::StrictEncode;
use super::{ConfidentialState, ExposedState};
use crate::{MediaType, StateCommitment, StateData, StateType, LIB_NAME_RGB};
#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, From)]
#[wrapper(Deref, BorrowSlice, Hex, Index, RangeOps)]
#[display(Self::to_baid58)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct AttachId(
#[from]
#[from([u8; 32])]
Bytes32,
);
impl ToBaid58<32> for AttachId {
const HRI: &'static str = "att";
fn to_baid58_payload(&self) -> [u8; 32] { self.to_raw_array() }
}
impl FromBaid58<32> for AttachId {}
impl FromStr for AttachId {
type Err = Baid58ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> { Self::from_baid58_str(s) }
}
#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB)]
#[derive(CommitEncode)]
#[commit_encode(conceal, strategy = strict)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct RevealedAttach {
pub id: AttachId,
pub media_type: MediaType,
pub salt: u64,
}
impl RevealedAttach {
pub fn new(id: AttachId, media_type: MediaType) -> Self {
Self {
id,
media_type,
salt: thread_rng().next_u64(),
}
}
}
impl ExposedState for RevealedAttach {
type Confidential = ConcealedAttach;
fn state_type(&self) -> StateType { StateType::Attachment }
fn state_data(&self) -> StateData { StateData::Attachment(self.clone()) }
}
impl Conceal for RevealedAttach {
type Concealed = ConcealedAttach;
fn conceal(&self) -> Self::Concealed { ConcealedAttach::commit(self) }
}
#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)]
#[wrapper(Deref, BorrowSlice, Hex, Index, RangeOps)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB)]
#[derive(CommitEncode)]
#[commit_encode(strategy = strict)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct ConcealedAttach(
#[from]
#[from([u8; 32])]
Bytes32,
);
impl ConfidentialState for ConcealedAttach {
fn state_type(&self) -> StateType { StateType::Attachment }
fn state_commitment(&self) -> StateCommitment { StateCommitment::Attachment(*self) }
}
impl CommitVerify<RevealedAttach, StrictEncodedProtocol> for ConcealedAttach {
fn commit(revealed: &RevealedAttach) -> Self { Bytes32::commit(revealed).into() }
}