From ef5e68d0a9abae5975eb89082ad6fb614a382925 Mon Sep 17 00:00:00 2001 From: PhilReact Date: Thu, 9 Jan 2025 18:36:43 +0200 Subject: [PATCH] fix saving of apikeys --- src/App.tsx | 102 +++++++++++++++++++---------- src/ExtStates/NotAuthenticated.tsx | 93 ++++++++++++++++++++++---- 2 files changed, 149 insertions(+), 46 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index c8704cb..9de3b2c 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -400,6 +400,11 @@ function App() { const [isOpenSendQort, setIsOpenSendQort] = useState(false); const [isOpenSendQortSuccess, setIsOpenSendQortSuccess] = useState(false); const [rootHeight, setRootHeight] = useState("100%"); + const [currentNode, setCurrentNode] = useState({ + url: "http://127.0.0.1:12391", + }); + const [useLocalNode, setUseLocalNode] = useState(false); + const [isSettingsOpen, setIsSettingsOpen] = useState(false); const [showSeed, setShowSeed] = useState(false) const [creationStep, setCreationStep] = useState(1) @@ -516,7 +521,9 @@ function App() { globalApiKey = key; }; useEffect(() => { - window + try { + setIsLoading(true); + window .sendMessage("getApiKey") .then((response) => { if (response) { @@ -529,7 +536,36 @@ function App() { "Failed to get API key:", error?.message || "An error occurred" ); - }); + }).finally(()=> { + window + .sendMessage("getWalletInfo") + .then((response) => { + if (response && response?.walletInfo) { + setRawWallet(response?.walletInfo); + if ( + holdRefExtState.current === "web-app-request-payment" || + holdRefExtState.current === "web-app-request-connection" || + holdRefExtState.current === "web-app-request-buy-order" + ) + return; + if (response?.hasKeyPair) { + setExtstate("authenticated"); + } else { + setExtstate("wallet-dropped"); + } + } + }) + .catch((error) => { + console.error("Failed to get wallet info:", error); + }); + }) + } catch (error) { + + } finally { + setIsLoading(false); + + } + }, []); useEffect(() => { if (extState) { @@ -627,8 +663,6 @@ function App() { setdecryptedWallet(null); } catch (e) { console.log(e); - - error = e; } }, }); @@ -848,36 +882,6 @@ function App() { // REMOVED FOR MOBILE APP }; - useEffect(() => { - try { - setIsLoading(true); - - window - .sendMessage("getWalletInfo") - .then((response) => { - if (response && response?.walletInfo) { - setRawWallet(response?.walletInfo); - if ( - holdRefExtState.current === "web-app-request-payment" || - holdRefExtState.current === "web-app-request-connection" || - holdRefExtState.current === "web-app-request-buy-order" - ) - return; - if (response?.hasKeyPair) { - setExtstate("authenticated"); - } else { - setExtstate("wallet-dropped"); - } - } - }) - .catch((error) => { - console.error("Failed to get wallet info:", error); - }); - } catch (error) { - } finally { - setIsLoading(false); - } - }, []); const getUserInfo = useCallback(async (useTimer?: boolean) => { try { @@ -1712,6 +1716,10 @@ function App() { globalApiKey={globalApiKey} setApiKey={setApiKey} handleSetGlobalApikey={handleSetGlobalApikey} + currentNode={currentNode} + setCurrentNode={setCurrentNode} + setUseLocalNode={setUseLocalNode} + useLocalNode={useLocalNode} /> )} {/* {extState !== "not-authenticated" && ( @@ -2467,7 +2475,31 @@ function App() { }} ref={passwordRef} /> - + {useLocalNode ? ( + <> + + + {"Using node: "} {currentNode?.url} + + + ) : ( + <> + + + {"Using gateway"} + + + )} + + Authenticate diff --git a/src/ExtStates/NotAuthenticated.tsx b/src/ExtStates/NotAuthenticated.tsx index 0b0ef42..f7f4e91 100644 --- a/src/ExtStates/NotAuthenticated.tsx +++ b/src/ExtStates/NotAuthenticated.tsx @@ -22,13 +22,19 @@ import Info from "../assets/svgs/Info.svg"; import HelpIcon from '@mui/icons-material/Help'; import { CustomizedSnackbars } from "../components/Snackbar/Snackbar"; import { set } from "lodash"; -import { cleanUrl, isUsingLocal } from "../background"; +import { cleanUrl, gateways, isUsingLocal } from "../background"; import { GlobalContext } from "../App"; const manifestData = { version: "0.5.0", }; + +function removeTrailingSlash(url) { + return url.replace(/\/+$/, ''); +} + + export const NotAuthenticated = ({ getRootProps, getInputProps, @@ -38,18 +44,22 @@ export const NotAuthenticated = ({ setApiKey, globalApiKey, handleSetGlobalApikey, + currentNode, + setCurrentNode, + useLocalNode, + setUseLocalNode }) => { const [isValidApiKey, setIsValidApiKey] = useState(null); const [hasLocalNode, setHasLocalNode] = useState(null); - const [useLocalNode, setUseLocalNode] = useState(false); + // const [useLocalNode, setUseLocalNode] = useState(false); const [openSnack, setOpenSnack] = React.useState(false); const [infoSnack, setInfoSnack] = React.useState(null); const [show, setShow] = React.useState(false); const [mode, setMode] = React.useState("list"); const [customNodes, setCustomNodes] = React.useState(null); - const [currentNode, setCurrentNode] = React.useState({ - url: "http://127.0.0.1:12391", - }); + // const [currentNode, setCurrentNode] = React.useState({ + // url: "http://127.0.0.1:12391", + // }); const [importedApiKey, setImportedApiKey] = React.useState(null); //add and edit states const [url, setUrl] = React.useState("http://"); @@ -70,6 +80,34 @@ export const NotAuthenticated = ({ const text = e.target.result; // Get the file content setImportedApiKey(text); // Store the file content in the state + if(customNodes){ + setCustomNodes((prev)=> { + const copyPrev = [...prev] + const findLocalIndex = copyPrev?.findIndex((item)=> item?.url === 'http://127.0.0.1:12391') + if(findLocalIndex === -1){ + copyPrev.unshift({ + url: "http://127.0.0.1:12391", + apikey: text + }) + } else { + copyPrev[findLocalIndex] = { + url: "http://127.0.0.1:12391", + apikey: text + } + } + window + .sendMessage("setCustomNodes", copyPrev) + .catch((error) => { + console.error( + "Failed to set custom nodes:", + error.message || "An error occurred" + ); + }); + return copyPrev + }) + + } + }; reader.readAsText(file); // Read the file as text } @@ -105,11 +143,16 @@ export const NotAuthenticated = ({ window .sendMessage("getCustomNodesFromStorage") .then((response) => { - if (response) { + setCustomNodes(response || []); window.electronAPI.setAllowedDomains(response?.map((node)=> node.url)) - - } + if(Array.isArray(response)){ + const findLocal = response?.find((item)=> item?.url === 'http://127.0.0.1:12391') + if(findLocal && findLocal?.apikey){ + setImportedApiKey(findLocal?.apikey) + } + } + }) .catch((error) => { console.error( @@ -130,14 +173,42 @@ export const NotAuthenticated = ({ hasLocalNodeRef.current = hasLocalNode; }, [hasLocalNode]); + + const validateApiKey = useCallback(async (key, fromStartUp) => { try { + const isLocalKey = cleanUrl(key?.url) === "127.0.0.1:12391"; + if(fromStartUp && key?.url && key?.apikey && !isLocalKey && !gateways.some(gateway => apiKey?.url?.includes(gateway))){ + setCurrentNode({ + url: key?.url, + apikey: key?.apikey, + }); + const url = `${key?.url}/admin/apikey/test`; + const response = await fetch(url, { + method: "GET", + headers: { + accept: "text/plain", + "X-API-KEY": key?.apikey, // Include the API key here + }, + }); + + // Assuming the response is in plain text and will be 'true' or 'false' + const data = await response.text(); + if (data === "true") { + setIsValidApiKey(true); + setUseLocalNode(true); + return + } + + } if (!currentNodeRef.current) return; const stillHasLocal = await checkIfUserHasLocalNode() - const isLocalKey = cleanUrl(key?.url) === "127.0.0.1:12391"; + if (isLocalKey && !stillHasLocal && !fromStartUp) { throw new Error("Please turn on your local node"); } + //check custom nodes + // !gateways.some(gateway => apiKey?.url?.includes(gateway)) const isCurrentNodeLocal = cleanUrl(currentNodeRef.current?.url) === "127.0.0.1:12391"; if (isLocalKey && !isCurrentNodeLocal) { @@ -240,12 +311,12 @@ export const NotAuthenticated = ({ let nodes = [...(myNodes || [])]; if (customNodeToSaveIndex !== null) { nodes.splice(customNodeToSaveIndex, 1, { - url, + url: removeTrailingSlash(url), apikey: customApikey, }); } else if (url && customApikey) { nodes.push({ - url, + url: removeTrailingSlash(url), apikey: customApikey, }); }