- let secp_ctx = Secp256k1::new();
-
- let mut preimage = [0; 32];
- rand::thread_rng().fill_bytes(&mut preimage);
- let payment_hash = Sha256Hash::hash(&preimage);
-
- let our_node_pubkey = channel_manager.get_our_node_id();
- let mut invoice = lightning_invoice::InvoiceBuilder::new(match network {
- Network::Bitcoin => lightning_invoice::Currency::Bitcoin,
- Network::Testnet => lightning_invoice::Currency::BitcoinTestnet,
- Network::Regtest => lightning_invoice::Currency::Regtest,
- Network::Signet => panic!("Signet invoices not supported"),
- })
- .payment_hash(payment_hash)
- .description("rust-lightning-bitcoinrpc invoice".to_string())
- .amount_pico_btc(amt_sat * 10_000)
- .current_timestamp()
- .payee_pub_key(our_node_pubkey);
-
- // Add route hints to the invoice.
- let our_channels = channel_manager.list_usable_channels();
- for channel in our_channels {
- let short_channel_id = match channel.short_channel_id {
- Some(id) => id.to_be_bytes(),
- None => continue,
- };
- let forwarding_info = match channel.counterparty_forwarding_info {
- Some(info) => info,
- None => continue,
- };
- println!("VMW: adding routehop, info.fee base: {}", forwarding_info.fee_base_msat);
- invoice = invoice.route(vec![lightning_invoice::RouteHop {
- pubkey: channel.remote_network_id,
- short_channel_id,
- fee_base_msat: forwarding_info.fee_base_msat,
- fee_proportional_millionths: forwarding_info.fee_proportional_millionths,
- cltv_expiry_delta: forwarding_info.cltv_expiry_delta,
- }]);
- }
-
- // Sign the invoice.
- let invoice =
- invoice.build_signed(|msg_hash| secp_ctx.sign_recoverable(msg_hash, &our_node_privkey));
-
- match invoice {
- Ok(invoice) => println!("SUCCESS: generated invoice: {}", invoice),
- Err(e) => println!("ERROR: failed to create invoice: {:?}", e),
- }
+ let currency = match network {
+ Network::Bitcoin => Currency::Bitcoin,
+ Network::Testnet => Currency::BitcoinTestnet,
+ Network::Regtest => Currency::Regtest,
+ Network::Signet => panic!("Signet unsupported"),
+ };
+ let invoice = match utils::create_invoice_from_channelmanager(
+ &channel_manager,
+ keys_manager,
+ currency,
+ Some(amt_msat),
+ "ldk-tutorial-node".to_string(),
+ ) {
+ Ok(inv) => {
+ println!("SUCCESS: generated invoice: {}", inv);
+ inv
+ }
+ Err(e) => {
+ println!("ERROR: failed to create invoice: {:?}", e);
+ return;
+ }
+ };