Skip to content

Commit edc9441

Browse files
committed
Expand invoice module docs and include an example
1 parent 7857ceb commit edc9441

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

lightning/src/offers/invoice.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,78 @@
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 = bitcoin::util::key::PublicKey::new(PublicKey::from(keys));
43+
//! let mut buffer = Vec::new();
44+
//!
45+
//! // Invoice for the "offer to be paid" flow.
46+
//! InvoiceRequest::try_from(bytes)?
47+
//! .respond_with(payment_paths, payment_hash)?
48+
//! .relative_expiry(3600)
49+
//! .allow_mpp()
50+
//! .fallback_v0_p2wpkh(&pubkey.wpubkey_hash().unwrap())
51+
//! .build()?
52+
//! .sign::<_, Infallible>(|digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys)))
53+
//! .expect("failed verifying signature")
54+
//! .write(&mut buffer)
55+
//! .unwrap();
56+
//! # Ok(())
57+
//! # }
58+
//!
59+
//! # fn parse_refund(bytes: Vec<u8>) -> Result<(), lightning::offers::parse::ParseError> {
60+
//! # let payment_paths = create_payment_paths();
61+
//! # let payment_hash = create_payment_hash();
62+
//! # let secp_ctx = Secp256k1::new();
63+
//! # let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32])?);
64+
//! # let pubkey = bitcoin::util::key::PublicKey::new(PublicKey::from(keys));
65+
//! # let mut buffer = Vec::new();
66+
//!
67+
//! // Invoice for the "offer for money" flow.
68+
//! "lnr1qcp4256ypq"
69+
//! .parse::<Refund>()?
70+
//! .respond_with(payment_paths, payment_hash, pubkey.inner)?
71+
//! .relative_expiry(3600)
72+
//! .allow_mpp()
73+
//! .fallback_v0_p2wpkh(&pubkey.wpubkey_hash().unwrap())
74+
//! .build()?
75+
//! .sign::<_, Infallible>(|digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys)))
76+
//! .expect("failed verifying signature")
77+
//! .write(&mut buffer)
78+
//! .unwrap();
79+
//! # Ok(())
80+
//! # }
81+
//!
82+
//! ```
1183
1284
use bitcoin::blockdata::constants::ChainHash;
1385
use bitcoin::hash_types::{WPubkeyHash, WScriptHash};

0 commit comments

Comments
 (0)