torvyon-core 0.1.3

Keys, addresses, post-quantum signing and transaction building for the Torvyon blockchain
Documentation
//! Keys, addresses, signing and transaction building for the Torvyon blockchain.
//!
//! Pure logic: no network, no I/O, no async. Every encoding here is checked
//! against vectors generated by the Go node (`spec/vectors`).

mod address;
mod amount;
mod atomic_swap;
mod conditions;
mod error;
mod hash;
mod keys;
mod mnemonic;
pub mod pq;
pub mod tx;

pub use address::Address;
pub use amount::{Amount, NIBBLES_PER_BYTE};
pub use atomic_swap::{
    AtomicSwap, AtomicSwapParams, ChainClock, SwapLeg, SwapLocks, SwapSecret, SwapTimeouts,
};
pub use conditions::{
    Condition, ConditionTemplates, EscrowRefundTemplate, HtlcTemplate, PaymentChannelTemplate,
};
pub use error::{Error, Result};
pub use keys::{PrivateKey, PublicKey, Signature};
pub use mnemonic::{mnemonic_from_private_key, private_key_from_mnemonic};
pub use pq::{PQAddress, PQKeypair, PQNetwork, PQSignature};

/// Chain ID of Torvyon Mainnet.
pub const MAINNET_CHAIN_ID: &str = "torvyon-mainnet";

/// Chain ID of Torvyon Testnet.
pub const TESTNET_CHAIN_ID: &str = "torvyon-testnet-1";

/// Torvyon network identity.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Network {
    /// Live mainnet: `torvyon-mainnet`, `bt1`.
    Mainnet,
    /// Planned public testnet: `torvyon-testnet-1`, `tbt1`.
    Testnet,
}

impl Network {
    /// Chain ID for this network.
    pub const fn chain_id(self) -> &'static str {
        match self {
            Self::Mainnet => MAINNET_CHAIN_ID,
            Self::Testnet => TESTNET_CHAIN_ID,
        }
    }

    /// Address prefix for this network.
    pub const fn address_prefix(self) -> &'static str {
        match self {
            Self::Mainnet => "bt1",
            Self::Testnet => "tbt1",
        }
    }

    /// Network for a known chain ID. Unknown IDs default to mainnet for
    /// backwards-compatible validation of custom/private mainnet-like chains.
    pub fn from_chain_id(chain_id: &str) -> Self {
        match chain_id {
            TESTNET_CHAIN_ID => Self::Testnet,
            _ => Self::Mainnet,
        }
    }

    /// Network implied by an address prefix, if recognized.
    pub fn from_address(address: &str) -> Self {
        if address.starts_with(Self::Testnet.address_prefix()) {
            Self::Testnet
        } else {
            Self::Mainnet
        }
    }
}