-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnanorpc.go
185 lines (164 loc) · 3.77 KB
/
nanorpc.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
package nanorpc
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"net/http"
)
type Account struct {
Frontier string `json:"frontier"`
OpenBlock string `json:"open_block"`
RepresentativeBlock string `json:"representative_block"`
Balance string `json:"balance"`
ModifiedTimestamp string `json:"modified_timestamp"`
BlockCount string `json:"block_count"`
}
type Block struct {
Type string
Previous string
Destination string
Balance string
Work string
Signature string
}
type actionRPC struct {
Action string `json:"action"`
}
type accountRPC struct {
actionRPC
Account string `json:"account"`
}
type blockRPC struct {
actionRPC
Hash string `json:"hash"`
}
type sendRPC struct {
actionRPC
Wallet string `json:"wallet"`
Source string `json:"source"`
Destination string `json:"destination"`
Amount string `json:"amount"`
}
type walletRPC struct {
actionRPC
Wallet string `json:"wallet"`
}
func marshal(x interface{}) []byte {
buf, err := json.Marshal(x)
if err != nil {
panic(err)
}
return buf
}
func unmarshal(buf []byte) map[string]interface{} {
res := make(map[string]interface{})
if err := json.Unmarshal(buf, &res); err != nil {
panic(err)
}
return res
}
func request(url string, req interface{}) (
resdata []byte, res map[string]interface{}, err error,
) {
reqdata := marshal(req)
response, err := http.Post(url, "application/json", bytes.NewReader(reqdata))
if err != nil {
return
}
resdata, err = ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
res = unmarshal(resdata)
if val, ok := res["error"]; ok {
panic(val.(string) + ": " + string(reqdata))
}
return
}
func NanoUint64(rawstr string) uint64 {
var raw, rawtonano, nano = new(big.Int), new(big.Int), new(big.Int)
raw, ok := new(big.Int).SetString(rawstr, 10)
if !ok {
panic("invalid balance")
}
rawtonano.Exp(big.NewInt(10), big.NewInt(24), nil)
nano.Div(raw, rawtonano)
if nano.BitLen() > 64 {
return 9999999999999999999
}
return nano.Uint64()
}
func Accounts(url string, wallet string) (accounts []string, err error) {
req := walletRPC{actionRPC{"account_list"}, wallet}
_, res, err := request(url, req)
if err != nil {
return
}
v := res["accounts"].([]interface{})
accounts = make([]string, len(v))
for i, x := range v {
accounts[i] = x.(string)
}
return
}
func AccountInfo(url, acc string) (info Account, err error) {
req := accountRPC{actionRPC{"account_info"}, acc}
resdata, _, err := request(url, req)
if err != nil {
return
}
err = json.Unmarshal(resdata, &info)
return
}
func Balance(url, acc string) (balance string, pending string, err error) {
req := accountRPC{actionRPC{"account_balance"}, acc}
_, res, err := request(url, req)
if err != nil {
return
}
balance = res["balance"].(string)
pending = res["pending"].(string)
return
}
func BlockAccount(url, hash string) (acc string, err error) {
req := blockRPC{actionRPC{"block_account"}, hash}
_, res, err := request(url, req)
if err != nil {
return
}
acc = res["account"].(string)
return
}
func BlockInfo(url, hash string) (block Block, err error) {
if len(hash) != 64 {
err = fmt.Errorf("invalid hash")
return
}
req := blockRPC{actionRPC{"block"}, hash}
_, res, err := request(url, req)
if err != nil {
return
}
err = json.Unmarshal([]byte(res["contents"].(string)), &block)
if err != nil {
return
}
i, ok := new(big.Int).SetString(block.Balance, 16)
if !ok {
err = fmt.Errorf("internal error")
return
}
block.Balance = i.Text(10)
return
}
func Send(url, wallet, src, dst, amount string) (hash string, err error) {
req := sendRPC{actionRPC{"send"}, wallet, src, dst, amount}
_, res, err := request(url, req)
if err != nil {
return
}
hash = res["block"].(string)
return
}