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

feat(keys): return of the eth_secp256k1 #1264

Merged
merged 3 commits into from
Nov 1, 2023
Merged

feat(keys): return of the eth_secp256k1 #1264

merged 3 commits into from
Nov 1, 2023

Conversation

itsdevbear
Copy link
Member

@itsdevbear itsdevbear commented Nov 1, 2023

Summary by CodeRabbit

New Features:

  • Introduced support for Ethereum secp256k1 keys in the Cosmos SDK, enhancing compatibility with Ethereum-based systems.
  • Added a new function to generate Bip32 HD wallets from a mnemonic string, improving key management capabilities.
  • Implemented a new keyring option to set the supported algorithms to only hd.EthSecp256k1, providing more control over cryptographic operations.

Refactor:

  • Updated cryptographic algorithm for key generation from "secp256k1" to "eth_secp256k1" across multiple scripts, aligning with the new Ethereum secp256k1 keys support.

Tests:

  • Added comprehensive test coverage for the new Ethereum secp256k1 keys functionality, ensuring robust and reliable operations.

Chores:

  • Updated import statements and package registrations to accommodate the new Ethereum secp256k1 keys support.

Copy link

coderabbitai bot commented Nov 1, 2023

Walkthrough

The changes primarily focus on integrating Ethereum's secp256k1 cryptographic algorithm into the Cosmos SDK. This includes the addition of new packages and files, updates to existing ones, and modifications to test files to ensure the correct functioning of the new features. The changes also reflect in the configuration files, where the key generation algorithm has been updated to eth_secp256k1.

Changes

File(s) Summary
cosmos/crypto/codec/codec.go, cosmos/crypto/codec/codec_test.go Addition of new Go files to register and test the ethsecp256k1 package.
cosmos/crypto/hd/... Introduction of a new package for handling HD keys, with the addition of new functions and tests.
cosmos/crypto/keyring/... Addition of new files to set the supported algorithms for keyring options to hd.EthSecp256k1.
cosmos/crypto/keys/ethsecp256k1/... Changes to integrate Ethereum secp256k1 keys within the Cosmos SDK, including functions for signing and verifying ECDSA signatures.
e2e/... Modifications in the configuration files to change the cryptographic algorithm used for key generation to eth_secp256k1.
e2e/testapp/app.go, e2e/testapp/polard/cmd/root.go Updates to register eth_secp256k1 keys in the interface registry and integrate Ethereum functionality into the Polaris sample app.
eth/crypto/imported.go Changes in the crypto package to include functions and variables related to Ethereum's secp256k1 cryptographic algorithm.
proto/polaris/crypto/ethsecp256k1/v1/keys.proto Introduction of a new file to define PubKey and PrivKey message types for Ethereum's secp256k1 keys.

Poem

🍂 As autumn leaves fall, so does old code retire, 🍁

Replaced by new lines, that set the world on fire. 🔥

Ethereum's secp256k1, now in Cosmos' attire, 🌌

A change that developers truly admire. 👩‍💻👨‍💻

So let's celebrate this merge, with a hot apple cider, 🍎🍹

And continue to code, as the nights grow wider. 🌃💻

Here's to the season of change, our eternal guide, 🍂🧭

And to the joy of coding, that we all confide. 😄💖


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.json

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between f659c51 and d23c3c1.
Files ignored due to filter (4)
  • cosmos/crypto/keys/ethsecp256k1/keys.pb.go
  • cosmos/go.mod
  • cosmos/go.sum
  • go.work.sum
