use crate::error::Error;
use base64::{
Engine as _,
engine::general_purpose::{STANDARD, STANDARD_NO_PAD, URL_SAFE, URL_SAFE_NO_PAD},
};
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct Hash([u8; 32]);
impl Hash {
pub const ZERO: Self = Self([0; 32]);
#[must_use]
pub const fn from_bytes(bytes: [u8; 32]) -> Self {
Self(bytes)
}
pub fn parse(text: &str) -> Result<Self, Error> {
let hex = text.strip_prefix("0x").unwrap_or(text);
let mut bytes = [0; 32];
if hex.len() == 64 && hex.bytes().all(|b| b.is_ascii_hexdigit()) {
const_hex::decode_to_slice(hex, &mut bytes).map_err(|_| super::invalid_hash())?;
return Ok(Self(bytes));
}
if !(43..=44).contains(&text.len()) {
return Err(super::invalid_hash());
}
for engine in [&STANDARD, &STANDARD_NO_PAD, &URL_SAFE, &URL_SAFE_NO_PAD] {
if let Ok(value) = engine.decode(text)
&& value.len() == 32
&& engine.encode(&value) == text
{
bytes.copy_from_slice(&value);
return Ok(Self(bytes));
}
}
Err(super::invalid_hash())
}
#[must_use]
pub const fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
#[must_use]
pub fn to_hex(self) -> String {
const_hex::encode(self.0)
}
#[must_use]
pub fn to_base64(self) -> String {
STANDARD.encode(self.0)
}
}
impl fmt::Debug for Hash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("TonHash").field(&self.to_hex()).finish()
}
}
impl fmt::Display for Hash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.to_hex())
}
}
impl TryFrom<String> for Hash {
type Error = Error;
fn try_from(v: String) -> Result<Self, Error> {
Self::parse(&v)
}
}
impl From<Hash> for String {
fn from(v: Hash) -> Self {
v.to_hex()
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "ZeroStateFields")]
pub struct ZeroState {
workchain: i32,
root_hash: Hash,
file_hash: Hash,
}
impl ZeroState {
pub fn new(workchain: i32, root_hash: Hash, file_hash: Hash) -> Result<Self, Error> {
if workchain != -1 || root_hash == Hash::ZERO || file_hash == Hash::ZERO {
return Err(super::invalid_network());
}
Ok(Self {
workchain,
root_hash,
file_hash,
})
}
#[must_use]
pub const fn workchain(self) -> i32 {
self.workchain
}
#[must_use]
pub const fn root_hash(self) -> Hash {
self.root_hash
}
#[must_use]
pub const fn file_hash(self) -> Hash {
self.file_hash
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ZeroStateFields {
workchain: i32,
root_hash: Hash,
file_hash: Hash,
}
impl TryFrom<ZeroStateFields> for ZeroState {
type Error = Error;
fn try_from(v: ZeroStateFields) -> Result<Self, Error> {
Self::new(v.workchain, v.root_hash, v.file_hash)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NetworkCategory {
Mainnet,
Testnet,
Custom,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "NetworkFields")]
pub struct Network {
category: NetworkCategory,
zero_state: ZeroState,
}
impl Network {
pub fn new(category: NetworkCategory, zero_state: ZeroState) -> Result<Self, Error> {
let mainnet = public_zero_state(false)?;
let testnet = public_zero_state(true)?;
let valid = match category {
NetworkCategory::Mainnet => zero_state == mainnet,
NetworkCategory::Testnet => zero_state == testnet,
NetworkCategory::Custom => zero_state != mainnet && zero_state != testnet,
};
if !valid {
return Err(super::invalid_network());
}
Ok(Self {
category,
zero_state,
})
}
#[must_use]
pub const fn category(self) -> NetworkCategory {
self.category
}
#[must_use]
pub const fn zero_state(self) -> ZeroState {
self.zero_state
}
}
fn public_zero_state(testnet: bool) -> Result<ZeroState, Error> {
let (root, file) = if testnet {
(
"gj+B8wb/AmlPk1z1AhVI484rhrUpgSr2oSFIh56VoSg=",
"Z+IKwYS54DmmJmesw/nAD5DzWadnOCMzee+kdgSYDOg=",
)
} else {
(
"F6OpKZKqvqeFp6CQmFomXNMfMj2EnaUSOXN+Mh+wVWk=",
"XplPz01CXAps5qeSWUtxcyBfdAo5zVb1N979KLSKD24=",
)
};
ZeroState::new(-1, Hash::parse(root)?, Hash::parse(file)?)
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct NetworkFields {
category: NetworkCategory,
zero_state: ZeroState,
}
impl TryFrom<NetworkFields> for Network {
type Error = Error;
fn try_from(v: NetworkFields) -> Result<Self, Error> {
Self::new(v.category, v.zero_state)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FriendlyFlags {
pub bounceable: bool,
pub test_only: bool,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(
tag = "kind",
content = "flags",
rename_all = "snake_case",
deny_unknown_fields
)]
pub enum AddressFormat {
Raw,
Friendly(FriendlyFlags),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "AddressFields")]
pub struct Address {
workchain: i8,
account: Hash,
format: AddressFormat,
}
impl Address {
#[must_use]
pub const fn new(workchain: i8, account: Hash, format: AddressFormat) -> Self {
Self {
workchain,
account,
format,
}
}
pub fn parse(text: &str) -> Result<Self, Error> {
if let Some((workchain, account)) = text.split_once(':') {
let digits = workchain.strip_prefix('-').unwrap_or(workchain);
if digits.is_empty()
|| !digits.bytes().all(|b| b.is_ascii_digit())
|| workchain.len() > 4
|| account.len() != 64
|| !account.bytes().all(|b| b.is_ascii_hexdigit())
{
return Err(super::invalid_address());
}
let workchain = workchain
.parse::<i8>()
.map_err(|_| super::invalid_address())?;
let account = Hash::parse(account).map_err(|_| super::invalid_address())?;
return Ok(Self::new(workchain, account, AddressFormat::Raw));
}
if text.len() != 48 {
return Err(super::invalid_address());
}
let bytes = STANDARD
.decode(text)
.or_else(|_| URL_SAFE.decode(text))
.map_err(|_| super::invalid_address())?;
if bytes.len() != 36 || (STANDARD.encode(&bytes) != text && URL_SAFE.encode(&bytes) != text)
{
return Err(super::invalid_address());
}
let tag = bytes[0];
if ![0x11, 0x51, 0x91, 0xd1].contains(&tag) {
return Err(super::invalid_address());
}
let checksum = crc::Crc::<u16>::new(&crc::CRC_16_XMODEM)
.checksum(&bytes[..34])
.to_be_bytes();
if bytes[34..] != checksum {
return Err(super::invalid_address());
}
let mut account = [0; 32];
account.copy_from_slice(&bytes[2..34]);
Ok(Self::new(
i8::from_be_bytes([bytes[1]]),
Hash::from_bytes(account),
AddressFormat::Friendly(FriendlyFlags {
bounceable: tag & 0x40 == 0,
test_only: tag & 0x80 != 0,
}),
))
}
pub fn check_network(self, network: Network) -> Result<(), Error> {
if matches!(
self.format,
AddressFormat::Friendly(FriendlyFlags {
test_only: true,
..
})
) && network.category == NetworkCategory::Mainnet
{
return Err(super::invalid_network());
}
Ok(())
}
#[must_use]
pub const fn workchain(self) -> i8 {
self.workchain
}
#[must_use]
pub const fn account(self) -> Hash {
self.account
}
#[must_use]
pub const fn format(self) -> AddressFormat {
self.format
}
#[must_use]
pub fn same_account(self, other: Self) -> bool {
self.workchain == other.workchain && self.account.0 == other.account.0
}
#[must_use]
pub fn to_raw(self) -> String {
format!("{}:{}", self.workchain, self.account)
}
#[must_use]
pub fn to_friendly(self, flags: FriendlyFlags, url_safe: bool) -> String {
let mut bytes = [0; 36];
bytes[0] =
(if flags.bounceable { 0x11 } else { 0x51 }) | if flags.test_only { 0x80 } else { 0 };
bytes[1] = self.workchain.to_be_bytes()[0];
bytes[2..34].copy_from_slice(self.account.as_bytes());
let checksum = crc::Crc::<u16>::new(&crc::CRC_16_XMODEM)
.checksum(&bytes[..34])
.to_be_bytes();
bytes[34..].copy_from_slice(&checksum);
if url_safe {
URL_SAFE.encode(bytes)
} else {
STANDARD.encode(bytes)
}
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct AddressFields {
workchain: i8,
account: Hash,
format: AddressFormat,
}
impl TryFrom<AddressFields> for Address {
type Error = Error;
fn try_from(v: AddressFields) -> Result<Self, Error> {
Ok(Self::new(v.workchain, v.account, v.format))
}
}