use alloc::string::{String, ToString};
use core::fmt;
use core::str::{self, FromStr};
use hashes::Hash;
use hashes::sha256::Hash as Sha256Hash;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{Value, json};
use super::error::Error;
use super::{Kind, Tag, Tags};
use crate::nips::nip13;
use crate::nips::nip19::FromBech32;
use crate::nips::nip21::FromNostrUri;
use crate::{PublicKey, Timestamp};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EventId([u8; 32]);
impl fmt::Debug for EventId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "EventId({})", self.to_hex())
}
}
impl EventId {
pub const LEN: usize = 32;
pub fn new(
public_key: &PublicKey,
created_at: &Timestamp,
kind: &Kind,
tags: &Tags,
content: &str,
) -> Self {
let json: Value = json!([0, public_key, created_at, kind, tags, content]);
let event_str: String = json.to_string();
let hash: Sha256Hash = Sha256Hash::hash(event_str.as_bytes());
Self::from_byte_array(hash.to_byte_array())
}
#[inline]
pub const fn from_byte_array(bytes: [u8; Self::LEN]) -> Self {
Self(bytes)
}
#[inline]
pub const fn all_zeros() -> Self {
Self::from_byte_array([0u8; Self::LEN])
}
pub fn parse(id: &str) -> Result<Self, Error> {
if let Ok(id) = Self::from_hex(id) {
return Ok(id);
}
if let Ok(id) = Self::from_bech32(id) {
return Ok(id);
}
if let Ok(id) = Self::from_nostr_uri(id) {
return Ok(id);
}
Err(Error::InvalidId)
}
pub fn from_hex(hex: &str) -> Result<Self, Error> {
let mut bytes: [u8; Self::LEN] = [0u8; Self::LEN];
faster_hex::hex_decode(hex.as_bytes(), &mut bytes)?;
Ok(Self::from_byte_array(bytes))
}
pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
if slice.len() != Self::LEN {
return Err(Error::InvalidId);
}
let mut bytes: [u8; Self::LEN] = [0u8; Self::LEN];
bytes.copy_from_slice(slice);
Ok(Self::from_byte_array(bytes))
}
#[inline]
pub fn as_bytes(&self) -> &[u8; Self::LEN] {
&self.0
}
#[inline]
pub fn to_bytes(self) -> [u8; Self::LEN] {
self.0
}
#[inline]
pub fn to_hex(&self) -> String {
unsafe { String::from_utf8_unchecked(self.to_hex_byte_array().to_vec()) }
}
#[inline]
pub fn to_hex_byte_array(&self) -> [u8; Self::LEN * 2] {
let mut buf = [0u8; Self::LEN * 2];
faster_hex::hex_encode(self.as_bytes(), &mut buf).expect("Buffer size is correct");
buf
}
#[inline]
pub fn check_pow(&self, difficulty: u8) -> bool {
nip13::get_leading_zero_bits(self.as_bytes()) >= difficulty
}
}
impl FromStr for EventId {
type Err = Error;
fn from_str(id: &str) -> Result<Self, Self::Err> {
Self::parse(id)
}
}
impl AsRef<[u8]> for EventId {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl AsRef<[u8; EventId::LEN]> for EventId {
fn as_ref(&self) -> &[u8; EventId::LEN] {
self.as_bytes()
}
}
impl fmt::LowerHex for EventId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_hex())
}
}
impl fmt::Display for EventId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::LowerHex::fmt(self, f)
}
}
impl From<EventId> for String {
fn from(event_id: EventId) -> Self {
event_id.to_hex()
}
}
impl From<EventId> for Tag {
fn from(event_id: EventId) -> Self {
Tag::event(event_id)
}
}
impl Serialize for EventId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let bytes: [u8; Self::LEN * 2] = self.to_hex_byte_array();
let encoded: &str = unsafe { str::from_utf8_unchecked(&bytes) };
serializer.serialize_str(encoded)
}
}
impl<'de> Deserialize<'de> for EventId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let id: String = String::deserialize(deserializer)?;
Self::parse(&id).map_err(serde::de::Error::custom)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_check_pow() {
let id =
EventId::from_hex("2be17aa3031bdcb006f0fce80c146dea9c1c0268b0af2398bb673365c6444d45")
.unwrap();
assert!(!id.check_pow(16));
let id =
EventId::from_hex("00000340cb60be5829fbf2712a285f12cf89e5db951c5303b731651f0d71ac1b")
.unwrap();
assert!(id.check_pow(16));
assert!(id.check_pow(20));
assert!(!id.check_pow(25));
}
}
#[cfg(bench)]
mod benches {
use super::*;
use crate::test::{Bencher, black_box};
const ID: &str = "2be17aa3031bdcb006f0fce80c146dea9c1c0268b0af2398bb673365c6444d45";
#[bench]
pub fn parse_event_id_from_hex(bh: &mut Bencher) {
bh.iter(|| {
black_box(EventId::from_hex(ID)).unwrap();
});
}
}