Merge pull request #1582: Simplify auth.StdSignMsg fields, add testcase

* Simplify auth.StdSignMsg

* Add StdSignMsg.Bytes() test
This commit is contained in:
Christopher Goes 2018-07-07 01:26:22 +02:00 committed by Rigel
parent 723e057d95
commit 3805c35aff
3 changed files with 30 additions and 22 deletions

View File

@ -6,6 +6,9 @@
BREAKING CHANGES
* msg.GetSignBytes() returns sorted JSON (by key)
* msg.GetSignBytes() field changes
* `msg_bytes` -> `msgs`
* `fee_bytes` -> `fee`
* Update Tendermint to v0.22.0
* Default ports changed from 466xx to 266xx
* Amino JSON uses type names instead of prefix bytes

View File

@ -110,33 +110,27 @@ func (fee StdFee) Bytes() []byte {
// and the Sequence numbers for each signature (prevent
// inchain replay and enforce tx ordering per account).
type StdSignDoc struct {
ChainID string `json:"chain_id"`
AccountNumber int64 `json:"account_number"`
Sequence int64 `json:"sequence"`
FeeBytes json.RawMessage `json:"fee_bytes"`
MsgsBytes json.RawMessage `json:"msg_bytes"`
Memo string `json:"memo"`
AccountNumber int64 `json:"account_number"`
ChainID string `json:"chain_id"`
Fee json.RawMessage `json:"fee"`
Memo string `json:"memo"`
Msgs []json.RawMessage `json:"msgs"`
Sequence int64 `json:"sequence"`
}
// StdSignBytes returns the bytes to sign for a transaction.
// TODO: change the API to just take a chainID and StdTx ?
func StdSignBytes(chainID string, accnum int64, sequence int64, fee StdFee, msgs []sdk.Msg, memo string) []byte {
var msgsBytes []json.RawMessage
for _, msg := range msgs {
msgsBytes = append(msgsBytes, json.RawMessage(msg.GetSignBytes()))
}
msgBytes, err := msgCdc.MarshalJSON(msgsBytes)
if err != nil {
panic(err)
}
bz, err := msgCdc.MarshalJSON(StdSignDoc{
ChainID: chainID,
AccountNumber: accnum,
Sequence: sequence,
FeeBytes: json.RawMessage(fee.Bytes()),
MsgsBytes: json.RawMessage(msgBytes),
ChainID: chainID,
Fee: json.RawMessage(fee.Bytes()),
Memo: memo,
Msgs: msgsBytes,
Sequence: sequence,
})
if err != nil {
panic(err)

View File

@ -1,6 +1,7 @@
package auth
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
@ -10,12 +11,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// func newStdFee() StdFee {
// return NewStdFee(100,
// Coin{"atom", 150},
// )
// }
func TestStdTx(t *testing.T) {
priv := crypto.GenPrivKeyEd25519()
addr := priv.PubKey().Address()
@ -30,3 +25,19 @@ func TestStdTx(t *testing.T) {
feePayer := FeePayer(tx)
require.Equal(t, addr, feePayer)
}
func TestStdSignBytes(t *testing.T) {
priv := crypto.GenPrivKeyEd25519()
addr := priv.PubKey().Address()
msgs := []sdk.Msg{sdk.NewTestMsg(addr)}
fee := newStdFee()
signMsg := StdSignMsg{
"1234",
3,
6,
fee,
msgs,
"memo",
}
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()))
}