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

Pad r and s to 32 bytes #14

Merged
merged 2 commits into from
Jan 10, 2025
Merged

Pad r and s to 32 bytes #14

merged 2 commits into from
Jan 10, 2025

Conversation

Inkvi
Copy link
Collaborator

@Inkvi Inkvi commented Jan 10, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improved signature generation process to ensure consistent and properly formatted signature output
    • Enhanced handling of signature components during processing

Copy link

coderabbitai bot commented Jan 10, 2025

Warning

Rate limit exceeded

@Inkvi has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 24 minutes and 30 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between cd08237 and 9b0e0d2.

📒 Files selected for processing (1)
  • src/server.ts (1 hunks)

Walkthrough

The pull request modifies the signature processing logic in the server-side TypeScript code. The changes focus on improving the handling of signature components by converting signature values to hexadecimal strings, padding them to a consistent length, and then converting them back to buffers. This approach ensures a more standardized format for signature representation while maintaining the existing control flow of the signature generation process.

Changes

File Change Summary
src/server.ts Modified signature processing logic to convert signature.r and signature.s to padded hexadecimal strings before buffer conversion

Poem

🐰 Signature dance, a hexadecimal twirl,
Padding bytes with a computational swirl,
Buffers align, sixty-four chars just right,
CodeRabbit's magic makes signatures tight!
A cryptographic hop, precise and neat 🔒


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

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.

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.

Actionable comments posted: 3

🧹 Nitpick comments (1)
src/server.ts (1)

71-71: Consider using Buffer.alloc for predictable buffer sizes.

When concatenating fixed-size components, consider pre-allocating the buffer for better performance.

-     const res = Buffer.concat([rPadded, sPadded, bigintToBuffer(signature.v)]).toString('hex');
+     const resultBuffer = Buffer.alloc(65); // 32 bytes for r, 32 bytes for s, 1 byte for v
+     rPadded.copy(resultBuffer, 0);
+     sPadded.copy(resultBuffer, 32);
+     bigintToBuffer(signature.v).copy(resultBuffer, 64);
+     const res = resultBuffer.toString('hex');
🧰 Tools
🪛 Biome (1.9.4)

[error] 71-71: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5fad47b and cd08237.

📒 Files selected for processing (1)
  • src/server.ts (2 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
src/server.ts

[error] 65-65: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 66-66: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 67-67: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 68-68: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 69-69: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 70-70: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 71-71: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)

🔇 Additional comments (1)
src/server.ts (1)

67-70: 🛠️ Refactor suggestion

Verify signature component lengths.

The code assumes that signature.r and signature.s are valid ECDSA signature components. Add validation to ensure they don't exceed 32 bytes after padding.

      const rHex = signature.r.toString('hex').padStart(64, '0');
      const sHex = signature.s.toString('hex').padStart(64, '0');
+     // Each signature component should be exactly 32 bytes (64 hex chars) after padding
+     if (rHex.length !== 64 || sHex.length !== 64) {
+       throw new Error('Invalid signature component length after padding');
+     }
      const rPadded = Buffer.from(rHex, 'hex');
      const sPadded = Buffer.from(sHex, 'hex');
🧰 Tools
🪛 Biome (1.9.4)

[error] 67-67: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 68-68: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 69-69: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 70-70: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)

src/server.ts Outdated Show resolved Hide resolved
@@ -61,8 +62,13 @@
case 'health_status':
return reply.code(200).send({result: 'ok'});
case 'sign':
const signature = await wallets.ecsign({keyId: keyId}, Buffer.from(params[0].slice(2), 'hex'), 1);
const res = Buffer.concat([signature.r, signature.s, bigintToBuffer(signature.v)]).toString('hex');
let digest = Buffer.from(params[0].slice(2), 'hex');
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add input validation for the digest.

The code should validate that the input digest is the correct length before processing.

+     if (!params[0] || typeof params[0] !== 'string' || !params[0].startsWith('0x')) {
+       return reply.code(400).send({error: 'Invalid digest format. Expected 0x-prefixed hex string'});
+     }
      let digest = Buffer.from(params[0].slice(2), 'hex');
+     if (digest.length !== 32) {
+       return reply.code(400).send({error: 'Invalid digest length. Expected 32 bytes'});
+     }

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 Biome (1.9.4)

[error] 65-65: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)

src/server.ts Outdated
Comment on lines 65 to 71
let digest = Buffer.from(params[0].slice(2), 'hex');
const signature = await wallets.ecsign({keyId: keyId}, digest, 1);
const rHex = signature.r.toString('hex').padStart(64, '0');
const sHex = signature.s.toString('hex').padStart(64, '0');
const rPadded = Buffer.from(rHex, 'hex');
const sPadded = Buffer.from(sHex, 'hex');
const res = Buffer.concat([rPadded, sPadded, bigintToBuffer(signature.v)]).toString('hex');
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add block scope to prevent variable leakage.

The static analysis tool correctly flags that variable declarations in switch cases can leak to other cases. Wrap the code in a block to prevent variable scope issues.

    case 'sign': {
      let digest = Buffer.from(params[0].slice(2), 'hex');
      const signature = await wallets.ecsign({keyId: keyId}, digest, 1);
      const rHex = signature.r.toString('hex').padStart(64, '0');
      const sHex = signature.s.toString('hex').padStart(64, '0');
      const rPadded = Buffer.from(rHex, 'hex');
      const sPadded = Buffer.from(sHex, 'hex');
      const res = Buffer.concat([rPadded, sPadded, bigintToBuffer(signature.v)]).toString('hex');
      return reply.code(200).send({result: res});
    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let digest = Buffer.from(params[0].slice(2), 'hex');
const signature = await wallets.ecsign({keyId: keyId}, digest, 1);
const rHex = signature.r.toString('hex').padStart(64, '0');
const sHex = signature.s.toString('hex').padStart(64, '0');
const rPadded = Buffer.from(rHex, 'hex');
const sPadded = Buffer.from(sHex, 'hex');
const res = Buffer.concat([rPadded, sPadded, bigintToBuffer(signature.v)]).toString('hex');
case 'sign': {
let digest = Buffer.from(params[0].slice(2), 'hex');
const signature = await wallets.ecsign({keyId: keyId}, digest, 1);
const rHex = signature.r.toString('hex').padStart(64, '0');
const sHex = signature.s.toString('hex').padStart(64, '0');
const rPadded = Buffer.from(rHex, 'hex');
const sPadded = Buffer.from(sHex, 'hex');
const res = Buffer.concat([rPadded, sPadded, bigintToBuffer(signature.v)]).toString('hex');
return reply.code(200).send({result: res});
}
🧰 Tools
🪛 Biome (1.9.4)

[error] 65-65: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 66-66: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 67-67: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 68-68: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 69-69: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 70-70: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 71-71: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)

@Inkvi Inkvi force-pushed the inkvi/r-s-32-bytes branch from 807dab2 to 9b0e0d2 Compare January 10, 2025 01:13
Copy link
Member

@dshiell dshiell left a comment

Choose a reason for hiding this comment

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

fixes signature encoding?

@Inkvi
Copy link
Collaborator Author

Inkvi commented Jan 10, 2025

fixes signature encoding?
Yes fixes errors like this

│ t=2025-01-09T22:33:12+0000 lvl=error msg="failed to sign signBytes" chainID=5003 signerEndpoint=http://rpc-signer:8000 error="invalid signature length: got 64, want 65"                                                                   │

@Inkvi Inkvi merged commit a3ec558 into main Jan 10, 2025
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants