Merge PR #2377: auth: Move stdsignmsg to txbuilder, add tests

This commit is contained in:
Dev Ojha 2018-09-26 06:39:05 -07:00 committed by Christopher Goes
parent 91cac96fea
commit e6a8b2278e
6 changed files with 138 additions and 39 deletions

View File

@ -56,6 +56,7 @@ BREAKING CHANGES
* [codec] \#2324 All referrences to wire have been renamed to codec. Additionally, wire.NewCodec is now codec.New().
* [types] \#2343 Make sdk.Msg have a names field, to facilitate automatic tagging.
* [baseapp] \#2366 Automatically add action tags to all messages
* [x/auth] \#2377 auth.StdSignMsg -> txbuilder.StdSignMsg
* [x/staking] \#2244 staking now holds a consensus-address-index instead of a consensus-pubkey-index
* [x/staking] \#2236 more distribution hooks for distribution

View File

@ -0,0 +1,23 @@
package context
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
)
// StdSignMsg is a convenience structure for passing along
// a Msg with the other requirements for a StdSignDoc before
// it is signed. For use in the CLI.
type StdSignMsg struct {
ChainID string `json:"chain_id"`
AccountNumber int64 `json:"account_number"`
Sequence int64 `json:"sequence"`
Fee auth.StdFee `json:"fee"`
Msgs []sdk.Msg `json:"msgs"`
Memo string `json:"memo"`
}
// get message bytes
func (msg StdSignMsg) Bytes() []byte {
return auth.StdSignBytes(msg.ChainID, msg.AccountNumber, msg.Sequence, msg.Fee, msg.Msgs, msg.Memo)
}

View File

