-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtx.go
67 lines (57 loc) · 2.8 KB
/
tx.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
package bn
import (
"context"
imodels "github.com/libsv/go-bn/internal/models"
"github.com/libsv/go-bn/models"
"github.com/libsv/go-bt/v2"
)
// TransactionClient interfaces interaction with the transaction sub commands on a bitcoin node.
type TransactionClient interface {
CreateRawTransaction(ctx context.Context, utxos bt.UTXOs, params models.ParamsCreateRawTransaction) (*bt.Tx, error)
FundRawTransaction(ctx context.Context, tx *bt.Tx,
opts *models.OptsFundRawTransaction) (*models.FundRawTransaction, error)
RawTransaction(ctx context.Context, txID string) (*bt.Tx, error)
SignRawTransaction(ctx context.Context, tx *bt.Tx,
opts *models.OptsSignRawTransaction) (*models.SignedRawTransaction, error)
SendRawTransaction(ctx context.Context, tx *bt.Tx, opts *models.OptsSendRawTransaction) (string, error)
SendRawTransactions(ctx context.Context,
params ...models.ParamsSendRawTransactions) (*models.SendRawTransactionsResponse, error)
}
// NewTransactionClient returns a client only capable of interfacing with the transaction sub commands
// on a bitcoin node.
func NewTransactionClient(oo ...BitcoinClientOptFunc) TransactionClient {
return NewNodeClient(oo...)
}
func (c *client) CreateRawTransaction(ctx context.Context, utxos bt.UTXOs,
params models.ParamsCreateRawTransaction) (*bt.Tx, error) {
params.SetIsMainnet(c.isMainnet)
var resp string
if err := c.rpc.Do(ctx, "createrawtransaction", &resp, c.argsFor(¶ms, utxos.NodeJSON())...); err != nil {
return nil, err
}
return bt.NewTxFromString(resp)
}
func (c *client) FundRawTransaction(ctx context.Context, tx *bt.Tx,
opts *models.OptsFundRawTransaction) (*models.FundRawTransaction, error) {
resp := imodels.InternalFundRawTransaction{FundRawTransaction: &models.FundRawTransaction{}}
return resp.FundRawTransaction, c.rpc.Do(ctx, "fundrawtransaction", &resp, c.argsFor(opts, tx.String())...)
}
func (c *client) RawTransaction(ctx context.Context, txID string) (*bt.Tx, error) {
var resp bt.Tx
return &resp, c.rpc.Do(ctx, "getrawtransaction", &resp, txID, true)
}
func (c *client) SignRawTransaction(ctx context.Context, tx *bt.Tx,
opts *models.OptsSignRawTransaction) (*models.SignedRawTransaction, error) {
var resp imodels.InternalSignRawTransaction
return resp.SignedRawTransaction, c.rpc.Do(ctx, "signrawtransaction", &resp, c.argsFor(opts, tx.String())...)
}
func (c *client) SendRawTransaction(ctx context.Context, tx *bt.Tx,
opts *models.OptsSendRawTransaction) (string, error) {
var resp string
return resp, c.rpc.Do(ctx, "sendrawtransaction", &resp, c.argsFor(opts, tx.String())...)
}
func (c *client) SendRawTransactions(ctx context.Context,
params ...models.ParamsSendRawTransactions) (*models.SendRawTransactionsResponse, error) {
var resp models.SendRawTransactionsResponse
return &resp, c.rpc.Do(ctx, "sendrawtransactions", &resp, params)
}