|
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,15 +78,64 @@ 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 %v is "+ |
| 86 | + "correct, skipping...", single.FundingOutpoint) |
| 87 | + |
81 | 88 | continue |
82 | 89 |
|
83 | 90 | case errors.Is(err, keychain.ErrCannotDerivePrivKey): |
| 91 | + // Before fixing the descriptor, print the funding |
| 92 | + // multisig P2WSH witness program hash. Derive the |
| 93 | + // local multisig pubkey via its locator if needed |
| 94 | + // (and possible). |
| 95 | + localPub, err := ring.PubKeyForDescriptor( |
| 96 | + single.LocalChanCfg.MultiSigKey, |
| 97 | + ) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("could not derive local "+ |
| 100 | + "multisig pubkey for channel %v: %w", |
| 101 | + single.FundingOutpoint, err) |
| 102 | + } |
| 103 | + |
| 104 | + remotePub, err := ring.PubKeyForDescriptor( |
| 105 | + single.RemoteChanCfg.MultiSigKey, |
| 106 | + ) |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("could not derive remote "+ |
| 109 | + "multisig pubkey for channel %v: %w", |
| 110 | + single.FundingOutpoint, err) |
| 111 | + } |
| 112 | + |
| 113 | + a := localPub.SerializeCompressed() |
| 114 | + b := remotePub.SerializeCompressed() |
| 115 | + msScript, err := input.GenMultiSigScript(a, b) |
| 116 | + if err != nil { |
| 117 | + return fmt.Errorf("could not generate "+ |
| 118 | + "multisig script for channel "+ |
| 119 | + "%v: %w", |
| 120 | + single.FundingOutpoint, err) |
| 121 | + } |
| 122 | + |
| 123 | + msHash, err := input.WitnessScriptHash(msScript) |
| 124 | + if err != nil { |
| 125 | + return fmt.Errorf("could not compute "+ |
| 126 | + "multisig script hash: %w", err) |
| 127 | + } |
| 128 | + |
| 129 | + log.Infof("Channel %v funding multisig P2WSH "+ |
| 130 | + "(pubkey): %x", |
| 131 | + single.FundingOutpoint, msHash) |
| 132 | + |
84 | 133 | // Fix the incorrect descriptor by deriving a default |
85 | 134 | // one and overwriting it in the backup. |
86 | | - log.Infof("The shachain root for channel %s could "+ |
| 135 | + log.Infof("The shachain root for channel %v could "+ |
87 | 136 | "not be derived, must be in old format. "+ |
88 | | - "Fixing...", single.FundingOutpoint.String()) |
| 137 | + "Fixing...", single.FundingOutpoint) |
| 138 | + |
89 | 139 | baseKeyDesc, err := ring.DeriveKey(keychain.KeyLocator{ |
90 | 140 | Family: keychain.KeyFamilyRevocationRoot, |
91 | 141 | Index: 0, |
|
0 commit comments