Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: EthAPI: Add EthAddressToFilecoinAddress #10286

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,11 @@ workflows:
suite: itest-eth_account_abstraction
target: "./itests/eth_account_abstraction_test.go"

- test:
name: test-itest-eth_api
suite: itest-eth_api
target: "./itests/eth_api_test.go"

- test:
name: test-itest-eth_balance
suite: itest-eth_balance
Expand Down
2 changes: 2 additions & 0 deletions api/api_full.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,8 @@ type FullNode interface {
//
// EthAccounts will always return [] since we don't expect Lotus to manage private keys
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
// 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.
16 changes: 16 additions & 0 deletions documentation/en/api-v1-unstable-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
* [CreateBackup](#CreateBackup)
* [Eth](#Eth)
* [EthAccounts](#EthAccounts)
* [EthAddressToFilecoinAddress](#EthAddressToFilecoinAddress)
* [EthBlockNumber](#EthBlockNumber)
* [EthCall](#EthCall)
* [EthChainId](#EthChainId)
Expand Down Expand Up @@ -2262,6 +2263,21 @@ Response:
]
```

### EthAddressToFilecoinAddress
EthAddressToFilecoinAddress converts an EthAddress into an f410 Filecoin Address


Perms: read

Inputs:
```json
[
"0x5cbeecf99d3fdb3f25e309cc264f240bb0664031"
]
```

Response: `"f01234"`

### EthBlockNumber
EthBlockNumber returns the height of the latest (heaviest) TipSet

Expand Down
1 change: 1 addition & 0 deletions gateway/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type TargetAPI interface {
StateVMCirculatingSupplyInternal(context.Context, types.TipSetKey) (api.CirculatingSupply, error)
WalletBalance(context.Context, address.Address) (types.BigInt, error)

EthAddressToFilecoinAddress(ctx context.Context, ethAddress ethtypes.EthAddress) (address.Address, 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
47 changes: 47 additions & 0 deletions itests/eth_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package itests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't have a strong opinion on this but maybe move this to fevm_address_test.go and rename that file to eth_address_test.go since that name does cover this domain

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to have a dedicated test suite for the EthAPI, my hope is that this will be the start of that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point but the Eth API seems very large to stick in a single file?

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"

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

"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
"github.com/filecoin-project/lotus/chain/wallet/key"
"github.com/filecoin-project/lotus/itests/kit"
)

func TestEthAddressToFilecoinAddress(t *testing.T) {
client, _, _ := kit.EnsembleMinimal(t, kit.MockProofs(), kit.ThroughRPC())

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

secpKey, err := key.GenerateKey(types.KTDelegated)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still a secpKey since the type here is delegated?

Copy link
Contributor Author

@arajasek arajasek Feb 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, "Delegated" signature keys are solely secp.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my own knowledge,
We also have a secp address type which I presume have secp keys
What is the mapping of address types to key types? Are those orthogonal concepts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is a little gnarly. Secp addresses correspond to secp keys. Delegated addresses today solely correspond to secp keys. They will eventually support other kinds of signatures, such as "account abstraction".

So why do we have the KTDelegated sigtype today? It's because the Lotus keystore couples the stored key with its address representation. So when working with keys, we need to treat delegated sigtypes differently from secp sigtypes (eg. here).

require.NoError(t, err)

filecoinKeyAddr, err := client.WalletImport(ctx, &secpKey.KeyInfo)
require.NoError(t, err)

ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(filecoinKeyAddr)
require.NoError(t, err)

apiFilAddr, err := client.EthAddressToFilecoinAddress(ctx, ethAddr)
require.NoError(t, err)

require.Equal(t, filecoinKeyAddr, apiFilAddr)

filecoinIdArr := builtin.StorageMarketActorAddr
ethAddr, err = ethtypes.EthAddressFromFilecoinAddress(filecoinIdArr)
require.NoError(t, err)

apiFilAddr, err = client.EthAddressToFilecoinAddress(ctx, ethAddr)
require.NoError(t, err)

require.Equal(t, filecoinIdArr, apiFilAddr)

}
5 changes: 5 additions & 0 deletions node/impl/full/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/ipfs/go-cid"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-jsonrpc"

"github.com/filecoin-project/lotus/api"
Expand All @@ -16,6 +17,10 @@ var ErrModuleDisabled = errors.New("module disabled, enable with Fevm.EnableEthR

type EthModuleDummy struct{}

func (e *EthModuleDummy) EthAddressToFilecoinAddress(ctx context.Context, ethAddress ethtypes.EthAddress) (address.Address, error) {
return address.Undef, ErrModuleDisabled
}

func (e *EthModuleDummy) EthGetMessageCidByTransactionHash(ctx context.Context, txHash *ethtypes.EthHash) (*cid.Cid, error) {
return nil, ErrModuleDisabled
}
Expand Down
4 changes: 4 additions & 0 deletions node/impl/full/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ func (a *EthModule) EthAccounts(context.Context) ([]ethtypes.EthAddress, error)
return []ethtypes.EthAddress{}, nil
}

func (a *EthAPI) EthAddressToFilecoinAddress(ctx context.Context, ethAddress ethtypes.EthAddress) (address.Address, error) {
return ethAddress.ToFilecoinAddress()
}

func (a *EthModule) countTipsetMsgs(ctx context.Context, ts *types.TipSet) (int, error) {
blkMsgs, err := a.Chain.BlockMsgsForTipset(ctx, ts)
if err != nil {
Expand Down