Skip to content

Commit b51721f

Browse files
author
Antoine Riard
committed
Underscore TxCreationKeys ownership
A TxCreationKeys set represents the key which will be embedded in output scripts of a party's commitment tx state. Among them there is a always a key belonging to counter-party, the HTLC pubkey. To dissociate strongly, prefix keys with broadcaster/countersignatory. A revocation keypair is attributed to the broadcaster as it's used to punish a fraudulent broadcast while minding that such keypair derivation method will be always used by countersignatory as it's its task to enforce punishement thanks to the release secret.
1 parent c6a91f2 commit b51721f

File tree

9 files changed

+55
-53
lines changed

9 files changed

+55
-53
lines changed

lightning-c-bindings/src/chain/keysinterface.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ pub extern "C" fn InMemoryChannelKeys_new(mut funding_key: crate::c_types::Secre
666666
crate::chain::keysinterface::InMemoryChannelKeys { inner: Box::into_raw(Box::new(ret)), is_owned: true }
667667
}
668668

669-
/// Remote pubkeys.
669+
/// Counterparty pubkeys.
670670
/// Will panic if on_accept wasn't called.
671671
#[must_use]
672672
#[no_mangle]
@@ -675,10 +675,10 @@ pub extern "C" fn InMemoryChannelKeys_counterparty_pubkeys(this_arg: &InMemoryCh
675675
crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
676676
}
677677

678-
/// The to_self_delay value specified by our counterparty and applied on locally-broadcastable
678+
/// The contest delay value specified by our counterparty and applied on holder-broadcastable
679679
/// transactions, ie the amount of time that we have to wait to recover our funds if we
680680
/// broadcast a transaction. You'll likely want to pass this to the
681-
/// ln::chan_utils::build*_transaction functions when signing local transactions.
681+
/// ln::chan_utils::build*_transaction functions when signing holder transactions.
682682
/// Will panic if on_accept wasn't called.
683683
#[must_use]
684684
#[no_mangle]
@@ -687,7 +687,7 @@ pub extern "C" fn InMemoryChannelKeys_counterparty_selected_contest_delay(this_a
687687
ret
688688
}
689689

690-
/// The to_self_delay value specified by us and applied on transactions broadcastable
690+
/// The to_contest delay value specified by us and applied on transactions broadcastable
691691
/// by our counterparty, ie the amount of time that they have to wait to recover their funds
692692
/// if they broadcast a transaction.
693693
/// Will panic if on_accept wasn't called.

