Skip to content

Commit 5fea57e

Browse files
committed
Introduce CurrencyConversion trait
Adds the `CurrencyConversion` trait to allow users to define custom logic for converting fiat amounts into millisatoshis (msat). This abstraction lays the groundwork for supporting Offers denominated in fiat currencies, where conversion is inherently context-dependent.
1 parent 61e5819 commit 5fea57e

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

lightning/src/offers/invoice_request.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ use crate::offers::merkle::{
7676
};
7777
use crate::offers::nonce::Nonce;
7878
use crate::offers::offer::{
79-
Amount, ExperimentalOfferTlvStream, ExperimentalOfferTlvStreamRef, Offer, OfferContents,
80-
OfferId, OfferTlvStream, OfferTlvStreamRef, EXPERIMENTAL_OFFER_TYPES, OFFER_TYPES,
79+
Amount, CurrencyCode, ExperimentalOfferTlvStream, ExperimentalOfferTlvStreamRef, Offer,
80+
OfferContents, OfferId, OfferTlvStream, OfferTlvStreamRef, EXPERIMENTAL_OFFER_TYPES,
81+
OFFER_TYPES,
8182
};
8283
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
8384
use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
@@ -94,6 +95,7 @@ use bitcoin::constants::ChainHash;
9495
use bitcoin::network::Network;
9596
use bitcoin::secp256k1::schnorr::Signature;
9697
use bitcoin::secp256k1::{self, Keypair, PublicKey, Secp256k1};
98+
use core::ops::Deref;
9799

98100
#[cfg(not(c_bindings))]
99101
use crate::offers::invoice::{DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder};
@@ -573,6 +575,35 @@ impl AsRef<TaggedHash> for UnsignedInvoiceRequest {
573575
}
574576
}
575577

578+
/// A trait for converting fiat currencies into millisatoshi values.
579+
///
580+
/// This is used for handling conversions between fiat currencies and Bitcoin denominated in millisatoshis
581+
/// when working with Bolt12 invoice requests.
582+
///
583+
/// Implementors must provide a method to convert from a specified fiat currency (using ISO 4217 currency codes)
584+
/// to millisatoshis, handling any potential conversion errors.
585+
pub trait CurrencyConversion {
586+
/// Converts a fiat currency specified by its ISO 4217 code to millisatoshis.
587+
fn fiat_to_msats(&self, iso4217_code: CurrencyCode) -> Result<u64, Bolt12SemanticError>;
588+
}
589+
590+
/// A default implementation of the `CurrencyConversion` trait that does not support any currency conversions.
591+
pub struct DefaultCurrencyConversion {}
592+
593+
impl Deref for DefaultCurrencyConversion {
594+
type Target = Self;
595+
596+
fn deref(&self) -> &Self::Target {
597+
self
598+
}
599+
}
600+
601+
impl CurrencyConversion for DefaultCurrencyConversion {
602+
fn fiat_to_msats(&self, _iso4217_code: CurrencyCode) -> Result<u64, Bolt12SemanticError> {
603+
Err(Bolt12SemanticError::UnsupportedCurrency)
604+
}
605+
}
606+
576607
/// An `InvoiceRequest` is a request for a [`Bolt12Invoice`] formulated from an [`Offer`].
577608
///
578609
/// An offer may provide choices such as quantity, amount, chain, features, etc. An invoice request

0 commit comments

Comments
 (0)