Skip to content

Commit

Permalink
feat: EthAPI: Add FilecoinAddressToEthAddress (#10343)
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Feb 24, 2023
1 parent 80aa6d1 commit 8975f0b
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/api_full.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions api/mocks/mock_full.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions api/proxy_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified build/openrpc/full.json.gz
Binary file not shown.
Binary file modified build/openrpc/gateway.json.gz
Binary file not shown.
Binary file modified build/openrpc/miner.json.gz
Binary file not shown.
Binary file modified build/openrpc/worker.json.gz
Binary file not shown.
20 changes: 20 additions & 0 deletions documentation/en/api-v1-unstable-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
* [EthSubscribe](#EthSubscribe)
* [EthUninstallFilter](#EthUninstallFilter)
* [EthUnsubscribe](#EthUnsubscribe)
* [Filecoin](#Filecoin)
* [FilecoinAddressToEthAddress](#FilecoinAddressToEthAddress)
* [Gas](#Gas)
* [GasEstimateFeeCap](#GasEstimateFeeCap)
* [GasEstimateGasLimit](#GasEstimateGasLimit)
Expand Down Expand Up @@ -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


Expand Down
1 change: 1 addition & 0 deletions gateway/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions itests/eth_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
4 changes: 4 additions & 0 deletions node/impl/full/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8975f0b

Please sign in to comment.