github.com/decred/dcrlnd@v0.7.6/lnwallet/revocation_producer.go (about)

     1  //go:build !rpctest
     2  // +build !rpctest
     3  
     4  package lnwallet
     5  
     6  import (
     7  	"github.com/decred/dcrlnd/keychain"
     8  	"github.com/decred/dcrlnd/shachain"
     9  )
    10  
    11  // nextRevocationProducer creates a new revocation producer, deriving the
    12  // revocation root by applying ECDH to a new key from our revocation root family
    13  // and the multisig key we use for the channel.
    14  func (l *LightningWallet) nextRevocationProducer(res *ChannelReservation,
    15  	keyRing keychain.KeyRing) (shachain.Producer, error) {
    16  
    17  	// Derive the next key in the revocation root family.
    18  	nextRevocationKeyDesc, err := keyRing.DeriveNextKey(
    19  		keychain.KeyFamilyRevocationRoot,
    20  	)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	// If the DeriveNextKey call returns the first key with Index 0, we need
    26  	// to re-derive the key as the keychain/btcwallet.go DerivePrivKey call
    27  	// special-cases Index 0.
    28  	if nextRevocationKeyDesc.Index == 0 {
    29  		nextRevocationKeyDesc, err = keyRing.DeriveNextKey(
    30  			keychain.KeyFamilyRevocationRoot,
    31  		)
    32  		if err != nil {
    33  			return nil, err
    34  		}
    35  	}
    36  
    37  	res.nextRevocationKeyLoc = nextRevocationKeyDesc.KeyLocator
    38  
    39  	// Perform an ECDH operation between the private key described in
    40  	// nextRevocationKeyDesc and our public multisig key. The result will be
    41  	// used to seed the revocation producer.
    42  	revRoot, err := l.ECDH(
    43  		nextRevocationKeyDesc, res.ourContribution.MultiSigKey.PubKey,
    44  	)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	// Once we have the root, we can then generate our shachain producer
    50  	// and from that generate the per-commitment point.
    51  	return shachain.NewRevocationProducer(revRoot), nil
    52  }