Files selected for processing (27)
  • cosmos/api/polaris/crypto/ethsecp256k1/v1/keys.pulsar.go (1 hunks)
  • cosmos/crypto/codec/codec.go (1 hunks)
  • cosmos/crypto/codec/codec_test.go (1 hunks)
  • cosmos/crypto/hd/algo.go (1 hunks)
  • cosmos/crypto/hd/algo_test.go (1 hunks)
  • cosmos/crypto/hd/hd.go (1 hunks)
  • cosmos/crypto/hd/hd_test.go (1 hunks)
  • cosmos/crypto/keyring/option.go (1 hunks)
  • cosmos/crypto/keyring/option_test.go (1 hunks)
  • cosmos/crypto/keys/ethsecp256k1/keys.go (1 hunks)
  • cosmos/crypto/keys/ethsecp256k1/keys_test.go (1 hunks)
  • cosmos/crypto/keys/ethsecp256k1/signature_cgo.go (1 hunks)
  • cosmos/crypto/keys/ethsecp256k1/signature_cgo_test.go (1 hunks)
  • e2e/hive/clients/polard/entrypoint.sh (1 hunks)
  • e2e/precompile/polard/start-node.sh (1 hunks)
  • e2e/testapp/app.go (2 hunks)
  • e2e/testapp/docker/local/docker-init.sh (1 hunks)
  • e2e/testapp/docker/seed/init.sh (1 hunks)
  • e2e/testapp/docker/seed/scripts/seed0-init-step1.sh (1 hunks)
  • e2e/testapp/docker/seed/scripts/seed0-init-step2.sh (1 hunks)
  • e2e/testapp/docker/seed/scripts/seed1-init-step1.sh (1 hunks)
  • e2e/testapp/docker/validator/scripts/val0-init-step1.sh (1 hunks)
  • e2e/testapp/docker/validator/scripts/val1-init-step1.sh (1 hunks)
  • e2e/testapp/entrypoint.sh (1 hunks)
  • e2e/testapp/polard/cmd/root.go (2 hunks)
  • eth/crypto/imported.go (1 hunks)
  • proto/polaris/crypto/ethsecp256k1/v1/keys.proto (1 hunks)
Files not summarized due to errors (1)
  • cosmos/api/polaris/crypto/ethsecp256k1/v1/keys.pulsar.go: Error: Message exceeds token limit
Files not reviewed due to errors (1)
  • cosmos/api/polaris/crypto/ethsecp256k1/v1/keys.pulsar.go (Error: diff too large)
Files skipped from review due to trivial changes (9)
  • cosmos/crypto/codec/codec.go
  • cosmos/crypto/codec/codec_test.go
  • cosmos/crypto/hd/algo.go
  • cosmos/crypto/hd/algo_test.go
  • cosmos/crypto/hd/hd_test.go
  • cosmos/crypto/keys/ethsecp256k1/signature_cgo_test.go
  • e2e/testapp/app.go
  • e2e/testapp/docker/seed/init.sh
  • proto/polaris/crypto/ethsecp256k1/v1/keys.proto
Additional comments: 40
e2e/testapp/docker/seed/scripts/seed1-init-step1.sh (1)
  • 25-29: The default value for KEYALGO has been changed to "eth_secp256k1". Ensure that the system supports this new key algorithm and that all dependencies are updated accordingly.
e2e/hive/clients/polard/entrypoint.sh (1)
  • 24-30: The change in the KEYALGO variable from "secp256k1" to "eth_secp256k1" indicates a shift in the cryptographic algorithm used for key generation. Ensure that the new algorithm is compatible with the rest of the system and that all dependencies have been updated accordingly.
e2e/testapp/docker/validator/scripts/val1-init-step1.sh (1)
  • 22-28: The change in the KEYALGO variable from "secp256k1" to "eth_secp256k1" suggests a modification in the cryptographic algorithm used for key generation or verification. Ensure that the new algorithm "eth_secp256k1" is fully integrated and compatible with the rest of the system.
- KEYALGO="secp256k1"
+ KEYALGO="eth_secp256k1"
e2e/testapp/docker/seed/scripts/seed0-init-step2.sh (1)
  • 25-29: The change from "secp256k1" to "eth_secp256k1" indicates a shift in the cryptographic algorithm used for key generation. Ensure that the rest of the codebase and any dependent systems are compatible with this change.
-    KEYALGO="secp256k1"
+    KEYALGO="eth_secp256k1"
e2e/precompile/polard/start-node.sh (1)
  • 28-34: The change in the cryptographic algorithm used for key generation and management from "secp256k1" to "eth_secp256k1" is noted. Ensure that this change is compatible with the rest of the system and that all dependencies are updated accordingly.
- KEYALGO="secp256k1"
+ KEYALGO="eth_secp256k1"
e2e/testapp/docker/local/docker-init.sh (1)
  • 32-32: The change from "secp256k1" to "eth_secp256k1" indicates a shift in the cryptographic algorithm used for key generation. Ensure that this change is compatible with the rest of the system and that all dependencies are updated accordingly.
