Permalink
Browse files

api server

  • Loading branch information...
zenywallet committed Jun 12, 2018
1 parent 896020b commit d2ce781035015b997052922ee148b1c1720d6027
Showing with 442 additions and 1 deletion.
  1. +72 −0 ApiServer.js
  2. +4 −0 config/default-sample.json5
  3. +3 −1 index.js
  4. +362 −0 package-lock.json
  5. +1 −0 package.json
@@ -0,0 +1,72 @@
var express = require('express');
var bodyParser = require('body-parser');
function ApiServer(opts, libs) {
var db = libs.db;
var app;
this.start = function() {
app = express();
app.use(bodyParser.json({limit: "50mb"}));
app.use(bodyParser.urlencoded({limit: "50mb", extended: true, parameterLimit: 50000}));
var router = express.Router();
async function get_addr(address) {
var utxos = await db.getUnspent(address);
var balance = 0;
for(var i in utxos) {
var utxo = utxos[i];
balance += utxo.value;
}
return {balance: balance};
}
async function get_utxos(address) {
var utxos = await db.getUnspent(address);
return utxos;
}
// GET - /addr/{addr}
router.get('/addr/:addr', async function(req, res) {
res.json({err: 0, res: await get_addr(req.params.addr)});
});
// POST - {addrs: [addr1, addr2, ..., addrN]}
router.post('/addrs', async function(req, res) {
var addrs = req.body.addrs;
console.log(addrs);
var balances = [];
for(var i in addrs) {
var addr = addrs[i];
balances.push(await get_addr(addr));
}
res.json({err: 0, res: balances});
});
// GET - /utxo/{addr}
router.get('/utxo/:addr', async function(req, res) {
res.json({err: 0, res: await get_utxos(req.params.addr)});
});
// POST - {addrs: [addr1, addr2, ..., addrN]}
router.post('/utxos', async function(req, res) {
var addrs = req.body.addrs;
var utxos = [];
for(var i in addrs) {
var addr = addrs[i];
utxos.push(await get_utxos(addr));
}
res.json({err: 0, res: utxos});
});
app.use('/api', router);
app.use(function(err, req, res, next) {
res.send({err:1, res: err.message});
});
app.listen(opts.server.http_port);
}
}
module.exports = ApiServer;
@@ -4,5 +4,9 @@
port: 9252,
user: 'username',
pass: 'password'
},
server: {
http_port: 8000,
ws_port: 8001
}
}
@@ -2,6 +2,7 @@ var opts = require('config');
var Rpc = require('./Rpc');
var bitcoin = require('bitcoinjs-lib');
var Db = require('./Db');
var ApiServer = require('./ApiServer');
var rpc = new Rpc(opts);
rpc.cb = function(cmd, err, res) {
if(err) {
@@ -10,6 +11,7 @@ rpc.cb = function(cmd, err, res) {
return res;
}
var db = new Db(opts);
var apiserver = new ApiServer(opts, {db: db});
bitcoin.networks['bitzeny'] = {
messagePrefix: '\u0018Bitzeny Signed Message:\n',
@@ -126,5 +128,5 @@ async function txs_parser(block, txs_cb, txins_cb, txouts_cb) {
}
}
//console.log(await db.getUnspent('some address here!'));
apiserver.start();
})();
Oops, something went wrong.

0 comments on commit d2ce781

Please sign in to comment.