Skip to content

Commit 3518f1f

Browse files
author
Antoine Riard
committed
Add test_static_spendable_outputs_preimage_tx
Aims to covered both keysinterace preimage tx case and detection of second remote commitment tx Split DynamicDescriptor between *P2WSH and *P2WKH
1 parent 1b33064 commit 3518f1f

File tree

4 files changed

+231
-32
lines changed

4 files changed

+231
-32
lines changed

src/chain/keysinterface.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,34 @@ pub enum SpendableOutputDescriptor {
3737
/// The output which is referenced by the given outpoint
3838
output: TxOut,
3939
},
40-
/// Outpoint commits to a P2WSH, should be spend by the following witness :
40+
/// Outpoint commits to a P2WSH
41+
/// P2WSH should be spend by the following witness :
4142
/// <local_delayedsig> 0 <witnessScript>
4243
/// With input nSequence set to_self_delay.
43-
/// Outputs from a HTLC-Success/Timeout tx
44-
DynamicOutput {
44+
/// Outputs from a HTLC-Success/Timeout tx/commitment tx
45+
DynamicOutputP2WSH {
4546
/// Outpoint spendable by user wallet
4647
outpoint: OutPoint,
47-
/// local_delayedkey = delayed_payment_basepoint_secret + SHA256(per_commitment_point || delayed_payment_basepoint)
48-
local_delayedkey: SecretKey,
49-
/// witness redeemScript encumbering output
48+
/// local_delayedkey = delayed_payment_basepoint_secret + SHA256(per_commitment_point || delayed_payment_basepoint) OR
49+
key: SecretKey,
50+
/// witness redeemScript encumbering output.
5051
witness_script: Script,
5152
/// nSequence input must commit to self_delay to satisfy script's OP_CSV
5253
to_self_delay: u16,
5354
/// The output which is referenced by the given outpoint
5455
output: TxOut,
56+
},
57+
/// Outpoint commits to a P2WPKH
58+
/// P2WPKH should be spend by the following witness :
59+
/// <local_sig> <local_pubkey>
60+
/// Outputs to_remote from a commitment tx
61+
DynamicOutputP2WPKH {
62+
/// Outpoint spendable by user wallet
63+
outpoint: OutPoint,
64+
/// localkey = payment_basepoint_secret + SHA256(per_commitment_point || payment_basepoint
65+
key: SecretKey,
66+
/// The output which is reference by the given outpoint
67+
output: TxOut,
5568
}
5669
}
5770

