X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Finbound_payment.rs;h=0c80ae7bb4ad32b55af832eb3f1e2911ec457fb6;hb=50d12600b43c0c4a65547c6d37fca3efce0a12f2;hp=f7fbb48985ef07492a5ce129e95aeec739643c92;hpb=847f26013f0e2904f761bd7a3231540f30824039;p=rust-lightning diff --git a/lightning/src/ln/inbound_payment.rs b/lightning/src/ln/inbound_payment.rs index f7fbb489..0c80ae7b 100644 --- a/lightning/src/ln/inbound_payment.rs +++ b/lightning/src/ln/inbound_payment.rs @@ -14,14 +14,14 @@ use bitcoin::hashes::{Hash, HashEngine}; use bitcoin::hashes::cmp::fixed_time_eq; use bitcoin::hashes::hmac::{Hmac, HmacEngine}; use bitcoin::hashes::sha256::Hash as Sha256; -use chain::keysinterface::{KeyMaterial, KeysInterface, Sign}; -use ln::{PaymentHash, PaymentPreimage, PaymentSecret}; -use ln::msgs; -use ln::msgs::MAX_VALUE_MSAT; -use util::chacha20::ChaCha20; -use util::crypto::hkdf_extract_expand_thrice; -use util::errors::APIError; -use util::logger::Logger; +use crate::chain::keysinterface::{KeyMaterial, EntropySource}; +use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret}; +use crate::ln::msgs; +use crate::ln::msgs::MAX_VALUE_MSAT; +use crate::util::chacha20::ChaCha20; +use crate::util::crypto::hkdf_extract_expand_thrice; +use crate::util::errors::APIError; +use crate::util::logger::Logger; use core::convert::TryInto; use core::ops::Deref; @@ -35,10 +35,10 @@ const AMT_MSAT_LEN: usize = 8; const METHOD_TYPE_OFFSET: usize = 5; /// A set of keys that were HKDF-expanded from an initial call to -/// [`KeysInterface::get_inbound_payment_key_material`]. +/// [`NodeSigner::get_inbound_payment_key_material`]. /// -/// [`KeysInterface::get_inbound_payment_key_material`]: crate::chain::keysinterface::KeysInterface::get_inbound_payment_key_material -pub(super) struct ExpandedKey { +/// [`NodeSigner::get_inbound_payment_key_material`]: crate::chain::keysinterface::NodeSigner::get_inbound_payment_key_material +pub struct ExpandedKey { /// The key used to encrypt the bytes containing the payment metadata (i.e. the amount and /// expiry, included for payment verification on decryption). metadata_key: [u8; 32], @@ -51,7 +51,10 @@ pub(super) struct ExpandedKey { } impl ExpandedKey { - pub(super) fn new(key_material: &KeyMaterial) -> ExpandedKey { + /// Create a new [`ExpandedKey`] for generating an inbound payment hash and secret. + /// + /// It is recommended to cache this value and not regenerate it for each new inbound payment. + pub fn new(key_material: &KeyMaterial) -> ExpandedKey { let (metadata_key, ldk_pmt_hash_key, user_pmt_hash_key) = hkdf_extract_expand_thrice(b"LDK Inbound Payment Key Expansion", &key_material.0); Self { @@ -77,13 +80,25 @@ impl Method { } } -pub(super) fn create(keys: &ExpandedKey, min_value_msat: Option, invoice_expiry_delta_secs: u32, keys_manager: &K, highest_seen_timestamp: u64) -> Result<(PaymentHash, PaymentSecret), ()> - where K::Target: KeysInterface +/// Equivalent to [`crate::ln::channelmanager::ChannelManager::create_inbound_payment`], but no +/// `ChannelManager` is required. Useful for generating invoices for [phantom node payments] without +/// a `ChannelManager`. +/// +/// `keys` is generated by calling [`NodeSigner::get_inbound_payment_key_material`] and then +/// calling [`ExpandedKey::new`] with its result. It is recommended to cache this value and not +/// regenerate it for each new inbound payment. +/// +/// `current_time` is a Unix timestamp representing the current time. +/// +/// [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager +/// [`NodeSigner::get_inbound_payment_key_material`]: crate::chain::keysinterface::NodeSigner::get_inbound_payment_key_material +pub fn create(keys: &ExpandedKey, min_value_msat: Option, invoice_expiry_delta_secs: u32, entropy_source: &ES, current_time: u64) -> Result<(PaymentHash, PaymentSecret), ()> + where ES::Target: EntropySource { - let metadata_bytes = construct_metadata_bytes(min_value_msat, Method::LdkPaymentHash, invoice_expiry_delta_secs, highest_seen_timestamp)?; + let metadata_bytes = construct_metadata_bytes(min_value_msat, Method::LdkPaymentHash, invoice_expiry_delta_secs, current_time)?; let mut iv_bytes = [0 as u8; IV_LEN]; - let rand_bytes = keys_manager.get_secure_random_bytes(); + let rand_bytes = entropy_source.get_secure_random_bytes(); iv_bytes.copy_from_slice(&rand_bytes[..IV_LEN]); let mut hmac = HmacEngine::::new(&keys.ldk_pmt_hash_key); @@ -96,8 +111,15 @@ pub(super) fn create(keys: &ExpandedKey, min_value_msat: Ok((ldk_pmt_hash, payment_secret)) } -pub(super) fn create_from_hash(keys: &ExpandedKey, min_value_msat: Option, payment_hash: PaymentHash, invoice_expiry_delta_secs: u32, highest_seen_timestamp: u64) -> Result { - let metadata_bytes = construct_metadata_bytes(min_value_msat, Method::UserPaymentHash, invoice_expiry_delta_secs, highest_seen_timestamp)?; +/// Equivalent to [`crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash`], +/// but no `ChannelManager` is required. Useful for generating invoices for [phantom node payments] +/// without a `ChannelManager`. +/// +/// See [`create`] for information on the `keys` and `current_time` parameters. +/// +/// [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager +pub fn create_from_hash(keys: &ExpandedKey, min_value_msat: Option, payment_hash: PaymentHash, invoice_expiry_delta_secs: u32, current_time: u64) -> Result { + let metadata_bytes = construct_metadata_bytes(min_value_msat, Method::UserPaymentHash, invoice_expiry_delta_secs, current_time)?; let mut hmac = HmacEngine::::new(&keys.user_pmt_hash_key); hmac.input(&metadata_bytes); @@ -155,7 +177,7 @@ fn construct_payment_secret(iv_bytes: &[u8; IV_LEN], metadata_bytes: &[u8; METAD /// /// The metadata is constructed as: /// payment method (3 bits) || payment amount (8 bytes - 3 bits) || expiry (8 bytes) -/// and encrypted using a key derived from [`KeysInterface::get_inbound_payment_key_material`]. +/// and encrypted using a key derived from [`NodeSigner::get_inbound_payment_key_material`]. /// /// Then on payment receipt, we verify in this method that the payment preimage and payment secret /// match what was constructed. @@ -176,10 +198,10 @@ fn construct_payment_secret(iv_bytes: &[u8; IV_LEN], metadata_bytes: &[u8; METAD /// /// See [`ExpandedKey`] docs for more info on the individual keys used. /// -/// [`KeysInterface::get_inbound_payment_key_material`]: crate::chain::keysinterface::KeysInterface::get_inbound_payment_key_material +/// [`NodeSigner::get_inbound_payment_key_material`]: crate::chain::keysinterface::NodeSigner::get_inbound_payment_key_material /// [`create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment /// [`create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash -pub(super) fn verify(payment_hash: PaymentHash, payment_data: msgs::FinalOnionHopData, highest_seen_timestamp: u64, keys: &ExpandedKey, logger: &L) -> Result, ()> +pub(super) fn verify(payment_hash: PaymentHash, payment_data: &msgs::FinalOnionHopData, highest_seen_timestamp: u64, keys: &ExpandedKey, logger: &L) -> Result, ()> where L::Target: Logger { let (iv_bytes, metadata_bytes) = decrypt_metadata(payment_data.payment_secret, keys);