|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/hex" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/lightninglabs/chantools/btc" |
| 12 | + "github.com/lightninglabs/chantools/lnd" |
| 13 | + "github.com/lightninglabs/chantools/scbforceclose" |
| 14 | + "github.com/lightningnetwork/lnd/chanbackup" |
| 15 | + "github.com/lightningnetwork/lnd/input" |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +type scbForceCloseCommand struct { |
| 20 | + APIURL string |
| 21 | + Publish bool |
| 22 | + |
| 23 | + // channel.backup. |
| 24 | + SingleBackup string |
| 25 | + SingleFile string |
| 26 | + MultiBackup string |
| 27 | + MultiFile string |
| 28 | + |
| 29 | + rootKey *rootKey |
| 30 | + cmd *cobra.Command |
| 31 | +} |
| 32 | + |
| 33 | +func newScbForceCloseCommand() *cobra.Command { |
| 34 | + cc := &scbForceCloseCommand{} |
| 35 | + cc.cmd = &cobra.Command{ |
| 36 | + Use: "scbforceclose", |
| 37 | + Short: "Force-close the last state that is in the SCB " + |
| 38 | + "provided", |
| 39 | + Long: forceCloseWarning, |
| 40 | + Example: `chantools scbforceclose --multi_file channel.backup`, |
| 41 | + RunE: cc.Execute, |
| 42 | + } |
| 43 | + cc.cmd.Flags().StringVar( |
| 44 | + &cc.APIURL, "apiurl", defaultAPIURL, "API URL to use (must "+ |
| 45 | + "be esplora compatible)", |
| 46 | + ) |
| 47 | + |
| 48 | + cc.cmd.Flags().StringVar( |
| 49 | + &cc.SingleBackup, "single_backup", "", "a hex encoded single "+ |
| 50 | + "channel backup obtained from exportchanbackup for "+ |
| 51 | + "force-closing channels", |
| 52 | + ) |
| 53 | + cc.cmd.Flags().StringVar( |
| 54 | + &cc.MultiBackup, "multi_backup", "", "a hex encoded "+ |
| 55 | + "multi-channel backup obtained from exportchanbackup "+ |
| 56 | + "for force-closing channels", |
| 57 | + ) |
| 58 | + cc.cmd.Flags().StringVar( |
| 59 | + &cc.SingleFile, "single_file", "", "the path to a "+ |
| 60 | + "single-channel backup file", |
| 61 | + ) |
| 62 | + cc.cmd.Flags().StringVar( |
| 63 | + &cc.MultiFile, "multi_file", "", "the path to a "+ |
| 64 | + "single-channel backup file (channel.backup)", |
| 65 | + ) |
| 66 | + |
| 67 | + cc.cmd.Flags().BoolVar( |
| 68 | + &cc.Publish, "publish", false, "publish force-closing TX to "+ |
| 69 | + "the chain API instead of just printing the TX", |
| 70 | + ) |
| 71 | + |
| 72 | + cc.rootKey = newRootKey(cc.cmd, "decrypting the backup and signing tx") |
| 73 | + |
| 74 | + return cc.cmd |
| 75 | +} |
| 76 | + |
| 77 | +func (c *scbForceCloseCommand) Execute(_ *cobra.Command, _ []string) error { |
| 78 | + extendedKey, err := c.rootKey.read() |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("error reading root key: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + api := &btc.ExplorerAPI{BaseURL: c.APIURL} |
| 84 | + |
| 85 | + keyRing := &lnd.HDKeyRing{ |
| 86 | + ExtendedKey: extendedKey, |
| 87 | + ChainParams: chainParams, |
| 88 | + } |
| 89 | + |
| 90 | + signer := &lnd.Signer{ |
| 91 | + ExtendedKey: extendedKey, |
| 92 | + ChainParams: chainParams, |
| 93 | + } |
| 94 | + signer.MusigSessionManager = input.NewMusigSessionManager( |
| 95 | + signer.FetchPrivateKey, |
| 96 | + ) |
| 97 | + |
| 98 | + var backups []chanbackup.Single |
| 99 | + if c.SingleBackup != "" || c.SingleFile != "" { |
| 100 | + if c.SingleBackup != "" && c.SingleFile != "" { |
| 101 | + return errors.New("must not pass --single_backup and " + |
| 102 | + "--single_file together") |
| 103 | + } |
| 104 | + var singleBackupBytes []byte |
| 105 | + if c.SingleBackup != "" { |
| 106 | + singleBackupBytes, err = hex.DecodeString( |
| 107 | + c.SingleBackup, |
| 108 | + ) |
| 109 | + } else if c.SingleFile != "" { |
| 110 | + singleBackupBytes, err = os.ReadFile(c.SingleFile) |
| 111 | + } |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("failed to get single backup: %w", |
| 114 | + err) |
| 115 | + } |
| 116 | + var s chanbackup.Single |
| 117 | + r := bytes.NewReader(singleBackupBytes) |
| 118 | + if err := s.UnpackFromReader(r, keyRing); err != nil { |
| 119 | + return fmt.Errorf("failed to unpack single backup: %w", |
| 120 | + err) |
| 121 | + } |
| 122 | + backups = append(backups, s) |
| 123 | + } |
| 124 | + if c.MultiBackup != "" || c.MultiFile != "" { |
| 125 | + if len(backups) != 0 { |
| 126 | + return errors.New("must not pass single and multi " + |
| 127 | + "backups together") |
| 128 | + } |
| 129 | + if c.MultiBackup != "" && c.MultiFile != "" { |
| 130 | + return errors.New("must not pass --multi_backup and " + |
| 131 | + "--multi_file together") |
| 132 | + } |
| 133 | + var multiBackupBytes []byte |
| 134 | + if c.MultiBackup != "" { |
| 135 | + multiBackupBytes, err = hex.DecodeString(c.MultiBackup) |
| 136 | + } else if c.MultiFile != "" { |
| 137 | + multiBackupBytes, err = os.ReadFile(c.MultiFile) |
| 138 | + } |
| 139 | + if err != nil { |
| 140 | + return fmt.Errorf("failed to get multi backup: %w", err) |
| 141 | + } |
| 142 | + var m chanbackup.Multi |
| 143 | + r := bytes.NewReader(multiBackupBytes) |
| 144 | + if err := m.UnpackFromReader(r, keyRing); err != nil { |
| 145 | + return fmt.Errorf("failed to unpack multi backup: %w", |
| 146 | + err) |
| 147 | + } |
| 148 | + backups = append(backups, m.StaticBackups...) |
| 149 | + } |
| 150 | + |
| 151 | + backupsWithInputs := make([]chanbackup.Single, 0, len(backups)) |
| 152 | + for _, s := range backups { |
| 153 | + if s.CloseTxInputs.IsSome() { |
| 154 | + backupsWithInputs = append(backupsWithInputs, s) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + fmt.Println() |
| 159 | + fmt.Printf("Found %d channel backups, %d of them have close tx.\n", |
| 160 | + len(backups), len(backupsWithInputs)) |
| 161 | + |
| 162 | + if len(backupsWithInputs) == 0 { |
| 163 | + fmt.Println("No channel backups that can be used for force " + |
| 164 | + "close.") |
| 165 | + return nil |
| 166 | + } |
| 167 | + |
| 168 | + fmt.Println() |
| 169 | + fmt.Println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@") |
| 170 | + fmt.Println(strings.TrimSpace(forceCloseWarning)) |
| 171 | + fmt.Println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@") |
| 172 | + fmt.Println() |
| 173 | + |
| 174 | + fmt.Printf("Type YES to proceed: ") |
| 175 | + var userInput string |
| 176 | + if _, err := fmt.Scan(&userInput); err != nil { |
| 177 | + return errors.New("failed to read user input") |
| 178 | + } |
| 179 | + if strings.TrimSpace(userInput) != "YES" { |
| 180 | + return errors.New("canceled by user, must type uppercase 'YES'") |
| 181 | + } |
| 182 | + |
| 183 | + if c.Publish { |
| 184 | + fmt.Println("Signed transactions will be broadcasted " + |
| 185 | + "automatically.") |
| 186 | + fmt.Printf("Type YES again to proceed: ") |
| 187 | + if _, err := fmt.Scan(&userInput); err != nil { |
| 188 | + return errors.New("failed to read user input") |
| 189 | + } |
| 190 | + if strings.TrimSpace(userInput) != "YES" { |
| 191 | + return errors.New("canceled by user, must type " + |
| 192 | + "uppercase 'YES'") |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + for _, s := range backupsWithInputs { |
| 197 | + signedTx, err := scbforceclose.SignCloseTx( |
| 198 | + s, keyRing, signer, signer, |
| 199 | + ) |
| 200 | + if err != nil { |
| 201 | + return fmt.Errorf("signCloseTx failed for %s: %w", |
| 202 | + s.FundingOutpoint, err) |
| 203 | + } |
| 204 | + var buf bytes.Buffer |
| 205 | + if err := signedTx.Serialize(&buf); err != nil { |
| 206 | + return fmt.Errorf("failed to serialize signed %s: %w", |
| 207 | + s.FundingOutpoint, err) |
| 208 | + } |
| 209 | + txHex := hex.EncodeToString(buf.Bytes()) |
| 210 | + fmt.Println("Channel point:", s.FundingOutpoint) |
| 211 | + fmt.Println("Raw transaction hex:", txHex) |
| 212 | + fmt.Println() |
| 213 | + |
| 214 | + // Publish TX. |
| 215 | + if c.Publish { |
| 216 | + response, err := api.PublishTx(txHex) |
| 217 | + if err != nil { |
| 218 | + return err |
| 219 | + } |
| 220 | + log.Infof("Published TX %s, response: %s", |
| 221 | + signedTx.TxHash(), response) |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + return nil |
| 226 | +} |
0 commit comments