Skip to content

Commit

Permalink
cleanup ethtypes.{Try=>}EthAddressFromFilecoinAddress.
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk committed Jan 12, 2023
1 parent b4b1241 commit 6d0c4d2
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 61 deletions.
2 changes: 1 addition & 1 deletion chain/types/ethtypes/eth_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func NewEthTxArgsFromMessage(msg *types.Message) (EthTxArgs, error) {
return EthTxArgs{}, fmt.Errorf("unsupported EAM method")
}
} else {
addr, err := EthAddressFromFilecoinAddress(msg.To)
addr, err := EthAddressFromFilecoinAddress(msg.To, true)
if err != nil {
return EthTxArgs{}, err
}
Expand Down
72 changes: 31 additions & 41 deletions chain/types/ethtypes/eth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
mathbig "math/big"
"strconv"
Expand All @@ -30,6 +31,11 @@ var (
EthTopic4 = "topic4"
)

var (
ErrIsIDAddress = errors.New("supplied Eth address is a masked ID address")
ErrInvalidAddress = errors.New("invalid Filecoin Eth address")
)

type EthUint64 uint64

func (e EthUint64) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -248,17 +254,32 @@ func EthAddressFromPubKey(pubk []byte) ([]byte, error) {
return ethAddr, nil
}

func EthAddressFromFilecoinAddress(addr address.Address) (EthAddress, error) {
ethAddr, ok, err := TryEthAddressFromFilecoinAddress(addr, true)
if err != nil {
return EthAddress{}, xerrors.Errorf("failed to try converting filecoin to eth addr: %w", err)
}

if !ok {
return EthAddress{}, xerrors.Errorf("failed to convert filecoin address %s to an equivalent eth address", addr)
func EthAddressFromFilecoinAddress(addr address.Address, allowId bool) (EthAddress, error) {
switch addr.Protocol() {
case address.ID:
if !allowId {
return EthAddress{}, ErrIsIDAddress
}
id, err := address.IDFromAddress(addr)
if err != nil {
return EthAddress{}, err
}
var ethaddr EthAddress
ethaddr[0] = 0xff
binary.BigEndian.PutUint64(ethaddr[12:], id)
return ethaddr, nil
case address.Delegated:
payload := addr.Payload()
namespace, n, err := varint.FromUvarint(payload)
if err != nil {
return EthAddress{}, xerrors.Errorf("invalid delegated address namespace in: %s", addr)
}
payload = payload[n:]
if namespace == builtintypes.EthereumAddressManagerActorID {
return CastEthAddress(payload)
}
}

return ethAddr, nil
return EthAddress{}, ErrInvalidAddress
}

// ParseEthAddress parses an Ethereum address from a hex string.
Expand Down Expand Up @@ -321,37 +342,6 @@ func (ea EthAddress) ToFilecoinAddress() (address.Address, error) {
return addr, nil
}

// This API assumes that if an ID address is passed in, it doesn't have an equivalent
// delegated address
func TryEthAddressFromFilecoinAddress(addr address.Address, allowId bool) (EthAddress, bool, error) {
switch addr.Protocol() {
case address.ID:
if !allowId {
return EthAddress{}, false, nil
}
id, err := address.IDFromAddress(addr)
if err != nil {
return EthAddress{}, false, err
}
var ethaddr EthAddress
ethaddr[0] = 0xff
binary.BigEndian.PutUint64(ethaddr[12:], id)
return ethaddr, true, nil
case address.Delegated:
payload := addr.Payload()
namespace, n, err := varint.FromUvarint(payload)
if err != nil {
return EthAddress{}, false, xerrors.Errorf("invalid delegated address namespace in: %s", addr)
}
payload = payload[n:]
if namespace == builtintypes.EthereumAddressManagerActorID {
addr, err := CastEthAddress(payload)
return addr, err == nil, err
}
}
return EthAddress{}, false, nil
}

type EthHash [EthHashLength]byte

func (h EthHash) MarshalJSON() ([]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion chain/types/ethtypes/eth_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestParseEthAddr(t *testing.T) {
addr, err := address.NewIDAddress(id)
require.Nil(t, err)

eaddr, err := EthAddressFromFilecoinAddress(addr)
eaddr, err := EthAddressFromFilecoinAddress(addr, true)
require.Nil(t, err)

faddr, err := eaddr.ToFilecoinAddress()
Expand Down
2 changes: 1 addition & 1 deletion cli/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ func ethAddrFromFilecoinAddress(ctx context.Context, addr address.Address, fnapi
return ethtypes.EthAddress{}, addr, xerrors.Errorf("Filecoin address doesn't match known protocols")
}

ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(faddr)
ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(faddr, true)
if err != nil {
return ethtypes.EthAddress{}, addr, err
}
Expand Down
4 changes: 2 additions & 2 deletions itests/eth_balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestEthGetBalanceExistentIDMaskedAddr(t *testing.T) {
fid, err := client.StateLookupID(ctx, faddr, types.EmptyTSK)
require.NoError(t, err)

ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(fid)
ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(fid, true)
require.NoError(t, err)

balance, err := client.WalletBalance(ctx, fid)
Expand All @@ -88,7 +88,7 @@ func TestEthGetBalanceBuiltinActor(t *testing.T) {

kit.SendFunds(ctx, t, client, fid, abi.TokenAmount{Int: big.NewInt(10).Int})

ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(fid)
ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(fid, true)
require.NoError(t, err)

ebal, err := client.EthGetBalance(ctx, ethAddr, "latest")
Expand Down
4 changes: 2 additions & 2 deletions itests/eth_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func TestEthNewFilterCatchAll(t *testing.T) {
actor, err := client.StateGetActor(ctx, idAddr, ts.Key())
require.NoError(err)
require.NotNil(actor.Address)
ethContractAddr, err := ethtypes.EthAddressFromFilecoinAddress(*actor.Address)
ethContractAddr, err := ethtypes.EthAddressFromFilecoinAddress(*actor.Address, true)
require.NoError(err)

// collect filter results
Expand Down Expand Up @@ -557,7 +557,7 @@ func invokeContractAndWaitUntilAllOnChain(t *testing.T, client *kit.TestFullNode
actor, err := client.StateGetActor(ctx, idAddr, head.Key())
require.NoError(err)
require.NotNil(actor.Address)
ethContractAddr, err := ethtypes.EthAddressFromFilecoinAddress(*actor.Address)
ethContractAddr, err := ethtypes.EthAddressFromFilecoinAddress(*actor.Address, true)
require.NoError(err)

return ethContractAddr, received
Expand Down
2 changes: 1 addition & 1 deletion itests/fevm_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestAddressCreationBeforeDeploy(t *testing.T) {
fromId, err := client.StateLookupID(ctx, fromAddr, types.EmptyTSK)
require.NoError(t, err)

senderEthAddr, err := ethtypes.EthAddressFromFilecoinAddress(fromId)
senderEthAddr, err := ethtypes.EthAddressFromFilecoinAddress(fromId, true)
require.NoError(t, err)

var salt [32]byte
Expand Down
2 changes: 1 addition & 1 deletion itests/kit/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (e *EVM) AssertAddressBalanceConsistent(ctx context.Context, addr address.A
fbal, err := e.WalletBalance(ctx, addr)
require.NoError(e.t, err)

ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(addr)
ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(addr, true)
require.NoError(e.t, err)

ebal, err := e.EthGetBalance(ctx, ethAddr, "latest")
Expand Down
22 changes: 11 additions & 11 deletions node/impl/full/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ func ethFilterResultFromEvents(evs []*filter.CollectedEvent) (*ethtypes.EthFilte
}
}

log.Address, err = ethtypes.EthAddressFromFilecoinAddress(ev.EmitterAddr)
log.Address, err = ethtypes.EthAddressFromFilecoinAddress(ev.EmitterAddr, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1431,11 +1431,11 @@ func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTx
// use that ID to form the masked ID address.
// 4. Otherwise, we fetch the actor's ID from the state tree and form the masked ID with it.
func lookupEthAddress(ctx context.Context, addr address.Address, sa StateAPI) (ethtypes.EthAddress, error) {
// Attempt to convert directly.
if ethAddr, ok, err := ethtypes.TryEthAddressFromFilecoinAddress(addr, false); err != nil {
return ethtypes.EthAddress{}, err
} else if ok {
// Attempt to convert directly, if it's an f4 address.
if ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(addr, false); err == nil {
return ethAddr, nil
} else if !errors.Is(err, ethtypes.ErrIsIDAddress) {
return ethtypes.EthAddress{}, err
}

// Lookup on the target actor.
Expand All @@ -1444,17 +1444,17 @@ func lookupEthAddress(ctx context.Context, addr address.Address, sa StateAPI) (e
return ethtypes.EthAddress{}, err
}
if actor.Address != nil {
if ethAddr, ok, err := ethtypes.TryEthAddressFromFilecoinAddress(*actor.Address, false); err != nil {
return ethtypes.EthAddress{}, err
} else if ok {
if ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(*actor.Address, false); err == nil {
return ethAddr, nil
} else if !errors.Is(err, ethtypes.ErrIsIDAddress) {
return ethtypes.EthAddress{}, err
}
}

// Check if we already have an ID addr, and use it if possible.
if ethAddr, ok, err := ethtypes.TryEthAddressFromFilecoinAddress(addr, true); err != nil {
if ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(addr, true); err != nil {
return ethtypes.EthAddress{}, err
} else if ok {
} else {
return ethAddr, nil
}

Expand All @@ -1463,7 +1463,7 @@ func lookupEthAddress(ctx context.Context, addr address.Address, sa StateAPI) (e
if err != nil {
return ethtypes.EthAddress{}, err
}
return ethtypes.EthAddressFromFilecoinAddress(idAddr)
return ethtypes.EthAddressFromFilecoinAddress(idAddr, true)
}

func newEthTxFromFilecoinMessage(ctx context.Context, smsg *types.SignedMessage, sa StateAPI) (ethtypes.EthTx, error) {
Expand Down

0 comments on commit 6d0c4d2

Please sign in to comment.