2019-05-21 07:42:39 -07:00
|
|
|
"use strict"
|
2019-05-27 20:41:11 -07:00
|
|
|
const { Socket } = require("net")
|
2019-05-21 07:42:39 -07:00
|
|
|
|
2019-05-27 20:41:11 -07:00
|
|
|
const express = require("express")
|
2019-05-21 07:42:39 -07:00
|
|
|
|
|
|
|
|
// Middleware
|
2019-05-27 20:41:11 -07:00
|
|
|
const { routeRateLimit } = require("./middleware/route-ratelimit")
|
2019-05-21 07:42:39 -07:00
|
|
|
|
|
|
|
|
const path = require("path")
|
|
|
|
|
const logger = require("morgan")
|
|
|
|
|
const wlogger = require("./util/winston-logging")
|
|
|
|
|
const cookieParser = require("cookie-parser")
|
|
|
|
|
const bodyParser = require("body-parser")
|
|
|
|
|
const basicAuth = require("express-basic-auth")
|
|
|
|
|
const helmet = require("helmet")
|
|
|
|
|
const debug = require("debug")("rest-cloud:server")
|
|
|
|
|
const http = require("http")
|
|
|
|
|
const cors = require("cors")
|
|
|
|
|
const AuthMW = require("./middleware/auth")
|
|
|
|
|
|
|
|
|
|
const BitcoinCashZMQDecoder = require("bitcoincash-zmq-decoder")
|
|
|
|
|
|
|
|
|
|
const zmq = require("zeromq")
|
|
|
|
|
|
2019-05-27 20:41:11 -07:00
|
|
|
const sock = zmq.socket("sub")
|
2019-05-27 20:45:58 -07:00
|
|
|
/*
|
2019-05-21 07:42:39 -07:00
|
|
|
const swStats = require("swagger-stats")
|
|
|
|
|
let apiSpec
|
2019-05-27 20:41:11 -07:00
|
|
|
if (process.env.NETWORK === "mainnet")
|
2019-05-21 07:42:39 -07:00
|
|
|
apiSpec = require("./public/bitcoin-com-mainnet-rest-v2.json")
|
2019-05-27 20:41:11 -07:00
|
|
|
else apiSpec = require("./public/bitcoin-com-testnet-rest-v2.json")
|
2019-05-27 20:45:58 -07:00
|
|
|
*/
|
2019-05-21 07:42:39 -07:00
|
|
|
|
|
|
|
|
// v2
|
|
|
|
|
const indexV2 = require("./routes/v2/index")
|
|
|
|
|
const healthCheckV2 = require("./routes/v2/health-check")
|
|
|
|
|
const addressV2 = require("./routes/v2/address")
|
|
|
|
|
const blockV2 = require("./routes/v2/block")
|
|
|
|
|
const blockchainV2 = require("./routes/v2/blockchain")
|
|
|
|
|
const controlV2 = require("./routes/v2/control")
|
|
|
|
|
const generatingV2 = require("./routes/v2/generating")
|
|
|
|
|
const miningV2 = require("./routes/v2/mining")
|
|
|
|
|
const networkV2 = require("./routes/v2/network")
|
|
|
|
|
const rawtransactionsV2 = require("./routes/v2/rawtransactions")
|
|
|
|
|
const transactionV2 = require("./routes/v2/transaction")
|
|
|
|
|
const utilV2 = require("./routes/v2/util")
|
|
|
|
|
const slpV2 = require("./routes/v2/slp")
|
|
|
|
|
|
2019-05-31 08:42:57 -07:00
|
|
|
// v3
|
|
|
|
|
const indexV3 = require("./routes/v3/index")
|
|
|
|
|
const healthCheckV3 = require("./routes/v3/health-check")
|
|
|
|
|
const blockchainV3 = require("./routes/v3/blockchain")
|
|
|
|
|
const controlV3 = require("./routes/v3/control")
|
|
|
|
|
const generatingV3 = require("./routes/v3/generating")
|
|
|
|
|
const miningV3 = require("./routes/v3/mining")
|
|
|
|
|
const networkV3 = require("./routes/v3/network")
|
|
|
|
|
const rawtransactionsV3 = require("./routes/v3/rawtransactions")
|
|
|
|
|
const utilV3 = require("./routes/v3/util")
|
|
|
|
|
const slpV3 = require("./routes/v3/slp")
|
2019-06-13 15:42:00 -07:00
|
|
|
const xpubV3 = require("./routes/v3/xpub")
|
2019-05-31 08:42:57 -07:00
|
|
|
|
2019-05-21 07:42:39 -07:00
|
|
|
require("dotenv").config()
|
|
|
|
|
|
2019-05-27 20:41:11 -07:00
|
|
|
const app = express()
|
2019-05-21 07:42:39 -07:00
|
|
|
|
|
|
|
|
app.locals.env = process.env
|
|
|
|
|
|
2019-05-27 20:45:58 -07:00
|
|
|
//app.use(swStats.getMiddleware({ swaggerSpec: apiSpec }))
|
2019-05-21 07:42:39 -07:00
|
|
|
|
|
|
|
|
app.use(helmet())
|
|
|
|
|
|
|
|
|
|
app.use(cors())
|
|
|
|
|
app.enable("trust proxy")
|
|
|
|
|
|
|
|
|
|
// view engine setup
|
|
|
|
|
app.set("views", path.join(__dirname, "views"))
|
|
|
|
|
app.set("view engine", "jade")
|
|
|
|
|
|
|
|
|
|
app.use("/public", express.static(`${__dirname}/public`))
|
|
|
|
|
app.use(logger("dev"))
|
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false }))
|
|
|
|
|
app.use(cookieParser())
|
|
|
|
|
app.use(express.static(path.join(__dirname, "public")))
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// let username = process.env.USERNAME;
|
|
|
|
|
// let password = process.env.PASSWORD;
|
|
|
|
|
//
|
|
|
|
|
// app.use(basicAuth(
|
|
|
|
|
// {
|
|
|
|
|
// users: { username: password }
|
|
|
|
|
// }
|
|
|
|
|
// ));
|
|
|
|
|
|
|
|
|
|
// Make io accessible to our router
|
2019-05-27 20:41:11 -07:00
|
|
|
app.use((req, res, next) => {
|
|
|
|
|
req.io = io
|
2019-05-21 07:42:39 -07:00
|
|
|
|
2019-05-27 20:41:11 -07:00
|
|
|
next()
|
|
|
|
|
})
|
2019-05-21 07:42:39 -07:00
|
|
|
|
|
|
|
|
const v2prefix = "v2"
|
2019-05-31 08:42:57 -07:00
|
|
|
const v3prefix = "v3"
|
2019-05-21 07:42:39 -07:00
|
|
|
|
|
|
|
|
// Instantiate the authorization middleware, used to implement pro-tier rate limiting.
|
|
|
|
|
const auth = new AuthMW()
|
|
|
|
|
app.use(`/${v2prefix}/`, auth.mw())
|
2019-05-31 08:42:57 -07:00
|
|
|
app.use(`/${v3prefix}/`, auth.mw())
|
2019-05-21 07:42:39 -07:00
|
|
|
|
|
|
|
|
// Rate limit on all v2 routes
|
|
|
|
|
app.use(`/${v2prefix}/`, routeRateLimit)
|
|
|
|
|
app.use("/", indexV2)
|
|
|
|
|
app.use(`/${v2prefix}/` + `health-check`, healthCheckV2)
|
|
|
|
|
app.use(`/${v2prefix}/` + `address`, addressV2.router)
|
|
|
|
|
app.use(`/${v2prefix}/` + `blockchain`, blockchainV2.router)
|
|
|
|
|
app.use(`/${v2prefix}/` + `block`, blockV2.router)
|
|
|
|
|
app.use(`/${v2prefix}/` + `control`, controlV2.router)
|
|
|
|
|
app.use(`/${v2prefix}/` + `generating`, generatingV2)
|
|
|
|
|
app.use(`/${v2prefix}/` + `mining`, miningV2.router)
|
|
|
|
|
app.use(`/${v2prefix}/` + `network`, networkV2)
|
|
|
|
|
app.use(`/${v2prefix}/` + `rawtransactions`, rawtransactionsV2.router)
|
|
|
|
|
app.use(`/${v2prefix}/` + `transaction`, transactionV2.router)
|
|
|
|
|
app.use(`/${v2prefix}/` + `util`, utilV2.router)
|
|
|
|
|
app.use(`/${v2prefix}/` + `slp`, slpV2.router)
|
|
|
|
|
|
2019-06-18 13:21:06 -07:00
|
|
|
// Rate limit on all v3 routes
|
2019-05-31 08:42:57 -07:00
|
|
|
app.use(`/${v3prefix}/`, routeRateLimit)
|
|
|
|
|
app.use(`/${v3prefix}/` + `health-check`, healthCheckV3)
|
|
|
|
|
app.use(`/${v3prefix}/` + `blockchain`, blockchainV3.router)
|
|
|
|
|
app.use(`/${v3prefix}/` + `control`, controlV3.router)
|
|
|
|
|
app.use(`/${v3prefix}/` + `generating`, generatingV3)
|
|
|
|
|
app.use(`/${v3prefix}/` + `mining`, miningV3.router)
|
|
|
|
|
app.use(`/${v3prefix}/` + `network`, networkV3)
|
|
|
|
|
app.use(`/${v3prefix}/` + `rawtransactions`, rawtransactionsV3.router)
|
|
|
|
|
app.use(`/${v3prefix}/` + `util`, utilV3.router)
|
|
|
|
|
app.use(`/${v3prefix}/` + `slp`, slpV3.router)
|
2019-06-13 15:42:00 -07:00
|
|
|
app.use(`/${v3prefix}/` + `xpub`, xpubV3.router)
|
2019-05-31 08:42:57 -07:00
|
|
|
|
2019-05-21 07:42:39 -07:00
|
|
|
// catch 404 and forward to error handler
|
2019-05-27 20:41:11 -07:00
|
|
|
app.use((req, res, next) => {
|
|
|
|
|
const err = {
|
|
|
|
|
message: "Not Found",
|
|
|
|
|
status: 404
|
2019-05-21 07:42:39 -07:00
|
|
|
}
|
2019-05-27 20:41:11 -07:00
|
|
|
|
|
|
|
|
next(err)
|
|
|
|
|
})
|
2019-05-21 07:42:39 -07:00
|
|
|
|
|
|
|
|
// error handler
|
2019-05-27 20:41:11 -07:00
|
|
|
app.use((err, req, res, next) => {
|
2019-05-21 07:42:39 -07:00
|
|
|
const status = err.status || 500
|
|
|
|
|
|
|
|
|
|
// set locals, only providing error in development
|
|
|
|
|
res.locals.message = err.message
|
|
|
|
|
res.locals.error = req.app.get("env") === "development" ? err : {}
|
|
|
|
|
|
|
|
|
|
// render the error page
|
|
|
|
|
res.status(status)
|
|
|
|
|
res.json({
|
|
|
|
|
status: status,
|
|
|
|
|
message: err.message
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get port from environment and store in Express.
|
|
|
|
|
*/
|
|
|
|
|
const port = normalizePort(process.env.PORT || "3000")
|
|
|
|
|
app.set("port", port)
|
|
|
|
|
console.log(`rest.bitcoin.com started on port ${port}`)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create HTTP server.
|
|
|
|
|
*/
|
|
|
|
|
const server = http.createServer(app)
|
|
|
|
|
const io = require("socket.io").listen(server)
|
2019-05-27 20:41:11 -07:00
|
|
|
io.on("connection", socket => {
|
2019-05-21 07:42:39 -07:00
|
|
|
console.log("Socket Connected")
|
|
|
|
|
|
|
|
|
|
socket.on("disconnect", () => {
|
|
|
|
|
console.log("Socket Disconnected")
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Setup ZMQ connections if ZMQ URL and port provided
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (process.env.ZEROMQ_URL && process.env.ZEROMQ_PORT) {
|
2019-05-27 20:41:11 -07:00
|
|
|
console.log(
|
2019-06-09 14:31:36 -07:00
|
|
|
`Connecting to BCH ZMQ at ${process.env.ZEROMQ_URL}:${process.env.ZEROMQ_PORT}`
|
2019-05-27 20:41:11 -07:00
|
|
|
)
|
2019-05-21 07:42:39 -07:00
|
|
|
const bitcoincashZmqDecoder = new BitcoinCashZMQDecoder(process.env.NETWORK)
|
|
|
|
|
|
|
|
|
|
sock.connect(`tcp://${process.env.ZEROMQ_URL}:${process.env.ZEROMQ_PORT}`)
|
|
|
|
|
sock.subscribe("raw")
|
|
|
|
|
|
2019-05-27 20:41:11 -07:00
|
|
|
sock.on("message", (topic, message) => {
|
2019-05-21 07:42:39 -07:00
|
|
|
try {
|
|
|
|
|
const decoded = topic.toString("ascii")
|
|
|
|
|
if (decoded === "rawtx") {
|
|
|
|
|
const txd = bitcoincashZmqDecoder.decodeTransaction(message)
|
|
|
|
|
io.emit("transactions", JSON.stringify(txd, null, 2))
|
|
|
|
|
} else if (decoded === "rawblock") {
|
|
|
|
|
const blck = bitcoincashZmqDecoder.decodeBlock(message)
|
|
|
|
|
io.emit("blocks", JSON.stringify(blck, null, 2))
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2019-05-27 20:41:11 -07:00
|
|
|
const errorMessage = "Error processing ZMQ message"
|
2019-05-21 07:42:39 -07:00
|
|
|
console.log(errorMessage, error)
|
|
|
|
|
wlogger.error(errorMessage, error)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} else {
|
2019-05-27 20:41:11 -07:00
|
|
|
console.log(
|
|
|
|
|
"ZEROMQ_URL and ZEROMQ_PORT env vars missing. Skipping ZMQ connection."
|
|
|
|
|
)
|
2019-05-21 07:42:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Listen on provided port, on all network interfaces.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
server.listen(port)
|
|
|
|
|
server.on("error", onError)
|
|
|
|
|
server.on("listening", onListening)
|
|
|
|
|
|
|
|
|
|
// Set the time before a timeout error is generated. This impacts testing and
|
|
|
|
|
// the handling of timeout errors. Is 10 seconds too agressive?
|
|
|
|
|
server.setTimeout(30 * 1000)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Normalize a port into a number, string, or false.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-05-27 20:41:11 -07:00
|
|
|
function normalizePort(val) {
|
2019-05-21 07:42:39 -07:00
|
|
|
const port = parseInt(val, 10)
|
|
|
|
|
|
|
|
|
|
if (isNaN(port)) {
|
|
|
|
|
// named pipe
|
|
|
|
|
return val
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (port >= 0) {
|
|
|
|
|
// port number
|
|
|
|
|
return port
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Event listener for HTTP server "error" event.
|
|
|
|
|
*/
|
2019-05-27 20:41:11 -07:00
|
|
|
function onError(error) {
|
2019-05-21 07:42:39 -07:00
|
|
|
if (error.syscall !== "listen") throw error
|
|
|
|
|
|
|
|
|
|
const bind = typeof port === "string" ? `Pipe ${port}` : `Port ${port}`
|
|
|
|
|
|
|
|
|
|
// handle specific listen errors with friendly messages
|
|
|
|
|
switch (error.code) {
|
|
|
|
|
case "EACCES":
|
|
|
|
|
console.error(`${bind} requires elevated privileges`)
|
|
|
|
|
process.exit(1)
|
|
|
|
|
break
|
|
|
|
|
case "EADDRINUSE":
|
|
|
|
|
console.error(`${bind} is already in use`)
|
|
|
|
|
process.exit(1)
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Event listener for HTTP server "listening" event.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
function onListening() {
|
|
|
|
|
const addr = server.address()
|
|
|
|
|
const bind = typeof addr === "string" ? `pipe ${addr}` : `port ${addr.port}`
|
|
|
|
|
debug(`Listening on ${bind}`)
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
// module.exports = app;
|