|
| 1 | +package filterdeposit |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/hex" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/spf13/cobra" |
| 15 | + "github.com/zeta-chain/zetacore/cmd/zetatool/config" |
| 16 | + "github.com/zeta-chain/zetacore/common" |
| 17 | +) |
| 18 | + |
| 19 | +func NewBtcCmd() *cobra.Command { |
| 20 | + return &cobra.Command{ |
| 21 | + Use: "btc", |
| 22 | + Short: "Filter inbound btc deposits", |
| 23 | + RunE: FilterBTCTransactions, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// FilterBTCTransactions is a command that queries the bitcoin explorer for inbound transactions that qualify for |
| 28 | +// cross chain transactions. |
| 29 | +func FilterBTCTransactions(cmd *cobra.Command, _ []string) error { |
| 30 | + configFile, err := cmd.Flags().GetString(config.FlagConfig) |
| 31 | + fmt.Println("config file name: ", configFile) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + btcChainID, err := cmd.Flags().GetString(BTCChainIDFlag) |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + cfg, err := config.GetConfig(configFile) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + fmt.Println("getting tss Address") |
| 44 | + res, err := GetTssAddress(cfg, btcChainID) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + fmt.Println("got tss Address") |
| 49 | + list, err := getHashList(cfg, res.Btc) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + _, err = CheckForCCTX(list, cfg) |
| 55 | + return err |
| 56 | +} |
| 57 | + |
| 58 | +// getHashList is called by FilterBTCTransactions to help query and filter inbound transactions on btc |
| 59 | +func getHashList(cfg *config.Config, tssAddress string) ([]Deposit, error) { |
| 60 | + var list []Deposit |
| 61 | + lastHash := "" |
| 62 | + |
| 63 | + // Setup URL for query |
| 64 | + btcURL, err := url.JoinPath(cfg.BtcExplorerURL, "address", tssAddress, "txs") |
| 65 | + if err != nil { |
| 66 | + return list, err |
| 67 | + } |
| 68 | + |
| 69 | + // This loop will query the bitcoin explorer for transactions associated with the TSS address. Since the api only |
| 70 | + // allows a response of 25 transactions per request, several requests will be required in order to retrieve a |
| 71 | + // complete list. |
| 72 | + for { |
| 73 | + // The Next Query is determined by the last transaction hash provided by the previous response. |
| 74 | + nextQuery := btcURL |
| 75 | + if lastHash != "" { |
| 76 | + nextQuery, err = url.JoinPath(btcURL, "chain", lastHash) |
| 77 | + if err != nil { |
| 78 | + return list, err |
| 79 | + } |
| 80 | + } |
| 81 | + // #nosec G107 url must be variable |
| 82 | + res, getErr := http.Get(nextQuery) |
| 83 | + if getErr != nil { |
| 84 | + return list, getErr |
| 85 | + } |
| 86 | + |
| 87 | + body, readErr := ioutil.ReadAll(res.Body) |
| 88 | + if readErr != nil { |
| 89 | + return list, readErr |
| 90 | + } |
| 91 | + closeErr := res.Body.Close() |
| 92 | + if closeErr != nil { |
| 93 | + return list, closeErr |
| 94 | + } |
| 95 | + |
| 96 | + // NOTE: decoding json from request dynamically is not ideal, however there isn't a detailed, defined data structure |
| 97 | + // provided by blockstream. Will need to create one in the future using following definition: |
| 98 | + // https://github.com/Blockstream/esplora/blob/master/API.md#transaction-format |
| 99 | + var txns []map[string]interface{} |
| 100 | + err := json.Unmarshal(body, &txns) |
| 101 | + if err != nil { |
| 102 | + return list, err |
| 103 | + } |
| 104 | + |
| 105 | + if len(txns) == 0 { |
| 106 | + break |
| 107 | + } |
| 108 | + |
| 109 | + fmt.Println("Length of txns: ", len(txns)) |
| 110 | + |
| 111 | + // The "/address" blockstream api provides a maximum of 25 transactions associated with a given address. This |
| 112 | + // loop will iterate over that list of transactions to determine whether each transaction can be considered |
| 113 | + // a deposit to ZetaChain. |
| 114 | + for _, txn := range txns { |
| 115 | + // Get tx hash of the current transaction |
| 116 | + hash := txn["txid"].(string) |
| 117 | + |
| 118 | + // Read the first output of the transaction and parse the destination address. |
| 119 | + // This address should be the TSS address. |
| 120 | + vout := txn["vout"].([]interface{}) |
| 121 | + vout0 := vout[0].(map[string]interface{}) |
| 122 | + var vout1 map[string]interface{} |
| 123 | + if len(vout) > 1 { |
| 124 | + vout1 = vout[1].(map[string]interface{}) |
| 125 | + } else { |
| 126 | + continue |
| 127 | + } |
| 128 | + _, found := vout0["scriptpubkey"] |
| 129 | + scriptpubkey := "" |
| 130 | + if found { |
| 131 | + scriptpubkey = vout0["scriptpubkey"].(string) |
| 132 | + } |
| 133 | + _, found = vout0["scriptpubkey_address"] |
| 134 | + targetAddr := "" |
| 135 | + if found { |
| 136 | + targetAddr = vout0["scriptpubkey_address"].(string) |
| 137 | + } |
| 138 | + |
| 139 | + //Check if txn is confirmed |
| 140 | + status := txn["status"].(map[string]interface{}) |
| 141 | + confirmed := status["confirmed"].(bool) |
| 142 | + if !confirmed { |
| 143 | + continue |
| 144 | + } |
| 145 | + |
| 146 | + //Filter out deposits less than min base fee |
| 147 | + if vout0["value"].(float64) < 1360 { |
| 148 | + continue |
| 149 | + } |
| 150 | + |
| 151 | + //Check if Deposit is a donation |
| 152 | + scriptpubkey1 := vout1["scriptpubkey"].(string) |
| 153 | + if len(scriptpubkey1) >= 4 && scriptpubkey1[:2] == "6a" { |
| 154 | + memoSize, err := strconv.ParseInt(scriptpubkey1[2:4], 16, 32) |
| 155 | + if err != nil { |
| 156 | + continue |
| 157 | + } |
| 158 | + if int(memoSize) != (len(scriptpubkey1)-4)/2 { |
| 159 | + continue |
| 160 | + } |
| 161 | + memoBytes, err := hex.DecodeString(scriptpubkey1[4:]) |
| 162 | + if err != nil { |
| 163 | + continue |
| 164 | + } |
| 165 | + if bytes.Equal(memoBytes, []byte(common.DonationMessage)) { |
| 166 | + continue |
| 167 | + } |
| 168 | + } else { |
| 169 | + continue |
| 170 | + } |
| 171 | + |
| 172 | + //Make sure Deposit is sent to correct tss address |
| 173 | + if strings.Compare("0014", scriptpubkey[:4]) == 0 && targetAddr == tssAddress { |
| 174 | + entry := Deposit{ |
| 175 | + hash, |
| 176 | + // #nosec G701 parsing json requires float64 type from blockstream |
| 177 | + uint64(vout0["value"].(float64)), |
| 178 | + } |
| 179 | + list = append(list, entry) |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + lastTxn := txns[len(txns)-1] |
| 184 | + lastHash = lastTxn["txid"].(string) |
| 185 | + } |
| 186 | + |
| 187 | + return list, nil |
| 188 | +} |
0 commit comments