use crate::{domain::ExactDecimal, error::Error};
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Family {
Evm,
Solana,
Utxo,
Move,
Tron,
Stellar,
}
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(try_from = "ChainFields")]
pub struct Chain {
id: u64,
family: Family,
}
impl Chain {
pub fn new(id: u64, family: Family) -> Result<Self, Error> {
let reserved = match id {
1_151_111_081_099_710 | 1_021_111_031_099_710 => Some(Family::Solana),
9_270_000_000_000_000 => Some(Family::Move),
20_000_000_000_001..=20_000_000_000_005 => Some(Family::Utxo),
728_126_428 => Some(Family::Tron),
1_201_081_091_099_710 => Some(Family::Stellar),
_ => None,
};
if id == 0 || reserved.is_some_and(|expected| expected != family) {
return Err(super::invalid_identity());
}
Ok(Self { id, family })
}
#[must_use]
pub const fn id(self) -> u64 {
self.id
}
#[must_use]
pub const fn family(self) -> Family {
self.family
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ChainFields {
id: u64,
family: Family,
}
impl TryFrom<ChainFields> for Chain {
type Error = Error;
fn try_from(v: ChainFields) -> Result<Self, Error> {
Self::new(v.id, v.family)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "Vec<Chain>", into = "Vec<Chain>")]
pub struct ChainCatalogue(Vec<Chain>);
impl ChainCatalogue {
pub fn new(chains: Vec<Chain>) -> Result<Self, Error> {
let mut ids = BTreeSet::new();
if chains.is_empty() || chains.len() > 128 || chains.iter().any(|c| !ids.insert(c.id)) {
return Err(super::invalid_identity());
}
Ok(Self(chains))
}
pub fn resolve(&self, id: u64) -> Result<Chain, Error> {
self.0
.iter()
.find(|c| c.id == id)
.copied()
.ok_or(Error::UnsupportedCapability)
}
#[must_use]
pub fn chains(&self) -> &[Chain] {
&self.0
}
}
impl TryFrom<Vec<Chain>> for ChainCatalogue {
type Error = Error;
fn try_from(v: Vec<Chain>) -> Result<Self, Error> {
Self::new(v)
}
}
impl From<ChainCatalogue> for Vec<Chain> {
fn from(v: ChainCatalogue) -> Self {
v.0
}
}
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct Identifier(String);
impl Identifier {
pub fn new(text: &str) -> Result<Self, Error> {
if text.is_empty()
|| text.len() > 256
|| !text
.bytes()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b':' | b'+'))
{
return Err(super::invalid_identity());
}
Ok(Self(text.to_owned()))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl TryFrom<String> for Identifier {
type Error = Error;
fn try_from(v: String) -> Result<Self, Error> {
Self::new(&v)
}
}
impl From<Identifier> for String {
fn from(v: Identifier) -> Self {
v.0
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct Text(String);
impl Text {
pub fn new(text: &str) -> Result<Self, Error> {
if text.len() > 512 {
return Err(super::invalid_record());
}
Ok(Self(text.to_owned()))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl TryFrom<String> for Text {
type Error = Error;
fn try_from(v: String) -> Result<Self, Error> {
Self::new(&v)
}
}
impl From<Text> for String {
fn from(v: Text) -> Self {
v.0
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "IdentityFields")]
pub struct Asset {
chain: Chain,
identifier: String,
}
impl Asset {
pub fn new(chain: Chain, identifier: &str) -> Result<Self, Error> {
Ok(Self {
chain,
identifier: qualified_text(chain, identifier, false)?,
})
}
#[must_use]
pub const fn chain(&self) -> Chain {
self.chain
}
#[must_use]
pub fn identifier(&self) -> &str {
&self.identifier
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "IdentityFields")]
pub struct Account {
chain: Chain,
identifier: String,
}
impl Account {
pub fn new(chain: Chain, identifier: &str) -> Result<Self, Error> {
Ok(Self {
chain,
identifier: qualified_text(chain, identifier, true)?,
})
}
#[must_use]
pub const fn chain(&self) -> Chain {
self.chain
}
#[must_use]
pub fn identifier(&self) -> &str {
&self.identifier
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct IdentityFields {
chain: Chain,
identifier: String,
}
impl TryFrom<IdentityFields> for Asset {
type Error = Error;
fn try_from(v: IdentityFields) -> Result<Self, Error> {
Self::new(v.chain, &v.identifier)
}
}
impl TryFrom<IdentityFields> for Account {
type Error = Error;
fn try_from(v: IdentityFields) -> Result<Self, Error> {
Self::new(v.chain, &v.identifier)
}
}
fn qualified_text(chain: Chain, text: &str, account: bool) -> Result<String, Error> {
if text.is_empty() || text.len() > 1024 || !text.bytes().all(|b| b.is_ascii_graphic()) {
return Err(super::invalid_identity());
}
match chain.family {
Family::Evm => crate::domain::Address::parse(text)
.map(|v| v.to_string())
.map_err(|_| super::invalid_identity()),
Family::Solana => {
let mut bytes = [0_u8; 32];
if bs58::decode(text).onto(&mut bytes).ok() != Some(32)
|| bs58::encode(bytes).into_string() != text
{
return Err(super::invalid_identity());
}
Ok(text.to_owned())
}
Family::Utxo if account && chain.id == 20_000_000_000_001 => {
let address = text
.parse::<bitcoin::Address<bitcoin::address::NetworkUnchecked>>()
.map_err(|_| super::invalid_identity())?;
address
.require_network(bitcoin::Network::Bitcoin)
.map(|v| v.to_string())
.map_err(|_| super::invalid_identity())
}
_ => Ok(text.to_owned()),
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "ExactDecimal", into = "ExactDecimal")]
pub struct Slippage(ExactDecimal);
impl Slippage {
pub fn new(value: ExactDecimal) -> Result<Self, Error> {
if value.is_negative() || value >= ExactDecimal::parse("1")? {
return Err(super::invalid_record());
}
Ok(Self(value))
}
#[must_use]
pub const fn value(&self) -> &ExactDecimal {
&self.0
}
}
impl TryFrom<ExactDecimal> for Slippage {
type Error = Error;
fn try_from(v: ExactDecimal) -> Result<Self, Error> {
Self::new(v)
}
}
impl From<Slippage> for ExactDecimal {
fn from(v: Slippage) -> Self {
v.0
}
}