feat: enhance login options with read-only and nsec support (#126)

* feat: enhance login options with read-only and nsec support

- Add read-only login mode supporting:
  - npub (bech32 public key)
  - nprofile (bech32 profile with relay hints)
  - hex public key
  - NIP-05 addresses (user@domain.com)

- Add private key (nsec) login with security warning
  - Supports nsec1... format
  - Supports 64-char hex private key
  - Shows prominent security warning about localStorage storage

- Reorganize user menu to show login before theme option

- Use ReadonlyAccount from applesauce-accounts for read-only mode
- Use PrivateKeyAccount from applesauce-accounts for nsec login
- Update LoginDialog with 4 tabs: Extension, Read-Only, Private Key, Remote
- All account types properly registered via registerCommonAccountTypes()

Technical notes:
- ReadonlySigner throws errors on sign/encrypt operations
- Existing components naturally handle accounts without signing capability
- Hub/ActionRunner already syncs with account signers automatically

* feat: add generate identity button to login dialog

- Add "Generate Identity" button above login tabs
- Uses Wand2 icon from lucide-react
- Creates new key pair using PrivateKeyAccount.generateNew()
- Automatically stores nsec in localStorage and sets as active account
- Provides quick onboarding for new users without external wallet setup

* feat: add useAccount hook for signing capability detection

Created a centralized hook to check account signing capabilities and
refactored components to distinguish between signing and read-only operations.

New hook (src/hooks/useAccount.ts):
- Returns account, pubkey, canSign, signer, isLoggedIn
- Detects ReadonlyAccount vs signing accounts
- Provides clear API for checking signing capability

Refactored components:
- ChatViewer: Use canSign for message composer, replying, actions
  - Show "Sign in to send messages" for read-only accounts
  - Disable message input for accounts without signing
- SpellDialog: Use canSign for publishing spells
  - Show clear warning for read-only accounts
  - Updated error messages to mention read-only limitation
- useEmojiSearch: Use pubkey for loading custom emoji lists
  - Works correctly with both signing and read-only accounts

Benefits:
- Clear separation between read (pubkey) and write (canSign, signer) operations
- Read-only accounts can browse, view profiles, load data
- Signing operations properly disabled for read-only accounts
- Consistent pattern across the codebase for account checks
- Better UX with specific messages about account capabilities

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Alejandro
2026-01-17 21:14:44 +01:00
committed by GitHub
parent 5300d87d53
commit ee2b62f2d6
6 changed files with 421 additions and 68 deletions

69
src/hooks/useAccount.ts Normal file
View File

@@ -0,0 +1,69 @@
import { useMemo } from "react";
import { use$ } from "applesauce-react/hooks";
import accounts from "@/services/accounts";
/**
* Hook to access the active account with signing capability detection
*
* @returns {object} Account state
* @property {IAccount | undefined} account - The full active account object
* @property {string | undefined} pubkey - The account's public key (available for all account types)
* @property {boolean} canSign - Whether the account can sign events (false for read-only accounts)
* @property {ISigner | undefined} signer - The signer instance (undefined for read-only accounts)
* @property {boolean} isLoggedIn - Whether any account is active (including read-only)
*
* @example
* // For read-only operations (viewing profiles, loading data)
* const { pubkey, isLoggedIn } = useAccount();
* if (pubkey) {
* // Load user's relay list, emoji list, etc.
* }
*
* @example
* // For signing operations (posting, publishing, uploading)
* const { canSign, signer, pubkey } = useAccount();
* if (canSign) {
* // Can publish events
* await adapter.sendMessage({ activePubkey: pubkey, activeSigner: signer, ... });
* } else {
* // Show "log in to post" message
* }
*/
export function useAccount() {
const account = use$(accounts.active$);
return useMemo(() => {
if (!account) {
return {
account: undefined,
pubkey: undefined,
canSign: false,
signer: undefined,
isLoggedIn: false,
};
}
// Check if the account has a functional signer
// Read-only accounts have a signer that throws errors on sign operations
// We detect this by checking for the ReadonlySigner type or checking signer methods
const signer = account.signer;
let canSign = false;
if (signer) {
// ReadonlyAccount from applesauce-accounts has a ReadonlySigner
// that throws on signEvent, nip04, nip44 operations
// We can detect it by checking if it's an instance with the expected methods
// but we'll use a safer approach: check the account type name
const accountType = account.constructor.name;
canSign = accountType !== "ReadonlyAccount";
}
return {
account,
pubkey: account.pubkey,
canSign,
signer: canSign ? signer : undefined,
isLoggedIn: true,
};
}, [account]);
}

View File

@@ -1,13 +1,12 @@
import { useEffect, useMemo, useRef } from "react";
import { use$ } from "applesauce-react/hooks";
import {
EmojiSearchService,
type EmojiSearchResult,
} from "@/services/emoji-search";
import { UNICODE_EMOJIS } from "@/lib/unicode-emojis";
import eventStore from "@/services/event-store";
import accounts from "@/services/accounts";
import type { NostrEvent } from "@/types/nostr";
import { useAccount } from "./useAccount";
/**
* Hook to provide emoji search functionality with automatic indexing
@@ -15,7 +14,7 @@ import type { NostrEvent } from "@/types/nostr";
*/
export function useEmojiSearch(contextEvent?: NostrEvent) {
const serviceRef = useRef<EmojiSearchService | null>(null);
const activeAccount = use$(accounts.active$);
const { pubkey } = useAccount();
// Create service instance (singleton per component mount)
if (!serviceRef.current) {
@@ -35,12 +34,10 @@ export function useEmojiSearch(contextEvent?: NostrEvent) {
// Subscribe to user's emoji list (kind 10030) and emoji sets (kind 30030)
useEffect(() => {
if (!activeAccount?.pubkey) {
if (!pubkey) {
return;
}
const pubkey = activeAccount.pubkey;
// Subscribe to user's emoji list (kind 10030 - replaceable)
const userEmojiList$ = eventStore.replaceable(10030, pubkey);
const userEmojiSub = userEmojiList$.subscribe({
@@ -99,7 +96,7 @@ export function useEmojiSearch(contextEvent?: NostrEvent) {
// Clear custom emojis but keep unicode
service.clearCustom();
};
}, [activeAccount?.pubkey, service]);
}, [pubkey, service]);
// Memoize search function
const searchEmojis = useMemo(