updated admin_action

This commit is contained in:
PhilReact 2024-12-17 08:19:37 +02:00
parent f97c91fe35
commit bd19a0b6d2

View File

@ -3048,46 +3048,102 @@ export const cancelSellOrder = async (data, isFromExtension) => {
}; };
export const adminAction = async (data, isFromExtension) => { export const adminAction = async (data, isFromExtension) => {
const requiredFields = [ const requiredFields = ["type"];
"type",
];
const missingFields: string[] = []; const missingFields: string[] = [];
requiredFields.forEach((field) => { requiredFields.forEach((field) => {
if (!data[field]) { if (!data[field]) {
missingFields.push(field); missingFields.push(field);
} }
}); });
// For actions that require a value, check for 'value' field
const actionsRequiringValue = [
"addpeer",
"removepeer",
"forcesync",
"addmintingaccount",
"removemintingaccount",
];
if (
actionsRequiringValue.includes(data.type.toLowerCase()) &&
!data.value
) {
missingFields.push("value");
}
if (missingFields.length > 0) { if (missingFields.length > 0) {
const missingFieldsString = missingFields.join(", "); const missingFieldsString = missingFields.join(", ");
const errorMsg = `Missing fields: ${missingFieldsString}`; const errorMsg = `Missing fields: ${missingFieldsString}`;
throw new Error(errorMsg); throw new Error(errorMsg);
} }
const isGateway = await isRunningGateway() const isGateway = await isRunningGateway();
if(isGateway){ if (isGateway) {
throw new Error('This action cannot be done through a gateway') throw new Error("This action cannot be done through a gateway");
} }
let apiEndpoint = ''; let apiEndpoint = "";
let method = "GET"; // Default method
let includeValueInBody = false;
switch (data.type.toLowerCase()) { switch (data.type.toLowerCase()) {
case 'stop': case "stop":
apiEndpoint = await createEndpoint('/admin/stop'); apiEndpoint = await createEndpoint("/admin/stop");
break; break;
case 'restart': case "restart":
apiEndpoint = await createEndpoint('/admin/restart'); apiEndpoint = await createEndpoint("/admin/restart");
break; break;
case 'bootstrap': case "bootstrap":
apiEndpoint = await createEndpoint('/admin/bootstrap'); apiEndpoint = await createEndpoint("/admin/bootstrap");
break;
case "addmintingaccount":
apiEndpoint = await createEndpoint("/admin/mintingaccounts");
method = "POST";
includeValueInBody = true;
break;
case "removemintingaccount":
apiEndpoint = await createEndpoint("/admin/mintingaccounts");
method = "DELETE";
includeValueInBody = true;
break;
case "forcesync":
apiEndpoint = await createEndpoint("/admin/forcesync");
method = "POST";
includeValueInBody = true;
break;
case "addpeer":
apiEndpoint = await createEndpoint("/peers");
method = "POST";
includeValueInBody = true;
break;
case "removepeer":
apiEndpoint = await createEndpoint("/peers");
method = "DELETE";
includeValueInBody = true;
break; break;
default: default:
throw new Error(`Unknown admin action type: ${data.type}`); throw new Error(`Unknown admin action type: ${data.type}`);
} }
// Prepare the permission prompt text
let permissionText = `Do you give this application permission to perform the admin action: ${data.type}`;
if (data.value) {
permissionText += ` with value: ${data.value}`;
}
const resPermission = await getUserPermission({ const resPermission = await getUserPermission(
text1: `Do you give this application permission to perform a node ${data.type}?`, {
}, isFromExtension); text1: permissionText,
},
isFromExtension
);
const { accepted } = resPermission; const { accepted } = resPermission;
if (accepted) { if (accepted) {
const response = await fetch(apiEndpoint); // Set up options for the API call
const options: RequestInit = {
method: method,
headers: {},
};
if (includeValueInBody) {
options.headers["Content-Type"] = "text/plain";
options.body = data.value;
}
const response = await fetch(apiEndpoint, options);
if (!response.ok) throw new Error("Failed to perform request"); if (!response.ok) throw new Error("Failed to perform request");
let res; let res;
@ -3100,7 +3156,6 @@ export const adminAction = async (data, isFromExtension) => {
} else { } else {
throw new Error("User declined request"); throw new Error("User declined request");
} }
}; };
export const openNewTab = async (data, isFromExtension) => { export const openNewTab = async (data, isFromExtension) => {