Skip to content

Commit 7efaf2e

Browse files
authored
Merge pull request #230 from ariard/handle_sizeable_push_msat
Handle sizeable push msat (fix #195) + handle two first per_commitment_point + keys interface tests
2 parents b14baa0 + 3a7b40e commit 7efaf2e

File tree

4 files changed

+739
-147
lines changed

4 files changed

+739
-147
lines changed

src/chain/keysinterface.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +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,
54+
/// The output which is referenced by the given outpoint
55+
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,
5368
}
5469
}
5570

src/ln/channel.rs

Lines changed: 17 additions & 8 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, &keys_provider.get_shutdown_pubkey(), BREAKDOWN_TIMEOUT,
440440
keys_provider.get_destination_script(), logger.clone());
441441

442442
Ok(Channel {
@@ -624,9 +624,10 @@ 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, &keys_provider.get_shutdown_pubkey(), BREAKDOWN_TIMEOUT,
628628
keys_provider.get_destination_script(), logger.clone());
629629
channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
630+
channel_monitor.provide_their_next_revocation_point(Some((INITIAL_COMMITMENT_NUMBER, msg.first_per_commitment_point)));
630631
channel_monitor.set_their_to_self_delay(msg.to_self_delay);
631632

632633
let mut chan = Channel {
@@ -1351,6 +1352,7 @@ impl Channel {
13511352
}
13521353

13531354
self.channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
1355+
self.channel_monitor.provide_their_next_revocation_point(Some((INITIAL_COMMITMENT_NUMBER, msg.first_per_commitment_point)));
13541356

13551357
self.their_dust_limit_satoshis = msg.dust_limit_satoshis;
13561358
self.their_max_htlc_value_in_flight_msat = cmp::min(msg.max_htlc_value_in_flight_msat, self.channel_value_satoshis * 1000);
@@ -1375,22 +1377,25 @@ impl Channel {
13751377
Ok(())
13761378
}
13771379

1378-
fn funding_created_signature(&mut self, sig: &Signature) -> Result<(Transaction, Signature), HandleError> {
1380+
fn funding_created_signature(&mut self, sig: &Signature) -> Result<(Transaction, Transaction, Signature, TxCreationKeys), HandleError> {
13791381
let funding_script = self.get_funding_redeemscript();
13801382

13811383
let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?;
1382-
let local_initial_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, self.feerate_per_kw).0;
1384+
let mut local_initial_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, self.feerate_per_kw).0;
13831385
let local_sighash = Message::from_slice(&bip143::SighashComponents::new(&local_initial_commitment_tx).sighash_all(&local_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
13841386

1385-
// They sign the "local" commitment transaction, allowing us to broadcast the tx if we wish.
1387+
// They sign the "local" commitment transaction...
13861388
secp_call!(self.secp_ctx.verify(&local_sighash, &sig, &self.their_funding_pubkey.unwrap()), "Invalid funding_created signature from peer", self.channel_id());
13871389

1390+
// ...and we sign it, allowing us to broadcast the tx if we wish
1391+
self.sign_commitment_transaction(&mut local_initial_commitment_tx, sig);
1392+
13881393
let remote_keys = self.build_remote_transaction_keys()?;
13891394
let remote_initial_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false, self.feerate_per_kw).0;
13901395
let remote_sighash = Message::from_slice(&bip143::SighashComponents::new(&remote_initial_commitment_tx).sighash_all(&remote_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
13911396

13921397
// We sign the "remote" commitment transaction, allowing them to broadcast the tx if they wish.
1393-
Ok((remote_initial_commitment_tx, self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key)))
1398+
Ok((remote_initial_commitment_tx, local_initial_commitment_tx, self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key), local_keys))
13941399
}
13951400

13961401
pub fn funding_created(&mut self, msg: &msgs::FundingCreated) -> Result<(msgs::FundingSigned, ChannelMonitor), HandleError> {
@@ -1413,7 +1418,7 @@ impl Channel {
14131418
let funding_txo_script = self.get_funding_redeemscript().to_v0_p2wsh();
14141419
self.channel_monitor.set_funding_info((funding_txo, funding_txo_script));
14151420

1416-
let (remote_initial_commitment_tx, our_signature) = match self.funding_created_signature(&msg.signature) {
1421+
let (remote_initial_commitment_tx, local_initial_commitment_tx, our_signature, local_keys) = match self.funding_created_signature(&msg.signature) {
14171422
Ok(res) => res,
14181423
Err(e) => {
14191424
self.channel_monitor.unset_funding_info();
@@ -1424,6 +1429,8 @@ impl Channel {
14241429
// Now that we're past error-generating stuff, update our local state:
14251430

14261431
self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_initial_commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number);
1432+
self.last_local_commitment_txn = vec![local_initial_commitment_tx.clone()];
1433+
self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx, local_keys, self.feerate_per_kw, Vec::new());
14271434
self.channel_state = ChannelState::FundingSent as u32;
14281435
self.channel_id = funding_txo.to_channel_id();
14291436
self.cur_remote_commitment_transaction_number -= 1;
@@ -1493,6 +1500,7 @@ impl Channel {
14931500
return Err(ChannelError::Close("Peer sent a funding_locked at a strange time"));
14941501
}
14951502

1503+
self.channel_monitor.provide_their_next_revocation_point(Some((INITIAL_COMMITMENT_NUMBER - 1 , msg.next_per_commitment_point)));
14961504
self.their_prev_commitment_point = self.their_cur_commitment_point;
14971505
self.their_cur_commitment_point = Some(msg.next_per_commitment_point);
14981506
Ok(())
@@ -1872,7 +1880,8 @@ impl Channel {
18721880
return Err(HandleError{err: "Got a revoke commitment secret which didn't correspond to their current pubkey", action: None});
18731881
}
18741882
}
1875-
self.channel_monitor.provide_secret(self.cur_remote_commitment_transaction_number + 1, msg.per_commitment_secret, Some((self.cur_remote_commitment_transaction_number - 1, msg.next_per_commitment_point)))?;
1883+
self.channel_monitor.provide_secret(self.cur_remote_commitment_transaction_number + 1, msg.per_commitment_secret)?;
1884+
self.channel_monitor.provide_their_next_revocation_point(Some((self.cur_remote_commitment_transaction_number - 1, msg.next_per_commitment_point)));
18761885

18771886
// Update state now that we've passed all the can-fail calls...
18781887
// (note that we may still fail to generate the new commitment_signed message, but that's

0 commit comments

Comments
 (0)