Fix nsec import validation for Uint8Array data
**Motivations:** - Fix import error when importing nsec private keys - Support both string and Uint8Array formats from nip19.decode **Root causes:** - nip19.decode returns decoded.data as Uint8Array, not string - Validation in KeyManagementManager only checked for string type - importPrivateKey in keyManagement.ts only handled string type **Correctifs:** - Updated KeyManagementManager validation to accept Uint8Array - Updated importPrivateKey to convert Uint8Array to hex using bytesToHex - Improved error messages to show actual validation errors **Evolutions:** - None **Pages affectées:** - components/KeyManagementManager.tsx - lib/keyManagement.ts
This commit is contained in:
parent
57ac98ebe4
commit
d7a04dd8f8
@ -94,13 +94,18 @@ export function KeyManagementManager() {
|
||||
try {
|
||||
// Try to decode as nsec
|
||||
const decoded = nip19.decode(extractedKey)
|
||||
if (decoded.type !== 'nsec' || typeof decoded.data !== 'string') {
|
||||
if (decoded.type !== 'nsec') {
|
||||
throw new Error('Invalid nsec format')
|
||||
}
|
||||
} catch {
|
||||
// Assume it's hex, validate length (64 hex chars = 32 bytes)
|
||||
// decoded.data can be string (hex) or Uint8Array, both are valid
|
||||
if (typeof decoded.data !== 'string' && !(decoded.data instanceof Uint8Array)) {
|
||||
throw new Error('Invalid nsec format')
|
||||
}
|
||||
} catch (e) {
|
||||
// If decoding failed, assume it's hex, validate length (64 hex chars = 32 bytes)
|
||||
if (!/^[0-9a-f]{64}$/i.test(extractedKey)) {
|
||||
setError('Invalid key format. Please provide a nsec (nsec1...) or hex (64 characters) private key.')
|
||||
const errorMsg = e instanceof Error ? e.message : 'Invalid format'
|
||||
setError(`Invalid key format: ${errorMsg}. Please provide a nsec (nsec1...) or hex (64 characters) private key.`)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,8 +39,15 @@ export class KeyManagementService {
|
||||
// Try to decode as nsec
|
||||
try {
|
||||
const decoded = nip19.decode(privateKey)
|
||||
if (decoded.type === 'nsec' && typeof decoded.data === 'string') {
|
||||
privateKeyHex = decoded.data
|
||||
if (decoded.type === 'nsec') {
|
||||
// decoded.data can be string (hex) or Uint8Array depending on nostr-tools version
|
||||
if (typeof decoded.data === 'string') {
|
||||
privateKeyHex = decoded.data
|
||||
} else if (decoded.data instanceof Uint8Array) {
|
||||
privateKeyHex = bytesToHex(decoded.data)
|
||||
} else {
|
||||
throw new Error('Invalid nsec format: data is neither string nor Uint8Array')
|
||||
}
|
||||
} else {
|
||||
throw new Error('Invalid nsec format')
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user