lightning-c-bindings/src/ln/chan_utils.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,24 +167,24 @@ pub extern "C" fn TxCreationKeys_set_countersignatory_htlc_key(this_ptr: &mut Tx
167167
}
168168
/// Payment Key (which isn't allowed to be spent from for some delay)
169169
#[no_mangle]
170-
pub extern "C" fn TxCreationKeys_get_delayed_payment_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
171-
let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.delayed_payment_key;
170+
pub extern "C" fn TxCreationKeys_get_broadcaster_delayed_payment_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
171+
let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.broadcaster_delayed_payment_key;
172172
crate::c_types::PublicKey::from_rust(&(*inner_val))
173173
}
174174
/// Payment Key (which isn't allowed to be spent from for some delay)
175175
#[no_mangle]
176-
pub extern "C" fn TxCreationKeys_set_delayed_payment_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
177-
unsafe { &mut *this_ptr.inner }.delayed_payment_key = val.into_rust();
176+
pub extern "C" fn TxCreationKeys_set_broadcaster_delayed_payment_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
177+
unsafe { &mut *this_ptr.inner }.broadcaster_delayed_payment_key = val.into_rust();
178178
}
179179
#[must_use]
180180
#[no_mangle]
181-
pub extern "C" fn TxCreationKeys_new(mut per_commitment_point_arg: crate::c_types::PublicKey, mut revocation_key_arg: crate::c_types::PublicKey, mut broadcaster_htlc_key_arg: crate::c_types::PublicKey, mut countersignatory_htlc_key_arg: crate::c_types::PublicKey, mut delayed_payment_key_arg: crate::c_types::PublicKey) -> TxCreationKeys {
181+
pub extern "C" fn TxCreationKeys_new(mut per_commitment_point_arg: crate::c_types::PublicKey, mut revocation_key_arg: crate::c_types::PublicKey, mut broadcaster_htlc_key_arg: crate::c_types::PublicKey, mut countersignatory_htlc_key_arg: crate::c_types::PublicKey, mut broadcaster_delayed_payment_key_arg: crate::c_types::PublicKey) -> TxCreationKeys {
182182
TxCreationKeys { inner: Box::into_raw(Box::new(nativeTxCreationKeys {
183183
per_commitment_point: per_commitment_point_arg.into_rust(),
184184
revocation_key: revocation_key_arg.into_rust(),
185185
broadcaster_htlc_key: broadcaster_htlc_key_arg.into_rust(),
186186
countersignatory_htlc_key: countersignatory_htlc_key_arg.into_rust(),
187-
delayed_payment_key: delayed_payment_key_arg.into_rust(),
187+
broadcaster_delayed_payment_key: broadcaster_delayed_payment_key_arg.into_rust(),
188188
})), is_owned: true }
189189
}
190190
#[no_mangle]
@@ -422,11 +422,11 @@ pub extern "C" fn TxCreationKeys_derive_new(per_commitment_point: crate::c_types
422422
}
423423

424424
/// A script either spendable by the revocation
425-
/// key or the delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
425+
/// key or the broadcaster_delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
426426
/// Encumbering a `to_local` output on a commitment transaction or 2nd-stage HTLC transactions.
427427
#[no_mangle]
428-
pub extern "C" fn get_revokeable_redeemscript(revocation_key: crate::c_types::PublicKey, mut to_self_delay: u16, delayed_payment_key: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z {
429-
let mut ret = lightning::ln::chan_utils::get_revokeable_redeemscript(&revocation_key.into_rust(), to_self_delay, &delayed_payment_key.into_rust());
428+
pub extern "C" fn get_revokeable_redeemscript(revocation_key: crate::c_types::PublicKey, mut to_self_delay: u16, broadcaster_delayed_payment_key: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z {
429+
let mut ret = lightning::ln::chan_utils::get_revokeable_redeemscript(&revocation_key.into_rust(), to_self_delay, &broadcaster_delayed_payment_key.into_rust());
430430
ret.into_bytes().into()
431431
}
432432

@@ -563,8 +563,8 @@ pub extern "C" fn make_funding_redeemscript(a: crate::c_types::PublicKey, b: cra
563563

564564
/// panics if htlc.transaction_output_index.is_none()!
565565
#[no_mangle]
566-
pub extern "C" fn build_htlc_transaction(prev_hash: *const [u8; 32], mut feerate_per_kw: u32, mut to_self_delay: u16, htlc: &crate::ln::chan_utils::HTLCOutputInCommitment, a_delayed_payment_key: crate::c_types::PublicKey, revocation_key: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z {
567-
let mut ret = lightning::ln::chan_utils::build_htlc_transaction(&::bitcoin::hash_types::Txid::from_slice(&unsafe { &*prev_hash }[..]).unwrap(), feerate_per_kw, to_self_delay, unsafe { &*htlc.inner }, &a_delayed_payment_key.into_rust(), &revocation_key.into_rust());
566+
pub extern "C" fn build_htlc_transaction(prev_hash: *const [u8; 32], mut feerate_per_kw: u32, mut to_self_delay: u16, htlc: &crate::ln::chan_utils::HTLCOutputInCommitment, broadcaster_delayed_payment_key: crate::c_types::PublicKey, revocation_key: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z {
567+
let mut ret = lightning::ln::chan_utils::build_htlc_transaction(&::bitcoin::hash_types::Txid::from_slice(&unsafe { &*prev_hash }[..]).unwrap(), feerate_per_kw, to_self_delay, unsafe { &*htlc.inner }, &broadcaster_delayed_payment_key.into_rust(), &revocation_key.into_rust());
568568
let mut local_ret = ::bitcoin::consensus::encode::serialize(&ret);
569569
local_ret.into()
570570
}

lightning/src/chain/keysinterface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ impl ChannelKeys for InMemoryChannelKeys {
485485
let mut htlc_sigs = Vec::with_capacity(htlcs.len());
486486
for ref htlc in htlcs {
487487
if let Some(_) = htlc.transaction_output_index {
488-
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, feerate_per_kw, accepted_data.locally_selected_contest_delay, htlc, &keys.delayed_payment_key, &keys.revocation_key);
488+
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, feerate_per_kw, accepted_data.locally_selected_contest_delay, htlc, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
489489
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &keys);
490490
let htlc_sighash = hash_to_message!(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, SigHashType::All)[..]);
491491
let our_htlc_key = match chan_utils::derive_private_key(&secp_ctx, &keys.per_commitment_point, &self.htlc_base_key) {

lightning/src/ln/chan_utils.rs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -217,32 +217,33 @@ pub fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_com
217217

218218
/// Derives a per-commitment-transaction revocation key from its constituent parts.
219219
///
220+
/// Only the cheating participant owns a valid witness to propagate a revoked
221+
/// commitment transaction, thus per_commitment_secret always come from cheater
222+
/// and revocation_base_secret always come from punisher, which is the broadcaster
223+
/// of the transaction spending with this key knowledge.
224+
///
220225
/// Note that this is infallible iff we trust that at least one of the two input keys are randomly
221226
/// generated (ie our own).
222-
pub fn derive_private_revocation_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_secret: &SecretKey, revocation_base_secret: &SecretKey) -> Result<SecretKey, SecpError> {
223-
let revocation_base_point = PublicKey::from_secret_key(&secp_ctx, &revocation_base_secret);
227+
pub fn derive_private_revocation_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_secret: &SecretKey, countersignatory_revocation_base_secret: &SecretKey) -> Result<SecretKey, SecpError> {
228+
let countersignatory_revocation_base_point = PublicKey::from_secret_key(&secp_ctx, &countersignatory_revocation_base_secret);
224229
let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
225230

226231
let rev_append_commit_hash_key = {
227232
let mut sha = Sha256::engine();
228-
sha.input(&revocation_base_point.serialize());
233+
sha.input(&countersignatory_revocation_base_point.serialize());
229234
sha.input(&per_commitment_point.serialize());
230235

231236
Sha256::from_engine(sha).into_inner()
232237
};
233238
let commit_append_rev_hash_key = {
234239
let mut sha = Sha256::engine();
235240
sha.input(&per_commitment_point.serialize());
236-
sha.input(&revocation_base_point.serialize());
241+
sha.input(&countersignatory_revocation_base_point.serialize());
237242

238243
Sha256::from_engine(sha).into_inner()
239244
};
240245

241-
// Only the transaction broadcaster owns a valid witness to propagate
242-
// a revoked commitment transaction, thus per_commitment_secret always
243-
// come from broadcaster and revocation_base_secret always come
244-
// from countersignatory of the transaction.
245-
let mut countersignatory_contrib = revocation_base_secret.clone();
246+
let mut countersignatory_contrib = countersignatory_revocation_base_secret.clone();
246247
countersignatory_contrib.mul_assign(&rev_append_commit_hash_key)?;
247248
let mut broadcaster_contrib = per_commitment_secret.clone();
248249
broadcaster_contrib.mul_assign(&commit_append_rev_hash_key)?;
@@ -254,29 +255,30 @@ pub fn derive_private_revocation_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1
254255
/// the public equivalend of derive_private_revocation_key - using only public keys to derive a
255256
/// public key instead of private keys.
256257
///
258+
/// Only the cheating participant owns a valid witness to propagate a revoked
259+
/// commitment transaction, thus per_commitment_point always come from cheater
260+
/// and revocation_base_point always come from punisher, which is the broadcaster
261+
/// of the transaction spending with this key knowledge.
262+
///
257263
/// Note that this is infallible iff we trust that at least one of the two input keys are randomly
258264
/// generated (ie our own).
259-
pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, revocation_base_point: &PublicKey) -> Result<PublicKey, SecpError> {
265+
pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, countersignatory_revocation_base_point: &PublicKey) -> Result<PublicKey, SecpError> {
260266
let rev_append_commit_hash_key = {
261267
let mut sha = Sha256::engine();
262-
sha.input(&revocation_base_point.serialize());
268+
sha.input(&countersignatory_revocation_base_point.serialize());
263269
sha.input(&per_commitment_point.serialize());
264270

265271
Sha256::from_engine(sha).into_inner()
266272
};
267273
let commit_append_rev_hash_key = {
268274
let mut sha = Sha256::engine();
269275
sha.input(&per_commitment_point.serialize());
270-
sha.input(&revocation_base_point.serialize());
276+
sha.input(&countersignatory_revocation_base_point.serialize());
271277

272278
Sha256::from_engine(sha).into_inner()
273279
};
274280

275-
// Only the transaction broadcaster owns a valid witness to propagate
276-
// a revoked commitment transaction, thus per_commitment_point always
277-
// come from broadcaster and revocation_base_point always come
278-
// from countersignatory of the transaction.
279-
let mut countersignatory_contrib = revocation_base_point.clone();
281+
let mut countersignatory_contrib = countersignatory_revocation_base_point.clone();
280282
countersignatory_contrib.mul_assign(&secp_ctx, &rev_append_commit_hash_key)?;
281283
let mut broadcaster_contrib = per_commitment_point.clone();
282284
broadcaster_contrib.mul_assign(&secp_ctx, &commit_append_rev_hash_key)?;
@@ -298,7 +300,7 @@ pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp2
298300
pub struct TxCreationKeys {
299301
/// The broadcaster's per-commitment public key which was used to derive the other keys.
300302
pub per_commitment_point: PublicKey,
301-
/// The broadcaster's revocation key which is used to allow the broadcaster of the commitment
303+
/// The revocation key which is used to allow the broadcaster of the commitment
302304
/// transaction to provide their counterparty the ability to punish them if they broadcast
303305
/// an old state.
304306
pub revocation_key: PublicKey,
@@ -307,10 +309,10 @@ pub struct TxCreationKeys {
307309
/// Countersignatory's HTLC Key
308310
pub countersignatory_htlc_key: PublicKey,
309311
/// Broadcaster's Payment Key (which isn't allowed to be spent from for some delay)
310-
pub delayed_payment_key: PublicKey,
312+
pub broadcaster_delayed_payment_key: PublicKey,
311313
}
312314
impl_writeable!(TxCreationKeys, 33*6,
313-
{ per_commitment_point, revocation_key, broadcaster_htlc_key, countersignatory_htlc_key, delayed_payment_key });
315+
{ per_commitment_point, revocation_key, broadcaster_htlc_key, countersignatory_htlc_key, broadcaster_delayed_payment_key });
314316

315317
/// The per-commitment point and a set of pre-calculated public keys used for transaction creation
316318
/// in the signer.
@@ -377,22 +379,22 @@ impl TxCreationKeys {
377379
revocation_key: derive_public_revocation_key(&secp_ctx, &per_commitment_point, &countersignatory_revocation_base)?,
378380
broadcaster_htlc_key: derive_public_key(&secp_ctx, &per_commitment_point, &broadcaster_htlc_base)?,
379381
countersignatory_htlc_key: derive_public_key(&secp_ctx, &per_commitment_point, &countersignatory_htlc_base)?,
380-
delayed_payment_key: derive_public_key(&secp_ctx, &per_commitment_point, &broadcaster_delayed_payment_base)?,
382+
broadcaster_delayed_payment_key: derive_public_key(&secp_ctx, &per_commitment_point, &broadcaster_delayed_payment_base)?,
381383
})
382384
}
383385
}
384386

385387
/// A script either spendable by the revocation
386-
/// key or the delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
388+
/// key or the broadcaster_delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
387389
/// Encumbering a `to_local` output on a commitment transaction or 2nd-stage HTLC transactions.
388-
pub fn get_revokeable_redeemscript(revocation_key: &PublicKey, contest_delay: u16, delayed_payment_key: &PublicKey) -> Script {
390+
pub fn get_revokeable_redeemscript(revocation_key: &PublicKey, contest_delay: u16, broadcaster_delayed_payment_key: &PublicKey) -> Script {
389391
Builder::new().push_opcode(opcodes::all::OP_IF)
390392
.push_slice(&revocation_key.serialize())
391393
.push_opcode(opcodes::all::OP_ELSE)
392394
.push_int(contest_delay as i64)
393395
.push_opcode(opcodes::all::OP_CSV)
394396
.push_opcode(opcodes::all::OP_DROP)
395-
.push_slice(&delayed_payment_key.serialize())
397+
.push_slice(&broadcaster_delayed_payment_key.serialize())
396398
.push_opcode(opcodes::all::OP_ENDIF)
397399
.push_opcode(opcodes::all::OP_CHECKSIG)
398400
.into_script()
@@ -516,7 +518,7 @@ pub fn make_funding_redeemscript(broadcaster: &PublicKey, countersignatory: &Pub
516518
}
517519

518520
/// panics if htlc.transaction_output_index.is_none()!
519-
pub fn build_htlc_transaction(prev_hash: &Txid, feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
521+
pub fn build_htlc_transaction(prev_hash: &Txid, feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, broadcaster_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
520522
let mut txins: Vec<TxIn> = Vec::new();
521523
txins.push(TxIn {
522524
previous_output: OutPoint {
@@ -536,7 +538,7 @@ pub fn build_htlc_transaction(prev_hash: &Txid, feerate_per_kw: u32, contest_del
536538

537539
let mut txouts: Vec<TxOut> = Vec::new();
538540
txouts.push(TxOut {
539-
script_pubkey: get_revokeable_redeemscript(revocation_key, contest_delay, delayed_payment_key).to_v0_p2wsh(),
541+
script_pubkey: get_revokeable_redeemscript(revocation_key, contest_delay, broadcaster_delayed_payment_key).to_v0_p2wsh(),
540542
value: htlc.amount_msat / 1000 - total_fee //TODO: BOLT 3 does not specify if we should add amount_msat before dividing or if we should divide by 1000 before subtracting (as we do here)
541543
});
542544

@@ -605,7 +607,7 @@ impl LocalCommitmentTransaction {
605607
revocation_key: dummy_key.clone(),
606608
broadcaster_htlc_key: dummy_key.clone(),
607609
countersignatory_htlc_key: dummy_key.clone(),
608-
delayed_payment_key: dummy_key.clone(),
610+
broadcaster_delayed_payment_key: dummy_key.clone(),
609611
},
610612
feerate_per_kw: 0,
611613
per_htlc: Vec::new()
@@ -701,7 +703,7 @@ impl LocalCommitmentTransaction {
701703

702704
for this_htlc in self.per_htlc.iter() {
703705
if this_htlc.0.transaction_output_index.is_some() {
704-
let htlc_tx = build_htlc_transaction(&txid, self.feerate_per_kw, local_csv, &this_htlc.0, &self.local_keys.delayed_payment_key, &self.local_keys.revocation_key);
706+
let htlc_tx = build_htlc_transaction(&txid, self.feerate_per_kw, local_csv, &this_htlc.0, &self.local_keys.broadcaster_delayed_payment_key, &self.local_keys.revocation_key);
705707

706708
let htlc_redeemscript = get_htlc_redeemscript_with_explicit_keys(&this_htlc.0, &self.local_keys.broadcaster_htlc_key, &self.local_keys.countersignatory_htlc_key, &self.local_keys.revocation_key);
707709

@@ -724,7 +726,7 @@ impl LocalCommitmentTransaction {
724726
// Further, we should never be provided the preimage for an HTLC-Timeout transaction.
725727
if this_htlc.0.offered && preimage.is_some() { unreachable!(); }
726728

727-
let mut htlc_tx = build_htlc_transaction(&txid, self.feerate_per_kw, local_csv, &this_htlc.0, &self.local_keys.delayed_payment_key, &self.local_keys.revocation_key);
729+
let mut htlc_tx = build_htlc_transaction(&txid, self.feerate_per_kw, local_csv, &this_htlc.0, &self.local_keys.broadcaster_delayed_payment_key, &self.local_keys.revocation_key);
728730
// Channel should have checked that we have a remote signature for this HTLC at
729731
// creation, and we should have a sensible htlc transaction:
730732
assert!(this_htlc.1.is_some());

0 commit comments

Comments
 (0)