lnwallet: use channel type to derive keys

We abstract away how keys are generated for the different channel types
types (currently tweak(less)).

Intention is that more of the logic that is unique for each commitment
type lives in commitment.go, making the channel state machine oblivious
to the keys and outputs being created on the commitment tx for a given
channel state.
This commit is contained in:
Johan T. Halseth 2020-01-06 11:42:04 +01:00
parent 4fde31229c
commit 5e3718a1b5
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
8 changed files with 44 additions and 52 deletions

View File

@ -1799,7 +1799,7 @@ func createInitChannels(revocationWindow int) (*lnwallet.LightningChannel, *lnwa
aliceCommitTx, bobCommitTx, err := lnwallet.CreateCommitmentTxns( aliceCommitTx, bobCommitTx, err := lnwallet.CreateCommitmentTxns(
channelBal, channelBal, &aliceCfg, &bobCfg, aliceCommitPoint, channelBal, channelBal, &aliceCfg, &bobCfg, aliceCommitPoint,
bobCommitPoint, *fundingTxIn, true, bobCommitPoint, *fundingTxIn, channeldb.SingleFunderTweaklessBit,
) )
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err

View File

@ -331,7 +331,8 @@ func (c *chainWatcher) SubscribeChannelEvents() *ChainEventSubscription {
// based off of only the set of outputs included. // based off of only the set of outputs included.
func isOurCommitment(localChanCfg, remoteChanCfg channeldb.ChannelConfig, func isOurCommitment(localChanCfg, remoteChanCfg channeldb.ChannelConfig,
commitSpend *chainntnfs.SpendDetail, broadcastStateNum uint64, commitSpend *chainntnfs.SpendDetail, broadcastStateNum uint64,
revocationProducer shachain.Producer, tweakless bool) (bool, error) { revocationProducer shachain.Producer,
chanType channeldb.ChannelType) (bool, error) {
// First, we'll re-derive our commitment point for this state since // First, we'll re-derive our commitment point for this state since
// this is what we use to randomize each of the keys for this state. // this is what we use to randomize each of the keys for this state.
@ -345,7 +346,7 @@ func isOurCommitment(localChanCfg, remoteChanCfg channeldb.ChannelConfig,
// and remote keys for this state. We use our point as only we can // and remote keys for this state. We use our point as only we can
// revoke our own commitment. // revoke our own commitment.
commitKeyRing := lnwallet.DeriveCommitmentKeys( commitKeyRing := lnwallet.DeriveCommitmentKeys(
commitPoint, true, tweakless, &localChanCfg, &remoteChanCfg, commitPoint, true, chanType, &localChanCfg, &remoteChanCfg,
) )
// With the keys derived, we'll construct the remote script that'll be // With the keys derived, we'll construct the remote script that'll be
@ -422,11 +423,6 @@ func (c *chainWatcher) closeObserver(spendNtfn *chainntnfs.SpendEvent) {
// revoked state...!!! // revoked state...!!!
commitTxBroadcast := commitSpend.SpendingTx commitTxBroadcast := commitSpend.SpendingTx
// An additional piece of information we need to properly
// dispatch a close event if is this channel was using the
// tweakless remove key format or not.
tweaklessCommit := c.cfg.chanState.ChanType.IsTweakless()
localCommit, remoteCommit, err := c.cfg.chanState.LatestCommitments() localCommit, remoteCommit, err := c.cfg.chanState.LatestCommitments()
if err != nil { if err != nil {
log.Errorf("Unable to fetch channel state for "+ log.Errorf("Unable to fetch channel state for "+
@ -484,7 +480,7 @@ func (c *chainWatcher) closeObserver(spendNtfn *chainntnfs.SpendEvent) {
c.cfg.chanState.LocalChanCfg, c.cfg.chanState.LocalChanCfg,
c.cfg.chanState.RemoteChanCfg, commitSpend, c.cfg.chanState.RemoteChanCfg, commitSpend,
broadcastStateNum, c.cfg.chanState.RevocationProducer, broadcastStateNum, c.cfg.chanState.RevocationProducer,
tweaklessCommit, c.cfg.chanState.ChanType,
) )
if err != nil { if err != nil {
log.Errorf("unable to determine self commit for "+ log.Errorf("unable to determine self commit for "+
@ -596,6 +592,7 @@ func (c *chainWatcher) closeObserver(spendNtfn *chainntnfs.SpendEvent) {
// close and sweep immediately using a fake commitPoint // close and sweep immediately using a fake commitPoint
// as it isn't actually needed for recovery anymore. // as it isn't actually needed for recovery anymore.
commitPoint := c.cfg.chanState.RemoteCurrentRevocation commitPoint := c.cfg.chanState.RemoteCurrentRevocation
tweaklessCommit := c.cfg.chanState.ChanType.IsTweakless()
if !tweaklessCommit { if !tweaklessCommit {
commitPoint = c.waitForCommitmentPoint() commitPoint = c.waitForCommitmentPoint()
if commitPoint == nil { if commitPoint == nil {

View File

@ -262,7 +262,7 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte,
aliceCommitTx, bobCommitTx, err := lnwallet.CreateCommitmentTxns( aliceCommitTx, bobCommitTx, err := lnwallet.CreateCommitmentTxns(
aliceAmount, bobAmount, &aliceCfg, &bobCfg, aliceCommitPoint, aliceAmount, bobAmount, &aliceCfg, &bobCfg, aliceCommitPoint,
bobCommitPoint, *fundingTxIn, true, bobCommitPoint, *fundingTxIn, channeldb.SingleFunderTweaklessBit,
) )
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err

View File

@ -866,11 +866,6 @@ func (lc *LightningChannel) diskCommitToMemCommit(isLocal bool,
diskCommit *channeldb.ChannelCommitment, localCommitPoint, diskCommit *channeldb.ChannelCommitment, localCommitPoint,
remoteCommitPoint *btcec.PublicKey) (*commitment, error) { remoteCommitPoint *btcec.PublicKey) (*commitment, error) {
// If this commit is tweakless, then it'll affect the way we derive our
// keys, which will affect the commitment transaction reconstruction.
// So we'll determine this first, before we do anything else.
tweaklessCommit := lc.channelState.ChanType.IsTweakless()
// First, we'll need to re-derive the commitment key ring for each // First, we'll need to re-derive the commitment key ring for each
// party used within this particular state. If this is a pending commit // party used within this particular state. If this is a pending commit
// (we extended but weren't able to complete the commitment dance // (we extended but weren't able to complete the commitment dance
@ -879,14 +874,14 @@ func (lc *LightningChannel) diskCommitToMemCommit(isLocal bool,
var localCommitKeys, remoteCommitKeys *CommitmentKeyRing var localCommitKeys, remoteCommitKeys *CommitmentKeyRing
if localCommitPoint != nil { if localCommitPoint != nil {
localCommitKeys = DeriveCommitmentKeys( localCommitKeys = DeriveCommitmentKeys(
localCommitPoint, true, tweaklessCommit, localCommitPoint, true, lc.channelState.ChanType,
&lc.channelState.LocalChanCfg, &lc.channelState.LocalChanCfg,
&lc.channelState.RemoteChanCfg, &lc.channelState.RemoteChanCfg,
) )
} }
if remoteCommitPoint != nil { if remoteCommitPoint != nil {
remoteCommitKeys = DeriveCommitmentKeys( remoteCommitKeys = DeriveCommitmentKeys(
remoteCommitPoint, false, tweaklessCommit, remoteCommitPoint, false, lc.channelState.ChanType,
&lc.channelState.LocalChanCfg, &lc.channelState.LocalChanCfg,
&lc.channelState.RemoteChanCfg, &lc.channelState.RemoteChanCfg,
) )
@ -1581,9 +1576,8 @@ func (lc *LightningChannel) restoreCommitState(
// We'll also re-create the set of commitment keys needed to // We'll also re-create the set of commitment keys needed to
// fully re-derive the state. // fully re-derive the state.
tweaklessCommit := lc.channelState.ChanType.IsTweakless()
pendingRemoteKeyChain = DeriveCommitmentKeys( pendingRemoteKeyChain = DeriveCommitmentKeys(
pendingCommitPoint, false, tweaklessCommit, pendingCommitPoint, false, lc.channelState.ChanType,
&lc.channelState.LocalChanCfg, &lc.channelState.RemoteChanCfg, &lc.channelState.LocalChanCfg, &lc.channelState.RemoteChanCfg,
) )
} }
@ -1869,9 +1863,8 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
// With the commitment point generated, we can now generate the four // With the commitment point generated, we can now generate the four
// keys we'll need to reconstruct the commitment state, // keys we'll need to reconstruct the commitment state,
tweaklessCommit := chanState.ChanType.IsTweakless()
keyRing := DeriveCommitmentKeys( keyRing := DeriveCommitmentKeys(
commitmentPoint, false, tweaklessCommit, commitmentPoint, false, chanState.ChanType,
&chanState.LocalChanCfg, &chanState.RemoteChanCfg, &chanState.LocalChanCfg, &chanState.RemoteChanCfg,
) )
@ -1939,6 +1932,7 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
// If this is a tweakless commitment, then we can safely blank // If this is a tweakless commitment, then we can safely blank
// out the SingleTweak value as it isn't needed. // out the SingleTweak value as it isn't needed.
tweaklessCommit := chanState.ChanType.IsTweakless()
if tweaklessCommit { if tweaklessCommit {
localSignDesc.SingleTweak = nil localSignDesc.SingleTweak = nil
} }
@ -2986,7 +2980,7 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig, []ch
// used within fetchCommitmentView to derive all the keys necessary to // used within fetchCommitmentView to derive all the keys necessary to
// construct the commitment state. // construct the commitment state.
keyRing := DeriveCommitmentKeys( keyRing := DeriveCommitmentKeys(
commitPoint, false, lc.channelState.ChanType.IsTweakless(), commitPoint, false, lc.channelState.ChanType,
&lc.channelState.LocalChanCfg, &lc.channelState.RemoteChanCfg, &lc.channelState.LocalChanCfg, &lc.channelState.RemoteChanCfg,
) )
@ -3744,7 +3738,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSig lnwire.Sig,
} }
commitPoint := input.ComputeCommitmentPoint(commitSecret[:]) commitPoint := input.ComputeCommitmentPoint(commitSecret[:])
keyRing := DeriveCommitmentKeys( keyRing := DeriveCommitmentKeys(
commitPoint, true, lc.channelState.ChanType.IsTweakless(), commitPoint, true, lc.channelState.ChanType,
&lc.channelState.LocalChanCfg, &lc.channelState.RemoteChanCfg, &lc.channelState.LocalChanCfg, &lc.channelState.RemoteChanCfg,
) )
@ -4749,10 +4743,9 @@ func NewUnilateralCloseSummary(chanState *channeldb.OpenChannel, signer input.Si
// First, we'll generate the commitment point and the revocation point // First, we'll generate the commitment point and the revocation point
// so we can re-construct the HTLC state and also our payment key. // so we can re-construct the HTLC state and also our payment key.
tweaklessCommit := chanState.ChanType.IsTweakless()
keyRing := DeriveCommitmentKeys( keyRing := DeriveCommitmentKeys(
commitPoint, false, tweaklessCommit, &chanState.LocalChanCfg, commitPoint, false, chanState.ChanType,
&chanState.RemoteChanCfg, &chanState.LocalChanCfg, &chanState.RemoteChanCfg,
) )
// Next, we'll obtain HTLC resolutions for all the outgoing HTLC's we // Next, we'll obtain HTLC resolutions for all the outgoing HTLC's we
@ -4817,6 +4810,7 @@ func NewUnilateralCloseSummary(chanState *channeldb.OpenChannel, signer input.Si
// If this is a tweakless commitment, then we can safely blank // If this is a tweakless commitment, then we can safely blank
// out the SingleTweak value as it isn't needed. // out the SingleTweak value as it isn't needed.
tweaklessCommit := chanState.ChanType.IsTweakless()
if tweaklessCommit { if tweaklessCommit {
commitResolution.SelfOutputSignDesc.SingleTweak = nil commitResolution.SelfOutputSignDesc.SingleTweak = nil
} }
@ -5407,11 +5401,13 @@ func NewLocalForceCloseSummary(chanState *channeldb.OpenChannel, signer input.Si
} }
commitPoint := input.ComputeCommitmentPoint(revocation[:]) commitPoint := input.ComputeCommitmentPoint(revocation[:])
keyRing := DeriveCommitmentKeys( keyRing := DeriveCommitmentKeys(
commitPoint, true, chanState.ChanType.IsTweakless(), commitPoint, true, chanState.ChanType,
&chanState.LocalChanCfg, &chanState.RemoteChanCfg, &chanState.LocalChanCfg, &chanState.RemoteChanCfg,
) )
selfScript, err := input.CommitScriptToSelf(csvTimeout, keyRing.ToLocalKey,
keyRing.RevocationKey) selfScript, err := input.CommitScriptToSelf(
csvTimeout, keyRing.ToLocalKey, keyRing.RevocationKey,
)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -86,13 +86,16 @@ type CommitmentKeyRing struct {
RevocationKey *btcec.PublicKey RevocationKey *btcec.PublicKey
} }
// DeriveCommitmentKey generates a new commitment key set using the base points // DeriveCommitmentKeys generates a new commitment key set using the base points
// and commitment point. The keys are derived differently depending whether the // and commitment point. The keys are derived differently depending on the type
// commitment transaction is ours or the remote peer's. // of channel, and whether the commitment transaction is ours or the remote
// peer's.
func DeriveCommitmentKeys(commitPoint *btcec.PublicKey, func DeriveCommitmentKeys(commitPoint *btcec.PublicKey,
isOurCommit, tweaklessCommit bool, isOurCommit bool, chanType channeldb.ChannelType,
localChanCfg, remoteChanCfg *channeldb.ChannelConfig) *CommitmentKeyRing { localChanCfg, remoteChanCfg *channeldb.ChannelConfig) *CommitmentKeyRing {
tweaklessCommit := chanType.IsTweakless()
// First, we'll derive all the keys that don't depend on the context of // First, we'll derive all the keys that don't depend on the context of
// whose commitment transaction this is. // whose commitment transaction this is.
keyRing := &CommitmentKeyRing{ keyRing := &CommitmentKeyRing{

View File

@ -206,9 +206,14 @@ func CreateTestChannels(tweaklessCommits bool) (
} }
aliceCommitPoint := input.ComputeCommitmentPoint(aliceFirstRevoke[:]) aliceCommitPoint := input.ComputeCommitmentPoint(aliceFirstRevoke[:])
chanType := channeldb.SingleFunderTweaklessBit
if !tweaklessCommits {
chanType = channeldb.SingleFunderBit
}
aliceCommitTx, bobCommitTx, err := CreateCommitmentTxns( aliceCommitTx, bobCommitTx, err := CreateCommitmentTxns(
channelBal, channelBal, &aliceCfg, &bobCfg, aliceCommitPoint, channelBal, channelBal, &aliceCfg, &bobCfg, aliceCommitPoint,
bobCommitPoint, *fundingTxIn, tweaklessCommits, bobCommitPoint, *fundingTxIn, chanType,
) )
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
@ -275,7 +280,7 @@ func CreateTestChannels(tweaklessCommits bool) (
IdentityPub: aliceKeys[0].PubKey(), IdentityPub: aliceKeys[0].PubKey(),
FundingOutpoint: *prevOut, FundingOutpoint: *prevOut,
ShortChannelID: shortChanID, ShortChannelID: shortChanID,
ChanType: channeldb.SingleFunderTweaklessBit, ChanType: chanType,
IsInitiator: true, IsInitiator: true,
Capacity: channelCapacity, Capacity: channelCapacity,
RemoteCurrentRevocation: bobCommitPoint, RemoteCurrentRevocation: bobCommitPoint,
@ -293,7 +298,7 @@ func CreateTestChannels(tweaklessCommits bool) (
IdentityPub: bobKeys[0].PubKey(), IdentityPub: bobKeys[0].PubKey(),
FundingOutpoint: *prevOut, FundingOutpoint: *prevOut,
ShortChannelID: shortChanID, ShortChannelID: shortChanID,
ChanType: channeldb.SingleFunderTweaklessBit, ChanType: chanType,
IsInitiator: false, IsInitiator: false,
Capacity: channelCapacity, Capacity: channelCapacity,
RemoteCurrentRevocation: aliceCommitPoint, RemoteCurrentRevocation: aliceCommitPoint,
@ -305,11 +310,6 @@ func CreateTestChannels(tweaklessCommits bool) (
Packager: channeldb.NewChannelPackager(shortChanID), Packager: channeldb.NewChannelPackager(shortChanID),
} }
if !tweaklessCommits {
aliceChannelState.ChanType = channeldb.SingleFunderBit
bobChannelState.ChanType = channeldb.SingleFunderBit
}
aliceSigner := &input.MockSigner{Privkeys: aliceKeys} aliceSigner := &input.MockSigner{Privkeys: aliceKeys}
bobSigner := &input.MockSigner{Privkeys: bobKeys} bobSigner := &input.MockSigner{Privkeys: bobKeys}

View File

@ -772,16 +772,14 @@ func (l *LightningWallet) handleFundingCancelRequest(req *fundingReserveCancelMs
func CreateCommitmentTxns(localBalance, remoteBalance btcutil.Amount, func CreateCommitmentTxns(localBalance, remoteBalance btcutil.Amount,
ourChanCfg, theirChanCfg *channeldb.ChannelConfig, ourChanCfg, theirChanCfg *channeldb.ChannelConfig,
localCommitPoint, remoteCommitPoint *btcec.PublicKey, localCommitPoint, remoteCommitPoint *btcec.PublicKey,
fundingTxIn wire.TxIn, fundingTxIn wire.TxIn, chanType channeldb.ChannelType) (
tweaklessCommit bool) (*wire.MsgTx, *wire.MsgTx, error) { *wire.MsgTx, *wire.MsgTx, error) {
localCommitmentKeys := DeriveCommitmentKeys( localCommitmentKeys := DeriveCommitmentKeys(
localCommitPoint, true, tweaklessCommit, ourChanCfg, localCommitPoint, true, chanType, ourChanCfg, theirChanCfg,
theirChanCfg,
) )
remoteCommitmentKeys := DeriveCommitmentKeys( remoteCommitmentKeys := DeriveCommitmentKeys(
remoteCommitPoint, false, tweaklessCommit, ourChanCfg, remoteCommitPoint, false, chanType, ourChanCfg, theirChanCfg,
theirChanCfg,
) )
ourCommitTx, err := CreateCommitTx( ourCommitTx, err := CreateCommitTx(
@ -930,13 +928,12 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
// With the funding tx complete, create both commitment transactions. // With the funding tx complete, create both commitment transactions.
localBalance := pendingReservation.partialState.LocalCommitment.LocalBalance.ToSatoshis() localBalance := pendingReservation.partialState.LocalCommitment.LocalBalance.ToSatoshis()
remoteBalance := pendingReservation.partialState.LocalCommitment.RemoteBalance.ToSatoshis() remoteBalance := pendingReservation.partialState.LocalCommitment.RemoteBalance.ToSatoshis()
tweaklessCommits := pendingReservation.partialState.ChanType.IsTweakless()
ourCommitTx, theirCommitTx, err := CreateCommitmentTxns( ourCommitTx, theirCommitTx, err := CreateCommitmentTxns(
localBalance, remoteBalance, ourContribution.ChannelConfig, localBalance, remoteBalance, ourContribution.ChannelConfig,
theirContribution.ChannelConfig, theirContribution.ChannelConfig,
ourContribution.FirstCommitmentPoint, ourContribution.FirstCommitmentPoint,
theirContribution.FirstCommitmentPoint, fundingTxIn, theirContribution.FirstCommitmentPoint, fundingTxIn,
tweaklessCommits, pendingReservation.partialState.ChanType,
) )
if err != nil { if err != nil {
req.err <- err req.err <- err
@ -1290,14 +1287,13 @@ func (l *LightningWallet) handleSingleFunderSigs(req *addSingleFunderSigsMsg) {
// remote node's commitment transactions. // remote node's commitment transactions.
localBalance := pendingReservation.partialState.LocalCommitment.LocalBalance.ToSatoshis() localBalance := pendingReservation.partialState.LocalCommitment.LocalBalance.ToSatoshis()
remoteBalance := pendingReservation.partialState.LocalCommitment.RemoteBalance.ToSatoshis() remoteBalance := pendingReservation.partialState.LocalCommitment.RemoteBalance.ToSatoshis()
tweaklessCommits := pendingReservation.partialState.ChanType.IsTweakless()
ourCommitTx, theirCommitTx, err := CreateCommitmentTxns( ourCommitTx, theirCommitTx, err := CreateCommitmentTxns(
localBalance, remoteBalance, localBalance, remoteBalance,
pendingReservation.ourContribution.ChannelConfig, pendingReservation.ourContribution.ChannelConfig,
pendingReservation.theirContribution.ChannelConfig, pendingReservation.theirContribution.ChannelConfig,
pendingReservation.ourContribution.FirstCommitmentPoint, pendingReservation.ourContribution.FirstCommitmentPoint,
pendingReservation.theirContribution.FirstCommitmentPoint, pendingReservation.theirContribution.FirstCommitmentPoint,
*fundingTxIn, tweaklessCommits, *fundingTxIn, pendingReservation.partialState.ChanType,
) )
if err != nil { if err != nil {
req.err <- err req.err <- err

View File

@ -195,7 +195,7 @@ func createTestPeer(notifier chainntnfs.ChainNotifier, publTx chan *wire.MsgTx,
aliceCommitTx, bobCommitTx, err := lnwallet.CreateCommitmentTxns( aliceCommitTx, bobCommitTx, err := lnwallet.CreateCommitmentTxns(
channelBal, channelBal, &aliceCfg, &bobCfg, aliceCommitPoint, channelBal, channelBal, &aliceCfg, &bobCfg, aliceCommitPoint,
bobCommitPoint, *fundingTxIn, true, bobCommitPoint, *fundingTxIn, channeldb.SingleFunderTweaklessBit,
) )
if err != nil { if err != nil {
return nil, nil, nil, nil, err return nil, nil, nil, nil, err