-
Notifications
You must be signed in to change notification settings - Fork 123
/
bitcoin.go
416 lines (381 loc) · 13.3 KB
/
bitcoin.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package bitcoin
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"time"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil"
"github.com/renproject/multichain/api/address"
"github.com/renproject/multichain/api/utxo"
"github.com/renproject/pack"
)
const (
// DefaultClientTimeout used by the Client.
DefaultClientTimeout = time.Minute
// DefaultClientTimeoutRetry used by the Client.
DefaultClientTimeoutRetry = time.Second
// DefaultClientHost used by the Client. This should only be used for local
// deployments of the multichain.
DefaultClientHost = "http://0.0.0.0:18443"
// DefaultClientUser used by the Client. This is insecure, and should only
// be used for local — or publicly accessible — deployments of the
// multichain.
DefaultClientUser = "user"
// DefaultClientPassword used by the Client. This is insecure, and should
// only be used for local — or publicly accessible — deployments of the
// multichain.
DefaultClientPassword = "password"
)
// ClientOptions are used to parameterise the behaviour of the Client.
type ClientOptions struct {
Timeout time.Duration
TimeoutRetry time.Duration
Host string
User string
Password string
}
// DefaultClientOptions returns ClientOptions with the default settings. These
// settings are valid for use with the default local deployment of the
// multichain. In production, the host, user, and password should be changed.
func DefaultClientOptions() ClientOptions {
return ClientOptions{
Timeout: DefaultClientTimeout,
TimeoutRetry: DefaultClientTimeoutRetry,
Host: DefaultClientHost,
User: DefaultClientUser,
Password: DefaultClientPassword,
}
}
// WithHost sets the URL of the Bitcoin node.
func (opts ClientOptions) WithHost(host string) ClientOptions {
opts.Host = host
return opts
}
// WithUser sets the username that will be used to authenticate with the Bitcoin
// node.
func (opts ClientOptions) WithUser(user string) ClientOptions {
opts.User = user
return opts
}
// WithPassword sets the password that will be used to authenticate with the
// Bitcoin node.
func (opts ClientOptions) WithPassword(password string) ClientOptions {
opts.Password = password
return opts
}
// A Client interacts with an instance of the Bitcoin network using the RPC
// interface exposed by a Bitcoin node.
type Client interface {
utxo.Client
// UnspentOutputs spendable by the given address.
UnspentOutputs(ctx context.Context, minConf, maxConf int64, address address.Address) ([]utxo.Output, error)
// Confirmations of a transaction in the Bitcoin network.
Confirmations(ctx context.Context, txHash pack.Bytes) (int64, error)
// EstimateSmartFee
EstimateSmartFee(ctx context.Context, numBlocks int64) (float64, error)
// EstimateFeeLegacy
EstimateFeeLegacy(ctx context.Context, numBlocks int64) (float64, error)
}
type client struct {
opts ClientOptions
httpClient http.Client
}
// NewClient returns a new Client.
func NewClient(opts ClientOptions) Client {
httpClient := http.Client{}
httpClient.Timeout = opts.Timeout
return &client{
opts: opts,
httpClient: httpClient,
}
}
// LatestBlock returns the height of the longest blockchain.
func (client *client) LatestBlock(ctx context.Context) (pack.U64, error) {
var resp int64
if err := client.send(ctx, &resp, "getblockcount"); err != nil {
return pack.NewU64(0), fmt.Errorf("get block count: %v", err)
}
if resp < 0 {
return pack.NewU64(0), fmt.Errorf("unexpected block count, expected > 0, got: %v", resp)
}
return pack.NewU64(uint64(resp)), nil
}
// Output associated with an outpoint, and its number of confirmations.
func (client *client) Output(ctx context.Context, outpoint utxo.Outpoint) (utxo.Output, pack.U64, error) {
resp := btcjson.TxRawResult{}
hash := chainhash.Hash{}
copy(hash[:], outpoint.Hash)
if err := client.send(ctx, &resp, "getrawtransaction", hash.String(), 1); err != nil {
return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad \"getrawtransaction\": %v", err)
}
if outpoint.Index.Uint32() >= uint32(len(resp.Vout)) {
return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad index: %v is out of range", outpoint.Index)
}
vout := resp.Vout[outpoint.Index.Uint32()]
amount, err := btcutil.NewAmount(vout.Value)
if err != nil {
return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad amount: %v", err)
}
if amount < 0 {
return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad amount: %v", amount)
}
pubKeyScript, err := hex.DecodeString(vout.ScriptPubKey.Hex)
if err != nil {
return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad pubkey script: %v", err)
}
output := utxo.Output{
Outpoint: outpoint,
Value: pack.NewU256FromU64(pack.NewU64(uint64(amount))),
PubKeyScript: pack.NewBytes(pubKeyScript),
}
return output, pack.NewU64(resp.Confirmations), nil
}
// UnspentOutput returns the unspent transaction output identified by the
// given outpoint. It also returns the number of confirmations for the
// output. If the output cannot be found before the context is done, the
// output is invalid, or the output has been spent, then an error should be
// returned.
func (client *client) UnspentOutput(ctx context.Context, outpoint utxo.Outpoint) (utxo.Output, pack.U64, error) {
resp := btcjson.GetTxOutResult{}
hash := chainhash.Hash{}
copy(hash[:], outpoint.Hash)
if err := client.send(ctx, &resp, "gettxout", hash.String(), outpoint.Index.Uint32()); err != nil {
return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad \"gettxout\": %v", err)
}
amount, err := btcutil.NewAmount(resp.Value)
if err != nil {
return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad amount: %v", err)
}
if amount < 0 {
return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad amount: %v", amount)
}
if resp.Confirmations < 0 {
return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad confirmations: %v", resp.Confirmations)
}
pubKeyScript, err := hex.DecodeString(resp.ScriptPubKey.Hex)
if err != nil {
return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad pubkey script: %v", err)
}
output := utxo.Output{
Outpoint: outpoint,
Value: pack.NewU256FromU64(pack.NewU64(uint64(amount))),
PubKeyScript: pack.NewBytes(pubKeyScript),
}
return output, pack.NewU64(uint64(resp.Confirmations)), nil
}
// SubmitTx to the Bitcoin network.
func (client *client) SubmitTx(ctx context.Context, tx utxo.Tx) error {
serial, err := tx.Serialize()
if err != nil {
return fmt.Errorf("bad tx: %v", err)
}
resp := ""
if err := client.send(ctx, &resp, "sendrawtransaction", hex.EncodeToString(serial)); err != nil {
return fmt.Errorf("bad \"sendrawtransaction\": %v", err)
}
return nil
}
// TxSenders returns the senders of the transaction.
func (client *client) TxSenders(ctx context.Context, id pack.Bytes) ([]pack.String, error) {
resp, err := client.getRawTransaction(ctx, id)
if err != nil {
return nil, fmt.Errorf("bad \"getrawtransaction\": %v", err)
}
addrs := make([]pack.String, 0)
for _, vin := range resp.Vin {
txHash, err := chainhash.NewHashFromStr(vin.Txid)
if err != nil {
return nil, err
}
rawTx, err := client.getRawTransaction(ctx, txHash.CloneBytes())
if err != nil {
return nil, err
}
for _, addr := range rawTx.Vout[int(vin.Vout)].ScriptPubKey.Addresses {
addrs = append(addrs, pack.String(addr))
}
}
return addrs, nil
}
// UnspentOutputs spendable by the given address.
func (client *client) UnspentOutputs(ctx context.Context, minConf, maxConf int64, addr address.Address) ([]utxo.Output, error) {
resp := []btcjson.ListUnspentResult{}
if err := client.send(ctx, &resp, "listunspent", minConf, maxConf, []string{string(addr)}); err != nil && err != io.EOF {
return []utxo.Output{}, fmt.Errorf("bad \"listunspent\": %v", err)
}
outputs := make([]utxo.Output, len(resp))
for i := range outputs {
amount, err := btcutil.NewAmount(resp[i].Amount)
if err != nil {
return []utxo.Output{}, fmt.Errorf("bad amount: %v", err)
}
if amount < 0 {
return []utxo.Output{}, fmt.Errorf("bad amount: %v", amount)
}
pubKeyScript, err := hex.DecodeString(resp[i].ScriptPubKey)
if err != nil {
return []utxo.Output{}, fmt.Errorf("bad pubkey script: %v", err)
}
txid, err := chainhash.NewHashFromStr(resp[i].TxID)
if err != nil {
return []utxo.Output{}, fmt.Errorf("bad txid: %v", err)
}
outputs[i] = utxo.Output{
Outpoint: utxo.Outpoint{
Hash: pack.NewBytes(txid[:]),
Index: pack.NewU32(resp[i].Vout),
},
Value: pack.NewU256FromU64(pack.NewU64(uint64(amount))),
PubKeyScript: pack.NewBytes(pubKeyScript),
}
}
return outputs, nil
}
// Confirmations of a transaction in the Bitcoin network.
func (client *client) Confirmations(ctx context.Context, txHash pack.Bytes) (int64, error) {
resp := btcjson.GetTransactionResult{}
size := len(txHash)
txHashReversed := make([]byte, size)
copy(txHashReversed[:], txHash[:])
for i := 0; i < size/2; i++ {
txHashReversed[i], txHashReversed[size-1-i] = txHashReversed[size-1-i], txHashReversed[i]
}
if err := client.send(ctx, &resp, "gettransaction", hex.EncodeToString(txHashReversed)); err != nil {
return 0, fmt.Errorf("bad \"gettransaction\": %v", err)
}
confirmations := resp.Confirmations
if confirmations < 0 {
confirmations = 0
}
return confirmations, nil
}
// EstimateSmartFee fetches the estimated bitcoin network fees to be paid (in
// BTC per kilobyte) needed for a transaction to be confirmed within `numBlocks`
// blocks. An error will be returned if the bitcoin node hasn't observed enough
// blocks to make an estimate for the provided target `numBlocks`.
func (client *client) EstimateSmartFee(ctx context.Context, numBlocks int64) (float64, error) {
resp := btcjson.EstimateSmartFeeResult{}
if err := client.send(ctx, &resp, "estimatesmartfee", numBlocks); err != nil {
return 0.0, fmt.Errorf("estimating smart fee: %v", err)
}
if resp.Errors != nil && len(resp.Errors) > 0 {
return 0.0, fmt.Errorf("estimating smart fee: %v", resp.Errors[0])
}
return *resp.FeeRate, nil
}
func (client *client) EstimateFeeLegacy(ctx context.Context, numBlocks int64) (float64, error) {
var resp float64
switch numBlocks {
case int64(0):
if err := client.send(ctx, &resp, "estimatefee"); err != nil {
return 0.0, fmt.Errorf("estimating fee: %v", err)
}
default:
if err := client.send(ctx, &resp, "estimatefee", numBlocks); err != nil {
return 0.0, fmt.Errorf("estimating fee: %v", err)
}
}
return resp, nil
}
func (client *client) getRawTransaction(ctx context.Context, txhash pack.Bytes) (btcjson.TxRawResult, error) {
resp := btcjson.TxRawResult{}
hash := chainhash.Hash{}
copy(hash[:], txhash)
if err := client.send(ctx, &resp, "getrawtransaction", hash.String(), 1); err != nil {
return resp, fmt.Errorf("bad \"getrawtransaction\": %v", err)
}
return resp, nil
}
func (client *client) send(ctx context.Context, resp interface{}, method string, params ...interface{}) error {
// Encode the request.
data, err := encodeRequest(method, params)
if err != nil {
return err
}
return retry(ctx, client.opts.TimeoutRetry, func() error {
// Create request and add basic authentication headers. The context is
// not attached to the request, and instead we all each attempt to run
// for the timeout duration, and we keep attempting until success, or
// the context is done.
req, err := http.NewRequest("POST", client.opts.Host, bytes.NewBuffer(data))
if err != nil {
return fmt.Errorf("building http request: %v", err)
}
req.SetBasicAuth(client.opts.User, client.opts.Password)
// Send the request and decode the response.
res, err := client.httpClient.Do(req)
if err != nil {
return fmt.Errorf("sending http request: %v", err)
}
defer res.Body.Close()
if err := decodeResponse(resp, res.Body); err != nil {
return fmt.Errorf("decoding http response: %v", err)
}
return nil
})
}
func encodeRequest(method string, params []interface{}) ([]byte, error) {
rawParams, err := json.Marshal(params)
if err != nil {
return nil, fmt.Errorf("encoding params: %v", err)
}
req := struct {
Version string `json:"version"`
ID int `json:"id"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
}{
Version: "2.0",
ID: rand.Int(),
Method: method,
Params: rawParams,
}
rawReq, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("encoding request: %v", err)
}
return rawReq, nil
}
func decodeResponse(resp interface{}, r io.Reader) error {
res := struct {
Version string `json:"version"`
ID int `json:"id"`
Result *json.RawMessage `json:"result"`
Error *json.RawMessage `json:"error"`
}{}
if err := json.NewDecoder(r).Decode(&res); err != nil {
return fmt.Errorf("decoding response: %v", err)
}
if res.Error != nil {
return fmt.Errorf("decoding response: %v", string(*res.Error))
}
if res.Result == nil {
return fmt.Errorf("decoding result: result is nil")
}
if err := json.Unmarshal(*res.Result, resp); err != nil {
return fmt.Errorf("decoding result: %v", err)
}
return nil
}
func retry(ctx context.Context, dur time.Duration, f func() error) error {
ticker := time.NewTicker(dur)
err := f()
for err != nil {
log.Printf("retrying: %v", err)
select {
case <-ctx.Done():
return fmt.Errorf("%v: %v", ctx.Err(), err)
case <-ticker.C:
err = f()
}
}
return nil
}