Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Teach lens about EthAccounts (et al) #168

Merged
merged 8 commits into from
Jun 7, 2022
Merged

Teach lens about EthAccounts (et al) #168

merged 8 commits into from
Jun 7, 2022

Conversation

joeabbey
Copy link
Contributor

@joeabbey joeabbey commented Jun 6, 2022

relayer needs to know about EthAccounts to interoperate with EVM chains.

This integrates the approach used by @giansalex into lens

cosmos/relayer@main...disperze:ethermint

@jtieri
Copy link
Member

jtieri commented Jun 6, 2022

This looks correct but it doesn't seem to restore the same public key that is displayed in Keplr.

For example:

In Keplr using this mnemonic

three elevator silk family street child flip also leaf inmate call frame shock little legal october vivid enable fetch siege sell burger dolphin green

I see this evmos address evmos1dea7vlekr9e34vugwkvesulglt8fx4e457vk9z

But if I run lens keys restore --chain evmos test (this should default to coin type 118)
I see this address evmos1f840flrx7jzfz7tqws6aewf20czpd3a836ckw5

If I run lens keys restore --chain evmos test1 --coin-type 60
Then I get this address evmos1j57yt284w9zqwz6tkrvlrc3p76s8vzv96wgsrk

Chain registry says coin type for evmos should be 60

I saw similar results before this PR where the keys would never match what Keplr displays and thought this would fix it but I'm starting to think Keplr is doing something behind the scenes to generate the address.

@jtieri
Copy link
Member

jtieri commented Jun 6, 2022

It's not immediately obvious what the value for --algo should be if a user is trying to restore ethereum based keys.

lens keys restore --chain evmos testnew2 --coin-type 60 --algo eth_secp256k1 yields the following output:

panic: Cannot encode unregistered concrete type ethsecp256k1.PubKey.

goroutine 1 [running]:
github.com/cosmos/cosmos-sdk/codec.(*LegacyAmino).MustMarshalLengthPrefixed(...)
        /Users/justintieri/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.45.5-0.20220523154235-2921a1c3c918/codec/amino.go:107
github.com/cosmos/cosmos-sdk/crypto/keyring.marshalInfo({0x1053ca4a8?, 0x14000fbadc0?})
        /Users/justintieri/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.45.5-0.20220523154235-2921a1c3c918/crypto/keyring/info.go:247 +0x6c
github.com/cosmos/cosmos-sdk/crypto/keyring.keystore.writeInfo({{0x1053c9180, 0x14000fb7e60}, {{0x105d73fa0, 0x2, 0x2}, {0x105d73fc0, 0x2, 0x2}}}, {0x1053ca4a8, 0x14000fbadc0})
        /Users/justintieri/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.45.5-0.20220523154235-2921a1c3c918/crypto/keyring/keyring.go:759 +0xc8
github.com/cosmos/cosmos-sdk/crypto/keyring.keystore.writeLocalKey({{0x1053c9180, 0x14000fb7e60}, {{0x105d73fa0, 0x2, 0x2}, {0x105d73fc0, 0x2, 0x2}}}, {0x16bd17968, 0x8}, ...)
        /Users/justintieri/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.45.5-0.20220523154235-2921a1c3c918/crypto/keyring/keyring.go:750 +0x1b8
github.com/cosmos/cosmos-sdk/crypto/keyring.keystore.NewAccount({{0x1053c9180, 0x14000fb7e60}, {{0x105d73fa0, 0x2, 0x2}, {0x105d73fc0, 0x2, 0x2}}}, {0x16bd17968, 0x8}, ...)
        /Users/justintieri/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.45.5-0.20220523154235-2921a1c3c918/crypto/keyring/keyring.go:556 +0x200
github.com/strangelove-ventures/lens/client.(*ChainClient).KeyAddOrRestore(0x14000fbc4d0, {0x16bd17968, 0x8}, 0x1ba010?, {0x16bd17987?, 0x14000cefd50?}, {0x14000cefd48?, 0x1046e1eb4?, 0x14000f3e820?})
        /Users/justintieri/go/src/github.com/strangelove-ventures/lens/client/keys.go:138 +0x220
github.com/strangelove-ventures/lens/client.(*ChainClient).RestoreKey(...)
        /Users/justintieri/go/src/github.com/strangelove-ventures/lens/client/keys.go:61
github.com/strangelove-ventures/lens/cmd.keysRestoreCmd.func1(0x14000f68780?, {0x14000f4c770, 0x1, 0x7?})
        /Users/justintieri/go/src/github.com/strangelove-ventures/lens/cmd/keys.go:138 +0x1ac
