-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx.go
240 lines (178 loc) · 8.31 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package rpcclient
import (
"github.com/omarhachach/rpcclient-core/types"
)
// GetTxOut returns details about an unspent transaction output.
func (c *Client) GetTxOut(txid string, vout int, includeMempool bool) (*types.TransactionOut, error) {
var txout *types.TransactionOut
return txout, c.SendReq("gettxout", &txout, txid, vout, includeMempool)
}
// GetTxOutProof returns a hex-encoded proof that the transaction was included in a block.
// Read RPC docs for a note on reliability.
func (c *Client) GetTxOutProof(txidsFilter []string) (string, error) {
var proof string
return proof, c.SendReq("gettxoutproof", &proof, txidsFilter)
}
// GetTxOutProofInBlock returns a hex-encoded proof that the transaction was included in the block.
// Read RPC docs for a note on reliability.
func (c *Client) GetTxOutProofInBlock(txidsFilter []string, blockhash string) (string, error) {
var proof string
return proof, c.SendReq("gettxoutproof", &proof, txidsFilter, blockhash)
}
// GetTxOutSetInfo returns statistics about the unspect transactio output set.
func (c *Client) GetTxOutSetInfo() (*types.TransactionOutSetInfo, error) {
var info *types.TransactionOutSetInfo
return info, c.SendReq("gettxoutsetinfo", &info)
}
// ScanTxOutSet is experimental. Please read the docs https://developer.bitcoin.org/reference/rpc/scantxoutset.html.
func (c *Client) ScanTxOutSet(action types.ScanTxOutSetObject, scanObjects ...*types.ScanTxOutSetObject) (*types.ScanTxOutSetDetails, error) {
var details *types.ScanTxOutSetDetails
objs := make([]string, len(scanObjects))
for idx, obj := range scanObjects {
serializedObj, err := obj.ToJSON()
if err != nil {
return nil, err
}
objs[idx] = string(serializedObj)
}
return details, c.SendReq("scantxoutset", &details, action, objs)
}
// VerifyTxOutProof verifies that proof points to a transaction in ablock.
func (c *Client) VerifyTxOutProof(proof string) ([]string, error) {
var txids []string
return txids, c.SendReq("verifytxoutproof", &txids, proof)
}
// AnalyzePSBT analyzes and provides information about the current status of a AnalyzePSBTResult and its inputs.
func (c *Client) AnalyzePSBT(psbtbase64 string) (*types.AnalyzePSBTResult, error) {
var psbt *types.AnalyzePSBTResult
return psbt, c.SendReq("analyzepsbt", &psbt, psbtbase64)
}
// CombinePSBT combines multiple PSBTs into one.
func (c *Client) CombinePSBT(psbts []string) (string, error) {
var psbt string
return psbt, c.SendReq("combinepsbt", &psbt, psbts)
}
// CombineRawTransaction combines multiple partially signed transaction into one transaction.
func (c *Client) CombineRawTransaction(txs []string) (string, error) {
var tx string
return tx, c.SendReq("combinerawtransaction", &tx, txs)
}
// ConvertToPSBT converts a transaction to a psbt.
// If iswitness is null, it will use a heuristic to determine it.
func (c *Client) ConvertToPSBT(hex string, permitsigdata bool, iswitness *bool) (string, error) {
var psbt string
if iswitness != nil {
return psbt, c.SendReq("converttopsbt", &psbt, hex, permitsigdata, *iswitness)
}
return psbt, c.SendReq("converttopsbt", &psbt, hex, permitsigdata)
}
// CreatePSBT creates a psbt.
func (c *Client) CreatePSBT(inputs []*types.CreateTxInput, outputs []map[string]string, locktime int, replaceable bool) (string, error) {
var psbt string
return psbt, c.SendReq("createpsbt", &psbt, inputs, outputs, locktime, replaceable)
}
// CreateRawTransaction creates a raw transaction.
func (c *Client) CreateRawTransaction(inputs []*types.CreateTxInput, outputs []map[string]string, locktime int, replaceable bool) (string, error) {
var rawtx string
return rawtx, c.SendReq("createrawtransaction", &rawtx, inputs, outputs, locktime, replaceable)
}
// DecodePSBT takes a base64 psbt string and converts it to an object.
func (c *Client) DecodePSBT(psbtbase64 string) (*types.PSBT, error) {
var psbt *types.PSBT
return psbt, c.SendReq("decodepsbt", &psbt, psbtbase64)
}
// DecodeRawTransaction takes a hex transaction and converts it to an object.
// If iswitness is null it will use a heuristic to determine it.
func (c *Client) DecodeRawTransaction(txhex string, iswitness *bool) (*types.Transaction, error) {
var tx *types.Transaction
if iswitness != nil {
return tx, c.SendReq("decoderawtransaction", &tx, txhex, *iswitness)
}
return tx, c.SendReq("decoderawtransaction", &tx, txhex)
}
// DecodeScript decodes a hex-encoded script.
func (c *Client) DecodeScript(scripthex string) (*types.DecodedScript, error) {
var script *types.DecodedScript
return script, c.SendReq("decodescript", &script, scripthex)
}
// FinalizePSBT finalizes the inputs of a PSBT.
func (c *Client) FinalizePSBT(psbtbase64 string, extract bool) (*types.FinalizePSBTResult, error) {
var res *types.FinalizePSBTResult
return res, c.SendReq("finalizepsbt", &res, psbtbase64, extract)
}
// FundRawTransaction will select inputs to meet its output value..
// If iswitness is null it will use a heuristic to determine it.
func (c *Client) FundRawTransaction(tx string, opts *types.FundRawTransactionOptions, iswitness *bool) (*types.FundRawTransactionResult, error) {
var res *types.FundRawTransactionResult
if iswitness != nil {
return res, c.SendReq("fundrawtransaction", &res, tx, opts, *iswitness)
}
return res, c.SendReq("fundrawtransaction", &res, tx, opts)
}
// GetRawTransaction gets a transaction from mempool or the blockchain.
// If blockhash is not nil, will use the blockhash to look for the transaction.
func (c *Client) GetRawTransaction(txid string, blockhash *string) (string, error) {
var tx string
if blockhash != nil {
return tx, c.SendReq("getrawtransaction", &tx, txid, false, *blockhash)
}
return tx, c.SendReq("getrawtransaction", &tx, txid, false)
}
// GetRawTransactionVerbose gets a transaction from mempool or the blockchain.
// If blockhash is not nil, will use the blockhash to look for the transaction.
func (c *Client) GetRawTransactionVerbose(txid string, blockhash *string) (*types.Transaction, error) {
var tx *types.Transaction
if blockhash != nil {
return tx, c.SendReq("getrawtransaction", &tx, txid, true, *blockhash)
}
return tx, c.SendReq("getrawtransaction", &tx, txid, true)
}
// JoinPSBTs joins multiple distinct PSBTs with different inputs and outputs into one PSBT.
func (c *Client) JoinPSBTs(psbts []string) (string, error) {
var psbt string
return psbt, c.SendReq("joinpsbts", &psbt, psbts)
}
// SendRawTransaction sends a transaction to the local node and network.
// If maxfeerate is nil it will use node default.
func (c *Client) SendRawTransaction(hex string, maxfeerate *float64) (string, error) {
var tx string
if maxfeerate != nil {
return tx, c.SendReq("sendrawtransaction", &tx, hex, *maxfeerate)
}
return tx, c.SendReq("sendrawtransaction", &tx, hex)
}
// SignRawTransactionWithKey signs a raw transaction with the provided keys.
// If prevTxs is null or length 0, will be omitted. If sigHashType is "" will be set to types.SigHashTypeAll.
func (c *Client) SignRawTransactionWithKey(hex string, privKeys []string, prevTxs []*types.PreviousTransaction, sigHashType types.SigHashType) (*types.SignRawTransactionResult, error) {
var res *types.SignRawTransactionResult
if prevTxs == nil || len(prevTxs) == 0 {
return res, c.SendReq("signrawtransactionwithkey", &res, hex, privKeys)
}
if sigHashType == "" {
sigHashType = types.SigHashTypeAll
}
return res, c.SendReq("signrawtransactionwithkey", &res, hex, privKeys, prevTxs, sigHashType)
}
// TestMempoolAccept returns the result of mempool acceptance tsts indicating if raw transaction would be accepted by
// the mempool.
func (c *Client) TestMempoolAccept(rawtxs []string, maxfeeRate *float64) ([]*types.TestMempoolAcceptResult, error) {
var res []*types.TestMempoolAcceptResult
if maxfeeRate != nil {
return res, c.SendReq("testmempoolaccept", &res, rawtxs, *maxfeeRate)
}
return res, c.SendReq("testmempoolaccept", &res, rawtxs)
}
// UtxoUpdatePSBT updates all segwit inputs and outputs in a PSBT with data from output descriptors, the UTXO set or the
// mempool.
func (c *Client) UtxoUpdatePSBT(psbt string, scanObjects ...*types.ScanTxOutSetObject) (string, error) {
var res string
objs := make([]string, len(scanObjects))
for idx, obj := range scanObjects {
serializedObj, err := obj.ToJSON()
if err != nil {
return "", err
}
objs[idx] = string(serializedObj)
}
return res, c.SendReq("utxoupdatepsbt", &res, psbt, objs)
}