1
0
mirror of https://github.com/GOSTSec/gostexplr synced 2025-01-15 09:10:06 +00:00
gostexplr/routes/search.js

70 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-02-03 21:14:56 +05:00
var models = require('../models');
var express = require('express');
var router = express.Router();
/* GET home page. */
2018-02-03 23:54:25 +05:00
router.post('/', async function(req, res, next) {
2018-02-03 21:14:56 +05:00
2018-02-04 00:00:43 +05:00
let search = encodeURI(req.body.search).trim();
2018-02-04 00:09:03 +05:00
// checking extra -000 in the end
2018-02-04 00:00:43 +05:00
if (search.endsWith('-000')) {
search = search.slice(0, -4);
}
2018-02-03 21:14:56 +05:00
2018-02-04 00:09:03 +05:00
// checking if it is block index
const blockIndex = parseInt(search);
if (isNaN(blockIndex) === false && blockIndex.toString() === search) {
const block = await models.Block.findOne({
2018-02-06 22:29:42 +05:00
attributes: ['hash'],
2018-02-04 00:09:03 +05:00
where: {
height: blockIndex,
},
});
if (block) {
res.redirect(`/block/${block.hash}/`);
return;
}
}
if (search.length === 34) {
// looking for address
const address = await models.Address.findOne({
where: {
address: search,
},
});
if (address) {
res.redirect(`/address/${address.address}/`);
return;
}
} else if(search.length === 64) {
// looking for transaction
const transaction = await models.Transaction.findOne({
where: {
txid: search,
},
});
if (transaction) {
res.redirect(`/transaction/${transaction.txid}/`);
return;
}
2018-02-03 23:54:25 +05:00
// looking for block
const block = await models.Block.findOne({
2018-02-06 22:29:42 +05:00
attributes: ['hash'],
where: {
hash: search,
},
});
if (block) {
res.redirect(`/block/${block.hash}/`);
return;
}
2018-02-03 23:54:25 +05:00
}
res.status(404).render('404');
2018-02-03 21:14:56 +05:00
});
module.exports = router;