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.
This commit is contained in:
Jim Posen 2017-10-01 19:42:05 -07:00 committed by Olaoluwa Osuntokun
parent 3151a3a596
commit bd497438af
3 changed files with 39 additions and 60 deletions

View File

@ -1638,8 +1638,7 @@ func (lc *LightningChannel) restoreStateLogs() error {
if !isDustLocal { if !isDustLocal {
ourP2WSH, ourWitnessScript, err = lc.genHtlcScript( ourP2WSH, ourWitnessScript, err = lc.genHtlcScript(
htlc.Incoming, true, htlc.RefundTimeout, htlc.RHash, htlc.Incoming, true, htlc.RefundTimeout, htlc.RHash,
localCommitKeys.localKey, localCommitKeys.remoteKey, localCommitKeys)
localCommitKeys.revocationKey)
if err != nil { if err != nil {
return err return err
} }
@ -1647,8 +1646,7 @@ func (lc *LightningChannel) restoreStateLogs() error {
if !isDustRemote { if !isDustRemote {
theirP2WSH, theirWitnessScript, err = lc.genHtlcScript( theirP2WSH, theirWitnessScript, err = lc.genHtlcScript(
htlc.Incoming, false, htlc.RefundTimeout, htlc.RHash, htlc.Incoming, false, htlc.RefundTimeout, htlc.RHash,
remoteCommitKeys.localKey, remoteCommitKeys.remoteKey, remoteCommitKeys)
remoteCommitKeys.revocationKey)
if err != nil { if err != nil {
return err return err
} }
@ -1873,9 +1871,8 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
// Generate a new commitment transaction with all the latest // Generate a new commitment transaction with all the latest
// unsettled/un-timed out HTLCs. // unsettled/un-timed out HTLCs.
commitTx, err := CreateCommitTx(lc.fundingTxIn, keyRing.delayKey, commitTx, err := CreateCommitTx(lc.fundingTxIn, keyRing, delay,
keyRing.paymentKey, keyRing.revocationKey, delay, delayBalance, delayBalance, p2wkhBalance, dustLimit)
p2wkhBalance, dustLimit)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -1890,8 +1887,7 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
continue continue
} }
err := lc.addHTLC(commitTx, ourCommitTx, false, htlc, keyRing.localKey, err := lc.addHTLC(commitTx, ourCommitTx, false, htlc, keyRing)
keyRing.remoteKey, keyRing.revocationKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -1902,8 +1898,7 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
continue continue
} }
err := lc.addHTLC(commitTx, ourCommitTx, true, htlc, keyRing.localKey, err := lc.addHTLC(commitTx, ourCommitTx, true, htlc, keyRing)
keyRing.remoteKey, keyRing.revocationKey)
if err != nil { if err != nil {
return nil, err 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 // 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. // if the HTLC is being applied to their commitment transaction or ours.
func (lc *LightningChannel) genHtlcScript(isIncoming, ourCommit bool, func (lc *LightningChannel) genHtlcScript(isIncoming, ourCommit bool,
timeout uint32, rHash [32]byte, localKey, remoteKey *btcec.PublicKey, timeout uint32, rHash [32]byte, keyRing *commitmentKeyRing,
revocationKey *btcec.PublicKey) ([]byte, []byte, error) { ) ([]byte, []byte, error) {
var ( var (
witnessScript []byte 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 // transaction. So we need to use the receiver's version of HTLC the
// script. // script.
case isIncoming && ourCommit: case isIncoming && ourCommit:
witnessScript, err = receiverHTLCScript(timeout, remoteKey, witnessScript, err = receiverHTLCScript(timeout, keyRing.remoteKey,
localKey, revocationKey, rHash[:]) keyRing.localKey, keyRing.revocationKey, rHash[:])
// We're being paid via an HTLC by the remote party, and the HTLC is // 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 // being added to their commitment transaction, so we use the sender's
// version of the HTLC script. // version of the HTLC script.
case isIncoming && !ourCommit: case isIncoming && !ourCommit:
witnessScript, err = senderHTLCScript(remoteKey, localKey, witnessScript, err = senderHTLCScript(keyRing.remoteKey,
revocationKey, rHash[:]) keyRing.localKey, keyRing.revocationKey, rHash[:])
// We're sending an HTLC which is being added to our commitment // We're sending an HTLC which is being added to our commitment
// transaction. Therefore, we need to use the sender's version of the // transaction. Therefore, we need to use the sender's version of the
// HTLC script. // HTLC script.
case !isIncoming && ourCommit: case !isIncoming && ourCommit:
witnessScript, err = senderHTLCScript(localKey, remoteKey, witnessScript, err = senderHTLCScript(keyRing.localKey,
revocationKey, rHash[:]) keyRing.remoteKey, keyRing.revocationKey, rHash[:])
// Finally, we're paying the remote party via an HTLC, which is being // Finally, we're paying the remote party via an HTLC, which is being
// added to their commitment transaction. Therefore, we use the // added to their commitment transaction. Therefore, we use the
// receiver's version of the HTLC script. // receiver's version of the HTLC script.
case !isIncoming && !ourCommit: case !isIncoming && !ourCommit:
witnessScript, err = receiverHTLCScript(timeout, localKey, witnessScript, err = receiverHTLCScript(timeout, keyRing.localKey,
remoteKey, revocationKey, rHash[:]) keyRing.remoteKey, keyRing.revocationKey, rHash[:])
} }
if err != nil { if err != nil {
return nil, nil, err 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 // PaymentDescriptor that generated it, the generated script is stored within
// the descriptor itself. // the descriptor itself.
func (lc *LightningChannel) addHTLC(commitTx *wire.MsgTx, ourCommit bool, func (lc *LightningChannel) addHTLC(commitTx *wire.MsgTx, ourCommit bool,
isIncoming bool, paymentDesc *PaymentDescriptor, isIncoming bool, paymentDesc *PaymentDescriptor, keyRing *commitmentKeyRing,
localKey, remoteKey, revocationKey *btcec.PublicKey) error { ) error {
timeout := paymentDesc.Timeout timeout := paymentDesc.Timeout
rHash := paymentDesc.RHash rHash := paymentDesc.RHash
p2wsh, witnessScript, err := lc.genHtlcScript(isIncoming, p2wsh, witnessScript, err := lc.genHtlcScript(isIncoming, ourCommit,
ourCommit, timeout, rHash, localKey, remoteKey, revocationKey) timeout, rHash, keyRing)
if err != nil { if err != nil {
return err 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 // to the "owner" of the commitment transaction which can be spent after a
// relative block delay or revocation event, and the other paying the // relative block delay or revocation event, and the other paying the
// counterparty within the channel, which can be spent immediately. // counterparty within the channel, which can be spent immediately.
func CreateCommitTx(fundingOutput *wire.TxIn, delayKey, paymentKey *btcec.PublicKey, func CreateCommitTx(fundingOutput *wire.TxIn, keyRing *commitmentKeyRing,
revokeKey *btcec.PublicKey, csvTimeout uint32, amountToSelf, csvTimeout uint32, amountToSelf, amountToThem, dustLimit btcutil.Amount,
amountToThem, dustLimit btcutil.Amount) (*wire.MsgTx, error) { ) (*wire.MsgTx, error) {
// First, we create the script for the delayed "pay-to-self" output. // First, we create the script for the delayed "pay-to-self" output.
// This output has 2 main redemption clauses: either we can redeem the // This output has 2 main redemption clauses: either we can redeem the
// output after a relative block delay, or the remote node can claim // output after a relative block delay, or the remote node can claim
// the funds with the revocation key if we broadcast a revoked // the funds with the revocation key if we broadcast a revoked
// commitment transaction. // commitment transaction.
ourRedeemScript, err := commitScriptToSelf(csvTimeout, delayKey, ourRedeemScript, err := commitScriptToSelf(csvTimeout, keyRing.delayKey,
revokeKey) keyRing.revocationKey)
if err != nil { if err != nil {
return nil, err 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 // Next, we create the script paying to them. This is just a regular
// P2WPKH output, without any added CSV delay. // P2WPKH output, without any added CSV delay.
theirWitnessKeyHash, err := commitScriptUnencumbered(paymentKey) theirWitnessKeyHash, err := commitScriptUnencumbered(keyRing.paymentKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -71,9 +71,13 @@ func TestCommitmentSpendValidation(t *testing.T) {
// This is Alice's commitment transaction, so she must wait a CSV delay // This is Alice's commitment transaction, so she must wait a CSV delay
// of 5 blocks before sweeping the output, while bob can spend // of 5 blocks before sweeping the output, while bob can spend
// immediately with either the revocation key, or his regular key. // immediately with either the revocation key, or his regular key.
commitmentTx, err := CreateCommitTx(fakeFundingTxIn, aliceDelayKey, keyRing := &commitmentKeyRing{
bobPayKey, revokePubKey, csvTimeout, channelBalance, delayKey: aliceDelayKey,
channelBalance, DefaultDustLimit()) revocationKey: revokePubKey,
paymentKey: bobPayKey,
}
commitmentTx, err := CreateCommitTx(fakeFundingTxIn, keyRing, csvTimeout,
channelBalance, channelBalance, DefaultDustLimit())
if err != nil { if err != nil {
t.Fatalf("unable to create commitment transaction: %v", nil) t.Fatalf("unable to create commitment transaction: %v", nil)
} }

View File

@ -649,31 +649,12 @@ func CreateCommitmentTxns(localBalance, remoteBalance btcutil.Amount,
localCommitPoint, remoteCommitPoint *btcec.PublicKey, localCommitPoint, remoteCommitPoint *btcec.PublicKey,
fundingTxIn *wire.TxIn) (*wire.MsgTx, *wire.MsgTx, error) { fundingTxIn *wire.TxIn) (*wire.MsgTx, *wire.MsgTx, error) {
remoteRevocation := DeriveRevocationPubkey( localCommitmentKeys := deriveCommitmentKeys(localCommitPoint, true,
ourChanCfg.RevocationBasePoint, ourChanCfg, theirChanCfg)
remoteCommitPoint, remoteCommitmentKeys := deriveCommitmentKeys(remoteCommitPoint, false,
) ourChanCfg, theirChanCfg)
localRevocation := DeriveRevocationPubkey(
theirChanCfg.RevocationBasePoint,
localCommitPoint,
)
remoteDelayKey := TweakPubKey(theirChanCfg.DelayBasePoint, ourCommitTx, err := CreateCommitTx(fundingTxIn, localCommitmentKeys,
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,
uint32(ourChanCfg.CsvDelay), localBalance, remoteBalance, uint32(ourChanCfg.CsvDelay), localBalance, remoteBalance,
ourChanCfg.DustLimit) ourChanCfg.DustLimit)
if err != nil { if err != nil {
@ -685,8 +666,7 @@ func CreateCommitmentTxns(localBalance, remoteBalance btcutil.Amount,
return nil, nil, err return nil, nil, err
} }
theirCommitTx, err := CreateCommitTx(fundingTxIn, theirCommitTx, err := CreateCommitTx(fundingTxIn, remoteCommitmentKeys,
remoteDelayKey, localPaymentKey, remoteRevocation,
uint32(theirChanCfg.CsvDelay), remoteBalance, localBalance, uint32(theirChanCfg.CsvDelay), remoteBalance, localBalance,
theirChanCfg.DustLimit) theirChanCfg.DustLimit)
if err != nil { if err != nil {