11import { useIntl } from '@cookbook/solid-intl' ;
2- import { Component , createEffect , createSignal , Match , Switch } from 'solid-js' ;
2+ import { Component , createEffect , createSignal , Match , Show , Switch } from 'solid-js' ;
33
44import { login as tLogin , actions as tActions } from '../../translations' ;
55
66import styles from './LoginModal.module.scss' ;
77import { hookForDev } from '../../lib/devTools' ;
88import ButtonPrimary from '../Buttons/ButtonPrimary' ;
99import CreatePinModal from '../CreatePinModal/CreatePinModal' ;
10- import TextInput from '../TextInput/TextInput' ;
11- import { nip19 } from '../../lib/nTools' ;
10+ import { nip19 , nip46 , SimplePool } from '../../lib/nTools' ;
1211import { storeSec } from '../../lib/localStore' ;
1312import AdvancedSearchDialog from '../AdvancedSearch/AdvancedSearchDialog' ;
14- import { accountStore , loginUsingExtension , loginUsingLocalNsec , loginUsingNpub , setSec } from '../../stores/accountStore' ;
13+ import { accountStore , doAfterLogin , loginUsingExtension , loginUsingLocalNsec , loginUsingNpub , setLoginType , setPublicKey , setSec } from '../../stores/accountStore' ;
1514import { Tabs } from '@kobalte/core/tabs' ;
1615
17- import extensionIcon from '../../assets/images/extension.svg' ;
18- import nsecIcon from '../../assets/images/nsec.svg' ;
1916import { useToastContext } from '../Toaster/Toaster' ;
2017import QrCode from '../QrCode/QrCode' ;
18+ import { useAppContext } from '../../contexts/AppContext' ;
19+ import { generateClientConnectionUrl , getAppSK , storeBunker } from '../../lib/PrimalNip46' ;
2120
2221const LoginModal : Component < {
2322 id ?: string ,
@@ -27,11 +26,13 @@ const LoginModal: Component<{
2726
2827 const intl = useIntl ( ) ;
2928 const toaster = useToastContext ( ) ;
29+ const app = useAppContext ( ) ;
3030
3131 const [ step , setStep ] = createSignal < 'login' | 'pin' | 'none' > ( 'login' )
3232 const [ enteredKey , setEnteredKey ] = createSignal ( '' ) ;
3333 const [ enteredNpub , setEnteredNpub ] = createSignal ( '' ) ;
3434
35+ const [ clientUrl , setClientUrl ] = createSignal ( '' ) ;
3536
3637 const [ activeTab , setActiveTab ] = createSignal ( 'simple' ) ;
3738
@@ -105,12 +106,7 @@ const LoginModal: Component<{
105106 } ;
106107
107108 createEffect ( ( ) => {
108- if ( props . open && step ( ) === 'login' ) {
109- nsecInput ?. focus ( ) ;
110- }
111- } ) ;
112-
113- createEffect ( ( ) => {
109+ if ( ! props . open ) return ;
114110 if ( activeTab ( ) === 'nsec' ) {
115111 setTimeout ( ( ) => {
116112 nsecInput ?. focus ( ) ;
@@ -122,8 +118,38 @@ const LoginModal: Component<{
122118 npubInput ?. focus ( ) ;
123119 } , 100 )
124120 }
121+
122+ if ( activeTab ( ) === 'simple' ) {
123+ setupSigner ( ) ;
124+ }
125125 } ) ;
126126
127+ const setupSigner = async ( ) => {
128+ const clientUrl = generateClientConnectionUrl ( ) ;
129+
130+ if ( clientUrl . length === 0 ) return ;
131+
132+ setClientUrl ( clientUrl ) ;
133+
134+ const sec = getAppSK ( ) ;
135+ if ( ! sec ) return ;
136+
137+ const pool = new SimplePool ( )
138+ const signer = await nip46 . BunkerSigner . fromURI ( sec , clientUrl , { pool } )
139+
140+ storeBunker ( signer ) ;
141+ const pk = await signer . getPublicKey ( ) ;
142+
143+ setLoginType ( 'nip46' ) ;
144+ setPublicKey ( pk ) ;
145+ doAfterLogin ( pk ) ;
146+
147+ // cleanup
148+ await signer . close ( )
149+ pool . close ( [ ] )
150+
151+ }
152+
127153 const onKeyUp = ( e : KeyboardEvent ) => {
128154 if ( e . code === 'Enter' && isValidNsec ( ) ) {
129155 onLogin ( ) ;
@@ -167,11 +193,13 @@ const LoginModal: Component<{
167193 < Tabs . Content value = "simple" >
168194 < div class = { styles . extensionLogin } >
169195 < div class = { styles . qrCode } >
170- < QrCode
171- data = "https://primal.net"
172- width = { 234 }
173- height = { 234 }
174- />
196+ < Show when = { clientUrl ( ) . length > 0 } >
197+ < QrCode
198+ data = { clientUrl ( ) }
199+ width = { 234 }
200+ height = { 234 }
201+ />
202+ </ Show >
175203 </ div >
176204 < div class = { styles . simpleDesc } >
177205 < div class = { styles . loginExplain } >
0 commit comments