@ -92,23 +92,23 @@ func (bldr TxBuilder) WithAccountNumber(accnum int64) TxBuilder {
// Build builds a single message to be signed from a TxBuilder given a set of
// messages. It returns an error if a fee is supplied but cannot be parsed.
func (bldr TxBuilder) Build(msgs []sdk.Msg) (auth.StdSignMsg, error) {
func (bldr TxBuilder) Build(msgs []sdk.Msg) (StdSignMsg, error) {
chainID := bldr.ChainID
if chainID == "" {
return auth.StdSignMsg{}, errors.Errorf("chain ID required but not specified")
return StdSignMsg{}, errors.Errorf("chain ID required but not specified")
}
fee := sdk.Coin{}
if bldr.Fee != "" {
parsedFee, err := sdk.ParseCoin(bldr.Fee)
if err != nil {
return auth.StdSignMsg{}, err
return StdSignMsg{}, err
}
fee = parsedFee
}
return auth.StdSignMsg{
return StdSignMsg{
ChainID: bldr.ChainID,
AccountNumber: bldr.AccountNumber,
Sequence: bldr.Sequence,
@ -120,7 +120,7 @@ func (bldr TxBuilder) Build(msgs []sdk.Msg) (auth.StdSignMsg, error) {
// Sign signs a transaction given a name, passphrase, and a single message to
// signed. An error is returned if signing fails.
func (bldr TxBuilder) Sign(name, passphrase string, msg auth.StdSignMsg) ([]byte, error) {
func (bldr TxBuilder) Sign(name, passphrase string, msg StdSignMsg) ([]byte, error) {
sig, err := MakeSignature(name, passphrase, msg)
if err != nil {
return nil, err
@ -172,7 +172,7 @@ func (bldr TxBuilder) BuildWithPubKey(name string, msgs []sdk.Msg) ([]byte, erro
// SignStdTx appends a signature to a StdTx and returns a copy of a it. If append
// is false, it replaces the signatures already attached with the new signature.
func (bldr TxBuilder) SignStdTx(name, passphrase string, stdTx auth.StdTx, appendSig bool) (signedStdTx auth.StdTx, err error) {
stdSignature, err := MakeSignature(name, passphrase, auth.StdSignMsg{
stdSignature, err := MakeSignature(name, passphrase, StdSignMsg{
ChainID: bldr.ChainID,
AccountNumber: bldr.AccountNumber,
Sequence: bldr.Sequence,
@ -195,7 +195,7 @@ func (bldr TxBuilder) SignStdTx(name, passphrase string, stdTx auth.StdTx, appen
}
// MakeSignature builds a StdSignature given key name, passphrase, and a StdSignMsg.
func MakeSignature(name, passphrase string, msg auth.StdSignMsg) (sig auth.StdSignature, err error) {
func MakeSignature(name, passphrase string, msg StdSignMsg) (sig auth.StdSignature, err error) {
keybase, err := keys.GetKeyBase()
if err != nil {
return

View File

@ -0,0 +1,81 @@
package context
import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/tendermint/tendermint/crypto/ed25519"
)
var (
priv = ed25519.GenPrivKey()
addr = sdk.AccAddress(priv.PubKey().Address())
)
func TestTxBuilderBuild(t *testing.T) {
type fields struct {
Codec *codec.Codec
AccountNumber int64
Sequence int64
Gas int64
GasAdjustment float64
SimulateGas bool
ChainID string
Memo string
Fee string
}
defaultMsg := []sdk.Msg{sdk.NewTestMsg(addr)}
tests := []struct {
fields fields
msgs []sdk.Msg
want StdSignMsg
wantErr bool
}{
{
fields{
Codec: codec.New(),
AccountNumber: 1,
Sequence: 1,
Gas: 100,
GasAdjustment: 1.1,
SimulateGas: false,
ChainID: "test-chain",
Memo: "hello",
Fee: "1steak",
},
defaultMsg,
StdSignMsg{
ChainID: "test-chain",
AccountNumber: 1,
Sequence: 1,
Memo: "hello",
Msgs: defaultMsg,
Fee: auth.NewStdFee(100, sdk.NewCoin("steak", sdk.NewInt(1))),
},
false,
},
}
for i, tc := range tests {
bldr := TxBuilder{
Codec: tc.fields.Codec,
AccountNumber: tc.fields.AccountNumber,
Sequence: tc.fields.Sequence,
Gas: tc.fields.Gas,
GasAdjustment: tc.fields.GasAdjustment,
SimulateGas: tc.fields.SimulateGas,
ChainID: tc.fields.ChainID,
Memo: tc.fields.Memo,
Fee: tc.fields.Fee,
}
got, err := bldr.Build(tc.msgs)
require.Equal(t, tc.wantErr, (err != nil), "TxBuilder.Build() error = %v, wantErr %v, tc %d", err, tc.wantErr, i)
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("TxBuilder.Build() = %v, want %v", got, tc.want)
}
}
}

View File

@ -139,23 +139,6 @@ func StdSignBytes(chainID string, accnum int64, sequence int64, fee StdFee, msgs
return sdk.MustSortJSON(bz)
}
// StdSignMsg is a convenience structure for passing along
// a Msg with the other requirements for a StdSignDoc before
// it is signed. For use in the CLI.
type StdSignMsg struct {
ChainID string `json:"chain_id"`
AccountNumber int64 `json:"account_number"`
Sequence int64 `json:"sequence"`
Fee StdFee `json:"fee"`
Msgs []sdk.Msg `json:"msgs"`
Memo string `json:"memo"`
}
// get message bytes
func (msg StdSignMsg) Bytes() []byte {
return StdSignBytes(msg.ChainID, msg.AccountNumber, msg.Sequence, msg.Fee, msg.Msgs, msg.Memo)
}
// Standard Signature
type StdSignature struct {
crypto.PubKey `json:"pub_key"` // optional

View File

@ -4,15 +4,17 @@ import (
"fmt"
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/ed25519"
)
sdk "github.com/cosmos/cosmos-sdk/types"
var (
priv = ed25519.GenPrivKey()
addr = sdk.AccAddress(priv.PubKey().Address())
)
func TestStdTx(t *testing.T) {
priv := ed25519.GenPrivKey()
addr := sdk.AccAddress(priv.PubKey().Address())
msgs := []sdk.Msg{sdk.NewTestMsg(addr)}
fee := newStdFee()
sigs := []StdSignature{}
@ -26,17 +28,26 @@ func TestStdTx(t *testing.T) {
}
func TestStdSignBytes(t *testing.T) {
priv := ed25519.GenPrivKey()
addr := sdk.AccAddress(priv.PubKey().Address())
msgs := []sdk.Msg{sdk.NewTestMsg(addr)}
fee := newStdFee()
signMsg := StdSignMsg{
"1234",
3,
6,
fee,
msgs,
"memo",
type args struct {
chainID string
accnum int64
sequence int64
fee StdFee
msgs []sdk.Msg
memo string
}
defaultFee := newStdFee()
tests := []struct {
args args
want string
}{
{
args{"1234", 3, 6, defaultFee, []sdk.Msg{sdk.NewTestMsg(addr)}, "memo"},
fmt.Sprintf("{\"account_number\":\"3\",\"chain_id\":\"1234\",\"fee\":{\"amount\":[{\"amount\":\"150\",\"denom\":\"atom\"}],\"gas\":\"5000\"},\"memo\":\"memo\",\"msgs\":[[\"%s\"]],\"sequence\":\"6\"}", addr),
},
}
for i, tc := range tests {
got := string(StdSignBytes(tc.args.chainID, tc.args.accnum, tc.args.sequence, tc.args.fee, tc.args.msgs, tc.args.memo))
require.Equal(t, tc.want, got, "Got unexpected result on test case i: %d", i)
}
require.Equal(t, fmt.Sprintf("{\"account_number\":\"3\",\"chain_id\":\"1234\",\"fee\":{\"amount\":[{\"amount\":\"150\",\"denom\":\"atom\"}],\"gas\":\"5000\"},\"memo\":\"memo\",\"msgs\":[[\"%s\"]],\"sequence\":\"6\"}", addr), string(signMsg.Bytes()))
}