Skip to content

Commit

Permalink
fix signing
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Dec 1, 2023
1 parent 455664f commit d99d7c9
Show file tree
Hide file tree
Showing 20 changed files with 900 additions and 361 deletions.
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
1 change: 1 addition & 0 deletions app/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func MakeEncodingConfig() params.EncodingConfig {
// 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
1 change: 1 addition & 0 deletions crypto/codec/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import (
// codec.
func RegisterCrypto(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&ethsecp256k1.PubKey{}, ethsecp256k1.PubKeyName, nil)
cdc.RegisterConcrete(&ethsecp256k1.PrivKey{}, ethsecp256k1.PrivKeyName, nil)
}
1 change: 1 addition & 0 deletions crypto/codec/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import (
// 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)
}
return hasher.Sum(nil)
}
Loading

0 comments on commit d99d7c9

Please sign in to comment.