forked from decred/decrediton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vspapi.js
183 lines (165 loc) · 4.86 KB
/
vspapi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { getJSON, postJSON } from "helpers/fetch";
import querystring from "querystring";
const URL_BASE = "https://api.decred.org";
const LEGACY_GET = (path, apiToken) => {
const config = {
headers: {
Authorization: "Bearer " + apiToken
}
};
return getJSON(path, config);
};
const GET = (path, vspClientSig) => {
const config = vspClientSig
? {
headers: {
"VSP-CLIENT-SIGNATURE": vspClientSig
}
}
: {};
return getJSON(path, config);
};
const LEGACY_POST = (path, apiToken, json) => {
const config = {
headers: {
Authorization: "Bearer " + apiToken
}
};
return postJSON(path, querystring.stringify(json), 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);
};
// stakePoolInfo gets vsp info from vsps v1 and v2.
// This can be removed after stopping to support them.
export function stakePoolInfo(cb) {
// stakepPoolInfoResponseToConfig converts a response object for the
// stakePoolInfo call into an object array of available stakepool configs.
const stakepPoolInfoResponseToConfig = (response) => {
const stakePoolNames = Object.keys(response.data);
return stakePoolNames
.map((name) => {
const {
APIEnabled,
URL,
Network,
APIVersionsSupported
} = response.data[name];
return !APIEnabled
? null
: { Host: URL, Network, APIVersionsSupported };
})
.filter((v) => v);
};
GET(URL_BASE + "/?c=gsd")
.then(function (response) {
cb(stakepPoolInfoResponseToConfig(response));
})
.catch(function (error) {
console.log("Error contacting remote stakepools api.", error);
cb(null, error);
});
}
// 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);
});
}
function parseStakePoolResults(response) {
const stakePoolNames = Object.keys(response.data);
return stakePoolNames
.map((name) => {
const { APIEnabled, URL } = response.data[name];
return !APIEnabled ? null : { Host: URL, ...response.data[name] };
})
.filter((v) => v);
}
export function allStakePoolStats(cb) {
GET(URL_BASE + "/?c=gsd")
.then(function (response) {
cb(parseStakePoolResults(response));
})
.catch(function (error) {
console.log("Error contacting remote stakepools api.", error);
cb(null, error);
});
}
export function setStakePoolAddress({ apiUrl, apiToken, pKAddress }, cb) {
LEGACY_POST(apiUrl + "/api/v1/address", apiToken, {
UserPubKeyAddr: pKAddress
})
.then(function (response) {
cb(response);
})
.catch(function (error) {
cb(null, error);
});
}
export function setVoteChoices({ apiUrl, apiToken, voteChoices }, cb) {
LEGACY_POST(apiUrl + "/api/v2/voting", apiToken, {
VoteBits: voteChoices.toString()
})
.then(function (response) {
cb(response);
})
.catch(function (error) {
cb(null, error);
});
}
export function getPurchaseInfo({ apiUrl, apiToken }, cb) {
LEGACY_GET(apiUrl + "/api/v1/getpurchaseinfo", apiToken)
.then(function (response) {
cb(response, null, apiUrl);
})
.catch(function (error) {
cb(null, error, apiUrl);
});
}
// statsFromStakePool grabs stats and config information directly from the
// stakepool host.
export function statsFromStakePool(host, cb) {
LEGACY_GET(host + "/api/v1/stats")
.then((resp) => cb(resp, null, host))
.catch((error) => cb(null, error, host));
}
// 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));
}