Skip to content

Commit 07cc40a

Browse files
committed
Support responding to refunds with transient keys
1 parent e984781 commit 07cc40a

File tree

3 files changed

+93
-7
lines changed

3 files changed

+93
-7
lines changed

lightning/src/offers/invoice.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,22 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
207207

208208
Self::new(&invoice_request.bytes, contents, Some(keys))
209209
}
210+
211+
pub(super) fn for_refund_using_keys(
212+
refund: &'a Refund, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
213+
payment_hash: PaymentHash, keys: KeyPair,
214+
) -> Result<Self, SemanticError> {
215+
let contents = InvoiceContents::ForRefund {
216+
refund: refund.contents.clone(),
217+
fields: InvoiceFields {
218+
payment_paths, created_at, relative_expiry: None, payment_hash,
219+
amount_msats: refund.amount_msats(), fallbacks: None,
220+
features: Bolt12InvoiceFeatures::empty(), signing_pubkey: keys.public_key(),
221+
},
222+
};
223+
224+
Self::new(&refund.bytes, contents, Some(keys))
225+
}
210226
}
211227

212228
impl<'a, S: SigningPubkeyStrategy> InvoiceBuilder<'a, S> {
@@ -322,12 +338,9 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
322338
}
323339

324340
let InvoiceBuilder { invreq_bytes, invoice, keys, .. } = self;
325-
let keys = match &invoice {
326-
InvoiceContents::ForOffer { .. } => keys.unwrap(),
327-
InvoiceContents::ForRefund { .. } => unreachable!(),
328-
};
329-
330341
let unsigned_invoice = UnsignedInvoice { invreq_bytes, invoice };
342+
343+
let keys = keys.unwrap();
331344
let invoice = unsigned_invoice
332345
.sign::<_, Infallible>(|digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys)))
333346
.unwrap();
@@ -1216,6 +1229,26 @@ mod tests {
12161229
}
12171230
}
12181231

1232+
#[test]
1233+
fn builds_invoice_from_refund_using_derived_keys() {
1234+
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
1235+
let entropy = FixedEntropy {};
1236+
let secp_ctx = Secp256k1::new();
1237+
1238+
let refund = RefundBuilder::new("foo".into(), vec![1; 32], payer_pubkey(), 1000).unwrap()
1239+
.build().unwrap();
1240+
1241+
if let Err(e) = refund
1242+
.respond_using_derived_keys_no_std(
1243+
payment_paths(), payment_hash(), now(), &expanded_key, &entropy
1244+
)
1245+
.unwrap()
1246+
.build_and_sign(&secp_ctx)
1247+
{
1248+
panic!("error building invoice: {:?}", e);
1249+
}
1250+
}
1251+
12191252
#[test]
12201253
fn builds_invoice_with_relative_expiry() {
12211254
let now = now();

lightning/src/offers/refund.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ use crate::ln::PaymentHash;
8484
use crate::ln::features::InvoiceRequestFeatures;
8585
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce};
8686
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
87-
use crate::offers::invoice::{BlindedPayInfo, ExplicitSigningPubkey, InvoiceBuilder};
87+
use crate::offers::invoice::{BlindedPayInfo, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder};
8888
use crate::offers::invoice_request::{InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef};
8989
use crate::offers::offer::{OfferTlvStream, OfferTlvStreamRef};
9090
use crate::offers::parse::{Bech32Encode, ParseError, ParsedMessage, SemanticError};
9191
use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
92-
use crate::offers::signer::{Metadata, MetadataMaterial};
92+
use crate::offers::signer::{Metadata, MetadataMaterial, self};
9393
use crate::onion_message::BlindedPath;
9494
use crate::util::ser::{SeekReadable, WithoutLength, Writeable, Writer};
9595
use crate::util::string::PrintableString;
@@ -429,6 +429,51 @@ impl Refund {
429429
InvoiceBuilder::for_refund(self, payment_paths, created_at, payment_hash, signing_pubkey)
430430
}
431431

432+
/// Creates an [`InvoiceBuilder`] for the refund using the given required fields and that uses
433+
/// derived signing keys to sign the [`Invoice`].
434+
///
435+
/// See [`Refund::respond_with`] for further details.
436+
///
437+
/// [`Invoice`]: crate::offers::invoice::Invoice
438+
#[cfg(feature = "std")]
439+
pub fn respond_using_derived_keys<ES: Deref>(
440+
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
441+
expanded_key: &ExpandedKey, entropy_source: ES
442+
) -> Result<InvoiceBuilder<DerivedSigningPubkey>, SemanticError>
443+
where
444+
ES::Target: EntropySource,
445+
{
446+
let created_at = std::time::SystemTime::now()
447+
.duration_since(std::time::SystemTime::UNIX_EPOCH)
448+
.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
449+
450+
self.respond_using_derived_keys_no_std(
451+
payment_paths, payment_hash, created_at, expanded_key, entropy_source
452+
)
453+
}
454+
455+
/// Creates an [`InvoiceBuilder`] for the refund using the given required fields and that uses
456+
/// derived signing keys to sign the [`Invoice`].
457+
///
458+
/// See [`Refund::respond_with_no_std`] for further details.
459+
///
460+
/// [`Invoice`]: crate::offers::invoice::Invoice
461+
pub fn respond_using_derived_keys_no_std<ES: Deref>(
462+
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
463+
created_at: core::time::Duration, expanded_key: &ExpandedKey, entropy_source: ES
464+
) -> Result<InvoiceBuilder<DerivedSigningPubkey>, SemanticError>
465+
where
466+
ES::Target: EntropySource,
467+
{
468+
if self.features().requires_unknown_bits() {
469+
return Err(SemanticError::UnknownRequiredFeatures);
470+
}
471+
472+
let nonce = Nonce::from_entropy_source(entropy_source);
473+
let keys = signer::derive_keys(nonce, expanded_key);
474+
InvoiceBuilder::for_refund_using_keys(self, payment_paths, created_at, payment_hash, keys)
475+
}
476+
432477
#[cfg(test)]
433478
fn as_tlv_stream(&self) -> RefundTlvStreamRef {
434479
self.contents.as_tlv_stream()

lightning/src/offers/signer.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ impl MetadataMaterial {
149149
}
150150
}
151151

152+
pub(super) fn derive_keys(nonce: Nonce, expanded_key: &ExpandedKey) -> KeyPair {
153+
const IV_BYTES: &[u8; IV_LEN] = b"LDK Invoice ~~~~";
154+
let secp_ctx = Secp256k1::new();
155+
let hmac = Hmac::from_engine(expanded_key.hmac_for_offer(nonce, IV_BYTES));
156+
let privkey = SecretKey::from_slice(hmac.as_inner()).unwrap();
157+
KeyPair::from_secret_key(&secp_ctx, &privkey)
158+
}
159+
152160
/// Verifies data given in a TLV stream was used to produce the given metadata, consisting of:
153161
/// - a 128-bit [`Nonce`] and possibly
154162
/// - a [`Sha256`] hash of the nonce and the TLV records using the [`ExpandedKey`].

0 commit comments

Comments
 (0)