Skip to content

Commit bda15e1

Browse files
author
vincent.ch.cn
committed
add byteTx
1 parent 31a6b40 commit bda15e1

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed

cmd/iris/rest.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/cosmos/cosmos-sdk/client"
1414
"github.com/cosmos/cosmos-sdk/client/commands"
1515
rest "github.com/cosmos/cosmos-sdk/client/rest"
16+
byteTx "github.com/irisnet/iris-hub/rest"
1617
coinrest "github.com/cosmos/cosmos-sdk/modules/coin/rest"
1718
noncerest "github.com/cosmos/cosmos-sdk/modules/nonce/rest"
1819
rolerest "github.com/cosmos/cosmos-sdk/modules/roles/rest"
@@ -46,6 +47,7 @@ func cmdRestServer(cmd *cobra.Command, args []string) error {
4647
rootDir := viper.GetString(cli.HomeFlag)
4748
keyMan := client.GetKeyManager(rootDir)
4849
serviceKeys := rest.NewServiceKeys(keyMan)
50+
serviceByteTx := byteTx.NewServiceByteTx(keyMan)
4951
serviceTxs := rest.NewServiceTxs(commands.GetNode())
5052

5153
routeRegistrars := []func(*mux.Router) error{
@@ -63,6 +65,9 @@ func cmdRestServer(cmd *cobra.Command, args []string) error {
6365
// Iris post transaction handler
6466
serviceTxs.RegisterPostTx,
6567

68+
// Iris transfer Tx to byte[]
69+
serviceByteTx.RegisterByteTx,
70+
6671
// Nonce query handler
6772
noncerest.RegisterQueryNonce,
6873

cmd/iris/rest_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
8+
"github.com/spf13/viper"
9+
"github.com/gorilla/mux"
10+
"testing"
11+
"github.com/tendermint/tmlibs/cli"
12+
"github.com/cosmos/cosmos-sdk/client"
13+
"github.com/cosmos/cosmos-sdk/client/commands"
14+
rest "github.com/cosmos/cosmos-sdk/client/rest"
15+
byteTx "github.com/irisnet/iris-hub/rest"
16+
coinrest "github.com/cosmos/cosmos-sdk/modules/coin/rest"
17+
noncerest "github.com/cosmos/cosmos-sdk/modules/nonce/rest"
18+
rolerest "github.com/cosmos/cosmos-sdk/modules/roles/rest"
19+
20+
stakerest "github.com/cosmos/gaia/modules/stake/rest"
21+
)
22+
23+
func TestRest(t *testing.T) {
24+
25+
26+
router := mux.NewRouter()
27+
28+
rootDir := viper.GetString(cli.HomeFlag)
29+
keyMan := client.GetKeyManager(rootDir)
30+
serviceKeys := rest.NewServiceKeys(keyMan)
31+
serviceByteTx := byteTx.NewServiceByteTx(keyMan)
32+
serviceTxs := rest.NewServiceTxs(commands.GetNode())
33+
34+
35+
routeRegistrars := []func(*mux.Router) error{
36+
// rest.Keys handlers
37+
serviceKeys.RegisterCRUD,
38+
39+
// Coin handlers (Send, Query, SearchSent)
40+
coinrest.RegisterAll,
41+
42+
// Roles createRole handler
43+
rolerest.RegisterCreateRole,
44+
45+
// Iris sign transactions handler
46+
serviceKeys.RegisterSignTx,
47+
// Iris post transaction handler
48+
serviceTxs.RegisterPostTx,
49+
50+
// Iris transfer Tx to byte[]
51+
serviceByteTx.RegisterByteTx,
52+
53+
// Nonce query handler
54+
noncerest.RegisterQueryNonce,
55+
56+
// Staking query handlers
57+
stakerest.RegisterQueryCandidate,
58+
stakerest.RegisterQueryCandidates,
59+
stakerest.RegisterQueryDelegatorBond,
60+
stakerest.RegisterQueryDelegatorCandidates,
61+
// Staking tx builders
62+
stakerest.RegisterDelegate,
63+
stakerest.RegisterUnbond,
64+
}
65+
66+
for _, routeRegistrar := range routeRegistrars {
67+
if err := routeRegistrar(router); err != nil {
68+
log.Fatal(err)
69+
}
70+
}
71+
72+
addr := fmt.Sprintf(":%d", 8080)
73+
74+
log.Printf("Serving on %q", addr)
75+
http.ListenAndServe(addr, router)
76+
77+
}

rest/byteTx.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package rest
2+
3+
import (
4+
"github.com/gorilla/mux"
5+
"github.com/tendermint/go-crypto/keys"
6+
"net/http"
7+
sdk "github.com/cosmos/cosmos-sdk"
8+
"encoding/hex"
9+
"github.com/spf13/viper"
10+
"github.com/cosmos/cosmos-sdk/client/commands"
11+
"github.com/pkg/errors"
12+
cmn "github.com/tendermint/tmlibs/common"
13+
"github.com/cosmos/cosmos-sdk/client"
14+
"github.com/cosmos/cosmos-sdk/client/commands/query"
15+
"github.com/tendermint/tendermint/types"
16+
"github.com/tendermint/go-wire"
17+
"io"
18+
)
19+
20+
21+
type ServiceByteTx struct {
22+
manager keys.Manager
23+
}
24+
25+
type RequestTx struct {
26+
Tx sdk.Tx `json:"tx" validate:"required"`
27+
}
28+
29+
func NewServiceByteTx(manager keys.Manager) *ServiceByteTx {
30+
return &ServiceByteTx{
31+
manager: manager, // XXX keycmd.GetKeyManager()
32+
}
33+
}
34+
35+
func (s *ServiceByteTx) RegisterByteTx(r *mux.Router) error {
36+
r.HandleFunc("/byteTx", s.ByteTx).Methods("POST")
37+
return nil
38+
}
39+
40+
41+
func (s *ServiceByteTx) RegisterqueryTx(r *mux.Router) error {
42+
r.HandleFunc("/tx/{hash}", s.queryTx).Methods("GET")
43+
return nil
44+
}
45+
46+
func (s *ServiceByteTx) ByteTx(w http.ResponseWriter, r *http.Request) {
47+
req := new(RequestTx)
48+
if err := sdk.ParseRequestAndValidateJSON(r, req); err != nil {
49+
sdk.WriteError(w, err)
50+
return
51+
}
52+
53+
tx := req.Tx
54+
55+
if sign, ok := tx.Unwrap().(keys.Signable); ok {
56+
sdk.WriteSuccess(w, hex.EncodeToString(sign.SignBytes()))
57+
return
58+
}
59+
sdk.WriteSuccess(w, "")
60+
}
61+
62+
func (s *ServiceByteTx) queryTx(w http.ResponseWriter, r *http.Request){
63+
args := mux.Vars(r)
64+
hash := args["hash"]
65+
66+
if hash == "" {
67+
sdk.WriteError(w, errors.Errorf("[%s] argument must be non-empty ", "hash"))
68+
return
69+
}
70+
// with tx, we always just parse key as hex and use to lookup
71+
hashByte, err := hex.DecodeString(cmn.StripHex(hash))
72+
73+
// get the proof -> this will be used by all prover commands
74+
node := commands.GetNode()
75+
prove := !viper.GetBool(commands.FlagTrustNode)
76+
res, err := node.Tx(hashByte, prove)
77+
if err != nil {
78+
sdk.WriteError(w, err)
79+
return
80+
}
81+
82+
// no checks if we don't get a proof
83+
if !prove {
84+
sdk.WriteSuccess(w,showTx(w,res.Height, res.Tx))
85+
return
86+
}
87+
88+
cert, err := commands.GetCertifier()
89+
if err != nil {
90+
sdk.WriteError(w, err)
91+
return
92+
}
93+
94+
check, err := client.GetCertifiedCommit(res.Height, node, cert)
95+
if err != nil {
96+
sdk.WriteError(w, err)
97+
return
98+
}
99+
err = res.Proof.Validate(check.Header.DataHash)
100+
if err != nil {
101+
sdk.WriteError(w, err)
102+
return
103+
}
104+
105+
// note that we return res.Proof.Data, not res.Tx,
106+
// as res.Proof.Validate only verifies res.Proof.Data
107+
sdk.WriteSuccess(w,showTx(w,res.Height, res.Proof.Data))
108+
}
109+
110+
// showTx parses anything that was previously registered as sdk.Tx
111+
func showTx(w io.Writer ,h int64, tx types.Tx) error {
112+
var info sdk.Tx
113+
err := wire.ReadBinaryBytes(tx, &info)
114+
if err != nil {
115+
return err
116+
}
117+
return query.FoutputProof(w,info,h)
118+
}

0 commit comments

Comments
 (0)