2020-03-20 18:04:01 -04:00
|
|
|
use ln::chan_utils::{HTLCOutputInCommitment, TxCreationKeys, ChannelPublicKeys, LocalCommitmentTransaction};
|
2020-03-21 15:39:19 -04:00
|
|
|
use ln::channelmanager::PaymentPreimage;
|
2019-12-07 17:54:55 -05:00
|
|
|
use ln::msgs;
|
2019-11-26 16:46:33 -05:00
|
|
|
use chain::keysinterface::{ChannelKeys, InMemoryChannelKeys};
|
|
|
|
|
2019-11-27 16:08:48 -05:00
|
|
|
use std::cmp;
|
2020-02-04 09:15:59 -08:00
|
|
|
use std::sync::{Mutex, Arc};
|
2019-11-27 16:08:48 -05:00
|
|
|
|
|
|
|
use bitcoin::blockdata::transaction::Transaction;
|
|
|
|
|
|
|
|
use secp256k1;
|
2020-01-09 17:28:48 -08:00
|
|
|
use secp256k1::key::{SecretKey, PublicKey};
|
2019-11-27 16:08:48 -05:00
|
|
|
use secp256k1::{Secp256k1, Signature};
|
2020-02-04 09:15:59 -08:00
|
|
|
use util::ser::{Writeable, Writer, Readable};
|
|
|
|
use std::io::Error;
|
|
|
|
use ln::msgs::DecodeError;
|
2019-11-26 16:46:33 -05:00
|
|
|
|
|
|
|
/// Enforces some rules on ChannelKeys calls. Eventually we will probably want to expose a variant
|
|
|
|
/// of this which would essentially be what you'd want to run on a hardware wallet.
|
2020-02-04 09:15:59 -08:00
|
|
|
#[derive(Clone)]
|
2019-11-26 16:46:33 -05:00
|
|
|
pub struct EnforcingChannelKeys {
|
|
|
|
pub inner: InMemoryChannelKeys,
|
2020-02-04 09:15:59 -08:00
|
|
|
commitment_number_obscure_and_last: Arc<Mutex<(Option<u64>, u64)>>,
|
2019-11-26 16:46:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EnforcingChannelKeys {
|
|
|
|
pub fn new(inner: InMemoryChannelKeys) -> Self {
|
|
|
|
Self {
|
|
|
|
inner,
|
2020-02-04 09:15:59 -08:00
|
|
|
commitment_number_obscure_and_last: Arc::new(Mutex::new((None, 0))),
|
2019-11-26 16:46:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-16 16:51:15 -08:00
|
|
|
|
|
|
|
impl EnforcingChannelKeys {
|
|
|
|
fn check_keys<T: secp256k1::Signing + secp256k1::Verification>(&self, secp_ctx: &Secp256k1<T>,
|
|
|
|
keys: &TxCreationKeys) {
|
2020-02-04 09:15:59 -08:00
|
|
|
let revocation_base = PublicKey::from_secret_key(secp_ctx, &self.inner.revocation_base_key());
|
|
|
|
let payment_base = PublicKey::from_secret_key(secp_ctx, &self.inner.payment_base_key());
|
|
|
|
let htlc_base = PublicKey::from_secret_key(secp_ctx, &self.inner.htlc_base_key());
|
2020-01-16 16:51:15 -08:00
|
|
|
|
|
|
|
let remote_points = self.inner.remote_channel_pubkeys.as_ref().unwrap();
|
|
|
|
|
|
|
|
let keys_expected = TxCreationKeys::new(secp_ctx,
|
|
|
|
&keys.per_commitment_point,
|
|
|
|
&remote_points.delayed_payment_basepoint,
|
|
|
|
&remote_points.htlc_basepoint,
|
|
|
|
&revocation_base,
|
|
|
|
&payment_base,
|
|
|
|
&htlc_base).unwrap();
|
|
|
|
if keys != &keys_expected { panic!("derived different per-tx keys") }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 16:46:33 -05:00
|
|
|
impl ChannelKeys for EnforcingChannelKeys {
|
|
|
|
fn funding_key(&self) -> &SecretKey { self.inner.funding_key() }
|
|
|
|
fn revocation_base_key(&self) -> &SecretKey { self.inner.revocation_base_key() }
|
|
|
|
fn payment_base_key(&self) -> &SecretKey { self.inner.payment_base_key() }
|
|
|
|
fn delayed_payment_base_key(&self) -> &SecretKey { self.inner.delayed_payment_base_key() }
|
|
|
|
fn htlc_base_key(&self) -> &SecretKey { self.inner.htlc_base_key() }
|
|
|
|
fn commitment_seed(&self) -> &[u8; 32] { self.inner.commitment_seed() }
|
2020-02-04 09:15:59 -08:00
|
|
|
fn pubkeys<'a>(&'a self) -> &'a ChannelPublicKeys { self.inner.pubkeys() }
|
2019-11-27 16:08:48 -05:00
|
|
|
|
2020-01-23 13:33:31 -08:00
|
|
|
fn sign_remote_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, feerate_per_kw: u64, commitment_tx: &Transaction, keys: &TxCreationKeys, htlcs: &[&HTLCOutputInCommitment], to_self_delay: u16, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()> {
|
2020-01-16 16:51:15 -08:00
|
|
|
if commitment_tx.input.len() != 1 { panic!("lightning commitment transactions have a single input"); }
|
|
|
|
self.check_keys(secp_ctx, keys);
|
2019-11-27 16:08:48 -05:00
|
|
|
let obscured_commitment_transaction_number = (commitment_tx.lock_time & 0xffffff) as u64 | ((commitment_tx.input[0].sequence as u64 & 0xffffff) << 3*8);
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut commitment_data = self.commitment_number_obscure_and_last.lock().unwrap();
|
|
|
|
if commitment_data.0.is_none() {
|
|
|
|
commitment_data.0 = Some(obscured_commitment_transaction_number ^ commitment_data.1);
|
|
|
|
}
|
|
|
|
let commitment_number = obscured_commitment_transaction_number ^ commitment_data.0.unwrap();
|
|
|
|
assert!(commitment_number == commitment_data.1 || commitment_number == commitment_data.1 + 1);
|
|
|
|
commitment_data.1 = cmp::max(commitment_number, commitment_data.1)
|
|
|
|
}
|
|
|
|
|
2020-01-23 13:33:31 -08:00
|
|
|
Ok(self.inner.sign_remote_commitment(feerate_per_kw, commitment_tx, keys, htlcs, to_self_delay, secp_ctx).unwrap())
|
2019-11-27 16:08:48 -05:00
|
|
|
}
|
2019-12-07 17:54:55 -05:00
|
|
|
|
2020-04-19 17:26:41 -04:00
|
|
|
fn sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &LocalCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
|
2020-04-17 21:26:38 -04:00
|
|
|
self.inner.sign_local_commitment(local_commitment_tx, secp_ctx)
|
2020-03-20 18:04:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2020-04-19 17:26:41 -04:00
|
|
|
fn unsafe_sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &LocalCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
|
|
|
|
self.inner.unsafe_sign_local_commitment(local_commitment_tx, secp_ctx)
|
2020-03-20 18:04:01 -04:00
|
|
|
}
|
|
|
|
|
2020-03-09 18:15:35 -04:00
|
|
|
fn sign_htlc_transaction<T: secp256k1::Signing>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, htlc_index: u32, preimage: Option<PaymentPreimage>, local_csv: u16, secp_ctx: &Secp256k1<T>) {
|
|
|
|
self.inner.sign_htlc_transaction(local_commitment_tx, htlc_index, preimage, local_csv, secp_ctx);
|
2020-03-21 15:39:19 -04:00
|
|
|
}
|
|
|
|
|
2020-01-23 14:32:29 -08:00
|
|
|
fn sign_closing_transaction<T: secp256k1::Signing>(&self, closing_tx: &Transaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
|
|
|
|
Ok(self.inner.sign_closing_transaction(closing_tx, secp_ctx).unwrap())
|
2019-12-13 01:57:45 -05:00
|
|
|
}
|
|
|
|
|
2019-12-07 17:54:55 -05:00
|
|
|
fn sign_channel_announcement<T: secp256k1::Signing>(&self, msg: &msgs::UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
|
|
|
|
self.inner.sign_channel_announcement(msg, secp_ctx)
|
|
|
|
}
|
2020-01-09 17:28:48 -08:00
|
|
|
|
2020-01-16 16:51:15 -08:00
|
|
|
fn set_remote_channel_pubkeys(&mut self, channel_pubkeys: &ChannelPublicKeys) {
|
|
|
|
self.inner.set_remote_channel_pubkeys(channel_pubkeys)
|
2020-01-09 17:28:48 -08:00
|
|
|
}
|
2019-11-26 16:46:33 -05:00
|
|
|
}
|
|
|
|
|
2020-02-04 09:15:59 -08:00
|
|
|
impl Writeable for EnforcingChannelKeys {
|
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
|
|
|
|
self.inner.write(writer)?;
|
|
|
|
let (obscure, last) = *self.commitment_number_obscure_and_last.lock().unwrap();
|
|
|
|
obscure.write(writer)?;
|
|
|
|
last.write(writer)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-01-16 16:51:15 -08:00
|
|
|
|
2020-02-23 23:12:19 -05:00
|
|
|
impl Readable for EnforcingChannelKeys {
|
|
|
|
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
|
2020-02-04 09:15:59 -08:00
|
|
|
let inner = Readable::read(reader)?;
|
|
|
|
let obscure_and_last = Readable::read(reader)?;
|
|
|
|
Ok(EnforcingChannelKeys {
|
|
|
|
inner: inner,
|
|
|
|
commitment_number_obscure_and_last: Arc::new(Mutex::new(obscure_and_last))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|