github.com/iotexproject/iotex-core@v1.14.1-rc1/pkg/util/addrutil/addrutil_test.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package addrutil
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/ethereum/go-ethereum/common"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/iotexproject/iotex-core/test/identityset"
    15  )
    16  
    17  func TestIoAddrToEvmAddr(t *testing.T) {
    18  	t.Run("converts IoTeX address into evm address successfully", func(t *testing.T) {
    19  		ioAddr := identityset.PrivateKey(28)
    20  
    21  		ethAddr, err := IoAddrToEvmAddr(ioAddr.PublicKey().Address().String())
    22  		require.NoError(t, err)
    23  		require.Equal(t, ioAddr.PublicKey().Address().Bytes(), ethAddr.Bytes())
    24  	})
    25  
    26  	t.Run("failed to convert IoTeX address into evm address", func(t *testing.T) {
    27  		ethAddr, err := IoAddrToEvmAddr("")
    28  		require.Error(t, err)
    29  		require.Equal(t, common.Address{}, ethAddr)
    30  		require.Contains(t, err.Error(), "address length = 0, expecting 41")
    31  	})
    32  }