Skip to main content

fiber_types/
lib.rs

1//! Core domain types for the Fiber Network.
2//!
3//! This crate provides the shared type definitions used across the Fiber Network
4//! ecosystem, including:
5//! - Primitive types: `Hash256`, `Pubkey`
6//! - Channel state types: bitflags, `ChannelState`, `TLCId`, TLC status enums
7//! - Payment types: `PaymentStatus`, `PaymentCustomRecords`
8//! - Invoice types: `CkbInvoiceStatus`, `Currency`, `HashAlgorithm`, `CkbScript`,
9//!   `InvoiceSignature`
10//! - CCH types: `CchOrderStatus`
11//! - Network types: `PersistentNetworkActorState`
12//! - Watchtower types: `ChannelData` (feature-gated)
13//! - Store schema constants
14//! - Serde utilities for hex and base58 serialization
15//! - Molecule generated types for protocol messages
16
17#[cfg(feature = "cch")]
18pub mod cch;
19pub mod channel;
20pub mod config;
21pub mod gen;
22pub mod invoice;
23pub mod network;
24pub mod onion;
25pub mod payment;
26pub mod primitives;
27pub mod protocol;
28pub mod schema;
29pub mod serde_utils;
30
31#[cfg(feature = "sample")]
32pub mod sample;
33
34#[cfg(feature = "cch")]
35pub use cch::{
36    CchInvoice, CchOrder, CchOrderStatus, CchReceiveBtcOrderCreation, CchSendBtcOrderCreation,
37};
38pub use channel::*;
39pub use config::*;
40pub use invoice::*;
41pub use network::{HopRequire, PersistentNetworkActorState};
42pub use onion::*;
43pub use payment::*;
44pub use primitives::{Hash256, NodeId, Privkey, Pubkey};
45pub use protocol::*;
46
47#[cfg(feature = "watchtower")]
48pub use watchtower::{ChannelData, RevocationData, SettlementData, SettlementTlc};
49
50#[cfg(feature = "watchtower")]
51pub mod watchtower;
52
53pub use serde_utils::{
54    duration_hex, from_hex, to_hex, CompactSignatureAsBytes, EntityHex, PartialSignatureAsBytes,
55    PubNonceAsBytes, SliceBase58, SliceHex, SliceHexNoPrefix, U128Hex, U16Hex, U32Hex, U64Hex,
56};
57
58pub use tentacle_multiaddr::Multiaddr;
59
60#[cfg(not(target_arch = "wasm32"))]
61pub(crate) use std::time as crate_time;
62#[cfg(target_arch = "wasm32")]
63pub(crate) use web_time as crate_time;
64
65/// Get the current timestamp as milliseconds since the Unix epoch.
66pub fn now_timestamp_as_millis_u64() -> u64 {
67    crate_time::SystemTime::now()
68        .duration_since(crate_time::UNIX_EPOCH)
69        .expect("Duration since unix epoch")
70        .as_millis() as u64
71}
72
73/// Deserialize a value from bincode-encoded bytes.
74///
75/// This function is used to deserialize values stored in the node's RocksDB.
76/// External applications can use this to read and parse store data directly.
77///
78/// # Example
79///
80/// ```ignore
81/// use fiber_types::{schema, ChannelActorState, deserialize};
82///
83/// let key = [&[schema::CHANNEL_ACTOR_STATE_PREFIX], channel_id.as_ref()].concat();
84/// let value = db.get(&key)?;
85/// let state: ChannelActorState = deserialize(&value)?;
86/// ```
87pub fn deserialize<'a, T: serde::Deserialize<'a>>(bytes: &'a [u8]) -> Result<T, bincode::Error> {
88    bincode::deserialize(bytes)
89}
90
91/// Serialize a value to bincode-encoded bytes.
92///
93/// This function is used to serialize values for storage in the node's RocksDB.
94pub fn serialize<T: serde::Serialize>(value: &T) -> Result<Vec<u8>, bincode::Error> {
95    bincode::serialize(value)
96}