use super::invalid;
use crate::{
domain::{Amount, U256, valid_label},
error::{Error, ValidationError},
};
use serde::{Deserialize, Deserializer, Serialize, Serializer, de::DeserializeOwned};
use std::{fmt, marker::PhantomData};
#[doc(hidden)]
pub trait NetworkPolicy:
super::sealed::Network + Copy + fmt::Debug + Eq + Serialize + DeserializeOwned
{
fn genesis_hash(self) -> BlockHash;
fn blockcypher_name(self) -> Option<&'static str>;
}
#[doc(hidden)]
pub trait AddressPolicy:
super::sealed::Address + Clone + fmt::Debug + fmt::Display + Eq + Serialize + DeserializeOwned
{
type Network: NetworkPolicy;
const ATOMIC_UNIT: &'static str;
const TRANSACTION_MAXIMUM: u64;
const SUPPORTS_WITNESS: bool;
fn parse(value: &str, network: Self::Network) -> Result<Self, Error>;
fn network(&self) -> Self::Network;
}
macro_rules! hash_type {
($name:ident, $doc:literal) => {
#[doc = $doc]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct $name([u8; 32]);
impl $name {
#[must_use]
pub const fn from_display_bytes(value: [u8; 32]) -> Self {
Self(value)
}
pub fn parse(value: &str) -> Result<Self, Error> {
let decoded = const_hex::decode_to_array(value).map_err(|_| invalid())?;
if value.len() != 64 {
return Err(invalid());
}
Ok(Self(decoded))
}
#[must_use]
pub const fn display_bytes(&self) -> &[u8; 32] {
&self.0
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&const_hex::encode(self.0))
}
}
impl Serialize for $name {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.collect_str(self)
}
}
impl<'de> Deserialize<'de> for $name {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Self::parse(&String::deserialize(d)?).map_err(serde::de::Error::custom)
}
}
};
}
hash_type!(
BlockHash,
"An exact source-reported block hash; no proof-of-work or inclusion proof is implied."
);
hash_type!(
Txid,
"An exact source-reported transaction identifier; raw bytes are not decoded or independently hashed."
);
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "NetworkFields<N>", bound(deserialize = "N: NetworkPolicy"))]
pub struct NetworkId<N: NetworkPolicy> {
network: N,
genesis_hash: BlockHash,
alias: String,
}
impl<N: NetworkPolicy> NetworkId<N> {
pub fn new(network: N, alias: impl Into<String>) -> Result<Self, Error> {
let alias = alias.into();
if !valid_label(&alias, 64) {
return Err(ValidationError::InvalidNetworkAlias.into());
}
Ok(Self {
network,
genesis_hash: network.genesis_hash(),
alias,
})
}
#[must_use]
pub const fn network(&self) -> N {
self.network
}
#[must_use]
pub const fn genesis_hash(&self) -> &BlockHash {
&self.genesis_hash
}
#[must_use]
pub fn alias(&self) -> &str {
&self.alias
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields, bound(deserialize = "N: NetworkPolicy"))]
struct NetworkFields<N: NetworkPolicy> {
network: N,
genesis_hash: BlockHash,
alias: String,
}
impl<N: NetworkPolicy> TryFrom<NetworkFields<N>> for NetworkId<N> {
type Error = Error;
fn try_from(value: NetworkFields<N>) -> Result<Self, Error> {
let network = Self::new(value.network, value.alias)?;
if network.genesis_hash != value.genesis_hash {
return Err(ValidationError::NetworkMismatch.into());
}
Ok(network)
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct AtomicAmount<A> {
raw: u64,
family: PhantomData<fn() -> A>,
}
impl<A> Copy for AtomicAmount<A> {}
impl<A> Clone for AtomicAmount<A> {
fn clone(&self) -> Self {
*self
}
}
impl<A: AddressPolicy> AtomicAmount<A> {
#[must_use]
pub const fn new(raw: u64) -> Self {
Self {
raw,
family: PhantomData,
}
}
pub fn from_decimal(value: &str) -> Result<Self, Error> {
if value.is_empty()
|| !value.bytes().all(|b| b.is_ascii_digit())
|| (value.len() > 1 && value.starts_with('0'))
{
return Err(invalid());
}
value.parse().map(Self::new).map_err(|_| invalid())
}
#[must_use]
pub const fn raw(self) -> u64 {
self.raw
}
#[must_use]
pub fn amount(self) -> Amount {
Amount::new(U256::from(self.raw), Some(8))
}
#[must_use]
pub const fn unit() -> &'static str {
A::ATOMIC_UNIT
}
}
impl<A: AddressPolicy> Serialize for AtomicAmount<A> {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&self.raw.to_string())
}
}
impl<'de, A: AddressPolicy> Deserialize<'de> for AtomicAmount<A> {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Self::from_decimal(&String::deserialize(d)?).map_err(serde::de::Error::custom)
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct MempoolDelta<A> {
raw: i128,
family: PhantomData<fn() -> A>,
}
impl<A> Copy for MempoolDelta<A> {}
impl<A> Clone for MempoolDelta<A> {
fn clone(&self) -> Self {
*self
}
}
impl<A: AddressPolicy> MempoolDelta<A> {
pub fn new(raw: i128) -> Result<Self, Error> {
if raw.unsigned_abs() > u128::from(u64::MAX) {
return Err(invalid());
}
Ok(Self {
raw,
family: PhantomData,
})
}
pub fn from_decimal(value: &str) -> Result<Self, Error> {
let magnitude = value.strip_prefix('-').unwrap_or(value);
if magnitude.is_empty()
|| !magnitude.bytes().all(|b| b.is_ascii_digit())
|| (magnitude.len() > 1 && magnitude.starts_with('0'))
|| value == "-0"
{
return Err(invalid());
}
Self::new(value.parse().map_err(|_| invalid())?)
}
#[must_use]
pub const fn raw(&self) -> i128 {
self.raw
}
}
impl<A: AddressPolicy> Serialize for MempoolDelta<A> {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&self.raw.to_string())
}
}
impl<'de, A: AddressPolicy> Deserialize<'de> for MempoolDelta<A> {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Self::from_decimal(&String::deserialize(d)?).map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Eq, PartialEq)]
pub struct Bytes(Vec<u8>);
impl Bytes {
pub const MAXIMUM_BYTES: usize = 1_048_576;
pub fn new(value: Vec<u8>) -> Result<Self, Error> {
if value.len() > Self::MAXIMUM_BYTES {
return Err(ValidationError::InvalidUtxoBytes.into());
}
Ok(Self(value))
}
pub fn from_hex(value: &str) -> Result<Self, Error> {
if value.len() > Self::MAXIMUM_BYTES * 2
|| !value.len().is_multiple_of(2)
|| !value.bytes().all(|b| b.is_ascii_hexdigit())
{
return Err(ValidationError::InvalidUtxoBytes.into());
}
Self::new(
const_hex::decode(value)
.map_err(|_| Error::Validation(ValidationError::InvalidUtxoBytes))?,
)
}
#[must_use]
pub fn as_slice(&self) -> &[u8] {
&self.0
}
}
impl fmt::Debug for Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Bytes")
.field("length", &self.0.len())
.finish_non_exhaustive()
}
}
impl Serialize for Bytes {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&const_hex::encode(&self.0))
}
}
impl<'de> Deserialize<'de> for Bytes {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Self::from_hex(&String::deserialize(d)?).map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct SourceText(String);
impl SourceText {
pub fn new(value: impl Into<String>) -> Result<Self, Error> {
let value = value.into();
if value.len() > 128 || value.chars().any(char::is_control) {
return Err(invalid());
}
Ok(Self(value))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Debug for SourceText {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SourceText")
.field("bytes", &self.0.len())
.finish_non_exhaustive()
}
}
impl TryFrom<String> for SourceText {
type Error = Error;
fn try_from(value: String) -> Result<Self, Error> {
Self::new(value)
}
}
impl From<SourceText> for String {
fn from(value: SourceText) -> Self {
value.0
}
}