Skip to content

Commit fe83aed

Browse files
committed
Expand invoice module docs and include an example
1 parent 7f52d26 commit fe83aed

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

lightning/src/offers/invoice.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,80 @@
88
// licenses.
99

1010
//! Data structures and encoding for `invoice` messages.
11+
//!
12+
//! An [`Invoice`] can be built from a parsed [`InvoiceRequest`] for the "offer to be paid" flow or
13+
//! from a [`Refund`] as an "offer for money" flow. The expected recipient of the payment then sends
14+
//! the invoice to the intended payer, who will then pay it.
15+
//!
16+
//! The payment recipient must include a [`PaymentHash`], so as to reveal the preimage upon payment
17+
//! receipt, and one or more [`BlindedPath`]s for the payer to use when sending the payment.
18+
//!
19+
//! ```ignore
20+
//! extern crate bitcoin;
21+
//! extern crate lightning;
22+
//!
23+
//! use bitcoin::hashes::Hash;
24+
//! use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey};
25+
//! use core::convert::{Infallible, TryFrom};
26+
//! use lightning::offers::invoice_request::InvoiceRequest;
27+
//! use lightning::offers::refund::Refund;
28+
//! use lightning::util::ser::Writeable;
29+
//!
30+
//! # use lightning::ln::PaymentHash;
31+
//! # use lightning::offers::invoice::BlindedPayInfo;
32+
//! # use lightning::onion_message::BlindedPath;
33+
//! #
34+
//! # fn create_payment_paths() -> Vec<(BlindedPath, BlindedPayInfo)> { unimplemented!() }
35+
//! # fn create_payment_hash() -> PaymentHash { unimplemented!() }
36+
//! #
37+
//! # fn parse_invoice_request(bytes: Vec<u8>) -> Result<(), lightning::offers::parse::ParseError> {
38+
//! let payment_paths = create_payment_paths();
39+
//! let payment_hash = create_payment_hash();
40+
//! let secp_ctx = Secp256k1::new();
41+
//! let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32])?);
42+
//! let pubkey = PublicKey::from(keys);
43+
//! let wpubkey_hash = bitcoin::util::key::PublicKey::new(pubkey).wpubkey_hash().unwrap();
44+
//! let mut buffer = Vec::new();
45+
//!
46+
//! // Invoice for the "offer to be paid" flow.
47+
//! InvoiceRequest::try_from(bytes)?
48+
//! .respond_with(payment_paths, payment_hash)?
49+
//! .relative_expiry(3600)
50+
//! .allow_mpp()
51+
//! .fallback_v0_p2wpkh(&wpubkey_hash)
52+
//! .build()?
53+
//! .sign::<_, Infallible>(|digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys)))
54+
//! .expect("failed verifying signature")
55+
//! .write(&mut buffer)
56+
//! .unwrap();
57+
//! # Ok(())
58+
//! # }
59+
//!
60+
//! # fn parse_refund(bytes: Vec<u8>) -> Result<(), lightning::offers::parse::ParseError> {
61+
//! # let payment_paths = create_payment_paths();
62+
//! # let payment_hash = create_payment_hash();
63+
//! # let secp_ctx = Secp256k1::new();
64+
//! # let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32])?);
65+
//! # let pubkey = PublicKey::from(keys);
66+
//! # let wpubkey_hash = bitcoin::util::key::PublicKey::new(pubkey).wpubkey_hash().unwrap();
67+
//! # let mut buffer = Vec::new();
68+
//!
69+
//! // Invoice for the "offer for money" flow.
70+
//! "lnr1qcp4256ypq"
71+
//! .parse::<Refund>()?
72+
//! .respond_with(payment_paths, payment_hash, pubkey)?
73+
//! .relative_expiry(3600)
74+
//! .allow_mpp()
75+
//! .fallback_v0_p2wpkh(&wpubkey_hash)
76+
//! .build()?
77+
//! .sign::<_, Infallible>(|digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys)))
78+
//! .expect("failed verifying signature")
79+
//! .write(&mut buffer)
80+
//! .unwrap();
81+
//! # Ok(())
82+
//! # }
83+
//!
84+
//! ```
1185
1286
use bitcoin::blockdata::constants::ChainHash;
1387
use bitcoin::hash_types::{WPubkeyHash, WScriptHash};

0 commit comments

Comments
 (0)