X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fscid_utils.rs;h=19e2f5527ccbcbcb7c997e8f628ef4cf292974e3;hb=c3de782a2baab0c742332bea4783c393edb83311;hp=676c303bfa8dc1384b404cfe355aa77d99990242;hpb=168b3a51ae024e3abf73112c2acf9f60523c5674;p=rust-lightning diff --git a/lightning/src/util/scid_utils.rs b/lightning/src/util/scid_utils.rs index 676c303b..19e2f552 100644 --- a/lightning/src/util/scid_utils.rs +++ b/lightning/src/util/scid_utils.rs @@ -20,7 +20,7 @@ pub const MAX_SCID_TX_INDEX: u64 = 0x00ffffff; pub const MAX_SCID_VOUT_INDEX: u64 = 0xffff; /// A `short_channel_id` construction error -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Eq)] pub enum ShortChannelIdError { BlockOverflow, TxIndexOverflow, @@ -63,17 +63,19 @@ pub fn scid_from_parts(block: u64, tx_index: u64, vout_index: u64) -> Result(&self, highest_seen_blockheight: u32, genesis_hash: &BlockHash, fake_scid_rand_bytes: &[u8; 32], keys_manager: &K) -> u64 - where K::Target: KeysInterface, + pub(crate) fn get_fake_scid(&self, highest_seen_blockheight: u32, genesis_hash: &BlockHash, fake_scid_rand_bytes: &[u8; 32], entropy_source: &ES) -> u64 + where ES::Target: EntropySource, { // Ensure we haven't created a namespace that doesn't fit into the 3 bits we've allocated for // namespaces. assert!((*self as u8) < MAX_NAMESPACES); - let rand_bytes = keys_manager.get_secure_random_bytes(); + let rand_bytes = entropy_source.get_secure_random_bytes(); let segwit_activation_height = segwit_activation_height(genesis_hash); let mut blocks_since_segwit_activation = highest_seen_blockheight.saturating_sub(segwit_activation_height); @@ -150,29 +153,44 @@ pub(crate) mod fake_scid { } } - /// Returns whether the given fake scid falls into the given namespace. - pub fn is_valid_phantom(fake_scid_rand_bytes: &[u8; 32], scid: u64) -> bool { + /// Returns whether the given fake scid falls into the phantom namespace. + pub fn is_valid_phantom(fake_scid_rand_bytes: &[u8; 32], scid: u64, genesis_hash: &BlockHash) -> bool { let block_height = scid_utils::block_from_scid(&scid); let tx_index = scid_utils::tx_index_from_scid(&scid); let namespace = Namespace::Phantom; let valid_vout = namespace.get_encrypted_vout(block_height, tx_index, fake_scid_rand_bytes); - valid_vout == scid_utils::vout_from_scid(&scid) as u8 + block_height >= segwit_activation_height(genesis_hash) + && valid_vout == scid_utils::vout_from_scid(&scid) as u8 + } + + /// Returns whether the given fake scid falls into the intercept namespace. + pub fn is_valid_intercept(fake_scid_rand_bytes: &[u8; 32], scid: u64, genesis_hash: &BlockHash) -> bool { + let block_height = scid_utils::block_from_scid(&scid); + let tx_index = scid_utils::tx_index_from_scid(&scid); + let namespace = Namespace::Intercept; + let valid_vout = namespace.get_encrypted_vout(block_height, tx_index, fake_scid_rand_bytes); + block_height >= segwit_activation_height(genesis_hash) + && valid_vout == scid_utils::vout_from_scid(&scid) as u8 } #[cfg(test)] mod tests { use bitcoin::blockdata::constants::genesis_block; use bitcoin::network::constants::Network; - use util::scid_utils::fake_scid::{is_valid_phantom, MAINNET_SEGWIT_ACTIVATION_HEIGHT, MAX_TX_INDEX, MAX_NAMESPACES, Namespace, NAMESPACE_ID_BITMASK, segwit_activation_height, TEST_SEGWIT_ACTIVATION_HEIGHT}; - use util::scid_utils; - use util::test_utils; - use sync::Arc; + use crate::util::scid_utils::fake_scid::{is_valid_intercept, is_valid_phantom, MAINNET_SEGWIT_ACTIVATION_HEIGHT, MAX_TX_INDEX, MAX_NAMESPACES, Namespace, NAMESPACE_ID_BITMASK, segwit_activation_height, TEST_SEGWIT_ACTIVATION_HEIGHT}; + use crate::util::scid_utils; + use crate::util::test_utils; + use crate::sync::Arc; #[test] fn namespace_identifier_is_within_range() { let phantom_namespace = Namespace::Phantom; assert!((phantom_namespace as u8) < MAX_NAMESPACES); assert!((phantom_namespace as u8) <= NAMESPACE_ID_BITMASK); + + let intercept_namespace = Namespace::Intercept; + assert!((intercept_namespace as u8) < MAX_NAMESPACES); + assert!((intercept_namespace as u8) <= NAMESPACE_ID_BITMASK); } #[test] @@ -194,11 +212,24 @@ pub(crate) mod fake_scid { fn test_is_valid_phantom() { let namespace = Namespace::Phantom; let fake_scid_rand_bytes = [0; 32]; + let testnet_genesis = genesis_block(Network::Testnet).header.block_hash(); + let valid_encrypted_vout = namespace.get_encrypted_vout(0, 0, &fake_scid_rand_bytes); + let valid_fake_scid = scid_utils::scid_from_parts(1, 0, valid_encrypted_vout as u64).unwrap(); + assert!(is_valid_phantom(&fake_scid_rand_bytes, valid_fake_scid, &testnet_genesis)); + let invalid_fake_scid = scid_utils::scid_from_parts(1, 0, 12).unwrap(); + assert!(!is_valid_phantom(&fake_scid_rand_bytes, invalid_fake_scid, &testnet_genesis)); + } + + #[test] + fn test_is_valid_intercept() { + let namespace = Namespace::Intercept; + let fake_scid_rand_bytes = [0; 32]; + let testnet_genesis = genesis_block(Network::Testnet).header.block_hash(); let valid_encrypted_vout = namespace.get_encrypted_vout(0, 0, &fake_scid_rand_bytes); - let valid_fake_scid = scid_utils::scid_from_parts(0, 0, valid_encrypted_vout as u64).unwrap(); - assert!(is_valid_phantom(&fake_scid_rand_bytes, valid_fake_scid)); - let invalid_fake_scid = scid_utils::scid_from_parts(0, 0, 12).unwrap(); - assert!(!is_valid_phantom(&fake_scid_rand_bytes, invalid_fake_scid)); + let valid_fake_scid = scid_utils::scid_from_parts(1, 0, valid_encrypted_vout as u64).unwrap(); + assert!(is_valid_intercept(&fake_scid_rand_bytes, valid_fake_scid, &testnet_genesis)); + let invalid_fake_scid = scid_utils::scid_from_parts(1, 0, 12).unwrap(); + assert!(!is_valid_intercept(&fake_scid_rand_bytes, invalid_fake_scid, &testnet_genesis)); } #[test]