Fix Amber login: store as 'amber' type (NIP-55) not 'bunker' (NIP-46)

The nostrconnect flow was incorrectly creating a bunker login and trying
to do NIP-46 key exchange. Amber signs as the user's own key directly,
so the pubkey from the connect response IS the user pubkey. Now stores
a proper amber login which uses AmberSigner via useCurrentUser.

Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
This commit is contained in:
shakespeare.diy 2026-07-03 12:38:56 +07:00
parent 03ea569a2a
commit e3dd66d078

View file

@ -12,7 +12,7 @@ import { useToast } from '@/hooks/useToast';
import { cn } from '@/lib/utils';
import { NLogin } from '@nostrify/react/login';
import { useNostrLogin } from '@nostrify/react/login';
import { NConnectSigner, NSecSigner, NRelay1 } from '@nostrify/nostrify';
import { SimplePool, generateSecretKey, getPublicKey, nip19, nip04 } from 'nostr-tools';
interface LoginDialogProps {
@ -119,33 +119,14 @@ export const LoginDialog: React.FC<LoginDialogProps> = ({ isOpen, onClose, onLog
if (elapsedIntervalRef.current) { clearInterval(elapsedIntervalRef.current); elapsedIntervalRef.current = null; }
}, []);
// Complete login once we have signerPubkey from any source
const completeLogin = useCallback(async (signerPubkey: string, clientSk: Uint8Array, clientNsec: string, relays: string[]) => {
setAmberStatus('Getting your pubkey...');
let userPubkey = signerPubkey;
// Complete login — store as 'amber' type (NIP-55), not bunker.
// Amber signs as the user's own key, so signerPubkey IS the user's pubkey.
const completeLogin = useCallback(async (signerPubkey: string) => {
setAmberStatus('Logging in...');
console.log('[Amber] Completing login for pubkey:', signerPubkey);
// Try to get the real user pubkey via NConnectSigner
try {
const clientSigner = new NSecSigner(clientSk);
const relay = new NRelay1(relays[0]);
const signer = new NConnectSigner({
pubkey: signerPubkey,
signer: clientSigner,
relay,
timeout: 15000,
});
userPubkey = await signer.getPublicKey();
try { relay.close(); } catch { /* ignore */ }
} catch {
// Amber signs as the user's key directly, so signerPubkey IS the userPubkey
userPubkey = signerPubkey;
}
const nlogin = new NLogin('bunker', userPubkey, {
bunkerPubkey: signerPubkey,
clientNsec,
relays,
});
// Store as amber login type — picked up by useCurrentUser → AmberSigner
const nlogin = new NLogin('amber' as Parameters<typeof NLogin>[0], signerPubkey);
addLogin(nlogin);
setLogin(nlogin.id);
clearPendingNC();
@ -193,7 +174,7 @@ export const LoginDialog: React.FC<LoginDialogProps> = ({ isOpen, onClose, onLog
freshPool.close(NOSTRCONNECT_RELAYS);
pendingRef.current = null;
abortRef.current?.abort();
await completeLogin(event.pubkey, clientSk, pending.clientNsec, pending.relays);
await completeLogin(event.pubkey);
resolve();
} catch { /* wrong encryption key or malformed event */ }
},
@ -343,7 +324,7 @@ export const LoginDialog: React.FC<LoginDialogProps> = ({ isOpen, onClose, onLog
console.log('[Amber] Valid response — completing login');
controller.abort();
pendingRef.current = null;
await completeLogin(event.pubkey, clientSk, clientNsec, NOSTRCONNECT_RELAYS);
await completeLogin(event.pubkey);
} catch (e) {
console.warn('[Amber] Failed to decrypt event:', e);
}