feat: EthAPI: Add FilecoinAddressToEthAddress (#10343)

This commit is contained in:
Aayush Rajasekaran 2023-02-24 14:19:52 -05:00 committed by GitHub
parent 80aa6d1d64
commit 8975f0b753
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 96 additions and 0 deletions

View File

@ -771,6 +771,8 @@ type FullNode interface {
EthAccounts(ctx context.Context) ([]ethtypes.EthAddress, error) //perm:read
// EthAddressToFilecoinAddress converts an EthAddress into an f410 Filecoin Address
EthAddressToFilecoinAddress(ctx context.Context, ethAddress ethtypes.EthAddress) (address.Address, error) //perm:read
// FilecoinAddressToEthAddress converts an f410 or f0 Filecoin Address to an EthAddress
FilecoinAddressToEthAddress(ctx context.Context, filecoinAddress address.Address) (ethtypes.EthAddress, error) //perm:read
// EthBlockNumber returns the height of the latest (heaviest) TipSet
EthBlockNumber(ctx context.Context) (ethtypes.EthUint64, error) //perm:read
// EthGetBlockTransactionCountByNumber returns the number of messages in the TipSet

View File

@ -1448,6 +1448,21 @@ func (mr *MockFullNodeMockRecorder) EthUnsubscribe(arg0, arg1 interface{}) *gomo
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EthUnsubscribe", reflect.TypeOf((*MockFullNode)(nil).EthUnsubscribe), arg0, arg1)
}
// FilecoinAddressToEthAddress mocks base method.
func (m *MockFullNode) FilecoinAddressToEthAddress(arg0 context.Context, arg1 address.Address) (ethtypes.EthAddress, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FilecoinAddressToEthAddress", arg0, arg1)
ret0, _ := ret[0].(ethtypes.EthAddress)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FilecoinAddressToEthAddress indicates an expected call of FilecoinAddressToEthAddress.
func (mr *MockFullNodeMockRecorder) FilecoinAddressToEthAddress(arg0, arg1 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FilecoinAddressToEthAddress", reflect.TypeOf((*MockFullNode)(nil).FilecoinAddressToEthAddress), arg0, arg1)
}
// GasEstimateFeeCap mocks base method.
func (m *MockFullNode) GasEstimateFeeCap(arg0 context.Context, arg1 *types.Message, arg2 int64, arg3 types.TipSetKey) (big.Int, error) {
m.ctrl.T.Helper()

View File

@ -310,6 +310,8 @@ type FullNodeMethods struct {
EthUnsubscribe func(p0 context.Context, p1 ethtypes.EthSubscriptionID) (bool, error) `perm:"write"`
FilecoinAddressToEthAddress func(p0 context.Context, p1 address.Address) (ethtypes.EthAddress, error) `perm:"read"`
GasEstimateFeeCap func(p0 context.Context, p1 *types.Message, p2 int64, p3 types.TipSetKey) (types.BigInt, error) `perm:"read"`
GasEstimateGasLimit func(p0 context.Context, p1 *types.Message, p2 types.TipSetKey) (int64, error) `perm:"read"`
@ -2372,6 +2374,17 @@ func (s *FullNodeStub) EthUnsubscribe(p0 context.Context, p1 ethtypes.EthSubscri
return false, ErrNotSupported
}
func (s *FullNodeStruct) FilecoinAddressToEthAddress(p0 context.Context, p1 address.Address) (ethtypes.EthAddress, error) {
if s.Internal.FilecoinAddressToEthAddress == nil {
return *new(ethtypes.EthAddress), ErrNotSupported
}
return s.Internal.FilecoinAddressToEthAddress(p0, p1)
}
func (s *FullNodeStub) FilecoinAddressToEthAddress(p0 context.Context, p1 address.Address) (ethtypes.EthAddress, error) {
return *new(ethtypes.EthAddress), ErrNotSupported
}
func (s *FullNodeStruct) GasEstimateFeeCap(p0 context.Context, p1 *types.Message, p2 int64, p3 types.TipSetKey) (types.BigInt, error) {
if s.Internal.GasEstimateFeeCap == nil {
return *new(types.BigInt), ErrNotSupported

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -101,6 +101,8 @@
* [EthSubscribe](#EthSubscribe)
* [EthUninstallFilter](#EthUninstallFilter)
* [EthUnsubscribe](#EthUnsubscribe)
* [Filecoin](#Filecoin)
* [FilecoinAddressToEthAddress](#FilecoinAddressToEthAddress)
* [Gas](#Gas)
* [GasEstimateFeeCap](#GasEstimateFeeCap)
* [GasEstimateGasLimit](#GasEstimateGasLimit)
@ -2944,6 +2946,24 @@ Inputs:
Response: `true`
## Filecoin
### FilecoinAddressToEthAddress
FilecoinAddressToEthAddress converts an f410 or f0 Filecoin Address to an EthAddress
Perms: read
Inputs:
```json
[
"f01234"
]
```
Response: `"0x5cbeecf99d3fdb3f25e309cc264f240bb0664031"`
## Gas

View File

@ -89,6 +89,7 @@ type TargetAPI interface {
WalletBalance(context.Context, address.Address) (types.BigInt, error)
EthAddressToFilecoinAddress(ctx context.Context, ethAddress ethtypes.EthAddress) (address.Address, error)
FilecoinAddressToEthAddress(ctx context.Context, filecoinAddress address.Address) (ethtypes.EthAddress, error)
EthBlockNumber(ctx context.Context) (ethtypes.EthUint64, error)
EthGetBlockTransactionCountByNumber(ctx context.Context, blkNum ethtypes.EthUint64) (ethtypes.EthUint64, error)
EthGetBlockTransactionCountByHash(ctx context.Context, blkHash ethtypes.EthHash) (ethtypes.EthUint64, error)

View File

@ -46,3 +46,44 @@ func TestEthAddressToFilecoinAddress(t *testing.T) {
require.Equal(t, filecoinIdArr, apiFilAddr)
}
func TestFilecoinAddressToEthAddress(t *testing.T) {
// Disable EthRPC to confirm that this method does NOT need the EthEnableRPC config set to true
client, _, _ := kit.EnsembleMinimal(t, kit.MockProofs(), kit.ThroughRPC(), kit.DisableEthRPC())
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
secpDelegatedKey, err := key.GenerateKey(types.KTDelegated)
require.NoError(t, err)
filecoinKeyAddr, err := client.WalletImport(ctx, &secpDelegatedKey.KeyInfo)
require.NoError(t, err)
ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(filecoinKeyAddr)
require.NoError(t, err)
apiEthAddr, err := client.FilecoinAddressToEthAddress(ctx, filecoinKeyAddr)
require.NoError(t, err)
require.Equal(t, ethAddr, apiEthAddr)
filecoinIdArr := builtin.StorageMarketActorAddr
ethAddr, err = ethtypes.EthAddressFromFilecoinAddress(filecoinIdArr)
require.NoError(t, err)
apiEthAddr, err = client.FilecoinAddressToEthAddress(ctx, filecoinIdArr)
require.NoError(t, err)
require.Equal(t, ethAddr, apiEthAddr)
secpKey, err := key.GenerateKey(types.KTSecp256k1)
require.NoError(t, err)
filecoinSecpAddr, err := client.WalletImport(ctx, &secpKey.KeyInfo)
require.NoError(t, err)
_, err = client.FilecoinAddressToEthAddress(ctx, filecoinSecpAddr)
require.ErrorContains(t, err, ethtypes.ErrInvalidAddress.Error())
}

View File

@ -186,6 +186,10 @@ func (a *EthAPI) EthAddressToFilecoinAddress(ctx context.Context, ethAddress eth
return ethAddress.ToFilecoinAddress()
}
func (a *EthAPI) FilecoinAddressToEthAddress(ctx context.Context, filecoinAddress address.Address) (ethtypes.EthAddress, error) {
return ethtypes.EthAddressFromFilecoinAddress(filecoinAddress)
}
func (a *EthModule) countTipsetMsgs(ctx context.Context, ts *types.TipSet) (int, error) {
blkMsgs, err := a.Chain.BlockMsgsForTipset(ctx, ts)
if err != nil {