forked from VincentDebug/go-ord-tx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btcapi.go
43 lines (39 loc) · 1.14 KB
/
btcapi.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
package btcapi
import (
"fmt"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/pkg/errors"
"io"
"net/http"
)
type UnspentOutput struct {
Outpoint *wire.OutPoint
Output *wire.TxOut
}
type BTCAPIClient interface {
GetRawTransaction(txHash *chainhash.Hash) (*wire.MsgTx, error)
BroadcastTx(tx *wire.MsgTx) (*chainhash.Hash, error)
ListUnspent(address btcutil.Address) ([]*UnspentOutput, error)
GetFeeRate() (*int64, error)
}
func Request(method, baseURL, subPath string, requestBody io.Reader) ([]byte, error) {
url := fmt.Sprintf("%s%s", baseURL, subPath)
req, err := http.NewRequest(method, url, requestBody)
if err != nil {
return nil, errors.Wrap(err, "failed to create request")
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to send request")
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read response body")
}
return body, nil
}