Fix getPublicKey: JSON parse JSObject result + restore AMBER_PACKAGE arg

- JSObject from Capacitor needs JSON.parse(JSON.stringify()) to be
  accessible as plain JS object with property access
- Restore AMBER_PACKAGE as argument (null was causing broken URI)
- Remove debug toast from LoginDialog

Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
This commit is contained in:
shakespeare.diy 2026-07-09 14:28:58 +07:00
parent 0b1bf05e74
commit de5de7abec
2 changed files with 18 additions and 21 deletions

View file

@ -60,12 +60,6 @@ export const LoginDialog: React.FC<LoginDialogProps> = ({ isOpen, onClose, onLog
try { try {
// This opens Amber natively, user approves, pubkey comes back as a Promise // This opens Amber natively, user approves, pubkey comes back as a Promise
const pubkey = await getAmberPublicKeyViaPlugin(); 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); completeAmberLogin(pubkey);
} catch (e: unknown) { } catch (e: unknown) {
const msg = (e as Error)?.message ?? 'Amber login failed'; const msg = (e as Error)?.message ?? 'Amber login failed';

View file

@ -52,31 +52,34 @@ export async function getAmberPublicKeyViaPlugin(): Promise<string> {
// Pass null as packageName to skip the Content Resolver silent path // Pass null as packageName to skip the Content Resolver silent path
// and always open the Amber UI so the user explicitly selects their key. // and always open the Amber UI so the user explicitly selects their key.
// This prevents returning a stale/wrong cached 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 // JSObject from Capacitor — stringify and reparse to get a plain JS object
console.log('[CapacitorAmber] getPublicKey raw result:', JSON.stringify(result)); // so property access works reliably
const plain = JSON.parse(JSON.stringify(result)) as Record<string, string>;
const npub = result?.npub ?? result?.pubkey ?? result; // Java puts the value in "npub" field but calls npubToHex() first,
console.log('[CapacitorAmber] npub field value:', npub, 'type:', typeof npub, 'length:', String(npub).length); // 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) // Already hex
if (/^[0-9a-f]{64}$/i.test(String(npub))) { if (/^[0-9a-f]{64}$/i.test(value)) {
console.log('[CapacitorAmber] Returning hex pubkey:', String(npub).slice(0, 16) + '...'); return value.toLowerCase();
return String(npub).toLowerCase();
} }
// Fallback: handle real npub just in case // Real npub — decode to hex
if (String(npub).startsWith('npub1')) { if (value.startsWith('npub1')) {
const decoded = nip19.decode(String(npub)); const decoded = nip19.decode(value);
if (decoded.type !== 'npub') throw new Error('Unexpected key format from Amber'); if (decoded.type !== 'npub') throw new Error('Unexpected key format from Amber');
console.log('[CapacitorAmber] Decoded npub to hex');
return decoded.data as string; 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 { export class CapacitorAmberSigner implements NostrSigner {