|
1 |
| -package tx_test |
| 1 | +package tx |
2 | 2 |
|
3 | 3 | import (
|
4 | 4 | "testing"
|
5 | 5 |
|
6 |
| - "github.com/cosmos/cosmos-sdk/client" |
7 |
| - "github.com/cosmos/cosmos-sdk/client/tx" |
8 | 6 | "github.com/stretchr/testify/require"
|
| 7 | + |
| 8 | + "github.com/cosmos/cosmos-sdk/client" |
| 9 | + "github.com/cosmos/cosmos-sdk/codec" |
| 10 | + "github.com/cosmos/cosmos-sdk/crypto/hd" |
| 11 | + "github.com/cosmos/cosmos-sdk/crypto/keyring" |
| 12 | + "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" |
| 13 | + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" |
| 14 | + "github.com/cosmos/cosmos-sdk/crypto/types" |
| 15 | + "github.com/cosmos/cosmos-sdk/testutil/testdata" |
| 16 | + "github.com/cosmos/cosmos-sdk/types/tx/signing" |
| 17 | + |
| 18 | + codectypes "github.com/cosmos/cosmos-sdk/codec/types" |
| 19 | + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" |
9 | 20 | )
|
10 | 21 |
|
11 |
| -func TestFactoryPrepate(t *testing.T) { |
| 22 | +func TestFactoryPrepare(t *testing.T) { |
12 | 23 | t.Parallel()
|
13 | 24 |
|
14 |
| - factory := tx.Factory{} |
| 25 | + factory := Factory{} |
15 | 26 | clientCtx := client.Context{}
|
16 | 27 |
|
17 | 28 | output, err := factory.Prepare(clientCtx.WithOffline(true))
|
18 | 29 | require.NoError(t, err)
|
19 | 30 | require.Equal(t, output, factory)
|
20 | 31 |
|
21 |
| - factory = tx.Factory{}.WithAccountRetriever(client.MockAccountRetriever{ReturnAccNum: 10, ReturnAccSeq: 1}).WithAccountNumber(5) |
| 32 | + factory = Factory{}.WithAccountRetriever(client.MockAccountRetriever{ReturnAccNum: 10, ReturnAccSeq: 1}).WithAccountNumber(5) |
22 | 33 | output, err = factory.Prepare(clientCtx.WithFrom("foo"))
|
23 | 34 | require.NoError(t, err)
|
24 | 35 | require.NotEqual(t, output, factory)
|
25 | 36 | require.Equal(t, output.AccountNumber(), uint64(5))
|
26 | 37 | require.Equal(t, output.Sequence(), uint64(1))
|
27 | 38 |
|
28 |
| - factory = tx.Factory{}.WithAccountRetriever(client.MockAccountRetriever{ReturnAccNum: 10, ReturnAccSeq: 1}) |
| 39 | + factory = Factory{}.WithAccountRetriever(client.MockAccountRetriever{ReturnAccNum: 10, ReturnAccSeq: 1}) |
29 | 40 | output, err = factory.Prepare(clientCtx.WithFrom("foo"))
|
30 | 41 | require.NoError(t, err)
|
31 | 42 | require.NotEqual(t, output, factory)
|
32 | 43 | require.Equal(t, output.AccountNumber(), uint64(10))
|
33 | 44 | require.Equal(t, output.Sequence(), uint64(1))
|
34 | 45 | }
|
| 46 | + |
| 47 | +func TestFactory_getSimPKType(t *testing.T) { |
| 48 | + // setup keyring |
| 49 | + registry := codectypes.NewInterfaceRegistry() |
| 50 | + cryptocodec.RegisterInterfaces(registry) |
| 51 | + k := keyring.NewInMemory(codec.NewProtoCodec(registry)) |
| 52 | + |
| 53 | + tests := []struct { |
| 54 | + name string |
| 55 | + fromName string |
| 56 | + genKey func(fromName string, k keyring.Keyring) error |
| 57 | + wantType types.PubKey |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "simple key", |
| 61 | + fromName: "testKey", |
| 62 | + genKey: func(fromName string, k keyring.Keyring) error { |
| 63 | + _, err := k.NewAccount(fromName, testdata.TestMnemonic, "", "", hd.Secp256k1) |
| 64 | + return err |
| 65 | + }, |
| 66 | + wantType: (*secp256k1.PubKey)(nil), |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "multisig key", |
| 70 | + fromName: "multiKey", |
| 71 | + genKey: func(fromName string, k keyring.Keyring) error { |
| 72 | + pk := multisig.NewLegacyAminoPubKey(1, []types.PubKey{&multisig.LegacyAminoPubKey{}}) |
| 73 | + _, err := k.SaveMultisig(fromName, pk) |
| 74 | + return err |
| 75 | + }, |
| 76 | + wantType: (*multisig.LegacyAminoPubKey)(nil), |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for _, tt := range tests { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + err := tt.genKey(tt.fromName, k) |
| 83 | + require.NoError(t, err) |
| 84 | + f := Factory{ |
| 85 | + keybase: k, |
| 86 | + fromName: tt.fromName, |
| 87 | + simulateAndExecute: true, |
| 88 | + } |
| 89 | + got, err := f.getSimPK() |
| 90 | + require.NoError(t, err) |
| 91 | + require.IsType(t, tt.wantType, got) |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestFactory_getSimSignatureData(t *testing.T) { |
| 97 | + tests := []struct { |
| 98 | + name string |
| 99 | + pk types.PubKey |
| 100 | + wantType any |
| 101 | + }{ |
| 102 | + { |
| 103 | + name: "simple pubkey", |
| 104 | + pk: &secp256k1.PubKey{}, |
| 105 | + wantType: (*signing.SingleSignatureData)(nil), |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "multisig pubkey", |
| 109 | + pk: &multisig.LegacyAminoPubKey{}, |
| 110 | + wantType: (*signing.MultiSignatureData)(nil), |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + for _, tt := range tests { |
| 115 | + t.Run(tt.name, func(t *testing.T) { |
| 116 | + got := Factory{}.getSimSignatureData(tt.pk) |
| 117 | + require.IsType(t, tt.wantType, got) |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
0 commit comments