2
0
mirror of https://github.com/guggero/chantools synced 2024-11-18 21:26:23 +00:00
chantools/btc/explorer_api.go

243 lines
5.4 KiB
Go
Raw Normal View History

2020-01-04 22:10:26 +00:00
package btc
2019-11-09 14:41:31 +00:00
import (
"bytes"
"encoding/json"
2019-11-24 11:32:59 +00:00
"errors"
2019-11-09 14:41:31 +00:00
"fmt"
"net/http"
"strconv"
2019-11-24 14:59:24 +00:00
"strings"
2019-11-09 14:41:31 +00:00
)
2019-11-24 11:32:59 +00:00
var (
ErrTxNotFound = errors.New("transaction not found")
)
2020-01-04 22:58:10 +00:00
type ExplorerAPI struct {
BaseURL string
2019-11-09 14:41:31 +00:00
}
2020-01-04 21:36:24 +00:00
type TX struct {
2020-08-30 12:00:19 +00:00
TXID string `json:"txid"`
2020-01-04 21:36:24 +00:00
Vin []*Vin `json:"vin"`
Vout []*Vout `json:"vout"`
2019-11-09 14:41:31 +00:00
}
2020-01-04 21:36:24 +00:00
type Vin struct {
2019-11-24 11:32:59 +00:00
Tixid string `json:"txid"`
Vout int `json:"vout"`
2020-01-04 21:36:24 +00:00
Prevout *Vout `json:"prevout"`
2019-11-24 11:32:59 +00:00
Sequence uint32 `json:"sequence"`
2019-11-09 14:41:31 +00:00
}
2020-01-04 21:36:24 +00:00
type Vout struct {
2019-11-09 14:41:31 +00:00
ScriptPubkey string `json:"scriptpubkey"`
ScriptPubkeyAsm string `json:"scriptpubkey_asm"`
ScriptPubkeyType string `json:"scriptpubkey_type"`
2019-11-24 11:32:59 +00:00
ScriptPubkeyAddr string `json:"scriptpubkey_address"`
Value uint64 `json:"value"`
2020-01-04 21:36:24 +00:00
Outspend *Outspend
2019-11-09 14:41:31 +00:00
}
2020-01-04 21:36:24 +00:00
type Outspend struct {
2019-11-09 14:41:31 +00:00
Spent bool `json:"spent"`
Txid string `json:"txid"`
Vin int `json:"vin"`
2020-01-04 21:36:24 +00:00
Status *Status `json:"status"`
2019-11-09 14:41:31 +00:00
}
2020-01-04 21:36:24 +00:00
type Status struct {
2019-11-09 14:41:31 +00:00
Confirmed bool `json:"confirmed"`
BlockHeight int `json:"block_height"`
BlockHash string `json:"block_hash"`
}
type Stats struct {
FundedTXOCount uint32 `json:"funded_txo_count"`
FundedTXOSum uint64 `json:"funded_txo_sum"`
SpentTXOCount uint32 `json:"spent_txo_count"`
SpentTXOSum uint64 `json:"spent_txo_sum"`
TXCount uint32 `json:"tx_count"`
}
type AddressStats struct {
Address string `json:"address"`
ChainStats *Stats `json:"chain_stats"`
MempoolStats *Stats `json:"mempool_stats"`
}
2020-01-04 22:58:10 +00:00
func (a *ExplorerAPI) Transaction(txid string) (*TX, error) {
2020-01-04 21:36:24 +00:00
tx := &TX{}
2020-01-04 22:58:10 +00:00
err := fetchJSON(fmt.Sprintf("%s/tx/%s", a.BaseURL, txid), tx)
2019-11-09 14:41:31 +00:00
if err != nil {
return nil, err
}
for idx, vout := range tx.Vout {
url := fmt.Sprintf(
2020-01-04 22:58:10 +00:00
"%s/tx/%s/outspend/%d", a.BaseURL, txid, idx,
2019-11-09 14:41:31 +00:00
)
2020-01-04 21:36:24 +00:00
outspend := Outspend{}
2020-01-04 22:10:26 +00:00
err := fetchJSON(url, &outspend)
2019-11-09 14:41:31 +00:00
if err != nil {
return nil, err
}
2020-01-04 21:36:24 +00:00
vout.Outspend = &outspend
2019-11-09 14:41:31 +00:00
}
return tx, nil
}
2020-08-30 12:00:19 +00:00
func (a *ExplorerAPI) Outpoint(addr string) (*TX, int, error) {
var txs []*TX
2023-02-25 10:39:52 +00:00
err := fetchJSON(
fmt.Sprintf("%s/address/%s/txs", a.BaseURL, addr), &txs,
)
2020-08-30 12:00:19 +00:00
if err != nil {
return nil, 0, err
}
for _, tx := range txs {
for idx, vout := range tx.Vout {
if vout.ScriptPubkeyAddr == addr {
return tx, idx, nil
}
}
}
return nil, 0, errors.New("no tx found")
2020-08-30 12:00:19 +00:00
}
2023-02-25 10:39:52 +00:00
func (a *ExplorerAPI) Spends(addr string) ([]*TX, error) {
var txs []*TX
err := fetchJSON(
fmt.Sprintf("%s/address/%s/txs", a.BaseURL, addr), &txs,
)
if err != nil {
return nil, err
}
var spends []*TX
for txIndex := range txs {
tx := txs[txIndex]
for _, vin := range tx.Vin {
if vin.Prevout.ScriptPubkeyAddr == addr {
spends = append(spends, tx)
}
}
}
return spends, nil
}
func (a *ExplorerAPI) Unspent(addr string) ([]*Vout, error) {
var (
stats = &AddressStats{}
outputs []*Vout
txs []*TX
err error
)
err = fetchJSON(fmt.Sprintf("%s/address/%s", a.BaseURL, addr), &stats)
if err != nil {
return nil, err
}
confirmedUnspent := stats.ChainStats.FundedTXOSum -
stats.ChainStats.SpentTXOSum
unconfirmedUnspent := stats.MempoolStats.FundedTXOSum -
stats.MempoolStats.SpentTXOSum
if confirmedUnspent+unconfirmedUnspent == 0 {
return nil, nil
}
err = fetchJSON(fmt.Sprintf("%s/address/%s/txs", a.BaseURL, addr), &txs)
if err != nil {
return nil, err
}
for _, tx := range txs {
for voutIdx, vout := range tx.Vout {
if vout.ScriptPubkeyAddr == addr {
vout.Outspend = &Outspend{
Txid: tx.TXID,
Vin: voutIdx,
}
outputs = append(outputs, vout)
}
}
}
return outputs, nil
}
func (a *ExplorerAPI) Address(outpoint string) (string, error) {
parts := strings.Split(outpoint, ":")
if len(parts) != 2 {
return "", fmt.Errorf("invalid outpoint: %v", outpoint)
}
tx, err := a.Transaction(parts[0])
if err != nil {
return "", err
}
vout, err := strconv.Atoi(parts[1])
if err != nil {
return "", err
}
if len(tx.Vout) <= vout {
return "", fmt.Errorf("invalid output index: %d", vout)
}
return tx.Vout[vout].ScriptPubkeyAddr, nil
}
2020-01-04 22:58:10 +00:00
func (a *ExplorerAPI) PublishTx(rawTxHex string) (string, error) {
url := a.BaseURL + "/tx"
2019-11-24 14:59:24 +00:00
resp, err := http.Post(url, "text/plain", strings.NewReader(rawTxHex))
if err != nil {
2023-12-29 08:18:27 +00:00
return "", fmt.Errorf("error posting data to API '%s', "+
"server might be experiencing temporary issues, try "+
"again later; error details: %w", url, err)
2019-11-24 14:59:24 +00:00
}
2020-01-04 22:58:10 +00:00
defer resp.Body.Close()
2019-11-24 14:59:24 +00:00
body := new(bytes.Buffer)
_, err = body.ReadFrom(resp.Body)
if err != nil {
2023-12-29 08:18:27 +00:00
return "", fmt.Errorf("error fetching data from API '%s', "+
"server might be experiencing temporary issues, try "+
"again later; error details: %w", url, err)
2019-11-24 14:59:24 +00:00
}
return body.String(), nil
}
2020-01-04 22:10:26 +00:00
func fetchJSON(url string, target interface{}) error {
2019-11-09 14:41:31 +00:00
resp, err := http.Get(url)
if err != nil {
2023-12-29 08:18:27 +00:00
return fmt.Errorf("error fetching data from API '%s', "+
"server might be experiencing temporary issues, try "+
"again later; error details: %w", url, err)
2019-11-09 14:41:31 +00:00
}
defer resp.Body.Close()
body := new(bytes.Buffer)
_, err = body.ReadFrom(resp.Body)
if err != nil {
2023-12-29 08:18:27 +00:00
return fmt.Errorf("error fetching data from API '%s', "+
"server might be experiencing temporary issues, try "+
"again later; error details: %w", url, err)
2019-11-09 14:41:31 +00:00
}
2019-11-24 11:32:59 +00:00
err = json.Unmarshal(body.Bytes(), target)
if err != nil {
2020-01-04 22:58:10 +00:00
if body.String() == "Transaction not found" {
2019-11-24 11:32:59 +00:00
return ErrTxNotFound
}
2023-12-29 08:18:27 +00:00
return fmt.Errorf("error decoding data from API '%s', "+
"server might be experiencing temporary issues, try "+
"again later; error details: %w", url, err)
2019-11-24 11:32:59 +00:00
}
2023-12-29 08:18:27 +00:00
return nil
2019-11-09 14:41:31 +00:00
}