diff --git a/app/main.development.js b/app/main.development.js index 9d28f0ff61..2c897d9bee 100644 --- a/app/main.development.js +++ b/app/main.development.js @@ -30,6 +30,11 @@ import { allowExternalRequest } from "./main_dev/externalRequests"; import { setupProxy } from "./main_dev/proxy"; +import { + getAllVspsInfo, + getVSPInfo, + getVSPTicketStatus +} from "./main_dev/vspRequests"; import { getDaemonInfo, cleanShutdown, @@ -471,6 +476,12 @@ handle("check-daemon", getBlockChainInfo); handle("daemon-getinfo", getDaemonInfo); +handle("get-all-vsps-info", getAllVspsInfo); + +handle("get-vsp-info", getVSPInfo); + +handle("get-vsp-ticket-status", getVSPTicketStatus); + handle("clean-shutdown", () => cleanShutdown(mainWindow, app, GetDcrdPID(), GetDcrwPID()) ); diff --git a/app/main_dev/vspRequests.js b/app/main_dev/vspRequests.js new file mode 100644 index 0000000000..3f95e815dc --- /dev/null +++ b/app/main_dev/vspRequests.js @@ -0,0 +1,56 @@ +import { getJSON, postJSON } from "helpers/fetch"; + +const URL_BASE = "https://api.decred.org"; + +const GET = (path, vspClientSig) => { + const config = vspClientSig + ? { + headers: { + "VSP-Client-Signature": vspClientSig + } + } + : {}; + return getJSON(path, config); +}; + +const POST = (path, vspClientSig, json) => { + const config = vspClientSig + ? { + headers: { + "VSP-Client-Signature": vspClientSig + } + } + : {}; + // This json request is strigfied at the call which is making it. + return postJSON(path, json, config); +}; + +const pickResponse = (res) => ({ data: res.data, status: res.status }); + +// getAllVspsInfo gets vsp info from vsps v1 and v2. +// This can be removed after stopping to support them. +export const getAllVspsInfo = async () => { + const response = await GET(URL_BASE + "/?c=vsp"); + const hosts = Object.keys(response.data); + return hosts.reduce((availableVsps, host) => { + const vspData = response.data[host]; + if (vspData.closed) return availableVsps; + // call from /?c=vsp does not include its protocol, becaise when calling + // from dcrwallet, it is not used. Therefore, we need to add them. + availableVsps.push({ + host, + vspData + }); + return availableVsps; + }, []); +}; + +export const getVSPInfo = async (host) => ({ + ...pickResponse(await GET(host + "/api/v3/vspinfo")), + host +}); + +export const getVSPTicketStatus = async ({ host, sig, json }) => ({ + ...pickResponse(await POST(host + "/api/v3/ticketstatus", sig, json)), + host +}); diff --git a/app/middleware/vspapi.js b/app/middleware/vspapi.js deleted file mode 100644 index 82977fe459..0000000000 --- a/app/middleware/vspapi.js +++ /dev/null @@ -1,68 +0,0 @@ -import { getJSON, postJSON } from "helpers/fetch"; - -const URL_BASE = "https://api.decred.org"; - -const GET = (path, vspClientSig) => { - const config = vspClientSig - ? { - headers: { - "VSP-CLIENT-SIGNATURE": vspClientSig - } - } - : {}; - return getJSON(path, config); -}; - -const POST = (path, vspClientSig, json) => { - const config = vspClientSig - ? { - headers: { - "VSP-Client-Signature": vspClientSig - } - } - : {}; - // This json request is strigfied at the call which is making it. - return postJSON(path, json, config); -}; - -// getAllVspsInfo gets vsp info from vsps v1 and v2. -// This can be removed after stopping to support them. -export function getAllVspsInfo(cb) { - const readVspInfoResponse = (response) => { - const hosts = Object.keys(response.data); - const vsps = hosts.reduce((availableVsps, host) => { - const vspData = response.data[host]; - if (vspData.closed) return availableVsps; - // call from /?c=vsp does not include its protocol, becaise when calling - // from dcrwallet, it is not used. Therefore, we need to add them. - availableVsps.push({ - host, - vspData - }); - return availableVsps; - }, []); - return vsps; - }; - - GET(URL_BASE + "/?c=vsp") - .then(function (response) { - cb(readVspInfoResponse(response)); - }) - .catch(function (error) { - console.log("Error contacting remote stakepools api.", error); - cb(null, error); - }); -} - -// getVSPInfo gets the vspinfo. -export function getVSPInfo(host, cb) { - GET(host + "/api/v3/vspinfo") - .then((resp) => cb(resp, null, host)) - .catch((error) => cb(null, error, host)); -} - -export function getVSPTicketStatus({ host, sig, json }, cb) { - POST(host + "/api/v3/ticketstatus", sig, json) - .then((resp) => cb(resp, null, host)) - .catch((error) => cb(null, error, host)); -} diff --git a/app/wallet/vsp.js b/app/wallet/vsp.js index 41dbca78d6..a02a3d5407 100644 --- a/app/wallet/vsp.js +++ b/app/wallet/vsp.js @@ -1,24 +1,18 @@ -import * as api from "../middleware/vspapi"; import { withLog as log, withLogNoData } from "./index"; import * as cfg from "../config"; import * as cfgConstants from "constants/config"; import { ipcRenderer } from "electron"; +import { invoke } from "helpers/electronRenderer"; import { reloadAllowedExternalRequests } from "./daemon"; import { allowVSPHost as confDialogAllowVSPHost } from "./confirmationDialog"; -const promisifyReqLogNoData = (fnName, Req) => - withLogNoData( - (...args) => - new Promise((ok, fail) => - Req(...args, (res, err) => (err ? fail(err) : ok(res))) - ), - fnName - ); - -export const getVSPInfo = promisifyReqLogNoData("getVSPInfo", api.getVSPInfo); -export const getVSPTicketStatus = promisifyReqLogNoData( - "getVSPTicketStatus", - api.getVSPTicketStatus +export const getVSPInfo = withLogNoData( + (host) => invoke("get-vsp-info", host), + "getVSPInfo" +); +export const getVSPTicketStatus = withLogNoData( + (req) => invoke("get-vsp-ticket-status", req), + "getVSPTicketStatus" ); // addAllowedVSPsInCfg modifies the config file to allow the given VSP hosts @@ -37,9 +31,7 @@ const addAllowedVSPsInCfg = (hosts) => { }; export const getAllVSPs = withLogNoData(async () => { - const res = await new Promise((ok, fail) => - api.getAllVspsInfo((res, err) => (err ? fail(err) : ok(res))) - ); + const res = await invoke("get-all-vsps-info"); // Allow access to all VSPs returned by the official VSP listing endpoint. // This is less then ideal because this endpoint might eventually return