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::{CchInvoice, CchOrder, CchOrderStatus};
36pub use channel::*;
37pub use config::*;
38pub use invoice::*;
39pub use network::{HopRequire, PersistentNetworkActorState};
40pub use onion::*;
41pub use payment::*;
42pub use primitives::{Hash256, NodeId, Privkey, Pubkey};
43pub use protocol::*;
44
45#[cfg(feature = "watchtower")]
46pub use watchtower::{ChannelData, RevocationData, SettlementData, SettlementTlc};
47
48#[cfg(feature = "watchtower")]
49pub mod watchtower;
50
51pub use serde_utils::{
52    duration_hex, from_hex, to_hex, CompactSignatureAsBytes, EntityHex, PartialSignatureAsBytes,
53    PubNonceAsBytes, SliceBase58, SliceHex, SliceHexNoPrefix, U128Hex, U16Hex, U32Hex, U64Hex,
54};
55
56pub use tentacle_multiaddr::Multiaddr;
57
58#[cfg(not(target_arch = "wasm32"))]
59pub(crate) use std::time as crate_time;
60#[cfg(target_arch = "wasm32")]
61pub(crate) use web_time as crate_time;
62
63/// Get the current timestamp as milliseconds since the Unix epoch.
64pub fn now_timestamp_as_millis_u64() -> u64 {
65    crate_time::SystemTime::now()
66        .duration_since(crate_time::UNIX_EPOCH)
67        .expect("Duration since unix epoch")
68        .as_millis() as u64
69}
70
71/// Deserialize a value from bincode-encoded bytes.
72///
73/// This function is used to deserialize values stored in the node's RocksDB.
74/// External applications can use this to read and parse store data directly.
75///
76/// # Example
77///
78/// ```ignore
79/// use fiber_types::{schema, ChannelActorState, deserialize};
80///
81/// let key = [&[schema::CHANNEL_ACTOR_STATE_PREFIX], channel_id.as_ref()].concat();
82/// let value = db.get(&key)?;
83/// let state: ChannelActorState = deserialize(&value)?;
84/// ```
85pub fn deserialize<'a, T: serde::Deserialize<'a>>(bytes: &'a [u8]) -> Result<T, bincode::Error> {
86    bincode::deserialize(bytes)
87}
88
89/// Serialize a value to bincode-encoded bytes.
90///
91/// This function is used to serialize values for storage in the node's RocksDB.
92pub fn serialize<T: serde::Serialize>(value: &T) -> Result<Vec<u8>, bincode::Error> {
93    bincode::serialize(value)
94}