X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fchain%2Fkeysinterface.rs;h=2dc42abad3ae4cb92bd6284de47c816b4a801d1f;hb=276c607fa87ac502b6b262a4ba1d59153ad779f9;hp=daee746bed7687553b50bb018e6182223c486aa0;hpb=4909d3cd6a05dfcba39fe08e6d8f539944c8ef66;p=rust-lightning diff --git a/lightning/src/chain/keysinterface.rs b/lightning/src/chain/keysinterface.rs index daee746b..2dc42aba 100644 --- a/lightning/src/chain/keysinterface.rs +++ b/lightning/src/chain/keysinterface.rs @@ -13,21 +13,19 @@ use bitcoin::hashes::{Hash, HashEngine}; use bitcoin::hashes::sha256::HashEngine as Sha256State; use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::hashes::sha256d::Hash as Sha256dHash; -use bitcoin::hashes::hash160::Hash as Hash160; +use bitcoin::hash_types::WPubkeyHash; -use secp256k1::key::{SecretKey, PublicKey}; -use secp256k1::{Secp256k1, Signature, Signing}; -use secp256k1; +use bitcoin::secp256k1::key::{SecretKey, PublicKey}; +use bitcoin::secp256k1::{Secp256k1, Signature, Signing}; +use bitcoin::secp256k1; use util::byte_utils; -use util::logger::Logger; use util::ser::{Writeable, Writer, Readable}; use ln::chan_utils; use ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, LocalCommitmentTransaction}; use ln::msgs; -use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; use std::io::Error; use ln::msgs::DecodeError; @@ -74,6 +72,8 @@ pub enum SpendableOutputDescriptor { /// The output which is referenced by the given outpoint output: TxOut, }, + // TODO: Note that because key is now static and exactly what is provided by us, we should drop + // this in favor of StaticOutput: /// An output to a P2WPKH, spendable exclusively by the given private key. /// The witness in the spending input, is, thus, simply: /// @@ -194,9 +194,10 @@ pub trait ChannelKeys : Send+Clone { fn funding_key<'a>(&'a self) -> &'a SecretKey; /// Gets the local secret key for blinded revocation pubkey fn revocation_base_key<'a>(&'a self) -> &'a SecretKey; - /// Gets the local secret key used in to_remote output of remote commitment tx - /// (and also as part of obscured commitment number) - fn payment_base_key<'a>(&'a self) -> &'a SecretKey; + /// Gets the local secret key used in the to_remote output of remote commitment tx (ie the + /// output to us in transactions our counterparty broadcasts). + /// Also as part of obscured commitment number. + fn payment_key<'a>(&'a self) -> &'a SecretKey; /// Gets the local secret key used in HTLC-Success/HTLC-Timeout txn and to_local output fn delayed_payment_base_key<'a>(&'a self) -> &'a SecretKey; /// Gets the local htlc secret key used in commitment tx htlc outputs @@ -245,6 +246,27 @@ pub trait ChannelKeys : Send+Clone { /// return value must contain a signature. fn sign_local_commitment_htlc_transactions(&self, local_commitment_tx: &LocalCommitmentTransaction, local_csv: u16, secp_ctx: &Secp256k1) -> Result>, ()>; + /// Create a signature for a transaction spending an HTLC or commitment transaction output + /// when our counterparty broadcast an old state. + /// + /// Justice transaction may claim multiples outputs at same time if timelock are similar. + /// It may be called multiples time for same output(s) if a fee-bump is needed with regards + /// to an upcoming timelock expiration. + /// + /// Witness_script is a revokable witness script as defined in BOLT3 for `to_local`/HTLC + /// outputs. + /// + /// Input index is a pointer towards outpoint spent, commited by sigs (BIP 143). + /// + /// Amount is value of the output spent by this input, committed by sigs (BIP 143). + /// + /// Per_commitment key is revocation secret such as provided by remote party while + /// revocating detected onchain transaction. It's not a _local_ secret key, therefore + /// it may cross interfaces, a node compromise won't allow to spend revoked output without + /// also compromissing revocation key. + //TODO: dry-up witness_script and pass pubkeys + fn sign_justice_transaction(&self, justice_tx: &Transaction, input: usize, witness_script: &Script, amount: u64, per_commitment_key: &SecretKey, revocation_pubkey: &PublicKey, is_htlc: bool, secp_ctx: &Secp256k1) -> Result; + /// Create a signature for a (proposed) closing transaction. /// /// Note that, due to rounding, there may be one "missing" satoshi, and either party may have @@ -273,8 +295,8 @@ pub struct InMemoryChannelKeys { funding_key: SecretKey, /// Local secret key for blinded revocation pubkey revocation_base_key: SecretKey, - /// Local secret key used in commitment tx htlc outputs - payment_base_key: SecretKey, + /// Local secret key used for our balance in remote-broadcasted commitment transactions + payment_key: SecretKey, /// Local secret key used in HTLC tx delayed_payment_base_key: SecretKey, /// Local htlc secret key used in commitment tx htlc outputs @@ -295,19 +317,19 @@ impl InMemoryChannelKeys { secp_ctx: &Secp256k1, funding_key: SecretKey, revocation_base_key: SecretKey, - payment_base_key: SecretKey, + payment_key: SecretKey, delayed_payment_base_key: SecretKey, htlc_base_key: SecretKey, commitment_seed: [u8; 32], channel_value_satoshis: u64) -> InMemoryChannelKeys { let local_channel_pubkeys = InMemoryChannelKeys::make_local_keys(secp_ctx, &funding_key, &revocation_base_key, - &payment_base_key, &delayed_payment_base_key, + &payment_key, &delayed_payment_base_key, &htlc_base_key); InMemoryChannelKeys { funding_key, revocation_base_key, - payment_base_key, + payment_key, delayed_payment_base_key, htlc_base_key, commitment_seed, @@ -320,14 +342,14 @@ impl InMemoryChannelKeys { fn make_local_keys(secp_ctx: &Secp256k1, funding_key: &SecretKey, revocation_base_key: &SecretKey, - payment_base_key: &SecretKey, + payment_key: &SecretKey, delayed_payment_base_key: &SecretKey, htlc_base_key: &SecretKey) -> ChannelPublicKeys { let from_secret = |s: &SecretKey| PublicKey::from_secret_key(secp_ctx, s); ChannelPublicKeys { funding_pubkey: from_secret(&funding_key), revocation_basepoint: from_secret(&revocation_base_key), - payment_basepoint: from_secret(&payment_base_key), + payment_point: from_secret(&payment_key), delayed_payment_basepoint: from_secret(&delayed_payment_base_key), htlc_basepoint: from_secret(&htlc_base_key), } @@ -337,7 +359,7 @@ impl InMemoryChannelKeys { impl ChannelKeys for InMemoryChannelKeys { fn funding_key(&self) -> &SecretKey { &self.funding_key } fn revocation_base_key(&self) -> &SecretKey { &self.revocation_base_key } - fn payment_base_key(&self) -> &SecretKey { &self.payment_base_key } + fn payment_key(&self) -> &SecretKey { &self.payment_key } fn delayed_payment_base_key(&self) -> &SecretKey { &self.delayed_payment_base_key } fn htlc_base_key(&self) -> &SecretKey { &self.htlc_base_key } fn commitment_seed(&self) -> &[u8; 32] { &self.commitment_seed } @@ -393,6 +415,15 @@ impl ChannelKeys for InMemoryChannelKeys { local_commitment_tx.get_htlc_sigs(&self.htlc_base_key, local_csv, secp_ctx) } + fn sign_justice_transaction(&self, justice_tx: &Transaction, input: usize, witness_script: &Script, amount: u64, per_commitment_key: &SecretKey, revocation_pubkey: &PublicKey, is_htlc: bool, secp_ctx: &Secp256k1) -> Result { + if let Ok(revocation_key) = chan_utils::derive_private_revocation_key(&secp_ctx, &per_commitment_key, &self.revocation_base_key) { + let sighash_parts = bip143::SighashComponents::new(&justice_tx); + let sighash = hash_to_message!(&sighash_parts.sighash_all(&justice_tx.input[input], &witness_script, amount)[..]); + return Ok(secp_ctx.sign(&sighash, &revocation_key)) + } + Err(()) + } + fn sign_closing_transaction(&self, closing_tx: &Transaction, secp_ctx: &Secp256k1) -> Result { if closing_tx.input.len() != 1 { return Err(()); } if closing_tx.input[0].witness.len() != 0 { return Err(()); } @@ -422,7 +453,7 @@ impl Writeable for InMemoryChannelKeys { fn write(&self, writer: &mut W) -> Result<(), Error> { self.funding_key.write(writer)?; self.revocation_base_key.write(writer)?; - self.payment_base_key.write(writer)?; + self.payment_key.write(writer)?; self.delayed_payment_base_key.write(writer)?; self.htlc_base_key.write(writer)?; self.commitment_seed.write(writer)?; @@ -437,7 +468,7 @@ impl Readable for InMemoryChannelKeys { fn read(reader: &mut R) -> Result { let funding_key = Readable::read(reader)?; let revocation_base_key = Readable::read(reader)?; - let payment_base_key = Readable::read(reader)?; + let payment_key = Readable::read(reader)?; let delayed_payment_base_key = Readable::read(reader)?; let htlc_base_key = Readable::read(reader)?; let commitment_seed = Readable::read(reader)?; @@ -446,13 +477,13 @@ impl Readable for InMemoryChannelKeys { let secp_ctx = Secp256k1::signing_only(); let local_channel_pubkeys = InMemoryChannelKeys::make_local_keys(&secp_ctx, &funding_key, &revocation_base_key, - &payment_base_key, &delayed_payment_base_key, + &payment_key, &delayed_payment_base_key, &htlc_base_key); Ok(InMemoryChannelKeys { funding_key, revocation_base_key, - payment_base_key, + payment_key, delayed_payment_base_key, htlc_base_key, commitment_seed, @@ -483,7 +514,6 @@ pub struct KeysManager { channel_id_child_index: AtomicUsize, unique_start: Sha256State, - logger: Arc, } impl KeysManager { @@ -506,16 +536,16 @@ impl KeysManager { /// Note that until the 0.1 release there is no guarantee of backward compatibility between /// versions. Once the library is more fully supported, the docs will be updated to include a /// detailed description of the guarantee. - pub fn new(seed: &[u8; 32], network: Network, logger: Arc, starting_time_secs: u64, starting_time_nanos: u32) -> KeysManager { + pub fn new(seed: &[u8; 32], network: Network, starting_time_secs: u64, starting_time_nanos: u32) -> KeysManager { let secp_ctx = Secp256k1::signing_only(); match ExtendedPrivKey::new_master(network.clone(), seed) { Ok(master_key) => { let node_secret = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(0).unwrap()).expect("Your RNG is busted").private_key.key; let destination_script = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(1).unwrap()) { Ok(destination_key) => { - let pubkey_hash160 = Hash160::hash(&ExtendedPubKey::from_private(&secp_ctx, &destination_key).public_key.key.serialize()[..]); + let wpubkey_hash = WPubkeyHash::hash(&ExtendedPubKey::from_private(&secp_ctx, &destination_key).public_key.to_bytes()); Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0) - .push_slice(&pubkey_hash160.into_inner()) + .push_slice(&wpubkey_hash.into_inner()) .into_script() }, Err(_) => panic!("Your RNG is busted"), @@ -546,7 +576,6 @@ impl KeysManager { channel_id_child_index: AtomicUsize::new(0), unique_start, - logger, } }, Err(_) => panic!("Your rng is busted"), @@ -598,15 +627,15 @@ impl KeysInterface for KeysManager { } let funding_key = key_step!(b"funding key", commitment_seed); let revocation_base_key = key_step!(b"revocation base key", funding_key); - let payment_base_key = key_step!(b"payment base key", revocation_base_key); - let delayed_payment_base_key = key_step!(b"delayed payment base key", payment_base_key); + let payment_key = key_step!(b"payment key", revocation_base_key); + let delayed_payment_base_key = key_step!(b"delayed payment base key", payment_key); let htlc_base_key = key_step!(b"HTLC base key", delayed_payment_base_key); InMemoryChannelKeys::new( &self.secp_ctx, funding_key, revocation_base_key, - payment_base_key, + payment_key, delayed_payment_base_key, htlc_base_key, commitment_seed,