diff --git a/src/qortalRequests/get.ts b/src/qortalRequests/get.ts index fa59def..c7f7046 100644 --- a/src/qortalRequests/get.ts +++ b/src/qortalRequests/get.ts @@ -3169,6 +3169,20 @@ export const adminAction = async (data, isFromExtension) => { 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) { const missingFieldsString = missingFields.join(", "); const errorMsg = `Missing fields: ${missingFieldsString}`; @@ -3180,6 +3194,8 @@ export const adminAction = async (data, isFromExtension) => { } let apiEndpoint = ""; + let method = "GET"; // Default method + let includeValueInBody = false; switch (data.type.toLowerCase()) { case "stop": apiEndpoint = await createEndpoint("/admin/stop"); @@ -3190,19 +3206,58 @@ export const adminAction = async (data, isFromExtension) => { case "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; default: 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( { - text1: `Do you give this application permission to perform a node ${data.type}?`, + text1: permissionText, }, isFromExtension ); const { accepted } = resPermission; 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"); let res;