@@ -338,6 +338,157 @@ mod inbound_payment {
338
338
}
339
339
}
340
340
341
+ /// LDK has multiple reasons to generate fake short channel ids:
342
+ /// 1) zero-conf channels that don't have a confirmed channel id yet
343
+ /// 2) phantom node payments, to get an scid for the phantom node's phantom channel
344
+ mod fake_scid {
345
+ use bitcoin:: blockdata:: constants:: genesis_block;
346
+ use bitcoin:: hash_types:: BlockHash ;
347
+ use bitcoin:: network:: constants:: Network ;
348
+ use chain:: keysinterface:: { Sign , KeysInterface } ;
349
+ use ln:: channelmanager:: DecodeError ;
350
+ use util:: scid_utils;
351
+ use util:: ser:: Readable ;
352
+
353
+ use core:: convert:: TryInto ;
354
+ use core:: ops:: Deref ;
355
+
356
+ const TEST_SEGWIT_ACTIVATION_HEIGHT : u32 = 0 ;
357
+ const MAINNET_SEGWIT_ACTIVATION_HEIGHT : u32 = 481_824 ;
358
+ const MAX_TX_INDEX : u32 = 2_500 ;
359
+ const MAX_VOUT : u16 = 10_000 ;
360
+ const MAX_NAMESPACES : u8 = 8 ;
361
+ const NAMESPACE_ID_BITMASK : u8 = 0b111 ;
362
+
363
+ /// Fake scids are divided into namespaces, with each namespace having its own randomly-selected
364
+ /// identifier between [0..7], such that `scid_tx_index % MAX_NAMESPACES == namespace_identifier`.
365
+ /// This allows us to identify what namespace a fake scid corresponds to upon HTLC receipt, and
366
+ /// handle the HTLC accordingly.
367
+ pub ( super ) enum Namespace {
368
+ Phantom ( u8 ) ,
369
+ // Coming soon: a variant for the zero-conf scid namespace
370
+ }
371
+
372
+ impl Namespace {
373
+ /// We generate "realistic-looking" random scids here, meaning the scid's block height is
374
+ /// between segwit activation and the current best known height, and the tx index and output
375
+ /// index are also selected from a "reasonable" range. We add this logic because it makes it
376
+ /// non-obvious at a glance that the scid is fake, e.g. if it appears in invoice route hints.
377
+ pub ( super ) fn get_fake_scid < Signer : Sign , K : Deref > ( & self , highest_seen_blockheight : u32 , genesis_hash : & BlockHash , keys_manager : & K ) -> u64
378
+ where K :: Target : KeysInterface < Signer = Signer > ,
379
+ {
380
+ let scid_namespace = self . as_u8 ( ) ;
381
+ let rand_bytes = keys_manager. get_secure_random_bytes ( ) ;
382
+
383
+ let valid_block_range = highest_seen_blockheight - segwit_activation_height ( genesis_hash) ;
384
+ let rand_value_for_height = u32:: from_be_bytes ( rand_bytes[ ..4 ] . try_into ( ) . unwrap ( ) ) ;
385
+ let fake_scid_height = segwit_activation_height ( genesis_hash) + rand_value_for_height % valid_block_range;
386
+
387
+ let rand_i32 = i32:: from_be_bytes ( rand_bytes[ 4 ..8 ] . try_into ( ) . unwrap ( ) ) . abs ( ) ;
388
+ let rand_tx_index = rand_i32 % ( MAX_TX_INDEX as i32 ) ;
389
+ let offset = ( scid_namespace as i32 - rand_tx_index) % ( MAX_NAMESPACES as i32 ) ;
390
+ let fake_scid_tx_index: u64 = ( rand_tx_index + offset) as u64 ;
391
+
392
+ let rand_value_for_vout = u16:: from_be_bytes ( rand_bytes[ 8 ..10 ] . try_into ( ) . unwrap ( ) ) ;
393
+ let fake_scid_vout_index = rand_value_for_vout % MAX_VOUT ;
394
+ scid_utils:: scid_from_parts ( fake_scid_height as u64 , fake_scid_tx_index, fake_scid_vout_index as u64 ) . unwrap ( )
395
+ }
396
+
397
+ fn as_u8 ( & self ) -> u8 {
398
+ match self {
399
+ Namespace :: Phantom ( namespace) => * namespace,
400
+ }
401
+ }
402
+
403
+ pub ( super ) fn phantom < Signer : Sign , K : Deref > ( keys_manager : & K ) -> Namespace
404
+ where K :: Target : KeysInterface < Signer = Signer > ,
405
+ {
406
+ let phantom_scid_namespace_rand_bytes = keys_manager. get_secure_random_bytes ( ) ;
407
+ let phantom_scid_namespace = phantom_scid_namespace_rand_bytes[ 0 ] & NAMESPACE_ID_BITMASK ;
408
+ Namespace :: Phantom ( phantom_scid_namespace)
409
+ }
410
+ }
411
+
412
+ fn segwit_activation_height ( genesis : & BlockHash ) -> u32 {
413
+ if genesis_block ( Network :: Bitcoin ) . header . block_hash ( ) == * genesis {
414
+ MAINNET_SEGWIT_ACTIVATION_HEIGHT
415
+ } else {
416
+ TEST_SEGWIT_ACTIVATION_HEIGHT
417
+ }
418
+ }
419
+
420
+ /// Returns whether the given fake scid falls into the given namespace.
421
+ pub ( super ) fn is_valid ( namespace : & Namespace , scid : u64 ) -> bool {
422
+ scid_utils:: tx_index_from_scid ( & scid) % MAX_NAMESPACES as u32 == namespace. as_u8 ( ) as u32
423
+ }
424
+
425
+ impl_writeable_tlv_based_enum ! ( Namespace , ;
426
+ ( 0 , Phantom ) ,
427
+ ) ;
428
+
429
+ #[ cfg( test) ]
430
+ mod tests {
431
+ use bitcoin:: blockdata:: constants:: genesis_block;
432
+ use bitcoin:: network:: constants:: Network ;
433
+ use ln:: channelmanager:: fake_scid:: { is_valid, MAINNET_SEGWIT_ACTIVATION_HEIGHT , MAX_TX_INDEX , MAX_VOUT , Namespace , NAMESPACE_ID_BITMASK , segwit_activation_height, TEST_SEGWIT_ACTIVATION_HEIGHT } ;
434
+ use util:: scid_utils;
435
+ use util:: test_utils;
436
+ use sync:: Arc ;
437
+
438
+ #[ test]
439
+ fn namespace_identifier_is_within_range ( ) {
440
+ let seed = [ 0 as u8 ; 32 ] ;
441
+ let keys_manager = Arc :: new ( test_utils:: TestKeysInterface :: new ( & seed, Network :: Testnet ) ) ;
442
+ let Namespace :: Phantom ( ns) = Namespace :: phantom ( & keys_manager) ;
443
+ assert ! ( ns <= NAMESPACE_ID_BITMASK ) ;
444
+ }
445
+
446
+ #[ test]
447
+ fn test_segwit_activation_height ( ) {
448
+ let mainnet_genesis = genesis_block ( Network :: Bitcoin ) . header . block_hash ( ) ;
449
+ assert_eq ! ( segwit_activation_height( & mainnet_genesis) , MAINNET_SEGWIT_ACTIVATION_HEIGHT ) ;
450
+
451
+ let testnet_genesis = genesis_block ( Network :: Testnet ) . header . block_hash ( ) ;
452
+ assert_eq ! ( segwit_activation_height( & testnet_genesis) , TEST_SEGWIT_ACTIVATION_HEIGHT ) ;
453
+
454
+ let signet_genesis = genesis_block ( Network :: Signet ) . header . block_hash ( ) ;
455
+ assert_eq ! ( segwit_activation_height( & signet_genesis) , TEST_SEGWIT_ACTIVATION_HEIGHT ) ;
456
+
457
+ let regtest_genesis = genesis_block ( Network :: Regtest ) . header . block_hash ( ) ;
458
+ assert_eq ! ( segwit_activation_height( & regtest_genesis) , TEST_SEGWIT_ACTIVATION_HEIGHT ) ;
459
+ }
460
+
461
+ #[ test]
462
+ fn test_is_valid ( ) {
463
+ let namespace = Namespace :: Phantom ( 3 ) ;
464
+ let valid_fake_scid = scid_utils:: scid_from_parts ( 0 , 11 , 0 ) . unwrap ( ) ;
465
+ assert ! ( is_valid( & namespace, valid_fake_scid) ) ;
466
+ let invalid_fake_scid = scid_utils:: scid_from_parts ( 0 , 12 , 0 ) . unwrap ( ) ;
467
+ assert ! ( !is_valid( & namespace, invalid_fake_scid) ) ;
468
+ }
469
+
470
+ #[ test]
471
+ fn test_get_fake_scid ( ) {
472
+ let mainnet_genesis = genesis_block ( Network :: Bitcoin ) . header . block_hash ( ) ;
473
+ let seed = [ 0 as u8 ; 32 ] ;
474
+ let keys_manager = Arc :: new ( test_utils:: TestKeysInterface :: new ( & seed, Network :: Testnet ) ) ;
475
+ let namespace = Namespace :: phantom ( & keys_manager) ;
476
+ let fake_scid = namespace. get_fake_scid ( 500_000 , & mainnet_genesis, & keys_manager) ;
477
+
478
+ let fake_height = scid_utils:: block_from_scid ( & fake_scid) ;
479
+ assert ! ( fake_height >= MAINNET_SEGWIT_ACTIVATION_HEIGHT ) ;
480
+ assert ! ( fake_height <= 500_000 ) ;
481
+
482
+ let fake_tx_index = scid_utils:: tx_index_from_scid ( & fake_scid) ;
483
+ assert ! ( fake_tx_index <= MAX_TX_INDEX ) ;
484
+
485
+ let fake_vout = fake_scid & scid_utils:: MAX_SCID_VOUT_INDEX ;
486
+ assert ! ( fake_vout <= MAX_VOUT . into( ) ) ;
487
+ }
488
+ }
489
+ }
490
+
491
+
341
492
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
342
493
//
343
494
// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
@@ -960,6 +1111,8 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
960
1111
961
1112
inbound_payment_key : inbound_payment:: ExpandedKey ,
962
1113
1114
+ phantom_scid_namespace : fake_scid:: Namespace ,
1115
+
963
1116
/// Used to track the last value sent in a node_announcement "timestamp" field. We ensure this
964
1117
/// value increases strictly since we don't assume access to a time source.
965
1118
last_node_announcement_serial : AtomicUsize ,
@@ -1655,6 +1808,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1655
1808
secp_ctx. seeded_randomize ( & keys_manager. get_secure_random_bytes ( ) ) ;
1656
1809
let inbound_pmt_key_material = keys_manager. get_inbound_payment_key_material ( ) ;
1657
1810
let expanded_inbound_key = inbound_payment:: ExpandedKey :: new ( & inbound_pmt_key_material) ;
1811
+ let phantom_scid_namespace = fake_scid:: Namespace :: phantom ( & keys_manager) ;
1658
1812
ChannelManager {
1659
1813
default_configuration : config. clone ( ) ,
1660
1814
genesis_hash : genesis_block ( params. network ) . header . block_hash ( ) ,
@@ -1680,6 +1834,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1680
1834
1681
1835
inbound_payment_key : expanded_inbound_key,
1682
1836
1837
+ phantom_scid_namespace,
1838
+
1683
1839
last_node_announcement_serial : AtomicUsize :: new ( 0 ) ,
1684
1840
highest_seen_timestamp : AtomicUsize :: new ( 0 ) ,
1685
1841
@@ -6145,6 +6301,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
6145
6301
write_tlv_fields ! ( writer, {
6146
6302
( 1 , pending_outbound_payments_no_retry, required) ,
6147
6303
( 3 , pending_outbound_payments, required) ,
6304
+ ( 5 , self . phantom_scid_namespace, required) ,
6148
6305
} ) ;
6149
6306
6150
6307
Ok ( ( ) )
@@ -6439,10 +6596,16 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
6439
6596
// pending_outbound_payments_no_retry is for compatibility with 0.0.101 clients.
6440
6597
let mut pending_outbound_payments_no_retry: Option < HashMap < PaymentId , HashSet < [ u8 ; 32 ] > > > = None ;
6441
6598
let mut pending_outbound_payments = None ;
6599
+ let mut phantom_scid_namespace: Option < fake_scid:: Namespace > = None ;
6442
6600
read_tlv_fields ! ( reader, {
6443
6601
( 1 , pending_outbound_payments_no_retry, option) ,
6444
6602
( 3 , pending_outbound_payments, option) ,
6603
+ ( 5 , phantom_scid_namespace, option) ,
6445
6604
} ) ;
6605
+ if phantom_scid_namespace. is_none ( ) {
6606
+ phantom_scid_namespace = Some ( fake_scid:: Namespace :: phantom ( & args. keys_manager ) ) ;
6607
+ }
6608
+
6446
6609
if pending_outbound_payments. is_none ( ) && pending_outbound_payments_no_retry. is_none ( ) {
6447
6610
pending_outbound_payments = Some ( pending_outbound_payments_compat) ;
6448
6611
} else if pending_outbound_payments. is_none ( ) {
@@ -6525,6 +6688,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
6525
6688
inbound_payment_key : expanded_inbound_key,
6526
6689
pending_inbound_payments : Mutex :: new ( pending_inbound_payments) ,
6527
6690
pending_outbound_payments : Mutex :: new ( pending_outbound_payments. unwrap ( ) ) ,
6691
+ phantom_scid_namespace : phantom_scid_namespace. unwrap ( ) ,
6528
6692
6529
6693
our_network_key : args. keys_manager . get_node_secret ( ) ,
6530
6694
our_network_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & args. keys_manager . get_node_secret ( ) ) ,
0 commit comments