Skip to content

Commit

Permalink
feat: support metamask eth sign
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverHappened committed Nov 25, 2024
1 parent ed4b390 commit de79a9a
Show file tree
Hide file tree
Showing 11 changed files with 1,107 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/core/appmodule"
authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"
ethcryptocodec "github.com/neutron-org/neutron/v5/x/crypto/codec"

appconfig "github.com/neutron-org/neutron/v5/app/config"

Expand Down Expand Up @@ -476,6 +477,9 @@ func New(
legacyAmino := encodingConfig.Amino
interfaceRegistry := encodingConfig.InterfaceRegistry

ethcryptocodec.RegisterLegacyAminoCodec(encodingConfig.Amino)
ethcryptocodec.RegisterInterfaces(encodingConfig.InterfaceRegistry)

bApp := baseapp.NewBaseApp(Name, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetVersion(version.Version)
Expand Down
4 changes: 3 additions & 1 deletion cmd/neutrond/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"github.com/neutron-org/neutron/v5/x/crypto/keyring"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -82,7 +83,8 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
WithAccountRetriever(authtypes.AccountRetriever{}).
WithBroadcastMode(flags.BroadcastSync).
WithHomeDir(app.DefaultNodeHome).
WithViper("")
WithViper("").
WithKeyringOptions(keyring.Option())

// Allows you to add extra params to your client.toml
// gas, gas-price, gas-adjustment, fees, note, etc.
Expand Down
21 changes: 21 additions & 0 deletions proto/neutron/crypto/v1beta1/ethsecp256k1/keys.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";
package neutron.crypto.v1beta1.ethsecp256k1;

import "gogoproto/gogo.proto";

option go_package = "github.com/neutron-org/neutron/v5/x/crypto/ethsecp256k1";

// PubKey defines a type alias for an ecdsa.PublicKey that implements
// Tendermint's PubKey interface. It represents the 33-byte compressed public
// key format.
message PubKey {
option (gogoproto.goproto_stringer) = false;

bytes key = 1;
}

// PrivKey defines a type alias for an ecdsa.PrivateKey that implements
// Tendermint's PrivateKey interface.
message PrivKey {
bytes key = 1;
}
21 changes: 21 additions & 0 deletions x/crypto/codec/amino.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package codec

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy"

"github.com/neutron-org/neutron/v5/x/crypto/ethsecp256k1"
)

// RegisterLegacyAminoCodec registers all crypto dependency types with the provided Amino
// codec.
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&ethsecp256k1.PubKey{},
ethsecp256k1.PubKeyName, nil)
cdc.RegisterConcrete(&ethsecp256k1.PrivKey{},
ethsecp256k1.PrivKeyName, nil)

// NOTE: update SDK's amino codec to include the ethsecp256k1 keys.
// DO NOT REMOVE unless deprecated on the SDK.
legacy.Cdc = cdc
}
14 changes: 14 additions & 0 deletions x/crypto/codec/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package codec

import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"

"github.com/neutron-org/neutron/v5/x/crypto/ethsecp256k1"
)

// RegisterInterfaces register the Evmos key concrete types.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &ethsecp256k1.PubKey{})
registry.RegisterImplementations((*cryptotypes.PrivKey)(nil), &ethsecp256k1.PrivKey{})
}
29 changes: 29 additions & 0 deletions x/crypto/ethsecp256k1/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ethsecp256k1

import (
"fmt"
"testing"
)

func BenchmarkGenerateKey(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = GenerateKey()
}
}

func BenchmarkPubKey_VerifySignature(b *testing.B) {
privKey := GenerateKey()
pubKey := privKey.PubKey()

b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
msg := []byte(fmt.Sprintf("%10d", i))
sig, err := privKey.Sign(msg)
if err != nil {
b.Fatal(err)
}
pubKey.VerifySignature(msg, sig)
}
}
Loading

0 comments on commit de79a9a

Please sign in to comment.