/
OntHandler.go
183 lines (165 loc) · 4.5 KB
/
OntHandler.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
// "bytes"
// "time"
// "errors"
"./ont"
"./rpcclient"
"log"
// "strconv"
// "io/ioutil"
)
func dealwithOntMethod(t rpcclient.JsonRequest, response *rpcclient.Response2, retError *rpcclient.ErrorStruct) {
url := "http://" + cfg.Ont.Host + ":" + cfg.Ont.Port
s := ont.NewRPCClient(url, cfg.Ont.Wallet)
switch t.Method {
case "getdefaultaccount":
var pass string
if len(t.Params) > 0 {
pass = fmt.Sprintf("%v", t.Params[0])
}
b, err := s.GetDefaultAccount(pass)
if err != nil {
retError.Message = err.Error()
response.Error = retError
} else {
log.Println("balance:", b)
bytes, _ := json.Marshal(b)
response.Result = bytes
response.Error = nil
}
case "getbalance":
var account string
if len(t.Params) > 0 {
account = fmt.Sprintf("%v", t.Params[0])
}
// change, points, balance, err := s.GetBalance(addr)
b, err := s.GetBalance(account)
if err != nil {
retError.Message = err.Error()
response.Error = retError
} else {
log.Println("balance:", b)
bytes, _ := json.Marshal(b)
response.Result = bytes
response.Error = nil
}
// case "getbalanceofaddress":
// var addr string
// if len(t.Params) > 0 {
// addr = fmt.Sprintf("%v", t.Params[0])
// }
// b, err := s.GetBalanceOfAddr(addr)
// if err != nil {
// retError.Message = err.Error()
// response.Error = retError
// } else {
// log.Println("balance:", b)
// bytes, _ := json.Marshal(fmt.Sprintf("%.8f", b))
// response.Result = bytes
// response.Error = nil
// }
case "getnewaddress":
var pass string
if len(t.Params) > 0 {
pass = fmt.Sprintf("%v", t.Params[0])
}
// change, points, balance, err := s.GetBalance(addr)
b, err := s.GetNewAddress(pass, cfg.Ont.EncryptFilePass)
if err != nil {
retError.Message = err.Error()
response.Error = retError
} else {
log.Println("newaddress:", b)
bytes, _ := json.Marshal(b)
response.Result = bytes
response.Error = nil
}
case "gettransaction":
var transactionId string
if len(t.Params) > 0 {
transactionId = fmt.Sprintf("%v", t.Params[0])
}
// change, points, balance, err := s.GetBalance(addr)
b, err := s.GetTransaction(transactionId)
if err != nil {
retError.Message = err.Error()
response.Error = retError
} else {
log.Println("transaction list:", b)
bytes, _ := json.Marshal(b)
response.Result = bytes
response.Error = nil
}
case "send":
var from string
var to string
var amount string
var fromPass string
var gasprice string
if len(t.Params) >= 4 {
from = fmt.Sprintf("%v", t.Params[0])
to = fmt.Sprintf("%v", t.Params[1])
amount = fmt.Sprintf("%v", t.Params[2])
fromPass = fmt.Sprintf("%v", t.Params[3])
gasprice = fmt.Sprintf("%v", t.Params[4])
}
h, err := s.Send(from, fromPass, to, amount, gasprice)
log.Println("hash:", h)
if err != nil {
retError.Message = err.Error()
response.Error = retError
} else {
bytes, _ := json.Marshal(h)
response.Result = bytes
response.Error = nil
}
default:
retError.Message = "Method not found"
response.Error = retError
}
}
func OntHandler(w http.ResponseWriter, r *http.Request) {
////////////////////////////////
addr := r.Header.Get("X-Real-IP")
if addr == "" {
addr = r.Header.Get("X-Forwarded-For")
if addr == "" {
addr = r.RemoteAddr
}
}
log.Println("Method:", r.Method)
/////////////////////////////////////////////////////////////////
if r.Method != "POST" {
fmt.Fprint(w, "this interface should be post!")
} else {
// var ret string
body, _ := ioutil.ReadAll(r.Body)
var t rpcclient.JsonRequest
err_decode := json.Unmarshal(body, &t)
log.Println("request:", t)
defer r.Body.Close()
var response rpcclient.Response2
response.ID = t.ID
var retError rpcclient.ErrorStruct
retError.Code = -1
// {"result":null,"error":{"code":-32601,"message":"Method not found"},"id":"curltext"}
if err_decode != nil {
retError.Message = "json unmarshal error"
response.Error = &retError
} else {
dealwithOntMethod(t, &response, &retError)
}
var content string
if b, err := json.Marshal(response); err == nil {
content = string(b)
}
fmt.Fprint(w, content)
log_str := fmt.Sprintf("Started %s %s for %s:%s response:%s", r.Method, r.URL.Path, addr, body, content)
log.Println(log_str)
}
}