-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrpc.go
73 lines (62 loc) · 1.4 KB
/
rpc.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
package bn
import (
"net/http"
"reflect"
"time"
"github.com/libsv/go-bn/internal/config"
"github.com/libsv/go-bn/internal/service"
)
// NodeClient interfaces interacting with all commands on a bitcoin node.
type NodeClient interface {
BlockChainClient
ControlClient
MiningClient
NetworkClient
TransactionClient
UtilClient
WalletClient
}
type positionalOptionalArgs interface {
Args() []interface{}
}
type client struct {
rpc service.RPC
isMainnet bool
}
// NewNodeClient returns a node client, built from the provided option funcs.
// This client is used for interfacing with the bitcoin node across all subcategories.
func NewNodeClient(oo ...BitcoinClientOptFunc) NodeClient {
opts := &clientOpts{
timeout: 30 * time.Second,
host: "http://localhost:8332",
username: "bitcoin",
password: "bitcoin",
}
for _, o := range oo {
o(opts)
}
if opts.rpc != nil {
return &client{
rpc: opts.rpc,
isMainnet: opts.isMainnet,
}
}
rpc := service.NewRPC(&config.RPC{
Username: opts.username,
Password: opts.password,
Host: opts.host,
}, &http.Client{Timeout: opts.timeout})
if opts.cache {
rpc = service.NewCache(rpc)
}
return &client{
rpc: rpc,
isMainnet: opts.isMainnet,
}
}
func (c *client) argsFor(p positionalOptionalArgs, args ...interface{}) []interface{} {
if reflect.ValueOf(p).IsNil() {
return args
}
return append(args, p.Args()...)
}