-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
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 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. 📒 Files selected for processing (1)
WalkthroughThe 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
Poem
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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
📒 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 suggestionVerify signature component lengths.
The code assumes that
signature.r
andsignature.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)
@@ -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'); |
There was a problem hiding this comment.
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
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'); |
There was a problem hiding this comment.
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.
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)
807dab2
to
9b0e0d2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixes signature encoding?
|
Summary by CodeRabbit