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

fea: eth secp256k1 #24

Merged
merged 6 commits into from
Dec 7, 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
2 changes: 1 addition & 1 deletion app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {

sigGasConsumer := options.SigGasConsumer
if sigGasConsumer == nil {
sigGasConsumer = ante.DefaultSigVerificationGasConsumer
sigGasConsumer = DefaultSigVerificationGasConsumer
}

txFeeChecker := options.TxFeeChecker
Expand Down
27 changes: 27 additions & 0 deletions app/ante/sigverify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ante

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/initia-labs/initia/crypto/keys/eth/secp256k1"
)

// DefaultSigVerificationGasConsumer is the default implementation of SignatureVerificationGasConsumer. It consumes gas
// for signature verification based upon the public key type. The cost is fetched from the given params and is matched
// by the concrete type.
func DefaultSigVerificationGasConsumer(
meter sdk.GasMeter, sig signing.SignatureV2, params authtypes.Params,
) error {
pubkey := sig.PubKey
switch pubkey.(type) {
case *secp256k1.PubKey:
meter.ConsumeGas(params.SigVerifyCostSecp256k1, "ante verify: eth_secp256k1")
return nil

default:
return ante.DefaultSigVerificationGasConsumer(meter, sig, params)
}
}
1 change: 0 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,6 @@ func (app *InitiaApp) setAnteHandler(
BankKeeper: app.BankKeeper,
FeegrantKeeper: app.FeeGrantKeeper,
SignModeHandler: app.txConfig.SignModeHandler(),
SigGasConsumer: cosmosante.DefaultSigVerificationGasConsumer,
},
IBCkeeper: app.IBCKeeper,
MoveKeeper: movekeeper.NewDexKeeper(app.MoveKeeper),
Expand Down
4 changes: 4 additions & 0 deletions app/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/cosmos/cosmos-sdk/std"

"github.com/initia-labs/initia/app/params"
cryptocodec "github.com/initia-labs/initia/crypto/codec"
)

var legacyCodecRegistered = false
Expand All @@ -14,13 +15,16 @@ func MakeEncodingConfig() params.EncodingConfig {
encodingConfig := params.MakeEncodingConfig()
std.RegisterLegacyAminoCodec(encodingConfig.Amino)
std.RegisterInterfaces(encodingConfig.InterfaceRegistry)
cryptocodec.RegisterCrypto(encodingConfig.Amino)
cryptocodec.RegisterInterfaces(encodingConfig.InterfaceRegistry)
ModuleBasics.RegisterLegacyAminoCodec(encodingConfig.Amino)
ModuleBasics.RegisterInterfaces(encodingConfig.InterfaceRegistry)

if !legacyCodecRegistered {
// authz module use this codec to get signbytes.
// authz MsgExec can execute all message types,
// so legacy.Cdc need to register all amino messages to get proper signature
cryptocodec.RegisterCrypto(legacy.Cdc)
ModuleBasics.RegisterLegacyAminoCodec(legacy.Cdc)
legacyCodecRegistered = true
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/initiad/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/server"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -31,6 +32,7 @@ import (

initiaapp "github.com/initia-labs/initia/app"
"github.com/initia-labs/initia/app/params"
initiahd "github.com/initia-labs/initia/crypto/hd"
genutilcli "github.com/initia-labs/initia/x/genutil/client/cli"
moveconfig "github.com/initia-labs/initia/x/move/config"
)
Expand Down Expand Up @@ -77,7 +79,10 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
WithInput(os.Stdin).
WithAccountRetriever(types.AccountRetriever{}).
WithHomeDir(initiaapp.DefaultNodeHome).
WithViper(initiaapp.EnvPrefix)
WithViper(initiaapp.EnvPrefix).
WithKeyringOptions(func(options *keyring.Options) {
options.SupportedAlgos = append(options.SupportedAlgos, initiahd.EthSecp256k1)
})

rootCmd := &cobra.Command{
Use: basename,
Expand Down
14 changes: 14 additions & 0 deletions crypto/codec/amino.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package codec

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

ethsecp256k1 "github.com/initia-labs/initia/crypto/keys/eth/secp256k1"
)

// RegisterCrypto registers all crypto dependency types with the provided Amino
// codec.
func RegisterCrypto(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&ethsecp256k1.PubKey{}, ethsecp256k1.PubKeyName, nil)
cdc.RegisterConcrete(&ethsecp256k1.PrivKey{}, ethsecp256k1.PrivKeyName, nil)
}
14 changes: 14 additions & 0 deletions crypto/codec/proto.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"

ethsecp256k1 "github.com/initia-labs/initia/crypto/keys/eth/secp256k1"
)

// RegisterInterfaces registers the sdk.Tx interface.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &ethsecp256k1.PubKey{})
registry.RegisterImplementations((*cryptotypes.PrivKey)(nil), &ethsecp256k1.PrivKey{})
}
37 changes: 37 additions & 0 deletions crypto/hd/algo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package hd

import (
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/types"

"github.com/initia-labs/initia/crypto/keys/eth/secp256k1"
)

var (
// EthSecp256k1Type uses the ethereum secp256k1 ECDSA parameters.
EthSecp256k1Type = hd.PubKeyType("eth_secp256k1")
)

// EthSecp256k1 uses the Bitcoin secp256k1 ECDSA parameters.
var EthSecp256k1 = ethSecp256k1Algo{}

type ethSecp256k1Algo struct{}

func (s ethSecp256k1Algo) Name() hd.PubKeyType {
return EthSecp256k1Type
}

// Derive derives and returns the secp256k1 private key for the given seed and HD path.
func (s ethSecp256k1Algo) Derive() hd.DeriveFn {
return hd.Secp256k1.Derive()
}

// Generate generates a eth_secp256k1 private key from the given bytes.
func (s ethSecp256k1Algo) Generate() hd.GenerateFn {
return func(bz []byte) types.PrivKey {
bzArr := make([]byte, secp256k1.PrivKeySize)
copy(bzArr, bz)

return &secp256k1.PrivKey{Key: bzArr}
}
}
11 changes: 11 additions & 0 deletions crypto/keys/eth/secp256k1/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package secp256k1

import "golang.org/x/crypto/sha3"

func Keccak256(msg []byte) []byte {
hasher := sha3.NewLegacyKeccak256()
if _, err := hasher.Write(msg); err != nil {
panic(err)

Check warning on line 8 in crypto/keys/eth/secp256k1/hash.go

View check run for this annotation

Codecov / codecov/patch

crypto/keys/eth/secp256k1/hash.go#L8

Added line #L8 was not covered by tests
}
return hasher.Sum(nil)
}
Loading
Loading