Skip to content

Commit

Permalink
OfferBuilder for c_bindings
Browse files Browse the repository at this point in the history
OfferBuilder is currently not exported to bindings because it uses move
semantics and has impl blocks for specific type parameterizations.
Define a macro that defines OfferBuilder methods such that c_bindings
versions can be defined.
  • Loading branch information
jkczyz committed Feb 20, 2024
1 parent 51d9ee3 commit 795a396
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 74 deletions.
1 change: 1 addition & 0 deletions lightning/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ default = ["std", "grind_signatures"]

[dependencies]
bitcoin = { version = "0.30.2", default-features = false, features = ["secp-recovery"] }
secp256k1 = { version = "0.27", features = ["global-context", "recovery"] }

hashbrown = { version = "0.8", optional = true }
hex = { package = "hex-conservative", version = "0.1.1", default-features = false }
Expand Down
36 changes: 27 additions & 9 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use crate::ln::wire::Encode;
use crate::offers::invoice::{BlindedPayInfo, Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, InvoiceBuilder};
use crate::offers::invoice_error::InvoiceError;
use crate::offers::merkle::SignError;
use crate::offers::offer::{DerivedMetadata, Offer, OfferBuilder};
use crate::offers::offer::Offer;
use crate::offers::parse::Bolt12SemanticError;
use crate::offers::refund::{Refund, RefundBuilder};
use crate::onion_message::messenger::{Destination, MessageRouter, PendingOnionMessage, new_pending_onion_message};
Expand Down Expand Up @@ -98,6 +98,12 @@ use core::ops::Deref;
pub use crate::ln::outbound_payment::{PaymentSendFailure, ProbeSendFailure, Retry, RetryableSendFailure, RecipientOnionFields};
use crate::ln::script::ShutdownScript;

#[cfg(not(c_bindings))]
type OfferBuilder<'a> =
crate::offers::offer::OfferBuilder<'a, crate::offers::offer::DerivedMetadata, secp256k1::All>;
#[cfg(c_bindings)]
type OfferBuilder<'a> = crate::offers::offer::OfferWithDerivedMetadataBuilder;

// We hold various information about HTLC relay in the HTLC objects in Channel itself:
//
// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
Expand Down Expand Up @@ -7550,20 +7556,32 @@ where
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
pub fn create_offer_builder(
&self, description: String
) -> Result<OfferBuilder<DerivedMetadata, secp256k1::All>, Bolt12SemanticError> {
) -> Result<OfferBuilder, Bolt12SemanticError> {
let node_id = self.get_our_node_id();
let expanded_key = &self.inbound_payment_key;
let entropy = &*self.entropy_source;
let secp_ctx = &self.secp_ctx;

let path = self.create_blinded_path().map_err(|_| Bolt12SemanticError::MissingPaths)?;
let builder = OfferBuilder::deriving_signing_pubkey(
description, node_id, expanded_key, entropy, secp_ctx
)
.chain_hash(self.chain_hash)
.path(path);

Ok(builder)
#[cfg(not(c_bindings))] {
let builder = OfferBuilder::deriving_signing_pubkey(
description, node_id, expanded_key, entropy, &self.secp_ctx
)
.chain_hash(self.chain_hash)
.path(path);

Ok(builder)
}

#[cfg(c_bindings)] {
let mut builder = OfferBuilder::deriving_signing_pubkey(
description, node_id, expanded_key, entropy, &self.secp_ctx
);
builder.chain_hash(self.chain_hash);
builder.path(path);

Ok(builder)
}
}

/// Creates a [`RefundBuilder`] such that the [`Refund`] it builds is recognized by the
Expand Down
2 changes: 2 additions & 0 deletions lightning/src/offers/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,8 @@ mod tests {
],
};

#[cfg(c_bindings)]
use crate::offers::offer::OfferWithDerivedMetadataBuilder as OfferBuilder;
let offer = OfferBuilder
::deriving_signing_pubkey(desc, node_id, &expanded_key, &entropy, &secp_ctx)
.amount_msats(1000)
Expand Down
Loading

0 comments on commit 795a396

Please sign in to comment.