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:
parent
4fde31229c
commit
5e3718a1b5
@ -1799,7 +1799,7 @@ func createInitChannels(revocationWindow int) (*lnwallet.LightningChannel, *lnwa
|
||||
|
||||
aliceCommitTx, bobCommitTx, err := lnwallet.CreateCommitmentTxns(
|
||||
channelBal, channelBal, &aliceCfg, &bobCfg, aliceCommitPoint,
|
||||
bobCommitPoint, *fundingTxIn, true,
|
||||
bobCommitPoint, *fundingTxIn, channeldb.SingleFunderTweaklessBit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
|
@ -331,7 +331,8 @@ func (c *chainWatcher) SubscribeChannelEvents() *ChainEventSubscription {
|
||||
// based off of only the set of outputs included.
|
||||
func isOurCommitment(localChanCfg, remoteChanCfg channeldb.ChannelConfig,
|
||||
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
|
||||
// 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
|
||||
// revoke our own commitment.
|
||||
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
|
||||
@ -422,11 +423,6 @@ func (c *chainWatcher) closeObserver(spendNtfn *chainntnfs.SpendEvent) {
|
||||
// revoked state...!!!
|
||||
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()
|
||||
if err != nil {
|
||||
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.RemoteChanCfg, commitSpend,
|
||||
broadcastStateNum, c.cfg.chanState.RevocationProducer,
|
||||
tweaklessCommit,
|
||||
c.cfg.chanState.ChanType,
|
||||
)
|
||||
if err != nil {
|
||||
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
|
||||
// as it isn't actually needed for recovery anymore.
|
||||
commitPoint := c.cfg.chanState.RemoteCurrentRevocation
|
||||
tweaklessCommit := c.cfg.chanState.ChanType.IsTweakless()
|
||||
if !tweaklessCommit {
|
||||
commitPoint = c.waitForCommitmentPoint()
|
||||
if commitPoint == nil {
|
||||
|
@ -262,7 +262,7 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte,
|
||||
|
||||
aliceCommitTx, bobCommitTx, err := lnwallet.CreateCommitmentTxns(
|
||||
aliceAmount, bobAmount, &aliceCfg, &bobCfg, aliceCommitPoint,
|
||||
bobCommitPoint, *fundingTxIn, true,
|
||||
bobCommitPoint, *fundingTxIn, channeldb.SingleFunderTweaklessBit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
|
@ -866,11 +866,6 @@ func (lc *LightningChannel) diskCommitToMemCommit(isLocal bool,
|
||||
diskCommit *channeldb.ChannelCommitment, localCommitPoint,
|
||||
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
|
||||
// party used within this particular state. If this is a pending commit
|
||||
// (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
|
||||
if localCommitPoint != nil {
|
||||
localCommitKeys = DeriveCommitmentKeys(
|
||||
localCommitPoint, true, tweaklessCommit,
|
||||
localCommitPoint, true, lc.channelState.ChanType,
|
||||
&lc.channelState.LocalChanCfg,
|
||||
&lc.channelState.RemoteChanCfg,
|
||||
)
|
||||
}
|
||||
if remoteCommitPoint != nil {
|
||||
remoteCommitKeys = DeriveCommitmentKeys(
|
||||
remoteCommitPoint, false, tweaklessCommit,
|
||||
remoteCommitPoint, false, lc.channelState.ChanType,
|
||||
&lc.channelState.LocalChanCfg,
|
||||
&lc.channelState.RemoteChanCfg,
|
||||
)
|
||||
@ -1581,9 +1576,8 @@ func (lc *LightningChannel) restoreCommitState(
|
||||
|
||||
// We'll also re-create the set of commitment keys needed to
|
||||
// fully re-derive the state.
|
||||
tweaklessCommit := lc.channelState.ChanType.IsTweakless()
|
||||
pendingRemoteKeyChain = DeriveCommitmentKeys(
|
||||
pendingCommitPoint, false, tweaklessCommit,
|
||||
pendingCommitPoint, false, lc.channelState.ChanType,
|
||||
&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
|
||||
// keys we'll need to reconstruct the commitment state,
|
||||
tweaklessCommit := chanState.ChanType.IsTweakless()
|
||||
keyRing := DeriveCommitmentKeys(
|
||||
commitmentPoint, false, tweaklessCommit,
|
||||
commitmentPoint, false, chanState.ChanType,
|
||||
&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
|
||||
// out the SingleTweak value as it isn't needed.
|
||||
tweaklessCommit := chanState.ChanType.IsTweakless()
|
||||
if tweaklessCommit {
|
||||
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
|
||||
// construct the commitment state.
|
||||
keyRing := DeriveCommitmentKeys(
|
||||
commitPoint, false, lc.channelState.ChanType.IsTweakless(),
|
||||
commitPoint, false, lc.channelState.ChanType,
|
||||
&lc.channelState.LocalChanCfg, &lc.channelState.RemoteChanCfg,
|
||||
)
|
||||
|
||||
@ -3744,7 +3738,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSig lnwire.Sig,
|
||||
}
|
||||
commitPoint := input.ComputeCommitmentPoint(commitSecret[:])
|
||||
keyRing := DeriveCommitmentKeys(
|
||||
commitPoint, true, lc.channelState.ChanType.IsTweakless(),
|
||||
commitPoint, true, lc.channelState.ChanType,
|
||||
&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
|
||||
// so we can re-construct the HTLC state and also our payment key.
|
||||
tweaklessCommit := chanState.ChanType.IsTweakless()
|
||||
keyRing := DeriveCommitmentKeys(
|
||||
commitPoint, false, tweaklessCommit, &chanState.LocalChanCfg,
|
||||
&chanState.RemoteChanCfg,
|
||||
commitPoint, false, chanState.ChanType,
|
||||
&chanState.LocalChanCfg, &chanState.RemoteChanCfg,
|
||||
)
|
||||
|
||||
// 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
|
||||
// out the SingleTweak value as it isn't needed.
|
||||
tweaklessCommit := chanState.ChanType.IsTweakless()
|
||||
if tweaklessCommit {
|
||||
commitResolution.SelfOutputSignDesc.SingleTweak = nil
|
||||
}
|
||||
@ -5407,11 +5401,13 @@ func NewLocalForceCloseSummary(chanState *channeldb.OpenChannel, signer input.Si
|
||||
}
|
||||
commitPoint := input.ComputeCommitmentPoint(revocation[:])
|
||||
keyRing := DeriveCommitmentKeys(
|
||||
commitPoint, true, chanState.ChanType.IsTweakless(),
|
||||
commitPoint, true, chanState.ChanType,
|
||||
&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 {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -86,13 +86,16 @@ type CommitmentKeyRing struct {
|
||||
RevocationKey *btcec.PublicKey
|
||||
}
|
||||
|
||||
// DeriveCommitmentKey generates a new commitment key set using the base points
|
||||
// and commitment point. The keys are derived differently depending whether the
|
||||
// commitment transaction is ours or the remote peer's.
|
||||
// DeriveCommitmentKeys generates a new commitment key set using the base points
|
||||
// and commitment point. The keys are derived differently depending on the type
|
||||
// of channel, and whether the commitment transaction is ours or the remote
|
||||
// peer's.
|
||||
func DeriveCommitmentKeys(commitPoint *btcec.PublicKey,
|
||||
isOurCommit, tweaklessCommit bool,
|
||||
isOurCommit bool, chanType channeldb.ChannelType,
|
||||
localChanCfg, remoteChanCfg *channeldb.ChannelConfig) *CommitmentKeyRing {
|
||||
|
||||
tweaklessCommit := chanType.IsTweakless()
|
||||
|
||||
// First, we'll derive all the keys that don't depend on the context of
|
||||
// whose commitment transaction this is.
|
||||
keyRing := &CommitmentKeyRing{
|
||||
|
@ -206,9 +206,14 @@ func CreateTestChannels(tweaklessCommits bool) (
|
||||
}
|
||||
aliceCommitPoint := input.ComputeCommitmentPoint(aliceFirstRevoke[:])
|
||||
|
||||
chanType := channeldb.SingleFunderTweaklessBit
|
||||
if !tweaklessCommits {
|
||||
chanType = channeldb.SingleFunderBit
|
||||
}
|
||||
|
||||
aliceCommitTx, bobCommitTx, err := CreateCommitmentTxns(
|
||||
channelBal, channelBal, &aliceCfg, &bobCfg, aliceCommitPoint,
|
||||
bobCommitPoint, *fundingTxIn, tweaklessCommits,
|
||||
bobCommitPoint, *fundingTxIn, chanType,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
@ -275,7 +280,7 @@ func CreateTestChannels(tweaklessCommits bool) (
|
||||
IdentityPub: aliceKeys[0].PubKey(),
|
||||
FundingOutpoint: *prevOut,
|
||||
ShortChannelID: shortChanID,
|
||||
ChanType: channeldb.SingleFunderTweaklessBit,
|
||||
ChanType: chanType,
|
||||
IsInitiator: true,
|
||||
Capacity: channelCapacity,
|
||||
RemoteCurrentRevocation: bobCommitPoint,
|
||||
@ -293,7 +298,7 @@ func CreateTestChannels(tweaklessCommits bool) (
|
||||
IdentityPub: bobKeys[0].PubKey(),
|
||||
FundingOutpoint: *prevOut,
|
||||
ShortChannelID: shortChanID,
|
||||
ChanType: channeldb.SingleFunderTweaklessBit,
|
||||
ChanType: chanType,
|
||||
IsInitiator: false,
|
||||
Capacity: channelCapacity,
|
||||
RemoteCurrentRevocation: aliceCommitPoint,
|
||||
@ -305,11 +310,6 @@ func CreateTestChannels(tweaklessCommits bool) (
|
||||
Packager: channeldb.NewChannelPackager(shortChanID),
|
||||
}
|
||||
|
||||
if !tweaklessCommits {
|
||||
aliceChannelState.ChanType = channeldb.SingleFunderBit
|
||||
bobChannelState.ChanType = channeldb.SingleFunderBit
|
||||
}
|
||||
|
||||
aliceSigner := &input.MockSigner{Privkeys: aliceKeys}
|
||||
bobSigner := &input.MockSigner{Privkeys: bobKeys}
|
||||
|
||||
|
@ -772,16 +772,14 @@ func (l *LightningWallet) handleFundingCancelRequest(req *fundingReserveCancelMs
|
||||
func CreateCommitmentTxns(localBalance, remoteBalance btcutil.Amount,
|
||||
ourChanCfg, theirChanCfg *channeldb.ChannelConfig,
|
||||
localCommitPoint, remoteCommitPoint *btcec.PublicKey,
|
||||
fundingTxIn wire.TxIn,
|
||||
tweaklessCommit bool) (*wire.MsgTx, *wire.MsgTx, error) {
|
||||
fundingTxIn wire.TxIn, chanType channeldb.ChannelType) (
|
||||
*wire.MsgTx, *wire.MsgTx, error) {
|
||||
|
||||
localCommitmentKeys := DeriveCommitmentKeys(
|
||||
localCommitPoint, true, tweaklessCommit, ourChanCfg,
|
||||
theirChanCfg,
|
||||
localCommitPoint, true, chanType, ourChanCfg, theirChanCfg,
|
||||
)
|
||||
remoteCommitmentKeys := DeriveCommitmentKeys(
|
||||
remoteCommitPoint, false, tweaklessCommit, ourChanCfg,
|
||||
theirChanCfg,
|
||||
remoteCommitPoint, false, chanType, ourChanCfg, theirChanCfg,
|
||||
)
|
||||
|
||||
ourCommitTx, err := CreateCommitTx(
|
||||
@ -930,13 +928,12 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
|
||||
// With the funding tx complete, create both commitment transactions.
|
||||
localBalance := pendingReservation.partialState.LocalCommitment.LocalBalance.ToSatoshis()
|
||||
remoteBalance := pendingReservation.partialState.LocalCommitment.RemoteBalance.ToSatoshis()
|
||||
tweaklessCommits := pendingReservation.partialState.ChanType.IsTweakless()
|
||||
ourCommitTx, theirCommitTx, err := CreateCommitmentTxns(
|
||||
localBalance, remoteBalance, ourContribution.ChannelConfig,
|
||||
theirContribution.ChannelConfig,
|
||||
ourContribution.FirstCommitmentPoint,
|
||||
theirContribution.FirstCommitmentPoint, fundingTxIn,
|
||||
tweaklessCommits,
|
||||
pendingReservation.partialState.ChanType,
|
||||
)
|
||||
if err != nil {
|
||||
req.err <- err
|
||||
@ -1290,14 +1287,13 @@ func (l *LightningWallet) handleSingleFunderSigs(req *addSingleFunderSigsMsg) {
|
||||
// remote node's commitment transactions.
|
||||
localBalance := pendingReservation.partialState.LocalCommitment.LocalBalance.ToSatoshis()
|
||||
remoteBalance := pendingReservation.partialState.LocalCommitment.RemoteBalance.ToSatoshis()
|
||||
tweaklessCommits := pendingReservation.partialState.ChanType.IsTweakless()
|
||||
ourCommitTx, theirCommitTx, err := CreateCommitmentTxns(
|
||||
localBalance, remoteBalance,
|
||||
pendingReservation.ourContribution.ChannelConfig,
|
||||
pendingReservation.theirContribution.ChannelConfig,
|
||||
pendingReservation.ourContribution.FirstCommitmentPoint,
|
||||
pendingReservation.theirContribution.FirstCommitmentPoint,
|
||||
*fundingTxIn, tweaklessCommits,
|
||||
*fundingTxIn, pendingReservation.partialState.ChanType,
|
||||
)
|
||||
if err != nil {
|
||||
req.err <- err
|
||||
|
@ -195,7 +195,7 @@ func createTestPeer(notifier chainntnfs.ChainNotifier, publTx chan *wire.MsgTx,
|
||||
|
||||
aliceCommitTx, bobCommitTx, err := lnwallet.CreateCommitmentTxns(
|
||||
channelBal, channelBal, &aliceCfg, &bobCfg, aliceCommitPoint,
|
||||
bobCommitPoint, *fundingTxIn, true,
|
||||
bobCommitPoint, *fundingTxIn, channeldb.SingleFunderTweaklessBit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, nil, nil, nil, err
|
||||
|
Loading…
Reference in New Issue
Block a user