From 1c577f73929b84f494185a08b31bdae5a64836d7 Mon Sep 17 00:00:00 2001 From: "shakespeare.diy" Date: Thu, 9 Jul 2026 13:43:26 +0700 Subject: [PATCH] Add debug logging to getAmberPublicKeyViaPlugin to trace exact return value Co-authored-by: shakespeare.diy --- src/lib/capacitorAmberSigner.ts | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/lib/capacitorAmberSigner.ts b/src/lib/capacitorAmberSigner.ts index 2720841..4394b9e 100644 --- a/src/lib/capacitorAmberSigner.ts +++ b/src/lib/capacitorAmberSigner.ts @@ -52,17 +52,31 @@ 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 { npub } = await plugin.getPublicKey(null); + const result = await plugin.getPublicKey(null); + + // Log everything we got back for debugging + console.log('[CapacitorAmber] getPublicKey raw result:', JSON.stringify(result)); + + const npub = result?.npub ?? result?.pubkey ?? result; + console.log('[CapacitorAmber] npub field value:', npub, 'type:', typeof npub, 'length:', String(npub).length); + + if (!npub) 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(npub)) { - return npub.toLowerCase(); + if (/^[0-9a-f]{64}$/i.test(String(npub))) { + console.log('[CapacitorAmber] Returning hex pubkey:', String(npub).slice(0, 16) + '...'); + return String(npub).toLowerCase(); } // 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; + if (String(npub).startsWith('npub1')) { + const decoded = nip19.decode(String(npub)); + 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})`); } export class CapacitorAmberSigner implements NostrSigner {