1 use ln::chan_utils::{HTLCOutputInCommitment, TxCreationKeys};
3 use chain::keysinterface::{ChannelKeys, InMemoryChannelKeys};
8 use bitcoin::blockdata::transaction::Transaction;
9 use bitcoin::blockdata::script::Script;
12 use secp256k1::key::{SecretKey, PublicKey};
13 use secp256k1::{Secp256k1, Signature};
15 /// Enforces some rules on ChannelKeys calls. Eventually we will probably want to expose a variant
16 /// of this which would essentially be what you'd want to run on a hardware wallet.
17 pub struct EnforcingChannelKeys {
18 pub inner: InMemoryChannelKeys,
19 commitment_number_obscure_and_last: Mutex<(Option<u64>, u64)>,
22 impl EnforcingChannelKeys {
23 pub fn new(inner: InMemoryChannelKeys) -> Self {
26 commitment_number_obscure_and_last: Mutex::new((None, 0)),
30 impl ChannelKeys for EnforcingChannelKeys {
31 fn funding_key(&self) -> &SecretKey { self.inner.funding_key() }
32 fn revocation_base_key(&self) -> &SecretKey { self.inner.revocation_base_key() }
33 fn payment_base_key(&self) -> &SecretKey { self.inner.payment_base_key() }
34 fn delayed_payment_base_key(&self) -> &SecretKey { self.inner.delayed_payment_base_key() }
35 fn htlc_base_key(&self) -> &SecretKey { self.inner.htlc_base_key() }
36 fn commitment_seed(&self) -> &[u8; 32] { self.inner.commitment_seed() }
38 fn sign_remote_commitment<T: secp256k1::Signing>(&self, channel_value_satoshis: u64, channel_funding_script: &Script, feerate_per_kw: u64, commitment_tx: &Transaction, keys: &TxCreationKeys, htlcs: &[&HTLCOutputInCommitment], to_self_delay: u16, secp_ctx: &Secp256k1<T>, redeem_scripts: &Vec<Script>, remote_per_commitment_point: &PublicKey) -> Result<(Signature, Vec<Signature>), ()> {
39 if commitment_tx.input.len() != 1 { panic!(); }
40 if commitment_tx.output.len() != redeem_scripts.len() { panic!(); }
42 let obscured_commitment_transaction_number = (commitment_tx.lock_time & 0xffffff) as u64 | ((commitment_tx.input[0].sequence as u64 & 0xffffff) << 3*8);
45 let mut commitment_data = self.commitment_number_obscure_and_last.lock().unwrap();
46 if commitment_data.0.is_none() {
47 commitment_data.0 = Some(obscured_commitment_transaction_number ^ commitment_data.1);
49 let commitment_number = obscured_commitment_transaction_number ^ commitment_data.0.unwrap();
50 assert!(commitment_number == commitment_data.1 || commitment_number == commitment_data.1 + 1);
51 commitment_data.1 = cmp::max(commitment_number, commitment_data.1)
54 Ok(self.inner.sign_remote_commitment(channel_value_satoshis, channel_funding_script, feerate_per_kw, commitment_tx, keys, htlcs, to_self_delay, secp_ctx, redeem_scripts, remote_per_commitment_point).unwrap())
57 fn sign_closing_transaction<T: secp256k1::Signing>(&self, channel_value_satoshis: u64, channel_funding_redeemscript: &Script, closing_tx: &Transaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
58 Ok(self.inner.sign_closing_transaction(channel_value_satoshis, channel_funding_redeemscript, closing_tx, secp_ctx).unwrap())
61 fn sign_channel_announcement<T: secp256k1::Signing>(&self, msg: &msgs::UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
62 self.inner.sign_channel_announcement(msg, secp_ctx)
66 impl_writeable!(EnforcingChannelKeys, 0, {
68 commitment_number_obscure_and_last