Skip to content

Commit

Permalink
feat: eth API: reject masked ID addresses embedded in f410f payloads
Browse files Browse the repository at this point in the history
We'll never get an actor/account deployed to one of these
addresses (although we might get a placeholder). However, converting
such an address to an f4 address is definitely wrong.
  • Loading branch information
Stebalien committed Mar 10, 2023
1 parent 58900a7 commit aee1c36
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
19 changes: 14 additions & 5 deletions chain/types/ethtypes/eth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ func EthAddressFromPubKey(pubk []byte) ([]byte, error) {
return ethAddr, nil
}

var maskedIDPrefix = [20 - 8]byte{0xff}

func IsEthAddress(addr address.Address) bool {
if addr.Protocol() != address.Delegated {
return false
Expand All @@ -305,7 +307,7 @@ func IsEthAddress(addr address.Address) bool {
return false
}

return namespace == builtintypes.EthereumAddressManagerActorID
return namespace == builtintypes.EthereumAddressManagerActorID && len(payload) == 20 && !bytes.HasPrefix(payload, maskedIDPrefix[:])
}

func EthAddressFromFilecoinAddress(addr address.Address) (EthAddress, error) {
Expand All @@ -326,9 +328,17 @@ func EthAddressFromFilecoinAddress(addr address.Address) (EthAddress, error) {
return EthAddress{}, xerrors.Errorf("invalid delegated address namespace in: %s", addr)
}
payload = payload[n:]
if namespace == builtintypes.EthereumAddressManagerActorID {
return CastEthAddress(payload)
if namespace != builtintypes.EthereumAddressManagerActorID {
return EthAddress{}, ErrInvalidAddress
}
ethAddr, err := CastEthAddress(payload)
if err != nil {
return EthAddress{}, err
}
if ethAddr.IsMaskedID() {
return EthAddress{}, xerrors.Errorf("f410f addresses cannot embed masked-ID payloads: %s", ethAddr)
}
return ethAddr, nil
}
return EthAddress{}, ErrInvalidAddress
}
Expand Down Expand Up @@ -376,8 +386,7 @@ func (ea *EthAddress) UnmarshalJSON(b []byte) error {
}

func (ea EthAddress) IsMaskedID() bool {
idmask := [12]byte{0xff}
return bytes.Equal(ea[:12], idmask[:])
return bytes.HasPrefix(ea[:], maskedIDPrefix[:])
}

func (ea EthAddress) ToFilecoinAddress() (address.Address, error) {
Expand Down
15 changes: 15 additions & 0 deletions chain/types/ethtypes/eth_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/builtin"
)

type TestCase struct {
Expand Down Expand Up @@ -178,6 +179,20 @@ func TestParseEthAddr(t *testing.T) {
}
}

func TestMaskedIDInF4(t *testing.T) {
addr, err := address.NewIDAddress(100)
require.NoError(t, err)

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

badaddr, err := address.NewDelegatedAddress(builtin.EthereumAddressManagerActorID, eaddr[:])
require.NoError(t, err)

_, err = EthAddressFromFilecoinAddress(badaddr)
require.Error(t, err)
}

func TestUnmarshalEthCall(t *testing.T) {
data := `{"from":"0x4D6D86b31a112a05A473c4aE84afaF873f632325","to":"0xFe01CC39f5Ae8553D6914DBb9dC27D219fa22D7f","gas":"0x5","gasPrice":"0x6","value":"0x123","data":""}`

Expand Down

0 comments on commit aee1c36

Please sign in to comment.