-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
193 lines (175 loc) · 5.5 KB
/
tx.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
package rest
import (
"encoding/hex"
"encoding/json"
"fmt"
abcitype "github.com/ci123chain/ci123chain/pkg/abci/types"
sdkerrors "github.com/ci123chain/ci123chain/pkg/abci/types/errors"
"github.com/ci123chain/ci123chain/pkg/abci/types/rest"
types2 "github.com/ci123chain/ci123chain/pkg/app/types"
"github.com/ci123chain/ci123chain/pkg/client/context"
"github.com/ci123chain/ci123chain/pkg/client/helper"
"github.com/ci123chain/ci123chain/pkg/util"
"github.com/ethereum/go-ethereum/crypto"
"github.com/gorilla/mux"
"net/http"
)
var cdc = types2.GetCodec()
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(cliCtx context.Context, r *mux.Router) {
r.HandleFunc("/account/new", NewAccountRequestHandlerFn(cliCtx)).Methods("POST")
r.HandleFunc("/bank/balance", QueryBalancesRequestHandlerFn(cliCtx)).Methods("POST")
r.HandleFunc("/account/nonce", QueryNonceRequestHandleFn(cliCtx)).Methods("POST")
r.HandleFunc("/transaction/multi_msgs_tx", rest.MiddleHandler(cliCtx, MultiMsgsRequest, sdkerrors.RootCodespace)).Methods("POST")
}
type BalanceData struct {
BalanceList interface{} `json:"balance_list"`
}
type NonceData struct {
Nonce uint64 `json:"nonce"`
}
type AccountAddress struct {
Address string `json:"address"`
Height string `json:"height"`
}
type Account struct {
Address string `json:"address"`
PrivKey string `json:"privKey"`
}
type Key struct {
ValidatorKey string `json:"validator_key"`
}
func NewAccountRequestHandlerFn(cliCtx context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, request *http.Request) {
w.Header().Set("Content-Type", "application/json")
key, err := crypto.GenerateKey()
if err != nil {
rest.WriteErrorRes(w, "generate key failed")
return
}
if key == nil {
rest.WriteErrorRes(w, "generate empty key")
return
}
address := crypto.PubkeyToAddress(key.PublicKey).Hex()
privKey := hex.EncodeToString(key.D.Bytes())
resp := Account{
Address: address,
PrivKey: privKey,
}
rest.PostProcessResponseBare(w, cliCtx, resp)
}
}
func QueryBalancesRequestHandlerFn(cliCtx context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, request *http.Request) {
w.Header().Set("Content-Type", "application/json")
address := request.FormValue("address")
height := request.FormValue("height")
prove := request.FormValue("prove")
checkErr := util.CheckStringLength(42, 100, address)
if checkErr != nil {
rest.WriteErrorRes(w, fmt.Sprintf("invalid address: %v", address))
return
}
if !rest.CheckHeightAndProve(w, height, prove, sdkerrors.RootCodespace) {
return
}
cliCtx, ok, err := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, request, height)
if !ok || err != nil {
rest.WriteErrorRes(w, "parse height failed")
return
}
addr, err2 := helper.StrToAddress(address)
if err2 != nil {
rest.WriteErrorRes(w, fmt.Sprintf("invalid address: %v", address))
return
}
//params := types.NewQueryBalanceParams(addr)
isProve := false
if prove == "true" {
isProve = true
}
res, proof, err2 := cliCtx.GetBalanceByAddress(addr, isProve, height)
if err2 != nil {
rest.WriteErrorRes(w, err2.Error())
return
}
value := BalanceData{BalanceList:res}
resp := rest.BuildQueryRes(height, isProve, value, proof)
rest.PostProcessResponseBare(w, cliCtx, resp)
}
}
func QueryNonceRequestHandleFn(cliCtx context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
address := r.FormValue("address")
height := r.FormValue("height")
prove := r.FormValue("prove")
checkErr := util.CheckStringLength(42, 100, address)
if checkErr != nil {
rest.WriteErrorRes(w, fmt.Sprintf("invalid address: %v", address))
return
}
if !rest.CheckHeightAndProve(w, height, prove, sdkerrors.RootCodespace) {
return
}
cliCtx, ok, err := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r, "")
if !ok || err != nil {
rest.WriteErrorRes(w, "parse height failed")
return
}
addrBytes, err2 := helper.ParseAddrs(address)
if len(addrBytes) < 1 || err2 != nil {
rest.WriteErrorRes(w, fmt.Sprintf("invalid address: %v", address))
return
}
isProve := false
if prove == "true" {
isProve = true
}
res, proof, err2 := cliCtx.GetNonceByAddress(addrBytes[0], isProve)
if err2 != nil {
rest.WriteErrorRes(w, err2.Error())
return
}
value := NonceData{Nonce:res}
resp := rest.BuildQueryRes(height, isProve, value, proof)
rest.PostProcessResponseBare(w, cliCtx, resp)
}
}
func MultiMsgsRequest(cliCtx context.Context, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
msg_str := r.FormValue("msgs")
var msgs []abcitype.Msg
var msgs_str []string
err := json.Unmarshal([]byte(msg_str), &msgs_str)
if err != nil {
rest.WriteErrorRes(w, err.Error())
return
}
for _, v := range msgs_str{
var msg abcitype.Msg
msg_byte, err := hex.DecodeString(v)
if err != nil {
rest.WriteErrorRes(w, err.Error())
return
}
err = cdc.UnmarshalBinaryLengthPrefixed(msg_byte, &msg)
if err != nil {
rest.WriteErrorRes(w, err.Error())
return
}
msgs = append(msgs, msg)
}
txByte, err := types2.SignCommonTx(cliCtx.FromAddr, cliCtx.Nonce, cliCtx.Gas, msgs, cliCtx.PrivateKey, cdc)
if err != nil {
rest.WriteErrorRes(w, err.Error())
return
}
res, err := cliCtx.BroadcastSignedTx(txByte)
if err != nil {
rest.WriteErrorRes(w, err.Error())
return
}
rest.PostProcessResponseBare(w, cliCtx, res)
}