From 352eecc416a2e052494b4c1544f6598aa30f328c Mon Sep 17 00:00:00 2001 From: codytseng Date: Thu, 14 Aug 2025 22:25:35 +0800 Subject: [PATCH] feat: initialize default configuration for new users --- .../AccountManager/GenerateNewAccount.tsx | 2 +- src/lib/draft-event.ts | 2 +- src/providers/NostrProvider/index.tsx | 31 ++++++++++++++++--- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/components/AccountManager/GenerateNewAccount.tsx b/src/components/AccountManager/GenerateNewAccount.tsx index 2519cb09..313a781a 100644 --- a/src/components/AccountManager/GenerateNewAccount.tsx +++ b/src/components/AccountManager/GenerateNewAccount.tsx @@ -22,7 +22,7 @@ export default function GenerateNewAccount({ const [password, setPassword] = useState('') const handleLogin = () => { - nsecLogin(nsec, password).then(() => onLoginSuccess()) + nsecLogin(nsec, password, true).then(() => onLoginSuccess()) } return ( diff --git a/src/lib/draft-event.ts b/src/lib/draft-event.ts index 23c9d1b7..fc5be397 100644 --- a/src/lib/draft-event.ts +++ b/src/lib/draft-event.ts @@ -634,7 +634,7 @@ function buildReplaceableQTag(coordinate: string) { } function buildRTag(url: string, scope: TMailboxRelayScope) { - return scope === 'both' ? ['r', url, scope] : ['r', url] + return scope !== 'both' ? ['r', url, scope] : ['r', url] } function buildTTag(hashtag: string) { diff --git a/src/providers/NostrProvider/index.tsx b/src/providers/NostrProvider/index.tsx index 177bb417..6c079ecd 100644 --- a/src/providers/NostrProvider/index.tsx +++ b/src/providers/NostrProvider/index.tsx @@ -1,6 +1,10 @@ import LoginDialog from '@/components/LoginDialog' import { ApplicationDataKey, BIG_RELAY_URLS, ExtendedKind } from '@/constants' -import { createSeenNotificationsAtDraftEvent } from '@/lib/draft-event' +import { + createFollowListDraftEvent, + createRelayListDraftEvent, + createSeenNotificationsAtDraftEvent +} from '@/lib/draft-event' import { getLatestEvent, getReplaceableEventIdentifier } from '@/lib/event' import { getProfileFromEvent, getRelayListFromEvent } from '@/lib/event-metadata' import { formatPubkey, isValidPubkey, pubkeyToNpub } from '@/lib/pubkey' @@ -44,7 +48,7 @@ type TNostrContext = { nsec: string | null ncryptsec: string | null switchAccount: (account: TAccountPointer | null) => Promise - nsecLogin: (nsec: string, password?: string) => Promise + nsecLogin: (nsec: string, password?: string, needSetup?: boolean) => Promise ncryptsecLogin: (ncryptsec: string) => Promise nip07Login: () => Promise bunkerLogin: (bunker: string) => Promise @@ -375,7 +379,7 @@ export function NostrProvider({ children }: { children: React.ReactNode }) { await loginWithAccountPointer(act) } - const nsecLogin = async (nsecOrHex: string, password?: string) => { + const nsecLogin = async (nsecOrHex: string, password?: string, needSetup?: boolean) => { const nsecSigner = new NsecSigner() let privkey: Uint8Array if (nsecOrHex.startsWith('nsec')) { @@ -392,9 +396,14 @@ export function NostrProvider({ children }: { children: React.ReactNode }) { const pubkey = nsecSigner.login(privkey) if (password) { const ncryptsec = nip49.encrypt(privkey, password) - return login(nsecSigner, { pubkey, signerType: 'ncryptsec', ncryptsec }) + login(nsecSigner, { pubkey, signerType: 'ncryptsec', ncryptsec }) + } else { + login(nsecSigner, { pubkey, signerType: 'nsec', nsec: nip19.nsecEncode(privkey) }) } - return login(nsecSigner, { pubkey, signerType: 'nsec', nsec: nip19.nsecEncode(privkey) }) + if (needSetup) { + setupNewUser(nsecSigner) + } + return pubkey } const ncryptsecLogin = async (ncryptsec: string) => { @@ -526,6 +535,18 @@ export function NostrProvider({ children }: { children: React.ReactNode }) { return null } + const setupNewUser = async (signer: ISigner) => { + await Promise.allSettled([ + client.publishEvent(BIG_RELAY_URLS, await signer.signEvent(createFollowListDraftEvent([]))), + client.publishEvent( + BIG_RELAY_URLS, + await signer.signEvent( + createRelayListDraftEvent(BIG_RELAY_URLS.map((url) => ({ url, scope: 'both' }))) + ) + ) + ]) + } + const signEvent = async (draftEvent: TDraftEvent) => { const event = await signer?.signEvent(draftEvent) if (!event) {