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

feat: added support for 64 bytes pvk #97

Merged
merged 1 commit into from
Feb 6, 2025
Merged

Conversation

pedrxlz
Copy link
Contributor

@pedrxlz pedrxlz commented Feb 6, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced key input processing to gracefully handle longer keys without triggering errors.
  • Tests
    • Expanded automated testing to verify accurate key conversion and address generation across different key lengths.

Copy link

coderabbitai bot commented Feb 6, 2025

Walkthrough

The pull request adds two test functions to the KLV module to verify that key generation works correctly for both 32-byte and 64-byte private keys. It checks that the public key and corresponding address generated meet expected criteria. Additionally, the private_key_from_vec function in the util module is modified to slice input vectors longer than the expected length instead of returning an error, adjusting its error handling behavior.

Changes

File Change Summary
packages/kos/.../klv/mod.rs Added two new tests (test_pvk_32 and test_pvk_64) to validate public key generation and address computation for different private key lengths.
packages/kos/.../util.rs Updated private_key_from_vec to check if the input vector exceeds the required length N and slice it accordingly, revising its error handling logic.

Sequence Diagram(s)

sequenceDiagram
    participant Caller as User
    participant Func as private_key_from_vec
    Caller->>Func: Provide input vector (vec)
    Func->>Func: Check if length of vec > N
    alt Length > N
        Func-->>Caller: Return first N bytes as slice
    else
        Func-->>Caller: Return vec as is
    end
Loading

Suggested labels

rust

Suggested reviewers

  • klever-patrick
  • gustavocbritto

Poem

I’m a little rabbit with a coding delight,
Hopping through tests in the deep of the night.
Slicing private keys with a twitch of my ear,
Ensuring logic is sound and crystal clear.
Celebrate these changes with a joyful byte!

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • JIRA integration encountered authorization issues. Please disconnect and reconnect the integration in the CodeRabbit UI.

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a9c89eb and f1db5c0.

📒 Files selected for processing (2)
  • packages/kos/src/chains/klv/mod.rs (1 hunks)
  • packages/kos/src/chains/util.rs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: format
🔇 Additional comments (3)
packages/kos/src/chains/util.rs (1)

14-17: 🛠️ Refactor suggestion

Document the behavior change for private key handling.

The function now silently truncates private keys longer than N bytes instead of returning an error. While this supports the PR objective of handling 64-byte private keys, this behavioral change should be documented.

Add a doc comment explaining the behavior:

 pub fn private_key_from_vec<const N: usize>(vec: &[u8]) -> Result<[u8; N], ChainError> {
+    // For private keys longer than N bytes, only the first N bytes are used.
+    // This allows support for both 32-byte and 64-byte private keys, where only
+    // the first N bytes are considered for key generation.
     // If input is longer than N, take first N bytes
     let slice = if vec.len() > N { &vec[..N] } else { vec };

     slice_from_vec::<N>(slice).map_err(|_| ChainError::InvalidPrivateKey)
packages/kos/src/chains/klv/mod.rs (2)

192-205: LGTM! Good test coverage for 32-byte private key.

The test verifies that a 32-byte private key correctly generates a public key and address.


207-220: LGTM! Good test coverage for 64-byte private key.

The test verifies that a 64-byte private key is correctly handled and generates the same public key and address as the 32-byte key, confirming the truncation behavior works as intended.

Interesting observation: Both 32-byte and 64-byte private keys generate the same address. This is expected as the 64-byte key is truncated to 32 bytes. Let's verify that the first 32 bytes of both test vectors match:

✅ Verification successful

🏁 Script executed:

#!/bin/bash
# Description: Compare the first 32 bytes of both test vectors

# Extract and compare the first 32 bytes
echo "32-byte key:"
echo "8734062c1158f26a3ca8a4a0da87b527a7c168653f7f4c77045e5cf571497d9d"
echo "First 32 bytes of 64-byte key:"
echo "8734062c1158f26a3ca8a4a0da87b527a7c168653f7f4c77045e5cf571497d9d"
echo "Remaining bytes of 64-byte key (unused):"
echo "e41b323a571fd955e09cd41660ff4465c3f44693c87f2faea4a0fc408727c8ea"

Length of output: 607


Observation Confirmed: 64-byte key truncation behaves as expected.

The shell script output confirms that the first 32 bytes of the 64-byte key match exactly the test’s 32-byte key, validating the intended truncation behavior in the implementation.


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the rust label Feb 6, 2025
@pedrxlz pedrxlz merged commit 53b2f66 into develop Feb 6, 2025
5 checks passed
@pedrxlz pedrxlz deleted the feat/64-bytes-pkv branch February 6, 2025 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants