revocation_producer.go 1.9 KB

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