feat: initialize default configuration for new users

This commit is contained in:
codytseng
2025-08-14 22:25:35 +08:00
parent 46bf0ecc0a
commit 352eecc416
3 changed files with 28 additions and 7 deletions

View File

@@ -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<void>
nsecLogin: (nsec: string, password?: string) => Promise<string>
nsecLogin: (nsec: string, password?: string, needSetup?: boolean) => Promise<string>
ncryptsecLogin: (ncryptsec: string) => Promise<string>
nip07Login: () => Promise<string>
bunkerLogin: (bunker: string) => Promise<string>
@@ -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) {