|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/btcsuite/btcutil/hdkeychain" |
| 8 | + "github.com/guggero/chantools/lnd" |
| 9 | + "github.com/lightningnetwork/lnd/input" |
| 10 | + "github.com/lightningnetwork/lnd/keychain" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + MaxChannelLookup = 5000 |
| 15 | +) |
| 16 | + |
| 17 | +type rescueFundingCommand struct { |
| 18 | + RootKey string `long:"rootkey" description:"BIP32 HD root (m/) key to derive the key for our node from."` |
| 19 | + OtherNodePub string `long:"othernodepub" description:"The extended public key (xpub) of the other node's multisig branch (m/1017'/<coin_type>'/0'/0)."` |
| 20 | + FundingAddr string `long:"fundingaddr" description:"The bech32 script address of the funding output where the coins to be spent are locked in."` |
| 21 | + FundingOutpoint string `long:"fundingoutpoint" description:"The funding transaction outpoint (<txid>:<txindex>)."` |
| 22 | + FundingAmount int64 `long:"fundingamount" description:"The exact amount in satoshis that is locked in the funding output."` |
| 23 | + SweepAddr string `long:"sweepaddr" description:"The address to sweep the rescued funds to."` |
| 24 | + SatPerByte int64 `long:"satperbyte" description:"The fee rate to use in satoshis/vByte."` |
| 25 | +} |
| 26 | + |
| 27 | +func (c *rescueFundingCommand) Execute(_ []string) error { |
| 28 | + setupChainParams(cfg) |
| 29 | + |
| 30 | + var ( |
| 31 | + extendedKey *hdkeychain.ExtendedKey |
| 32 | + otherPub *hdkeychain.ExtendedKey |
| 33 | + err error |
| 34 | + ) |
| 35 | + |
| 36 | + // Check that root key is valid or fall back to console input. |
| 37 | + switch { |
| 38 | + case c.RootKey != "": |
| 39 | + extendedKey, err = hdkeychain.NewKeyFromString(c.RootKey) |
| 40 | + |
| 41 | + default: |
| 42 | + extendedKey, _, err = rootKeyFromConsole() |
| 43 | + } |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("error reading root key: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + // Read other node's xpub. |
| 49 | + otherPub, err = hdkeychain.NewKeyFromString(c.OtherNodePub) |
| 50 | + if err != nil { |
| 51 | + return fmt.Errorf("error parsing other node's xpub: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + // Decode target funding address. |
| 55 | + hash, isScript, err := lnd.DecodeAddressHash(c.FundingAddr, chainParams) |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("error decoding funding address: %v", err) |
| 58 | + } |
| 59 | + if !isScript { |
| 60 | + return fmt.Errorf("funding address must be a P2WSH address") |
| 61 | + } |
| 62 | + |
| 63 | + return rescueFunding(extendedKey, otherPub, hash) |
| 64 | +} |
| 65 | + |
| 66 | +func rescueFunding(localNodeKey *hdkeychain.ExtendedKey, |
| 67 | + otherNodekey *hdkeychain.ExtendedKey, scriptHash []byte) error { |
| 68 | + |
| 69 | + // First, we need to derive the correct branch from the local root key. |
| 70 | + localMultisig, err := lnd.DeriveChildren(localNodeKey, []uint32{ |
| 71 | + lnd.HardenedKeyStart + uint32(keychain.BIP0043Purpose), |
| 72 | + lnd.HardenedKeyStart + chainParams.HDCoinType, |
| 73 | + lnd.HardenedKeyStart + uint32(keychain.KeyFamilyMultiSig), |
| 74 | + 0, |
| 75 | + }) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("could not derive local multisig key: %v", |
| 78 | + err) |
| 79 | + } |
| 80 | + |
| 81 | + log.Infof("Looking for matching multisig keys, this will take a while") |
| 82 | + localIndex, otherIndex, script, err := findMatchingIndices( |
| 83 | + localMultisig, otherNodekey, scriptHash, |
| 84 | + ) |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("could not derive keys: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + log.Infof("Found local key with index %d and other key with index %d "+ |
| 90 | + "for witness script %x", localIndex, otherIndex, script) |
| 91 | + |
| 92 | + // TODO(guggero): |
| 93 | + // * craft PSBT with input, sweep output and partial signature |
| 94 | + // * do fee estimation based on full amount |
| 95 | + // * create `signpsbt` command for the other node operator |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func findMatchingIndices(localNodeKey *hdkeychain.ExtendedKey, |
| 100 | + otherNodekey *hdkeychain.ExtendedKey, scriptHash []byte) (uint32, |
| 101 | + uint32, []byte, error) { |
| 102 | + |
| 103 | + // Loop through both the local and the remote indices of the branches up |
| 104 | + // to MaxChannelLookup. |
| 105 | + for local := uint32(0); local < MaxChannelLookup; local++ { |
| 106 | + for other := uint32(0); other < MaxChannelLookup; other++ { |
| 107 | + localKey, err := localNodeKey.Child(local) |
| 108 | + if err != nil { |
| 109 | + return 0, 0, nil, fmt.Errorf("error "+ |
| 110 | + "deriving local key: %v", err) |
| 111 | + } |
| 112 | + localPub, err := localKey.ECPubKey() |
| 113 | + if err != nil { |
| 114 | + return 0, 0, nil, fmt.Errorf("error "+ |
| 115 | + "deriving local pubkey: %v", err) |
| 116 | + } |
| 117 | + otherKey, err := otherNodekey.Child(other) |
| 118 | + if err != nil { |
| 119 | + return 0, 0, nil, fmt.Errorf("error "+ |
| 120 | + "deriving other key: %v", err) |
| 121 | + } |
| 122 | + otherPub, err := otherKey.ECPubKey() |
| 123 | + if err != nil { |
| 124 | + return 0, 0, nil, fmt.Errorf("error "+ |
| 125 | + "deriving other pubkey: %v", err) |
| 126 | + } |
| 127 | + script, out, err := input.GenFundingPkScript( |
| 128 | + localPub.SerializeCompressed(), |
| 129 | + otherPub.SerializeCompressed(), 123, |
| 130 | + ) |
| 131 | + if err != nil { |
| 132 | + return 0, 0, nil, fmt.Errorf("error "+ |
| 133 | + "generating funding script: %v", err) |
| 134 | + } |
| 135 | + if bytes.Contains(out.PkScript, scriptHash) { |
| 136 | + return local, other, script, nil |
| 137 | + } |
| 138 | + } |
| 139 | + if local > 0 && local%100 == 0 { |
| 140 | + log.Infof("Checked %d of %d local keys", local, |
| 141 | + MaxChannelLookup) |
| 142 | + } |
| 143 | + } |
| 144 | + return 0, 0, nil, fmt.Errorf("no matching pubkeys found") |
| 145 | +} |
0 commit comments