Implement struct wrappers for channel key types to avoid confusion.
[rust-lightning] / lightning / src / sign / mod.rs
index bc15a3a7662c0dd12b06aab8b1927ee2f6e2eab3..c4d4bc002f4c476fb4864a40d8b72d26f4be4a21 100644 (file)
@@ -42,6 +42,7 @@ use crate::chain::transaction::OutPoint;
 use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
 use crate::ln::{chan_utils, PaymentPreimage};
 use crate::ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, HolderCommitmentTransaction, ChannelTransactionParameters, CommitmentTransaction, ClosingTransaction};
+use crate::ln::channel_keys::{DelayedPaymentBasepoint, DelayedPaymentKey, HtlcKey, HtlcBasepoint, RevocationKey, RevocationBasepoint};
 use crate::ln::msgs::{UnsignedChannelAnnouncement, UnsignedGossipMessage};
 use crate::ln::script::ShutdownScript;
 use crate::offers::invoice::UnsignedBolt12Invoice;
@@ -83,7 +84,7 @@ pub struct DelayedPaymentOutputDescriptor {
        pub output: TxOut,
        /// The revocation point specific to the commitment transaction which was broadcast. Used to
        /// derive the witnessScript for this output.
-       pub revocation_pubkey: PublicKey,
+       pub revocation_pubkey: RevocationKey,
        /// Arbitrary identification information returned by a call to [`ChannelSigner::channel_keys_id`].
        /// This may be useful in re-deriving keys used in the channel to spend the output.
        pub channel_keys_id: [u8; 32],
@@ -224,8 +225,8 @@ pub enum SpendableOutputDescriptor {
        /// To derive the delayed payment key which is used to sign this input, you must pass the
        /// holder [`InMemorySigner::delayed_payment_base_key`] (i.e., the private key which corresponds to the
        /// [`ChannelPublicKeys::delayed_payment_basepoint`] in [`ChannelSigner::pubkeys`]) and the provided
-       /// [`DelayedPaymentOutputDescriptor::per_commitment_point`] to [`chan_utils::derive_private_key`]. The public key can be
-       /// generated without the secret key using [`chan_utils::derive_public_key`] and only the
+       /// [`DelayedPaymentOutputDescriptor::per_commitment_point`] to [`chan_utils::derive_private_key`]. The DelayedPaymentKey can be
+       /// generated without the secret key using [`DelayedPaymentKey::from_basepoint`] and only the
        /// [`ChannelPublicKeys::delayed_payment_basepoint`] which appears in [`ChannelSigner::pubkeys`].
        ///
        /// To derive the [`DelayedPaymentOutputDescriptor::revocation_pubkey`] provided here (which is
@@ -233,7 +234,7 @@ pub enum SpendableOutputDescriptor {
        /// [`ChannelPublicKeys::revocation_basepoint`] (which appears in the call to
        /// [`ChannelSigner::provide_channel_parameters`]) and the provided
        /// [`DelayedPaymentOutputDescriptor::per_commitment_point`] to
-       /// [`chan_utils::derive_public_revocation_key`].
+       /// [`RevocationKey`].
        ///
        /// The witness script which is hashed and included in the output `script_pubkey` may be
        /// regenerated by passing the [`DelayedPaymentOutputDescriptor::revocation_pubkey`] (derived
@@ -493,12 +494,10 @@ impl HTLCDescriptor {
                let channel_params = self.channel_derivation_parameters.transaction_parameters.as_holder_broadcastable();
                let broadcaster_keys = channel_params.broadcaster_pubkeys();
                let counterparty_keys = channel_params.countersignatory_pubkeys();
-               let broadcaster_delayed_key = chan_utils::derive_public_key(
-                       secp, &self.per_commitment_point, &broadcaster_keys.delayed_payment_basepoint
-               );
-               let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
-                       secp, &self.per_commitment_point, &counterparty_keys.revocation_basepoint
+               let broadcaster_delayed_key = DelayedPaymentKey::from_basepoint(
+                       secp, &broadcaster_keys.delayed_payment_basepoint, &self.per_commitment_point
                );
+               let counterparty_revocation_key = &RevocationKey::from_basepoint(&secp, &counterparty_keys.revocation_basepoint, &self.per_commitment_point);
                chan_utils::build_htlc_output(
                        self.feerate_per_kw, channel_params.contest_delay(), &self.htlc,
                        channel_params.channel_type_features(), &broadcaster_delayed_key, &counterparty_revocation_key
@@ -510,15 +509,13 @@ impl HTLCDescriptor {
                let channel_params = self.channel_derivation_parameters.transaction_parameters.as_holder_broadcastable();
                let broadcaster_keys = channel_params.broadcaster_pubkeys();
                let counterparty_keys = channel_params.countersignatory_pubkeys();
-               let broadcaster_htlc_key = chan_utils::derive_public_key(
-                       secp, &self.per_commitment_point, &broadcaster_keys.htlc_basepoint
-               );
-               let counterparty_htlc_key = chan_utils::derive_public_key(
-                       secp, &self.per_commitment_point, &counterparty_keys.htlc_basepoint
+               let broadcaster_htlc_key = HtlcKey::from_basepoint(
+                       secp, &broadcaster_keys.htlc_basepoint, &self.per_commitment_point
                );
-               let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
-                       secp, &self.per_commitment_point, &counterparty_keys.revocation_basepoint
+               let counterparty_htlc_key = HtlcKey::from_basepoint(
+                       secp, &counterparty_keys.htlc_basepoint, &self.per_commitment_point,
                );
+               let counterparty_revocation_key = &RevocationKey::from_basepoint(&secp, &counterparty_keys.revocation_basepoint, &self.per_commitment_point);
                chan_utils::get_htlc_redeemscript_with_explicit_keys(
                        &self.htlc, channel_params.channel_type_features(), &broadcaster_htlc_key, &counterparty_htlc_key,
                        &counterparty_revocation_key,
@@ -1031,10 +1028,10 @@ impl InMemorySigner {
                let from_secret = |s: &SecretKey| PublicKey::from_secret_key(secp_ctx, s);
                ChannelPublicKeys {
                        funding_pubkey: from_secret(&funding_key),
-                       revocation_basepoint: from_secret(&revocation_base_key),
+                       revocation_basepoint: RevocationBasepoint::from(from_secret(&revocation_base_key)),
                        payment_point: from_secret(&payment_key),
-                       delayed_payment_basepoint: from_secret(&delayed_payment_base_key),
-                       htlc_basepoint: from_secret(&htlc_base_key),
+                       delayed_payment_basepoint: DelayedPaymentBasepoint::from(from_secret(&delayed_payment_base_key)),
+                       htlc_basepoint: HtlcBasepoint::from(from_secret(&htlc_base_key)),
                }
        }
 
@@ -1173,7 +1170,7 @@ impl InMemorySigner {
                if spend_tx.input[input_idx].sequence.0 != descriptor.to_self_delay as u32 { return Err(()); }
 
                let delayed_payment_key = chan_utils::derive_private_key(&secp_ctx, &descriptor.per_commitment_point, &self.delayed_payment_base_key);
-               let delayed_payment_pubkey = PublicKey::from_secret_key(&secp_ctx, &delayed_payment_key);
+               let delayed_payment_pubkey = DelayedPaymentKey::from_secret_key(&secp_ctx, &delayed_payment_key);
                let witness_script = chan_utils::get_revokeable_redeemscript(&descriptor.revocation_pubkey, descriptor.to_self_delay, &delayed_payment_pubkey);
                let sighash = hash_to_message!(&sighash::SighashCache::new(spend_tx).segwit_signature_hash(input_idx, &witness_script, descriptor.output.value, EcdsaSighashType::All).unwrap()[..]);
                let local_delayedsig = EcdsaSignature {
@@ -1286,12 +1283,14 @@ impl EcdsaChannelSigner for InMemorySigner {
        fn sign_justice_revoked_output(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
                let revocation_key = chan_utils::derive_private_revocation_key(&secp_ctx, &per_commitment_key, &self.revocation_base_key);
                let per_commitment_point = PublicKey::from_secret_key(secp_ctx, &per_commitment_key);
-               let revocation_pubkey = chan_utils::derive_public_revocation_key(&secp_ctx, &per_commitment_point, &self.pubkeys().revocation_basepoint);
+               let revocation_pubkey = RevocationKey::from_basepoint(
+                       &secp_ctx,  &self.pubkeys().revocation_basepoint, &per_commitment_point,
+               );
                let witness_script = {
                        let counterparty_keys = self.counterparty_pubkeys().expect(MISSING_PARAMS_ERR);
                        let holder_selected_contest_delay =
                                self.holder_selected_contest_delay().expect(MISSING_PARAMS_ERR);
-                       let counterparty_delayedpubkey = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &counterparty_keys.delayed_payment_basepoint);
+                       let counterparty_delayedpubkey = DelayedPaymentKey::from_basepoint(&secp_ctx, &counterparty_keys.delayed_payment_basepoint, &per_commitment_point);
                        chan_utils::get_revokeable_redeemscript(&revocation_pubkey, holder_selected_contest_delay, &counterparty_delayedpubkey)
                };
                let mut sighash_parts = sighash::SighashCache::new(justice_tx);
@@ -1302,11 +1301,17 @@ impl EcdsaChannelSigner for InMemorySigner {
        fn sign_justice_revoked_htlc(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
                let revocation_key = chan_utils::derive_private_revocation_key(&secp_ctx, &per_commitment_key, &self.revocation_base_key);
                let per_commitment_point = PublicKey::from_secret_key(secp_ctx, &per_commitment_key);
-               let revocation_pubkey = chan_utils::derive_public_revocation_key(&secp_ctx, &per_commitment_point, &self.pubkeys().revocation_basepoint);
+               let revocation_pubkey = RevocationKey::from_basepoint(
+                       &secp_ctx,  &self.pubkeys().revocation_basepoint, &per_commitment_point,
+               );
                let witness_script = {
                        let counterparty_keys = self.counterparty_pubkeys().expect(MISSING_PARAMS_ERR);
-                       let counterparty_htlcpubkey = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &counterparty_keys.htlc_basepoint);
-                       let holder_htlcpubkey = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.pubkeys().htlc_basepoint);
+                       let counterparty_htlcpubkey = HtlcKey::from_basepoint(
+                               &secp_ctx, &counterparty_keys.htlc_basepoint, &per_commitment_point,
+                       );
+                       let holder_htlcpubkey = HtlcKey::from_basepoint(
+                               &secp_ctx, &self.pubkeys().htlc_basepoint, &per_commitment_point,
+                       );
                        let chan_type = self.channel_type_features().expect(MISSING_PARAMS_ERR);
                        chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, chan_type, &counterparty_htlcpubkey, &holder_htlcpubkey, &revocation_pubkey)
                };
@@ -1331,10 +1336,14 @@ impl EcdsaChannelSigner for InMemorySigner {
 
        fn sign_counterparty_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, amount: u64, per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
                let htlc_key = chan_utils::derive_private_key(&secp_ctx, &per_commitment_point, &self.htlc_base_key);
-               let revocation_pubkey = chan_utils::derive_public_revocation_key(&secp_ctx, &per_commitment_point, &self.pubkeys().revocation_basepoint);
+               let revocation_pubkey = RevocationKey::from_basepoint(
+                       &secp_ctx,  &self.pubkeys().revocation_basepoint, &per_commitment_point,
+               );
                let counterparty_keys = self.counterparty_pubkeys().expect(MISSING_PARAMS_ERR);
-               let counterparty_htlcpubkey = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &counterparty_keys.htlc_basepoint);
-               let htlcpubkey = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.pubkeys().htlc_basepoint);
+               let counterparty_htlcpubkey = HtlcKey::from_basepoint(
+                       &secp_ctx, &counterparty_keys.htlc_basepoint, &per_commitment_point,
+               );
+               let htlcpubkey = HtlcKey::from_basepoint(&secp_ctx, &self.pubkeys().htlc_basepoint, &per_commitment_point);
                let chan_type = self.channel_type_features().expect(MISSING_PARAMS_ERR);
                let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, chan_type, &counterparty_htlcpubkey, &htlcpubkey, &revocation_pubkey);
                let mut sighash_parts = sighash::SighashCache::new(htlc_tx);