-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
R4R: validate sign tx request's body #3179
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,22 @@ | ||
package rest | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/context" | ||
"github.com/cosmos/cosmos-sdk/client/utils" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/keyerror" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth" | ||
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" | ||
) | ||
|
||
// SignBody defines the properties of a sign request's body. | ||
type SignBody struct { | ||
Tx auth.StdTx `json:"tx"` | ||
LocalAccountName string `json:"name"` | ||
Password string `json:"password"` | ||
ChainID string `json:"chain_id"` | ||
AccountNumber uint64 `json:"account_number"` | ||
Sequence uint64 `json:"sequence"` | ||
AppendSig bool `json:"append_sig"` | ||
Tx auth.StdTx `json:"tx"` | ||
AppendSig bool `json:"append_sig"` | ||
BaseReq utils.BaseReq `json:"base_req"` | ||
} | ||
|
||
// nolint: unparam | ||
|
@@ -30,21 +26,34 @@ func SignTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha | |
return func(w http.ResponseWriter, r *http.Request) { | ||
var m SignBody | ||
|
||
body, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
if err := utils.ReadRESTReq(w, r, cdc, &m); err != nil { | ||
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
err = cdc.UnmarshalJSON(body, &m) | ||
if err != nil { | ||
|
||
if !m.BaseReq.ValidateBasic(w) { | ||
return | ||
} | ||
|
||
// validate tx | ||
// discard error if it's CodeUnauthorized as the tx comes with no signatures | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little bit confused as to why CodeUnautherized implies that there tx comes with no signatures... maybe we should have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Amended, please review again
alessio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err := m.Tx.ValidateBasic(); err != nil && err.Code() != sdk.CodeNoSignatures { | ||
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
||
txBldr := authtxb.NewTxBuilder(utils.GetTxEncoder(cdc), m.AccountNumber, | ||
m.Sequence, m.Tx.Fee.Gas, 1.0, false, m.ChainID, m.Tx.GetMemo(), m.Tx.Fee.Amount) | ||
txBldr := authtxb.NewTxBuilder( | ||
utils.GetTxEncoder(cdc), | ||
m.BaseReq.AccountNumber, | ||
m.BaseReq.Sequence, | ||
m.Tx.Fee.Gas, | ||
1.0, | ||
false, | ||
m.BaseReq.ChainID, | ||
m.Tx.GetMemo(), | ||
m.Tx.Fee.Amount) | ||
|
||
signedTx, err := txBldr.SignStdTx(m.LocalAccountName, m.Password, m.Tx, m.AppendSig) | ||
signedTx, err := txBldr.SignStdTx(m.BaseReq.Name, m.BaseReq.Password, m.Tx, m.AppendSig) | ||
if keyerror.IsErrKeyNotFound(err) { | ||
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) | ||
return | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++