Skip to content

Commit

Permalink
add byteTx
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent.ch.cn committed Mar 13, 2018
1 parent 31a6b40 commit bda15e1
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/iris/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/commands"
rest "github.com/cosmos/cosmos-sdk/client/rest"
byteTx "github.com/irisnet/iris-hub/rest"
coinrest "github.com/cosmos/cosmos-sdk/modules/coin/rest"
noncerest "github.com/cosmos/cosmos-sdk/modules/nonce/rest"
rolerest "github.com/cosmos/cosmos-sdk/modules/roles/rest"
Expand Down Expand Up @@ -46,6 +47,7 @@ func cmdRestServer(cmd *cobra.Command, args []string) error {
rootDir := viper.GetString(cli.HomeFlag)
keyMan := client.GetKeyManager(rootDir)
serviceKeys := rest.NewServiceKeys(keyMan)
serviceByteTx := byteTx.NewServiceByteTx(keyMan)
serviceTxs := rest.NewServiceTxs(commands.GetNode())

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

// Iris transfer Tx to byte[]
serviceByteTx.RegisterByteTx,

// Nonce query handler
noncerest.RegisterQueryNonce,

Expand Down
77 changes: 77 additions & 0 deletions cmd/iris/rest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"fmt"
"log"
"net/http"

"github.com/spf13/viper"
"github.com/gorilla/mux"
"testing"
"github.com/tendermint/tmlibs/cli"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/commands"
rest "github.com/cosmos/cosmos-sdk/client/rest"
byteTx "github.com/irisnet/iris-hub/rest"
coinrest "github.com/cosmos/cosmos-sdk/modules/coin/rest"
noncerest "github.com/cosmos/cosmos-sdk/modules/nonce/rest"
rolerest "github.com/cosmos/cosmos-sdk/modules/roles/rest"

stakerest "github.com/cosmos/gaia/modules/stake/rest"
)

func TestRest(t *testing.T) {


router := mux.NewRouter()

rootDir := viper.GetString(cli.HomeFlag)
keyMan := client.GetKeyManager(rootDir)
serviceKeys := rest.NewServiceKeys(keyMan)
serviceByteTx := byteTx.NewServiceByteTx(keyMan)
serviceTxs := rest.NewServiceTxs(commands.GetNode())


routeRegistrars := []func(*mux.Router) error{
// rest.Keys handlers
serviceKeys.RegisterCRUD,

// Coin handlers (Send, Query, SearchSent)
coinrest.RegisterAll,

// Roles createRole handler
rolerest.RegisterCreateRole,

// Iris sign transactions handler
serviceKeys.RegisterSignTx,
// Iris post transaction handler
serviceTxs.RegisterPostTx,

// Iris transfer Tx to byte[]
serviceByteTx.RegisterByteTx,

// Nonce query handler
noncerest.RegisterQueryNonce,

// Staking query handlers
stakerest.RegisterQueryCandidate,
stakerest.RegisterQueryCandidates,
stakerest.RegisterQueryDelegatorBond,
stakerest.RegisterQueryDelegatorCandidates,
// Staking tx builders
stakerest.RegisterDelegate,
stakerest.RegisterUnbond,
}

for _, routeRegistrar := range routeRegistrars {
if err := routeRegistrar(router); err != nil {
log.Fatal(err)
}
}

addr := fmt.Sprintf(":%d", 8080)

log.Printf("Serving on %q", addr)
http.ListenAndServe(addr, router)

}
118 changes: 118 additions & 0 deletions rest/byteTx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package rest

import (
"github.com/gorilla/mux"
"github.com/tendermint/go-crypto/keys"
"net/http"
sdk "github.com/cosmos/cosmos-sdk"
"encoding/hex"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client/commands"
"github.com/pkg/errors"
cmn "github.com/tendermint/tmlibs/common"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/commands/query"
"github.com/tendermint/tendermint/types"
"github.com/tendermint/go-wire"
"io"
)


type ServiceByteTx struct {
manager keys.Manager
}

type RequestTx struct {
Tx sdk.Tx `json:"tx" validate:"required"`
}

func NewServiceByteTx(manager keys.Manager) *ServiceByteTx {
return &ServiceByteTx{
manager: manager, // XXX keycmd.GetKeyManager()
}
}

func (s *ServiceByteTx) RegisterByteTx(r *mux.Router) error {
r.HandleFunc("/byteTx", s.ByteTx).Methods("POST")
return nil
}


func (s *ServiceByteTx) RegisterqueryTx(r *mux.Router) error {
r.HandleFunc("/tx/{hash}", s.queryTx).Methods("GET")
return nil
}

func (s *ServiceByteTx) ByteTx(w http.ResponseWriter, r *http.Request) {
req := new(RequestTx)
if err := sdk.ParseRequestAndValidateJSON(r, req); err != nil {
sdk.WriteError(w, err)
return
}

tx := req.Tx

if sign, ok := tx.Unwrap().(keys.Signable); ok {
sdk.WriteSuccess(w, hex.EncodeToString(sign.SignBytes()))
return
}
sdk.WriteSuccess(w, "")
}

func (s *ServiceByteTx) queryTx(w http.ResponseWriter, r *http.Request){
args := mux.Vars(r)
hash := args["hash"]

if hash == "" {
sdk.WriteError(w, errors.Errorf("[%s] argument must be non-empty ", "hash"))
return
}
// with tx, we always just parse key as hex and use to lookup
hashByte, err := hex.DecodeString(cmn.StripHex(hash))

// get the proof -> this will be used by all prover commands
node := commands.GetNode()
prove := !viper.GetBool(commands.FlagTrustNode)
res, err := node.Tx(hashByte, prove)
if err != nil {
sdk.WriteError(w, err)
return
}

// no checks if we don't get a proof
if !prove {
sdk.WriteSuccess(w,showTx(w,res.Height, res.Tx))
return
}

cert, err := commands.GetCertifier()
if err != nil {
sdk.WriteError(w, err)
return
}

check, err := client.GetCertifiedCommit(res.Height, node, cert)
if err != nil {
sdk.WriteError(w, err)
return
}
err = res.Proof.Validate(check.Header.DataHash)
if err != nil {
sdk.WriteError(w, err)
return
}

// note that we return res.Proof.Data, not res.Tx,
// as res.Proof.Validate only verifies res.Proof.Data
sdk.WriteSuccess(w,showTx(w,res.Height, res.Proof.Data))
}

// showTx parses anything that was previously registered as sdk.Tx
func showTx(w io.Writer ,h int64, tx types.Tx) error {
var info sdk.Tx
err := wire.ReadBinaryBytes(tx, &info)
if err != nil {
return err
}
return query.FoutputProof(w,info,h)
}

0 comments on commit bda15e1

Please sign in to comment.