From 6ac604bcf312b0850fe27ff379395599c2fc66a9 Mon Sep 17 00:00:00 2001 From: prolic Date: Mon, 12 Aug 2024 15:43:15 -0400 Subject: [PATCH] finish welcome screen - users can now generate new accounts - users can import nsec - users can import private hex key - users can import mnemonic (also with password) --- resources/qml/content/App.qml | 1 - resources/qml/content/WelcomeScreen.ui.qml | 29 ++++++--------- src/Main.hs | 42 +++++++++++++--------- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/resources/qml/content/App.qml b/resources/qml/content/App.qml index ce0d076..996f20e 100644 --- a/resources/qml/content/App.qml +++ b/resources/qml/content/App.qml @@ -34,7 +34,6 @@ ApplicationWindow { } ErrorScreen { - id: errorScreen visible: errorMsg != "" } diff --git a/resources/qml/content/WelcomeScreen.ui.qml b/resources/qml/content/WelcomeScreen.ui.qml index 86a8e8d..642f9fd 100644 --- a/resources/qml/content/WelcomeScreen.ui.qml +++ b/resources/qml/content/WelcomeScreen.ui.qml @@ -19,12 +19,12 @@ Rectangle { Rectangle { width: parent.parent.width - 20 height: 80 - color: "#f0f0f0" // Light grey background - border.color: "#e0e0e0" // Slightly darker grey for border + color: "#f0f0f0" + border.color: "#e0e0e0" border.width: 2 anchors.margins: 10 - radius: 10 // Rounded corners + radius: 10 Column { width: parent.width @@ -62,10 +62,10 @@ Rectangle { Rectangle { width: parent.parent.width * 0.6 - 15 height: 400 - color: "#f0f0f0" // Light grey background - border.color: "#e0e0e0" // Slightly darker grey for border + color: "#f0f0f0" + border.color: "#e0e0e0" border.width: 2 - radius: 10 // Rounded corners + radius: 10 ColumnLayout { anchors.fill: parent @@ -151,7 +151,7 @@ Rectangle { if (radionsec.checked) { importSecretKey(secretkey.text) } else if (radioseedphrase.checked) { - importSeedPhrase(seedphrase.text, + importSeedphrase(seedphrase.text, password.text) } } @@ -163,10 +163,10 @@ Rectangle { Rectangle { width: parent.parent.width * 0.4 - 15 height: 400 - color: "#f0f0f0" // Light grey background - border.color: "#e0e0e0" // Slightly darker grey for border + color: "#f0f0f0" + border.color: "#e0e0e0" border.width: 2 - radius: 10 // Rounded corners + radius: 10 ColumnLayout { anchors.fill: parent @@ -212,12 +212,3 @@ Rectangle { } } } - -/*##^## -Designer { - D{i:0;formeditorZoom:0.5}D{i:5}D{i:6}D{i:4}D{i:3}D{i:2}D{i:10}D{i:11}D{i:12}D{i:14} -D{i:15}D{i:13}D{i:16}D{i:17}D{i:18}D{i:19}D{i:20}D{i:9}D{i:8}D{i:23}D{i:24}D{i:25} -D{i:26}D{i:27}D{i:22}D{i:21}D{i:7}D{i:1} -} -##^##*/ - diff --git a/src/Main.hs b/src/Main.hs index d699da3..5fd2edf 100755 --- a/src/Main.hs +++ b/src/Main.hs @@ -48,6 +48,12 @@ type ModelVar = MVar AppModel createContext :: ModelVar -> SignalKey (IO ()) -> IO (ObjRef ()) createContext modelVar changeKey = do + let handleError :: ObjRef() -> String -> IO () + handleError obj err = do + model <- takeMVar modelVar + putMVar modelVar model { errorMsg = pack err } + fireSignal changeKey obj + rootClass <- newClass [ defPropertySigRW' "seedphrase" changeKey (\_ -> do @@ -82,9 +88,7 @@ createContext modelVar changeKey = do (\_ -> do model <- readMVar modelVar return $ errorMsg model) - (\obj newErrorMsg -> do - modifyMVar_ modelVar $ \model -> return model { errorMsg = newErrorMsg } - fireSignal changeKey obj), + (\obj newErrorMsg -> handleError obj $ unpack newErrorMsg), defMethod' "importSecretKey" $ \this (input :: Text) -> do mkp <- importSecretKey input @@ -93,20 +97,25 @@ createContext modelVar changeKey = do model <- takeMVar modelVar putMVar modelVar model { keyPair = mkp, currentScreen = HomeScreen } fireSignal changeKey this - Nothing -> do - model <- takeMVar modelVar - putMVar modelVar model { errorMsg = "Error: Importing secret key failed" } - fireSignal changeKey this, + Nothing -> handleError this "Error: Importing secret key failed", - defMethod' "generateSeedphrase" $ \this -> do - let handleError :: String -> IO () - handleError err = do - model <- takeMVar modelVar - putMVar modelVar model { errorMsg = "Error: " <> pack err } - fireSignal changeKey this + defMethod' "importSeedphrase" $ \this input pwd -> do + mkp <- mnemonicToKeyPair input pwd + case mkp of + Right kp -> do + let secKey = keyPairToSecKey kp + importSecretKey (secKeyToBech32 secKey) >>= \mkp' -> + case mkp' of + Just _ -> do + model <- takeMVar modelVar + putMVar modelVar model { keyPair = mkp', currentScreen = HomeScreen } + fireSignal changeKey this + Nothing -> handleError this "Unknown error generating new keys" + Left err -> handleError this err, - createMnemonic >>= either handleError (\m' -> do - mnemonicToKeyPair m' "" >>= either handleError (\mkp' -> do + defMethod' "generateSeedphrase" $ \this -> do + createMnemonic >>= either (handleError this) (\m' -> do + mnemonicToKeyPair m' "" >>= either (handleError this) (\mkp' -> do let secKey = keyPairToSecKey mkp' importSecretKey (secKeyToBech32 secKey) >>= \mkp -> case mkp of @@ -114,7 +123,7 @@ createContext modelVar changeKey = do model <- takeMVar modelVar putMVar modelVar model { seedphrase = m', keyPair = mkp } fireSignal changeKey this - Nothing -> handleError "Unknown error generating new keys" + Nothing -> handleError this "Unknown error generating new keys" ) ) @@ -123,7 +132,6 @@ createContext modelVar changeKey = do newObject rootClass () - importSecretKey :: Text -> IO (Maybe KeyPair) importSecretKey input = do let skMaybe = if "nsec" `isPrefixOf` input