-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from initia-labs/feat/eth-secp256k1
fea: eth secp256k1
- Loading branch information
Showing
19 changed files
with
1,385 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(ðsecp256k1.PubKey{}, ethsecp256k1.PubKeyName, nil) | ||
cdc.RegisterConcrete(ðsecp256k1.PrivKey{}, ethsecp256k1.PrivKeyName, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), ðsecp256k1.PubKey{}) | ||
registry.RegisterImplementations((*cryptotypes.PrivKey)(nil), ðsecp256k1.PrivKey{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.