Skip to content

Commit 7b523bf

Browse files
fixup! Add BOLT 12 payer proof PoC implementation
1 parent e609ea5 commit 7b523bf

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

lightning/src/offers/payer_proof.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
2020
use alloc::collections::BTreeSet;
2121

22+
use super::signer::derive_keys;
2223
use crate::io;
2324
use crate::io::Read;
25+
use crate::ln::inbound_payment::ExpandedKey;
2426
use crate::offers::invoice::{Bolt12Invoice, SIGNATURE_TAG};
2527
use crate::offers::invoice_request::INVOICE_REQUEST_PAYER_ID_TYPE;
2628
use crate::offers::merkle::{
2729
self, SelectiveDisclosure, SelectiveDisclosureError, TaggedHash, TlvStream, SIGNATURE_TYPES,
2830
};
31+
use crate::offers::nonce::Nonce;
2932
use crate::offers::parse::Bech32Encode;
3033
use crate::types::features::Bolt12InvoiceFeatures;
3134
use crate::types::payment::{PaymentHash, PaymentPreimage};
@@ -311,6 +314,28 @@ impl UnsignedPayerProof {
311314
})
312315
}
313316

317+
/// Sign the proof using a key derived from an [`ExpandedKey`] and [`Nonce`].
318+
///
319+
/// This method derives the payer signing key using the same derivation scheme as invoice
320+
/// requests with derived signing pubkeys. Use this when the invoice request was created
321+
/// with `deriving_signing_pubkey` and you want to sign the payer proof with the same key.
322+
///
323+
/// The derived key must match the `payer_id` in the original invoice for the signature
324+
/// to be valid.
325+
pub fn sign_with_derived_key(
326+
self, expanded_key: &ExpandedKey, nonce: Nonce, note: Option<&str>,
327+
) -> Result<PayerProof, PayerProofError> {
328+
let keys = derive_keys(nonce, expanded_key);
329+
330+
// Verify the derived key matches the expected payer_id
331+
if keys.public_key() != self.payer_id {
332+
return Err(PayerProofError::InvalidPayerSignature);
333+
}
334+
335+
let secp_ctx = Secp256k1::new();
336+
self.sign(|message| Ok(secp_ctx.sign_schnorr_no_aux_rand(message, &keys)), note)
337+
}
338+
314339
/// Compute the payer signature message per BOLT 12 signature calculation.
315340
fn compute_payer_signature_message(note: Option<&str>, merkle_root: &sha256::Hash) -> Message {
316341
let mut inner_hasher = sha256::Hash::engine();

0 commit comments

Comments
 (0)