From bd497438afc30922ef8911b6cddfb0d0b3ceb746 Mon Sep 17 00:00:00 2001 From: Jim Posen Date: Sun, 1 Oct 2017 19:42:05 -0700 Subject: [PATCH] lnwallet: Pass around key ring instead of individual keys. The signatures of some functions/methods in lnwallet are simplified by passing in a commitmentKeyRing argument instead of multiple keys. --- lnwallet/channel.go | 57 ++++++++++++++++------------------- lnwallet/script_utils_test.go | 10 ++++-- lnwallet/wallet.go | 32 ++++---------------- 3 files changed, 39 insertions(+), 60 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index f7e53018f..d8b369f9c 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -1638,8 +1638,7 @@ func (lc *LightningChannel) restoreStateLogs() error { if !isDustLocal { ourP2WSH, ourWitnessScript, err = lc.genHtlcScript( htlc.Incoming, true, htlc.RefundTimeout, htlc.RHash, - localCommitKeys.localKey, localCommitKeys.remoteKey, - localCommitKeys.revocationKey) + localCommitKeys) if err != nil { return err } @@ -1647,8 +1646,7 @@ func (lc *LightningChannel) restoreStateLogs() error { if !isDustRemote { theirP2WSH, theirWitnessScript, err = lc.genHtlcScript( htlc.Incoming, false, htlc.RefundTimeout, htlc.RHash, - remoteCommitKeys.localKey, remoteCommitKeys.remoteKey, - remoteCommitKeys.revocationKey) + remoteCommitKeys) if err != nil { return err } @@ -1873,9 +1871,8 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool, // Generate a new commitment transaction with all the latest // unsettled/un-timed out HTLCs. - commitTx, err := CreateCommitTx(lc.fundingTxIn, keyRing.delayKey, - keyRing.paymentKey, keyRing.revocationKey, delay, delayBalance, - p2wkhBalance, dustLimit) + commitTx, err := CreateCommitTx(lc.fundingTxIn, keyRing, delay, + delayBalance, p2wkhBalance, dustLimit) if err != nil { return nil, err } @@ -1890,8 +1887,7 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool, continue } - err := lc.addHTLC(commitTx, ourCommitTx, false, htlc, keyRing.localKey, - keyRing.remoteKey, keyRing.revocationKey) + err := lc.addHTLC(commitTx, ourCommitTx, false, htlc, keyRing) if err != nil { return nil, err } @@ -1902,8 +1898,7 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool, continue } - err := lc.addHTLC(commitTx, ourCommitTx, true, htlc, keyRing.localKey, - keyRing.remoteKey, keyRing.revocationKey) + err := lc.addHTLC(commitTx, ourCommitTx, true, htlc, keyRing) if err != nil { return nil, err } @@ -3179,8 +3174,8 @@ func (lc *LightningChannel) ShortChanID() lnwire.ShortChannelID { // HTLC output modified by two-bits denoting if this is an incoming HTLC, and // if the HTLC is being applied to their commitment transaction or ours. func (lc *LightningChannel) genHtlcScript(isIncoming, ourCommit bool, - timeout uint32, rHash [32]byte, localKey, remoteKey *btcec.PublicKey, - revocationKey *btcec.PublicKey) ([]byte, []byte, error) { + timeout uint32, rHash [32]byte, keyRing *commitmentKeyRing, +) ([]byte, []byte, error) { var ( witnessScript []byte @@ -3195,29 +3190,29 @@ func (lc *LightningChannel) genHtlcScript(isIncoming, ourCommit bool, // transaction. So we need to use the receiver's version of HTLC the // script. case isIncoming && ourCommit: - witnessScript, err = receiverHTLCScript(timeout, remoteKey, - localKey, revocationKey, rHash[:]) + witnessScript, err = receiverHTLCScript(timeout, keyRing.remoteKey, + keyRing.localKey, keyRing.revocationKey, rHash[:]) // We're being paid via an HTLC by the remote party, and the HTLC is // being added to their commitment transaction, so we use the sender's // version of the HTLC script. case isIncoming && !ourCommit: - witnessScript, err = senderHTLCScript(remoteKey, localKey, - revocationKey, rHash[:]) + witnessScript, err = senderHTLCScript(keyRing.remoteKey, + keyRing.localKey, keyRing.revocationKey, rHash[:]) // We're sending an HTLC which is being added to our commitment // transaction. Therefore, we need to use the sender's version of the // HTLC script. case !isIncoming && ourCommit: - witnessScript, err = senderHTLCScript(localKey, remoteKey, - revocationKey, rHash[:]) + witnessScript, err = senderHTLCScript(keyRing.localKey, + keyRing.remoteKey, keyRing.revocationKey, rHash[:]) // Finally, we're paying the remote party via an HTLC, which is being // added to their commitment transaction. Therefore, we use the // receiver's version of the HTLC script. case !isIncoming && !ourCommit: - witnessScript, err = receiverHTLCScript(timeout, localKey, - remoteKey, revocationKey, rHash[:]) + witnessScript, err = receiverHTLCScript(timeout, keyRing.localKey, + keyRing.remoteKey, keyRing.revocationKey, rHash[:]) } if err != nil { return nil, nil, err @@ -3241,14 +3236,14 @@ func (lc *LightningChannel) genHtlcScript(isIncoming, ourCommit bool, // PaymentDescriptor that generated it, the generated script is stored within // the descriptor itself. func (lc *LightningChannel) addHTLC(commitTx *wire.MsgTx, ourCommit bool, - isIncoming bool, paymentDesc *PaymentDescriptor, - localKey, remoteKey, revocationKey *btcec.PublicKey) error { + isIncoming bool, paymentDesc *PaymentDescriptor, keyRing *commitmentKeyRing, +) error { timeout := paymentDesc.Timeout rHash := paymentDesc.RHash - p2wsh, witnessScript, err := lc.genHtlcScript(isIncoming, - ourCommit, timeout, rHash, localKey, remoteKey, revocationKey) + p2wsh, witnessScript, err := lc.genHtlcScript(isIncoming, ourCommit, + timeout, rHash, keyRing) if err != nil { return err } @@ -3847,17 +3842,17 @@ func (lc *LightningChannel) ReceiveUpdateFee(feePerKw btcutil.Amount) error { // to the "owner" of the commitment transaction which can be spent after a // relative block delay or revocation event, and the other paying the // counterparty within the channel, which can be spent immediately. -func CreateCommitTx(fundingOutput *wire.TxIn, delayKey, paymentKey *btcec.PublicKey, - revokeKey *btcec.PublicKey, csvTimeout uint32, amountToSelf, - amountToThem, dustLimit btcutil.Amount) (*wire.MsgTx, error) { +func CreateCommitTx(fundingOutput *wire.TxIn, keyRing *commitmentKeyRing, + csvTimeout uint32, amountToSelf, amountToThem, dustLimit btcutil.Amount, +) (*wire.MsgTx, error) { // First, we create the script for the delayed "pay-to-self" output. // This output has 2 main redemption clauses: either we can redeem the // output after a relative block delay, or the remote node can claim // the funds with the revocation key if we broadcast a revoked // commitment transaction. - ourRedeemScript, err := commitScriptToSelf(csvTimeout, delayKey, - revokeKey) + ourRedeemScript, err := commitScriptToSelf(csvTimeout, keyRing.delayKey, + keyRing.revocationKey) if err != nil { return nil, err } @@ -3868,7 +3863,7 @@ func CreateCommitTx(fundingOutput *wire.TxIn, delayKey, paymentKey *btcec.Public // Next, we create the script paying to them. This is just a regular // P2WPKH output, without any added CSV delay. - theirWitnessKeyHash, err := commitScriptUnencumbered(paymentKey) + theirWitnessKeyHash, err := commitScriptUnencumbered(keyRing.paymentKey) if err != nil { return nil, err } diff --git a/lnwallet/script_utils_test.go b/lnwallet/script_utils_test.go index 63126d48c..d17471bfb 100644 --- a/lnwallet/script_utils_test.go +++ b/lnwallet/script_utils_test.go @@ -71,9 +71,13 @@ func TestCommitmentSpendValidation(t *testing.T) { // This is Alice's commitment transaction, so she must wait a CSV delay // of 5 blocks before sweeping the output, while bob can spend // immediately with either the revocation key, or his regular key. - commitmentTx, err := CreateCommitTx(fakeFundingTxIn, aliceDelayKey, - bobPayKey, revokePubKey, csvTimeout, channelBalance, - channelBalance, DefaultDustLimit()) + keyRing := &commitmentKeyRing{ + delayKey: aliceDelayKey, + revocationKey: revokePubKey, + paymentKey: bobPayKey, + } + commitmentTx, err := CreateCommitTx(fakeFundingTxIn, keyRing, csvTimeout, + channelBalance, channelBalance, DefaultDustLimit()) if err != nil { t.Fatalf("unable to create commitment transaction: %v", nil) } diff --git a/lnwallet/wallet.go b/lnwallet/wallet.go index 42423b37f..a4b298758 100644 --- a/lnwallet/wallet.go +++ b/lnwallet/wallet.go @@ -649,31 +649,12 @@ func CreateCommitmentTxns(localBalance, remoteBalance btcutil.Amount, localCommitPoint, remoteCommitPoint *btcec.PublicKey, fundingTxIn *wire.TxIn) (*wire.MsgTx, *wire.MsgTx, error) { - remoteRevocation := DeriveRevocationPubkey( - ourChanCfg.RevocationBasePoint, - remoteCommitPoint, - ) - localRevocation := DeriveRevocationPubkey( - theirChanCfg.RevocationBasePoint, - localCommitPoint, - ) + localCommitmentKeys := deriveCommitmentKeys(localCommitPoint, true, + ourChanCfg, theirChanCfg) + remoteCommitmentKeys := deriveCommitmentKeys(remoteCommitPoint, false, + ourChanCfg, theirChanCfg) - remoteDelayKey := TweakPubKey(theirChanCfg.DelayBasePoint, - remoteCommitPoint) - localDelayKey := TweakPubKey(ourChanCfg.DelayBasePoint, - localCommitPoint) - - // The payment keys go on the opposite commitment transaction, so we'll - // swap the commitment points we use. As in the remote payment key will - // be used within our commitment transaction, and the local payment key - // used within the remote commitment transaction. - remotePaymentKey := TweakPubKey(theirChanCfg.PaymentBasePoint, - localCommitPoint) - localPaymentKey := TweakPubKey(ourChanCfg.PaymentBasePoint, - remoteCommitPoint) - - ourCommitTx, err := CreateCommitTx(fundingTxIn, - localDelayKey, remotePaymentKey, localRevocation, + ourCommitTx, err := CreateCommitTx(fundingTxIn, localCommitmentKeys, uint32(ourChanCfg.CsvDelay), localBalance, remoteBalance, ourChanCfg.DustLimit) if err != nil { @@ -685,8 +666,7 @@ func CreateCommitmentTxns(localBalance, remoteBalance btcutil.Amount, return nil, nil, err } - theirCommitTx, err := CreateCommitTx(fundingTxIn, - remoteDelayKey, localPaymentKey, remoteRevocation, + theirCommitTx, err := CreateCommitTx(fundingTxIn, remoteCommitmentKeys, uint32(theirChanCfg.CsvDelay), remoteBalance, localBalance, theirChanCfg.DustLimit) if err != nil {