cosmos-sdk/x/auth/tx/direct.go
Amaury Martiny 3d969a1ffd
Implement gRPC Simulate endpoint (#7035)
* Implement simulate endpoint

* Add GetProtoTx()

* Add signing in test

* Add txBuilderFromProto

* Remove stray println

* Update to master

* Merge master

* Fix tests

* Make tests pass

* Integrate in router

* Make proto-gen

* Fix lint

* Really fix lint

* Refactor to fix import cycles

* Rename builder -> wrapper

* Update proto/cosmos/base/reflection/v1beta1/reflection.proto

* Fix after merge

* t->w

Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
2020-08-24 14:41:08 +00:00

56 lines
1.7 KiB
Go

package tx
import (
"fmt"
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
sdk "github.com/cosmos/cosmos-sdk/types"
types "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
)
// signModeDirectHandler defines the SIGN_MODE_DIRECT SignModeHandler
type signModeDirectHandler struct{}
var _ signing.SignModeHandler = signModeDirectHandler{}
// DefaultMode implements SignModeHandler.DefaultMode
func (signModeDirectHandler) DefaultMode() signingtypes.SignMode {
return signingtypes.SignMode_SIGN_MODE_DIRECT
}
// Modes implements SignModeHandler.Modes
func (signModeDirectHandler) Modes() []signingtypes.SignMode {
return []signingtypes.SignMode{signingtypes.SignMode_SIGN_MODE_DIRECT}
}
// GetSignBytes implements SignModeHandler.GetSignBytes
func (signModeDirectHandler) GetSignBytes(mode signingtypes.SignMode, data signing.SignerData, tx sdk.Tx) ([]byte, error) {
if mode != signingtypes.SignMode_SIGN_MODE_DIRECT {
return nil, fmt.Errorf("expected %s, got %s", signingtypes.SignMode_SIGN_MODE_DIRECT, mode)
}
protoTx, ok := tx.(*wrapper)
if !ok {
return nil, fmt.Errorf("can only handle a protobuf Tx, got %T", tx)
}
bodyBz := protoTx.getBodyBytes()
authInfoBz := protoTx.getAuthInfoBytes()
return DirectSignBytes(bodyBz, authInfoBz, data.ChainID, data.AccountNumber)
}
// DirectSignBytes returns the SIGN_MODE_DIRECT sign bytes for the provided TxBody bytes, AuthInfo bytes, chain ID,
// account number and sequence.
func DirectSignBytes(bodyBytes, authInfoBytes []byte, chainID string, accnum uint64) ([]byte, error) {
signDoc := types.SignDoc{
BodyBytes: bodyBytes,
AuthInfoBytes: authInfoBytes,
ChainId: chainID,
AccountNumber: accnum,
}
return signDoc.Marshal()
}