-
Notifications
You must be signed in to change notification settings - Fork 7
fix(cli): standardize private key inputs and fix 0x prefix parsing error #423
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: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@offckb/cli": patch | ||
| --- | ||
|
|
||
| fix(cli): standardize private key inputs and fix 0x prefix parsing error (#422) |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -73,3 +73,43 @@ export function isValidVersion(version: unknown): boolean { | |||||||||
| // Test the version against the regex | ||||||||||
| return versionRegex.test(version); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| export function normalizePrivKey(privKey: string): string { | ||||||||||
| // Trim surrounding whitespaces | ||||||||||
| let key = privKey ? privKey.trim() : ''; | ||||||||||
|
|
||||||||||
| if (!key) { | ||||||||||
| throw new Error('Private key is required.'); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Strip surrounding quotes | ||||||||||
| if (key.startsWith('"') && key.endsWith('"')) { | ||||||||||
| key = key.slice(1, -1); | ||||||||||
| } | ||||||||||
| if (key.startsWith("'") && key.endsWith("'")) { | ||||||||||
| key = key.slice(1, -1); | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
||||||||||
| // Trim again to normalize whitespace that was inside surrounding quotes | |
| key = key.trim(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { normalizePrivKey } from '../src/util/validator'; | ||
|
|
||
| describe('normalizePrivKey', () => { | ||
| const validHex64 = '1234567812345678123456781234567812345678123456781234567812345678'; | ||
|
|
||
| it('should throw an error for empty or formatting values', () => { | ||
| expect(() => normalizePrivKey('')).toThrow('Private key is required.'); | ||
| expect(() => normalizePrivKey(' ')).toThrow('Private key is required.'); | ||
| }); | ||
|
|
||
| it('should accept a 64-character hex string without 0x prefix', () => { | ||
| const key = validHex64; | ||
| expect(normalizePrivKey(key)).toBe('0x' + key); | ||
| }); | ||
|
|
||
| it('should accept a 64-character hex string with 0x prefix', () => { | ||
| const key = '0x' + validHex64; | ||
| expect(normalizePrivKey(key)).toBe('0x' + validHex64); | ||
| }); | ||
|
|
||
| it('should accept a 64-character hex string with 0X prefix (case-insensitive)', () => { | ||
| const key = '0X' + validHex64; | ||
| expect(normalizePrivKey(key)).toBe('0x' + validHex64); | ||
| }); | ||
|
|
||
| it('should correctly trim spaces inside quotes', () => { | ||
| const key = `' 0x${validHex64} '`; | ||
| expect(normalizePrivKey(key)).toBe('0x' + validHex64); | ||
|
|
||
| const key2 = `" 0X${validHex64} "`; | ||
| expect(normalizePrivKey(key2)).toBe('0x' + validHex64); | ||
| }); | ||
|
|
||
| it('should throw an error if the key contains non-hexadecimal characters', () => { | ||
| const invalidHex = validHex64.slice(0, -1) + 'G'; // Replace last char with 'G' (invalid hex) | ||
| expect(() => normalizePrivKey(invalidHex)).toThrow('Invalid private key: contains non-hexadecimal characters.'); | ||
| }); | ||
|
|
||
| it('should throw an error if the key length is incorrect', () => { | ||
| const shortKey = validHex64.slice(0, 62); | ||
| expect(() => normalizePrivKey(shortKey)).toThrow('Invalid private key length'); | ||
|
|
||
| const longKey = validHex64 + '12'; | ||
| expect(() => normalizePrivKey(longKey)).toThrow('Invalid private key length'); | ||
| }); | ||
| }); |
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.
This normalization/validation logic is now on the
buildSignerhot-path for multiple CLI commands, but there are no tests covering common accepted inputs (with/without 0x, with quotes/whitespace) and rejection cases (bad hex, wrong length). Adding a small unit test suite fornormalizePrivKeywould help prevent regressions and ensure the error messages stay user-friendly.