refactor: remove electron-related code
This commit is contained in:
53
src/providers/NostrProvider/bunker.signer.ts
Normal file
53
src/providers/NostrProvider/bunker.signer.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { ISigner, TDraftEvent } from '@/types'
|
||||
import { bytesToHex, hexToBytes } from '@noble/hashes/utils'
|
||||
import { generateSecretKey } from 'nostr-tools'
|
||||
import { BunkerSigner as NBunkerSigner, parseBunkerInput } from 'nostr-tools/nip46'
|
||||
|
||||
export class BunkerSigner implements ISigner {
|
||||
signer: NBunkerSigner | null = null
|
||||
private clientSecretKey: Uint8Array
|
||||
private pubkey: string | null = null
|
||||
|
||||
constructor(clientSecretKey?: string) {
|
||||
this.clientSecretKey = clientSecretKey ? hexToBytes(clientSecretKey) : generateSecretKey()
|
||||
}
|
||||
|
||||
async login(bunker: string): Promise<string> {
|
||||
const bunkerPointer = await parseBunkerInput(bunker)
|
||||
if (!bunkerPointer) {
|
||||
throw new Error('Invalid bunker')
|
||||
}
|
||||
|
||||
this.signer = new NBunkerSigner(this.clientSecretKey, bunkerPointer, {
|
||||
onauth: (url) => {
|
||||
window.open(url, '_blank')
|
||||
}
|
||||
})
|
||||
await this.signer.connect()
|
||||
return await this.signer.getPublicKey()
|
||||
}
|
||||
|
||||
async getPublicKey() {
|
||||
if (!this.signer) {
|
||||
throw new Error('Not logged in')
|
||||
}
|
||||
if (!this.pubkey) {
|
||||
this.pubkey = await this.signer.getPublicKey()
|
||||
}
|
||||
return this.pubkey
|
||||
}
|
||||
|
||||
async signEvent(draftEvent: TDraftEvent) {
|
||||
if (!this.signer) {
|
||||
throw new Error('Not logged in')
|
||||
}
|
||||
return this.signer.signEvent({
|
||||
...draftEvent,
|
||||
pubkey: await this.signer.getPublicKey()
|
||||
})
|
||||
}
|
||||
|
||||
getClientSecretKey() {
|
||||
return bytesToHex(this.clientSecretKey)
|
||||
}
|
||||
}
|
||||
216
src/providers/NostrProvider/index.tsx
Normal file
216
src/providers/NostrProvider/index.tsx
Normal file
@@ -0,0 +1,216 @@
|
||||
import LoginDialog from '@/components/LoginDialog'
|
||||
import { useToast } from '@/hooks'
|
||||
import { useFetchRelayList } from '@/hooks/useFetchRelayList'
|
||||
import client from '@/services/client.service'
|
||||
import storage from '@/services/storage.service'
|
||||
import { ISigner, TDraftEvent } from '@/types'
|
||||
import dayjs from 'dayjs'
|
||||
import { Event, kinds } from 'nostr-tools'
|
||||
import { createContext, useContext, useEffect, useState } from 'react'
|
||||
import { useRelaySettings } from '../RelaySettingsProvider'
|
||||
import { NsecSigner } from './nsec.signer'
|
||||
import { BunkerSigner } from './bunker.signer'
|
||||
import { Nip07Signer } from './nip-07.signer'
|
||||
|
||||
type TNostrContext = {
|
||||
pubkey: string | null
|
||||
setPubkey: (pubkey: string) => void
|
||||
nsecLogin: (nsec: string) => Promise<string>
|
||||
nip07Login: () => Promise<string>
|
||||
bunkerLogin: (bunker: string) => Promise<string>
|
||||
logout: () => void
|
||||
/**
|
||||
* Default publish the event to current relays, user's write relays and additional relays
|
||||
*/
|
||||
publish: (draftEvent: TDraftEvent, additionalRelayUrls?: string[]) => Promise<Event>
|
||||
signHttpAuth: (url: string, method: string) => Promise<string>
|
||||
signEvent: (draftEvent: TDraftEvent) => Promise<Event>
|
||||
checkLogin: <T>(cb?: () => T) => Promise<T | void>
|
||||
}
|
||||
|
||||
const NostrContext = createContext<TNostrContext | undefined>(undefined)
|
||||
|
||||
export const useNostr = () => {
|
||||
const context = useContext(NostrContext)
|
||||
if (!context) {
|
||||
throw new Error('useNostr must be used within a NostrProvider')
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
export function NostrProvider({ children }: { children: React.ReactNode }) {
|
||||
const { toast } = useToast()
|
||||
const [pubkey, setPubkey] = useState<string | null>(null)
|
||||
const [signer, setSigner] = useState<ISigner | null>(null)
|
||||
const [openLoginDialog, setOpenLoginDialog] = useState(false)
|
||||
const { relayUrls: currentRelayUrls } = useRelaySettings()
|
||||
const relayList = useFetchRelayList(pubkey)
|
||||
|
||||
useEffect(() => {
|
||||
const init = async () => {
|
||||
const [account] = storage.getAccounts()
|
||||
if (!account) {
|
||||
if (!window.nostr) {
|
||||
return
|
||||
}
|
||||
|
||||
// For browser env, attempt to login with nip-07
|
||||
const nip07Signer = new Nip07Signer()
|
||||
const pubkey = await nip07Signer.getPublicKey()
|
||||
if (!pubkey) {
|
||||
return
|
||||
}
|
||||
storage.setAccounts([{ pubkey, signerType: 'nip-07' }])
|
||||
return login(nip07Signer, pubkey)
|
||||
}
|
||||
|
||||
if (account.pubkey) {
|
||||
setPubkey(account.pubkey)
|
||||
}
|
||||
|
||||
// browser-nsec is deprecated
|
||||
if (account.signerType === 'browser-nsec') {
|
||||
if (account.nsec) {
|
||||
const browserNsecSigner = new NsecSigner()
|
||||
const pubkey = browserNsecSigner.login(account.nsec)
|
||||
storage.setAccounts([{ pubkey, signerType: 'nsec', nsec: account.nsec }])
|
||||
return login(browserNsecSigner, pubkey)
|
||||
}
|
||||
} else if (account.signerType === 'nsec') {
|
||||
if (account.nsec) {
|
||||
const browserNsecSigner = new NsecSigner()
|
||||
const pubkey = browserNsecSigner.login(account.nsec)
|
||||
return login(browserNsecSigner, pubkey)
|
||||
}
|
||||
} else if (account.signerType === 'nip-07') {
|
||||
const nip07Signer = new Nip07Signer()
|
||||
return login(nip07Signer, account.pubkey)
|
||||
} else if (account.signerType === 'bunker') {
|
||||
if (account.bunker && account.bunkerClientSecretKey) {
|
||||
const bunkerSigner = new BunkerSigner(account.bunkerClientSecretKey)
|
||||
const pubkey = await bunkerSigner.login(account.bunker)
|
||||
return login(bunkerSigner, pubkey)
|
||||
}
|
||||
}
|
||||
|
||||
return logout()
|
||||
}
|
||||
init().catch(() => {
|
||||
logout()
|
||||
})
|
||||
}, [])
|
||||
|
||||
const login = (signer: ISigner, pubkey: string) => {
|
||||
setPubkey(pubkey)
|
||||
setSigner(signer)
|
||||
return pubkey
|
||||
}
|
||||
|
||||
const logout = () => {
|
||||
setPubkey(null)
|
||||
setSigner(null)
|
||||
storage.setAccounts([])
|
||||
}
|
||||
|
||||
const nsecLogin = async (nsec: string) => {
|
||||
const browserNsecSigner = new NsecSigner()
|
||||
const pubkey = browserNsecSigner.login(nsec)
|
||||
storage.setAccounts([{ pubkey, signerType: 'nsec', nsec }])
|
||||
return login(browserNsecSigner, pubkey)
|
||||
}
|
||||
|
||||
const nip07Login = async () => {
|
||||
try {
|
||||
const nip07Signer = new Nip07Signer()
|
||||
const pubkey = await nip07Signer.getPublicKey()
|
||||
if (!pubkey) {
|
||||
throw new Error('You did not allow to access your pubkey')
|
||||
}
|
||||
storage.setAccounts([{ pubkey, signerType: 'nip-07' }])
|
||||
return login(nip07Signer, pubkey)
|
||||
} catch (err) {
|
||||
toast({
|
||||
title: 'Login failed',
|
||||
description: (err as Error).message,
|
||||
variant: 'destructive'
|
||||
})
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
const bunkerLogin = async (bunker: string) => {
|
||||
const bunkerSigner = new BunkerSigner()
|
||||
const pubkey = await bunkerSigner.login(bunker)
|
||||
if (!pubkey) {
|
||||
throw new Error('Invalid bunker')
|
||||
}
|
||||
const bunkerUrl = new URL(bunker)
|
||||
bunkerUrl.searchParams.delete('secret')
|
||||
storage.setAccounts([
|
||||
{
|
||||
pubkey,
|
||||
signerType: 'bunker',
|
||||
bunker: bunkerUrl.toString(),
|
||||
bunkerClientSecretKey: bunkerSigner.getClientSecretKey()
|
||||
}
|
||||
])
|
||||
return login(bunkerSigner, pubkey)
|
||||
}
|
||||
|
||||
const signEvent = async (draftEvent: TDraftEvent) => {
|
||||
const event = await signer?.signEvent(draftEvent)
|
||||
if (!event) {
|
||||
throw new Error('sign event failed')
|
||||
}
|
||||
return event
|
||||
}
|
||||
|
||||
const publish = async (draftEvent: TDraftEvent, additionalRelayUrls: string[] = []) => {
|
||||
const event = await signEvent(draftEvent)
|
||||
await client.publishEvent(
|
||||
relayList.write.concat(additionalRelayUrls).concat(currentRelayUrls),
|
||||
event
|
||||
)
|
||||
return event
|
||||
}
|
||||
|
||||
const signHttpAuth = async (url: string, method: string) => {
|
||||
const event = await signEvent({
|
||||
content: '',
|
||||
kind: kinds.HTTPAuth,
|
||||
created_at: dayjs().unix(),
|
||||
tags: [
|
||||
['u', url],
|
||||
['method', method]
|
||||
]
|
||||
})
|
||||
return 'Nostr ' + btoa(JSON.stringify(event))
|
||||
}
|
||||
|
||||
const checkLogin = async <T,>(cb?: () => T): Promise<T | void> => {
|
||||
if (signer) {
|
||||
return cb && cb()
|
||||
}
|
||||
return setOpenLoginDialog(true)
|
||||
}
|
||||
|
||||
return (
|
||||
<NostrContext.Provider
|
||||
value={{
|
||||
pubkey,
|
||||
setPubkey,
|
||||
nsecLogin,
|
||||
nip07Login,
|
||||
bunkerLogin,
|
||||
logout,
|
||||
publish,
|
||||
signHttpAuth,
|
||||
checkLogin,
|
||||
signEvent
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
<LoginDialog open={openLoginDialog} setOpen={setOpenLoginDialog} />
|
||||
</NostrContext.Provider>
|
||||
)
|
||||
}
|
||||
26
src/providers/NostrProvider/nip-07.signer.ts
Normal file
26
src/providers/NostrProvider/nip-07.signer.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { ISigner, TDraftEvent, TNip07 } from '@/types'
|
||||
|
||||
export class Nip07Signer implements ISigner {
|
||||
private signer: TNip07
|
||||
private pubkey: string | null = null
|
||||
|
||||
constructor() {
|
||||
if (!window.nostr) {
|
||||
throw new Error(
|
||||
'You need to install a nostr signer extension to login. Such as alby, nostr-keyx or nos2x.'
|
||||
)
|
||||
}
|
||||
this.signer = window.nostr
|
||||
}
|
||||
|
||||
async getPublicKey() {
|
||||
if (!this.pubkey) {
|
||||
this.pubkey = await this.signer.getPublicKey()
|
||||
}
|
||||
return this.pubkey
|
||||
}
|
||||
|
||||
async signEvent(draftEvent: TDraftEvent) {
|
||||
return await this.signer.signEvent(draftEvent)
|
||||
}
|
||||
}
|
||||
35
src/providers/NostrProvider/nsec.signer.ts
Normal file
35
src/providers/NostrProvider/nsec.signer.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { ISigner, TDraftEvent } from '@/types'
|
||||
import { finalizeEvent, getPublicKey as nGetPublicKey, nip19 } from 'nostr-tools'
|
||||
|
||||
export class NsecSigner implements ISigner {
|
||||
private privkey: Uint8Array | null = null
|
||||
private pubkey: string | null = null
|
||||
|
||||
login(nsec: string) {
|
||||
const { type, data } = nip19.decode(nsec)
|
||||
if (type !== 'nsec') {
|
||||
throw new Error('invalid nsec')
|
||||
}
|
||||
|
||||
this.privkey = data
|
||||
this.pubkey = nGetPublicKey(data)
|
||||
return this.pubkey
|
||||
}
|
||||
|
||||
async getPublicKey() {
|
||||
return this.pubkey
|
||||
}
|
||||
|
||||
async signEvent(draftEvent: TDraftEvent) {
|
||||
if (!this.privkey) {
|
||||
return null
|
||||
}
|
||||
|
||||
try {
|
||||
return finalizeEvent(draftEvent, this.privkey)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user