X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fonion_utils.rs;h=52e7d2fefc882d955166d717730c0990b56f4f0f;hb=5bd7f24634a983326924f200bfff7c7c0fa56601;hp=ebea1334a8489bc81000b0df1e272ad4557fab73;hpb=6d1bd8bc988429a1c26e6fa0030735f9e844d022;p=rust-lightning diff --git a/lightning/src/ln/onion_utils.rs b/lightning/src/ln/onion_utils.rs index ebea1334..52e7d2fe 100644 --- a/lightning/src/ln/onion_utils.rs +++ b/lightning/src/ln/onion_utils.rs @@ -1,21 +1,21 @@ use ln::channelmanager::{PaymentHash, PaymentSecret, HTLCSource}; use ln::msgs; -use ln::router::{Route,RouteHop}; +use routing::router::RouteHop; use util::byte_utils; use util::chacha20::ChaCha20; use util::errors::{self, APIError}; use util::ser::{Readable, Writeable, LengthCalculatingWriter}; use util::logger::{Logger, LogHolder}; -use bitcoin_hashes::{Hash, HashEngine}; -use bitcoin_hashes::cmp::fixed_time_eq; -use bitcoin_hashes::hmac::{Hmac, HmacEngine}; -use bitcoin_hashes::sha256::Hash as Sha256; +use bitcoin::hashes::{Hash, HashEngine}; +use bitcoin::hashes::cmp::fixed_time_eq; +use bitcoin::hashes::hmac::{Hmac, HmacEngine}; +use bitcoin::hashes::sha256::Hash as Sha256; -use secp256k1::key::{SecretKey,PublicKey}; -use secp256k1::Secp256k1; -use secp256k1::ecdh::SharedSecret; -use secp256k1; +use bitcoin::secp256k1::key::{SecretKey,PublicKey}; +use bitcoin::secp256k1::Secp256k1; +use bitcoin::secp256k1::ecdh::SharedSecret; +use bitcoin::secp256k1; use std::io::Cursor; use std::sync::Arc; @@ -63,11 +63,11 @@ pub(super) fn gen_ammag_from_shared_secret(shared_secret: &[u8]) -> [u8; 32] { // can only fail if an intermediary hop has an invalid public key or session_priv is invalid #[inline] -pub(super) fn construct_onion_keys_callback (secp_ctx: &Secp256k1, route: &Route, session_priv: &SecretKey, mut callback: FType) -> Result<(), secp256k1::Error> { +pub(super) fn construct_onion_keys_callback (secp_ctx: &Secp256k1, path: &Vec, session_priv: &SecretKey, mut callback: FType) -> Result<(), secp256k1::Error> { let mut blinded_priv = session_priv.clone(); let mut blinded_pub = PublicKey::from_secret_key(secp_ctx, &blinded_priv); - for hop in route.hops.iter() { + for hop in path.iter() { let shared_secret = SharedSecret::new(&hop.pubkey, &blinded_priv); let mut sha = Sha256::engine(); @@ -87,10 +87,10 @@ pub(super) fn construct_onion_keys_callback(secp_ctx: &Secp256k1, route: &Route, session_priv: &SecretKey) -> Result, secp256k1::Error> { - let mut res = Vec::with_capacity(route.hops.len()); +pub(super) fn construct_onion_keys(secp_ctx: &Secp256k1, path: &Vec, session_priv: &SecretKey) -> Result, secp256k1::Error> { + let mut res = Vec::with_capacity(path.len()); - construct_onion_keys_callback(secp_ctx, route, session_priv, |shared_secret, _blinding_factor, ephemeral_pubkey, _| { + construct_onion_keys_callback(secp_ctx, path, session_priv, |shared_secret, _blinding_factor, ephemeral_pubkey, _| { let (rho, mu) = gen_rho_mu_from_shared_secret(&shared_secret[..]); res.push(OnionKeys { @@ -108,13 +108,13 @@ pub(super) fn construct_onion_keys(secp_ctx: &Secp256k1, starting_htlc_offset: u32) -> Result<(Vec, u64, u32), APIError> { +pub(super) fn build_onion_payloads(path: &Vec, total_msat: u64, payment_secret_option: &Option, starting_htlc_offset: u32) -> Result<(Vec, u64, u32), APIError> { let mut cur_value_msat = 0u64; let mut cur_cltv = starting_htlc_offset; let mut last_short_channel_id = 0; - let mut res: Vec = Vec::with_capacity(route.hops.len()); + let mut res: Vec = Vec::with_capacity(path.len()); - for (idx, hop) in route.hops.iter().rev().enumerate() { + for (idx, hop) in path.iter().rev().enumerate() { // First hop gets special values so that it can check, on receipt, that everything is // exactly as it should be (and the next hop isn't trying to probe to find out if we're // the intended recipient). @@ -127,7 +127,7 @@ pub(super) fn build_onion_payloads(route: &Route, payment_secret_option: &Option payment_data: if let &Some(ref payment_secret) = payment_secret_option { Some(msgs::FinalOnionHopData { payment_secret: payment_secret.clone(), - total_msat: hop.fee_msat, + total_msat, }) } else { None }, } @@ -317,16 +317,18 @@ pub(super) fn build_first_hop_failure_packet(shared_secret: &[u8], failure_type: /// Process failure we got back from upstream on a payment we sent (implying htlc_source is an /// OutboundRoute). /// Returns update, a boolean indicating that the payment itself failed, and the error code. -pub(super) fn process_onion_failure(secp_ctx: &Secp256k1, logger: &Arc, htlc_source: &HTLCSource, mut packet_decrypted: Vec) -> (Option, bool, Option) { - if let &HTLCSource::OutboundRoute { ref route, ref session_priv, ref first_hop_htlc_msat } = htlc_source { +#[inline] +pub(super) fn process_onion_failure(secp_ctx: &Secp256k1, logger: &Arc, htlc_source: &HTLCSource, mut packet_decrypted: Vec) -> (Option, bool, Option, Option>) { + if let &HTLCSource::OutboundRoute { ref path, ref session_priv, ref first_hop_htlc_msat } = htlc_source { let mut res = None; let mut htlc_msat = *first_hop_htlc_msat; let mut error_code_ret = None; + let mut error_packet_ret = None; let mut next_route_hop_ix = 0; let mut is_from_final_node = false; // Handle packed channel/node updates for passing back for the route handler - construct_onion_keys_callback(secp_ctx, route, session_priv, |shared_secret, _, _, route_hop| { + construct_onion_keys_callback(secp_ctx, path, session_priv, |shared_secret, _, _, route_hop| { next_route_hop_ix += 1; if res.is_some() { return; } @@ -341,7 +343,7 @@ pub(super) fn process_onion_failure(secp_ctx: &Secp256k1< chacha.process(&packet_decrypted, &mut decryption_tmp[..]); packet_decrypted = decryption_tmp; - is_from_final_node = route.hops.last().unwrap().pubkey == route_hop.pubkey; + is_from_final_node = path.last().unwrap().pubkey == route_hop.pubkey; if let Ok(err_packet) = msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(&packet_decrypted)) { let um = gen_um_from_shared_secret(&shared_secret[..]); @@ -356,6 +358,7 @@ pub(super) fn process_onion_failure(secp_ctx: &Secp256k1< let error_code = byte_utils::slice_to_be16(&error_code_slice); error_code_ret = Some(error_code); + error_packet_ret = Some(err_packet.failuremsg[2..].to_vec()); let (debug_field, debug_field_size) = errors::get_onion_debug_field(error_code); @@ -374,7 +377,7 @@ pub(super) fn process_onion_failure(secp_ctx: &Secp256k1< } else if error_code & PERM == PERM { fail_channel_update = if payment_failed {None} else {Some(msgs::HTLCFailChannelUpdate::ChannelClosed { - short_channel_id: route.hops[next_route_hop_ix - if next_route_hop_ix == route.hops.len() { 1 } else { 0 }].short_channel_id, + short_channel_id: path[next_route_hop_ix - if next_route_hop_ix == path.len() { 1 } else { 0 }].short_channel_id, is_permanent: true, })}; } @@ -456,11 +459,11 @@ pub(super) fn process_onion_failure(secp_ctx: &Secp256k1< } }).expect("Route that we sent via spontaneously grew invalid keys in the middle of it?"); if let Some((channel_update, payment_retryable)) = res { - (channel_update, payment_retryable, error_code_ret) + (channel_update, payment_retryable, error_code_ret, error_packet_ret) } else { // only not set either packet unparseable or hmac does not match with any // payment not retryable only when garbage is from the final node - (None, !is_from_final_node, None) + (None, !is_from_final_node, None, None) } } else { unreachable!(); } } @@ -469,14 +472,14 @@ pub(super) fn process_onion_failure(secp_ctx: &Secp256k1< mod tests { use ln::channelmanager::PaymentHash; use ln::features::{ChannelFeatures, NodeFeatures}; - use ln::router::{Route, RouteHop}; + use routing::router::{Route, RouteHop}; use ln::msgs; use util::ser::{Writeable, Writer}; use hex; - use secp256k1::Secp256k1; - use secp256k1::key::{PublicKey,SecretKey}; + use bitcoin::secp256k1::Secp256k1; + use bitcoin::secp256k1::key::{PublicKey,SecretKey}; use super::OnionKeys; @@ -485,7 +488,7 @@ mod tests { let secp_ctx = Secp256k1::new(); let route = Route { - hops: vec!( + paths: vec![vec![ RouteHop { pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(), channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(), @@ -511,13 +514,13 @@ mod tests { channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(), short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually }, - ), + ]], }; let session_priv = SecretKey::from_slice(&hex::decode("4141414141414141414141414141414141414141414141414141414141414141").unwrap()[..]).unwrap(); - let onion_keys = super::construct_onion_keys(&secp_ctx, &route, &session_priv).unwrap(); - assert_eq!(onion_keys.len(), route.hops.len()); + let onion_keys = super::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap(); + assert_eq!(onion_keys.len(), route.paths[0].len()); onion_keys }