-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #5023: Implement Gaia Tx Decode
- Loading branch information
1 parent
936cffe
commit ab6d0a0
Showing
7 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cli | ||
|
||
import ( | ||
"encoding/base64" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/tendermint/go-amino" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/context" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
) | ||
|
||
// txDecodeRespTx implements a simple Stringer wrapper for a decoded StdTx. | ||
type txDecodeRespTx authtypes.StdTx | ||
|
||
func (tx txDecodeRespTx) String() string { | ||
return tx.String() | ||
} | ||
|
||
// GetDecodeCommand returns the decode command to take Amino-serialized bytes | ||
// and turn it into a JSONified transaction. | ||
func GetDecodeCommand(codec *amino.Codec) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "decode [amino-byte-string]", | ||
Short: "Decode an amino-encoded transaction string", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
cliCtx := context.NewCLIContext().WithCodec(codec) | ||
|
||
txBytes, err := base64.StdEncoding.DecodeString(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var stdTx authtypes.StdTx | ||
err = cliCtx.Codec.UnmarshalBinaryLengthPrefixed(txBytes, &stdTx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return cliCtx.PrintOutput(txDecodeRespTx(stdTx)) | ||
}, | ||
} | ||
|
||
return client.PostCommands(cmd)[0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package rest | ||
|
||
import ( | ||
"encoding/base64" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/context" | ||
"github.com/cosmos/cosmos-sdk/types/rest" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
) | ||
|
||
type ( | ||
// DecodeReq defines a tx decoding request. | ||
DecodeReq struct { | ||
Tx string `json:"tx"` | ||
} | ||
|
||
// DecodeResp defines a tx decoding response. | ||
DecodeResp authtypes.StdTx | ||
) | ||
|
||
// DecodeTxRequestHandlerFn returns the decode tx REST handler. In particular, | ||
// it takes base64-decoded bytes, decodes it from the Amino wire protocol, | ||
// and responds with a json-formatted transaction. | ||
func DecodeTxRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
var req DecodeReq | ||
|
||
body, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
||
err = cliCtx.Codec.UnmarshalJSON(body, &req) | ||
if err != nil { | ||
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
||
txBytes, err := base64.StdEncoding.DecodeString(req.Tx) | ||
if err != nil { | ||
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
||
var stdTx authtypes.StdTx | ||
err = cliCtx.Codec.UnmarshalBinaryLengthPrefixed(txBytes, &stdTx) | ||
if err != nil { | ||
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
||
response := DecodeResp(stdTx) | ||
rest.PostProcessResponse(w, cliCtx, response) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters