-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
256 lines (209 loc) · 6.34 KB
/
client.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
package btc
import (
"os"
"path/filepath"
"github.com/cmingxu/wallet-keeper/keeper"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcutil"
log "github.com/sirupsen/logrus"
)
// default account for reserved usage, which represent
// account belongs to enterpise default
var DEFAULT_ACCOUNT = "duckduck"
// default confirmation
var DEFAULT_CONFIRMATION = 6
type Client struct {
rpcClient *rpcclient.Client
l *log.Logger
}
// connect to bitcoind with HTTP RPC transport
func NewClient(host, user, pass, logDir string) (*Client, error) {
connCfg := &rpcclient.ConnConfig{
Host: host,
User: user,
Pass: pass,
HTTPPostMode: true,
DisableTLS: true,
}
client := &Client{}
var err error
client.rpcClient, err = rpcclient.New(connCfg, nil)
if err != nil {
return nil, err
}
logPath := filepath.Join(logDir, "btc.log")
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
if err != nil {
return nil, err
}
client.l = &log.Logger{
Out: logFile,
Level: log.DebugLevel,
Formatter: new(log.JSONFormatter),
}
return client, nil
}
// Ping
func (client *Client) Ping() error {
return client.rpcClient.Ping()
}
// GetBlockCount
func (client *Client) GetBlockCount() (int64, error) {
client.l.Infof("[GetBlockCount]")
return client.rpcClient.GetBlockCount()
}
// GetAddress - default address
func (client *Client) GetAddress(account string) (string, error) {
if len(account) == 0 {
account = DEFAULT_ACCOUNT
}
client.l.Infof("[GetAddress] for account %s", account)
address, err := client.rpcClient.GetAccountAddress(account)
if err != nil {
return "", err
}
return address.String(), nil
}
// Create Account
// Returns customized account info
func (client *Client) CreateAccount(account string) (keeper.Account, error) {
client.l.Infof("[CreateAccount] for account %s", account)
// GetAddress will create account if not exists
address, err := client.GetAddress(account)
if err != nil {
return keeper.Account{}, err
}
return keeper.Account{
Account: account,
Balance: 0.0,
Addresses: []string{address},
}, nil
}
// GetAccountInfo
func (client *Client) GetAccountInfo(account string, minConf int) (keeper.Account, error) {
var accountsMap map[string]float64
var err error
client.l.Infof("[GetAccountInfo] account %s, with minConf %d", account, minConf)
if accountsMap, err = client.ListAccountsMinConf(minConf); err != nil {
return keeper.Account{}, err
}
balance, found := accountsMap[account]
if !found {
return keeper.Account{}, keeper.ErrAccountNotFound
}
addresses, err := client.GetAddressesByAccount(account)
if err != nil {
return keeper.Account{}, err
}
return keeper.Account{
Account: account,
Balance: balance,
Addresses: addresses,
}, nil
}
// TODO
// GetNewAddress does map to `getnewaddress` rpc call now
// rpcclient doesn't have such golang wrapper func.
func (client *Client) GetNewAddress(account string) (string, error) {
if len(account) == 0 {
account = DEFAULT_ACCOUNT
}
address, err := client.rpcClient.GetNewAddress(account)
if err != nil {
return "", err
}
client.l.Infof("[GetNewAddress] for account %s, address is %s", account, address.String())
return address.String(), nil
}
// GetAddressesByAccount
func (client *Client) GetAddressesByAccount(account string) ([]string, error) {
if len(account) == 0 {
account = DEFAULT_ACCOUNT
}
client.l.Infof("[GetAddressesByAccount] for account %s", account)
addresses, err := client.rpcClient.GetAddressesByAccount(account)
if err != nil {
return []string{}, err
}
addrs := make([]string, 0)
for _, addr := range addresses {
addrs = append(addrs, addr.String())
}
return addrs, nil
}
// ListAccountsMinConf
func (client *Client) ListAccountsMinConf(minConf int) (map[string]float64, error) {
accounts := make(map[string]float64)
client.l.Infof("[ListAccountsMinConf] with minConf %d", minConf)
accountsWithAmount, err := client.rpcClient.ListAccountsMinConf(minConf)
if err != nil {
return accounts, err
}
for account, amount := range accountsWithAmount {
accounts[account] = amount.ToBTC()
}
return accounts, nil
}
// SendToAddress
func (client *Client) SendToAddress(address string, amount float64) (string, error) {
client.l.Infof("[SendToAddress] to address %s: %f", address, amount)
decoded, err := decodeAddress(address, chaincfg.MainNetParams)
if err != nil {
return "", err
}
btcAmount, err := convertToBtcAmount(amount)
if err != nil {
return "", err
}
hash, err := client.rpcClient.SendToAddressComment(decoded, btcAmount, "", "")
if err != nil {
return "", err
}
client.l.Infof("[SendToAddress] to address %s: %f, hash is %s", address, amount, hash)
return hash.String(), nil
}
// TODO check validity of account and have sufficent balance
func (client *Client) SendFrom(account, address string, amount float64) (string, error) {
client.l.Infof("[SendFrom] from account %s to address %s with amount %f ", account, address, amount)
decoded, err := decodeAddress(address, chaincfg.MainNetParams)
if err != nil {
return "", err
}
btcAmount, err := convertToBtcAmount(amount)
if err != nil {
return "", err
}
hash, err := client.rpcClient.SendFrom(account, decoded, btcAmount)
if err != nil {
return "", err
}
client.l.Infof("[SendFrom] from account %s to address %s with amount %f, result hash %s ", account, address, amount, hash)
return hash.String(), nil
}
// Move
func (client *Client) Move(from, to string, amount float64) (bool, error) {
client.l.Infof("[Move] from %s to %s with amount %f ", from, to, amount)
btcAmount, err := convertToBtcAmount(amount)
if err != nil {
return false, err
}
client.l.Infof("[Move] success from %s to %s with amount %f ", from, to, amount)
return client.rpcClient.Move(from, to, btcAmount)
}
// ListUnspentMin
func (client *Client) ListUnspentMin(minConf int) ([]btcjson.ListUnspentResult, error) {
return client.rpcClient.ListUnspentMin(minConf)
}
// decodeAddress from string to decodedAddress
func decodeAddress(address string, cfg chaincfg.Params) (btcutil.Address, error) {
decodedAddress, err := btcutil.DecodeAddress(address, &cfg)
if err != nil {
return nil, err
}
return decodedAddress, nil
}
func convertToBtcAmount(amount float64) (btcutil.Amount, error) {
return btcutil.NewAmount(amount)
}