Skip to content

Commit

Permalink
Always consider CosmWasm contract address as associated (#1633)
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen authored May 7, 2024
1 parent 981f003 commit b7c1875
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions x/evm/keeper/address.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/sei-protocol/sei-chain/x/evm/types"
Expand All @@ -27,6 +28,12 @@ func (k *Keeper) DeleteAddressMapping(ctx sdk.Context, seiAddress sdk.AccAddress
}

func (k *Keeper) GetEVMAddress(ctx sdk.Context, seiAddress sdk.AccAddress) (common.Address, bool) {
// CW address has a different length and should always be considered associated
// Note that this association is one-way since CW address is longer than EOA address
// and would need to be cropped.
if len(seiAddress) == wasmtypes.ContractAddrLen {
return common.BytesToAddress(seiAddress), true
}
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.SeiAddressToEVMAddressKey(seiAddress))
addr := common.Address{}
Expand Down
10 changes: 10 additions & 0 deletions x/evm/keeper/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"testing"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
"github.com/ethereum/go-ethereum/common"
"github.com/sei-protocol/sei-chain/testutil/keeper"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -50,3 +52,11 @@ func TestGetAddressOrDefault(t *testing.T) {
defaultSeiAddr := k.GetSeiAddressOrDefault(ctx, evmAddr)
require.True(t, bytes.Equal(defaultSeiAddr, evmAddr[:]))
}

func TestGetEVMAddressForCW(t *testing.T) {
k, ctx := keeper.MockEVMKeeper()
cwAddr := wasmkeeper.BuildContractAddress(123, 456)
cwEvmAddr, associated := k.GetEVMAddress(ctx, cwAddr)
require.True(t, associated)
require.Equal(t, common.BytesToAddress(cwAddr), cwEvmAddr)
}

0 comments on commit b7c1875

Please sign in to comment.