Eth JSON-RPC API: support f4 addresses.
This commit is contained in:
parent
420bd34888
commit
17e9e97064
@ -23,6 +23,9 @@ import (
|
|||||||
|
|
||||||
type EthUint64 uint64
|
type EthUint64 uint64
|
||||||
|
|
||||||
|
// EthAddressManagerActorID is the actor ID of the Ethereum Address Manager singleton.
|
||||||
|
const EthAddressManagerActorID = 10
|
||||||
|
|
||||||
func (e EthUint64) MarshalJSON() ([]byte, error) {
|
func (e EthUint64) MarshalJSON() ([]byte, error) {
|
||||||
if e == 0 {
|
if e == 0 {
|
||||||
return json.Marshal("0x0")
|
return json.Marshal("0x0")
|
||||||
@ -244,8 +247,8 @@ func CheckContractCreation(lookup *MsgLookup) (*EthAddress, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ETH_ADDRESS_LENGTH = 20
|
EthAddressLength = 20
|
||||||
ETH_HASH_LENGTH = 32
|
EthHashLength = 32
|
||||||
)
|
)
|
||||||
|
|
||||||
type EthNonce [8]byte
|
type EthNonce [8]byte
|
||||||
@ -255,20 +258,20 @@ func (n EthNonce) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n EthNonce) MarshalJSON() ([]byte, error) {
|
func (n EthNonce) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal((n.String()))
|
return json.Marshal(n.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
type EthAddress [ETH_ADDRESS_LENGTH]byte
|
type EthAddress [EthAddressLength]byte
|
||||||
|
|
||||||
func (a EthAddress) String() string {
|
func (ea EthAddress) String() string {
|
||||||
return "0x" + hex.EncodeToString(a[:])
|
return "0x" + hex.EncodeToString(ea[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a EthAddress) MarshalJSON() ([]byte, error) {
|
func (ea EthAddress) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal((a.String()))
|
return json.Marshal(ea.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *EthAddress) UnmarshalJSON(b []byte) error {
|
func (ea *EthAddress) UnmarshalJSON(b []byte) error {
|
||||||
var s string
|
var s string
|
||||||
if err := json.Unmarshal(b, &s); err != nil {
|
if err := json.Unmarshal(b, &s); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -277,20 +280,28 @@ func (a *EthAddress) UnmarshalJSON(b []byte) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
copy(a[:], addr[:])
|
copy(ea[:], addr[:])
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a EthAddress) ToFilecoinAddress() (address.Address, error) {
|
func (ea EthAddress) ToFilecoinAddress() (address.Address, error) {
|
||||||
expectedPrefix := [12]byte{0xff}
|
idmask := [12]byte{0xff}
|
||||||
if !bytes.Equal(a[:12], expectedPrefix[:]) {
|
if bytes.Equal(ea[:12], idmask[:]) {
|
||||||
// TODO: handle f4 once we've added support to go-address.
|
// This is a masked ID address.
|
||||||
return address.Address{}, xerrors.Errorf("not a valid id-in-eth address: %s", a)
|
id := binary.BigEndian.Uint64(ea[12:])
|
||||||
}
|
|
||||||
id := binary.BigEndian.Uint64(a[12:])
|
|
||||||
return address.NewIDAddress(id)
|
return address.NewIDAddress(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Otherwise, translate the address into an address controlled by the
|
||||||
|
// Ethereum Address Manager.
|
||||||
|
addr, err := address.NewDelegatedAddress(EthAddressManagerActorID, ea[:])
|
||||||
|
if err != nil {
|
||||||
|
return address.Undef, fmt.Errorf("failed to translate supplied address (%s) into a "+
|
||||||
|
"Filecoin f4 address: %w", hex.EncodeToString(ea[:]), err)
|
||||||
|
}
|
||||||
|
return addr, nil
|
||||||
|
}
|
||||||
|
|
||||||
func EthAddressFromFilecoinIDAddress(addr address.Address) (EthAddress, error) {
|
func EthAddressFromFilecoinIDAddress(addr address.Address) (EthAddress, error) {
|
||||||
id, err := address.IDFromAddress(addr)
|
id, err := address.IDFromAddress(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -304,25 +315,25 @@ func EthAddressFromFilecoinIDAddress(addr address.Address) (EthAddress, error) {
|
|||||||
|
|
||||||
func EthAddressFromHex(s string) (EthAddress, error) {
|
func EthAddressFromHex(s string) (EthAddress, error) {
|
||||||
handlePrefix(&s)
|
handlePrefix(&s)
|
||||||
b, err := decodeHexString(s, ETH_ADDRESS_LENGTH)
|
b, err := decodeHexString(s, EthAddressLength)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return EthAddress{}, err
|
return EthAddress{}, err
|
||||||
}
|
}
|
||||||
var h EthAddress
|
var h EthAddress
|
||||||
copy(h[ETH_ADDRESS_LENGTH-len(b):], b)
|
copy(h[EthAddressLength-len(b):], b)
|
||||||
return h, nil
|
return h, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func EthAddressFromBytes(b []byte) (EthAddress, error) {
|
func EthAddressFromBytes(b []byte) (EthAddress, error) {
|
||||||
var a EthAddress
|
var a EthAddress
|
||||||
if len(b) != ETH_ADDRESS_LENGTH {
|
if len(b) != EthAddressLength {
|
||||||
return EthAddress{}, xerrors.Errorf("cannot initiate a new EthAddress: incorrect input length")
|
return EthAddress{}, xerrors.Errorf("cannot parse bytes into anœ EthAddress: incorrect input length")
|
||||||
}
|
}
|
||||||
copy(a[:], b[:])
|
copy(a[:], b[:])
|
||||||
return a, nil
|
return a, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type EthHash [ETH_HASH_LENGTH]byte
|
type EthHash [EthHashLength]byte
|
||||||
|
|
||||||
func (h EthHash) MarshalJSON() ([]byte, error) {
|
func (h EthHash) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(h.String())
|
return json.Marshal(h.String())
|
||||||
@ -370,12 +381,12 @@ func EthHashFromCid(c cid.Cid) (EthHash, error) {
|
|||||||
|
|
||||||
func EthHashFromHex(s string) (EthHash, error) {
|
func EthHashFromHex(s string) (EthHash, error) {
|
||||||
handlePrefix(&s)
|
handlePrefix(&s)
|
||||||
b, err := decodeHexString(s, ETH_HASH_LENGTH)
|
b, err := decodeHexString(s, EthHashLength)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return EthHash{}, err
|
return EthHash{}, err
|
||||||
}
|
}
|
||||||
var h EthHash
|
var h EthHash
|
||||||
copy(h[ETH_HASH_LENGTH-len(b):], b)
|
copy(h[EthHashLength-len(b):], b)
|
||||||
return h, nil
|
return h, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
go.sum
2
go.sum
@ -288,6 +288,8 @@ github.com/filecoin-project/go-address v0.0.5/go.mod h1:jr8JxKsYx+lQlQZmF5i2U0Z+
|
|||||||
github.com/filecoin-project/go-address v0.0.6/go.mod h1:7B0/5DA13n6nHkB8bbGx1gWzG/dbTsZ0fgOJVGsM3TE=
|
github.com/filecoin-project/go-address v0.0.6/go.mod h1:7B0/5DA13n6nHkB8bbGx1gWzG/dbTsZ0fgOJVGsM3TE=
|
||||||
github.com/filecoin-project/go-address v1.1.0 h1:ofdtUtEsNxkIxkDw67ecSmvtzaVSdcea4boAmLbnHfE=
|
github.com/filecoin-project/go-address v1.1.0 h1:ofdtUtEsNxkIxkDw67ecSmvtzaVSdcea4boAmLbnHfE=
|
||||||
github.com/filecoin-project/go-address v1.1.0/go.mod h1:5t3z6qPmIADZBtuE9EIzi0EwzcRy2nVhpo0I/c1r0OA=
|
github.com/filecoin-project/go-address v1.1.0/go.mod h1:5t3z6qPmIADZBtuE9EIzi0EwzcRy2nVhpo0I/c1r0OA=
|
||||||
|
github.com/filecoin-project/go-address v1.0.1-0.20221019124855-ac317c37debb h1:UvHJms0aGDItERFnspu5FgKYHU7DEiLo67H2C/asrNs=
|
||||||
|
github.com/filecoin-project/go-address v1.0.1-0.20221019124855-ac317c37debb/go.mod h1:5t3z6qPmIADZBtuE9EIzi0EwzcRy2nVhpo0I/c1r0OA=
|
||||||
github.com/filecoin-project/go-amt-ipld/v2 v2.1.0 h1:t6qDiuGYYngDqaLc2ZUvdtAg4UNxPeOYaXhBWSNsVaM=
|
github.com/filecoin-project/go-amt-ipld/v2 v2.1.0 h1:t6qDiuGYYngDqaLc2ZUvdtAg4UNxPeOYaXhBWSNsVaM=
|
||||||
github.com/filecoin-project/go-amt-ipld/v2 v2.1.0/go.mod h1:nfFPoGyX0CU9SkXX8EoCcSuHN1XcbN0c6KBh7yvP5fs=
|
github.com/filecoin-project/go-amt-ipld/v2 v2.1.0/go.mod h1:nfFPoGyX0CU9SkXX8EoCcSuHN1XcbN0c6KBh7yvP5fs=
|
||||||
github.com/filecoin-project/go-amt-ipld/v3 v3.0.0/go.mod h1:Qa95YNAbtoVCTSVtX38aAC1ptBnJfPma1R/zZsKmx4o=
|
github.com/filecoin-project/go-amt-ipld/v3 v3.0.0/go.mod h1:Qa95YNAbtoVCTSVtX38aAC1ptBnJfPma1R/zZsKmx4o=
|
||||||
|
Loading…
Reference in New Issue
Block a user