diff --git a/src/components/auth/LoginDialog.tsx b/src/components/auth/LoginDialog.tsx index 0561e30..8a3f21d 100644 --- a/src/components/auth/LoginDialog.tsx +++ b/src/components/auth/LoginDialog.tsx @@ -60,12 +60,6 @@ export const LoginDialog: React.FC = ({ isOpen, onClose, onLog try { // This opens Amber natively, user approves, pubkey comes back as a Promise const pubkey = await getAmberPublicKeyViaPlugin(); - // DEBUG: show pubkey on screen so we can verify it's correct - toast({ - title: '🔑 DEBUG: Amber returned', - description: `hex: ${pubkey.slice(0, 16)}... (${pubkey.length} chars)`, - duration: 10000, - }); completeAmberLogin(pubkey); } catch (e: unknown) { const msg = (e as Error)?.message ?? 'Amber login failed'; diff --git a/src/lib/capacitorAmberSigner.ts b/src/lib/capacitorAmberSigner.ts index 4394b9e..134b319 100644 --- a/src/lib/capacitorAmberSigner.ts +++ b/src/lib/capacitorAmberSigner.ts @@ -52,31 +52,34 @@ export async function getAmberPublicKeyViaPlugin(): Promise { // Pass null as packageName to skip the Content Resolver silent path // and always open the Amber UI so the user explicitly selects their key. // This prevents returning a stale/wrong cached key. - const result = await plugin.getPublicKey(null); + // Pass AMBER_PACKAGE (not null) so the Content Resolver URI is valid. + // If Content Resolver has a default account it returns immediately (silent). + // If not, it falls through to open Amber UI via intent. + const result = await plugin.getPublicKey(AMBER_PACKAGE); - // Log everything we got back for debugging - console.log('[CapacitorAmber] getPublicKey raw result:', JSON.stringify(result)); + // JSObject from Capacitor — stringify and reparse to get a plain JS object + // so property access works reliably + const plain = JSON.parse(JSON.stringify(result)) as Record; - const npub = result?.npub ?? result?.pubkey ?? result; - console.log('[CapacitorAmber] npub field value:', npub, 'type:', typeof npub, 'length:', String(npub).length); + // Java puts the value in "npub" field but calls npubToHex() first, + // so it's actually a 64-char hex string despite the misleading name + const value = plain.npub ?? plain.pubkey ?? plain.result ?? ''; - if (!npub) throw new Error('Amber returned empty key'); + if (!value) throw new Error('Amber returned empty key'); - // The "npub" field is actually hex (Java calls npubToHex before returning) - if (/^[0-9a-f]{64}$/i.test(String(npub))) { - console.log('[CapacitorAmber] Returning hex pubkey:', String(npub).slice(0, 16) + '...'); - return String(npub).toLowerCase(); + // Already hex + if (/^[0-9a-f]{64}$/i.test(value)) { + return value.toLowerCase(); } - // Fallback: handle real npub just in case - if (String(npub).startsWith('npub1')) { - const decoded = nip19.decode(String(npub)); + // Real npub — decode to hex + if (value.startsWith('npub1')) { + const decoded = nip19.decode(value); if (decoded.type !== 'npub') throw new Error('Unexpected key format from Amber'); - console.log('[CapacitorAmber] Decoded npub to hex'); return decoded.data as string; } - throw new Error(`Unexpected key format from Amber: "${String(npub).slice(0, 20)}" (length: ${String(npub).length})`); + throw new Error(`Unexpected Amber response: "${value.slice(0, 30)}"`); } export class CapacitorAmberSigner implements NostrSigner {