Skip to content

Commit

Permalink
Added testnet support
Browse files Browse the repository at this point in the history
  • Loading branch information
Duddino committed Nov 12, 2022
1 parent 61fb8f7 commit 44b2e9d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .env.mpw.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RPC_CREDENTIALS=masternode_test:p
ALLOWED_RPCS=listmasternodes,relaymasternodebroadcast,getbudgetprojection,mnbudgetrawvote,getbudgetinfo
PORT=3000
RPC_PORT=51473
TESTNET_RPC_PORT=51463 # Remove if you don't want testnet
4 changes: 3 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
RPC_CREDENTIALS=masternode_test:p
ALLOWED_RPCS=help,listmasternodes
PORT = 3000
PORT=3000
RPC_PORT=51473
TESTNET_RPC_PORT=51463 # Remove if you don't want testnet
41 changes: 31 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import cors from "cors";
const app = express()
app.use(cors());
const port = process.env["PORT"] || 3000;
const rpcPort = process.env["RPC_PORT"] || 51473;
const testnetRpcPort = process.env["TESTNET_RPC_PORT"];
const allowedRpcs = process.env["ALLOWED_RPCS"].split(",");

function checkEnv() {
Expand All @@ -20,9 +22,9 @@ const encodeBase64 = (data) => {
return Buffer.from(data).toString('base64');
}

async function makeRpc(name, ...params){
async function makeRpc(isTestnet, name, ...params){
try{
const output = await fetch('http://127.0.0.1:51473/', {
const output = await fetch(`http://127.0.0.1:${isTestnet ? testnetRpcPort : rpcPort}/`, {
method: 'POST',
headers: {
'content-type': 'text/plain;',
Expand Down Expand Up @@ -55,18 +57,19 @@ async function makeRpc(name, ...params){
}
}

//ce9efce67bf55fed12a162ab3c02e5b58e8f69ceac51524ae067ab06ad558a7f

function parseParams(params) {
return (params ? params.split(",") : [])
.map(v=>isNaN(v) ? v : parseInt(v))
.map(v=>v === "true" ? true : v)
.map(v=>v === "false" ? false : v);
}

app.get('/:rpc', async function(req, res) {
app.get('mainnet/:rpc', async function(req, res) {
try {
if (allowedRpcs.includes(req.params["rpc"])) {

const params = (req.query.params ? req.query.params.split(",") : [])
.map(v=>isNaN(v) ? v : parseInt(v))
.map(v=>v === "true" ? true : v)
.map(v=>v === "false" ? false : v);
const { status, response } = await makeRpc(req.params["rpc"], ...params);
const params = parseParams(req.query.params);
const { status, response } = await makeRpc(false, req.params["rpc"], ...params);
res.status(status).send(response + "");
} else {
const forbiddenStatus = 403;
Expand All @@ -77,6 +80,24 @@ app.get('/:rpc', async function(req, res) {
res.status(internalError).send("Internal server error");
}
});
if(testnetRpcPort) {
app.get('testnet/:rpc', async function(req, res) {
try {
if (allowedRpcs.includes(req.params["rpc"])) {

const params = parseParams(req.query.params);
const { status, response } = await makeRpc(true, req.params["rpc"], ...params);
res.status(status).send(response + "");
} else {
const forbiddenStatus = 403;
res.status(forbiddenStatus).send("Invalid RPC");
}
} catch (e) {
const internalError = 500;
res.status(internalError).send("Internal server error");
}
});
}

app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
Expand Down

0 comments on commit 44b2e9d

Please sign in to comment.