Skip to content

Commit

Permalink
Basic API structure
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdiyari committed Jul 4, 2018
1 parent 446a806 commit 6cdd695
Show file tree
Hide file tree
Showing 8 changed files with 2,772 additions and 0 deletions.
23 changes: 23 additions & 0 deletions api.js
@@ -0,0 +1,23 @@
// Main API process
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const global_props = require('./blockchain_apis/database/get_dynamic_global_properties.js');
const confirm_process = require('./login_apis/confirm_process');
app.use(bodyParser.json()); //support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); //support encoded bodies

app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});

//Adding APIs
app.use('/api/database/get_dynamic_global_properties', global_props);
app.use('/api/login/confirm_process', confirm_process);

//Listening
const host = "127.0.0.1";
const port = 4939;
app.listen(port,host, () => console.log(`App listening on ${host}:${port}`));
16 changes: 16 additions & 0 deletions blockchain_apis/database/get_dynamic_global_properties.js
@@ -0,0 +1,16 @@
// get DynamicGlobalProperties from steem blockchain
const express = require('express');
const router = express.Router();
const steem = require('steem');
const config = require('../../config');
steem.api.setOptions({ url: config.rpc });

router.get('/',function(req,res){
res.set('Content-Type', 'application/json');
const props = steem.api.getDynamicGlobalPropertiesAsync();
props.then(result=> res.status(200).send(JSON.stringify(result)))
.catch(e=> res.send('RPC node error!'));
})

//export API
module.exports = router;
12 changes: 12 additions & 0 deletions conf/mysql/index.js
@@ -0,0 +1,12 @@
// MySQL connection
const mysql = require('mysql');
const config = require('../../config');
var con = mysql.createConnection({
host: config.db.host,
user: config.db.user,
password: config.db.pw,
database: config.db.name,
charset: "utf8mb4"
});

module.exports = con;
10 changes: 10 additions & 0 deletions conf/steemconnect/index.js
@@ -0,0 +1,10 @@
// Steemconnect initialization
const sc2 = require('sc2-sdk');
const config = require('../../config');
const api = sc2.Initialize({
app: config.sc2.app,
callbackURL: config.sc2.callbackURL,
scope: config.sc2.scope
});

module.exports = api;
23 changes: 23 additions & 0 deletions config.js
@@ -0,0 +1,23 @@
// All variables may need changes in the future
var config = {};
config.db = {};
// MySQL details
config.db.pw = '';
config.db.user = 'root';
config.db.host = '127.0.0.1';
config.db.name = 'dblog';
//wifkey
config.wifkey = '';
//steemconnect
config.sc2 = {};
config.sc2.app = 'steem.app';
config.sc2.callbackURL = 'https://dblog.io';
config.sc2.scope = ['login'];
// RPC nodes
config.rpc = 'ws://127.0.0.1:8090';
config.rpc2 = 'ws://127.0.0.1:8090';
config.rpc3 = 'ws://127.0.0.1:8090';
config.rpchttp = 'http://127.0.0.1:8090';
config.nodejssrv = 'http://127.0.0.1';

module.exports = config;
159 changes: 159 additions & 0 deletions login_apis/confirm_process.js
@@ -0,0 +1,159 @@
// Confirming Login process
const express = require('express');
const router = express.Router();
const api = require('../conf/steemconnect');
const con = require('../conf/mysql');
const crypto = require("crypto");

//Process post requests
router.post('/', function(req, res){
res.set('Content-Type', 'application/json'); //JSON header
if(req.body.access_token && req.body.username && req.body.uid){
console.log(1);
const access_token = req.body.access_token;
api.setAccessToken(access_token); //set access_token to steemconnect
const uid = req.body.uid; //user id in blockchain
const username = req.body.username;
if(req.body.hash_key){ // skip if there was already correct hash_key
console.log(2);

const hash_key = req.body.hash_key;
CheckHash(hash_key,uid,username,function(result){
res.status(200).send(result);
});
}else{ //User don't have hash_key
console.log(3);

con.query('SELECT EXISTS(SELECT `username` FROM `users` WHERE `uid`=?)',[uid],
function (error, results, fields){
for (const i in results) {
for(const j in results[i]){
const exists = results[i][j];
if(exists){ // Get hash_key from database
console.log(4);

RetriveHash(uid,username,function(result){
res.status(200).send(result);
});
}else{ // Generate new hash_key
console.log(5);

SetHash(uid,username,function(result){
res.status(200).send(result);
});
}
}
}
}
);
}
}else{
res.status(401).end(); // Unauthorized
}
});

function CheckHash(hash_key,uid,username,callback) {
con.query('SELECT EXISTS(SELECT `username` FROM `users` WHERE `uid`=?)',[uid],
function (error, results, fields){
for (const i in results) {
for(const j in results[i]){
const exists = results[i][j];
if(exists){ // Username is already in the database
con.query('SELECT `hash_key` FROM `users` WHERE `uid`=?',[uid],
function (error, results, fields){
for (const i in results) {
const hash = results[i].hash_key;
if(hash == hash_key){ // hash_key is correct
callback(JSON.stringify({ // return hash_key
hash_key:hash_key,
username:username,
uid:uid
}));
}else{ // hash_key is not correct
// return correct hash_key
RetriveHash(uid,username,function(result){
callback(result);
});
//callback(JSON.stringify({error:'invalid request'}));
}
}
}
);
}else{ // Username is not in the database
// set a new hash_key
SetHash(uid,username,function(result){
callback(result);
});
//callback(JSON.stringify({error:'invalid request'}));
}
}
}
}
);
}
function RetriveHash(uid,username,callback){
con.query('SELECT `hash_key` FROM `users` WHERE `uid`=?',[uid],
function (error, results, fields){
for (const i in results) {
//call to steemconnect
api.me(function (err, ress) {
if(!err && ress){
// steemconnect access_token is correct
if(ress.account && ress.account.id == uid){
if(results[i].hash_key){ //hash_key isn't null
callback(JSON.stringify({ // return result
username:username,
uid:uid,
hash_key:results[i].hash_key
}));
}else{
// hash_key is null
callback(JSON.stringify({error:'invalid request'}));
}
}else{
// Not logged in steemconnect
callback(JSON.stringify({error:'invalid request'}));
}
}else{
// steemconnect call error
callback(JSON.stringify({error:'invalid request'}));
}
});
}
}
);
}
function SetHash(uid,username,callback){
// Create random hash_key
const hash = crypto.randomBytes(20).toString('hex');
//call to steemconnect
api.me(function (err, ress) {
if(!err && ress){
if(ress.account && ress.account.id == uid){
//logged in steemconnect
const username = ress.user;
con.query('INSERT INTO `users`(`uid`, `username`, `hash_key`) VALUES (?,?,?)',[uid,username,hash],
function (error, results, fields){
callback(JSON.stringify({ //return result
username:username,
uid:uid,
hash_key:hash
}));
}
);
}else{
callback(JSON.stringify({error:'invalid request'}));
}
}else{
// steemconnect call error
callback(JSON.stringify({error:'invalid request'}));
}
});
}
//export API
module.exports = router;

// keep MySQL connection live
setInterval(function (){
con.query('SELECT 1', function (error, results, fields) {});
},5000);

0 comments on commit 6cdd695

Please sign in to comment.