shhwallet
Version:
293 lines (237 loc) • 7.69 kB
JavaScript
/*
* btcroutes.js
* Package: shhwallet
*
* Description : Bitcoind Server Routes..
* Author: Arun Panneerselvam
* email: arun@gsunitedtechnologies.com
* website: aeroarunn.com
*/
;
const config = require( "./config.js" );
var Block = require('./block.js');
var express = require('express');
var User = require('../model/user.js'); // get user model
var rlog = require('../model/requests.js'); //get req logger model
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
var md5 = require('md5')
var http = require('http');
var request = require('request');
var fs = require('fs');
var util = require('util');
var Promise = require('promise');
const Client = require('bitcoin-core');
// Bitcoind client
const client = new Client(config.bitcoind_bitseed);
const block = new Block();
//Route
var btcRoutes = express.Router();
/*
*
* Verify authentication token
*/ /*
btcRoutes.use(function(req, res, next) {
var token = req.body.token || req.query.token || req.headers['x-access-token'];
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
// decode token
if (token) {
// verifies secret and checks exp
jwt.verify(token, config.api['secret'], function (err, decoded) {
if (err) {
return res.json({success: false, message: 'Token authentication failed.'});
} else {
// confirm authentication to routes
req.decoded = decoded;
next();
}
});
} else {
// if there is no token
// return an error
return res.status(403).send({
success: false,
message: 'Token exchange failed.'
});
}
});
*/
/*
* API Routes for bitcoind server
* Welcome message (GET http://localhost:port/bitcoind/)
* Method : GET
*/
btcRoutes.get('/', function(req, res) {
//res.send({ message: 'SHH Networks - Bitcoin Server' });
client.command('getinfo').then((info) => res.json(info));
});
/*
* return information about the bitcoin server
* Method : GET
*/
btcRoutes.get('/info', function(req, res) {
res.format({
'text/plain': function () {
client.command('getinfo').then((info) => res.json(JSON.stringify(info)));
},
});
});
/*
* return information about the bitcoin server
* Method : GET
*/
btcRoutes.get('/bbh', function(req, res) {
res.format({
'text/plain': function () {
client.command('getbestblockhash').then((bbhash) => res.send(bbhash));
},
});
});
/*
* return information about the bitcoin server
* Method : GET
*/
btcRoutes.get('/block', function(req, res) {
var blockhash = req.body.blockhash || req.query.blockhash;
console.log('Blockhash : ' + blockhash);
const batch = [
{ method: 'getblockcount', parameters: [] },
{ method: 'getblock', parameters: [blockhash] },
/*{ method: '', parameters: [] },
{ method: '', parameters: [] },
{ method: '', parameters: [] },
{ method: '', parameters: [] },
{ method: '', parameters: [] },*/
];
res.format({
'text/plain': function () {
client.command(batch).then(([count,blk]) => (res.json({ count : count, block : blk })));
},
});
});
/*
* const batch = [{ method: 'getnewaddress' }, { method: 'validateaddress', parameters: ['mkteeBFmGkraJaWN5WzqHCjmbQWVrPo5X3'] }];
*/
/*
* return information about the bitcoin accounts
* Method : GET
*/
btcRoutes.get('/accounts', function(req, res) {
var accounts = {};
const batch = [
{ method: 'listaccounts', parameters: [] },
];
res.format({
'text/plain': function () {
//client.command(batch).then(([count,blk]) => (res.json(count, block.blockFromArray(block))));
client.command(batch).then((accounts) => (res.json(accounts)));
},
});
});
/*
* Get Account address, create new one if requested
* Method : GET
*/
btcRoutes.get('/accountaddress', function(req, res) {
var isNewAddress = req.body.isNewAddress || req.query.isNewAddress;
var account_name = req.body.account_name || req.query.account_name;
console.log(isNewAddress);
console.log(account_name);
const batch = [
{ method: 'getnewaddress', parameters: [account_name] },
{ method: 'getaccountaddress', parameters: [account_name] },
];
if(isNewAddress) {
res.format({
'text/plain': function () {
client.command(batch).then(([new_address, account_address]) => res.json({ newaddress : new_address, accaddress: account_address}) );
},
});
} else {
res.format({
'text/plain': function () {
client.command('getaccountaddress').then((address) => res.json(address) );
},
});
}
});
/*
* Valid Address for transaction and process
* Method : GET
*/
btcRoutes.get('/vaddr', function(req, res) {
var address = req.body.address || req.query.address;
var btc = req.body.btc || req.query.btc;
console.log(address);
console.log(btc);
const batch = [
{ method: 'getnewaddress', parameters: [account_name] },
{ method: 'getaccountaddress', parameters: [account_name] },
];
if(isNewAddress) {
res.format({
'text/plain': function () {
client.command(batch).then(([new_address, account_address]) => res.json({ newaddress : new_address, accaddress: account_address}) );
},
});
} else {
res.format({
'text/plain': function () {
client.command('getaccountaddress').then((address) => res.json(address) );
},
});
}
});
/*
* List transactions for account
* Method : GET
*/
btcRoutes.get('/lsttx', function(req, res) {
var name = req.body.account_name || req.query.account_name;
var noOf = req.body.noOf || req.query.noOf;
var from = req.body.from || req.query.from;
const batch = [
{ method: 'listtransactions', parameters: [name, 10, from] },
];
res.format({
'text/plain': function () {
client.command(batch).then((accounts) => (res.json(accounts)));
},
});
});
/* List receivedby address -
* Returns an array of objects containing:
* "account" : the account of the receiving addresses
* "amount" : total amount received by addresses with this account
* "confirmations" : number of confirmations of the most recent transaction inc
* Usage: bitcoin-cli listreceivedbyaddress 0 true
*/
/*
* List transactions for account
* Method : GET
*/
btcRoutes.get('/lstrbyaddr', function(req, res) {
var minConf = req.body.minconfs || req.query.minconfs;
var empty = req.body.empty || req.query.empty; //default false
var from = req.body.from || req.query.from;
const batch = [
{ method: 'listreceivedbyaddress', parameters: [name, 10, from] },
];
res.format({
'text/plain': function () {
client.command(batch).then((accounts) => (res.json(accounts)));
},
});
});
/*
* return all wallet balances
* Method : GET
*/
btcRoutes.get('/balance', function(req, res) {
res.format({
'text/plain': function () {
client.command('getbalance').then((balance) => res.json({balance : balance.toString()}));
},
});
});
// Export the Module
module.exports = btcRoutes;