Skip to content

Make payment_key derivation deterministic #3391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion fuzz/src/chanmon_consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ impl SignerProvider for KeyProvider {
.into_script())
}

fn get_shutdown_scriptpubkey(&self) -> Result<ShutdownScript, ()> {
fn get_shutdown_scriptpubkey(&self, _channel_keys_id: [u8; 32]) -> Result<ShutdownScript, ()> {
let secp_ctx = Secp256k1::signing_only();
#[rustfmt::skip]
let secret_key = SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, self.node_secret[31]]).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion fuzz/src/full_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ impl SignerProvider for KeyProvider {
.into_script())
}

fn get_shutdown_scriptpubkey(&self) -> Result<ShutdownScript, ()> {
fn get_shutdown_scriptpubkey(&self, _channel_keys_id: [u8; 32]) -> Result<ShutdownScript, ()> {
let secp_ctx = Secp256k1::signing_only();
let secret_key = SecretKey::from_slice(&[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand Down
2 changes: 1 addition & 1 deletion fuzz/src/onion_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl SignerProvider for KeyProvider {
unreachable!()
}

fn get_shutdown_scriptpubkey(&self) -> Result<ShutdownScript, ()> {
fn get_shutdown_scriptpubkey(&self, _channel_keys_id: [u8; 32]) -> Result<ShutdownScript, ()> {
unreachable!()
}
}
Expand Down
14 changes: 7 additions & 7 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {

assert!(commitment_transaction_number_obscure_factor <= (1 << 48));
let counterparty_payment_script = chan_utils::get_counterparty_payment_script(
&channel_parameters.channel_type_features, &keys.pubkeys().payment_point
&channel_parameters.channel_type_features, &keys.pubkeys().payment_basepoint
);

let counterparty_channel_parameters = channel_parameters.counterparty_parameters.as_ref().unwrap();
Expand Down Expand Up @@ -3298,7 +3298,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
commitment_tx_fee_satoshis,
anchor_descriptor: AnchorDescriptor {
channel_derivation_parameters: ChannelDerivationParameters {
keys_id: self.channel_keys_id,
channel_keys_id: self.channel_keys_id,
value_satoshis: self.channel_value_satoshis,
transaction_parameters: self.onchain_tx_handler.channel_transaction_parameters.clone(),
},
Expand All @@ -3322,7 +3322,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
for htlc in htlcs {
htlc_descriptors.push(HTLCDescriptor {
channel_derivation_parameters: ChannelDerivationParameters {
keys_id: self.channel_keys_id,
channel_keys_id: self.channel_keys_id,
value_satoshis: self.channel_value_satoshis,
transaction_parameters: self.onchain_tx_handler.channel_transaction_parameters.clone(),
},
Expand Down Expand Up @@ -4971,9 +4971,9 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
if onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx() &&
counterparty_payment_script.is_p2wpkh()
{
let payment_point = onchain_tx_handler.channel_transaction_parameters.holder_pubkeys.payment_point;
let payment_basepoint = onchain_tx_handler.channel_transaction_parameters.holder_pubkeys.payment_basepoint;
counterparty_payment_script =
chan_utils::get_to_countersignatory_with_anchors_redeemscript(&payment_point).to_p2wsh();
chan_utils::get_to_countersignatory_with_anchors_redeemscript(&payment_basepoint).to_p2wsh();
}

Ok((best_block.block_hash, ChannelMonitor::from_impl(ChannelMonitorImpl {
Expand Down Expand Up @@ -5243,7 +5243,7 @@ mod tests {
let counterparty_pubkeys = ChannelPublicKeys {
funding_pubkey: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[44; 32]).unwrap()),
revocation_basepoint: RevocationBasepoint::from(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap())),
payment_point: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap()),
payment_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap()),
delayed_payment_basepoint: DelayedPaymentBasepoint::from(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[47; 32]).unwrap())),
htlc_basepoint: HtlcBasepoint::from(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[48; 32]).unwrap()))
};
Expand Down Expand Up @@ -5495,7 +5495,7 @@ mod tests {
let counterparty_pubkeys = ChannelPublicKeys {
funding_pubkey: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[44; 32]).unwrap()),
revocation_basepoint: RevocationBasepoint::from(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap())),
payment_point: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap()),
payment_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap()),
delayed_payment_basepoint: DelayedPaymentBasepoint::from(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[47; 32]).unwrap())),
htlc_basepoint: HtlcBasepoint::from(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[48; 32]).unwrap())),
};
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/chain/onchaintx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
let htlc_descriptor = HTLCDescriptor {
channel_derivation_parameters: ChannelDerivationParameters {
value_satoshis: self.channel_value_satoshis,
keys_id: self.channel_keys_id,
channel_keys_id: self.channel_keys_id,
transaction_parameters: self.channel_transaction_parameters.clone(),
},
commitment_txid: trusted_tx.txid(),
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/events/bump_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl AnchorDescriptor {
{
let mut signer = signer_provider.derive_channel_signer(
self.channel_derivation_parameters.value_satoshis,
self.channel_derivation_parameters.keys_id,
self.channel_derivation_parameters.channel_keys_id,
);
signer.provide_channel_parameters(&self.channel_derivation_parameters.transaction_parameters);
signer
Expand Down Expand Up @@ -791,7 +791,7 @@ where

let mut signers = BTreeMap::new();
for (idx, htlc_descriptor) in htlc_descriptors.iter().enumerate() {
let signer = signers.entry(htlc_descriptor.channel_derivation_parameters.keys_id)
let signer = signers.entry(htlc_descriptor.channel_derivation_parameters.channel_keys_id)
.or_insert_with(|| htlc_descriptor.derive_channel_signer(&self.signer_provider));
let htlc_sig = signer.sign_holder_htlc_transaction(&htlc_tx, idx, htlc_descriptor, &self.secp)?;
let witness_script = htlc_descriptor.witness_script(&self.secp);
Expand Down Expand Up @@ -955,7 +955,7 @@ mod tests {
anchor_descriptor: AnchorDescriptor {
channel_derivation_parameters: ChannelDerivationParameters {
value_satoshis: 42_000_000,
keys_id: [42; 32],
channel_keys_id: [42; 32],
transaction_parameters: ChannelTransactionParameters::test_dummy(),
},
outpoint: OutPoint { txid: Txid::from_byte_array([42; 32]), vout: 0 },
Expand Down
5 changes: 3 additions & 2 deletions lightning/src/ln/async_signer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,8 +917,9 @@ fn do_test_closing_signed(extra_closing_signed: bool, reconnect: bool) {
if extra_closing_signed {
let node_1_closing_signed_2_bad = {
let mut node_1_closing_signed_2 = node_1_closing_signed.clone();
let holder_script = nodes[0].keys_manager.get_shutdown_scriptpubkey().unwrap();
let counterparty_script = nodes[1].keys_manager.get_shutdown_scriptpubkey().unwrap();
let channel_keys_id = [42u8; 32];
let holder_script = nodes[0].keys_manager.get_shutdown_scriptpubkey(channel_keys_id).unwrap();
let counterparty_script = nodes[1].keys_manager.get_shutdown_scriptpubkey(channel_keys_id).unwrap();
let funding_outpoint = bitcoin::OutPoint { txid: chan_1.3.compute_txid(), vout: 0 };
let closing_tx_2 = ClosingTransaction::new(50000, 0, holder_script.into(),
counterparty_script.into(), funding_outpoint);
Expand Down
38 changes: 19 additions & 19 deletions lightning/src/ln/chan_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ pub struct ChannelPublicKeys {
/// The public key on which the non-broadcaster (ie the countersignatory) receives an immediately
/// spendable primary channel balance on the broadcaster's commitment transaction. This key is
/// static across every commitment transaction.
pub payment_point: PublicKey,
pub payment_basepoint: PublicKey,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we discussed this elsewhere but IMO we should fix this in the spec, not here. The "base" in the "basepoint"s refer to the fact that its extended with the per_commitment_point to build a new point for each state. Since static_remotekey (which is now "ASSUMED" in the spec, and not described) its no longer used to derive a key but rather simply a key used as-is. The spec needs updating to remove references to its use as a base key.

/// The base point which is used (with derive_public_key) to derive a per-commitment payment
/// public key which receives non-HTLC-encumbered funds which are only available for spending
/// after some delay (or can be claimed via the revocation path).
Expand All @@ -491,7 +491,7 @@ pub struct ChannelPublicKeys {
impl_writeable_tlv_based!(ChannelPublicKeys, {
(0, funding_pubkey, required),
(2, revocation_basepoint, required),
(4, payment_point, required),
(4, payment_basepoint, required),
(6, delayed_payment_basepoint, required),
(8, htlc_basepoint, required),
});
Expand Down Expand Up @@ -550,11 +550,11 @@ pub fn get_revokeable_redeemscript(revocation_key: &RevocationKey, contest_delay

/// Returns the script for the counterparty's output on a holder's commitment transaction based on
/// the channel type.
pub fn get_counterparty_payment_script(channel_type_features: &ChannelTypeFeatures, payment_key: &PublicKey) -> ScriptBuf {
pub fn get_counterparty_payment_script(channel_type_features: &ChannelTypeFeatures, payment_basepoint: &PublicKey) -> ScriptBuf {
if channel_type_features.supports_anchors_zero_fee_htlc_tx() {
get_to_countersignatory_with_anchors_redeemscript(payment_key).to_p2wsh()
get_to_countersignatory_with_anchors_redeemscript(payment_basepoint).to_p2wsh()
} else {
ScriptBuf::new_p2wpkh(&WPubkeyHash::hash(&payment_key.serialize()))
ScriptBuf::new_p2wpkh(&WPubkeyHash::hash(&payment_basepoint.serialize()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm quite confused by this. The point of calling it a "key" and not a "basepoint" is that the script is paid directly to the key (in this case as a P2WPKH) rather than incorporating the per_commitment_secret/per_commitment_point when building t the output.

}
}

Expand Down Expand Up @@ -816,9 +816,9 @@ pub(crate) fn legacy_deserialization_prevention_marker_for_channel_type_features

/// Gets the witnessScript for the to_remote output when anchors are enabled.
#[inline]
pub fn get_to_countersignatory_with_anchors_redeemscript(payment_point: &PublicKey) -> ScriptBuf {
pub fn get_to_countersignatory_with_anchors_redeemscript(payment_basepoint: &PublicKey) -> ScriptBuf {
Builder::new()
.push_slice(payment_point.serialize())
.push_slice(payment_basepoint.serialize())
.push_opcode(opcodes::all::OP_CHECKSIGVERIFY)
.push_int(1)
.push_opcode(opcodes::all::OP_CSV)
Expand Down Expand Up @@ -933,7 +933,7 @@ impl ChannelTransactionParameters {
let dummy_keys = ChannelPublicKeys {
funding_pubkey: PublicKey::from_slice(&[2; 33]).unwrap(),
revocation_basepoint: PublicKey::from_slice(&[2; 33]).unwrap().into(),
payment_point: PublicKey::from_slice(&[2; 33]).unwrap(),
payment_basepoint: PublicKey::from_slice(&[2; 33]).unwrap(),
delayed_payment_basepoint: PublicKey::from_slice(&[2; 33]).unwrap().into(),
htlc_basepoint: PublicKey::from_slice(&[2; 33]).unwrap().into(),
};
Expand Down Expand Up @@ -1119,7 +1119,7 @@ impl HolderCommitmentTransaction {
let channel_pubkeys = ChannelPublicKeys {
funding_pubkey: dummy_key.clone(),
revocation_basepoint: RevocationBasepoint::from(dummy_key),
payment_point: dummy_key.clone(),
payment_basepoint: dummy_key.clone(),
delayed_payment_basepoint: DelayedPaymentBasepoint::from(dummy_key.clone()),
htlc_basepoint: HtlcBasepoint::from(dummy_key.clone())
};
Expand Down Expand Up @@ -1515,9 +1515,9 @@ impl CommitmentTransaction {

if to_countersignatory_value_sat > Amount::ZERO {
let script = if channel_parameters.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
get_to_countersignatory_with_anchors_redeemscript(&countersignatory_pubkeys.payment_point).to_p2wsh()
get_to_countersignatory_with_anchors_redeemscript(&countersignatory_pubkeys.payment_basepoint).to_p2wsh()
} else {
ScriptBuf::new_p2wpkh(&Hash160::hash(&countersignatory_pubkeys.payment_point.serialize()).into())
ScriptBuf::new_p2wpkh(&Hash160::hash(&countersignatory_pubkeys.payment_basepoint.serialize()).into())
};
txouts.push((
TxOut {
Expand Down Expand Up @@ -1608,8 +1608,8 @@ impl CommitmentTransaction {
let broadcaster_pubkeys = channel_parameters.broadcaster_pubkeys();
let countersignatory_pubkeys = channel_parameters.countersignatory_pubkeys();
let commitment_transaction_number_obscure_factor = get_commitment_transaction_number_obscure_factor(
&broadcaster_pubkeys.payment_point,
&countersignatory_pubkeys.payment_point,
&broadcaster_pubkeys.payment_basepoint,
&countersignatory_pubkeys.payment_basepoint,
channel_parameters.is_outbound(),
);

Expand Down Expand Up @@ -1974,13 +1974,13 @@ mod tests {
// Generate broadcaster and counterparty outputs
let tx = builder.build(1000, 2000);
assert_eq!(tx.built.transaction.output.len(), 2);
assert_eq!(tx.built.transaction.output[1].script_pubkey, bitcoin::address::Address::p2wpkh(&CompressedPublicKey(builder.counterparty_pubkeys.payment_point), Network::Testnet).script_pubkey());
assert_eq!(tx.built.transaction.output[1].script_pubkey, bitcoin::address::Address::p2wpkh(&CompressedPublicKey(builder.counterparty_pubkeys.payment_basepoint), Network::Testnet).script_pubkey());

// Generate broadcaster and counterparty outputs as well as two anchors
builder.channel_parameters.channel_type_features = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
let tx = builder.build(1000, 2000);
assert_eq!(tx.built.transaction.output.len(), 4);
assert_eq!(tx.built.transaction.output[3].script_pubkey, get_to_countersignatory_with_anchors_redeemscript(&builder.counterparty_pubkeys.payment_point).to_p2wsh());
assert_eq!(tx.built.transaction.output[3].script_pubkey, get_to_countersignatory_with_anchors_redeemscript(&builder.counterparty_pubkeys.payment_basepoint).to_p2wsh());

// Generate broadcaster output and anchor
let tx = builder.build(3000, 0);
Expand Down Expand Up @@ -2015,9 +2015,9 @@ mod tests {
assert_eq!(tx.built.transaction.output[0].script_pubkey, get_htlc_redeemscript(&received_htlc, &ChannelTypeFeatures::only_static_remote_key(), &keys).to_p2wsh());
assert_eq!(tx.built.transaction.output[1].script_pubkey, get_htlc_redeemscript(&offered_htlc, &ChannelTypeFeatures::only_static_remote_key(), &keys).to_p2wsh());
assert_eq!(get_htlc_redeemscript(&received_htlc, &ChannelTypeFeatures::only_static_remote_key(), &keys).to_p2wsh().to_hex_string(),
"0020e43a7c068553003fe68fcae424fb7b28ec5ce48cd8b6744b3945631389bad2fb");
"0020510277dd095f7e3a7a46efbd9de3a49f527b5b32f4acc70f144dc5c928529520");
assert_eq!(get_htlc_redeemscript(&offered_htlc, &ChannelTypeFeatures::only_static_remote_key(), &keys).to_p2wsh().to_hex_string(),
"0020215d61bba56b19e9eadb6107f5a85d7f99c40f65992443f69229c290165bc00d");
"0020b981c1ea6c0d0c3971e89800312d6fd5564872f22040771d1555605edf5b1cfe");

// Generate broadcaster output and received and offered HTLC outputs, with anchors
builder.channel_parameters.channel_type_features = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
Expand All @@ -2027,9 +2027,9 @@ mod tests {
assert_eq!(tx.built.transaction.output[2].script_pubkey, get_htlc_redeemscript(&received_htlc, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(), &keys).to_p2wsh());
assert_eq!(tx.built.transaction.output[3].script_pubkey, get_htlc_redeemscript(&offered_htlc, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(), &keys).to_p2wsh());
assert_eq!(get_htlc_redeemscript(&received_htlc, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(), &keys).to_p2wsh().to_hex_string(),
"0020b70d0649c72b38756885c7a30908d912a7898dd5d79457a7280b8e9a20f3f2bc");
"002078193f615fc1b050814d866d6c46097f9d19fdae0dbc4905b2b35f427c8d0662");
assert_eq!(get_htlc_redeemscript(&offered_htlc, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(), &keys).to_p2wsh().to_hex_string(),
"002087a3faeb1950a469c0e2db4a79b093a41b9526e5a6fc6ef5cb949bde3be379c7");
"0020ac28c4778c24e79755cedc11e7feb638630aa74478781ae7c809aed06e6bd325");
}

#[test]
Expand Down
Loading
Loading