+ #[test]
+ fn encoding_tx_add_input() {
+ let tx_add_input = msgs::TxAddInput {
+ channel_id: [2; 32],
+ serial_id: 4886718345,
+ prevtx: TransactionU16LenLimited::new(Transaction {
+ version: 2,
+ lock_time: PackedLockTime(0),
+ input: vec![TxIn {
+ previous_output: OutPoint { txid: Txid::from_hex("305bab643ee297b8b6b76b320792c8223d55082122cb606bf89382146ced9c77").unwrap(), index: 2 }.into_bitcoin_outpoint(),
+ script_sig: Script::new(),
+ sequence: Sequence(0xfffffffd),
+ witness: Witness::from_vec(vec![
+ hex::decode("304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701").unwrap(),
+ hex::decode("0301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944").unwrap()]),
+ }],
+ output: vec![
+ TxOut {
+ value: 12704566,
+ script_pubkey: Address::from_str("bc1qzlffunw52jav8vwdu5x3jfk6sr8u22rmq3xzw2").unwrap().script_pubkey(),
+ },
+ TxOut {
+ value: 245148,
+ script_pubkey: Address::from_str("bc1qxmk834g5marzm227dgqvynd23y2nvt2ztwcw2z").unwrap().script_pubkey(),
+ },
+ ],
+ }).unwrap(),
+ prevtx_out: 305419896,
+ sequence: 305419896,
+ };
+ let encoded_value = tx_add_input.encode();
+ let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202000000012345678900de02000000000101779ced6c148293f86b60cb222108553d22c89207326bb7b6b897e23e64ab5b300200000000fdffffff0236dbc1000000000016001417d29e4dd454bac3b1cde50d1926da80cfc5287b9cbd03000000000016001436ec78d514df462da95e6a00c24daa8915362d420247304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701210301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944000000001234567812345678").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_tx_add_output() {
+ let tx_add_output = msgs::TxAddOutput {
+ channel_id: [2; 32],
+ serial_id: 4886718345,
+ sats: 4886718345,
+ script: Address::from_str("bc1qxmk834g5marzm227dgqvynd23y2nvt2ztwcw2z").unwrap().script_pubkey(),
+ };
+ let encoded_value = tx_add_output.encode();
+ let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202000000012345678900000001234567890016001436ec78d514df462da95e6a00c24daa8915362d42").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_tx_remove_input() {
+ let tx_remove_input = msgs::TxRemoveInput {
+ channel_id: [2; 32],
+ serial_id: 4886718345,
+ };
+ let encoded_value = tx_remove_input.encode();
+ let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202020000000123456789").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_tx_remove_output() {
+ let tx_remove_output = msgs::TxRemoveOutput {
+ channel_id: [2; 32],
+ serial_id: 4886718345,
+ };
+ let encoded_value = tx_remove_output.encode();
+ let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202020000000123456789").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_tx_complete() {
+ let tx_complete = msgs::TxComplete {
+ channel_id: [2; 32],
+ };
+ let encoded_value = tx_complete.encode();
+ let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_tx_signatures() {
+ let tx_signatures = msgs::TxSignatures {
+ channel_id: [2; 32],
+ tx_hash: Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap(),
+ witnesses: vec![
+ Witness::from_vec(vec![
+ hex::decode("304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701").unwrap(),
+ hex::decode("0301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944").unwrap()]),
+ Witness::from_vec(vec![
+ hex::decode("3045022100ee00dbf4a862463e837d7c08509de814d620e4d9830fa84818713e0fa358f145022021c3c7060c4d53fe84fd165d60208451108a778c13b92ca4c6bad439236126cc01").unwrap(),
+ hex::decode("028fbbf0b16f5ba5bcb5dd37cd4047ce6f726a21c06682f9ec2f52b057de1dbdb5").unwrap()]),
+ ],
+ };
+ let encoded_value = tx_signatures.encode();
+ let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap(); // channel_id
+ target_value.append(&mut hex::decode("6e96fe9f8b0ddcd729ba03cfafa5a27b050b39d354dd980814268dfa9a44d4c2").unwrap()); // tx_hash (sha256) (big endian byte order)
+ target_value.append(&mut hex::decode("0002").unwrap()); // num_witnesses (u16)
+ // Witness 1
+ target_value.append(&mut hex::decode("006b").unwrap()); // len of witness_data
+ target_value.append(&mut hex::decode("02").unwrap()); // num_witness_elements (VarInt)
+ target_value.append(&mut hex::decode("47").unwrap()); // len of witness element data (VarInt)
+ target_value.append(&mut hex::decode("304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701").unwrap());
+ target_value.append(&mut hex::decode("21").unwrap()); // len of witness element data (VarInt)
+ target_value.append(&mut hex::decode("0301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944").unwrap());
+ // Witness 2
+ target_value.append(&mut hex::decode("006c").unwrap()); // len of witness_data
+ target_value.append(&mut hex::decode("02").unwrap()); // num_witness_elements (VarInt)
+ target_value.append(&mut hex::decode("48").unwrap()); // len of witness element data (VarInt)
+ target_value.append(&mut hex::decode("3045022100ee00dbf4a862463e837d7c08509de814d620e4d9830fa84818713e0fa358f145022021c3c7060c4d53fe84fd165d60208451108a778c13b92ca4c6bad439236126cc01").unwrap());
+ target_value.append(&mut hex::decode("21").unwrap()); // len of witness element data (VarInt)
+ target_value.append(&mut hex::decode("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: [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 = hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap(); // channel_id
+ target_value.append(&mut hex::decode("12345678").unwrap()); // locktime
+ target_value.append(&mut hex::decode("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 hex::decode(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: [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 = hex::decode("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 hex::decode(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: [2; 32],
+ data: hex::decode("54686520717569636B2062726F776E20666F78206A756D7073206F76657220746865206C617A7920646F672E").unwrap(),
+ };
+ let encoded_value = tx_abort.encode();
+ let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202002C54686520717569636B2062726F776E20666F78206A756D7073206F76657220746865206C617A7920646F672E").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+