Skip to main content

torvyon_core/
lib.rs

1//! Keys, addresses, signing and transaction building for the Torvyon blockchain.
2//!
3//! Pure logic: no network, no I/O, no async. Every encoding here is checked
4//! against vectors generated by the Go node (`spec/vectors`).
5
6mod address;
7mod amount;
8mod atomic_swap;
9mod conditions;
10mod error;
11mod hash;
12mod keys;
13mod mnemonic;
14pub mod pq;
15pub mod tx;
16
17pub use address::Address;
18pub use amount::{Amount, NIBBLES_PER_BYTE};
19pub use atomic_swap::{
20    AtomicSwap, AtomicSwapParams, ChainClock, SwapLeg, SwapLocks, SwapSecret, SwapTimeouts,
21};
22pub use conditions::{
23    Condition, ConditionTemplates, EscrowRefundTemplate, HtlcTemplate, PaymentChannelTemplate,
24};
25pub use error::{Error, Result};
26pub use keys::{PrivateKey, PublicKey, Signature};
27pub use mnemonic::{mnemonic_from_private_key, private_key_from_mnemonic};
28pub use pq::{PQAddress, PQKeypair, PQNetwork, PQSignature};
29
30/// Chain ID of Torvyon Mainnet.
31pub const MAINNET_CHAIN_ID: &str = "torvyon-mainnet";
32
33/// Chain ID of Torvyon Testnet.
34pub const TESTNET_CHAIN_ID: &str = "torvyon-testnet-1";
35
36/// Torvyon network identity.
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub enum Network {
39    /// Live mainnet: `torvyon-mainnet`, `bt1`.
40    Mainnet,
41    /// Planned public testnet: `torvyon-testnet-1`, `tbt1`.
42    Testnet,
43}
44
45impl Network {
46    /// Chain ID for this network.
47    pub const fn chain_id(self) -> &'static str {
48        match self {
49            Self::Mainnet => MAINNET_CHAIN_ID,
50            Self::Testnet => TESTNET_CHAIN_ID,
51        }
52    }
53
54    /// Address prefix for this network.
55    pub const fn address_prefix(self) -> &'static str {
56        match self {
57            Self::Mainnet => "bt1",
58            Self::Testnet => "tbt1",
59        }
60    }
61
62    /// Network for a known chain ID. Unknown IDs default to mainnet for
63    /// backwards-compatible validation of custom/private mainnet-like chains.
64    pub fn from_chain_id(chain_id: &str) -> Self {
65        match chain_id {
66            TESTNET_CHAIN_ID => Self::Testnet,
67            _ => Self::Mainnet,
68        }
69    }
70
71    /// Network implied by an address prefix, if recognized.
72    pub fn from_address(address: &str) -> Self {
73        if address.starts_with(Self::Testnet.address_prefix()) {
74            Self::Testnet
75        } else {
76            Self::Mainnet
77        }
78    }
79}