github.com/spf13/cobra.(*Command).execute(0x14000f68780, {0x14000f4c700, 0x7, 0x7})
        /Users/justintieri/go/pkg/mod/github.com/spf13/cobra@v1.4.0/command.go:856 +0x4c4
github.com/spf13/cobra.(*Command).ExecuteC(0x14000f42500)
        /Users/justintieri/go/pkg/mod/github.com/spf13/cobra@v1.4.0/command.go:974 +0x354
github.com/spf13/cobra.(*Command).Execute(...)
        /Users/justintieri/go/pkg/mod/github.com/spf13/cobra@v1.4.0/command.go:902
github.com/strangelove-ventures/lens/cmd.Execute()
        /Users/justintieri/go/src/github.com/strangelove-ventures/lens/cmd/root.go:130 +0x74
main.main()
        /Users/justintieri/go/src/github.com/strangelove-ventures/lens/main.go:23 +0x20

lens keys restore --chain evmos testnew2 --coin-type 60 --algo EthSecp256k1 yields the following output:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x10 pc=0x103173fec]

goroutine 1 [running]:
github.com/strangelove-ventures/lens/client.(*ChainClient).RestoreKey(...)
        /Users/justintieri/go/src/github.com/strangelove-ventures/lens/client/keys.go:65
github.com/strangelove-ventures/lens/cmd.keysRestoreCmd.func1(0x140010b9b80?, {0x140010a0380, 0x1, 0x7?})
        /Users/justintieri/go/src/github.com/strangelove-ventures/lens/cmd/keys.go:138 +0x1bc
github.com/spf13/cobra.(*Command).execute(0x140010b9b80, {0x140010a0310, 0x7, 0x7})
        /Users/justintieri/go/pkg/mod/github.com/spf13/cobra@v1.4.0/command.go:856 +0x4c4
github.com/spf13/cobra.(*Command).ExecuteC(0x140001d7900)
        /Users/justintieri/go/pkg/mod/github.com/spf13/cobra@v1.4.0/command.go:974 +0x354
github.com/spf13/cobra.(*Command).Execute(...)
        /Users/justintieri/go/pkg/mod/github.com/spf13/cobra@v1.4.0/command.go:902
github.com/strangelove-ventures/lens/cmd.Execute()
        /Users/justintieri/go/src/github.com/strangelove-ventures/lens/cmd/root.go:130 +0x74
main.main()
        /Users/justintieri/go/src/github.com/strangelove-ventures/lens/main.go:23 +0x20

@jtieri
Copy link
Member

jtieri commented Jun 6, 2022

Still seeing some panic's that stem from the SDK.

I am running this as a test case:

// TestKeyRestoreEth restores a test mnemonic
func TestKeyRestoreEth(t *testing.T) {
	keyName := "test_key"
	mnemonic := "three elevator silk family street child flip also leaf inmate call frame shock little legal october vivid enable fetch siege sell burger dolphin green"
	expectedAddress := "evmos1dea7vlekr9e34vugwkvesulglt8fx4e457vk9z"
	var coinType uint32
	coinType = 60 // Ethereum coin type used in address derivation

	homepath := t.TempDir()
	cl, err := client.NewChainClient(
		zaptest.NewLogger(t),
		&client.ChainClientConfig{
			Key:            "default",
			ChainID:        "evmos_9001-2",
			AccountPrefix:  "evmos",
			KeyringBackend: "test",
			GasAdjustment:  1.2,
			GasPrices:      "0.01uevmos",
			Timeout:        "20s",
			OutputFormat:   "json",
			SignModeStr:    "direct",
		},
		homepath, nil, nil,
	)
	if err != nil {
		t.Fatal(err)
	}
	_ = cl.DeleteKey(keyName) // Delete if test is being run again
	address, err := cl.RestoreKey(keyName, mnemonic, coinType)
	if err != nil {
		t.Fatalf("Error while restoring mnemonic: %v", err)
	}
	if address != expectedAddress {
		t.Fatalf("Restored address: %s does not match expected: %s", address, expectedAddress)
	}
}

and seeing this output:

=== RUN   TestKeyRestoreEth
--- FAIL: TestKeyRestoreEth (0.00s)
panic: Cannot encode unregistered concrete type ethsecp256k1.PubKey. [recovered]
	panic: Cannot encode unregistered concrete type ethsecp256k1.PubKey.

goroutine 22 [running]:
testing.tRunner.func1.2({0x103f76980, 0x140010101a0})
	/usr/local/go/src/testing/testing.go:1389 +0x1c8
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1392 +0x384
panic({0x103f76980, 0x140010101a0})
	/usr/local/go/src/runtime/panic.go:838 +0x204
