github.com/cosmos/cosmos-sdk@v0.50.10/server/mock/tx.go (about)

     1  package mock
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	protov2 "google.golang.org/protobuf/proto"
     8  
     9  	bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
    10  	errorsmod "cosmossdk.io/errors"
    11  
    12  	cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
    13  	sdk "github.com/cosmos/cosmos-sdk/types"
    14  	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
    15  	txsigning "github.com/cosmos/cosmos-sdk/types/tx/signing"
    16  	"github.com/cosmos/cosmos-sdk/x/auth/signing"
    17  )
    18  
    19  // An sdk.Tx which is its own sdk.Msg.
    20  type KVStoreTx struct {
    21  	key     []byte
    22  	value   []byte
    23  	bytes   []byte
    24  	address sdk.AccAddress
    25  }
    26  
    27  // testPubKey is a dummy implementation of PubKey used for testing.
    28  type testPubKey struct {
    29  	address sdk.AccAddress
    30  }
    31  
    32  func (t testPubKey) Reset() { panic("not implemented") }
    33  
    34  func (t testPubKey) String() string { panic("not implemented") }
    35  
    36  func (t testPubKey) ProtoMessage() { panic("not implemented") }
    37  
    38  func (t testPubKey) Address() cryptotypes.Address { return t.address.Bytes() }
    39  
    40  func (t testPubKey) Bytes() []byte { panic("not implemented") }
    41  
    42  func (t testPubKey) VerifySignature(msg, sig []byte) bool { panic("not implemented") }
    43  
    44  func (t testPubKey) Equals(key cryptotypes.PubKey) bool { panic("not implemented") }
    45  
    46  func (t testPubKey) Type() string { panic("not implemented") }
    47  
    48  func (msg *KVStoreTx) GetSignaturesV2() (res []txsigning.SignatureV2, err error) {
    49  	res = append(res, txsigning.SignatureV2{
    50  		PubKey:   testPubKey{address: msg.address},
    51  		Data:     nil,
    52  		Sequence: 1,
    53  	})
    54  
    55  	return res, nil
    56  }
    57  
    58  func (msg *KVStoreTx) VerifySignature(msgByte, sig []byte) bool {
    59  	panic("implement me")
    60  }
    61  
    62  func (msg *KVStoreTx) Address() cryptotypes.Address {
    63  	panic("implement me")
    64  }
    65  
    66  func (msg *KVStoreTx) Bytes() []byte {
    67  	panic("implement me")
    68  }
    69  
    70  func (msg *KVStoreTx) Equals(key cryptotypes.PubKey) bool {
    71  	panic("implement me")
    72  }
    73  
    74  // dummy implementation of proto.Message
    75  func (msg *KVStoreTx) Reset()         {}
    76  func (msg *KVStoreTx) String() string { return "TODO" }
    77  func (msg *KVStoreTx) ProtoMessage()  {}
    78  
    79  var (
    80  	_ sdk.Tx                  = &KVStoreTx{}
    81  	_ sdk.Msg                 = &KVStoreTx{}
    82  	_ signing.SigVerifiableTx = &KVStoreTx{}
    83  	_ cryptotypes.PubKey      = &KVStoreTx{}
    84  	_ cryptotypes.PubKey      = &testPubKey{}
    85  )
    86  
    87  func NewTx(key, value string, accAddress sdk.AccAddress) *KVStoreTx {
    88  	bytes := fmt.Sprintf("%s=%s=%s", key, value, accAddress)
    89  	return &KVStoreTx{
    90  		key:     []byte(key),
    91  		value:   []byte(value),
    92  		bytes:   []byte(bytes),
    93  		address: accAddress,
    94  	}
    95  }
    96  
    97  func (msg *KVStoreTx) Type() string {
    98  	return "kvstore_tx"
    99  }
   100  
   101  func (msg *KVStoreTx) GetMsgs() []sdk.Msg {
   102  	return []sdk.Msg{msg}
   103  }
   104  
   105  func (msg *KVStoreTx) GetMsgsV2() ([]protov2.Message, error) {
   106  	return []protov2.Message{&bankv1beta1.MsgSend{FromAddress: msg.address.String()}}, nil // this is a hack for tests
   107  }
   108  
   109  func (msg *KVStoreTx) GetSignBytes() []byte {
   110  	return msg.bytes
   111  }
   112  
   113  // Should the app be calling this? Or only handlers?
   114  func (msg *KVStoreTx) ValidateBasic() error {
   115  	return nil
   116  }
   117  
   118  func (msg *KVStoreTx) GetSigners() ([][]byte, error) {
   119  	return nil, nil
   120  }
   121  
   122  func (msg *KVStoreTx) GetPubKeys() ([]cryptotypes.PubKey, error) { panic("GetPubKeys not implemented") }
   123  
   124  // takes raw transaction bytes and decodes them into an sdk.Tx. An sdk.Tx has
   125  // all the signatures and can be used to authenticate.
   126  func decodeTx(txBytes []byte) (sdk.Tx, error) {
   127  	var tx sdk.Tx
   128  
   129  	split := bytes.Split(txBytes, []byte("="))
   130  	switch len(split) {
   131  	case 1:
   132  		k := split[0]
   133  		tx = &KVStoreTx{k, k, txBytes, nil}
   134  	case 2:
   135  		k, v := split[0], split[1]
   136  		tx = &KVStoreTx{k, v, txBytes, nil}
   137  	case 3:
   138  		k, v, addr := split[0], split[1], split[2]
   139  		tx = &KVStoreTx{k, v, txBytes, addr}
   140  	default:
   141  		return nil, errorsmod.Wrap(sdkerrors.ErrTxDecode, "too many '='")
   142  	}
   143  
   144  	return tx, nil
   145  }