From 4fde31229cd7ee7be8b6dae0e9180d163d74ef2d Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Mon, 6 Jan 2020 11:42:04 +0100 Subject: [PATCH] lnwallet: rename DelayKey->ToLocalKey, NoDelayKey->ToRemoteKey Since both parties are going to have their ouputs delayed, we move way from the DelayKey naming, and instead use ToLocalKey and ToRemoteKey. --- contractcourt/chain_watcher.go | 8 +-- lnwallet/channel.go | 26 +++++----- lnwallet/commitment.go | 52 ++++++++++++------- lnwallet/transactions_test.go | 8 +-- watchtower/wtclient/backup_task.go | 4 +- .../wtclient/backup_task_internal_test.go | 8 +-- watchtower/wtclient/client_test.go | 4 +- 7 files changed, 61 insertions(+), 49 deletions(-) diff --git a/contractcourt/chain_watcher.go b/contractcourt/chain_watcher.go index 7ea3da142..7a226de25 100644 --- a/contractcourt/chain_watcher.go +++ b/contractcourt/chain_watcher.go @@ -351,7 +351,7 @@ func isOurCommitment(localChanCfg, remoteChanCfg channeldb.ChannelConfig, // With the keys derived, we'll construct the remote script that'll be // present if they have a non-dust balance on the commitment. remotePkScript, err := input.CommitScriptUnencumbered( - commitKeyRing.NoDelayKey, + commitKeyRing.ToRemoteKey, ) if err != nil { return false, err @@ -361,7 +361,7 @@ func isOurCommitment(localChanCfg, remoteChanCfg channeldb.ChannelConfig, // the remote party allowing them to claim this output before the CSV // delay if we breach. localScript, err := input.CommitScriptToSelf( - uint32(localChanCfg.CsvDelay), commitKeyRing.DelayKey, + uint32(localChanCfg.CsvDelay), commitKeyRing.ToLocalKey, commitKeyRing.RevocationKey, ) if err != nil { @@ -928,8 +928,8 @@ func (c *chainWatcher) dispatchContractBreach(spendEvent *chainntnfs.SpendDetail retribution.KeyRing.CommitPoint.Curve = nil retribution.KeyRing.LocalHtlcKey = nil retribution.KeyRing.RemoteHtlcKey = nil - retribution.KeyRing.DelayKey = nil - retribution.KeyRing.NoDelayKey = nil + retribution.KeyRing.ToLocalKey = nil + retribution.KeyRing.ToRemoteKey = nil retribution.KeyRing.RevocationKey = nil return spew.Sdump(retribution) })) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 0c0f854fa..eb78cdd23 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -1880,7 +1880,7 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64, // within the final witness. remoteDelay := uint32(chanState.RemoteChanCfg.CsvDelay) remotePkScript, err := input.CommitScriptToSelf( - remoteDelay, keyRing.DelayKey, keyRing.RevocationKey, + remoteDelay, keyRing.ToLocalKey, keyRing.RevocationKey, ) if err != nil { return nil, err @@ -1889,7 +1889,7 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64, if err != nil { return nil, err } - localPkScript, err := input.CommitScriptUnencumbered(keyRing.NoDelayKey) + localPkScript, err := input.CommitScriptUnencumbered(keyRing.ToRemoteKey) if err != nil { return nil, err } @@ -1984,7 +1984,7 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64, // remote commitment transaction, and *they* go to the second // level. secondLevelWitnessScript, err := input.SecondLevelHtlcScript( - keyRing.RevocationKey, keyRing.DelayKey, remoteDelay, + keyRing.RevocationKey, keyRing.ToLocalKey, remoteDelay, ) if err != nil { return nil, err @@ -2563,7 +2563,7 @@ func genRemoteHtlcSigJobs(keyRing *CommitmentKeyRing, sigJob.Tx, err = createHtlcTimeoutTx( op, outputAmt, htlc.Timeout, uint32(remoteChanCfg.CsvDelay), - keyRing.RevocationKey, keyRing.DelayKey, + keyRing.RevocationKey, keyRing.ToLocalKey, ) if err != nil { return nil, nil, err @@ -2614,7 +2614,7 @@ func genRemoteHtlcSigJobs(keyRing *CommitmentKeyRing, } sigJob.Tx, err = createHtlcSuccessTx( op, outputAmt, uint32(remoteChanCfg.CsvDelay), - keyRing.RevocationKey, keyRing.DelayKey, + keyRing.RevocationKey, keyRing.ToLocalKey, ) if err != nil { return nil, nil, err @@ -3526,7 +3526,7 @@ func genHtlcSigValidationJobs(localCommitmentView *commitment, successTx, err := createHtlcSuccessTx(op, outputAmt, uint32(localChanCfg.CsvDelay), - keyRing.RevocationKey, keyRing.DelayKey) + keyRing.RevocationKey, keyRing.ToLocalKey) if err != nil { return nil, err } @@ -3579,7 +3579,7 @@ func genHtlcSigValidationJobs(localCommitmentView *commitment, timeoutTx, err := createHtlcTimeoutTx(op, outputAmt, htlc.Timeout, uint32(localChanCfg.CsvDelay), - keyRing.RevocationKey, keyRing.DelayKey, + keyRing.RevocationKey, keyRing.ToLocalKey, ) if err != nil { return nil, err @@ -4772,7 +4772,7 @@ func NewUnilateralCloseSummary(chanState *channeldb.OpenChannel, signer input.Si // Before we can generate the proper sign descriptor, we'll need to // locate the output index of our non-delayed output on the commitment // transaction. - selfP2WKH, err := input.CommitScriptUnencumbered(keyRing.NoDelayKey) + selfP2WKH, err := input.CommitScriptUnencumbered(keyRing.ToRemoteKey) if err != nil { return nil, fmt.Errorf("unable to create self commit "+ "script: %v", err) @@ -5015,7 +5015,7 @@ func newOutgoingHtlcResolution(signer input.Signer, // transaction. timeoutTx, err := createHtlcTimeoutTx( op, secondLevelOutputAmt, htlc.RefundTimeout, csvDelay, - keyRing.RevocationKey, keyRing.DelayKey, + keyRing.RevocationKey, keyRing.ToLocalKey, ) if err != nil { return nil, err @@ -5055,7 +5055,7 @@ func newOutgoingHtlcResolution(signer input.Signer, // transaction creates so we can generate the signDesc required to // complete the claim process after a delay period. htlcSweepScript, err := input.SecondLevelHtlcScript( - keyRing.RevocationKey, keyRing.DelayKey, csvDelay, + keyRing.RevocationKey, keyRing.ToLocalKey, csvDelay, ) if err != nil { return nil, err @@ -5149,7 +5149,7 @@ func newIncomingHtlcResolution(signer input.Signer, localChanCfg *channeldb.Chan secondLevelOutputAmt := htlc.Amt.ToSatoshis() - htlcFee successTx, err := createHtlcSuccessTx( op, secondLevelOutputAmt, csvDelay, - keyRing.RevocationKey, keyRing.DelayKey, + keyRing.RevocationKey, keyRing.ToLocalKey, ) if err != nil { return nil, err @@ -5192,7 +5192,7 @@ func newIncomingHtlcResolution(signer input.Signer, localChanCfg *channeldb.Chan // creates so we can generate the proper signDesc to sweep it after the // CSV delay has passed. htlcSweepScript, err := input.SecondLevelHtlcScript( - keyRing.RevocationKey, keyRing.DelayKey, csvDelay, + keyRing.RevocationKey, keyRing.ToLocalKey, csvDelay, ) if err != nil { return nil, err @@ -5410,7 +5410,7 @@ func NewLocalForceCloseSummary(chanState *channeldb.OpenChannel, signer input.Si commitPoint, true, chanState.ChanType.IsTweakless(), &chanState.LocalChanCfg, &chanState.RemoteChanCfg, ) - selfScript, err := input.CommitScriptToSelf(csvTimeout, keyRing.DelayKey, + selfScript, err := input.CommitScriptToSelf(csvTimeout, keyRing.ToLocalKey, keyRing.RevocationKey) if err != nil { return nil, err diff --git a/lnwallet/commitment.go b/lnwallet/commitment.go index bf95dd598..2be650298 100644 --- a/lnwallet/commitment.go +++ b/lnwallet/commitment.go @@ -59,18 +59,30 @@ type CommitmentKeyRing struct { // of whether this is our commit or not. RemoteHtlcKey *btcec.PublicKey - // DelayKey is the commitment transaction owner's key which is included - // in HTLC success and timeout transaction scripts. - DelayKey *btcec.PublicKey + // ToLocalKey is the commitment transaction owner's key which is + // included in HTLC success and timeout transaction scripts. This is + // the public key used for the to_local output of the commitment + // transaction. + // + // NOTE: Who's key this is depends on the current perspective. If this + // is our commitment this will be our key. + ToLocalKey *btcec.PublicKey - // NoDelayKey is the other party's payment key in the commitment tx. - // This is the key used to generate the unencumbered output within the + // ToRemoteKey is the non-owner's payment key in the commitment tx. + // This is the key used to generate the to_remote output within the // commitment transaction. - NoDelayKey *btcec.PublicKey + // + // NOTE: Who's key this is depends on the current perspective. If this + // is our commitment this will be their key. + ToRemoteKey *btcec.PublicKey // RevocationKey is the key that can be used by the other party to // redeem outputs from a revoked commitment transaction if it were to // be published. + // + // NOTE: Who can sign for this key depends on the current perspective. + // If this is our commitment, it means the remote node can sign for + // this key in case of a breach. RevocationKey *btcec.PublicKey } @@ -100,29 +112,29 @@ func DeriveCommitmentKeys(commitPoint *btcec.PublicKey, ), } - // We'll now compute the delay, no delay, and revocation key based on - // the current commitment point. All keys are tweaked each state in + // We'll now compute the to_local, to_remote, and revocation key based + // on the current commitment point. All keys are tweaked each state in // order to ensure the keys from each state are unlinkable. To create // the revocation key, we take the opposite party's revocation base // point and combine that with the current commitment point. var ( - delayBasePoint *btcec.PublicKey - noDelayBasePoint *btcec.PublicKey + toLocalBasePoint *btcec.PublicKey + toRemoteBasePoint *btcec.PublicKey revocationBasePoint *btcec.PublicKey ) if isOurCommit { - delayBasePoint = localChanCfg.DelayBasePoint.PubKey - noDelayBasePoint = remoteChanCfg.PaymentBasePoint.PubKey + toLocalBasePoint = localChanCfg.DelayBasePoint.PubKey + toRemoteBasePoint = remoteChanCfg.PaymentBasePoint.PubKey revocationBasePoint = remoteChanCfg.RevocationBasePoint.PubKey } else { - delayBasePoint = remoteChanCfg.DelayBasePoint.PubKey - noDelayBasePoint = localChanCfg.PaymentBasePoint.PubKey + toLocalBasePoint = remoteChanCfg.DelayBasePoint.PubKey + toRemoteBasePoint = localChanCfg.PaymentBasePoint.PubKey revocationBasePoint = localChanCfg.RevocationBasePoint.PubKey } // With the base points assigned, we can now derive the actual keys // using the base point, and the current commitment tweak. - keyRing.DelayKey = input.TweakPubKey(delayBasePoint, commitPoint) + keyRing.ToLocalKey = input.TweakPubKey(toLocalBasePoint, commitPoint) keyRing.RevocationKey = input.DeriveRevocationPubkey( revocationBasePoint, commitPoint, ) @@ -130,10 +142,10 @@ func DeriveCommitmentKeys(commitPoint *btcec.PublicKey, // If this commitment should omit the tweak for the remote point, then // we'll use that directly, and ignore the commitPoint tweak. if tweaklessCommit { - keyRing.NoDelayKey = noDelayBasePoint + keyRing.ToRemoteKey = toRemoteBasePoint } else { - keyRing.NoDelayKey = input.TweakPubKey( - noDelayBasePoint, commitPoint, + keyRing.ToRemoteKey = input.TweakPubKey( + toRemoteBasePoint, commitPoint, ) } @@ -377,7 +389,7 @@ func CreateCommitTx(fundingOutput wire.TxIn, keyRing *CommitmentKeyRing, // the funds with the revocation key if we broadcast a revoked // commitment transaction. toLocalRedeemScript, err := input.CommitScriptToSelf( - uint32(localChanCfg.CsvDelay), keyRing.DelayKey, + uint32(localChanCfg.CsvDelay), keyRing.ToLocalKey, keyRing.RevocationKey, ) if err != nil { @@ -393,7 +405,7 @@ func CreateCommitTx(fundingOutput wire.TxIn, keyRing *CommitmentKeyRing, // Next, we create the script paying to the remote. This is just a // regular P2WPKH output, without any added CSV delay. toRemoteWitnessKeyHash, err := input.CommitScriptUnencumbered( - keyRing.NoDelayKey, + keyRing.ToRemoteKey, ) if err != nil { return nil, err diff --git a/lnwallet/transactions_test.go b/lnwallet/transactions_test.go index 2ef864197..20f250352 100644 --- a/lnwallet/transactions_test.go +++ b/lnwallet/transactions_test.go @@ -439,8 +439,8 @@ func TestCommitmentAndHTLCTransactions(t *testing.T) { LocalHtlcKeyTweak: tweak, LocalHtlcKey: tc.localPaymentPubKey, RemoteHtlcKey: tc.remotePaymentPubKey, - DelayKey: tc.localDelayPubKey, - NoDelayKey: tc.remotePaymentPubKey, + ToLocalKey: tc.localDelayPubKey, + ToRemoteKey: tc.remotePaymentPubKey, RevocationKey: tc.localRevocationPubKey, } @@ -1081,9 +1081,9 @@ func testSpendValidation(t *testing.T, tweakless bool) { // of 5 blocks before sweeping the output, while bob can spend // immediately with either the revocation key, or his regular key. keyRing := &CommitmentKeyRing{ - DelayKey: aliceDelayKey, + ToLocalKey: aliceDelayKey, RevocationKey: revokePubKey, - NoDelayKey: bobPayKey, + ToRemoteKey: bobPayKey, } commitmentTx, err := CreateCommitTx( *fakeFundingTxIn, keyRing, aliceChanCfg, bobChanCfg, diff --git a/watchtower/wtclient/backup_task.go b/watchtower/wtclient/backup_task.go index abdabe2f9..c112c1016 100644 --- a/watchtower/wtclient/backup_task.go +++ b/watchtower/wtclient/backup_task.go @@ -189,7 +189,7 @@ func (t *backupTask) craftSessionPayload( justiceKit := &blob.JusticeKit{ SweepAddress: t.sweepPkScript, RevocationPubKey: toBlobPubKey(keyRing.RevocationKey), - LocalDelayPubKey: toBlobPubKey(keyRing.DelayKey), + LocalDelayPubKey: toBlobPubKey(keyRing.ToLocalKey), CSVDelay: t.breachInfo.RemoteDelay, } @@ -199,7 +199,7 @@ func (t *backupTask) craftSessionPayload( // output to spend from. if t.toRemoteInput != nil { justiceKit.CommitToRemotePubKey = toBlobPubKey( - keyRing.NoDelayKey, + keyRing.ToRemoteKey, ) } diff --git a/watchtower/wtclient/backup_task_internal_test.go b/watchtower/wtclient/backup_task_internal_test.go index d845631ce..3c8ed17f7 100644 --- a/watchtower/wtclient/backup_task_internal_test.go +++ b/watchtower/wtclient/backup_task_internal_test.go @@ -121,8 +121,8 @@ func genTaskTest( BreachTransaction: breachTxn, KeyRing: &lnwallet.CommitmentKeyRing{ RevocationKey: revPK, - DelayKey: toLocalPK, - NoDelayKey: toRemotePK, + ToLocalKey: toLocalPK, + ToRemoteKey: toRemotePK, }, RemoteDelay: csvDelay, } @@ -565,9 +565,9 @@ func testBackupTask(t *testing.T, test backupTaskTest) { } keyRing := test.breachInfo.KeyRing - expToLocalPK := keyRing.DelayKey.SerializeCompressed() + expToLocalPK := keyRing.ToLocalKey.SerializeCompressed() expRevPK := keyRing.RevocationKey.SerializeCompressed() - expToRemotePK := keyRing.NoDelayKey.SerializeCompressed() + expToRemotePK := keyRing.ToRemoteKey.SerializeCompressed() // Assert that the blob contained the serialized revocation and to-local // pubkeys. diff --git a/watchtower/wtclient/client_test.go b/watchtower/wtclient/client_test.go index bd16ac065..ea681d575 100644 --- a/watchtower/wtclient/client_test.go +++ b/watchtower/wtclient/client_test.go @@ -291,8 +291,8 @@ func (c *mockChannel) createRemoteCommitTx(t *testing.T) { commitKeyRing := &lnwallet.CommitmentKeyRing{ RevocationKey: c.revPK, - NoDelayKey: c.toLocalPK, - DelayKey: c.toRemotePK, + ToRemoteKey: c.toLocalPK, + ToLocalKey: c.toRemotePK, } retribution := &lnwallet.BreachRetribution{