Skip to main content

fedimint_core/
lib.rs

1#![deny(clippy::pedantic, clippy::nursery)]
2#![allow(clippy::cast_possible_truncation)]
3#![allow(clippy::cast_possible_wrap)]
4#![allow(clippy::cast_precision_loss)]
5#![allow(clippy::cast_sign_loss)]
6#![allow(clippy::cognitive_complexity)]
7#![allow(clippy::doc_markdown)]
8#![allow(clippy::future_not_send)]
9#![allow(clippy::missing_const_for_fn)]
10#![allow(clippy::missing_errors_doc)]
11#![allow(clippy::missing_panics_doc)]
12#![allow(clippy::module_name_repetitions)]
13#![allow(clippy::must_use_candidate)]
14#![allow(clippy::needless_lifetimes)]
15#![allow(clippy::redundant_pub_crate)]
16#![allow(clippy::return_self_not_must_use)]
17#![allow(clippy::similar_names)]
18#![allow(clippy::transmute_ptr_to_ptr)]
19#![allow(clippy::unsafe_derive_deserialize)]
20
21//! Fedimint Core library
22//!
23//! `fedimint-core` contains commonly used types, utilities and primitives,
24//! shared between both client and server code.
25//!
26//! Things that are server-side only typically live in `fedimint-server`, and
27//! client-side only in `fedimint-client`.
28//!
29//! ### Wasm support
30//!
31//! All code in `fedimint-core` needs to compile on Wasm, and `fedimint-core`
32//! includes helpers and wrappers around non-wasm-safe utitlies.
33//!
34//! In particular:
35//!
36//! * [`fedimint_core::task`] for task spawning and control
37//! * [`fedimint_core::time`] for time-related operations
38
39extern crate self as fedimint_core;
40
41use std::fmt::{self, Debug};
42use std::io::Error;
43use std::ops::{self, Range};
44use std::str::FromStr;
45
46pub use amount::*;
47/// Mostly re-exported for [`Decodable`] macros.
48pub use anyhow;
49use bitcoin::address::NetworkUnchecked;
50pub use bitcoin::hashes::Hash as BitcoinHash;
51use bitcoin::{Address, Network};
52use envs::BitcoinRpcConfig;
53use lightning::util::ser::Writeable;
54use lightning_types::features::Bolt11InvoiceFeatures;
55pub use macro_rules_attribute::apply;
56pub use peer_id::*;
57use serde::{Deserialize, Deserializer, Serialize, Serializer};
58use thiserror::Error;
59pub use tiered::Tiered;
60pub use tiered_multi::*;
61use util::SafeUrl;
62pub use {bitcoin, hex, secp256k1};
63
64use crate::encoding::{Decodable, DecodeError, Encodable};
65use crate::module::registry::ModuleDecoderRegistry;
66
67/// Admin (guardian) client types
68pub mod admin_client;
69/// Bitcoin amount types
70mod amount;
71/// Federation-stored client backups
72pub mod backup;
73/// Legacy serde encoding for `bls12_381`
74pub mod bls12_381_serde;
75/// Federation configuration
76pub mod config;
77/// Fundamental types
78pub mod core;
79/// Database handling
80pub mod db;
81/// Consensus encoding
82pub mod encoding;
83pub mod endpoint_constants;
84/// Common environment variables
85pub mod envs;
86pub mod epoch;
87/// Formatting helpers
88pub mod fmt_utils;
89/// Federation invite code
90pub mod invite_code;
91pub mod log;
92/// Common macros
93#[macro_use]
94pub mod macros;
95/// Base 32 encoding
96pub mod base32;
97/// Extendable module sysystem
98pub mod module;
99/// Peer networking
100pub mod net;
101/// `PeerId` type
102mod peer_id;
103/// Runtime (wasm32 vs native) differences handling
104pub mod runtime;
105/// Rustls support
106pub mod rustls;
107/// Peer setup code for setup ceremony
108pub mod setup_code;
109/// Task handling, including wasm safe logic
110pub mod task;
111/// Types handling per-denomination values
112pub mod tiered;
113/// Types handling multiple per-denomination values
114pub mod tiered_multi;
115/// Time handling, wasm safe functionality
116pub mod time;
117/// Timing helpers
118pub mod timing;
119/// Fedimint transaction (inpus + outputs + signature) types
120pub mod transaction;
121/// Peg-in txo proofs
122pub mod txoproof;
123/// General purpose utilities
124pub mod util;
125/// Version
126pub mod version;
127
128/// Atomic BFT unit containing consensus items
129pub mod session_outcome;
130
131// It's necessary to wrap `hash_newtype!` in a module because the generated code
132// references a module called "core", but we export a conflicting module in this
133// file.
134mod txid {
135    use bitcoin::hashes::hash_newtype;
136    use bitcoin::hashes::sha256::Hash as Sha256;
137
138    hash_newtype!(
139        /// A transaction id for peg-ins, peg-outs and reissuances
140        pub struct TransactionId(Sha256);
141    );
142}
143pub use txid::TransactionId;
144
145/// Bitcoin chain identifier
146///
147/// This is a newtype wrapper around [`bitcoin::BlockHash`] representing the
148/// block hash at height 1, which uniquely identifies a Bitcoin chain (mainnet,
149/// testnet, signet, regtest, or custom networks), unlike genesis block hash
150/// which is often the same for same types of networks (e.g. mutinynet vs
151/// signet4).
152///
153/// Using a distinct type instead of raw `BlockHash` provides type safety and
154/// makes the intent clearer when passing chain identifiers through APIs.
155#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
156pub struct ChainId(pub bitcoin::BlockHash);
157
158impl ChainId {
159    /// Create a new `ChainId` from a `BlockHash`
160    pub fn new(block_hash: bitcoin::BlockHash) -> Self {
161        Self(block_hash)
162    }
163
164    /// Get the inner `BlockHash`
165    pub fn block_hash(&self) -> bitcoin::BlockHash {
166        self.0
167    }
168}
169
170impl From<bitcoin::BlockHash> for ChainId {
171    fn from(block_hash: bitcoin::BlockHash) -> Self {
172        Self(block_hash)
173    }
174}
175
176impl From<ChainId> for bitcoin::BlockHash {
177    fn from(chain_id: ChainId) -> Self {
178        chain_id.0
179    }
180}
181
182impl std::fmt::Display for ChainId {
183    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
184        write!(f, "{}", self.0)
185    }
186}
187
188impl FromStr for ChainId {
189    type Err = bitcoin::hashes::hex::HexToArrayError;
190
191    fn from_str(s: &str) -> Result<Self, Self::Err> {
192        bitcoin::BlockHash::from_str(s).map(Self)
193    }
194}
195
196impl Serialize for ChainId {
197    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
198    where
199        S: Serializer,
200    {
201        self.0.serialize(serializer)
202    }
203}
204
205impl<'de> Deserialize<'de> for ChainId {
206    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
207    where
208        D: Deserializer<'de>,
209    {
210        bitcoin::BlockHash::deserialize(deserializer).map(Self)
211    }
212}
213
214/// Amount of bitcoin to send, or `All` to send all available funds
215#[derive(Debug, Eq, PartialEq, Copy, Hash, Clone)]
216pub enum BitcoinAmountOrAll {
217    All,
218    Amount(bitcoin::Amount),
219}
220
221impl std::fmt::Display for BitcoinAmountOrAll {
222    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
223        match self {
224            Self::All => write!(f, "all"),
225            Self::Amount(amount) => write!(f, "{amount}"),
226        }
227    }
228}
229
230impl FromStr for BitcoinAmountOrAll {
231    type Err = anyhow::Error;
232
233    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
234        if s.eq_ignore_ascii_case("all") {
235            Ok(Self::All)
236        } else {
237            let amount = Amount::from_str(s)?;
238            Ok(Self::Amount(amount.try_into()?))
239        }
240    }
241}
242
243// Custom serde to handle both "all" and numbers/strings
244impl<'de> Deserialize<'de> for BitcoinAmountOrAll {
245    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
246    where
247        D: Deserializer<'de>,
248    {
249        use serde::de::Error;
250
251        struct Visitor;
252
253        impl serde::de::Visitor<'_> for Visitor {
254            type Value = BitcoinAmountOrAll;
255
256            fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
257                write!(f, "a bitcoin amount as number or 'all'")
258            }
259
260            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
261            where
262                E: Error,
263            {
264                if v.eq_ignore_ascii_case("all") {
265                    Ok(BitcoinAmountOrAll::All)
266                } else {
267                    let sat: u64 = v.parse().map_err(E::custom)?;
268                    Ok(BitcoinAmountOrAll::Amount(bitcoin::Amount::from_sat(sat)))
269                }
270            }
271
272            fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
273            where
274                E: Error,
275            {
276                Ok(BitcoinAmountOrAll::Amount(bitcoin::Amount::from_sat(v)))
277            }
278
279            fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
280            where
281                E: Error,
282            {
283                if v < 0 {
284                    return Err(E::custom("amount cannot be negative"));
285                }
286                Ok(BitcoinAmountOrAll::Amount(bitcoin::Amount::from_sat(
287                    v as u64,
288                )))
289            }
290        }
291
292        deserializer.deserialize_any(Visitor)
293    }
294}
295
296impl Serialize for BitcoinAmountOrAll {
297    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
298    where
299        S: Serializer,
300    {
301        match self {
302            Self::All => serializer.serialize_str("all"),
303            Self::Amount(a) => serializer.serialize_u64(a.to_sat()),
304        }
305    }
306}
307
308/// `InPoint` represents a globally unique input in a transaction
309///
310/// Hence, a transaction ID and the input index is required.
311#[derive(
312    Debug,
313    Clone,
314    Copy,
315    Eq,
316    PartialEq,
317    PartialOrd,
318    Ord,
319    Hash,
320    Deserialize,
321    Serialize,
322    Encodable,
323    Decodable,
324)]
325pub struct InPoint {
326    /// The referenced transaction ID
327    pub txid: TransactionId,
328    /// As a transaction may have multiple inputs, this refers to the index of
329    /// the input in a transaction
330    pub in_idx: u64,
331}
332
333impl std::fmt::Display for InPoint {
334    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
335        write!(f, "{}:{}", self.txid, self.in_idx)
336    }
337}
338
339/// `OutPoint` represents a globally unique output in a transaction
340///
341/// Hence, a transaction ID and the output index is required.
342#[derive(
343    Debug,
344    Clone,
345    Copy,
346    Eq,
347    PartialEq,
348    PartialOrd,
349    Ord,
350    Hash,
351    Deserialize,
352    Serialize,
353    Encodable,
354    Decodable,
355)]
356pub struct OutPoint {
357    /// The referenced transaction ID
358    pub txid: TransactionId,
359    /// As a transaction may have multiple outputs, this refers to the index of
360    /// the output in a transaction
361    pub out_idx: u64,
362}
363
364impl std::fmt::Display for OutPoint {
365    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
366        write!(f, "{}:{}", self.txid, self.out_idx)
367    }
368}
369
370/// A contiguous range of input/output indexes
371#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Encodable, Decodable)]
372pub struct IdxRange {
373    start: u64,
374    end: u64,
375}
376
377impl IdxRange {
378    pub fn new_single(start: u64) -> Option<Self> {
379        start.checked_add(1).map(|end| Self { start, end })
380    }
381
382    pub fn start(self) -> u64 {
383        self.start
384    }
385
386    pub fn count(self) -> usize {
387        self.into_iter().count()
388    }
389
390    /// Number of indexes in the range, or `None` if the range is descending or
391    /// does not fit into a `usize`.
392    ///
393    /// Ranges are deserialized verbatim from untrusted API requests, so a
394    /// caller validating one has to go through this rather than
395    /// [`Self::count`], which reports a descending range as empty and panics on
396    /// `usize` overflow.
397    pub fn checked_count(self) -> Option<usize> {
398        usize::try_from(self.end.checked_sub(self.start)?).ok()
399    }
400
401    pub fn from_inclusive(range: ops::RangeInclusive<u64>) -> Option<Self> {
402        range.end().checked_add(1).map(|end| Self {
403            start: *range.start(),
404            end,
405        })
406    }
407}
408
409impl From<Range<u64>> for IdxRange {
410    fn from(Range { start, end }: Range<u64>) -> Self {
411        Self { start, end }
412    }
413}
414
415impl IntoIterator for IdxRange {
416    type Item = u64;
417    type IntoIter = ops::Range<u64>;
418
419    fn into_iter(self) -> Self::IntoIter {
420        ops::Range {
421            start: self.start,
422            end: self.end,
423        }
424    }
425}
426
427/// Represents a range of output indices for a single transaction
428#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Encodable, Decodable)]
429pub struct OutPointRange {
430    pub txid: TransactionId,
431    idx_range: IdxRange,
432}
433
434impl OutPointRange {
435    pub fn new(txid: TransactionId, idx_range: IdxRange) -> Self {
436        Self { txid, idx_range }
437    }
438
439    pub fn new_single(txid: TransactionId, idx: u64) -> Option<Self> {
440        IdxRange::new_single(idx).map(|idx_range| Self { txid, idx_range })
441    }
442
443    pub fn start_idx(self) -> u64 {
444        self.idx_range.start()
445    }
446
447    pub fn out_idx_iter(self) -> impl Iterator<Item = u64> {
448        self.idx_range.into_iter()
449    }
450
451    pub fn count(self) -> usize {
452        self.idx_range.count()
453    }
454
455    /// See [`IdxRange::checked_count`].
456    pub fn checked_count(self) -> Option<usize> {
457        self.idx_range.checked_count()
458    }
459
460    pub fn start_out_point(self) -> OutPoint {
461        OutPoint {
462            txid: self.txid,
463            out_idx: self.idx_range.start(),
464        }
465    }
466
467    pub fn end_out_point(self) -> OutPoint {
468        OutPoint {
469            txid: self.txid,
470            out_idx: self.idx_range.end,
471        }
472    }
473
474    pub fn txid(&self) -> TransactionId {
475        self.txid
476    }
477}
478
479impl IntoIterator for OutPointRange {
480    type Item = OutPoint;
481    type IntoIter = OutPointRangeIter;
482
483    fn into_iter(self) -> Self::IntoIter {
484        OutPointRangeIter {
485            txid: self.txid,
486            inner: self.idx_range.into_iter(),
487        }
488    }
489}
490
491pub struct OutPointRangeIter {
492    txid: TransactionId,
493    inner: ops::Range<u64>,
494}
495
496impl Iterator for OutPointRangeIter {
497    type Item = OutPoint;
498
499    fn next(&mut self) -> Option<Self::Item> {
500        self.inner.next().map(|idx| OutPoint {
501            txid: self.txid,
502            out_idx: idx,
503        })
504    }
505}
506
507impl Encodable for TransactionId {
508    fn consensus_encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), Error> {
509        let bytes = &self[..];
510        writer.write_all(bytes)?;
511        Ok(())
512    }
513}
514
515impl Decodable for TransactionId {
516    fn consensus_decode_partial<D: std::io::Read>(
517        d: &mut D,
518        _modules: &ModuleDecoderRegistry,
519    ) -> Result<Self, DecodeError> {
520        let mut bytes = [0u8; 32];
521        d.read_exact(&mut bytes).map_err(DecodeError::from_err)?;
522        Ok(Self::from_byte_array(bytes))
523    }
524}
525
526#[derive(
527    Copy,
528    Clone,
529    Debug,
530    PartialEq,
531    Ord,
532    PartialOrd,
533    Eq,
534    Hash,
535    Serialize,
536    Deserialize,
537    Encodable,
538    Decodable,
539)]
540pub struct Feerate {
541    pub sats_per_kvb: u64,
542}
543
544impl fmt::Display for Feerate {
545    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
546        f.write_fmt(format_args!("{}sat/kvb", self.sats_per_kvb))
547    }
548}
549
550impl Feerate {
551    pub fn calculate_fee(&self, weight: u64) -> bitcoin::Amount {
552        let sats = weight_to_vbytes(weight) * self.sats_per_kvb / 1000;
553        bitcoin::Amount::from_sat(sats)
554    }
555
556    /// Fee for a transaction of the given weight, reproducing the wrapping the
557    /// unchecked multiplication used to do in release profiles.
558    ///
559    /// Consensus behaviour must not depend on the build profile, and
560    /// [`Self::calculate_fee`]'s `*` panics with overflow checks on and wraps
561    /// without them. Sessions ordered before the fee computation was checked
562    /// have to replay as release binaries ran them, so the wrap is explicit.
563    pub fn wrapping_calculate_fee(&self, weight: u64) -> bitcoin::Amount {
564        let sats = weight_to_vbytes(weight).wrapping_mul(self.sats_per_kvb) / 1000;
565        bitcoin::Amount::from_sat(sats)
566    }
567
568    /// Fee for a transaction of the given weight, or `None` if the rate and
569    /// weight do not describe a fee that can exist on chain.
570    ///
571    /// [`Self::calculate_fee`] multiplies unchecked, which wraps to an
572    /// arbitrarily small fee in release profiles. Callers that decide whether
573    /// to accept a transaction must use this instead.
574    pub fn checked_calculate_fee(&self, weight: u64) -> Option<bitcoin::Amount> {
575        let sats = weight_to_vbytes(weight).checked_mul(self.sats_per_kvb)? / 1000;
576        (sats <= bitcoin::Amount::MAX_MONEY.to_sat()).then(|| bitcoin::Amount::from_sat(sats))
577    }
578}
579
580const WITNESS_SCALE_FACTOR: u64 = bitcoin::constants::WITNESS_SCALE_FACTOR as u64;
581
582/// Converts weight to virtual bytes, defined in [BIP-141] as weight / 4
583/// (rounded up to the next integer).
584///
585/// [BIP-141]: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#transaction-size-calculations
586pub fn weight_to_vbytes(weight: u64) -> u64 {
587    weight.div_ceil(WITNESS_SCALE_FACTOR)
588}
589
590#[derive(Debug, Error)]
591pub enum CoreError {
592    #[error("Mismatching outcome variant: expected {0}, got {1}")]
593    MismatchingVariant(&'static str, &'static str),
594}
595
596// Encode features for a bolt11 invoice without encoding the length.
597// This functionality was available in `lightning` v0.0.123, but has since been
598// removed. See the original code here:
599// https://docs.rs/lightning/0.0.123/src/lightning/ln/features.rs.html#745-750
600// https://docs.rs/lightning/0.0.123/src/lightning/ln/features.rs.html#1008-1012
601pub fn encode_bolt11_invoice_features_without_length(features: &Bolt11InvoiceFeatures) -> Vec<u8> {
602    let mut feature_bytes = vec![];
603    for f in features.le_flags().iter().rev() {
604        f.write(&mut feature_bytes)
605            .expect("Writing to byte vec can't fail");
606    }
607    feature_bytes
608}
609
610/// Outputs hex into an object implementing `fmt::Write`.
611///
612/// Vendored from `bitcoin_hashes` v0.11.0:
613/// <https://docs.rs/bitcoin_hashes/0.11.0/src/bitcoin_hashes/hex.rs.html#173-189>
614pub fn format_hex(data: &[u8], f: &mut std::fmt::Formatter) -> std::fmt::Result {
615    let prec = f.precision().unwrap_or(2 * data.len());
616    let width = f.width().unwrap_or(2 * data.len());
617    for _ in (2 * data.len())..width {
618        f.write_str("0")?;
619    }
620    for ch in data.iter().take(prec / 2) {
621        write!(f, "{:02x}", *ch)?;
622    }
623    if prec < 2 * data.len() && prec % 2 == 1 {
624        write!(f, "{:x}", data[prec / 2] / 16)?;
625    }
626    Ok(())
627}
628
629/// Gets the (approximate) network from a bitcoin address.
630///
631/// This function mimics how `Address.network` is calculated in bitcoin v0.30.
632/// However, that field was removed in more recent versions in part because it
633/// can only distinguish between `Bitcoin`, `Testnet` and `Regtest`.
634///
635/// As of bitcoin v0.32.4, `Address::is_valid_for_network()` performs equality
636/// checks using `NetworkKind` and `KnownHrp`, which only distinguish between
637/// `Bitcoin`, `Testnet` and `Regtest`.
638/// <https://docs.rs/bitcoin/0.32.4/src/bitcoin/address/mod.rs.html#709-716>
639/// <https://docs.rs/bitcoin/0.32.4/src/bitcoin/network.rs.html#51-58>
640/// <https://docs.rs/bitcoin/0.32.4/src/bitcoin/address/mod.rs.html#200-209>
641pub fn get_network_for_address(address: &Address<NetworkUnchecked>) -> Network {
642    if address.is_valid_for_network(Network::Bitcoin) {
643        Network::Bitcoin
644    } else if address.is_valid_for_network(Network::Testnet) {
645        Network::Testnet
646    } else if address.is_valid_for_network(Network::Regtest) {
647        Network::Regtest
648    } else {
649        panic!("Address is not valid for any network");
650    }
651}
652
653/// Returns the default esplora server according to the network
654pub fn default_esplora_server(network: Network, port: Option<String>) -> BitcoinRpcConfig {
655    BitcoinRpcConfig {
656        kind: "esplora".to_string(),
657        url: match network {
658            Network::Bitcoin => SafeUrl::parse("https://mempool.space/api/"),
659            Network::Testnet => SafeUrl::parse("https://mempool.space/testnet/api/"),
660            Network::Testnet4 => SafeUrl::parse("https://mempool.space/testnet4/api/"),
661            Network::Signet => SafeUrl::parse("https://mutinynet.com/api/"),
662            Network::Regtest => SafeUrl::parse(&format!(
663                "http://127.0.0.1:{}/",
664                port.unwrap_or_else(|| String::from("50002"))
665            )),
666        }
667        .expect("Failed to parse default esplora server"),
668    }
669}
670
671#[cfg(test)]
672mod tests;