use super::{Address, Bytes, TokenCategory, Txid};
use crate::{domain::ExactDecimal, error::Error};
use serde::{
Deserialize, Deserializer, Serialize, Serializer,
de::{SeqAccess, Visitor},
};
use std::{collections::HashSet, fmt};
pub(super) const MAX_ENTRIES: usize = 100_000;
pub(super) const MAX_MONEY: u64 = 2_100_000_000_000_000;
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Satoshis(u64);
impl Satoshis {
#[must_use]
pub const fn from_raw(value: u64) -> Self {
Self(value)
}
#[must_use]
pub const fn raw(self) -> u64 {
self.0
}
pub fn parse(value: &str) -> Result<Self, Error> {
if value.is_empty()
|| value.len() > 20
|| (value.len() > 1 && value.starts_with('0'))
|| !value.bytes().all(|b| b.is_ascii_digit())
{
return Err(super::invalid());
}
value.parse().map(Self).map_err(|_| super::invalid())
}
pub fn from_bch(value: &ExactDecimal) -> Result<Self, Error> {
if value.is_negative() {
return Err(super::invalid());
}
let text = value.canonical();
let (whole, fraction) = text.split_once('.').unwrap_or((&text, ""));
if fraction.len() > 8 {
return Err(super::invalid());
}
let whole = whole.parse::<u64>().map_err(|_| super::invalid())?;
let scale = whole.checked_mul(100_000_000).ok_or_else(super::invalid)?;
let fraction = if fraction.is_empty() {
0
} else {
format!("{fraction:0<8}")
.parse::<u64>()
.map_err(|_| super::invalid())?
};
scale
.checked_add(fraction)
.map(Self)
.ok_or_else(super::invalid)
}
}
impl fmt::Display for Satoshis {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Serialize for Satoshis {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.collect_str(self)
}
}
impl<'de> Deserialize<'de> for Satoshis {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Self::parse(&String::deserialize(d)?).map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SignedSatoshis(i128);
impl SignedSatoshis {
pub fn new(value: i128) -> Result<Self, Error> {
if value.unsigned_abs() > u128::from(u64::MAX) {
return Err(super::invalid());
}
Ok(Self(value))
}
#[must_use]
pub const fn raw(self) -> i128 {
self.0
}
pub fn parse(value: &str) -> Result<Self, Error> {
let magnitude = value.strip_prefix('-').unwrap_or(value);
let amount = Satoshis::parse(magnitude)?;
if value.starts_with('-') && amount.raw() == 0 {
return Err(super::invalid());
}
Self::new(if value.starts_with('-') {
-i128::from(amount.raw())
} else {
i128::from(amount.raw())
})
}
}
impl fmt::Display for SignedSatoshis {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Serialize for SignedSatoshis {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.collect_str(self)
}
}
impl<'de> Deserialize<'de> for SignedSatoshis {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Self::parse(&String::deserialize(d)?).map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TokenFilter {
IncludeTokens,
ExcludeTokens,
TokensOnly,
}
impl TokenFilter {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::IncludeTokens => "include_tokens",
Self::ExcludeTokens => "exclude_tokens",
Self::TokensOnly => "tokens_only",
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "u32", into = "u32")]
pub struct CollectionLimit(u32);
impl CollectionLimit {
pub fn new(value: u32) -> Result<Self, Error> {
if value == 0 || value > 100_000 {
return Err(super::invalid());
}
Ok(Self(value))
}
#[must_use]
pub const fn get(self) -> u32 {
self.0
}
}
impl TryFrom<u32> for CollectionLimit {
type Error = Error;
fn try_from(v: u32) -> Result<Self, Error> {
Self::new(v)
}
}
impl From<CollectionLimit> for u32 {
fn from(v: CollectionLimit) -> Self {
v.0
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AddressBalance {
pub address: Address,
pub token_filter: TokenFilter,
pub confirmed: Satoshis,
pub unconfirmed: SignedSatoshis,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum HistoryUpperBound {
Height {
height: u32,
},
OpenTip,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "RangeFields")]
pub struct HistoryRange {
from_height: u32,
to: HistoryUpperBound,
limit: CollectionLimit,
}
impl HistoryRange {
pub fn new(
from_height: u32,
to: HistoryUpperBound,
limit: CollectionLimit,
) -> Result<Self, Error> {
if matches!(to,HistoryUpperBound::Height{height} if from_height>height) {
return Err(super::invalid());
}
Ok(Self {
from_height,
to,
limit,
})
}
#[must_use]
pub const fn from_height(self) -> u32 {
self.from_height
}
#[must_use]
pub const fn upper(self) -> HistoryUpperBound {
self.to
}
#[must_use]
pub const fn limit(self) -> CollectionLimit {
self.limit
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RangeFields {
from_height: u32,
to: HistoryUpperBound,
limit: CollectionLimit,
}
impl TryFrom<RangeFields> for HistoryRange {
type Error = Error;
fn try_from(v: RangeFields) -> Result<Self, Error> {
Self::new(v.from_height, v.to, v.limit)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum HistoryState {
Confirmed {
height: u32,
},
ZeroHeight,
UnconfirmedParents,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "EntryFields")]
pub struct HistoryEntry {
txid: Txid,
state: HistoryState,
fee: Option<Satoshis>,
}
impl HistoryEntry {
pub fn new(txid: Txid, state: HistoryState, fee: Option<Satoshis>) -> Result<Self, Error> {
match state {
HistoryState::Confirmed { height } if height == 0 || fee.is_some() => {
return Err(super::invalid());
}
HistoryState::ZeroHeight | HistoryState::UnconfirmedParents if fee.is_none() => {
return Err(super::invalid());
}
_ => {}
}
Ok(Self { txid, state, fee })
}
#[must_use]
pub const fn txid(&self) -> Txid {
self.txid
}
#[must_use]
pub const fn state(&self) -> HistoryState {
self.state
}
#[must_use]
pub const fn fee(&self) -> Option<Satoshis> {
self.fee
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct EntryFields {
txid: Txid,
state: HistoryState,
fee: Option<Satoshis>,
}
impl TryFrom<EntryFields> for HistoryEntry {
type Error = Error;
fn try_from(v: EntryFields) -> Result<Self, Error> {
Self::new(v.txid, v.state, v.fee)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "HistoryFields")]
pub struct History {
address: Address,
range: HistoryRange,
entries: Vec<HistoryEntry>,
}
impl History {
pub fn new(
address: Address,
range: HistoryRange,
entries: Vec<HistoryEntry>,
) -> Result<Self, Error> {
if entries.len() > range.limit.get() as usize {
return Err(super::invalid());
}
let mut ids = HashSet::new();
let mut last_height = 0;
let mut mempool = false;
for entry in &entries {
if !ids.insert(entry.txid) {
return Err(super::invalid());
}
match entry.state {
HistoryState::Confirmed { height } => {
if mempool
|| height < last_height
|| height < range.from_height
|| matches!(range.to,HistoryUpperBound::Height{height:upper} if height>=upper)
{
return Err(super::invalid());
}
last_height = height;
}
HistoryState::ZeroHeight | HistoryState::UnconfirmedParents => {
if !matches!(range.to, HistoryUpperBound::OpenTip) {
return Err(super::invalid());
}
mempool = true;
}
}
}
Ok(Self {
address,
range,
entries,
})
}
#[must_use]
pub const fn address(&self) -> &Address {
&self.address
}
#[must_use]
pub const fn range(&self) -> HistoryRange {
self.range
}
#[must_use]
pub fn entries(&self) -> &[HistoryEntry] {
&self.entries
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct HistoryFields {
address: Address,
range: HistoryRange,
#[serde(deserialize_with = "bounded_entries")]
entries: Vec<HistoryEntry>,
}
impl TryFrom<HistoryFields> for History {
type Error = Error;
fn try_from(v: HistoryFields) -> Result<Self, Error> {
Self::new(v.address, v.range, v.entries)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "u32", into = "u32")]
pub struct FeeTarget(u32);
impl FeeTarget {
pub fn new(value: u32) -> Result<Self, Error> {
if value == 0 {
return Err(super::invalid());
}
Ok(Self(value))
}
#[must_use]
pub const fn blocks(self) -> u32 {
self.0
}
}
impl TryFrom<u32> for FeeTarget {
type Error = Error;
fn try_from(v: u32) -> Result<Self, Error> {
Self::new(v)
}
}
impl From<FeeTarget> for u32 {
fn from(v: FeeTarget) -> Self {
v.0
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "FeeFields")]
pub struct FeeEstimate {
target: FeeTarget,
bch_per_kilobyte: Option<ExactDecimal>,
}
impl FeeEstimate {
pub fn new(target: FeeTarget, bch_per_kilobyte: Option<ExactDecimal>) -> Result<Self, Error> {
if bch_per_kilobyte
.as_ref()
.is_some_and(ExactDecimal::is_negative)
{
return Err(super::invalid());
}
Ok(Self {
target,
bch_per_kilobyte,
})
}
#[must_use]
pub const fn target(&self) -> FeeTarget {
self.target
}
#[must_use]
pub const fn bch_per_kilobyte(&self) -> Option<&ExactDecimal> {
self.bch_per_kilobyte.as_ref()
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct FeeFields {
target: FeeTarget,
bch_per_kilobyte: Option<ExactDecimal>,
}
impl TryFrom<FeeFields> for FeeEstimate {
type Error = Error;
fn try_from(v: FeeFields) -> Result<Self, Error> {
Self::new(v.target, v.bch_per_kilobyte)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NftCapability {
None,
Mutable,
Minting,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "NftFields")]
pub struct Nft {
capability: NftCapability,
commitment: Bytes,
}
impl Nft {
pub fn new(capability: NftCapability, commitment: Bytes) -> Result<Self, Error> {
if commitment.as_slice().len() > 40 {
return Err(super::invalid());
}
Ok(Self {
capability,
commitment,
})
}
#[must_use]
pub const fn capability(&self) -> NftCapability {
self.capability
}
#[must_use]
pub const fn commitment(&self) -> &Bytes {
&self.commitment
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct NftFields {
capability: NftCapability,
commitment: Bytes,
}
impl TryFrom<NftFields> for Nft {
type Error = Error;
fn try_from(v: NftFields) -> Result<Self, Error> {
Self::new(v.capability, v.commitment)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TokenAmount(u64);
impl TokenAmount {
pub fn new(value: u64) -> Result<Self, Error> {
if value > i64::MAX as u64 {
return Err(super::invalid());
}
Ok(Self(value))
}
#[must_use]
pub const fn raw(self) -> u64 {
self.0
}
pub fn parse(value: &str) -> Result<Self, Error> {
Self::new(Satoshis::parse(value)?.raw())
}
}
impl Serialize for TokenAmount {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&self.0.to_string())
}
}
impl<'de> Deserialize<'de> for TokenAmount {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Self::parse(&String::deserialize(d)?).map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "TokenFields")]
pub struct TokenData {
category: TokenCategory,
amount: TokenAmount,
nft: Option<Nft>,
}
impl TokenData {
pub fn new(
category: TokenCategory,
amount: TokenAmount,
nft: Option<Nft>,
) -> Result<Self, Error> {
if amount.raw() > i64::MAX as u64 || (amount.raw() == 0 && nft.is_none()) {
return Err(super::invalid());
}
Ok(Self {
category,
amount,
nft,
})
}
#[must_use]
pub const fn category(&self) -> TokenCategory {
self.category
}
#[must_use]
pub const fn amount(&self) -> u64 {
self.amount.raw()
}
#[must_use]
pub const fn nft(&self) -> Option<&Nft> {
self.nft.as_ref()
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct TokenFields {
category: TokenCategory,
amount: TokenAmount,
nft: Option<Nft>,
}
impl TryFrom<TokenFields> for TokenData {
type Error = Error;
fn try_from(v: TokenFields) -> Result<Self, Error> {
Self::new(v.category, v.amount, v.nft)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct UnspentOutput {
pub txid: Txid,
pub output_index: u32,
pub height: u32,
pub value: Satoshis,
pub token_data: Option<TokenData>,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "UnspentFields")]
pub struct UnspentOutputs {
address: Address,
token_filter: TokenFilter,
limit: CollectionLimit,
outputs: Vec<UnspentOutput>,
}
impl UnspentOutputs {
pub fn new(
address: Address,
token_filter: TokenFilter,
limit: CollectionLimit,
outputs: Vec<UnspentOutput>,
) -> Result<Self, Error> {
if outputs.len() > limit.get() as usize {
return Err(super::invalid());
}
let mut ids = HashSet::new();
let mut last = 0;
let mut mempool = false;
for output in &outputs {
if !ids.insert((output.txid, output.output_index)) || output.value.raw() > MAX_MONEY {
return Err(super::invalid());
}
if matches!(token_filter, TokenFilter::ExcludeTokens) && output.token_data.is_some()
|| matches!(token_filter, TokenFilter::TokensOnly) && output.token_data.is_none()
{
return Err(super::invalid());
}
if output.height == 0 {
mempool = true;
} else {
if mempool || output.height < last {
return Err(super::invalid());
}
last = output.height;
}
}
Ok(Self {
address,
token_filter,
limit,
outputs,
})
}
#[must_use]
pub const fn address(&self) -> &Address {
&self.address
}
#[must_use]
pub const fn token_filter(&self) -> TokenFilter {
self.token_filter
}
#[must_use]
pub const fn limit(&self) -> CollectionLimit {
self.limit
}
#[must_use]
pub fn outputs(&self) -> &[UnspentOutput] {
&self.outputs
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct UnspentFields {
address: Address,
token_filter: TokenFilter,
limit: CollectionLimit,
#[serde(deserialize_with = "bounded_entries")]
outputs: Vec<UnspentOutput>,
}
impl TryFrom<UnspentFields> for UnspentOutputs {
type Error = Error;
fn try_from(v: UnspentFields) -> Result<Self, Error> {
Self::new(v.address, v.token_filter, v.limit, v.outputs)
}
}
pub(crate) fn bounded_entries<'de, D, T>(d: D) -> Result<Vec<T>, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
bounded_collection::<D, T, MAX_ENTRIES>(d)
}
pub(crate) fn bounded_addresses<'de, D, T>(d: D) -> Result<Vec<T>, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
bounded_collection::<D, T, 100>(d)
}
fn bounded_collection<'de, D, T, const MAX: usize>(d: D) -> Result<Vec<T>, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
struct Items<T, const MAX: usize>(std::marker::PhantomData<T>);
impl<'de, T: Deserialize<'de>, const MAX: usize> Visitor<'de> for Items<T, MAX> {
type Value = Vec<T>;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a bounded collection")
}
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Vec<T>, A::Error> {
let mut values = Vec::new();
while let Some(item) = seq.next_element()? {
if values.len() == MAX {
return Err(serde::de::Error::custom("collection exceeds limit"));
}
values.push(item);
}
Ok(values)
}
}
d.deserialize_seq(Items::<T, MAX>(std::marker::PhantomData))
}