+ use ::{hex, chain};
+ use prelude::*;
+ use ln::chan_utils::{CommitmentTransaction, TxCreationKeys, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, HTLCOutputInCommitment};
+ use bitcoin::secp256k1::{PublicKey, SecretKey, Secp256k1};
+ use util::test_utils;
+ use chain::keysinterface::{KeysInterface, BaseSign};
+ use bitcoin::Network;
+ use ln::PaymentHash;
+
+ #[test]
+ fn test_anchors() {
+ let secp_ctx = Secp256k1::new();
+
+ let seed = [42; 32];
+ let network = Network::Testnet;
+ let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
+ let signer = keys_provider.get_channel_signer(false, 3000);
+ let counterparty_signer = keys_provider.get_channel_signer(false, 3000);
+ let delayed_payment_base = &signer.pubkeys().delayed_payment_basepoint;
+ let per_commitment_secret = SecretKey::from_slice(&hex::decode("1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100").unwrap()[..]).unwrap();
+ let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
+ let htlc_basepoint = &signer.pubkeys().htlc_basepoint;
+ let holder_pubkeys = signer.pubkeys();
+ let counterparty_pubkeys = counterparty_signer.pubkeys();
+ let keys = TxCreationKeys::derive_new(&secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint).unwrap();
+ let channel_parameters = ChannelTransactionParameters {
+ holder_pubkeys: holder_pubkeys.clone(),
+ holder_selected_contest_delay: 0,
+ is_outbound_from_holder: false,
+ counterparty_parameters: Some(CounterpartyChannelTransactionParameters { pubkeys: counterparty_pubkeys.clone(), selected_contest_delay: 0 }),
+ funding_outpoint: Some(chain::transaction::OutPoint { txid: Default::default(), index: 0 })
+ };
+
+ let mut htlcs_with_aux: Vec<(_, ())> = Vec::new();
+
+ // Generate broadcaster and counterparty outputs
+ let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
+ 0, 1000, 2000,
+ false,
+ holder_pubkeys.funding_pubkey,
+ counterparty_pubkeys.funding_pubkey,
+ keys.clone(), 1,
+ &mut htlcs_with_aux, &channel_parameters.as_holder_broadcastable()
+ );
+ assert_eq!(tx.built.transaction.output.len(), 2);
+
+ // Generate broadcaster and counterparty outputs as well as two anchors
+ let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
+ 0, 1000, 2000,
+ true,
+ holder_pubkeys.funding_pubkey,
+ counterparty_pubkeys.funding_pubkey,
+ keys.clone(), 1,
+ &mut htlcs_with_aux, &channel_parameters.as_holder_broadcastable()
+ );
+ assert_eq!(tx.built.transaction.output.len(), 4);
+
+ // Generate broadcaster output and anchor
+ let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
+ 0, 3000, 0,
+ true,
+ holder_pubkeys.funding_pubkey,
+ counterparty_pubkeys.funding_pubkey,
+ keys.clone(), 1,
+ &mut htlcs_with_aux, &channel_parameters.as_holder_broadcastable()
+ );
+ assert_eq!(tx.built.transaction.output.len(), 2);
+
+ // Generate counterparty output and anchor
+ let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
+ 0, 0, 3000,
+ true,
+ holder_pubkeys.funding_pubkey,
+ counterparty_pubkeys.funding_pubkey,
+ keys.clone(), 1,
+ &mut htlcs_with_aux, &channel_parameters.as_holder_broadcastable()
+ );
+ assert_eq!(tx.built.transaction.output.len(), 2);
+
+ // Generate broadcaster output, an HTLC output and two anchors
+ let payment_hash = PaymentHash([42; 32]);
+ let htlc_info = HTLCOutputInCommitment {
+ offered: false,
+ amount_msat: 1000000,
+ cltv_expiry: 100,
+ payment_hash,
+ transaction_output_index: None,
+ };
+
+ let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
+ 0, 3000, 0,
+ true,
+ holder_pubkeys.funding_pubkey,
+ counterparty_pubkeys.funding_pubkey,
+ keys.clone(), 1,
+ &mut vec![(htlc_info, ())], &channel_parameters.as_holder_broadcastable()
+ );
+ assert_eq!(tx.built.transaction.output.len(), 4);
+ }