X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Fchannel.rs;h=5446dd914353c23bd034f61e54e59e084461a939;hb=e79e61f595d7f3cf54390354148d5fd9edf7296d;hp=054eed0d4c4dbb9a2d6badb30ad8b8b2b7901de4;hpb=6185a2819090bd077954244c5e2adaab5efcaa1a;p=rust-lightning diff --git a/src/ln/channel.rs b/src/ln/channel.rs index 054eed0d..5446dd91 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -7,15 +7,12 @@ use bitcoin::util::hash::{Sha256dHash, Hash160}; use bitcoin::util::bip143; use bitcoin::network::serialize::BitcoinHash; -use num::FromPrimitive; - use secp256k1::key::{PublicKey,SecretKey}; use secp256k1::{Secp256k1,Message,Signature}; use secp256k1; use crypto::digest::Digest; use crypto::hkdf::{hkdf_extract,hkdf_expand}; -use crypto::sha2::Sha256; use ln::msgs; use ln::msgs::{HandleError, MsgEncodable}; @@ -24,9 +21,8 @@ use ln::channelmanager::PendingForwardHTLCInfo; use ln::chan_utils::{TxCreationKeys,HTLCOutputInCommitment}; use ln::chan_utils; use chain::chaininterface::{FeeEstimator,ConfirmationTarget}; -use util::transaction_utils; - -use rand::{thread_rng,Rng}; +use util::{transaction_utils,rng}; +use util::sha2::Sha256; use std::default::Default; use std::cmp; @@ -39,36 +35,39 @@ pub struct ChannelKeys { pub delayed_payment_base_key: SecretKey, pub htlc_base_key: SecretKey, pub channel_close_key: SecretKey, + pub channel_monitor_claim_key: SecretKey, pub commitment_seed: [u8; 32], } impl ChannelKeys { pub fn new_from_seed(seed: &[u8; 32]) -> Result { - let sha = Sha256::new(); let mut prk = [0; 32]; - hkdf_extract(sha, b"rust-lightning key gen salt", seed, &mut prk); + hkdf_extract(Sha256::new(), b"rust-lightning key gen salt", seed, &mut prk); let secp_ctx = Secp256k1::new(); let mut okm = [0; 32]; - hkdf_expand(sha, &prk, b"rust-lightning funding key info", &mut okm); - let funding_key = try!(SecretKey::from_slice(&secp_ctx, &okm)); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning funding key info", &mut okm); + let funding_key = SecretKey::from_slice(&secp_ctx, &okm)?; + + hkdf_expand(Sha256::new(), &prk, b"rust-lightning revocation base key info", &mut okm); + let revocation_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; - hkdf_expand(sha, &prk, b"rust-lightning revocation base key info", &mut okm); - let revocation_base_key = try!(SecretKey::from_slice(&secp_ctx, &okm)); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning payment base key info", &mut okm); + let payment_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; - hkdf_expand(sha, &prk, b"rust-lightning payment base key info", &mut okm); - let payment_base_key = try!(SecretKey::from_slice(&secp_ctx, &okm)); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning delayed payment base key info", &mut okm); + let delayed_payment_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; - hkdf_expand(sha, &prk, b"rust-lightning delayed payment base key info", &mut okm); - let delayed_payment_base_key = try!(SecretKey::from_slice(&secp_ctx, &okm)); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning htlc base key info", &mut okm); + let htlc_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; - hkdf_expand(sha, &prk, b"rust-lightning htlc base key info", &mut okm); - let htlc_base_key = try!(SecretKey::from_slice(&secp_ctx, &okm)); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning channel close key info", &mut okm); + let channel_close_key = SecretKey::from_slice(&secp_ctx, &okm)?; - hkdf_expand(sha, &prk, b"rust-lightning channel close key info", &mut okm); - let channel_close_key = try!(SecretKey::from_slice(&secp_ctx, &okm)); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning channel monitor claim key info", &mut okm); + let channel_monitor_claim_key = SecretKey::from_slice(&secp_ctx, &okm)?; - hkdf_expand(sha, &prk, b"rust-lightning local commitment seed info", &mut okm); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning local commitment seed info", &mut okm); Ok(ChannelKeys { funding_key: funding_key, @@ -77,6 +76,7 @@ impl ChannelKeys { delayed_payment_base_key: delayed_payment_base_key, htlc_base_key: htlc_base_key, channel_close_key: channel_close_key, + channel_monitor_claim_key: channel_monitor_claim_key, commitment_seed: okm }) } @@ -149,9 +149,22 @@ enum ChannelState { /// later. /// Flag is set on ChannelFunded. AwaitingRemoteRevoke = (1 << 7), + /// Flag which is set on ChannelFunded or FundingSent after receiving a shutdown message from + /// the remote end. If set, they may not add any new HTLCs to the channel, and we are expected + /// to respond with our own shutdown message when possible. + RemoteShutdownSent = (1 << 8), + /// Flag which is set on ChannelFunded or FundingSent after sending a shutdown message. At this + /// point, we may not add any new HTLCs to the channel. + /// TODO: Investigate some kind of timeout mechanism by which point the remote end must provide + /// us their shutdown. + LocalShutdownSent = (1 << 9), + /// We've successfully negotiated a closing_signed dance. At this point ChannelManager is about + /// to drop us, but we store this anyway. + ShutdownComplete = (1 << 10), } +const BOTH_SIDES_SHUTDOWN_MASK: u32 = (ChannelState::LocalShutdownSent as u32 | ChannelState::RemoteShutdownSent as u32); -// TODO: We should refactor this to be a Inbound/OutboundChannel until initial setup handshaking +// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking // has been completed, and then turn into a Channel to get compiler-time enforcement of things like // calling get_channel_id() before we're set up or things like get_outbound_funding_signed on an // inbound channel. @@ -177,6 +190,8 @@ pub struct Channel { channel_update_count: u32, feerate_per_kw: u64, + last_sent_closing_fee: Option<(u64, u64)>, // (feerate, fee) + /// The hash of the block in which the funding transaction reached our CONF_TARGET. We use this /// to detect unconfirmation after a serialize-unserialize roudtrip where we may not see a full /// series of block_connected/block_disconnected calls. Obviously this is not a guarantee as we @@ -208,10 +223,12 @@ pub struct Channel { their_cur_commitment_point: PublicKey, their_node_id: PublicKey, + their_shutdown_scriptpubkey: Option