mirror of
https://github.com/Qortal/Qortal-Hub.git
synced 2025-04-29 14:27:52 +00:00
fix saving of apikeys
This commit is contained in:
parent
cc92b7d9bd
commit
ef5e68d0a9
102
src/App.tsx
102
src/App.tsx
@ -400,6 +400,11 @@ function App() {
|
|||||||
const [isOpenSendQort, setIsOpenSendQort] = useState(false);
|
const [isOpenSendQort, setIsOpenSendQort] = useState(false);
|
||||||
const [isOpenSendQortSuccess, setIsOpenSendQortSuccess] = useState(false);
|
const [isOpenSendQortSuccess, setIsOpenSendQortSuccess] = useState(false);
|
||||||
const [rootHeight, setRootHeight] = useState("100%");
|
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 [isSettingsOpen, setIsSettingsOpen] = useState(false);
|
||||||
const [showSeed, setShowSeed] = useState(false)
|
const [showSeed, setShowSeed] = useState(false)
|
||||||
const [creationStep, setCreationStep] = useState(1)
|
const [creationStep, setCreationStep] = useState(1)
|
||||||
@ -516,7 +521,9 @@ function App() {
|
|||||||
globalApiKey = key;
|
globalApiKey = key;
|
||||||
};
|
};
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
window
|
try {
|
||||||
|
setIsLoading(true);
|
||||||
|
window
|
||||||
.sendMessage("getApiKey")
|
.sendMessage("getApiKey")
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
if (response) {
|
if (response) {
|
||||||
@ -529,7 +536,36 @@ function App() {
|
|||||||
"Failed to get API key:",
|
"Failed to get API key:",
|
||||||
error?.message || "An error occurred"
|
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(() => {
|
useEffect(() => {
|
||||||
if (extState) {
|
if (extState) {
|
||||||
@ -627,8 +663,6 @@ function App() {
|
|||||||
setdecryptedWallet(null);
|
setdecryptedWallet(null);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
|
|
||||||
error = e;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -848,36 +882,6 @@ function App() {
|
|||||||
// REMOVED FOR MOBILE 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) => {
|
const getUserInfo = useCallback(async (useTimer?: boolean) => {
|
||||||
try {
|
try {
|
||||||
@ -1712,6 +1716,10 @@ function App() {
|
|||||||
globalApiKey={globalApiKey}
|
globalApiKey={globalApiKey}
|
||||||
setApiKey={setApiKey}
|
setApiKey={setApiKey}
|
||||||
handleSetGlobalApikey={handleSetGlobalApikey}
|
handleSetGlobalApikey={handleSetGlobalApikey}
|
||||||
|
currentNode={currentNode}
|
||||||
|
setCurrentNode={setCurrentNode}
|
||||||
|
setUseLocalNode={setUseLocalNode}
|
||||||
|
useLocalNode={useLocalNode}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{/* {extState !== "not-authenticated" && (
|
{/* {extState !== "not-authenticated" && (
|
||||||
@ -2467,7 +2475,31 @@ function App() {
|
|||||||
}}
|
}}
|
||||||
ref={passwordRef}
|
ref={passwordRef}
|
||||||
/>
|
/>
|
||||||
<Spacer height="20px" />
|
{useLocalNode ? (
|
||||||
|
<>
|
||||||
|
<Spacer height="20px" />
|
||||||
|
<Typography
|
||||||
|
sx={{
|
||||||
|
fontSize: "12px",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{"Using node: "} {currentNode?.url}
|
||||||
|
</Typography>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<Spacer height="20px" />
|
||||||
|
<Typography
|
||||||
|
sx={{
|
||||||
|
fontSize: "12px",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{"Using gateway"}
|
||||||
|
</Typography>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Spacer height="20px" />
|
||||||
<CustomButton onClick={authenticateWallet}>
|
<CustomButton onClick={authenticateWallet}>
|
||||||
Authenticate
|
Authenticate
|
||||||
</CustomButton>
|
</CustomButton>
|
||||||
|
@ -22,13 +22,19 @@ import Info from "../assets/svgs/Info.svg";
|
|||||||
import HelpIcon from '@mui/icons-material/Help';
|
import HelpIcon from '@mui/icons-material/Help';
|
||||||
import { CustomizedSnackbars } from "../components/Snackbar/Snackbar";
|
import { CustomizedSnackbars } from "../components/Snackbar/Snackbar";
|
||||||
import { set } from "lodash";
|
import { set } from "lodash";
|
||||||
import { cleanUrl, isUsingLocal } from "../background";
|
import { cleanUrl, gateways, isUsingLocal } from "../background";
|
||||||
import { GlobalContext } from "../App";
|
import { GlobalContext } from "../App";
|
||||||
|
|
||||||
const manifestData = {
|
const manifestData = {
|
||||||
version: "0.5.0",
|
version: "0.5.0",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function removeTrailingSlash(url) {
|
||||||
|
return url.replace(/\/+$/, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export const NotAuthenticated = ({
|
export const NotAuthenticated = ({
|
||||||
getRootProps,
|
getRootProps,
|
||||||
getInputProps,
|
getInputProps,
|
||||||
@ -38,18 +44,22 @@ export const NotAuthenticated = ({
|
|||||||
setApiKey,
|
setApiKey,
|
||||||
globalApiKey,
|
globalApiKey,
|
||||||
handleSetGlobalApikey,
|
handleSetGlobalApikey,
|
||||||
|
currentNode,
|
||||||
|
setCurrentNode,
|
||||||
|
useLocalNode,
|
||||||
|
setUseLocalNode
|
||||||
}) => {
|
}) => {
|
||||||
const [isValidApiKey, setIsValidApiKey] = useState<boolean | null>(null);
|
const [isValidApiKey, setIsValidApiKey] = useState<boolean | null>(null);
|
||||||
const [hasLocalNode, setHasLocalNode] = useState<boolean | null>(null);
|
const [hasLocalNode, setHasLocalNode] = useState<boolean | null>(null);
|
||||||
const [useLocalNode, setUseLocalNode] = useState(false);
|
// const [useLocalNode, setUseLocalNode] = useState(false);
|
||||||
const [openSnack, setOpenSnack] = React.useState(false);
|
const [openSnack, setOpenSnack] = React.useState(false);
|
||||||
const [infoSnack, setInfoSnack] = React.useState(null);
|
const [infoSnack, setInfoSnack] = React.useState(null);
|
||||||
const [show, setShow] = React.useState(false);
|
const [show, setShow] = React.useState(false);
|
||||||
const [mode, setMode] = React.useState("list");
|
const [mode, setMode] = React.useState("list");
|
||||||
const [customNodes, setCustomNodes] = React.useState(null);
|
const [customNodes, setCustomNodes] = React.useState(null);
|
||||||
const [currentNode, setCurrentNode] = React.useState({
|
// const [currentNode, setCurrentNode] = React.useState({
|
||||||
url: "http://127.0.0.1:12391",
|
// url: "http://127.0.0.1:12391",
|
||||||
});
|
// });
|
||||||
const [importedApiKey, setImportedApiKey] = React.useState(null);
|
const [importedApiKey, setImportedApiKey] = React.useState(null);
|
||||||
//add and edit states
|
//add and edit states
|
||||||
const [url, setUrl] = React.useState("http://");
|
const [url, setUrl] = React.useState("http://");
|
||||||
@ -70,6 +80,34 @@ export const NotAuthenticated = ({
|
|||||||
const text = e.target.result; // Get the file content
|
const text = e.target.result; // Get the file content
|
||||||
|
|
||||||
setImportedApiKey(text); // Store the file content in the state
|
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
|
reader.readAsText(file); // Read the file as text
|
||||||
}
|
}
|
||||||
@ -105,11 +143,16 @@ export const NotAuthenticated = ({
|
|||||||
window
|
window
|
||||||
.sendMessage("getCustomNodesFromStorage")
|
.sendMessage("getCustomNodesFromStorage")
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
if (response) {
|
|
||||||
setCustomNodes(response || []);
|
setCustomNodes(response || []);
|
||||||
window.electronAPI.setAllowedDomains(response?.map((node)=> node.url))
|
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) => {
|
.catch((error) => {
|
||||||
console.error(
|
console.error(
|
||||||
@ -130,14 +173,42 @@ export const NotAuthenticated = ({
|
|||||||
hasLocalNodeRef.current = hasLocalNode;
|
hasLocalNodeRef.current = hasLocalNode;
|
||||||
}, [hasLocalNode]);
|
}, [hasLocalNode]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const validateApiKey = useCallback(async (key, fromStartUp) => {
|
const validateApiKey = useCallback(async (key, fromStartUp) => {
|
||||||
try {
|
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;
|
if (!currentNodeRef.current) return;
|
||||||
const stillHasLocal = await checkIfUserHasLocalNode()
|
const stillHasLocal = await checkIfUserHasLocalNode()
|
||||||
const isLocalKey = cleanUrl(key?.url) === "127.0.0.1:12391";
|
|
||||||
if (isLocalKey && !stillHasLocal && !fromStartUp) {
|
if (isLocalKey && !stillHasLocal && !fromStartUp) {
|
||||||
throw new Error("Please turn on your local node");
|
throw new Error("Please turn on your local node");
|
||||||
}
|
}
|
||||||
|
//check custom nodes
|
||||||
|
// !gateways.some(gateway => apiKey?.url?.includes(gateway))
|
||||||
const isCurrentNodeLocal =
|
const isCurrentNodeLocal =
|
||||||
cleanUrl(currentNodeRef.current?.url) === "127.0.0.1:12391";
|
cleanUrl(currentNodeRef.current?.url) === "127.0.0.1:12391";
|
||||||
if (isLocalKey && !isCurrentNodeLocal) {
|
if (isLocalKey && !isCurrentNodeLocal) {
|
||||||
@ -240,12 +311,12 @@ export const NotAuthenticated = ({
|
|||||||
let nodes = [...(myNodes || [])];
|
let nodes = [...(myNodes || [])];
|
||||||
if (customNodeToSaveIndex !== null) {
|
if (customNodeToSaveIndex !== null) {
|
||||||
nodes.splice(customNodeToSaveIndex, 1, {
|
nodes.splice(customNodeToSaveIndex, 1, {
|
||||||
url,
|
url: removeTrailingSlash(url),
|
||||||
apikey: customApikey,
|
apikey: customApikey,
|
||||||
});
|
});
|
||||||
} else if (url && customApikey) {
|
} else if (url && customApikey) {
|
||||||
nodes.push({
|
nodes.push({
|
||||||
url,
|
url: removeTrailingSlash(url),
|
||||||
apikey: customApikey,
|
apikey: customApikey,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user