2018-08-09 01:06:34 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2021-07-12 17:42:53 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
log "github.com/xlab/suplog"
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2020-07-02 15:19:48 +00:00
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-06-21 13:09:23 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
var EmptyCodeHash = crypto.Keccak256(nil)
|
2019-10-22 16:40:34 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
// DecodeTxResponse decodes an protobuf-encoded byte slice into TxResponse
|
2021-04-18 15:54:18 +00:00
|
|
|
func DecodeTxResponse(in []byte) (*MsgEthereumTxResponse, error) {
|
|
|
|
var txMsgData sdk.TxMsgData
|
|
|
|
if err := proto.Unmarshal(in, &txMsgData); err != nil {
|
|
|
|
log.WithError(err).Errorln("failed to unmarshal TxMsgData")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-21 13:09:23 +00:00
|
|
|
data := txMsgData.GetData()
|
|
|
|
if len(data) == 0 {
|
2021-04-18 15:54:18 +00:00
|
|
|
return &MsgEthereumTxResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var res MsgEthereumTxResponse
|
|
|
|
|
2021-06-21 13:09:23 +00:00
|
|
|
err := proto.Unmarshal(data[0].GetData(), &res)
|
2021-04-18 15:54:18 +00:00
|
|
|
if err != nil {
|
2021-06-21 13:09:23 +00:00
|
|
|
return nil, sdkerrors.Wrap(err, "failed to unmarshal tx response message data")
|
2021-04-18 15:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeTransactionLogs encodes TransactionLogs slice into a protobuf-encoded byte slice.
|
|
|
|
func EncodeTransactionLogs(res *TransactionLogs) ([]byte, error) {
|
|
|
|
return proto.Marshal(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeTxResponse decodes an protobuf-encoded byte slice into TransactionLogs
|
|
|
|
func DecodeTransactionLogs(data []byte) (TransactionLogs, error) {
|
|
|
|
var logs TransactionLogs
|
|
|
|
err := proto.Unmarshal(data, &logs)
|
2020-04-01 18:49:21 +00:00
|
|
|
if err != nil {
|
2021-04-18 15:54:18 +00:00
|
|
|
return TransactionLogs{}, err
|
2019-10-22 16:40:34 +00:00
|
|
|
}
|
2021-04-18 15:54:18 +00:00
|
|
|
return logs, nil
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
2021-07-12 17:42:53 +00:00
|
|
|
|
|
|
|
// UnwrapEthereumMsg extract MsgEthereumTx from wrapping sdk.Tx
|
|
|
|
func UnwrapEthereumMsg(tx *sdk.Tx) (*MsgEthereumTx, error) {
|
|
|
|
if tx == nil {
|
|
|
|
return nil, fmt.Errorf("invalid tx: nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len((*tx).GetMsgs()) != 1 {
|
|
|
|
return nil, fmt.Errorf("invalid tx type: %T", tx)
|
|
|
|
}
|
|
|
|
msg, ok := (*tx).GetMsgs()[0].(*MsgEthereumTx)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid tx type: %T", tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|