2
0
mirror of https://github.com/guggero/chantools synced 2024-11-15 06:12:49 +00:00
chantools/btc/explorer_api.go

127 lines
2.6 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"
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"`
}
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
err := fetchJSON(fmt.Sprintf("%s/address/%s/txs", a.BaseURL, addr), &txs)
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, fmt.Errorf("no tx found")
}
2020-01-04 22:58:10 +00:00
func (a *ExplorerAPI) PublishTx(rawTxHex string) (string, error) {
url := fmt.Sprintf("%s/tx", a.BaseURL)
2019-11-24 14:59:24 +00:00
resp, err := http.Post(url, "text/plain", strings.NewReader(rawTxHex))
if err != nil {
return "", err
}
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 {
return "", err
}
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 {
return err
}
defer resp.Body.Close()
body := new(bytes.Buffer)
_, err = body.ReadFrom(resp.Body)
if err != nil {
return err
}
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
}
}
return err
2019-11-09 14:41:31 +00:00
}