mirror of https://github.com/guggero/chantools
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.1 KiB
Go
108 lines
2.1 KiB
Go
5 years ago
|
package chain
|
||
5 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
5 years ago
|
"errors"
|
||
5 years ago
|
"fmt"
|
||
|
"net/http"
|
||
5 years ago
|
"strings"
|
||
5 years ago
|
)
|
||
|
|
||
5 years ago
|
var (
|
||
|
ErrTxNotFound = errors.New("transaction not found")
|
||
|
)
|
||
|
|
||
5 years ago
|
type Api struct {
|
||
|
BaseUrl string
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
type TX struct {
|
||
|
Vin []*Vin `json:"vin"`
|
||
|
Vout []*Vout `json:"vout"`
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
type Vin struct {
|
||
5 years ago
|
Tixid string `json:"txid"`
|
||
|
Vout int `json:"vout"`
|
||
5 years ago
|
Prevout *Vout `json:"prevout"`
|
||
5 years ago
|
Sequence uint32 `json:"sequence"`
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
type Vout struct {
|
||
5 years ago
|
ScriptPubkey string `json:"scriptpubkey"`
|
||
|
ScriptPubkeyAsm string `json:"scriptpubkey_asm"`
|
||
|
ScriptPubkeyType string `json:"scriptpubkey_type"`
|
||
5 years ago
|
ScriptPubkeyAddr string `json:"scriptpubkey_address"`
|
||
|
Value uint64 `json:"value"`
|
||
5 years ago
|
Outspend *Outspend
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
type Outspend struct {
|
||
5 years ago
|
Spent bool `json:"spent"`
|
||
|
Txid string `json:"txid"`
|
||
|
Vin int `json:"vin"`
|
||
5 years ago
|
Status *Status `json:"status"`
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
type Status struct {
|
||
5 years ago
|
Confirmed bool `json:"confirmed"`
|
||
|
BlockHeight int `json:"block_height"`
|
||
|
BlockHash string `json:"block_hash"`
|
||
|
}
|
||
|
|
||
5 years ago
|
func (a *Api) Transaction(txid string) (*TX, error) {
|
||
|
tx := &TX{}
|
||
|
err := Fetch(fmt.Sprintf("%s/tx/%s", a.BaseUrl, txid), tx)
|
||
5 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
for idx, vout := range tx.Vout {
|
||
|
url := fmt.Sprintf(
|
||
5 years ago
|
"%s/tx/%s/outspend/%d", a.BaseUrl, txid, idx,
|
||
5 years ago
|
)
|
||
5 years ago
|
outspend := Outspend{}
|
||
5 years ago
|
err := Fetch(url, &outspend)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
5 years ago
|
vout.Outspend = &outspend
|
||
5 years ago
|
}
|
||
|
return tx, nil
|
||
|
}
|
||
|
|
||
5 years ago
|
func (a *Api) PublishTx(rawTxHex string) (string, error) {
|
||
|
url := fmt.Sprintf("%s/tx", a.BaseUrl)
|
||
5 years ago
|
resp, err := http.Post(url, "text/plain", strings.NewReader(rawTxHex))
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
body := new(bytes.Buffer)
|
||
|
_, err = body.ReadFrom(resp.Body)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return body.String(), nil
|
||
|
}
|
||
|
|
||
5 years ago
|
func Fetch(url string, target interface{}) error {
|
||
|
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
|
||
|
}
|
||
5 years ago
|
err = json.Unmarshal(body.Bytes(), target)
|
||
|
if err != nil {
|
||
|
if string(body.Bytes()) == "Transaction not found" {
|
||
|
return ErrTxNotFound
|
||
|
}
|
||
|
}
|
||
|
return err
|
||
5 years ago
|
}
|