|
8 | 8 |
|
9 | 9 | "github.com/lightninglabs/chantools/lnd" |
10 | 10 | "github.com/lightningnetwork/lnd/chanbackup" |
| 11 | + "github.com/lightningnetwork/lnd/input" |
11 | 12 | "github.com/lightningnetwork/lnd/keychain" |
12 | 13 | "github.com/spf13/cobra" |
13 | 14 | ) |
@@ -77,10 +78,61 @@ func fixOldChannelBackup(multiFile *chanbackup.MultiFile, |
77 | 78 | for idx, single := range multi.StaticBackups { |
78 | 79 | err := ring.CheckDescriptor(single.ShaChainRootDesc) |
79 | 80 | switch { |
80 | | - case err == nil: |
| 81 | + // If the descriptor is correct or the error is because no |
| 82 | + // public key is provided, skip the channel. This can happen |
| 83 | + // if the backup file has partially valid and invalid channels. |
| 84 | + case err == nil || errors.Is(err, lnd.ErrNoPublicKeyProvided): |
| 85 | + log.Infof("The shachain root for channel %s is "+ |
| 86 | + "correct, skipping...", |
| 87 | + single.FundingOutpoint.String()) |
| 88 | + |
81 | 89 | continue |
82 | 90 |
|
83 | 91 | case errors.Is(err, keychain.ErrCannotDerivePrivKey): |
| 92 | + // Before fixing the descriptor, print the funding multisig |
| 93 | + // P2WSH witness program hash. Derive the local multisig pubkey |
| 94 | + // via its locator if needed (and possible), similar to |
| 95 | + // CheckDescriptor. |
| 96 | + localPub, err := ring.PubKeyForDescriptor( |
| 97 | + single.LocalChanCfg.MultiSigKey, |
| 98 | + ) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("could not derive local "+ |
| 101 | + "multisig pubkey for channel %s: %w", |
| 102 | + single.FundingOutpoint.String(), err) |
| 103 | + } |
| 104 | + |
| 105 | + remotePub, err := ring.PubKeyForDescriptor( |
| 106 | + single.RemoteChanCfg.MultiSigKey, |
| 107 | + ) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("could not derive remote "+ |
| 110 | + "multisig pubkey for channel %s: %w", |
| 111 | + single.FundingOutpoint.String(), err) |
| 112 | + } |
| 113 | + |
| 114 | + a := localPub.SerializeCompressed() |
| 115 | + b := remotePub.SerializeCompressed() |
| 116 | + msScript, err := input.GenMultiSigScript(a, b) |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("could not generate "+ |
| 119 | + "multisig script for channel "+ |
| 120 | + "%s: %w", |
| 121 | + single.FundingOutpoint.String(), |
| 122 | + err) |
| 123 | + } |
| 124 | + |
| 125 | + msHash, err := input.WitnessScriptHash(msScript) |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("could not compute"+ |
| 128 | + "multisig script hash: %w", err) |
| 129 | + } |
| 130 | + |
| 131 | + log.Infof("Channel %s funding multisig P2WSH "+ |
| 132 | + "(pubkey): %x", |
| 133 | + single.FundingOutpoint.String(), |
| 134 | + msHash[:]) |
| 135 | + |
84 | 136 | // Fix the incorrect descriptor by deriving a default |
85 | 137 | // one and overwriting it in the backup. |
86 | 138 | log.Infof("The shachain root for channel %s could "+ |
|
0 commit comments