pub struct InvoiceBuilder<D: Bool, H: Bool, T: Bool, C: Bool, S: Bool> { /* private fields */ }
Expand description

Builder for Invoices. It’s the most convenient and advised way to use this library. It ensures that only a semantically and syntactically correct Invoice can be built using it.

extern crate secp256k1;
extern crate lightning;
extern crate lightning_invoice;
extern crate bitcoin_hashes;

use bitcoin_hashes::Hash;
use bitcoin_hashes::sha256;

use secp256k1::Secp256k1;
use secp256k1::SecretKey;

use lightning::ln::PaymentSecret;

use lightning_invoice::{Currency, InvoiceBuilder};

let private_key = SecretKey::from_slice(
	&[
		0xe1, 0x26, 0xf6, 0x8f, 0x7e, 0xaf, 0xcc, 0x8b, 0x74, 0xf5, 0x4d, 0x26, 0x9f,
		0xe2, 0x06, 0xbe, 0x71, 0x50, 0x00, 0xf9, 0x4d, 0xac, 0x06, 0x7d, 0x1c, 0x04,
		0xa8, 0xca, 0x3b, 0x2d, 0xb7, 0x34
	][..]
).unwrap();

let payment_hash = sha256::Hash::from_slice(&[0; 32][..]).unwrap();
let payment_secret = PaymentSecret([42u8; 32]);

let invoice = InvoiceBuilder::new(Currency::Bitcoin)
	.description("Coins pls!".into())
	.payment_hash(payment_hash)
	.payment_secret(payment_secret)
	.current_timestamp()
	.min_final_cltv_expiry(144)
	.build_signed(|hash| {
		Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)
	})
	.unwrap();

assert!(invoice.to_string().starts_with("lnbc1"));

Type parameters

The two parameters D and H signal if the builder already contains the correct amount of the given field:

  • D: exactly one Description or DescriptionHash
  • H: exactly one PaymentHash
  • T: the timestamp is set

(C-not exported) as we likely need to manually select one set of boolean type parameters.

Implementations

Construct new, empty InvoiceBuilder. All necessary fields have to be filled first before InvoiceBuilder::build(self) becomes available.

Sets the amount in millisatoshis. The optimal SI prefix is chosen automatically.

Sets the payee’s public key.

Sets the expiry time

Adds a fallback address.

Adds a private route.

Builds a RawInvoice if no CreationError occurred while construction any of the fields.

Set the description. This function is only available if no description (hash) was set.

Set the description hash. This function is only available if no description (hash) was set.

Set the payment hash. This function is only available if no payment hash was set.

Available on crate feature std only.

Sets the timestamp to a specific SystemTime.

Sets the timestamp to a duration since the Unix epoch.

Available on crate feature std only.

Sets the timestamp to the current system time.

Sets min_final_cltv_expiry.

Sets the payment secret and relevant features.

Sets the basic_mpp feature as optional.

Builds and signs an invoice using the supplied sign_function. This function MAY NOT fail and MUST produce a recoverable signature valid for the given hash and if applicable also for the included payee public key.

Builds and signs an invoice using the supplied sign_function. This function MAY fail with an error of type E and MUST produce a recoverable signature valid for the given hash and if applicable also for the included payee public key.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.