diff --git a/components/KeyManagementManager.tsx b/components/KeyManagementManager.tsx index 6fc80f5..c3caee1 100644 --- a/components/KeyManagementManager.tsx +++ b/components/KeyManagementManager.tsx @@ -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 } } diff --git a/lib/keyManagement.ts b/lib/keyManagement.ts index b471dee..7272744 100644 --- a/lib/keyManagement.ts +++ b/lib/keyManagement.ts @@ -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') }