src/ln/channel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ impl Channel {
436436

437437
let secp_ctx = Secp256k1::new();
438438
let channel_monitor = ChannelMonitor::new(&chan_keys.revocation_base_key, &chan_keys.delayed_payment_base_key,
439-
&chan_keys.htlc_base_key, BREAKDOWN_TIMEOUT,
439+
&chan_keys.htlc_base_key, &chan_keys.payment_base_key, BREAKDOWN_TIMEOUT,
440440
keys_provider.get_destination_script(), logger.clone());
441441

442442
Ok(Channel {
@@ -624,7 +624,7 @@ impl Channel {
624624

625625
let secp_ctx = Secp256k1::new();
626626
let mut channel_monitor = ChannelMonitor::new(&chan_keys.revocation_base_key, &chan_keys.delayed_payment_base_key,
627-
&chan_keys.htlc_base_key, BREAKDOWN_TIMEOUT,
627+
&chan_keys.htlc_base_key, &chan_keys.payment_base_key, BREAKDOWN_TIMEOUT,
628628
keys_provider.get_destination_script(), logger.clone());
629629
channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
630630
channel_monitor.provide_their_next_revocation_point(Some((INITIAL_COMMITMENT_NUMBER, msg.first_per_commitment_point)));

src/ln/channelmanager.rs

Lines changed: 147 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3225,6 +3225,8 @@ mod tests {
32253225

32263226
use bitcoin::util::hash::{BitcoinHash, Sha256dHash};
32273227
use bitcoin::util::bip143;
3228+
use bitcoin::util::address::Address;
3229+
use bitcoin::util::bip32::{ChildNumber, ExtendedPubKey, ExtendedPrivKey};
32283230
use bitcoin::blockdata::block::{Block, BlockHeader};
32293231
use bitcoin::blockdata::transaction::{Transaction, TxOut, TxIn, SigHashType};
32303232
use bitcoin::blockdata::script::{Builder, Script};
@@ -7536,7 +7538,7 @@ mod tests {
75367538
} else { panic!("Unexpected result"); }
75377539
}
75387540

7539-
macro_rules! check_dynamic_output {
7541+
macro_rules! check_dynamic_output_p2wsh {
75407542
($node: expr) => {
75417543
{
75427544
let events = $node.chan_monitor.simple_monitor.get_and_clear_pending_events();
@@ -7546,7 +7548,7 @@ mod tests {
75467548
Event::SpendableOutputs { ref outputs } => {
75477549
for outp in outputs {
75487550
match *outp {
7549-
SpendableOutputDescriptor::DynamicOutput { ref outpoint, ref local_delayedkey, ref witness_script, ref to_self_delay, ref output } => {
7551+
SpendableOutputDescriptor::DynamicOutputP2WSH { ref outpoint, ref key, ref witness_script, ref to_self_delay, ref output } => {
75507552
let input = TxIn {
75517553
previous_output: outpoint.clone(),
75527554
script_sig: Script::new(),
@@ -7563,9 +7565,9 @@ mod tests {
75637565
input: vec![input],
75647566
output: vec![outp],
75657567
};
7566-
let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], witness_script, output.value)[..]).unwrap();
75677568
let secp_ctx = Secp256k1::new();
7568-
let local_delaysig = secp_ctx.sign(&sighash, local_delayedkey);
7569+
let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], witness_script, output.value)[..]).unwrap();
7570+
let local_delaysig = secp_ctx.sign(&sighash, key);
75697571
spend_tx.input[0].witness.push(local_delaysig.serialize_der(&secp_ctx).to_vec());
75707572
spend_tx.input[0].witness[0].push(SigHashType::All as u8);
75717573
spend_tx.input[0].witness.push(vec!(0));
@@ -7584,6 +7586,106 @@ mod tests {
75847586
}
75857587
}
75867588

7589+
macro_rules! check_dynamic_output_p2wpkh {
7590+
($node: expr) => {
7591+
{
7592+
let events = $node.chan_monitor.simple_monitor.get_and_clear_pending_events();
7593+
let mut txn = Vec::new();
7594+
for event in events {
7595+
match event {
7596+
Event::SpendableOutputs { ref outputs } => {
7597+
for outp in outputs {
7598+
match *outp {
7599+
SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
7600+
let input = TxIn {
7601+
previous_output: outpoint.clone(),
7602+
script_sig: Script::new(),
7603+
sequence: 0,
7604+
witness: Vec::new(),
7605+
};
7606+
let outp = TxOut {
7607+
script_pubkey: Builder::new().push_opcode(opcodes::All::OP_RETURN).into_script(),
7608+
value: output.value,
7609+
};
7610+
let mut spend_tx = Transaction {
7611+
version: 2,
7612+
lock_time: 0,
7613+
input: vec![input],
7614+
output: vec![outp],
7615+
};
7616+
let secp_ctx = Secp256k1::new();
7617+
let remotepubkey = PublicKey::from_secret_key(&secp_ctx, &key);
7618+
let witness_script = Address::p2pkh(&remotepubkey, Network::Testnet).script_pubkey();
7619+
let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap();
7620+
let remotesig = secp_ctx.sign(&sighash, key);
7621+
spend_tx.input[0].witness.push(remotesig.serialize_der(&secp_ctx).to_vec());
7622+
spend_tx.input[0].witness[0].push(SigHashType::All as u8);
7623+
spend_tx.input[0].witness.push(remotepubkey.serialize().to_vec());
7624+
txn.push(spend_tx);
7625+
},
7626+
_ => panic!("Unexpected event"),
7627+
}
7628+
}
7629+
},
7630+
_ => panic!("Unexpected event"),
7631+
};
7632+
}
7633+
txn
7634+
}
7635+
}
7636+
}
7637+
7638+
macro_rules! check_static_output {
7639+
($event: expr, $node: expr, $event_idx: expr, $output_idx: expr, $der_idx: expr, $idx_node: expr) => {
7640+
match $event[$event_idx] {
7641+
Event::SpendableOutputs { ref outputs } => {
7642+
match outputs[$output_idx] {
7643+
SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output } => {
7644+
let secp_ctx = Secp256k1::new();
7645+
let input = TxIn {
7646+
previous_output: outpoint.clone(),
7647+
script_sig: Script::new(),
7648+
sequence: 0,
7649+
witness: Vec::new(),
7650+
};
7651+
let outp = TxOut {
7652+
script_pubkey: Builder::new().push_opcode(opcodes::All::OP_RETURN).into_script(),
7653+
value: output.value,
7654+
};
7655+
let mut spend_tx = Transaction {
7656+
version: 2,
7657+
lock_time: 0,
7658+
input: vec![input],
7659+
output: vec![outp.clone()],
7660+
};
7661+
let secret = {
7662+
match ExtendedPrivKey::new_master(&secp_ctx, Network::Testnet, &$node[$idx_node].node_seed) {
7663+
Ok(master_key) => {
7664+
match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx($der_idx)) {
7665+
Ok(key) => key,
7666+
Err(_) => panic!("Your RNG is busted"),
7667+
}
7668+
}
7669+
Err(_) => panic!("Your rng is busted"),
7670+
}
7671+
};
7672+
let pubkey = ExtendedPubKey::from_private(&secp_ctx, &secret).public_key;
7673+
let witness_script = Address::p2pkh(&pubkey, Network::Testnet).script_pubkey();
7674+
let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap();
7675+
let sig = secp_ctx.sign(&sighash, &secret.secret_key);
7676+
spend_tx.input[0].witness.push(sig.serialize_der(&secp_ctx).to_vec());
7677+
spend_tx.input[0].witness[0].push(SigHashType::All as u8);
7678+
spend_tx.input[0].witness.push(pubkey.serialize().to_vec());
7679+
spend_tx
7680+
},
7681+
_ => panic!("Unexpected event !"),
7682+
}
7683+
},
7684+
_ => panic!("Unexpected event !"),
7685+
};
7686+
}
7687+
}
7688+
75877689
#[test]
75887690
fn test_claim_sizeable_push_msat() {
75897691
// Incidentally test SpendableOutput event generation due to detection of to_local output on commitment tx
@@ -7603,8 +7705,48 @@ mod tests {
76037705

76047706
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
76057707
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, 0);
7606-
let spend_txn = check_dynamic_output!(nodes[1]);
7708+
let spend_txn = check_dynamic_output_p2wsh!(nodes[1]);
76077709
assert_eq!(spend_txn.len(), 1);
76087710
check_spends!(spend_txn[0], node_txn[0].clone());
76097711
}
7712+
7713+
#[test]
7714+
fn test_static_spendable_outputs_preimage_tx() {
7715+
let nodes = create_network(2);
7716+
7717+
// Create some initial channels
7718+
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
7719+
7720+
let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
7721+
7722+
let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
7723+
assert_eq!(commitment_tx[0].input.len(), 1);
7724+
assert_eq!(commitment_tx[0].input[0].previous_output.txid, chan_1.3.txid());
7725+
7726+
// Settle A's commitment tx on B's chain
7727+
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
7728+
assert!(nodes[1].node.claim_funds(payment_preimage));
7729+
check_added_monitors!(nodes[1], 1);
7730+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()] }, 1);
7731+
let events = nodes[1].node.get_and_clear_pending_msg_events();
7732+
match events[0] {
7733+
MessageSendEvent::UpdateHTLCs { .. } => {},
7734+
_ => panic!("Unexpected event"),
7735+
}
7736+
match events[1] {
7737+
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
7738+
_ => panic!("Unexepected event"),
7739+
}
7740+
7741+
// Check B's monitor was able to send back output descriptor event for preimage tx on A's commitment tx
7742+
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); // ChannelManager : 1 (local commitment tx), ChannelMonitor: 2 (1 preimage tx) * 2 (block-rescan)
7743+
check_spends!(node_txn[0], commitment_tx[0].clone());
7744+
assert_eq!(node_txn[0], node_txn[2]);
7745+
assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 133);
7746+
check_spends!(node_txn[1], chan_1.3.clone());
7747+
7748+
let events = nodes[1].chan_monitor.simple_monitor.get_and_clear_pending_events();
7749+
let spend_tx = check_static_output!(events, nodes, 0, 0, 1, 1);
7750+
check_spends!(spend_tx, node_txn[0].clone());
7751+
}
76107752
}

0 commit comments

Comments
 (0)