diff --git a/bin/libbfa.js b/bin/libbfa.js index c4b99374fc2f010f1be5da3740e2dfc8e771c2fa..30e559e268b1a689021781603441d67a69d27f23 100644 --- a/bin/libbfa.js +++ b/bin/libbfa.js @@ -3,13 +3,13 @@ "use strict" var request = require('request'); +var net = require('net'); module.exports = class Libbfa { constructor() { this.fs = require('fs'); this.Web3 = require('web3'); - var net = require('net'); // // BFAHOME if ( undefined == process.env.BFAHOME ) @@ -38,7 +38,7 @@ module.exports = class Libbfa files.push( filename ); }); } - // found none? + // found any? if ( files.length > 0 ) { files.sort(); @@ -54,10 +54,13 @@ module.exports = class Libbfa this.socketurl = this.nodedir+'/geth.ipc'; // overwrite with newer ipc method if ( this.sockettype == 'ipc' ) { this.provider = new this.Web3.providers.IpcProvider( this.nodedir+'/geth.ipc', net ); + this.req_url = 'http://unix:' + this.nodedir + '/geth.ipc:/'; } else if ( this.sockettype == 'ws' ) { this.provider = new this.Web3.providers.WebsocketProvider( this.socketurl ); + this.req_url = this.socketurl; } else if ( this.sockettype == 'http') { this.provider = new this.Web3.providers.HttpProvider( this.socketurl ); + this.req_url = this.socketurl; } else { fatal("Unknown sockettype."); } @@ -92,18 +95,25 @@ module.exports = class Libbfa newweb3() { var w3 = new this.Web3( this.provider ); - w3.rpcreq = function( opname, params, callback ) + var req_url = this.req_url; + var _bfa = this; + w3.jsonify = function( opname, params ) { var extra = params.join(','); - var body = JSON.parse("{"+ - '"jsonrpc":"2.0",' + - '"id":1,' + + return JSON.parse( + '{'+ + '"jsonrpc":"2.0",' + + '"id":1,' + '"method":"' + opname + '",' + - '"params":[' + extra + ']' - +"}" + '"params":[' + extra + ']' + + '}' ); + }; + w3.rpcreq = function( opname, params, callback ) + { + var body = w3.jsonify( opname, params ); request.post({ - uri: 'http://localhost:8545', + uri: req_url, json: true, body: body, callback: function RPCresponse( err, obj ) @@ -121,26 +131,70 @@ module.exports = class Libbfa } }); }; + w3.req = function( opname, params, callback ) + { + if ( _bfa.sockettype == 'ipc' ) + { + w3.ipcreq( opname, params, callback ); + } + else + { + w3.rpcreq( opname, params, callback ); + } + } + w3.ipcreq = function( opname, params, callback ) + { + var socket = net.connect( _bfa.socketurl ); + var result; + var err; + socket.on("ready", () => { + // When the socket has been established. + // We create a new connection per request, because it + // is easier than reliably handling JSON object boundaries + // in a TCP stream . + // Writes out data and closes our end of the connection. + // Geth will reply and then close it's end. + s.end( w3.jsonify(opname,params) ); + }); + socket.on("data", (d) => { + try { + result = JSON.parse( d.toString() ); + } + catch { + err = d.toString(); + } + }); + socket.on("timeout", () => { + socket.close(); + }); + socket.on("error", (e) => { + console.error(e); + err = e; + }); + socket.on("close", () => { + callback( err, result ); + }); + } w3.bfa = { clique: { getSigners: function clique_getSigners( cb ) - { w3.rpcreq( 'clique_getSigners', [], cb ) }, + { w3.req( 'clique_getSigners', [], cb ) }, }, miner: { start: function miner_start() - { w3.rpcreq( 'miner_start', [], function(){} ) }, + { w3.req( 'miner_start', [], function(){} ) }, stop: function miner_stop() - { w3.rpcreq( 'miner_stop', [], function(){} ) } + { w3.req( 'miner_stop', [], function(){} ) } }, admin: { peers: function admin_peers( cb ) - { w3.rpcreq( 'admin_peers', [], cb ) }, + { w3.req( 'admin_peers', [], cb ) }, addPeer: function admin_addPeer( peer ) - { w3.rpcreq( 'admin_addPeer', [ "\""+peer+"\"" ], function(){} ) } + { w3.req( 'admin_addPeer', [ "\""+peer+"\"" ], function(){} ) } }, personal: { listWallets: function personal_listWallets( cb ) - { w3.rpcreq( 'personal_listWallets', [], cb ) } + { w3.req( 'personal_listWallets', [], cb ) } } }; if ( undefined != process.env.BFAACCOUNT ) {