use alloc::vec::Vec;
use dusk_bls12_381::BlsScalar;
use dusk_bls12_381_sign::{PublicKey, Signature};
use dusk_bytes::Serializable;
use dusk_pki::StealthAddress;
#[cfg(feature = "rkyv-impl")]
use rkyv::{Archive, Deserialize, Serialize};
use crate::note::Note;
const ALLOW_MESSAGE_SIZE: usize = u64::SIZE + PublicKey::SIZE;
const STAKE_MESSAGE_SIZE: usize = u64::SIZE + u64::SIZE;
const UNSTAKE_MESSAGE_SIZE: usize = u64::SIZE + Note::SIZE;
const WITHDRAW_MESSAGE_SIZE: usize =
u64::SIZE + StealthAddress::SIZE + BlsScalar::SIZE;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
feature = "rkyv-impl",
derive(Archive, Serialize, Deserialize),
archive_attr(derive(bytecheck::CheckBytes))
)]
pub struct StakeData {
pub amount: Option<(u64, u64)>,
pub reward: u64,
pub counter: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
feature = "rkyv-impl",
derive(Archive, Serialize, Deserialize),
archive_attr(derive(bytecheck::CheckBytes))
)]
pub struct Stake {
pub public_key: PublicKey,
pub signature: Signature,
pub value: u64,
pub proof: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
feature = "rkyv-impl",
derive(Archive, Serialize, Deserialize),
archive_attr(derive(bytecheck::CheckBytes))
)]
pub struct Unstake {
pub public_key: PublicKey,
pub signature: Signature,
pub note: Note,
pub proof: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
feature = "rkyv-impl",
derive(Archive, Serialize, Deserialize),
archive_attr(derive(bytecheck::CheckBytes))
)]
pub struct Withdraw {
pub public_key: PublicKey,
pub signature: Signature,
pub address: StealthAddress,
pub nonce: BlsScalar,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
feature = "rkyv-impl",
derive(Archive, Serialize, Deserialize),
archive_attr(derive(bytecheck::CheckBytes))
)]
pub struct Allow {
pub public_key: PublicKey,
pub owner: PublicKey,
pub signature: Signature,
}
#[must_use]
pub fn allow_signature_message(
counter: u64,
staker: PublicKey,
) -> [u8; ALLOW_MESSAGE_SIZE] {
let mut bytes = [0u8; ALLOW_MESSAGE_SIZE];
bytes[..u64::SIZE].copy_from_slice(&counter.to_bytes());
bytes[u64::SIZE..].copy_from_slice(&staker.to_bytes());
bytes
}
#[must_use]
pub fn stake_signature_message(
counter: u64,
value: u64,
) -> [u8; STAKE_MESSAGE_SIZE] {
let mut bytes = [0u8; STAKE_MESSAGE_SIZE];
bytes[..u64::SIZE].copy_from_slice(&counter.to_bytes());
bytes[u64::SIZE..].copy_from_slice(&value.to_bytes());
bytes
}
#[must_use]
pub fn unstake_signature_message(
counter: u64,
note: Note,
) -> [u8; UNSTAKE_MESSAGE_SIZE] {
let mut bytes = [0u8; UNSTAKE_MESSAGE_SIZE];
bytes[..u64::SIZE].copy_from_slice(&counter.to_bytes());
bytes[u64::SIZE..].copy_from_slice(¬e.to_bytes());
bytes
}
#[must_use]
pub fn withdraw_signature_message(
counter: u64,
address: StealthAddress,
nonce: BlsScalar,
) -> [u8; WITHDRAW_MESSAGE_SIZE] {
let mut bytes = [0u8; WITHDRAW_MESSAGE_SIZE];
bytes[..u64::SIZE].copy_from_slice(&counter.to_bytes());
bytes[u64::SIZE..u64::SIZE + StealthAddress::SIZE]
.copy_from_slice(&address.to_bytes());
bytes[u64::SIZE + StealthAddress::SIZE..]
.copy_from_slice(&nonce.to_bytes());
bytes
}