- KEYALGO="secp256k1"
+ KEYALGO="eth_secp256k1"
cosmos/crypto/keyring/option.go (3)
  • 1-19: The license header is included correctly. Ensure that the license is compatible with the project's license.

  • 23-27: The imported packages are correct and necessary for the function defined in this file.

  • 29-35: The function OnlyEthSecp256k1Option is correctly defined and sets the supported algorithms for keyring options to hd.EthSecp256k1. Ensure that this is the intended behavior and that the hd.EthSecp256k1 algorithm is supported everywhere this keyring option is used.

e2e/testapp/polard/cmd/root.go (3)
  • 63-70: The new imports ethcryptocodec and keyring are introduced to support Ethereum-compatible key generation and management. Ensure these packages are correctly implemented and tested.

  • 115-115: The WithKeyringOptions(keyring.OnlyEthSecp256k1Option()) function call sets the keyring to only support the eth_secp256k1 option. This restricts the keyring to only generate Ethereum-compatible keys.

  • 118-118: The ethcryptocodec.RegisterInterfaces(interfaceRegistry) function call registers the PubKey and PrivKey interfaces for the eth_secp256k1 key type. This is necessary for the keyring to generate and manage Ethereum-compatible keys.

e2e/testapp/docker/seed/scripts/seed0-init-step1.sh (1)
  • 28-28: The change from "secp256k1" to "eth_secp256k1" indicates a shift in the cryptographic algorithm used for key generation. Ensure that this change is compatible with the rest of the system and that all dependencies are updated accordingly.
-    KEYALGO="secp256k1"
+    KEYALGO="eth_secp256k1"
cosmos/crypto/keys/ethsecp256k1/signature_cgo.go (3)
  • 1-19: The license header is included correctly. Ensure that the license is compatible with the project's license.

  • 27-43: The Sign function seems to be correctly implemented. It hashes the input if it's not already a hash, converts the private key to ECDSA format, and then signs the hash. However, it's important to note that the function will always hash the input, even if it's already a hash. This could lead to unexpected results if the caller expects the function to sign the input directly without hashing it first. Consider adding a comment to clarify this behavior.

  • 45-64: The VerifySignature function also seems to be correctly implemented. It hashes the message if it's not already a hash, checks the signature length, removes the recovery ID if it's included in the signature, and then verifies the signature. Similar to the Sign function, it will always hash the message, even if it's already a hash. Consider adding a comment to clarify this behavior.

e2e/testapp/entrypoint.sh (1)
  • 28-34: The change from "secp256k1" to "eth_secp256k1" is consistent with the pull request summary. Ensure that the new key algorithm "eth_secp256k1" is fully implemented and compatible with the rest of the system.
cosmos/crypto/keys/ethsecp256k1/keys.go (5)
  • 1-19: The license header has been updated. Ensure that the new license is compatible with the project's licensing terms.

  • 23-33: The import paths have been updated. Ensure that these packages are available in the project's dependencies.

  • 35-42: New constants have been defined for private key size, public key size, and key type. These constants seem to be correctly defined.

  • 48-101: The PrivKey type has been implemented with methods Bytes(), PubKey(), Equals(), Type(), GenPrivKey(), and ToECDSA(). These methods seem to be correctly implemented, but ensure that they are tested thoroughly.

  • 106-137: The PubKey type has been implemented with methods Address(), Bytes(), Type(), and Equals(). These methods seem to be correctly implemented, but ensure that they are tested thoroughly.

eth/crypto/imported.go (2)
  • 23-26: The import of github.com/ethereum/go-ethereum/crypto/secp256k1 is new and necessary for the Ethereum-compatible key generation and management.

  • 28-49: The variables are assigned functions from the crypto and secp256k1 packages. This is a common pattern in Go when you want to expose functions from another package in your package. However, it's important to ensure that these functions are used correctly and that their error returns are properly handled in the code that uses them.

e2e/testapp/docker/validator/scripts/val0-init-step1.sh (1)
  • 22-28: The change in the KEYALGO variable from "secp256k1" to "eth_secp256k1" suggests a modification in the cryptographic algorithm used for key generation or verification. Ensure that the new algorithm "eth_secp256k1" is fully supported and tested in the system. Also, verify that this change doesn't break any existing functionality or dependencies that rely on the old "secp256k1" algorithm.
cosmos/crypto/keys/ethsecp256k1/keys_test.go (4)
  • 1-19: The license header is included correctly. Ensure that the license is compatible with the project's license.

  • 23-32: The import statements are correct and necessary for the tests.

  • 34-37: The test suite setup is correct.

  • 39-80: The tests are well-structured and cover the necessary functionality of the ethsecp256k1 package. They test the generation of private keys, the equality and inequality of keys, and the conversion of private keys to Ethereum addresses. The tests also validate the type of the keys.

