-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_rpc.go
187 lines (157 loc) · 4.78 KB
/
json_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
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
package rpcclient
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type jsonRPCRequest struct {
Version string `json:"jsonrpc"` // default '1.0'
ID string `json:"id"` // default 'jsonrpc'
Method string `json:"method"`
Params []interface{} `json:"params"`
}
// JSONRPCResult repc result
type JSONRPCResult struct {
Data interface{} `json:"result"`
Error *JSONRPCError `json:"error"`
ID string `json:"id"`
}
// JSONRPCError will not be nil while failed
type JSONRPCError struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (err *JSONRPCError) Error() error {
return fmt.Errorf("[code: %d] %s", err.Code, err.Message)
}
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}
// BitcoinRPC struct need be initialed
type BitcoinRPC struct {
Debug bool
Host string
Version string
ID string
User string
Pass string
}
// Call certain method
func (btc *BitcoinRPC) Call(method string, params []interface{}) (interface{}, error) {
if btc.Version == "" {
btc.Version = "1.0"
}
if btc.ID == "" {
btc.ID = "jsonrpc"
}
rpcReq := jsonRPCRequest{Version: btc.Version, ID: btc.ID, Method: method, Params: params}
rpcReqData, err := json.Marshal(rpcReq)
if err != nil {
return nil, err
}
if btc.Debug {
fmt.Printf("[json-rpc-debug] Call: %s\n", rpcReqData)
}
req, err := http.NewRequest("POST", btc.Host, bytes.NewBuffer(rpcReqData))
req.Header.Set("content-type", "text/json")
req.Header.Add("Authorization", "Basic "+basicAuth(btc.User, btc.Pass))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if btc.Debug {
fmt.Printf("[json-rpc-debug] Resp: %s\n", body)
}
var result JSONRPCResult
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
if result.Error != nil {
return nil, result.Error.Error()
}
return result.Data, nil
}
// GetNewAddress returns a new Qtum address for receiving payments
func (btc *BitcoinRPC) GetNewAddress() (string, error) {
ret, err := btc.Call("getnewaddress", []interface{}{})
if err != nil {
return "", err
}
return ret.(string), nil
}
// GetAddressesByAccount returns the list of addresses for the given account.
func (btc *BitcoinRPC) GetAddressesByAccount(account string) ([]string, error) {
ret, err := btc.Call("getaddressesbyaccount", []interface{}{account})
if err != nil {
return nil, err
}
addresses := []string{}
for _, v := range ret.([]interface{}) {
s := v.(string)
addresses = append(addresses, s)
}
return addresses, nil
}
// GetAccountAddress returns the current address for receiving payments to this account.
// If the account don't exist, it creates both the account and address.
// Once a payment has been received to the address, future calls to this RPC for
// the same account will return a different address.
func (btc *BitcoinRPC) GetAccountAddress(account string) (string, error) {
ret, err := btc.Call("getaccountaddress", []interface{}{account})
if err != nil {
return "", err
}
return ret.(string), nil
}
// GetReceivedByAddress returns the total amount received by the given address
// in transactions with at least 1 confirmation.
func (btc *BitcoinRPC) GetReceivedByAddress(address string, minconf int) (float64, error) {
ret, err := btc.Call("getreceivedbyaddress", []interface{}{address, minconf})
if err != nil {
return 0, err
}
return ret.(float64), nil
}
// GetReceivedByAccount returns the total amount received by addresses in a particular
// account from transactions with the specified number of confirmations.
func (btc *BitcoinRPC) GetReceivedByAccount(account string, minconf int) (float64, error) {
ret, err := btc.Call("getreceivedbyaccount", []interface{}{account, minconf})
if err != nil {
return 0, err
}
return ret.(float64), nil
}
// SendToAddress send an amount to a given address, returns its transaction id.
func (btc *BitcoinRPC) SendToAddress(address string, amount float64) (string, error) {
ret, err := btc.Call("sendtoaddress", []interface{}{address})
if err != nil {
return "", err
}
return ret.(string), nil
}
// ImportAddress imports a address
func (btc *BitcoinRPC) ImportAddress(address string) error {
_, err := btc.Call("importaddress", []interface{}{address})
if err != nil {
return err
}
return nil
}
// ValidateAddress returns information about the given address
func (btc *BitcoinRPC) ValidateAddress(address string) (map[string]interface{}, error) {
ret, err := btc.Call("validateaddress", []interface{}{address})
if err != nil {
return nil, err
}
return ret.(map[string]interface{}), err
}