github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/sdk/testutils/testutils.go (about) 1 package testutils 2 3 import ( 4 "testing" 5 6 "github.com/gnolang/gno/tm2/pkg/amino" 7 "github.com/gnolang/gno/tm2/pkg/crypto" 8 "github.com/gnolang/gno/tm2/pkg/crypto/secp256k1" 9 "github.com/gnolang/gno/tm2/pkg/std" 10 "github.com/stretchr/testify/require" 11 ) 12 13 // msg type for testing 14 type TestMsg struct { 15 Signers []crypto.Address 16 } 17 18 var _ std.Msg = &TestMsg{} 19 20 func NewTestMsg(addrs ...crypto.Address) *TestMsg { 21 return &TestMsg{ 22 Signers: addrs, 23 } 24 } 25 26 func (msg *TestMsg) Route() string { return "TestMsg" } 27 func (msg *TestMsg) Type() string { return "Test message" } 28 func (msg *TestMsg) GetSignBytes() []byte { 29 bz, err := amino.MarshalJSON(msg.Signers) 30 if err != nil { 31 panic(err) 32 } 33 return std.MustSortJSON(bz) 34 } 35 func (msg *TestMsg) ValidateBasic() error { return nil } 36 func (msg *TestMsg) GetSigners() []crypto.Address { 37 return msg.Signers 38 } 39 40 // ---------------------------------------- 41 // Utility Methods 42 43 func NewTestFee() std.Fee { 44 return std.NewFee(50000, std.NewCoin("atom", 150)) 45 } 46 47 // coins to more than cover the fee 48 func NewTestCoins() std.Coins { 49 return std.Coins{std.NewCoin("atom", 10000000)} 50 } 51 52 func KeyTestPubAddr() (crypto.PrivKey, crypto.PubKey, crypto.Address) { 53 key := secp256k1.GenPrivKey() 54 pub := key.PubKey() 55 addr := pub.Address() 56 return key, pub, addr 57 } 58 59 func NewTestTx( 60 t *testing.T, 61 chainID string, 62 msgs []std.Msg, 63 privs []crypto.PrivKey, 64 accNums []uint64, 65 seqs []uint64, 66 fee std.Fee, 67 ) std.Tx { 68 t.Helper() 69 70 sigs := make([]std.Signature, len(privs)) 71 for i, priv := range privs { 72 signBytes, err := std.GetSignaturePayload(std.SignDoc{ 73 ChainID: chainID, 74 AccountNumber: accNums[i], 75 Sequence: seqs[i], 76 Fee: fee, 77 Msgs: msgs, 78 }) 79 require.NoError(t, err) 80 81 sig, err := priv.Sign(signBytes) 82 if err != nil { 83 panic(err) 84 } 85 86 sigs[i] = std.Signature{PubKey: priv.PubKey(), Signature: sig} 87 } 88 89 tx := std.NewTx(msgs, fee, sigs, "") 90 return tx 91 } 92 93 func NewTestTxWithMemo( 94 t *testing.T, 95 chainID string, 96 msgs []std.Msg, 97 privs []crypto.PrivKey, 98 accNums []uint64, 99 seqs []uint64, 100 fee std.Fee, 101 memo string, 102 ) std.Tx { 103 t.Helper() 104 105 sigs := make([]std.Signature, len(privs)) 106 for i, priv := range privs { 107 signBytes, err := std.GetSignaturePayload(std.SignDoc{ 108 ChainID: chainID, 109 AccountNumber: accNums[i], 110 Sequence: seqs[i], 111 Fee: fee, 112 Msgs: msgs, 113 Memo: memo, 114 }) 115 require.NoError(t, err) 116 117 sig, err := priv.Sign(signBytes) 118 if err != nil { 119 panic(err) 120 } 121 122 sigs[i] = std.Signature{PubKey: priv.PubKey(), Signature: sig} 123 } 124 125 tx := std.NewTx(msgs, fee, sigs, memo) 126 return tx 127 } 128 129 func NewTestTxWithSignBytes(msgs []std.Msg, privs []crypto.PrivKey, fee std.Fee, signBytes []byte, memo string) std.Tx { 130 sigs := make([]std.Signature, len(privs)) 131 for i, priv := range privs { 132 sig, err := priv.Sign(signBytes) 133 if err != nil { 134 panic(err) 135 } 136 137 sigs[i] = std.Signature{PubKey: priv.PubKey(), Signature: sig} 138 } 139 140 tx := std.NewTx(msgs, fee, sigs, memo) 141 return tx 142 } 143 144 func TestAddress(name string) crypto.Address { 145 if len(name) > crypto.AddressSize { 146 panic("address name cannot be greater than crypto.AddressSize bytes") 147 } 148 addr := crypto.Address{} 149 // TODO: use strings.RepeatString or similar. 150 // NOTE: I miss python's "".Join(). 151 blanks := "____________________" 152 copy(addr[:], []byte(blanks)) 153 copy(addr[:], []byte(name)) 154 return addr 155 } 156 157 func TestBech32Address(name string) crypto.Bech32Address { 158 return TestAddress(name).Bech32() 159 }