-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
44 lines (37 loc) · 1.01 KB
/
transaction.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
package txpool
import (
"bytes"
"encoding/hex"
"fmt"
"net/http"
"strings"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/pkg/errors"
)
func (c *TxpoolClient) GetRawTransaction(txHash *chainhash.Hash) (*wire.MsgTx, error) {
res, err := c.request(http.MethodGet, fmt.Sprintf("/tx/%s/raw", txHash.String()), nil)
if err != nil {
return nil, err
}
tx := wire.NewMsgTx(wire.TxVersion)
if err := tx.Deserialize(bytes.NewReader(res)); err != nil {
return nil, err
}
return tx, nil
}
func (c *TxpoolClient) BroadcastTx(tx *wire.MsgTx) (*chainhash.Hash, error) {
var buf bytes.Buffer
if err := tx.Serialize(&buf); err != nil {
return nil, err
}
res, err := c.request(http.MethodPost, "/tx", strings.NewReader(hex.EncodeToString(buf.Bytes())))
if err != nil {
return nil, err
}
txHash, err := chainhash.NewHashFromStr(string(res))
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to parse tx hash, %s", string(res)))
}
return txHash, nil
}