|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "regexp" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/btcsuite/btcd/btcec" |
| 11 | + "github.com/gogo/protobuf/jsonpb" |
| 12 | + "github.com/guggero/chantools/btc" |
| 13 | + "github.com/guggero/chantools/lnd" |
| 14 | + "github.com/lightningnetwork/lnd/lnrpc" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + patternRegistration = regexp.MustCompile( |
| 20 | + "(?m)(?s)ID: ([0-9a-f]{66})\nContact: (.*?)\n" + |
| 21 | + "Time: ") |
| 22 | +) |
| 23 | + |
| 24 | +type nodeInfo struct { |
| 25 | + PubKey string `json:"identity_pubkey"` |
| 26 | + Contact string `json:"contact"` |
| 27 | + PayoutAddr string `json:"payout_addr,omitempty"` |
| 28 | + MultisigKeys []string `json:"multisig_keys,omitempty"` |
| 29 | +} |
| 30 | + |
| 31 | +type channel struct { |
| 32 | + ChannelID string `json:"short_channel_id"` |
| 33 | + ChanPoint string `json:"chan_point"` |
| 34 | + Address string `json:"address"` |
| 35 | + Capacity int64 `json:"capacity"` |
| 36 | + txid string |
| 37 | + vout uint32 |
| 38 | + ourKeyIndex uint32 |
| 39 | + ourKey *btcec.PublicKey |
| 40 | + theirKey *btcec.PublicKey |
| 41 | + witnessScript []byte |
| 42 | +} |
| 43 | + |
| 44 | +type match struct { |
| 45 | + Node1 *nodeInfo `json:"node1"` |
| 46 | + Node2 *nodeInfo `json:"node2"` |
| 47 | + Channels []*channel `json:"channels"` |
| 48 | +} |
| 49 | + |
| 50 | +type zombieRecoveryFindMatchesCommand struct { |
| 51 | + APIURL string |
| 52 | + Registrations string |
| 53 | + ChannelGraph string |
| 54 | + |
| 55 | + cmd *cobra.Command |
| 56 | +} |
| 57 | + |
| 58 | +func newZombieRecoveryFindMatchesCommand() *cobra.Command { |
| 59 | + cc := &zombieRecoveryFindMatchesCommand{} |
| 60 | + cc.cmd = &cobra.Command{ |
| 61 | + Use: "findmatches", |
| 62 | + Short: "[0/3] Match maker only: Find matches between " + |
| 63 | + "registered nodes", |
| 64 | + Long: `Match maker only: Runs through all the nodes that have |
| 65 | +registered their ID on https://www.node-recovery.com and checks whether there |
| 66 | +are any matches of channels between them by looking at the whole channel graph. |
| 67 | +
|
| 68 | +This command will be run by guggero and the result will be sent to the |
| 69 | +registered nodes.`, |
| 70 | + Example: `chantools zombierecovery findmatches \ |
| 71 | + --registrations data.txt \ |
| 72 | + --channel_graph lncli_describegraph.json`, |
| 73 | + RunE: cc.Execute, |
| 74 | + } |
| 75 | + |
| 76 | + cc.cmd.Flags().StringVar( |
| 77 | + &cc.APIURL, "apiurl", defaultAPIURL, "API URL to use (must "+ |
| 78 | + "be esplora compatible)", |
| 79 | + ) |
| 80 | + cc.cmd.Flags().StringVar( |
| 81 | + &cc.Registrations, "registrations", "", "the raw data.txt "+ |
| 82 | + "where the registrations are stored in", |
| 83 | + ) |
| 84 | + cc.cmd.Flags().StringVar( |
| 85 | + &cc.ChannelGraph, "channel_graph", "", "the full LN channel "+ |
| 86 | + "graph in the JSON format that the "+ |
| 87 | + "'lncli describegraph' returns", |
| 88 | + ) |
| 89 | + |
| 90 | + return cc.cmd |
| 91 | +} |
| 92 | + |
| 93 | +func (c *zombieRecoveryFindMatchesCommand) Execute(_ *cobra.Command, |
| 94 | + _ []string) error { |
| 95 | + |
| 96 | + api := &btc.ExplorerAPI{BaseURL: c.APIURL} |
| 97 | + |
| 98 | + logFileBytes, err := ioutil.ReadFile(c.Registrations) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("error reading registrations file %s: %v", |
| 101 | + c.Registrations, err) |
| 102 | + } |
| 103 | + |
| 104 | + allMatches := patternRegistration.FindAllStringSubmatch( |
| 105 | + string(logFileBytes), -1, |
| 106 | + ) |
| 107 | + registrations := make(map[string]string, len(allMatches)) |
| 108 | + for _, groups := range allMatches { |
| 109 | + if _, err := pubKeyFromHex(groups[1]); err != nil { |
| 110 | + return fmt.Errorf("error parsing node ID: %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + registrations[groups[1]] = groups[2] |
| 114 | + |
| 115 | + log.Infof("%s: %s", groups[1], groups[2]) |
| 116 | + } |
| 117 | + |
| 118 | + graphBytes, err := ioutil.ReadFile(c.ChannelGraph) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("error reading graph JSON file %s: "+ |
| 121 | + "%v", c.ChannelGraph, err) |
| 122 | + } |
| 123 | + graph := &lnrpc.ChannelGraph{} |
| 124 | + err = jsonpb.UnmarshalString(string(graphBytes), graph) |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("error parsing graph JSON: %v", err) |
| 127 | + } |
| 128 | + |
| 129 | + // Loop through all nodes now. |
| 130 | + matches := make(map[string]map[string]*match) |
| 131 | + for node1, contact1 := range registrations { |
| 132 | + matches[node1] = make(map[string]*match) |
| 133 | + for node2, contact2 := range registrations { |
| 134 | + if node1 == node2 { |
| 135 | + continue |
| 136 | + } |
| 137 | + |
| 138 | + // We've already looked at this pair. |
| 139 | + if matches[node2][node1] != nil { |
| 140 | + continue |
| 141 | + } |
| 142 | + |
| 143 | + edges := lnd.FindCommonEdges(graph, node1, node2) |
| 144 | + if len(edges) > 0 { |
| 145 | + matches[node1][node2] = &match{ |
| 146 | + Node1: &nodeInfo{ |
| 147 | + PubKey: node1, |
| 148 | + Contact: contact1, |
| 149 | + }, |
| 150 | + Node2: &nodeInfo{ |
| 151 | + PubKey: node2, |
| 152 | + Contact: contact2, |
| 153 | + }, |
| 154 | + Channels: make([]*channel, len(edges)), |
| 155 | + } |
| 156 | + |
| 157 | + for idx, edge := range edges { |
| 158 | + cid := fmt.Sprintf("%d", edge.ChannelId) |
| 159 | + c := &channel{ |
| 160 | + ChannelID: cid, |
| 161 | + ChanPoint: edge.ChanPoint, |
| 162 | + Capacity: edge.Capacity, |
| 163 | + } |
| 164 | + |
| 165 | + addr, err := api.Address(c.ChanPoint) |
| 166 | + if err == nil { |
| 167 | + c.Address = addr |
| 168 | + } |
| 169 | + |
| 170 | + matches[node1][node2].Channels[idx] = c |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // Write the matches to files. |
| 177 | + for node1, node1map := range matches { |
| 178 | + for node2, match := range node1map { |
| 179 | + if match == nil { |
| 180 | + continue |
| 181 | + } |
| 182 | + |
| 183 | + matchBytes, err := json.MarshalIndent(match, "", " ") |
| 184 | + if err != nil { |
| 185 | + return err |
| 186 | + } |
| 187 | + |
| 188 | + fileName := fmt.Sprintf("results/match-%s-%s-%s.json", |
| 189 | + time.Now().Format("2006-01-02"), |
| 190 | + node1, node2) |
| 191 | + log.Infof("Writing result to %s", fileName) |
| 192 | + err = ioutil.WriteFile(fileName, matchBytes, 0644) |
| 193 | + if err != nil { |
| 194 | + return err |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + return nil |
| 200 | +} |
0 commit comments