+ let target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_splice() {
+ let secp_ctx = Secp256k1::new();
+ let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let splice = msgs::Splice {
+ chain_hash: ChainHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap(),
+ channel_id: ChannelId::from_bytes([2; 32]),
+ relative_satoshis: 123456,
+ funding_feerate_perkw: 2000,
+ locktime: 0,
+ funding_pubkey: pubkey_1,
+ };
+ let encoded_value = splice.encode();
+ assert_eq!(encoded_value.as_hex().to_string(), "02020202020202020202020202020202020202020202020202020202020202026fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000000000000001e240000007d000000000031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f");
+ }
+
+ #[test]
+ fn encoding_stfu() {
+ let stfu = msgs::Stfu {
+ channel_id: ChannelId::from_bytes([2; 32]),
+ initiator: 1,
+ };
+ let encoded_value = stfu.encode();
+ assert_eq!(encoded_value.as_hex().to_string(), "020202020202020202020202020202020202020202020202020202020202020201");
+ }
+
+ #[test]
+ fn encoding_splice_ack() {
+ let secp_ctx = Secp256k1::new();
+ let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
+ let splice = msgs::SpliceAck {
+ chain_hash: ChainHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap(),
+ channel_id: ChannelId::from_bytes([2; 32]),
+ relative_satoshis: 123456,
+ funding_pubkey: pubkey_1,
+ };
+ let encoded_value = splice.encode();
+ assert_eq!(encoded_value.as_hex().to_string(), "02020202020202020202020202020202020202020202020202020202020202026fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000000000000001e240031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f");
+ }
+
+ #[test]
+ fn encoding_splice_locked() {
+ let splice = msgs::SpliceLocked {
+ channel_id: ChannelId::from_bytes([2; 32]),
+ };
+ let encoded_value = splice.encode();
+ assert_eq!(encoded_value.as_hex().to_string(), "0202020202020202020202020202020202020202020202020202020202020202");
+ }
+
+ #[test]
+ fn encoding_tx_add_input() {
+ let tx_add_input = msgs::TxAddInput {
+ channel_id: ChannelId::from_bytes([2; 32]),
+ serial_id: 4886718345,
+ prevtx: TransactionU16LenLimited::new(Transaction {
+ version: 2,
+ lock_time: LockTime::ZERO,
+ input: vec![TxIn {
+ previous_output: OutPoint { txid: Txid::from_str("305bab643ee297b8b6b76b320792c8223d55082122cb606bf89382146ced9c77").unwrap(), index: 2 }.into_bitcoin_outpoint(),
+ script_sig: ScriptBuf::new(),
+ sequence: Sequence(0xfffffffd),
+ witness: Witness::from_slice(&vec![
+ <Vec<u8>>::from_hex("304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701").unwrap(),
+ <Vec<u8>>::from_hex("0301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944").unwrap()]),
+ }],
+ output: vec![
+ TxOut {
+ value: 12704566,
+ script_pubkey: Address::from_str("bc1qzlffunw52jav8vwdu5x3jfk6sr8u22rmq3xzw2").unwrap().payload.script_pubkey(),
+ },
+ TxOut {
+ value: 245148,
+ script_pubkey: Address::from_str("bc1qxmk834g5marzm227dgqvynd23y2nvt2ztwcw2z").unwrap().payload.script_pubkey(),
+ },
+ ],
+ }).unwrap(),
+ prevtx_out: 305419896,
+ sequence: 305419896,
+ };
+ let encoded_value = tx_add_input.encode();
+ let target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202000000012345678900de02000000000101779ced6c148293f86b60cb222108553d22c89207326bb7b6b897e23e64ab5b300200000000fdffffff0236dbc1000000000016001417d29e4dd454bac3b1cde50d1926da80cfc5287b9cbd03000000000016001436ec78d514df462da95e6a00c24daa8915362d420247304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701210301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944000000001234567812345678").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_tx_add_output() {
+ let tx_add_output = msgs::TxAddOutput {
+ channel_id: ChannelId::from_bytes([2; 32]),
+ serial_id: 4886718345,
+ sats: 4886718345,
+ script: Address::from_str("bc1qxmk834g5marzm227dgqvynd23y2nvt2ztwcw2z").unwrap().payload.script_pubkey(),
+ };
+ let encoded_value = tx_add_output.encode();
+ let target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202000000012345678900000001234567890016001436ec78d514df462da95e6a00c24daa8915362d42").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_tx_remove_input() {
+ let tx_remove_input = msgs::TxRemoveInput {
+ channel_id: ChannelId::from_bytes([2; 32]),
+ serial_id: 4886718345,
+ };
+ let encoded_value = tx_remove_input.encode();
+ let target_value = <Vec<u8>>::from_hex("02020202020202020202020202020202020202020202020202020202020202020000000123456789").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_tx_remove_output() {
+ let tx_remove_output = msgs::TxRemoveOutput {
+ channel_id: ChannelId::from_bytes([2; 32]),
+ serial_id: 4886718345,
+ };
+ let encoded_value = tx_remove_output.encode();
+ let target_value = <Vec<u8>>::from_hex("02020202020202020202020202020202020202020202020202020202020202020000000123456789").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_tx_complete() {
+ let tx_complete = msgs::TxComplete {
+ channel_id: ChannelId::from_bytes([2; 32]),
+ };
+ let encoded_value = tx_complete.encode();
+ let target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_tx_signatures() {
+ let tx_signatures = msgs::TxSignatures {
+ channel_id: ChannelId::from_bytes([2; 32]),
+ tx_hash: Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap(),
+ witnesses: vec![
+ Witness::from_slice(&vec![
+ <Vec<u8>>::from_hex("304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701").unwrap(),
+ <Vec<u8>>::from_hex("0301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944").unwrap()]),
+ Witness::from_slice(&vec![
+ <Vec<u8>>::from_hex("3045022100ee00dbf4a862463e837d7c08509de814d620e4d9830fa84818713e0fa358f145022021c3c7060c4d53fe84fd165d60208451108a778c13b92ca4c6bad439236126cc01").unwrap(),
+ <Vec<u8>>::from_hex("028fbbf0b16f5ba5bcb5dd37cd4047ce6f726a21c06682f9ec2f52b057de1dbdb5").unwrap()]),
+ ],
+ };
+ let encoded_value = tx_signatures.encode();
+ let mut target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202").unwrap(); // channel_id
+ target_value.append(&mut <Vec<u8>>::from_hex("6e96fe9f8b0ddcd729ba03cfafa5a27b050b39d354dd980814268dfa9a44d4c2").unwrap()); // tx_hash (sha256) (big endian byte order)
+ target_value.append(&mut <Vec<u8>>::from_hex("0002").unwrap()); // num_witnesses (u16)
+ // Witness 1
+ target_value.append(&mut <Vec<u8>>::from_hex("006b").unwrap()); // len of witness_data
+ target_value.append(&mut <Vec<u8>>::from_hex("02").unwrap()); // num_witness_elements (VarInt)
+ target_value.append(&mut <Vec<u8>>::from_hex("47").unwrap()); // len of witness element data (VarInt)
+ target_value.append(&mut <Vec<u8>>::from_hex("304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701").unwrap());
+ target_value.append(&mut <Vec<u8>>::from_hex("21").unwrap()); // len of witness element data (VarInt)
+ target_value.append(&mut <Vec<u8>>::from_hex("0301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944").unwrap());
+ // Witness 2
+ target_value.append(&mut <Vec<u8>>::from_hex("006c").unwrap()); // len of witness_data
+ target_value.append(&mut <Vec<u8>>::from_hex("02").unwrap()); // num_witness_elements (VarInt)
+ target_value.append(&mut <Vec<u8>>::from_hex("48").unwrap()); // len of witness element data (VarInt)
+ target_value.append(&mut <Vec<u8>>::from_hex("3045022100ee00dbf4a862463e837d7c08509de814d620e4d9830fa84818713e0fa358f145022021c3c7060c4d53fe84fd165d60208451108a778c13b92ca4c6bad439236126cc01").unwrap());
+ target_value.append(&mut <Vec<u8>>::from_hex("21").unwrap()); // len of witness element data (VarInt)
+ target_value.append(&mut <Vec<u8>>::from_hex("028fbbf0b16f5ba5bcb5dd37cd4047ce6f726a21c06682f9ec2f52b057de1dbdb5").unwrap());
+ assert_eq!(encoded_value, target_value);
+ }
+
+ fn do_encoding_tx_init_rbf(funding_value_with_hex_target: Option<(i64, &str)>) {
+ let tx_init_rbf = msgs::TxInitRbf {
+ channel_id: ChannelId::from_bytes([2; 32]),
+ locktime: 305419896,
+ feerate_sat_per_1000_weight: 20190119,
+ funding_output_contribution: if let Some((value, _)) = funding_value_with_hex_target { Some(value) } else { None },
+ };
+ let encoded_value = tx_init_rbf.encode();
+ let mut target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202").unwrap(); // channel_id
+ target_value.append(&mut <Vec<u8>>::from_hex("12345678").unwrap()); // locktime
+ target_value.append(&mut <Vec<u8>>::from_hex("013413a7").unwrap()); // feerate_sat_per_1000_weight
+ if let Some((_, target)) = funding_value_with_hex_target {
+ target_value.push(0x00); // Type
+ target_value.push(target.len() as u8 / 2); // Length
+ target_value.append(&mut <Vec<u8>>::from_hex(target).unwrap()); // Value (i64)
+ }
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_tx_init_rbf() {
+ do_encoding_tx_init_rbf(Some((1311768467284833366, "1234567890123456")));
+ do_encoding_tx_init_rbf(Some((13117684672, "000000030DDFFBC0")));
+ do_encoding_tx_init_rbf(None);
+ }
+
+ fn do_encoding_tx_ack_rbf(funding_value_with_hex_target: Option<(i64, &str)>) {
+ let tx_ack_rbf = msgs::TxAckRbf {
+ channel_id: ChannelId::from_bytes([2; 32]),
+ funding_output_contribution: if let Some((value, _)) = funding_value_with_hex_target { Some(value) } else { None },
+ };
+ let encoded_value = tx_ack_rbf.encode();
+ let mut target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202").unwrap();
+ if let Some((_, target)) = funding_value_with_hex_target {
+ target_value.push(0x00); // Type
+ target_value.push(target.len() as u8 / 2); // Length
+ target_value.append(&mut <Vec<u8>>::from_hex(target).unwrap()); // Value (i64)
+ }
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_tx_ack_rbf() {
+ do_encoding_tx_ack_rbf(Some((1311768467284833366, "1234567890123456")));
+ do_encoding_tx_ack_rbf(Some((13117684672, "000000030DDFFBC0")));
+ do_encoding_tx_ack_rbf(None);
+ }
+
+ #[test]
+ fn encoding_tx_abort() {
+ let tx_abort = msgs::TxAbort {
+ channel_id: ChannelId::from_bytes([2; 32]),
+ data: <Vec<u8>>::from_hex("54686520717569636B2062726F776E20666F78206A756D7073206F76657220746865206C617A7920646F672E").unwrap(),
+ };
+ let encoded_value = tx_abort.encode();
+ let target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202002C54686520717569636B2062726F776E20666F78206A756D7073206F76657220746865206C617A7920646F672E").unwrap();