Skip to content

Commit

Permalink
Merge pull request #3018 from jkczyz/2024-04-optional-description
Browse files Browse the repository at this point in the history
Optional description for `Offer` and `Refund`
  • Loading branch information
TheBlueMatt committed May 3, 2024
2 parents 3d5a691 + e61001f commit a27088d
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 256 deletions.
19 changes: 9 additions & 10 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1549,11 +1549,12 @@ where
/// # fn example<T: AChannelManager>(channel_manager: T) -> Result<(), Bolt12SemanticError> {
/// # let channel_manager = channel_manager.get_cm();
/// let offer = channel_manager
/// .create_offer_builder("coffee".to_string())?
/// .create_offer_builder()?
/// # ;
/// # // Needed for compiling for c_bindings
/// # let builder: lightning::offers::offer::OfferBuilder<_, _> = offer.into();
/// # let offer = builder
/// .description("coffee".to_string())
/// .amount_msats(10_000_000)
/// .build()?;
/// let bech32_offer = offer.to_string();
Expand Down Expand Up @@ -1652,13 +1653,13 @@ where
/// let payment_id = PaymentId([42; 32]);
/// let refund = channel_manager
/// .create_refund_builder(
/// "coffee".to_string(), amount_msats, absolute_expiry, payment_id, retry,
/// max_total_routing_fee_msat
/// amount_msats, absolute_expiry, payment_id, retry, max_total_routing_fee_msat
/// )?
/// # ;
/// # // Needed for compiling for c_bindings
/// # let builder: lightning::offers::refund::RefundBuilder<_> = refund.into();
/// # let refund = builder
/// .description("coffee".to_string())
/// .payer_note("refund for order 1234".to_string())
/// .build()?;
/// let bech32_refund = refund.to_string();
Expand Down Expand Up @@ -8562,17 +8563,15 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
///
/// [`Offer`]: crate::offers::offer::Offer
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
pub fn create_offer_builder(
&$self, description: String
) -> Result<$builder, Bolt12SemanticError> {
pub fn create_offer_builder(&$self) -> Result<$builder, 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
node_id, expanded_key, entropy, secp_ctx
)
.chain_hash($self.chain_hash)
.path(path);
Expand Down Expand Up @@ -8631,8 +8630,8 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
/// [`Bolt12Invoice::payment_paths`]: crate::offers::invoice::Bolt12Invoice::payment_paths
/// [Avoiding Duplicate Payments]: #avoiding-duplicate-payments
pub fn create_refund_builder(
&$self, description: String, amount_msats: u64, absolute_expiry: Duration,
payment_id: PaymentId, retry_strategy: Retry, max_total_routing_fee_msat: Option<u64>
&$self, amount_msats: u64, absolute_expiry: Duration, payment_id: PaymentId,
retry_strategy: Retry, max_total_routing_fee_msat: Option<u64>
) -> Result<$builder, Bolt12SemanticError> {
let node_id = $self.get_our_node_id();
let expanded_key = &$self.inbound_payment_key;
Expand All @@ -8641,7 +8640,7 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {

let path = $self.create_blinded_path().map_err(|_| Bolt12SemanticError::MissingPaths)?;
let builder = RefundBuilder::deriving_payer_id(
description, node_id, expanded_key, entropy, secp_ctx, amount_msats, payment_id
node_id, expanded_key, entropy, secp_ctx, amount_msats, payment_id
)?
.chain_hash($self.chain_hash)
.absolute_expiry(absolute_expiry)
Expand Down
52 changes: 20 additions & 32 deletions lightning/src/ln/offers_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor.clone());

let offer = bob.node
.create_offer_builder("coffee".to_string()).unwrap()
.create_offer_builder().unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), Some(bob_id));
Expand All @@ -282,7 +282,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
announce_node_address(&nodes[5], &[alice, bob, charlie, david, &nodes[4]], tor.clone());

let offer = bob.node
.create_offer_builder("coffee".to_string()).unwrap()
.create_offer_builder().unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), Some(bob_id));
Expand Down Expand Up @@ -332,7 +332,7 @@ fn prefers_more_connected_nodes_in_blinded_paths() {
disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);

let offer = bob.node
.create_offer_builder("coffee".to_string()).unwrap()
.create_offer_builder().unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), Some(bob_id));
Expand Down Expand Up @@ -381,7 +381,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);

let offer = alice.node
.create_offer_builder("coffee".to_string())
.create_offer_builder()
.unwrap()
.amount_msats(10_000_000)
.build().unwrap();
Expand Down Expand Up @@ -481,9 +481,7 @@ fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
let absolute_expiry = Duration::from_secs(u64::MAX);
let payment_id = PaymentId([1; 32]);
let refund = david.node
.create_refund_builder(
"refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
)
.create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
.unwrap()
.build().unwrap();
assert_eq!(refund.amount_msats(), 10_000_000);
Expand Down Expand Up @@ -541,7 +539,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
let bob_id = bob.node.get_our_node_id();

let offer = alice.node
.create_offer_builder("coffee".to_string()).unwrap()
.create_offer_builder().unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), Some(alice_id));
Expand Down Expand Up @@ -608,9 +606,7 @@ fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
let absolute_expiry = Duration::from_secs(u64::MAX);
let payment_id = PaymentId([1; 32]);
let refund = bob.node
.create_refund_builder(
"refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
)
.create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
.unwrap()
.build().unwrap();
assert_eq!(refund.amount_msats(), 10_000_000);
Expand Down Expand Up @@ -663,7 +659,7 @@ fn pays_for_offer_without_blinded_paths() {
let bob_id = bob.node.get_our_node_id();

let offer = alice.node
.create_offer_builder("coffee".to_string()).unwrap()
.create_offer_builder().unwrap()
.clear_paths()
.amount_msats(10_000_000)
.build().unwrap();
Expand Down Expand Up @@ -717,9 +713,7 @@ fn pays_for_refund_without_blinded_paths() {
let absolute_expiry = Duration::from_secs(u64::MAX);
let payment_id = PaymentId([1; 32]);
let refund = bob.node
.create_refund_builder(
"refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
)
.create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
.unwrap()
.clear_paths()
.build().unwrap();
Expand Down Expand Up @@ -753,7 +747,7 @@ fn fails_creating_offer_without_blinded_paths() {

create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);

match nodes[0].node.create_offer_builder("coffee".to_string()) {
match nodes[0].node.create_offer_builder() {
Ok(_) => panic!("Expected error"),
Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
}
Expand All @@ -773,7 +767,7 @@ fn fails_creating_refund_without_blinded_paths() {
let payment_id = PaymentId([1; 32]);

match nodes[0].node.create_refund_builder(
"refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
) {
Ok(_) => panic!("Expected error"),
Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
Expand All @@ -796,7 +790,7 @@ fn fails_creating_invoice_request_for_unsupported_chain() {
let bob = &nodes[1];

let offer = alice.node
.create_offer_builder("coffee".to_string()).unwrap()
.create_offer_builder().unwrap()
.clear_chains()
.chain(Network::Signet)
.build().unwrap();
Expand Down Expand Up @@ -824,9 +818,7 @@ fn fails_sending_invoice_with_unsupported_chain_for_refund() {
let absolute_expiry = Duration::from_secs(u64::MAX);
let payment_id = PaymentId([1; 32]);
let refund = bob.node
.create_refund_builder(
"refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
)
.create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
.unwrap()
.chain(Network::Signet)
.build().unwrap();
Expand Down Expand Up @@ -858,7 +850,7 @@ fn fails_creating_invoice_request_without_blinded_reply_path() {
disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);

let offer = alice.node
.create_offer_builder("coffee".to_string()).unwrap()
.create_offer_builder().unwrap()
.amount_msats(10_000_000)
.build().unwrap();

Expand Down Expand Up @@ -892,7 +884,7 @@ fn fails_creating_invoice_request_with_duplicate_payment_id() {
disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);

let offer = alice.node
.create_offer_builder("coffee".to_string()).unwrap()
.create_offer_builder().unwrap()
.amount_msats(10_000_000)
.build().unwrap();

Expand Down Expand Up @@ -925,13 +917,13 @@ fn fails_creating_refund_with_duplicate_payment_id() {
let payment_id = PaymentId([1; 32]);
assert!(
nodes[0].node.create_refund_builder(
"refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
).is_ok()
);
expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);

match nodes[0].node.create_refund_builder(
"refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
) {
Ok(_) => panic!("Expected error"),
Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
Expand Down Expand Up @@ -978,7 +970,7 @@ fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);

let offer = alice.node
.create_offer_builder("coffee".to_string()).unwrap()
.create_offer_builder().unwrap()
.amount_msats(10_000_000)
.build().unwrap();

Expand Down Expand Up @@ -1042,9 +1034,7 @@ fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
let absolute_expiry = Duration::from_secs(u64::MAX);
let payment_id = PaymentId([1; 32]);
let refund = david.node
.create_refund_builder(
"refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
)
.create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
.unwrap()
.build().unwrap();

Expand Down Expand Up @@ -1093,9 +1083,7 @@ fn fails_paying_invoice_more_than_once() {
let absolute_expiry = Duration::from_secs(u64::MAX);
let payment_id = PaymentId([1; 32]);
let refund = david.node
.create_refund_builder(
"refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
)
.create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
.unwrap()
.build().unwrap();
expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/ln/outbound_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2194,7 +2194,7 @@ mod tests {
assert!(outbound_payments.has_pending_payments());

let created_at = now() - DEFAULT_RELATIVE_EXPIRY;
let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
let invoice = OfferBuilder::new(recipient_pubkey())
.amount_msats(1000)
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
Expand Down Expand Up @@ -2237,7 +2237,7 @@ mod tests {
let payment_id = PaymentId([0; 32]);
let expiration = StaleExpiration::AbsoluteTimeout(Duration::from_secs(100));

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
let invoice = OfferBuilder::new(recipient_pubkey())
.amount_msats(1000)
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
Expand Down Expand Up @@ -2296,7 +2296,7 @@ mod tests {
let payment_id = PaymentId([0; 32]);
let expiration = StaleExpiration::AbsoluteTimeout(Duration::from_secs(100));

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
let invoice = OfferBuilder::new(recipient_pubkey())
.amount_msats(1000)
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
Expand Down

0 comments on commit a27088d

Please sign in to comment.