1a48e09e78
* Implement `eth_getPendingTransactions` Closes #244 * refactor repeatitive code * Update ethereum/rpc/namespaces/eth/api.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Update ethereum/rpc/namespaces/eth/api.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Update ethereum/rpc/namespaces/eth/api.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Update ethereum/rpc/namespaces/eth/api.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * test UnwrapEthereumMsg Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
package types
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
log "github.com/xlab/suplog"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
)
|
|
|
|
var EmptyCodeHash = crypto.Keccak256(nil)
|
|
|
|
// DecodeTxResponse decodes an protobuf-encoded byte slice into TxResponse
|
|
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
|
|
}
|
|
|
|
data := txMsgData.GetData()
|
|
if len(data) == 0 {
|
|
return &MsgEthereumTxResponse{}, nil
|
|
}
|
|
|
|
var res MsgEthereumTxResponse
|
|
|
|
err := proto.Unmarshal(data[0].GetData(), &res)
|
|
if err != nil {
|
|
return nil, sdkerrors.Wrap(err, "failed to unmarshal tx response message data")
|
|
}
|
|
|
|
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)
|
|
if err != nil {
|
|
return TransactionLogs{}, err
|
|
}
|
|
return logs, nil
|
|
}
|
|
|
|
// 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
|
|
}
|