-
Notifications
You must be signed in to change notification settings - Fork 15
/
gettransaction.go
68 lines (57 loc) · 1.29 KB
/
gettransaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"encoding/hex"
"encoding/json"
"net/http"
"github.com/btcsuite/btcd/chaincfg/chainhash"
)
type UTXOResponse struct {
Amount *int64 `json:"amount"`
Script *string `json:"script"`
}
type TxResponse struct {
TXID string `json:"txid"`
Vout []TxVout `json:"vout"`
}
type TxVout struct {
ScriptPubKey string `json:"scriptPubKey"`
Value int64 `json:"value"`
}
func getTransaction(txid string) (tx TxResponse, err error) {
// try bitcoind first
if bitcoind != nil {
var decodedChainHash chainhash.Hash
if err := chainhash.Decode(&decodedChainHash, txid); err == nil {
if tx, err := bitcoind.GetRawTransaction(&decodedChainHash); err == nil {
outputs := tx.MsgTx().TxOut
vout := make([]TxVout, len(outputs))
for i, out := range outputs {
vout[i] = TxVout{
ScriptPubKey: hex.EncodeToString(out.PkScript),
Value: out.Value,
}
}
return TxResponse{
TXID: txid,
Vout: vout,
}, nil
}
}
}
// then try explorers
for _, endpoint := range esploras(network) {
w, errW := http.Get(endpoint + "/tx/" + txid)
if errW != nil {
err = errW
continue
}
defer w.Body.Close()
errW = json.NewDecoder(w.Body).Decode(&tx)
if errW != nil {
err = errW
continue
}
return tx, nil
}
return
}