+
+ macro_rules! get_keys_from {
+ ($slice: expr, $secp_ctx: expr) => {
+ {
+ let privkey = SecretKey::from_slice(&hex::decode($slice).unwrap()[..]).unwrap();
+ let pubkey = PublicKey::from_secret_key(&$secp_ctx, &privkey);
+ (privkey, pubkey)
+ }
+ }
+ }
+
+ macro_rules! get_sig_on {
+ ($privkey: expr, $ctx: expr, $string: expr) => {
+ {
+ let sighash = Message::from_slice(&$string.into_bytes()[..]).unwrap();
+ $ctx.sign(&sighash, &$privkey)
+ }
+ }
+ }
+
+ #[test]
+ fn encoding_announcement_signatures() {
+ let secp_ctx = Secp256k1::new();
+ let (privkey, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let sig_1 = get_sig_on!(privkey, secp_ctx, String::from("01010101010101010101010101010101"));
+ let sig_2 = get_sig_on!(privkey, secp_ctx, String::from("02020202020202020202020202020202"));
+ let announcement_signatures = msgs::AnnouncementSignatures {
+ 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],
+ short_channel_id: 2316138423780173,
+ node_signature: sig_1,
+ bitcoin_signature: sig_2,
+ };
+
+ let encoded_value = announcement_signatures.encode();
+ assert_eq!(encoded_value, hex::decode("040000000000000005000000000000000600000000000000070000000000000000083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073acf9953cef4700860f5967838eba2bae89288ad188ebf8b20bf995c3ea53a26df1876d0a3a0e13172ba286a673140190c02ba9da60a2e43a745188c8a83c7f3ef").unwrap());
+ }
+
+ fn do_encoding_channel_announcement(unknown_features_bits: bool, non_bitcoin_chain_hash: bool, excess_data: bool) {
+ let secp_ctx = Secp256k1::new();
+ let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let (privkey_2, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
+ let (privkey_3, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
+ let (privkey_4, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
+ let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
+ let sig_2 = get_sig_on!(privkey_2, secp_ctx, String::from("01010101010101010101010101010101"));
+ let sig_3 = get_sig_on!(privkey_3, secp_ctx, String::from("01010101010101010101010101010101"));
+ let sig_4 = get_sig_on!(privkey_4, secp_ctx, String::from("01010101010101010101010101010101"));
+ let mut features = GlobalFeatures::new();
+ if unknown_features_bits {
+ features.flags = vec![0xFF, 0xFF];
+ }
+ let unsigned_channel_announcement = msgs::UnsignedChannelAnnouncement {
+ features,
+ chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
+ short_channel_id: 2316138423780173,
+ node_id_1: pubkey_1,
+ node_id_2: pubkey_2,
+ bitcoin_key_1: pubkey_3,
+ bitcoin_key_2: pubkey_4,
+ excess_data: if excess_data { vec![10, 0, 0, 20, 0, 0, 30, 0, 0, 40] } else { Vec::new() },
+ };
+ let channel_announcement = msgs::ChannelAnnouncement {
+ node_signature_1: sig_1,
+ node_signature_2: sig_2,
+ bitcoin_signature_1: sig_3,
+ bitcoin_signature_2: sig_4,
+ contents: unsigned_channel_announcement,
+ };
+ let encoded_value = channel_announcement.encode();
+ let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a1735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap();
+ if unknown_features_bits {
+ target_value.append(&mut hex::decode("0002ffff").unwrap());
+ } else {
+ target_value.append(&mut hex::decode("0000").unwrap());
+ }
+ if non_bitcoin_chain_hash {
+ target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
+ } else {
+ target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
+ }
+ target_value.append(&mut hex::decode("00083a840000034d031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b").unwrap());
+ if excess_data {
+ target_value.append(&mut hex::decode("0a00001400001e000028").unwrap());
+ }
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_channel_announcement() {
+ do_encoding_channel_announcement(false, false, false);
+ do_encoding_channel_announcement(true, false, false);
+ do_encoding_channel_announcement(true, true, false);
+ do_encoding_channel_announcement(true, true, true);
+ do_encoding_channel_announcement(false, true, true);
+ do_encoding_channel_announcement(false, false, true);
+ do_encoding_channel_announcement(false, true, false);
+ do_encoding_channel_announcement(true, false, true);
+ }
+
+ fn do_encoding_node_announcement(unknown_features_bits: bool, ipv4: bool, ipv6: bool, onionv2: bool, onionv3: bool, excess_address_data: bool, excess_data: bool) {
+ let secp_ctx = Secp256k1::new();
+ let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
+ let mut features = GlobalFeatures::new();
+ if unknown_features_bits {
+ features.flags = vec![0xFF, 0xFF];
+ }
+ let mut addresses = Vec::new();
+ if ipv4 {
+ addresses.push(msgs::NetAddress::IPv4 {
+ addr: [255, 254, 253, 252],
+ port: 9735
+ });
+ }
+ if ipv6 {
+ addresses.push(msgs::NetAddress::IPv6 {
+ addr: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240],
+ port: 9735
+ });
+ }
+ if onionv2 {
+ addresses.push(msgs::NetAddress::OnionV2 {
+ addr: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246],
+ port: 9735
+ });
+ }
+ if onionv3 {
+ addresses.push(msgs::NetAddress::OnionV3 {
+ ed25519_pubkey: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 237, 236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225, 224],
+ checksum: 32,
+ version: 16,
+ port: 9735
+ });
+ }
+ let mut addr_len = 0;
+ for addr in &addresses {
+ addr_len += addr.len() + 1;
+ }
+ let unsigned_node_announcement = msgs::UnsignedNodeAnnouncement {
+ features,
+ timestamp: 20190119,
+ node_id: pubkey_1,
+ rgb: [32; 3],
+ alias: [16;32],
+ addresses,
+ excess_address_data: if excess_address_data { vec![33, 108, 40, 11, 83, 149, 162, 84, 110, 126, 75, 38, 99, 224, 79, 129, 22, 34, 241, 90, 79, 146, 232, 58, 162, 233, 43, 162, 165, 115, 193, 57, 20, 44, 84, 174, 99, 7, 42, 30, 193, 238, 125, 192, 192, 75, 222, 92, 132, 120, 6, 23, 42, 160, 92, 146, 194, 42, 232, 227, 8, 209, 210, 105] } else { Vec::new() },
+ excess_data: if excess_data { vec![59, 18, 204, 25, 92, 224, 162, 209, 189, 166, 168, 139, 239, 161, 159, 160, 127, 81, 202, 167, 92, 232, 56, 55, 242, 137, 101, 96, 11, 138, 172, 171, 8, 85, 255, 176, 231, 65, 236, 95, 124, 65, 66, 30, 152, 41, 169, 212, 134, 17, 200, 200, 49, 247, 27, 229, 234, 115, 230, 101, 148, 151, 127, 253] } else { Vec::new() },
+ };
+ addr_len += unsigned_node_announcement.excess_address_data.len() as u16;
+ let node_announcement = msgs::NodeAnnouncement {
+ signature: sig_1,
+ contents: unsigned_node_announcement,
+ };
+ let encoded_value = node_announcement.encode();
+ let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
+ if unknown_features_bits {
+ target_value.append(&mut hex::decode("0002ffff").unwrap());
+ } else {
+ target_value.append(&mut hex::decode("0000").unwrap());
+ }
+ target_value.append(&mut hex::decode("013413a7031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2020201010101010101010101010101010101010101010101010101010101010101010").unwrap());
+ target_value.append(&mut vec![(addr_len >> 8) as u8, addr_len as u8]);
+ if ipv4 {
+ target_value.append(&mut hex::decode("01fffefdfc2607").unwrap());
+ }
+ if ipv6 {
+ target_value.append(&mut hex::decode("02fffefdfcfbfaf9f8f7f6f5f4f3f2f1f02607").unwrap());
+ }
+ if onionv2 {
+ target_value.append(&mut hex::decode("03fffefdfcfbfaf9f8f7f62607").unwrap());
+ }
+ if onionv3 {
+ target_value.append(&mut hex::decode("04fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e00020102607").unwrap());
+ }
+ if excess_address_data {
+ target_value.append(&mut hex::decode("216c280b5395a2546e7e4b2663e04f811622f15a4f92e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d269").unwrap());
+ }
+ if excess_data {
+ target_value.append(&mut hex::decode("3b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap());
+ }
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_node_announcement() {
+ do_encoding_node_announcement(true, true, true, true, true, true, true);
+ do_encoding_node_announcement(false, false, false, false, false, false, false);
+ do_encoding_node_announcement(false, true, false, false, false, false, false);
+ do_encoding_node_announcement(false, false, true, false, false, false, false);
+ do_encoding_node_announcement(false, false, false, true, false, false, false);
+ do_encoding_node_announcement(false, false, false, false, true, false, false);
+ do_encoding_node_announcement(false, false, false, false, false, true, false);
+ do_encoding_node_announcement(false, true, false, true, false, true, false);
+ do_encoding_node_announcement(false, false, true, false, true, false, false);
+ }
+
+ fn do_encoding_channel_update(non_bitcoin_chain_hash: bool, direction: bool, disable: bool, htlc_maximum_msat: bool) {
+ let secp_ctx = Secp256k1::new();
+ let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
+ let unsigned_channel_update = msgs::UnsignedChannelUpdate {
+ chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
+ short_channel_id: 2316138423780173,
+ timestamp: 20190119,
+ flags: if direction { 1 } else { 0 } | if disable { 1 << 1 } else { 0 } | if htlc_maximum_msat { 1 << 8 } else { 0 },
+ cltv_expiry_delta: 144,
+ htlc_minimum_msat: 1000000,
+ fee_base_msat: 10000,
+ fee_proportional_millionths: 20,
+ excess_data: if htlc_maximum_msat { vec![0, 0, 0, 0, 59, 154, 202, 0] } else { Vec::new() }
+ };
+ let channel_update = msgs::ChannelUpdate {
+ signature: sig_1,
+ contents: unsigned_channel_update
+ };
+ let encoded_value = channel_update.encode();
+ let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
+ if non_bitcoin_chain_hash {
+ target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
+ } else {
+ target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
+ }
+ target_value.append(&mut hex::decode("00083a840000034d013413a7").unwrap());
+ if htlc_maximum_msat {
+ target_value.append(&mut hex::decode("01").unwrap());
+ } else {
+ target_value.append(&mut hex::decode("00").unwrap());
+ }
+ target_value.append(&mut hex::decode("00").unwrap());
+ if direction {
+ let flag = target_value.last_mut().unwrap();
+ *flag = 1;
+ }
+ if disable {
+ let flag = target_value.last_mut().unwrap();
+ *flag = *flag | 1 << 1;
+ }
+ target_value.append(&mut hex::decode("009000000000000f42400000271000000014").unwrap());
+ if htlc_maximum_msat {
+ target_value.append(&mut hex::decode("000000003b9aca00").unwrap());
+ }
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_channel_update() {
+ do_encoding_channel_update(false, false, false, false);
+ do_encoding_channel_update(true, false, false, false);
+ do_encoding_channel_update(false, true, false, false);
+ do_encoding_channel_update(false, false, true, false);
+ do_encoding_channel_update(false, false, false, true);
+ do_encoding_channel_update(true, true, true, true);
+ }
+
+ fn do_encoding_open_channel(non_bitcoin_chain_hash: bool, random_bit: bool, shutdown: bool) {
+ let secp_ctx = Secp256k1::new();
+ let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
+ let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
+ let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
+ let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
+ let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
+ let open_channel = msgs::OpenChannel {
+ chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
+ temporary_channel_id: [2; 32],
+ funding_satoshis: 1311768467284833366,
+ push_msat: 2536655962884945560,
+ dust_limit_satoshis: 3608586615801332854,
+ max_htlc_value_in_flight_msat: 8517154655701053848,
+ channel_reserve_satoshis: 8665828695742877976,
+ htlc_minimum_msat: 2316138423780173,
+ feerate_per_kw: 821716,
+ to_self_delay: 49340,
+ max_accepted_htlcs: 49340,
+ funding_pubkey: pubkey_1,
+ revocation_basepoint: pubkey_2,
+ payment_basepoint: pubkey_3,
+ delayed_payment_basepoint: pubkey_4,
+ htlc_basepoint: pubkey_5,
+ first_per_commitment_point: pubkey_6,
+ channel_flags: if random_bit { 1 << 5 } else { 0 },
+ shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
+ };
+ let encoded_value = open_channel.encode();
+ let mut target_value = Vec::new();
+ if non_bitcoin_chain_hash {
+ target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
+ } else {
+ target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
+ }
+ target_value.append(&mut hex::decode("02020202020202020202020202020202020202020202020202020202020202021234567890123456233403289122369832144668701144767633030896203198784335490624111800083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap());
+ if random_bit {
+ target_value.append(&mut hex::decode("20").unwrap());
+ } else {
+ target_value.append(&mut hex::decode("00").unwrap());
+ }
+ if shutdown {
+ target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
+ }
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_open_channel() {
+ do_encoding_open_channel(false, false, false);
+ do_encoding_open_channel(true, false, false);
+ do_encoding_open_channel(false, true, false);
+ do_encoding_open_channel(false, false, true);
+ do_encoding_open_channel(true, true, true);
+ }
+
+ fn do_encoding_accept_channel(shutdown: bool) {
+ let secp_ctx = Secp256k1::new();
+ let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
+ let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
+ let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
+ let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
+ let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
+ let accept_channel = msgs::AcceptChannel {
+ temporary_channel_id: [2; 32],
+ dust_limit_satoshis: 1311768467284833366,
+ max_htlc_value_in_flight_msat: 2536655962884945560,
+ channel_reserve_satoshis: 3608586615801332854,
+ htlc_minimum_msat: 2316138423780173,
+ minimum_depth: 821716,
+ to_self_delay: 49340,
+ max_accepted_htlcs: 49340,
+ funding_pubkey: pubkey_1,
+ revocation_basepoint: pubkey_2,
+ payment_basepoint: pubkey_3,
+ delayed_payment_basepoint: pubkey_4,
+ htlc_basepoint: pubkey_5,
+ first_per_commitment_point: pubkey_6,
+ shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
+ };
+ let encoded_value = accept_channel.encode();
+ let mut target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020212345678901234562334032891223698321446687011447600083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap();
+ if shutdown {
+ target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
+ }
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_accept_channel() {
+ do_encoding_accept_channel(false);
+ do_encoding_accept_channel(true);
+ }
+
+ #[test]
+ fn encoding_funding_created() {
+ let secp_ctx = Secp256k1::new();
+ let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
+ let funding_created = msgs::FundingCreated {
+ temporary_channel_id: [2; 32],
+ funding_txid: Sha256dHash::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap(),
+ funding_output_index: 255,
+ signature: sig_1,
+ };
+ let encoded_value = funding_created.encode();
+ let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202026e96fe9f8b0ddcd729ba03cfafa5a27b050b39d354dd980814268dfa9a44d4c200ffd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_funding_signed() {
+ let secp_ctx = Secp256k1::new();
+ let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
+ let funding_signed = msgs::FundingSigned {
+ channel_id: [2; 32],
+ signature: sig_1,
+ };
+ let encoded_value = funding_signed.encode();
+ let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_funding_locked() {
+ let secp_ctx = Secp256k1::new();
+ let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let funding_locked = msgs::FundingLocked {
+ channel_id: [2; 32],
+ next_per_commitment_point: pubkey_1,
+ };
+ let encoded_value = funding_locked.encode();
+ let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ fn do_encoding_shutdown(script_type: u8) {
+ let secp_ctx = Secp256k1::new();
+ let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let script = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
+ let shutdown = msgs::Shutdown {
+ channel_id: [2; 32],
+ scriptpubkey: if script_type == 1 { Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey() } else if script_type == 2 { Address::p2sh(&script, Network::Testnet).script_pubkey() } else if script_type == 3 { Address::p2wpkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey() } else { Address::p2wsh(&script, Network::Testnet).script_pubkey() },
+ };
+ let encoded_value = shutdown.encode();
+ let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap();
+ if script_type == 1 {
+ target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
+ } else if script_type == 2 {
+ target_value.append(&mut hex::decode("0017a914da1745e9b549bd0bfa1a569971c77eba30cd5a4b87").unwrap());
+ } else if script_type == 3 {
+ target_value.append(&mut hex::decode("0016001479b000887626b294a914501a4cd226b58b235983").unwrap());
+ } else if script_type == 4 {
+ target_value.append(&mut hex::decode("002200204ae81572f06e1b88fd5ced7a1a000945432e83e1551e6f721ee9c00b8cc33260").unwrap());
+ }
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_shutdown() {
+ do_encoding_shutdown(1);
+ do_encoding_shutdown(2);
+ do_encoding_shutdown(3);
+ do_encoding_shutdown(4);
+ }
+
+ #[test]
+ fn encoding_closing_signed() {
+ let secp_ctx = Secp256k1::new();
+ let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
+ let closing_signed = msgs::ClosingSigned {
+ channel_id: [2; 32],
+ fee_satoshis: 2316138423780173,
+ signature: sig_1,
+ };
+ let encoded_value = closing_signed.encode();
+ let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_update_add_htlc() {
+ let secp_ctx = Secp256k1::new();
+ let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let onion_routing_packet = msgs::OnionPacket {
+ version: 255,
+ public_key: Ok(pubkey_1),
+ hop_data: [1; 20*65],
+ hmac: [2; 32]
+ };
+ let update_add_htlc = msgs::UpdateAddHTLC {
+ channel_id: [2; 32],
+ htlc_id: 2316138423780173,
+ amount_msat: 3608586615801332854,
+ payment_hash: PaymentHash([1; 32]),
+ cltv_expiry: 821716,
+ onion_routing_packet
+ };
+ let encoded_value = update_add_htlc.encode();
+ let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d32144668701144760101010101010101010101010101010101010101010101010101010101010101000c89d4ff031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_update_fulfill_htlc() {
+ let update_fulfill_htlc = msgs::UpdateFulfillHTLC {
+ channel_id: [2; 32],
+ htlc_id: 2316138423780173,
+ payment_preimage: PaymentPreimage([1; 32]),
+ };
+ let encoded_value = update_fulfill_htlc.encode();
+ let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d0101010101010101010101010101010101010101010101010101010101010101").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_update_fail_htlc() {
+ let reason = OnionErrorPacket {
+ data: [1; 32].to_vec(),
+ };
+ let update_fail_htlc = msgs::UpdateFailHTLC {
+ channel_id: [2; 32],
+ htlc_id: 2316138423780173,
+ reason
+ };
+ let encoded_value = update_fail_htlc.encode();
+ let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d00200101010101010101010101010101010101010101010101010101010101010101").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_update_fail_malformed_htlc() {
+ let update_fail_malformed_htlc = msgs::UpdateFailMalformedHTLC {
+ channel_id: [2; 32],
+ htlc_id: 2316138423780173,
+ sha256_of_onion: [1; 32],
+ failure_code: 255
+ };
+ let encoded_value = update_fail_malformed_htlc.encode();
+ let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d010101010101010101010101010101010101010101010101010101010101010100ff").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ fn do_encoding_commitment_signed(htlcs: bool) {
+ let secp_ctx = Secp256k1::new();
+ let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let (privkey_2, _) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
+ let (privkey_3, _) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
+ let (privkey_4, _) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
+ let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
+ let sig_2 = get_sig_on!(privkey_2, secp_ctx, String::from("01010101010101010101010101010101"));
+ let sig_3 = get_sig_on!(privkey_3, secp_ctx, String::from("01010101010101010101010101010101"));
+ let sig_4 = get_sig_on!(privkey_4, secp_ctx, String::from("01010101010101010101010101010101"));
+ let commitment_signed = msgs::CommitmentSigned {
+ channel_id: [2; 32],
+ signature: sig_1,
+ htlc_signatures: if htlcs { vec![sig_2, sig_3, sig_4] } else { Vec::new() },
+ };
+ let encoded_value = commitment_signed.encode();
+ let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
+ if htlcs {
+ target_value.append(&mut hex::decode("00031735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap());
+ } else {
+ target_value.append(&mut hex::decode("0000").unwrap());
+ }
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_commitment_signed() {
+ do_encoding_commitment_signed(true);
+ do_encoding_commitment_signed(false);
+ }
+
+ #[test]
+ fn encoding_revoke_and_ack() {
+ let secp_ctx = Secp256k1::new();
+ let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let raa = msgs::RevokeAndACK {
+ channel_id: [2; 32],
+ per_commitment_secret: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
+ next_per_commitment_point: pubkey_1,
+ };
+ let encoded_value = raa.encode();
+ let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202020101010101010101010101010101010101010101010101010101010101010101031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_update_fee() {
+ let update_fee = msgs::UpdateFee {
+ channel_id: [2; 32],
+ feerate_per_kw: 20190119,
+ };
+ let encoded_value = update_fee.encode();
+ let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202013413a7").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ fn do_encoding_init(unknown_global_bits: bool, initial_routing_sync: bool) {
+ let mut global = GlobalFeatures::new();
+ if unknown_global_bits {
+ global.flags = vec![0xFF, 0xFF];
+ }
+ let mut local = LocalFeatures::new();
+ if initial_routing_sync {
+ local.set_initial_routing_sync();
+ }
+ let init = msgs::Init {
+ global_features: global,
+ local_features: local,
+ };
+ let encoded_value = init.encode();
+ let mut target_value = Vec::new();
+ if unknown_global_bits {
+ target_value.append(&mut hex::decode("0002ffff").unwrap());
+ } else {
+ target_value.append(&mut hex::decode("0000").unwrap());
+ }
+ if initial_routing_sync {
+ target_value.append(&mut hex::decode("000108").unwrap());
+ } else {
+ target_value.append(&mut hex::decode("0000").unwrap());
+ }
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_init() {
+ do_encoding_init(false, false);
+ do_encoding_init(true, false);
+ do_encoding_init(false, true);
+ do_encoding_init(true, true);
+ }
+
+ #[test]
+ fn encoding_error() {
+ let error = msgs::ErrorMessage {
+ channel_id: [2; 32],
+ data: String::from("rust-lightning"),
+ };
+ let encoded_value = error.encode();
+ let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202000e727573742d6c696768746e696e67").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_ping() {
+ let ping = msgs::Ping {
+ ponglen: 64,
+ byteslen: 64
+ };
+ let encoded_value = ping.encode();
+ let target_value = hex::decode("0040004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_pong() {
+ let pong = msgs::Pong {
+ byteslen: 64
+ };
+ let encoded_value = pong.encode();
+ let target_value = hex::decode("004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }