From 834d9c3e5efa1a2fe4ac8181f62f5dd3ed3ac278 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 18 Oct 2022 12:05:02 -0700 Subject: [PATCH] fix(api): eth/id address conversion Specifically, check if we have the expected prefix and return a sane error. --- api/eth_types.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/api/eth_types.go b/api/eth_types.go index f476f60a071..03f3d7922ac 100644 --- a/api/eth_types.go +++ b/api/eth_types.go @@ -282,6 +282,11 @@ func (a *EthAddress) UnmarshalJSON(b []byte) error { } func (a EthAddress) ToFilecoinAddress() (address.Address, error) { + expectedPrefix := [12]byte{0xff} + if !bytes.Equal(a[:12], expectedPrefix[:]) { + // TODO: handle f4 once we've added support to go-address. + return address.Address{}, xerrors.Errorf("not a valid id-in-eth address: %s", a) + } id := binary.BigEndian.Uint64(a[12:]) return address.NewIDAddress(id) } @@ -291,12 +296,9 @@ func EthAddressFromFilecoinIDAddress(addr address.Address) (EthAddress, error) { if err != nil { return EthAddress{}, err } - buf := make([]byte, ETH_ADDRESS_LENGTH) - buf[0] = 0xff - binary.BigEndian.PutUint64(buf[12:], id) - var ethaddr EthAddress - copy(ethaddr[:], buf) + ethaddr[0] = 0xff + binary.BigEndian.PutUint64(ethaddr[12:], id) return ethaddr, nil }