Skip to content

Commit

Permalink
implement /txs/encode endpoint for backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
yun-yeo committed Sep 27, 2021
1 parent 4f37d36 commit 0485745
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 9 deletions.
2 changes: 1 addition & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions client/docs/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1098,10 +1098,10 @@ paths:
deprecated: true
tags:
- Transactions
summary: Encode a transaction to the Amino wire format
summary: Encode a legacy transaction to the Proto wire format
description: >-
Encode a transaction (signed or not) from JSON to base64-encoded Amino
serialized bytes
Encode a legacy transaction (signed or not) from JSON to base64-encoded
Proto serialized bytes
consumes:
- application/json
produces:
Expand Down Expand Up @@ -1161,6 +1161,17 @@ paths:
sequence:
type: string
example: '0'
sequences:
required: false
type: array
items:
type: string
example: '1'
fee_granter:
type: string
description: bech32 encoded address
example: terra1wg2mlrxdmnnkkykgqg4znky86nyrtc45q336yv
required: false
responses:
'200':
description: The tx was successfully decoded and re-encoded
Expand All @@ -1169,7 +1180,7 @@ paths:
properties:
tx:
type: string
example: The base64-encoded Amino-serialized bytes for the tx
example: The base64-encoded Proto-serialized bytes for the tx
'400':
description: The tx was malformed
'500':
Expand Down
15 changes: 12 additions & 3 deletions client/docs/swagger_legacy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ paths:
deprecated: true
tags:
- Transactions
summary: Encode a transaction to the Amino wire format
description: Encode a transaction (signed or not) from JSON to base64-encoded Amino serialized bytes
summary: Encode a legacy transaction to the Proto wire format
description: Encode a legacy transaction (signed or not) from JSON to base64-encoded Proto serialized bytes
consumes:
- application/json
produces:
Expand All @@ -356,6 +356,15 @@ paths:
properties:
tx:
$ref: "#/definitions/StdTx"
sequences:
required: false
type: array
items:
type: string
example: "1"
fee_granter:
required: false
$ref: "#/definitions/Address"
responses:
200:
description: The tx was successfully decoded and re-encoded
Expand All @@ -364,7 +373,7 @@ paths:
properties:
tx:
type: string
example: The base64-encoded Amino-serialized bytes for the tx
example: The base64-encoded Proto-serialized bytes for the tx
400:
description: The tx was malformed
500:
Expand Down
2 changes: 1 addition & 1 deletion custom/auth/client/rest/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func BroadcastTxRequest(clientCtx client.Context) http.HandlerFunc {
return
}

// compute signature bytes
// compute tx bytes
txBytes, err := clientCtx.TxConfig.TxEncoder()(txBuilder.GetTx())
if rest.CheckInternalServerError(w, err) {
return
Expand Down
112 changes: 112 additions & 0 deletions custom/auth/client/rest/encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package rest

import (
"errors"
"io/ioutil"
"net/http"

"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx"
)

// EncodeReq defines a tx encode request.
type EncodeReq struct {
Tx legacytx.StdTx `json:"tx" yaml:"tx"`
Sequences []uint64 `json:"sequences" yaml:"sequences"`
FeeGranter string `json:"fee_granter" yaml:"fee_granter"`
}

// EncodeResq defines a tx encode response.
type EncodeResq struct {
Tx []byte `json:"tx" yaml:"tx"`
}

var _ codectypes.UnpackInterfacesMessage = EncodeReq{}

// UnpackInterfaces implements the UnpackInterfacesMessage interface.
func (m EncodeReq) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
return m.Tx.UnpackInterfaces(unpacker)
}

// EncodeTxRequest implements a tx encode handler that is responsible
// for encoding a legacy tx into new format tx bytes.
func EncodeTxRequest(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req EncodeReq

body, err := ioutil.ReadAll(r.Body)
if rest.CheckBadRequestError(w, err) {
return
}

// NOTE: amino is used intentionally here, don't migrate it!
err = clientCtx.LegacyAmino.UnmarshalJSON(body, &req)
if err != nil {
if rest.CheckBadRequestError(w, err) {
return
}
}

txBuilder := clientCtx.TxConfig.NewTxBuilder()
txBuilder.SetFeeAmount(req.Tx.GetFee())
txBuilder.SetGasLimit(req.Tx.GetGas())
txBuilder.SetMemo(req.Tx.GetMemo())
if err := txBuilder.SetMsgs(req.Tx.GetMsgs()...); rest.CheckBadRequestError(w, err) {
return
}

txBuilder.SetTimeoutHeight(req.Tx.GetTimeoutHeight())
if req.FeeGranter != "" {
addr, err := sdk.AccAddressFromBech32(req.FeeGranter)
if rest.CheckBadRequestError(w, err) {
return
}

txBuilder.SetFeeGranter(addr)
}

signatures, err := req.Tx.GetSignaturesV2()
if rest.CheckBadRequestError(w, err) {
return
}

// if sequence is not given, try fetch from the chain
if len(req.Sequences) == 0 {
for _, sig := range signatures {
_, seq, err := clientCtx.AccountRetriever.GetAccountNumberSequence(clientCtx, sdk.AccAddress(sig.PubKey.Address().Bytes()))
if rest.CheckBadRequestError(w, err) {
return
}
req.Sequences = append(req.Sequences, seq)
}
}

// check the sequence nubmer is equal with the signature nubmer
if len(signatures) != len(req.Sequences) {
rest.CheckBadRequestError(w, errors.New("Must provide each signers's sequence number"))
return
}

// fill sequence number to new signature
for i, seq := range req.Sequences {
signatures[i].Sequence = seq
}

if err := txBuilder.SetSignatures(signatures...); rest.CheckBadRequestError(w, err) {
return
}

// compute tx bytes
txBytes, err := clientCtx.TxConfig.TxEncoder()(txBuilder.GetTx())
if rest.CheckInternalServerError(w, err) {
return
}

rest.PostProcessResponseBare(w, clientCtx, EncodeResq{
Tx: txBytes,
})
}
}
1 change: 1 addition & 0 deletions custom/auth/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ import (
func RegisterTxRoutes(clientCtx client.Context, rtr *mux.Router) {
r := clientrest.WithHTTPDeprecationHeaders(rtr)
r.HandleFunc("/txs/estimate_fee", EstimateTxFeeRequestHandlerFn(clientCtx)).Methods("POST")
r.HandleFunc("/txs/encode", EncodeTxRequest(clientCtx)).Methods("POST")
r.HandleFunc("/txs", BroadcastTxRequest(clientCtx)).Methods("POST")
}

0 comments on commit 0485745

Please sign in to comment.