-
Notifications
You must be signed in to change notification settings - Fork 44
feat(sdk): add validation/tests for registerName publicKeyId parameter #2832
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
base: master
Are you sure you want to change the base?
Conversation
…election Add comprehensive validation before signing DPNS registration state transitions to catch common errors early and provide clear, actionable error messages to developers. Fixes issue where users accidentally use MASTER key (ID 0) which is not allowed for DPNS registration, resulting in cryptic error messages only after signing attempt.
|
@coderabbitai review |
WalkthroughThese changes introduce a new Changes
Sequence DiagramsequenceDiagram
participant User as User Code
participant JsFacade as JS Facade
participant Wasm as WASM SDK
participant KeyValidator as Key Validator
User->>JsFacade: registerName(label, identityId, publicKeyId, ...)
activate JsFacade
JsFacade->>JsFacade: Validate publicKeyId (not null/undefined, non-negative)
alt publicKeyId invalid
JsFacade-->>User: Throw validation error
else publicKeyId valid
JsFacade->>Wasm: w.dpnsRegisterName(label, identityId, publicKeyId, privateKeyWif, ...)
activate Wasm
Wasm->>KeyValidator: Get and validate identity public key
activate KeyValidator
KeyValidator->>KeyValidator: Check purpose = AUTHENTICATION
KeyValidator->>KeyValidator: Check security level = CRITICAL/HIGH
KeyValidator->>KeyValidator: Verify private key matches
alt Key validation fails
KeyValidator-->>Wasm: Invalid key error
Wasm-->>User: Throw validation error
else Key validation succeeds
KeyValidator-->>Wasm: Valid key
Wasm->>Wasm: Register DPNS name
Wasm-->>JsFacade: Success
deactivate Wasm
end
deactivate KeyValidator
JsFacade-->>User: Registration result
end
deactivate JsFacade
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
✅ Actions performedReview triggered.
|
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: 0
🧹 Nitpick comments (2)
packages/wasm-sdk/src/dpns.rs (1)
82-141: Validation logic is correct and provides excellent error messages.The key validation properly enforces DPNS requirements (AUTHENTICATION purpose, CRITICAL/HIGH security levels) and provides helpful guidance by listing suitable alternative keys.
Consider these minor refinements for more idiomatic Rust:
Lines 107-118: Replace the if-else and string concatenation with a match expression and format! macro:
- let level_name = if k.security_level() == SecurityLevel::CRITICAL { - "CRITICAL" - } else { - "HIGH" - }; - Some( - String::from(" Key ") - + &key_id.to_string() - + ": " - + level_name - + " security level", - ) + let level_name = match k.security_level() { + SecurityLevel::CRITICAL => "CRITICAL", + SecurityLevel::HIGH => "HIGH", + _ => return None, + }; + Some(format!(" Key {}: {} security level", key_id, level_name))packages/js-evo-sdk/src/dpns/facade.ts (1)
60-81: Solid input validation with helpful error messages.The validation logic correctly handles the common error cases (undefined, null, non-number, negative) and provides clear guidance about which keys are suitable for DPNS registration. This addresses the PR objective of providing early validation before cryptographic operations.
For completeness, you could add validation for
NaNandInfinity, though these are unlikely in practice:- if (typeof publicKeyId !== 'number' || publicKeyId < 0) { + if (typeof publicKeyId !== 'number' || !Number.isFinite(publicKeyId) || publicKeyId < 0) { throw new Error(`publicKeyId must be a non-negative number, got: ${publicKeyId}`); }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
packages/js-evo-sdk/src/dpns/facade.ts(1 hunks)packages/js-evo-sdk/tests/unit/facades/dpns.spec.mjs(1 hunks)packages/wasm-sdk/src/dpns.rs(2 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
packages/**/tests/**
📄 CodeRabbit inference engine (AGENTS.md)
Place unit and integration tests alongside each package in packages//tests
Files:
packages/js-evo-sdk/tests/unit/facades/dpns.spec.mjs
**/*.rs
📄 CodeRabbit inference engine (CLAUDE.md)
**/*.rs: Format Rust code with cargo fmt
Run Clippy linter for Rust code
Files:
packages/wasm-sdk/src/dpns.rs
packages/wasm-sdk/**
📄 CodeRabbit inference engine (CLAUDE.md)
Keep WASM SDK docs in sync (run generate_docs.py) when updating the WASM SDK
Files:
packages/wasm-sdk/src/dpns.rs
packages/**/**/*.{js,ts,jsx,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
packages/**/**/*.{js,ts,jsx,tsx}: Adhere to ESLint with Airbnb/TypeScript configs for JS/TS code
Use camelCase for JS/TS variables and functions
Use PascalCase for JS/TS classes
Prefer kebab-case filenames within JS packages
Files:
packages/js-evo-sdk/src/dpns/facade.ts
🔇 Additional comments (4)
packages/wasm-sdk/src/dpns.rs (2)
5-7: LGTM! Imports support the new validation logic.The imported traits and enums are necessary for the key validation functionality added below.
143-155: Excellent security validation!Verifying that the private key matches the public key via
signer.can_sign_withbefore proceeding prevents cryptographic failures and provides clear error messages early in the flow. This directly addresses the PR objective of catching errors before attempting to sign state transitions.packages/js-evo-sdk/tests/unit/facades/dpns.spec.mjs (1)
58-143: Excellent test coverage for publicKeyId validation!The test suite comprehensively covers all edge cases:
- Missing, undefined, and null values
- Invalid types (string)
- Invalid ranges (negative numbers)
- Valid positive case
The error message assertions verify that users receive helpful guidance, directly supporting the PR objective of improving error messages for DPNS registration.
packages/js-evo-sdk/src/dpns/facade.ts (1)
36-59: Outstanding documentation with clear security guidance!The JSDoc provides comprehensive guidance that will help developers avoid the cryptic error described in the PR objectives. The explicit warning about key security levels and the practical example make the API much more developer-friendly.
| async registerName(args: { label: string; identityId: string; publicKeyId: number; privateKeyWif: string; onPreorder?: Function }): Promise<any> { | ||
| const { label, identityId, publicKeyId, privateKeyWif, onPreorder } = args; | ||
|
|
||
| // Validate inputs |
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.
How this is possible? Typing is not allow it. Oh, when you use it form JS, not TypeScript?
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.
Yes, it was from JS. Just trying to make things easier for people like me that don't know what they're doing 🤪 You can close the PR if it is a bad approach.
Issue being fixed or feature implemented
Developers using the DPNS registration API were encountering a cryptic error message "Invalid public key security level MASTER" when the identity key parameter was not passed. This error only appeared after attempting to sign the state transition, making it difficult to understand what went wrong and how to fix it.
What was done?
Added comprehensive early validation and helpful error messages for DPNS name registration:
packages/wasm-sdk/src/dpns.rs): Validate key purpose is AUTHENTICATION, security level is CRITICAL or HIGH (not MASTER), and private key matches the specified public key before attempting cryptographic operationspackages/js-evo-sdk/src/dpns/facade.ts): Add input validation forpublicKeyIdparameter with clear error messages and comprehensive JSDoc documentationpackages/js-evo-sdk/tests/unit/facades/dpns.spec.mjs): Add 6 test cases covering validation of omitted, undefined, null, negative, non-number, and validpublicKeyIdvaluesError messages now explain which requirement failed, list all suitable keys available in the identity, and provide specific guidance (e.g., "Use key 1 (CRITICAL) or 2 (HIGH), NOT 0 (MASTER)").
How Has This Been Tested?
Breaking Changes
None. This is fully backwards compatible - it only adds validation and improves error messages.
Checklist:
For repository code-owners and collaborators only
Summary by CodeRabbit
Release Notes
New Features
Tests