forked from chappjc/dcrspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpcclient.go
118 lines (99 loc) · 3.38 KB
/
rpcclient.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
package main
import (
"fmt"
"io/ioutil"
"github.com/decred/dcrd/rpcclient"
)
var requiredChainServerAPI = semver{major: 3, minor: 1, patch: 0}
var requiredWalletAPI = semver{major: 4, minor: 1, patch: 0}
func connectWalletRPC(cfg *config) (*rpcclient.Client, semver, error) {
var dcrwCerts []byte
var err error
var walletVer semver
if !cfg.DisableWalletTLS {
dcrwCerts, err = ioutil.ReadFile(cfg.DcrwCert)
if err != nil {
log.Errorf("Failed to read dcrwallet cert file at %s: %s\n",
cfg.DcrwCert, err.Error())
return nil, walletVer, err
}
}
log.Debugf("Attempting to connect to dcrwallet RPC %s as user %s "+
"using certificate located in %s",
cfg.DcrwServ, cfg.DcrwUser, cfg.DcrwCert)
connCfgWallet := &rpcclient.ConnConfig{
Host: cfg.DcrwServ,
Endpoint: "ws",
User: cfg.DcrwUser,
Pass: cfg.DcrwPass,
Certificates: dcrwCerts,
DisableTLS: cfg.DisableWalletTLS,
}
ntfnHandlers := getWalletNtfnHandlers(cfg)
dcrwClient, err := rpcclient.New(connCfgWallet, ntfnHandlers)
if err != nil {
log.Errorf("Failed to start dcrwallet RPC client: %s\nPerhaps you"+
" wanted to start with --nostakeinfo?\n", err.Error())
log.Errorf("Verify that rpc.cert is for your wallet:\n\t%v",
cfg.DcrwCert)
return nil, walletVer, err
}
// Ensure the wallet RPC server has a compatible API version.
ver, err := dcrwClient.Version()
if err != nil {
log.Error("Unable to get RPC version: ", err)
return nil, walletVer, fmt.Errorf("Unable to get wallet RPC version")
}
dcrwVer := ver["dcrwalletjsonrpcapi"]
walletVer = semver{dcrwVer.Major, dcrwVer.Minor, dcrwVer.Patch}
if !semverCompatible(requiredWalletAPI, walletVer) {
return nil, walletVer, fmt.Errorf("Wallet JSON-RPC server does not have "+
"a compatible API version. Advertises %v but require %v",
walletVer, requiredWalletAPI)
}
return dcrwClient, walletVer, nil
}
func connectNodeRPC(cfg *config) (*rpcclient.Client, semver, error) {
var dcrdCerts []byte
var err error
var nodeVer semver
if !cfg.DisableDaemonTLS {
dcrdCerts, err = ioutil.ReadFile(cfg.DcrdCert)
if err != nil {
log.Errorf("Failed to read dcrd cert file at %s: %s\n",
cfg.DcrdCert, err.Error())
return nil, nodeVer, err
}
}
log.Debugf("Attempting to connect to dcrd RPC %s as user %s "+
"using certificate located in %s",
cfg.DcrdServ, cfg.DcrdUser, cfg.DcrdCert)
connCfgDaemon := &rpcclient.ConnConfig{
Host: cfg.DcrdServ,
Endpoint: "ws", // websocket
User: cfg.DcrdUser,
Pass: cfg.DcrdPass,
Certificates: dcrdCerts,
DisableTLS: cfg.DisableDaemonTLS,
}
ntfnHandlers := getNodeNtfnHandlers(cfg)
dcrdClient, err := rpcclient.New(connCfgDaemon, ntfnHandlers)
if err != nil {
log.Errorf("Failed to start dcrd RPC client: %s\n", err.Error())
return nil, nodeVer, err
}
// Ensure the RPC server has a compatible API version.
ver, err := dcrdClient.Version()
if err != nil {
log.Error("Unable to get RPC version: ", err)
return nil, nodeVer, fmt.Errorf("Unable to get node RPC version")
}
dcrdVer := ver["dcrdjsonrpcapi"]
nodeVer = semver{dcrdVer.Major, dcrdVer.Minor, dcrdVer.Patch}
if !semverCompatible(requiredChainServerAPI, nodeVer) {
return nil, nodeVer, fmt.Errorf("Node JSON-RPC server does not have "+
"a compatible API version. Advertises %v but require %v",
nodeVer, requiredChainServerAPI)
}
return dcrdClient, nodeVer, nil
}