forked from evmos/ethermint
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfa90f9
commit 11ece47
Showing
37 changed files
with
571 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package evmv1 | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
ethtypes "github.com/ethereum/go-ethereum/core/types" | ||
"github.com/evmos/ethermint/types" | ||
) | ||
|
||
// GetChainID returns the chain id field from the AccessListTx | ||
func (tx *AccessListTx) GetChainID() *big.Int { | ||
return stringToBigInt(tx.GetChainId()) | ||
} | ||
|
||
// GetAccessList returns the AccessList field. | ||
func (tx *AccessListTx) GetAccessList() ethtypes.AccessList { | ||
if tx.Accesses == nil { | ||
return nil | ||
} | ||
var ethAccessList ethtypes.AccessList | ||
|
||
for _, tuple := range tx.Accesses { | ||
storageKeys := make([]common.Hash, len(tuple.StorageKeys)) | ||
|
||
for i := range tuple.StorageKeys { | ||
storageKeys[i] = common.HexToHash(tuple.StorageKeys[i]) | ||
} | ||
|
||
ethAccessList = append(ethAccessList, ethtypes.AccessTuple{ | ||
Address: common.HexToAddress(tuple.Address), | ||
StorageKeys: storageKeys, | ||
}) | ||
} | ||
|
||
return ethAccessList | ||
} | ||
|
||
// AsEthereumData returns an AccessListTx transaction tx from the proto-formatted | ||
// TxData defined on the Cosmos EVM. | ||
func (tx *AccessListTx) AsEthereumData() ethtypes.TxData { | ||
v, r, s := tx.GetRawSignatureValues() | ||
return ðtypes.AccessListTx{ | ||
ChainID: tx.GetChainID(), | ||
Nonce: tx.GetNonce(), | ||
GasPrice: stringToBigInt(tx.GetGasPrice()), | ||
Gas: tx.GetGas(), | ||
To: stringToAddress(tx.GetTo()), | ||
Value: stringToBigInt(tx.GetValue()), | ||
Data: tx.GetData(), | ||
AccessList: tx.GetAccessList(), | ||
V: v, | ||
R: r, | ||
S: s, | ||
} | ||
} | ||
|
||
// GetRawSignatureValues returns the V, R, S signature values of the transaction. | ||
// The return values should not be modified by the caller. | ||
func (tx *AccessListTx) GetRawSignatureValues() (v, r, s *big.Int) { | ||
return types.RawSignatureValues(tx.V, tx.R, tx.S) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright Tharsis Labs Ltd.(Evmos) | ||
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) | ||
|
||
package evmv1 | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
ethtypes "github.com/ethereum/go-ethereum/core/types" | ||
"github.com/evmos/ethermint/types" | ||
) | ||
|
||
// GetChainID returns the chain id field from the DynamicFeeTx | ||
func (tx *DynamicFeeTx) GetChainID() *big.Int { | ||
return stringToBigInt(tx.GetChainId()) | ||
} | ||
|
||
// AsEthereumData returns an DynamicFeeTx transaction tx from the proto-formatted | ||
// TxData defined on the Cosmos EVM. | ||
func (tx *DynamicFeeTx) AsEthereumData() ethtypes.TxData { | ||
v, r, s := tx.GetRawSignatureValues() | ||
return ðtypes.DynamicFeeTx{ | ||
ChainID: tx.GetChainID(), | ||
Nonce: tx.GetNonce(), | ||
GasTipCap: stringToBigInt(tx.GetGasTipCap()), | ||
GasFeeCap: stringToBigInt(tx.GetGasFeeCap()), | ||
Gas: tx.GetGas(), | ||
To: stringToAddress(tx.GetTo()), | ||
Value: stringToBigInt(tx.GetValue()), | ||
Data: tx.GetData(), | ||
AccessList: tx.GetAccessList(), | ||
V: v, | ||
R: r, | ||
S: s, | ||
} | ||
} | ||
|
||
// GetAccessList returns the AccessList field. | ||
func (tx *DynamicFeeTx) GetAccessList() ethtypes.AccessList { | ||
if tx.Accesses == nil { | ||
return nil | ||
} | ||
var ethAccessList ethtypes.AccessList | ||
|
||
for _, tuple := range tx.Accesses { | ||
storageKeys := make([]common.Hash, len(tuple.StorageKeys)) | ||
|
||
for i := range tuple.StorageKeys { | ||
storageKeys[i] = common.HexToHash(tuple.StorageKeys[i]) | ||
} | ||
|
||
ethAccessList = append(ethAccessList, ethtypes.AccessTuple{ | ||
Address: common.HexToAddress(tuple.Address), | ||
StorageKeys: storageKeys, | ||
}) | ||
} | ||
|
||
return ethAccessList | ||
} | ||
|
||
// GetRawSignatureValues returns the V, R, S signature values of the transaction. | ||
// The return values should not be modified by the caller. | ||
func (tx *DynamicFeeTx) GetRawSignatureValues() (v, r, s *big.Int) { | ||
return types.RawSignatureValues(tx.V, tx.R, tx.S) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package evmv1 | ||
|
||
import ( | ||
"math/big" | ||
|
||
ethtypes "github.com/ethereum/go-ethereum/core/types" | ||
"github.com/evmos/ethermint/types" | ||
) | ||
|
||
// GetChainID returns the chain id field from the derived signature values | ||
func (tx *LegacyTx) GetChainID() *big.Int { | ||
v, _, _ := tx.GetRawSignatureValues() | ||
return types.DeriveChainID(v) | ||
} | ||
|
||
// AsEthereumData returns an LegacyTx transaction tx from the proto-formatted | ||
// TxData defined on the Cosmos EVM. | ||
func (tx *LegacyTx) AsEthereumData() ethtypes.TxData { | ||
v, r, s := tx.GetRawSignatureValues() | ||
return ðtypes.LegacyTx{ | ||
Nonce: tx.GetNonce(), | ||
GasPrice: stringToBigInt(tx.GetGasPrice()), | ||
Gas: tx.GetGas(), | ||
To: stringToAddress(tx.GetTo()), | ||
Value: stringToBigInt(tx.GetValue()), | ||
Data: tx.GetData(), | ||
V: v, | ||
R: r, | ||
S: s, | ||
} | ||
} | ||
|
||
// GetRawSignatureValues returns the V, R, S signature values of the transaction. | ||
// The return values should not be modified by the caller. | ||
func (tx *LegacyTx) GetRawSignatureValues() (v, r, s *big.Int) { | ||
return types.RawSignatureValues(tx.V, tx.R, tx.S) | ||
} | ||
|
||
// GetAccessList returns nil | ||
func (tx *LegacyTx) GetAccessList() ethtypes.AccessList { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package evmv1 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
ethtypes "github.com/ethereum/go-ethereum/core/types" | ||
protov2 "google.golang.org/protobuf/proto" | ||
) | ||
|
||
// supportedTxs holds the Ethereum transaction types | ||
// supported by Evmos. | ||
// Use a function to return a new pointer and avoid | ||
// possible reuse or racing conditions when using the same pointer | ||
var supportedTxs = map[string]func() TxDataV2{ | ||
"/ethermint.evm.v1.DynamicFeeTx": func() TxDataV2 { return &DynamicFeeTx{} }, | ||
"/ethermint.evm.v1.AccessListTx": func() TxDataV2 { return &AccessListTx{} }, | ||
"/ethermint.evm.v1.LegacyTx": func() TxDataV2 { return &LegacyTx{} }, | ||
} | ||
|
||
// getSender extracts the sender address from the signature values using the latest signer for the given chainID. | ||
func getSender(txData TxDataV2) (common.Address, error) { | ||
signer := ethtypes.LatestSignerForChainID(txData.GetChainID()) | ||
from, err := signer.Sender(ethtypes.NewTx(txData.AsEthereumData())) | ||
if err != nil { | ||
return common.Address{}, err | ||
} | ||
return from, nil | ||
} | ||
|
||
// GetSigners is the custom function to get signers on Ethereum transactions | ||
// Gets the signer's address from the Ethereum tx signature | ||
func GetSigners(msg protov2.Message) ([][]byte, error) { | ||
msgEthTx, ok := msg.(*MsgEthereumTx) | ||
if !ok { | ||
return nil, fmt.Errorf("invalid type, expected MsgEthereumTx and got %T", msg) | ||
} | ||
|
||
txDataFn, found := supportedTxs[msgEthTx.Data.TypeUrl] | ||
if !found { | ||
return nil, fmt.Errorf("invalid TypeUrl %s", msgEthTx.Data.TypeUrl) | ||
} | ||
txData := txDataFn() | ||
|
||
// msgEthTx.Data is a message (DynamicFeeTx, LegacyTx or AccessListTx) | ||
if err := msgEthTx.Data.UnmarshalTo(txData); err != nil { | ||
return nil, err | ||
} | ||
|
||
sender, err := getSender(txData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return [][]byte{sender.Bytes()}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package evmv1 | ||
|
||
import ( | ||
"math/big" | ||
|
||
sdkmath "cosmossdk.io/math" | ||
"github.com/ethereum/go-ethereum/common" | ||
ethtypes "github.com/ethereum/go-ethereum/core/types" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
) | ||
|
||
var ( | ||
_ TxDataV2 = &LegacyTx{} | ||
_ TxDataV2 = &AccessListTx{} | ||
_ TxDataV2 = &DynamicFeeTx{} | ||
) | ||
|
||
// TxDataV2 implements the Ethereum transaction tx structure. It is used | ||
// solely to define the custom logic for getting signers on Ethereum transactions. | ||
type TxDataV2 interface { | ||
GetChainID() *big.Int | ||
AsEthereumData() ethtypes.TxData | ||
|
||
ProtoReflect() protoreflect.Message | ||
} | ||
|
||
// helper function to parse string to bigInt | ||
func stringToBigInt(str string) *big.Int { | ||
if str == "" { | ||
return nil | ||
} | ||
res, ok := sdkmath.NewIntFromString(str) | ||
if !ok { | ||
return nil | ||
} | ||
return res.BigInt() | ||
} | ||
|
||
func stringToAddress(toStr string) *common.Address { | ||
if toStr == "" { | ||
return nil | ||
} | ||
addr := common.HexToAddress(toStr) | ||
return &addr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.