-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.js
144 lines (129 loc) · 4.03 KB
/
http.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
const express = require("express");
const { createProxyMiddleware } = require("http-proxy-middleware");
const fs = require("fs");
const path = require('path')
var http = require("http");
var https = require("https");
const { XummSdk } = require("xumm-sdk");
var cors = require("cors");
const bodyParser = require("body-parser");
const xrpl = require("xrpl");
const rateLimit = require('express-rate-limit');
const Contract = require("web3-eth-contract");
require("dotenv").config();
const erc721abi = fs.readFileSync(path.resolve(__dirname, 'erc721.json'), 'utf8');
// Create Express Server
const app = express();
app.use(cors());
// Configuration
const PORT = process.env.API_SERVICE_PORT;
const API_SERVICE_URL = process.env.API_SERVICE_URL;
// Proxy endpoints
app.use("/api/xls20bridge/**", [
customValidation,
createProxyMiddleware({
target: {
protocol: "http:",
host: process.env.BRIDGE_MASTER_PROCESS_RPC,
path: "/",
port: process.env.BRIDGE_MASTER_PROCESS_RPC_PORT,
},
changeOrigin: true,
onProxyReq,
}),
]);
function onProxyReq(proxyReq, req, res) {
// add custom header to request
try {
proxyReq.setHeader("x-api-key", process.env.BRIDGE_MASTER_PROCESS_API_KEY);
} catch(err) {
console.log(err);
}
// or log the req
}
function customValidation(req, res, next) {
try {
if (req.get("origin") == process.env.WHITELIST_URL) {
next();
} else {
res.status(401).send("UnAuthorized");
}
} catch {
res.status(401).send("UnAuthorized");
}
}
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use("/eth/getTokenUri", async function (req, res, next) {
try {
let contractAddress = req.body.contractAddress;
let tokenID = req.body.tokenId;
console.log(req.body.contractAddress);
Contract.setProvider(process.env.ETH_ENDPOINT);
var TokenContract = new Contract(JSON.parse(erc721abi), contractAddress);
await TokenContract.methods
.tokenURI(tokenID)
.call()
.then(async function (result) {
res.send({tokenuri: result});
});
} catch(err) {
console.log(err)
}
});
app.use("/eth/isApprovedForAll", async function (req, res, next) {
try {
let contractAddress = req.body.contractAddress;
let ownerAddress = req.body.ownerAddress;
let operatorAddress = req.body.operatorAddress;
Contract.setProvider(process.env.ETH_ENDPOINT);
var TokenContract = new Contract(JSON.parse(erc721abi), contractAddress);
await TokenContract.methods
.isApprovedForAll(ownerAddress,operatorAddress)
.call()
.then(async function (result) {
res.send({isApproved: result});
});
} catch(err) {console.log(err);}
});
app.use("/xumm/createpayload", async function (req, res, next) {
try {
const Sdk = new XummSdk(
process.env.XUMM_API_KEY,
process.env.XUMM_API_SECRET
);
const payload = await Sdk.payload.create(req.body, true);
res.send(payload);
} catch (err) {
console.log(err);
}
});
app.use("/xumm/getpayload", async function (req, res, next) {
try {
const Sdk = new XummSdk(
process.env.XUMM_API_KEY,
process.env.XUMM_API_SECRET
);
const payload = await Sdk.payload.get(req.body.payloadID);
res.send(payload);
} catch {}
});
//Rate Limiting
const apiLimiter = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minutes
max: 20, // Limit each IP to 20 requests per `window` (here, per 15 minutes)
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
})
const apiLimiter10 = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minutes
max: 10, // Limit each IP to 20 requests per `window` (here, per 15 minutes)
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
})
// your express configuration here
app.use('/api', apiLimiter)
app.use('/eth', apiLimiter10)
app.use('/xumm', apiLimiter10)
var httpServer = http.createServer(app);
httpServer.listen(PORT);