Skip to content

Commit b440fa0

Browse files
Move invoice signing behind KeysInterface
1 parent 44d7a2e commit b440fa0

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use utils::test_logger;
5656
use utils::test_persister::TestPersister;
5757

5858
use bitcoin::secp256k1::key::{PublicKey,SecretKey};
59+
use bitcoin::secp256k1::recovery::RecoverableSignature;
5960
use bitcoin::secp256k1::Secp256k1;
6061

6162
use std::mem;
@@ -206,6 +207,10 @@ impl KeysInterface for KeyProvider {
206207
disable_revocation_policy_check: false,
207208
})
208209
}
210+
211+
fn sign_invoice(&self, _invoice_preimage: Vec<u8>) -> Result<RecoverableSignature, ()> {
212+
unreachable!()
213+
}
209214
}
210215

211216
impl KeyProvider {

fuzz/src/full_stack.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use utils::test_logger;
4848
use utils::test_persister::TestPersister;
4949

5050
use bitcoin::secp256k1::key::{PublicKey,SecretKey};
51+
use bitcoin::secp256k1::recovery::RecoverableSignature;
5152
use bitcoin::secp256k1::Secp256k1;
5253

5354
use std::cell::RefCell;
@@ -313,6 +314,10 @@ impl KeysInterface for KeyProvider {
313314
fn read_chan_signer(&self, data: &[u8]) -> Result<EnforcingSigner, DecodeError> {
314315
EnforcingSigner::read(&mut std::io::Cursor::new(data))
315316
}
317+
318+
fn sign_invoice(&self, _invoice_preimage: Vec<u8>) -> Result<RecoverableSignature, ()> {
319+
unreachable!()
320+
}
316321
}
317322

318323
#[inline]

lightning/src/chain/keysinterface.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use bitcoin::hash_types::WPubkeyHash;
2626

2727
use bitcoin::secp256k1::key::{SecretKey, PublicKey};
2828
use bitcoin::secp256k1::{Secp256k1, Signature, Signing};
29+
use bitcoin::secp256k1::recovery::RecoverableSignature;
2930
use bitcoin::secp256k1;
3031

3132
use util::{byte_utils, transaction_utils};
@@ -391,6 +392,12 @@ pub trait KeysInterface: Send + Sync {
391392
/// contain no versioning scheme. You may wish to include your own version prefix and ensure
392393
/// you've read all of the provided bytes to ensure no corruption occurred.
393394
fn read_chan_signer(&self, reader: &[u8]) -> Result<Self::Signer, DecodeError>;
395+
396+
/// Sign an invoice's preimage (note that this is the preimage of the invoice, not the HTLC's
397+
/// preimage). By parameterizing by the preimage instead of the hash, we allow implementors of
398+
/// this trait to parse the invoice and make sure they're signing what they expect, rather than
399+
/// blindly signing the hash.
400+
fn sign_invoice(&self, invoice_preimage: Vec<u8>) -> Result<RecoverableSignature, ()>;
394401
}
395402

396403
#[derive(Clone)]
@@ -1047,6 +1054,10 @@ impl KeysInterface for KeysManager {
10471054
fn read_chan_signer(&self, reader: &[u8]) -> Result<Self::Signer, DecodeError> {
10481055
InMemorySigner::read(&mut std::io::Cursor::new(reader))
10491056
}
1057+
1058+
fn sign_invoice(&self, invoice_preimage: Vec<u8>) -> Result<RecoverableSignature, ()> {
1059+
Ok(self.secp_ctx.sign_recoverable(&hash_to_message!(&Sha256::hash(&invoice_preimage)), &self.get_node_secret()))
1060+
}
10501061
}
10511062

10521063
// Ensure that BaseSign can have a vtable

lightning/src/ln/channel.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4843,6 +4843,7 @@ mod tests {
48434843
use bitcoin::secp256k1::{Secp256k1, Message, Signature, All};
48444844
use bitcoin::secp256k1::ffi::Signature as FFISignature;
48454845
use bitcoin::secp256k1::key::{SecretKey,PublicKey};
4846+
use bitcoin::secp256k1::recovery::RecoverableSignature;
48464847
use bitcoin::hashes::sha256::Hash as Sha256;
48474848
use bitcoin::hashes::Hash;
48484849
use bitcoin::hash_types::{Txid, WPubkeyHash};
@@ -4888,6 +4889,7 @@ mod tests {
48884889
}
48894890
fn get_secure_random_bytes(&self) -> [u8; 32] { [0; 32] }
48904891
fn read_chan_signer(&self, _data: &[u8]) -> Result<Self::Signer, DecodeError> { panic!(); }
4892+
fn sign_invoice(&self, _invoice_preimage: Vec<u8>) -> Result<RecoverableSignature, ()> { panic!(); }
48914893
}
48924894

48934895
fn public_from_secret_hex(secp_ctx: &Secp256k1<All>, hex: &str) -> PublicKey {

lightning/src/util/test_utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use bitcoin::network::constants::Network;
3232
use bitcoin::hash_types::{BlockHash, Txid};
3333

3434
use bitcoin::secp256k1::{SecretKey, PublicKey, Secp256k1, Signature};
35+
use bitcoin::secp256k1::recovery::RecoverableSignature;
3536

3637
use regex;
3738

@@ -75,6 +76,7 @@ impl keysinterface::KeysInterface for OnlyReadsKeysInterface {
7576
fn read_chan_signer(&self, reader: &[u8]) -> Result<Self::Signer, msgs::DecodeError> {
7677
EnforcingSigner::read(&mut std::io::Cursor::new(reader))
7778
}
79+
fn sign_invoice(&self, _invoice_preimage: Vec<u8>) -> Result<RecoverableSignature, ()> { unreachable!(); }
7880
}
7981

8082
pub struct TestChainMonitor<'a> {
@@ -483,6 +485,10 @@ impl keysinterface::KeysInterface for TestKeysInterface {
483485
disable_revocation_policy_check: self.disable_revocation_policy_check,
484486
})
485487
}
488+
489+
fn sign_invoice(&self, invoice_preimage: Vec<u8>) -> Result<RecoverableSignature, ()> {
490+
self.backing.sign_invoice(invoice_preimage)
491+
}
486492
}
487493

488494

0 commit comments

Comments
 (0)