-
Notifications
You must be signed in to change notification settings - Fork 13
/
bitIndexClient.go
64 lines (53 loc) · 1.17 KB
/
bitIndexClient.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
package bitcoin
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
// Utxo bitindex comment
type Utxo struct {
TxID string `json:"txid"`
Vout uint32 `json:"vout"`
Height uint32 `json:"height"`
Value uint64 `json:"value"`
}
// UtxoResponse comment
type UtxoResponse struct {
Address string `json:"address"`
Utxos []Utxo `json:"utxos"`
Balance uint64 `json:"balance"`
}
type bitIndexResponseData struct {
Data UtxoResponse `json:"data"`
}
// BitIndex comment
type BitIndex struct {
BaseURL string
}
// NewBitIndexClient returns a new bitIndex client for the given url
func NewBitIndexClient(url string) (*BitIndex, error) {
return &BitIndex{
BaseURL: url,
}, nil
}
// GetUtxos comment
func (b *BitIndex) GetUtxos(addr string) (*UtxoResponse, error) {
bitindexURL := fmt.Sprintf("%s/%s/%s", b.BaseURL, "utxos", addr)
resp, err := http.Get(bitindexURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
bir := bitIndexResponseData{}
err = json.Unmarshal(body, &bir)
if err != nil {
fmt.Printf("error unarshalling body %+v", err)
return nil, err
}
return &bir.Data, nil
}