diff --git a/src/lib/capacitorAmberSigner.ts b/src/lib/capacitorAmberSigner.ts index 072dacb..2720841 100644 --- a/src/lib/capacitorAmberSigner.ts +++ b/src/lib/capacitorAmberSigner.ts @@ -21,7 +21,10 @@ async function getPlugin() { try { const { NostrSignerPlugin } = await import('nostr-signer-capacitor-plugin'); if (!NostrSignerPlugin) return null; - // setPackageName(packageName: string) — plain string, first arg + // JS layer does: native.setPackageName({ packageName }) + // so the JS wrapper expects a plain string which it wraps into { packageName } + // BUT the JS source shows: async setPackageName(packageName) { native.setPackageName({ packageName }) } + // So we pass plain string to the JS wrapper — it handles the wrapping await NostrSignerPlugin.setPackageName(AMBER_PACKAGE); return NostrSignerPlugin; } catch (e: unknown) { @@ -35,22 +38,28 @@ async function getPlugin() { /** * Login: get public key from Amber. - * getPublicKey(packageName, permissions?) - * Returns { npub: string } + * + * The plugin tries Content Resolver first (silent, returns cached key), + * then falls back to opening Amber UI if no cached result. + * + * The Java bridge calls npubToHex() before returning, so the "npub" field + * is actually a hex pubkey string despite the misleading name. */ export async function getAmberPublicKeyViaPlugin(): Promise { const plugin = await getPlugin(); if (!plugin) throw new Error('Capacitor Amber plugin not available — are you on a native build?'); - const { npub } = await plugin.getPublicKey(AMBER_PACKAGE); + // 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 { npub } = await plugin.getPublicKey(null); - // Despite the field being called "npub", the Java bridge calls npubToHex() - // before returning — so this is actually a hex pubkey already. + // The "npub" field is actually hex (Java calls npubToHex before returning) if (/^[0-9a-f]{64}$/i.test(npub)) { return npub.toLowerCase(); } - // Fallback: if it somehow comes back as a real npub, decode it + // Fallback: handle real npub just in case const decoded = nip19.decode(npub); if (decoded.type !== 'npub') throw new Error('Unexpected key format from Amber'); return decoded.data as string;