@@ -114,9 +114,13 @@ impl LocalFeatures {
114
114
}
115
115
116
116
/// Tracks globalfeatures which are in init messages and routing announcements
117
- #[ derive( Clone , PartialEq ) ]
117
+ #[ derive( Clone , PartialEq , Debug ) ]
118
118
pub struct GlobalFeatures {
119
+ #[ cfg( not( test) ) ]
119
120
flags : Vec < u8 > ,
121
+ // Used to test encoding of diverse msgs
122
+ #[ cfg( test) ]
123
+ pub flags : Vec < u8 >
120
124
}
121
125
122
126
impl GlobalFeatures {
@@ -326,7 +330,7 @@ pub struct ChannelReestablish {
326
330
}
327
331
328
332
/// An announcement_signatures message to be sent or received from a peer
329
- #[ derive( Clone ) ]
333
+ #[ derive( PartialEq , Clone , Debug ) ]
330
334
pub struct AnnouncementSignatures {
331
335
pub ( crate ) channel_id : [ u8 ; 32 ] ,
332
336
pub ( crate ) short_channel_id : u64 ,
@@ -335,7 +339,7 @@ pub struct AnnouncementSignatures {
335
339
}
336
340
337
341
/// An address which can be used to connect to a remote peer
338
- #[ derive( PartialEq , Clone ) ]
342
+ #[ derive( Clone , PartialEq , Debug ) ]
339
343
pub enum NetAddress {
340
344
/// An IPv4 address/port on which the peer is listening.
341
345
IPv4 {
@@ -458,9 +462,9 @@ impl<R: ::std::io::Read> Readable<R> for Result<NetAddress, u8> {
458
462
}
459
463
}
460
464
461
- #[ derive( PartialEq , Clone ) ]
462
465
// Only exposed as broadcast of node_announcement should be filtered by node_id
463
466
/// The unsigned part of a node_announcement
467
+ #[ derive( PartialEq , Clone , Debug ) ]
464
468
pub struct UnsignedNodeAnnouncement {
465
469
pub ( crate ) features : GlobalFeatures ,
466
470
pub ( crate ) timestamp : u32 ,
@@ -484,7 +488,7 @@ pub struct NodeAnnouncement {
484
488
485
489
// Only exposed as broadcast of channel_announcement should be filtered by node_id
486
490
/// The unsigned part of a channel_announcement
487
- #[ derive( PartialEq , Clone ) ]
491
+ #[ derive( PartialEq , Clone , Debug ) ]
488
492
pub struct UnsignedChannelAnnouncement {
489
493
pub ( crate ) features : GlobalFeatures ,
490
494
pub ( crate ) chain_hash : Sha256dHash ,
@@ -498,7 +502,7 @@ pub struct UnsignedChannelAnnouncement {
498
502
pub ( crate ) excess_data : Vec < u8 > ,
499
503
}
500
504
/// A channel_announcement message to be sent or received from a peer
501
- #[ derive( PartialEq , Clone ) ]
505
+ #[ derive( PartialEq , Clone , Debug ) ]
502
506
pub struct ChannelAnnouncement {
503
507
pub ( crate ) node_signature_1 : Signature ,
504
508
pub ( crate ) node_signature_2 : Signature ,
@@ -507,7 +511,7 @@ pub struct ChannelAnnouncement {
507
511
pub ( crate ) contents : UnsignedChannelAnnouncement ,
508
512
}
509
513
510
- #[ derive( PartialEq , Clone ) ]
514
+ #[ derive( PartialEq , Clone , Debug ) ]
511
515
pub ( crate ) struct UnsignedChannelUpdate {
512
516
pub ( crate ) chain_hash : Sha256dHash ,
513
517
pub ( crate ) short_channel_id : u64 ,
@@ -520,7 +524,7 @@ pub(crate) struct UnsignedChannelUpdate {
520
524
pub ( crate ) excess_data : Vec < u8 > ,
521
525
}
522
526
/// A channel_update message to be sent or received from a peer
523
- #[ derive( PartialEq , Clone ) ]
527
+ #[ derive( PartialEq , Clone , Debug ) ]
524
528
pub struct ChannelUpdate {
525
529
pub ( crate ) signature : Signature ,
526
530
pub ( crate ) contents : UnsignedChannelUpdate ,
@@ -1388,10 +1392,13 @@ impl_writeable_len_match!(NodeAnnouncement, {
1388
1392
mod tests {
1389
1393
use hex;
1390
1394
use ln:: msgs;
1391
- use ln:: msgs:: OptionalField ;
1395
+ use ln:: msgs:: { GlobalFeatures , OptionalField } ;
1392
1396
use util:: ser:: Writeable ;
1397
+
1398
+ use bitcoin:: util:: hash:: Sha256dHash ;
1399
+
1393
1400
use secp256k1:: key:: { PublicKey , SecretKey } ;
1394
- use secp256k1:: Secp256k1 ;
1401
+ use secp256k1:: { Secp256k1 , Message } ;
1395
1402
1396
1403
#[ test]
1397
1404
fn encoding_channel_reestablish_no_secret ( ) {
@@ -1429,4 +1436,160 @@ mod tests {
1429
1436
vec![ 4 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 6 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 4 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 3 , 27 , 132 , 197 , 86 , 123 , 18 , 100 , 64 , 153 , 93 , 62 , 213 , 170 , 186 , 5 , 101 , 215 , 30 , 24 , 52 , 96 , 72 , 25 , 255 , 156 , 23 , 245 , 233 , 213 , 221 , 7 , 143 ]
1430
1437
) ;
1431
1438
}
1439
+
1440
+ macro_rules! get_keys_from {
1441
+ ( $slice: expr) => {
1442
+ {
1443
+ let secp_ctx = Secp256k1 :: new( ) ;
1444
+ let privkey = SecretKey :: from_slice( & hex:: decode( $slice) . unwrap( ) [ ..] ) . unwrap( ) ;
1445
+ let pubkey = PublicKey :: from_secret_key( & secp_ctx, & privkey) ;
1446
+ ( privkey, pubkey, secp_ctx)
1447
+ }
1448
+ }
1449
+ }
1450
+
1451
+ macro_rules! get_sig_on {
1452
+ ( $privkey: expr, $ctx: expr, $string: expr) => {
1453
+ {
1454
+ let sighash = Message :: from_slice( & $string. into_bytes( ) [ ..] ) . unwrap( ) ;
1455
+ $ctx. sign( & sighash, & $privkey)
1456
+ }
1457
+ }
1458
+ }
1459
+
1460
+ #[ test]
1461
+ fn encoding_announcement_signatures ( ) {
1462
+ let ( privkey, _, secp_ctx) = get_keys_from ! ( "0101010101010101010101010101010101010101010101010101010101010101" ) ;
1463
+ let sig_1 = get_sig_on ! ( privkey, secp_ctx, String :: from( "01010101010101010101010101010101" ) ) ;
1464
+ let sig_2 = get_sig_on ! ( privkey, secp_ctx, String :: from( "02020202020202020202020202020202" ) ) ;
1465
+ let announcement_signatures = msgs:: AnnouncementSignatures {
1466
+ channel_id : [ 4 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 6 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ,
1467
+ short_channel_id : 2316138423780173 ,
1468
+ node_signature : sig_1,
1469
+ bitcoin_signature : sig_2,
1470
+ } ;
1471
+
1472
+ let encoded_value = announcement_signatures. encode ( ) ;
1473
+ assert_eq ! ( encoded_value, hex:: decode( "040000000000000005000000000000000600000000000000070000000000000000083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073acf9953cef4700860f5967838eba2bae89288ad188ebf8b20bf995c3ea53a26df1876d0a3a0e13172ba286a673140190c02ba9da60a2e43a745188c8a83c7f3ef" ) . unwrap( ) ) ;
1474
+ }
1475
+
1476
+ fn do_encoding_channel_announcement ( unknown_features_bits : bool , non_bitcoin_chain_hash : bool , excess_data : bool ) {
1477
+ let ( privkey_1, pubkey_1, secp_ctx_1) = get_keys_from ! ( "0101010101010101010101010101010101010101010101010101010101010101" ) ;
1478
+ let ( privkey_2, pubkey_2, secp_ctx_2) = get_keys_from ! ( "0202020202020202020202020202020202020202020202020202020202020202" ) ;
1479
+ let ( privkey_3, pubkey_3, secp_ctx_3) = get_keys_from ! ( "0303030303030303030303030303030303030303030303030303030303030303" ) ;
1480
+ let ( privkey_4, pubkey_4, secp_ctx_4) = get_keys_from ! ( "0404040404040404040404040404040404040404040404040404040404040404" ) ;
1481
+ let sig_1 = get_sig_on ! ( privkey_1, secp_ctx_1, String :: from( "01010101010101010101010101010101" ) ) ;
1482
+ let sig_2 = get_sig_on ! ( privkey_2, secp_ctx_2, String :: from( "01010101010101010101010101010101" ) ) ;
1483
+ let sig_3 = get_sig_on ! ( privkey_3, secp_ctx_3, String :: from( "01010101010101010101010101010101" ) ) ;
1484
+ let sig_4 = get_sig_on ! ( privkey_4, secp_ctx_4, String :: from( "01010101010101010101010101010101" ) ) ;
1485
+ let mut features = GlobalFeatures :: new ( ) ;
1486
+ if unknown_features_bits {
1487
+ features. flags = vec ! [ 0xFF , 0xFF ] ;
1488
+ }
1489
+ let unsigned_channel_announcement = msgs:: UnsignedChannelAnnouncement {
1490
+ features,
1491
+ chain_hash : if !non_bitcoin_chain_hash { Sha256dHash :: from_hex ( "6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000" ) . unwrap ( ) } else { Sha256dHash :: from_hex ( "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943" ) . unwrap ( ) } ,
1492
+ short_channel_id : 2316138423780173 ,
1493
+ node_id_1 : pubkey_1,
1494
+ node_id_2 : pubkey_2,
1495
+ bitcoin_key_1 : pubkey_3,
1496
+ bitcoin_key_2 : pubkey_4,
1497
+ excess_data : if excess_data { vec ! [ 10 , 0 , 0 , 20 , 0 , 0 , 30 , 0 , 0 , 40 ] } else { Vec :: new ( ) } ,
1498
+ } ;
1499
+ let channel_announcement = msgs:: ChannelAnnouncement {
1500
+ node_signature_1 : sig_1,
1501
+ node_signature_2 : sig_2,
1502
+ bitcoin_signature_1 : sig_3,
1503
+ bitcoin_signature_2 : sig_4,
1504
+ contents : unsigned_channel_announcement,
1505
+ } ;
1506
+ let encoded_value = channel_announcement. encode ( ) ;
1507
+ let mut target_value = hex:: decode ( "d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a1735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd" ) . unwrap ( ) ;
1508
+ if unknown_features_bits {
1509
+ target_value. append ( & mut hex:: decode ( "0002ffff" ) . unwrap ( ) ) ;
1510
+ } else {
1511
+ target_value. append ( & mut hex:: decode ( "0000" ) . unwrap ( ) ) ;
1512
+ }
1513
+ if non_bitcoin_chain_hash {
1514
+ target_value. append ( & mut hex:: decode ( "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000" ) . unwrap ( ) ) ;
1515
+ } else {
1516
+ target_value. append ( & mut hex:: decode ( "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" ) . unwrap ( ) ) ;
1517
+ }
1518
+ target_value. append ( & mut hex:: decode ( "00083a840000034d031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b" ) . unwrap ( ) ) ;
1519
+ if excess_data {
1520
+ target_value. append ( & mut hex:: decode ( "0a00001400001e000028" ) . unwrap ( ) ) ;
1521
+ }
1522
+ assert_eq ! ( encoded_value, target_value) ;
1523
+ }
1524
+
1525
+ #[ test]
1526
+ fn encoding_channel_announcement ( ) {
1527
+ do_encoding_channel_announcement ( false , false , false ) ;
1528
+ do_encoding_channel_announcement ( true , false , false ) ;
1529
+ do_encoding_channel_announcement ( false , true , false ) ;
1530
+ do_encoding_channel_announcement ( false , false , true ) ;
1531
+ do_encoding_channel_announcement ( true , true , true ) ;
1532
+ }
1533
+
1534
+ #[ test]
1535
+ fn encoding_node_announcement ( ) {
1536
+ //TODO:
1537
+ }
1538
+
1539
+ fn do_encoding_channel_update ( non_bitcoin_chain_hash : bool , direction : bool , disable : bool , htlc_maximum_msat : bool ) {
1540
+ let ( privkey_1, _, secp_ctx_1) = get_keys_from ! ( "0101010101010101010101010101010101010101010101010101010101010101" ) ;
1541
+ let sig_1 = get_sig_on ! ( privkey_1, secp_ctx_1, String :: from( "01010101010101010101010101010101" ) ) ;
1542
+ let unsigned_channel_update = msgs:: UnsignedChannelUpdate {
1543
+ chain_hash : if !non_bitcoin_chain_hash { Sha256dHash :: from_hex ( "6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000" ) . unwrap ( ) } else { Sha256dHash :: from_hex ( "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943" ) . unwrap ( ) } ,
1544
+ short_channel_id : 2316138423780173 ,
1545
+ timestamp : 20190119 ,
1546
+ flags : if direction { 1 } else { 0 } | if disable { 1 << 1 } else { 0 } | if htlc_maximum_msat { 1 << 8 } else { 0 } ,
1547
+ cltv_expiry_delta : 144 ,
1548
+ htlc_minimum_msat : 1000000 ,
1549
+ fee_base_msat : 10000 ,
1550
+ fee_proportional_millionths : 20 ,
1551
+ excess_data : if htlc_maximum_msat { vec ! [ 0 , 0 , 0 , 0 , 59 , 154 , 202 , 0 ] } else { Vec :: new ( ) }
1552
+ } ;
1553
+ let channel_update = msgs:: ChannelUpdate {
1554
+ signature : sig_1,
1555
+ contents : unsigned_channel_update
1556
+ } ;
1557
+ let encoded_value = channel_update. encode ( ) ;
1558
+ let mut target_value = hex:: decode ( "d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a" ) . unwrap ( ) ;
1559
+ if non_bitcoin_chain_hash {
1560
+ target_value. append ( & mut hex:: decode ( "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000" ) . unwrap ( ) ) ;
1561
+ } else {
1562
+ target_value. append ( & mut hex:: decode ( "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" ) . unwrap ( ) ) ;
1563
+ }
1564
+ target_value. append ( & mut hex:: decode ( "00083a840000034d013413a7" ) . unwrap ( ) ) ;
1565
+ if htlc_maximum_msat {
1566
+ target_value. append ( & mut hex:: decode ( "01" ) . unwrap ( ) ) ;
1567
+ } else {
1568
+ target_value. append ( & mut hex:: decode ( "00" ) . unwrap ( ) ) ;
1569
+ }
1570
+ target_value. append ( & mut hex:: decode ( "00" ) . unwrap ( ) ) ;
1571
+ if direction {
1572
+ let flag = target_value. last_mut ( ) . unwrap ( ) ;
1573
+ * flag = 1 ;
1574
+ }
1575
+ if disable {
1576
+ let flag = target_value. last_mut ( ) . unwrap ( ) ;
1577
+ * flag = * flag | 1 << 1 ;
1578
+ }
1579
+ target_value. append ( & mut hex:: decode ( "009000000000000f42400000271000000014" ) . unwrap ( ) ) ;
1580
+ if htlc_maximum_msat {
1581
+ target_value. append ( & mut hex:: decode ( "000000003b9aca00" ) . unwrap ( ) ) ;
1582
+ }
1583
+ assert_eq ! ( encoded_value, target_value) ;
1584
+ }
1585
+
1586
+ #[ test]
1587
+ fn encoding_channel_update ( ) {
1588
+ do_encoding_channel_update ( false , false , false , false ) ;
1589
+ do_encoding_channel_update ( true , false , false , false ) ;
1590
+ do_encoding_channel_update ( false , true , false , false ) ;
1591
+ do_encoding_channel_update ( false , false , true , false ) ;
1592
+ do_encoding_channel_update ( false , false , false , true ) ;
1593
+ do_encoding_channel_update ( true , true , true , true ) ;
1594
+ }
1432
1595
}
0 commit comments