feat: added the ability to login with nsecbunker connection string
All checks were successful
Release / build_and_release (push) Successful in 43s

This commit is contained in:
Sabir Hassan 2024-03-22 12:29:23 +05:00
parent e57893b1b2
commit 4973721608
6 changed files with 99 additions and 6 deletions

View File

@ -67,7 +67,9 @@ export const AppBar = () => {
setAuthState({ setAuthState({
loggedIn: false, loggedIn: false,
usersPubkey: undefined, usersPubkey: undefined,
loginMethod: undefined loginMethod: undefined,
nsecBunkerPubkey: undefined,
nsecbunkerRelay: undefined
}) })
) )

View File

@ -12,13 +12,14 @@ import {
import { import {
updateKeyPair, updateKeyPair,
updateLoginMethod, updateLoginMethod,
updateNsecbunkerPubkey updateNsecbunkerPubkey,
updateNsecbunkerRelay
} from '../../store/actions' } from '../../store/actions'
import { LoginMethods } from '../../store/auth/types' import { LoginMethods } from '../../store/auth/types'
import { Dispatch } from '../../store/store' import { Dispatch } from '../../store/store'
import styles from './style.module.scss' import styles from './style.module.scss'
import { useNavigate } from 'react-router-dom' import { useNavigate } from 'react-router-dom'
import { queryNip05 } from '../../utils' import { pubToHex, queryNip05 } from '../../utils'
export const Login = () => { export const Login = () => {
const dispatch: Dispatch = useDispatch() const dispatch: Dispatch = useDispatch()
@ -206,7 +207,71 @@ export const Login = () => {
}) })
} }
const loginWithBunkerConnectionString = async () => {
// Extract the key
const keyStartIndex = inputValue.indexOf('bunker://') + 'bunker://'.length
const keyEndIndex = inputValue.indexOf('?relay=')
const key = inputValue.substring(keyStartIndex, keyEndIndex)
const pubkey = await pubToHex(key)
if (!pubkey) {
toast.error('Invalid pubkey in bunker connection string.')
setIsLoading(false)
return
}
// Extract the relay value
const relayIndex = inputValue.indexOf('relay=')
const relay = inputValue.substring(
relayIndex + 'relay='.length,
inputValue.length
)
setIsLoading(true)
setLoadingSpinnerDesc('Initializing bunker NDK')
await nostrController.nsecBunkerInit([relay])
setLoadingSpinnerDesc('Creating remote signer')
await nostrController
.createNsecBunkerSigner(pubkey)
.then(async (signer) => {
signer.on('authUrl', (url: string) => {
setAuthUrl(url)
})
dispatch(updateLoginMethod(LoginMethods.nsecBunker))
dispatch(updateNsecbunkerPubkey(pubkey))
dispatch(updateNsecbunkerRelay(relay))
setLoadingSpinnerDesc('Authenticating and finding metadata')
const redirectPath = await authController
.authenticateAndFindMetadata(pubkey!)
.catch((err) => {
toast.error('Error occurred in authentication: ' + err)
return null
})
if (redirectPath) navigate(redirectPath)
})
.catch((err) => {
toast.error(
'An error occurred while creating nsecbunker signer: ' + err
)
})
.finally(() => {
setIsLoading(false)
setLoadingSpinnerDesc('')
})
}
const login = () => { const login = () => {
if (inputValue.startsWith('bunker://')) {
return loginWithBunkerConnectionString()
}
if (inputValue.startsWith('nsec')) { if (inputValue.startsWith('nsec')) {
return loginWithNsec() return loginWithNsec()
} }
@ -238,7 +303,7 @@ export const Login = () => {
<div className={styles.loginPage}> <div className={styles.loginPage}>
<Typography variant='h4'>Welcome to Sigit</Typography> <Typography variant='h4'>Welcome to Sigit</Typography>
<TextField <TextField
label='nip05 / npub / nsec' label='nip05 / npub / nsec / bunker connx string'
value={inputValue} value={inputValue}
onChange={(e) => setInputValue(e.target.value)} onChange={(e) => setInputValue(e.target.value)}
sx={{ width: '100%', mt: 2 }} sx={{ width: '100%', mt: 2 }}

View File

@ -4,5 +4,6 @@ export const SET_AUTH_STATE = 'SET_AUTH_STATE'
export const UPDATE_LOGIN_METHOD = 'UPDATE_LOGIN_METHOD' export const UPDATE_LOGIN_METHOD = 'UPDATE_LOGIN_METHOD'
export const UPDATE_KEYPAIR = 'UPDATE_KEYPAIR' export const UPDATE_KEYPAIR = 'UPDATE_KEYPAIR'
export const UPDATE_NSECBUNKER_PUBKEY = 'UPDATE_NSECBUNKER_PUBKEY' export const UPDATE_NSECBUNKER_PUBKEY = 'UPDATE_NSECBUNKER_PUBKEY'
export const UPDATE_NSECBUNKER_RELAY = 'UPDATE_NSECBUNKER_RELAY'
export const SET_METADATA_EVENT = 'SET_METADATA_EVENT' export const SET_METADATA_EVENT = 'SET_METADATA_EVENT'

View File

@ -6,7 +6,8 @@ import {
SetAuthState, SetAuthState,
UpdateKeyPair, UpdateKeyPair,
UpdateLoginMethod, UpdateLoginMethod,
UpdateNsecBunkerPubkey UpdateNsecBunkerPubkey,
UpdateNsecbunkerRelay
} from './types' } from './types'
export const setAuthState = (payload: AuthState): SetAuthState => ({ export const setAuthState = (payload: AuthState): SetAuthState => ({
@ -32,3 +33,10 @@ export const updateNsecbunkerPubkey = (
type: ActionTypes.UPDATE_NSECBUNKER_PUBKEY, type: ActionTypes.UPDATE_NSECBUNKER_PUBKEY,
payload payload
}) })
export const updateNsecbunkerRelay = (
payload: string | undefined
): UpdateNsecbunkerRelay => ({
type: ActionTypes.UPDATE_NSECBUNKER_RELAY,
payload
})

View File

@ -11,12 +11,13 @@ const reducer = (
): AuthState | null => { ): AuthState | null => {
switch (action.type) { switch (action.type) {
case ActionTypes.SET_AUTH_STATE: { case ActionTypes.SET_AUTH_STATE: {
const { loginMethod, keyPair, nsecBunkerPubkey } = state const { loginMethod, keyPair, nsecBunkerPubkey, nsecbunkerRelay } = state
return { return {
loginMethod, loginMethod,
keyPair, keyPair,
nsecBunkerPubkey, nsecBunkerPubkey,
nsecbunkerRelay,
...action.payload ...action.payload
} }
} }
@ -47,6 +48,15 @@ const reducer = (
} }
} }
case ActionTypes.UPDATE_NSECBUNKER_RELAY: {
const { payload } = action
return {
...state,
nsecbunkerRelay: payload
}
}
default: default:
return state return state
} }

View File

@ -18,6 +18,7 @@ export interface AuthState {
loginMethod?: LoginMethods loginMethod?: LoginMethods
keyPair?: Keys keyPair?: Keys
nsecBunkerPubkey?: string nsecBunkerPubkey?: string
nsecbunkerRelay?: string
} }
export interface SetAuthState { export interface SetAuthState {
@ -40,8 +41,14 @@ export interface UpdateNsecBunkerPubkey {
payload: string | undefined payload: string | undefined
} }
export interface UpdateNsecbunkerRelay {
type: typeof ActionTypes.UPDATE_NSECBUNKER_RELAY
payload: string | undefined
}
export type AuthDispatchTypes = export type AuthDispatchTypes =
| SetAuthState | SetAuthState
| UpdateLoginMethod | UpdateLoginMethod
| UpdateKeyPair | UpdateKeyPair
| UpdateNsecBunkerPubkey | UpdateNsecBunkerPubkey
| UpdateNsecbunkerRelay