Add debug logging to getAmberPublicKeyViaPlugin to trace exact return value

Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
This commit is contained in:
shakespeare.diy 2026-07-09 13:43:26 +07:00
parent a6c220c911
commit 1c577f7392

View file

@ -52,17 +52,31 @@ export async function getAmberPublicKeyViaPlugin(): Promise<string> {
// 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 {