cosmos/crypto/keyring/option_test.go (11)
  • 1-19: The license information is correctly placed at the top of the file. Ensure that the license is compatible with the project's license.

  • 23-41: The import statements are well organized and separated by functionality. Ensure that all imported packages are used in the code.

  • 45-50: The registerCodec function is used to register interfaces. It's a good practice to separate this logic into a function for reusability.

  • 52-55: The TestKeyring function is the entry point for running the tests in this file. It uses the Ginkgo BDD testing framework.

  • 57-178: The tests are well organized into different contexts and scenarios. Each test has a clear purpose and uses the BeforeEach and AfterEach hooks to set up and tear down the test environment. This is a good practice as it ensures each test runs in isolation.

  • 64-79: The BeforeEach block is used to set up the test environment. It creates a temporary directory and a new keyring. Ensure that the keyring.New function is updated to accept the new OnlyEthSecp256k1Option option.

  • 81-84: The AfterEach block is used to clean up the test environment. It removes the temporary directory created in the BeforeEach block. This is a good practice as it ensures that each test runs in a clean environment.

  • 86-92: This test case checks that the Key function returns an error when trying to retrieve a non-existent key. This is a good negative test case.

  • 94-118: This test case checks that the NewMnemonic function correctly creates a new mnemonic and keyring record. It verifies the mnemonic, key name, key type, and public key type. This is a good positive test case.

  • 120-147: This test case checks that the Derive function correctly derives an HD path from a mnemonic. It also checks that the function returns an error when given an incorrect HD path. This is a good positive and negative test case.

  • 149-176: This test case checks that the Generate function correctly generates a private key from a derived HD path. It also checks that the BytesToAddress function correctly converts the public key address to an Ethereum address. This is a good positive test case.

cosmos/crypto/hd/hd.go Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between d23c3c1 and a0afc91.
Files selected for processing (1)
  • cosmos/crypto/keyring/option_test.go (1 hunks)
Additional comments: 8
cosmos/crypto/keyring/option_test.go (8)
  • 1-19: The license information is included at the top of the file. Ensure that the use of this license is appropriate for your project and that it is correctly referenced.

  • 23-41: The import statements are correctly formatted and organized. The imported packages are relevant to the tests being performed.

  • 45-50: The registerCodec function is correctly defined and registers the necessary interfaces for the codec.

  • 52-55: The TestKeyring function correctly sets up the test suite for the keyring package.

  • 57-94: The BeforeEach and AfterEach functions are correctly defined and perform necessary setup and teardown operations for each test. The Key operations context and its test case are correctly defined.

  • 96-120: The NewMnemonic operation context and its test cases are correctly defined. The BeforeEach function correctly sets up the necessary variables for the tests.

  • 122-150: The HD path operations context and its test cases are correctly defined. The BeforeEach function correctly sets up the necessary variables for the tests.

  • 151-179: The Key generation and retrieval context and its test cases are correctly defined. The BeforeEach function correctly sets up the necessary variables for the tests.

Copy link

codecov bot commented Nov 1, 2023

Codecov Report

Merging #1264 (a0afc91) into main (f659c51) will increase coverage by 0.35%.
The diff coverage is 63.47%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1264      +/-   ##
==========================================
+ Coverage   48.88%   49.23%   +0.35%     
==========================================
  Files          78       84       +6     
  Lines        4674     4789     +115     
==========================================
+ Hits         2285     2358      +73     
- Misses       2227     2257      +30     
- Partials      162      174      +12     
Files Coverage Δ
cosmos/crypto/codec/codec.go 100.00% <100.00%> (ø)
cosmos/crypto/keyring/option.go 100.00% <100.00%> (ø)
cosmos/crypto/hd/algo.go 83.33% <83.33%> (ø)
cosmos/crypto/keys/ethsecp256k1/signature_cgo.go 73.91% <73.91%> (ø)
cosmos/crypto/keys/ethsecp256k1/keys.go 64.86% <64.86%> (ø)
cosmos/crypto/hd/hd.go 40.00% <40.00%> (ø)

@itsdevbear itsdevbear merged commit e29bc32 into main Nov 1, 2023
14 checks passed
@itsdevbear itsdevbear deleted the eth_secp256k1 branch November 1, 2023 15:59
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.

2 participants