2020-08-03 12:47:25 -07:00
|
|
|
package tx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-08-07 16:32:22 -07:00
|
|
|
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2020-08-03 12:47:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
|
2021-07-05 07:55:51 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
|
2020-08-03 12:47:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/signing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_, _, addr1 = testdata.KeyTestPubAddr()
|
|
|
|
_, _, addr2 = testdata.KeyTestPubAddr()
|
|
|
|
|
2020-08-07 16:32:22 -07:00
|
|
|
coins = sdk.Coins{sdk.NewInt64Coin("foocoin", 10)}
|
|
|
|
gas = uint64(10000)
|
|
|
|
msg = testdata.NewTestMsg(addr1, addr2)
|
|
|
|
memo = "foo"
|
|
|
|
timeout = uint64(10)
|
2020-08-03 12:47:25 -07:00
|
|
|
)
|
|
|
|
|
2020-08-24 07:41:08 -07:00
|
|
|
func buildTx(t *testing.T, bldr *wrapper) {
|
2020-08-03 12:47:25 -07:00
|
|
|
bldr.SetFeeAmount(coins)
|
|
|
|
bldr.SetGasLimit(gas)
|
|
|
|
bldr.SetMemo(memo)
|
2020-08-07 16:32:22 -07:00
|
|
|
bldr.SetTimeoutHeight(timeout)
|
2020-08-03 12:47:25 -07:00
|
|
|
require.NoError(t, bldr.SetMsgs(msg))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLegacyAminoJSONHandler_GetSignBytes(t *testing.T) {
|
2020-09-21 09:48:28 -07:00
|
|
|
bldr := newBuilder()
|
2020-08-03 12:47:25 -07:00
|
|
|
buildTx(t, bldr)
|
|
|
|
tx := bldr.GetTx()
|
|
|
|
|
|
|
|
var (
|
|
|
|
chainId = "test-chain"
|
|
|
|
accNum uint64 = 7
|
|
|
|
seqNum uint64 = 7
|
|
|
|
)
|
|
|
|
|
|
|
|
handler := signModeLegacyAminoJSONHandler{}
|
|
|
|
signingData := signing.SignerData{
|
2020-08-21 07:20:47 -07:00
|
|
|
ChainID: chainId,
|
|
|
|
AccountNumber: accNum,
|
|
|
|
Sequence: seqNum,
|
2020-08-03 12:47:25 -07:00
|
|
|
}
|
|
|
|
signBz, err := handler.GetSignBytes(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-22 07:35:18 -07:00
|
|
|
expectedSignBz := legacytx.StdSignBytes(chainId, accNum, seqNum, timeout, legacytx.StdFee{
|
2020-08-03 12:47:25 -07:00
|
|
|
Amount: coins,
|
|
|
|
Gas: gas,
|
|
|
|
}, []sdk.Msg{msg}, memo)
|
|
|
|
|
|
|
|
require.Equal(t, expectedSignBz, signBz)
|
|
|
|
|
|
|
|
// expect error with wrong sign mode
|
|
|
|
_, err = handler.GetSignBytes(signingtypes.SignMode_SIGN_MODE_DIRECT, signingData, tx)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// expect error with extension options
|
2020-09-21 09:48:28 -07:00
|
|
|
bldr = newBuilder()
|
2020-08-03 12:47:25 -07:00
|
|
|
buildTx(t, bldr)
|
|
|
|
any, err := cdctypes.NewAnyWithValue(testdata.NewTestMsg())
|
|
|
|
require.NoError(t, err)
|
|
|
|
bldr.tx.Body.ExtensionOptions = []*cdctypes.Any{any}
|
|
|
|
tx = bldr.GetTx()
|
|
|
|
signBz, err = handler.GetSignBytes(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// expect error with non-critical extension options
|
2020-09-21 09:48:28 -07:00
|
|
|
bldr = newBuilder()
|
2020-08-03 12:47:25 -07:00
|
|
|
buildTx(t, bldr)
|
|
|
|
bldr.tx.Body.NonCriticalExtensionOptions = []*cdctypes.Any{any}
|
|
|
|
tx = bldr.GetTx()
|
|
|
|
signBz, err = handler.GetSignBytes(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLegacyAminoJSONHandler_DefaultMode(t *testing.T) {
|
|
|
|
handler := signModeLegacyAminoJSONHandler{}
|
|
|
|
require.Equal(t, signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, handler.DefaultMode())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLegacyAminoJSONHandler_Modes(t *testing.T) {
|
|
|
|
handler := signModeLegacyAminoJSONHandler{}
|
|
|
|
require.Equal(t, []signingtypes.SignMode{signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON}, handler.Modes())
|
|
|
|
}
|