Skip to content

Commit

Permalink
Provide the signer with a full RawBolt11Invoice to sign
Browse files Browse the repository at this point in the history
Now that the `lightning` crate depends on the `lightning-invoice`
crate, there's no reason to have the `sign_invoice` method take raw
base32 field elements as we can now give it a real
`RawBolt11Invoice`, which we do here.

This simplifies the interface and avoids a
serialization-deserialization roundtrip when signing invoices in a
validating signer.

FIxes lightningdevkit#3227
  • Loading branch information
TheBlueMatt committed Aug 9, 2024
1 parent 7e815be commit 08671c8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 51 deletions.
13 changes: 4 additions & 9 deletions lightning/src/ln/invoice_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use lightning_invoice::{Description, Bolt11InvoiceDescription, Sha256};

use crate::prelude::*;

use bech32::ToBase32;
use bitcoin::hashes::Hash;
use crate::chain;
use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
Expand Down Expand Up @@ -219,10 +218,8 @@ where
Ok(inv) => inv,
Err(e) => return Err(SignOrCreationError::CreationError(e))
};
let hrp_str = raw_invoice.hrp.to_string();
let hrp_bytes = hrp_str.as_bytes();
let data_without_signature = raw_invoice.data.to_base32();
let signed_raw_invoice = raw_invoice.sign(|_| node_signer.sign_invoice(hrp_bytes, &data_without_signature, Recipient::PhantomNode));
let signature = node_signer.sign_invoice(&raw_invoice, Recipient::PhantomNode);
let signed_raw_invoice = raw_invoice.sign(|_| signature);
match signed_raw_invoice {
Ok(inv) => Ok(Bolt11Invoice::from_signed(inv).unwrap()),
Err(e) => Err(SignOrCreationError::SignError(e))
Expand Down Expand Up @@ -571,10 +568,8 @@ fn _create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_has
Ok(inv) => inv,
Err(e) => return Err(SignOrCreationError::CreationError(e))
};
let hrp_str = raw_invoice.hrp.to_string();
let hrp_bytes = hrp_str.as_bytes();
let data_without_signature = raw_invoice.data.to_base32();
let signed_raw_invoice = raw_invoice.sign(|_| node_signer.sign_invoice(hrp_bytes, &data_without_signature, Recipient::Node));
let signature = node_signer.sign_invoice(&raw_invoice, Recipient::Node);
let signed_raw_invoice = raw_invoice.sign(|_| signature);
match signed_raw_invoice {
Ok(inv) => Ok(Bolt11Invoice::from_signed(inv).unwrap()),
Err(e) => Err(SignOrCreationError::SignError(e))
Expand Down
18 changes: 9 additions & 9 deletions lightning/src/sign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use bitcoin::sighash::EcdsaSighashType;
use bitcoin::transaction::Version;
use bitcoin::transaction::{Transaction, TxIn, TxOut};

use bech32::u5;
use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
use bitcoin::hashes::{Hash, HashEngine};
Expand All @@ -37,6 +36,8 @@ use bitcoin::secp256k1::All;
use bitcoin::secp256k1::{Keypair, PublicKey, Scalar, Secp256k1, SecretKey, Signing};
use bitcoin::{secp256k1, Psbt, Sequence, Txid, WPubkeyHash, Witness};

use lightning_invoice::RawBolt11Invoice;

use crate::chain::transaction::OutPoint;
use crate::crypto::utils::{hkdf_extract_expand_twice, sign, sign_with_aux_rand};
use crate::ln::chan_utils;
Expand Down Expand Up @@ -69,7 +70,6 @@ use crate::sign::ecdsa::EcdsaChannelSigner;
#[cfg(taproot)]
use crate::sign::taproot::TaprootChannelSigner;
use crate::util::atomic_counter::AtomicCounter;
use crate::util::invoice::construct_invoice_preimage;
use core::convert::TryInto;
use core::ops::Deref;
use core::sync::atomic::{AtomicUsize, Ordering};
Expand Down Expand Up @@ -867,7 +867,7 @@ pub trait NodeSigner {
///
/// Errors if the [`Recipient`] variant is not supported by the implementation.
fn sign_invoice(
&self, hrp_bytes: &[u8], invoice_data: &[u5], recipient: Recipient,
&self, invoice: &RawBolt11Invoice, recipient: Recipient,
) -> Result<RecoverableSignature, ()>;

/// Signs the [`TaggedHash`] of a BOLT 12 invoice request.
Expand Down Expand Up @@ -2174,15 +2174,15 @@ impl NodeSigner for KeysManager {
}

fn sign_invoice(
&self, hrp_bytes: &[u8], invoice_data: &[u5], recipient: Recipient,
&self, invoice: &RawBolt11Invoice, recipient: Recipient,
) -> Result<RecoverableSignature, ()> {
let preimage = construct_invoice_preimage(&hrp_bytes, &invoice_data);
let hash = invoice.signable_hash();
let secret = match recipient {
Recipient::Node => Ok(&self.node_secret),
Recipient::PhantomNode => Err(()),
}?;
Ok(self.secp_ctx.sign_ecdsa_recoverable(
&hash_to_message!(&Sha256::hash(&preimage).to_byte_array()),
&hash_to_message!(&hash),
secret,
))
}
Expand Down Expand Up @@ -2352,15 +2352,15 @@ impl NodeSigner for PhantomKeysManager {
}

fn sign_invoice(
&self, hrp_bytes: &[u8], invoice_data: &[u5], recipient: Recipient,
&self, invoice: &RawBolt11Invoice, recipient: Recipient,
) -> Result<RecoverableSignature, ()> {
let preimage = construct_invoice_preimage(&hrp_bytes, &invoice_data);
let hash = invoice.signable_hash();
let secret = match recipient {
Recipient::Node => &self.inner.node_secret,
Recipient::PhantomNode => &self.phantom_secret,
};
Ok(self.inner.secp_ctx.sign_ecdsa_recoverable(
&hash_to_message!(&Sha256::hash(&preimage).to_byte_array()),
&hash_to_message!(&hash),
secret,
))
}
Expand Down
28 changes: 0 additions & 28 deletions lightning/src/util/invoice.rs

This file was deleted.

1 change: 0 additions & 1 deletion lightning/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub mod ser_macros;
pub mod errors;
pub mod ser;
pub mod message_signing;
pub mod invoice;
pub mod persist;
pub mod scid_utils;
pub mod sweep;
Expand Down
9 changes: 5 additions & 4 deletions lightning/src/util/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ use bitcoin::secp256k1::ecdh::SharedSecret;
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
use bitcoin::secp256k1::schnorr;

use lightning_invoice::RawBolt11Invoice;

use crate::io;
use crate::prelude::*;
use core::cell::RefCell;
use core::time::Duration;
use crate::sync::{Mutex, Arc};
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use core::mem;
use bech32::u5;
use crate::sign::{InMemorySigner, RandomBytes, Recipient, EntropySource, NodeSigner, SignerProvider};

#[cfg(feature = "std")]
Expand Down Expand Up @@ -1217,7 +1218,7 @@ impl NodeSigner for TestNodeSigner {
Ok(SharedSecret::new(other_key, &node_secret))
}

fn sign_invoice(&self, _: &[u8], _: &[bech32::u5], _: Recipient) -> Result<bitcoin::secp256k1::ecdsa::RecoverableSignature, ()> {
fn sign_invoice(&self, _: &RawBolt11Invoice, _: Recipient) -> Result<RecoverableSignature, ()> {
unreachable!()
}

Expand Down Expand Up @@ -1270,8 +1271,8 @@ impl NodeSigner for TestKeysInterface {
self.backing.get_inbound_payment_key_material()
}

fn sign_invoice(&self, hrp_bytes: &[u8], invoice_data: &[u5], recipient: Recipient) -> Result<RecoverableSignature, ()> {
self.backing.sign_invoice(hrp_bytes, invoice_data, recipient)
fn sign_invoice(&self, invoice: &RawBolt11Invoice, recipient: Recipient) -> Result<RecoverableSignature, ()> {
self.backing.sign_invoice(invoice, recipient)
}

fn sign_bolt12_invoice_request(
Expand Down

0 comments on commit 08671c8

Please sign in to comment.