Fix getPublicKey: pass null packageName to force Amber UI path

The Content Resolver silent path returns a cached/wrong key.
Passing null skips it and always opens Amber UI so the user
explicitly selects their key and the correct pubkey is returned.

Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
This commit is contained in:
shakespeare.diy 2026-07-09 13:28:26 +07:00
parent da1d208579
commit 0d9cfa61fb

View file

@ -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<string> {
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;