github.com/cosmos/cosmos-sdk/codec.(*LegacyAmino).MustMarshalLengthPrefixed(...)
	/Users/justintieri/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.45.5-0.20220523154235-2921a1c3c918/codec/amino.go:107
github.com/cosmos/cosmos-sdk/crypto/keyring.marshalInfo({0x104133ab8?, 0x140010160c0?})
	/Users/justintieri/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.45.5-0.20220523154235-2921a1c3c918/crypto/keyring/info.go:247 +0x6c
github.com/cosmos/cosmos-sdk/crypto/keyring.keystore.writeInfo({{0x104132c50, 0x14000fed320}, {{0x104a1c080, 0x2, 0x2}, {0x104a1c0a0, 0x2, 0x2}}}, {0x104133ab8, 0x140010160c0})
	/Users/justintieri/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.45.5-0.20220523154235-2921a1c3c918/crypto/keyring/keyring.go:759 +0xc8
github.com/cosmos/cosmos-sdk/crypto/keyring.keystore.writeLocalKey({{0x104132c50, 0x14000fed320}, {{0x104a1c080, 0x2, 0x2}, {0x104a1c0a0, 0x2, 0x2}}}, {0x103b95ac8, 0x8}, ...)
	/Users/justintieri/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.45.5-0.20220523154235-2921a1c3c918/crypto/keyring/keyring.go:750 +0x1b8
github.com/cosmos/cosmos-sdk/crypto/keyring.keystore.NewAccount({{0x104132c50, 0x14000fed320}, {{0x104a1c080, 0x2, 0x2}, {0x104a1c0a0, 0x2, 0x2}}}, {0x103b95ac8, 0x8}, ...)
	/Users/justintieri/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.45.5-0.20220523154235-2921a1c3c918/crypto/keyring/keyring.go:556 +0x200
github.com/strangelove-ventures/lens/client.(*ChainClient).KeyAddOrRestore(0x140004bd290, {0x103b95ac8, 0x8}, 0x3c, {0x14001007f18?, 0x0?, 0x0?})
	/Users/justintieri/go/src/github.com/strangelove-ventures/lens/client/keys.go:136 +0xf8
github.com/strangelove-ventures/lens/client.(*ChainClient).RestoreKey(...)
	/Users/justintieri/go/src/github.com/strangelove-ventures/lens/client/keys.go:61
github.com/strangelove-ventures/lens/client_test.TestKeyRestoreEth(0x14000fc6680)
	/Users/justintieri/go/src/github.com/strangelove-ventures/lens/client/keys_test.go:55 +0x23c
testing.tRunner(0x14000fc6680, 0x10411b358)
	/usr/local/go/src/testing/testing.go:1439 +0x110
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:1486 +0x300

@giansalex
Copy link

giansalex commented Jun 7, 2022

this should solve it

import (
   "github.com/cosmos/cosmos-sdk/codec/legacy"
   "github.com/tharsis/ethermint/crypto/ethsecp256k1"
)

legacy.Cdc.RegisterConcrete(&ethsecp256k1.PubKey{},
	ethsecp256k1.PubKeyName, nil)
legacy.Cdc.RegisterConcrete(&ethsecp256k1.PrivKey{},
	ethsecp256k1.PrivKeyName, nil)

see this note https://github.com/tharsis/ethermint/blob/v.10.1/crypto/codec/amino.go#L24

@jtieri
Copy link
Member

jtieri commented Jun 7, 2022

this should solve it

import (
   "github.com/cosmos/cosmos-sdk/codec/legacy"
   "github.com/tharsis/ethermint/crypto/ethsecp256k1"
)

legacy.Cdc.RegisterConcrete(&ethsecp256k1.PubKey{},
	ethsecp256k1.PubKeyName, nil)
legacy.Cdc.RegisterConcrete(&ethsecp256k1.PrivKey{},
	ethsecp256k1.PrivKeyName, nil)

see this note https://github.com/tharsis/ethermint/blob/v.10.1/crypto/codec/amino.go#L24

Ahh looks like that does it! 😄

@giansalex
Copy link

giansalex commented Jun 7, 2022

this commit must also be added to allow to simulate tx
disperze@3a5fa8d (from cosmos-sdk)

@joeabbey
Copy link
Contributor Author

joeabbey commented Jun 7, 2022

see this note https://github.com/tharsis/ethermint/blob/v.10.1/crypto/codec/amino.go#L24

Right, I'm guessing this is a temporary need until the SDK has EthSecp256K1 built-in

@jtieri jtieri merged commit d290594 into main Jun 7, 2022
@jtieri jtieri deleted the explicitly-add-ethmint branch June 7, 2022 14:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants