Skip to content

Commit 9e1d6b1

Browse files
committed
Support responding to refunds with transient keys
1 parent a64af3d commit 9e1d6b1

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();
@@ -1223,6 +1236,26 @@ mod tests {
12231236
}
12241237
}
12251238

1239+
#[test]
1240+
fn builds_invoice_from_refund_using_derived_keys() {
1241+
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
1242+
let entropy = FixedEntropy {};
1243+
let secp_ctx = Secp256k1::new();
1244+
1245+
let refund = RefundBuilder::new("foo".into(), vec![1; 32], payer_pubkey(), 1000).unwrap()
1246+
.build().unwrap();
1247+
1248+
if let Err(e) = refund
1249+
.respond_using_derived_keys_no_std(
1250+
payment_paths(), payment_hash(), now(), &expanded_key, &entropy
1251+
)
1252+
.unwrap()
1253+
.build_and_sign(&secp_ctx)
1254+
{
1255+
panic!("error building invoice: {:?}", e);
1256+
}
1257+
}
1258+
12261259
#[test]
12271260
fn builds_invoice_with_relative_expiry() {
12281261
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;
@@ -431,6 +431,51 @@ impl Refund {
431431
InvoiceBuilder::for_refund(self, payment_paths, created_at, payment_hash, signing_pubkey)
432432
}
433433

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

lightning/src/offers/signer.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ impl MetadataMaterial {
157157
}
158158
}
159159

160+
pub(super) fn derive_keys(nonce: Nonce, expanded_key: &ExpandedKey) -> KeyPair {
161+
const IV_BYTES: &[u8; IV_LEN] = b"LDK Invoice ~~~~";
162+
let secp_ctx = Secp256k1::new();
163+
let hmac = Hmac::from_engine(expanded_key.hmac_for_offer(nonce, IV_BYTES));
164+
let privkey = SecretKey::from_slice(hmac.as_inner()).unwrap();
165+
KeyPair::from_secret_key(&secp_ctx, &privkey)
166+
}
167+
160168
/// Verifies data given in a TLV stream was used to produce the given metadata, consisting of:
161169
/// - a 128-bit [`Nonce`] and possibly
162170
/// - a [`Sha256`] hash of the nonce and the TLV records using the [`ExpandedKey`].

0 commit comments

Comments
 (0)