2018-05-17 19:32:57 +00:00
const async = require ( 'async' ) ;
2016-09-25 06:23:33 +00:00
2017-02-22 02:01:38 +00:00
// TODO: make all of this async
2017-03-30 11:12:39 +00:00
class GethCommands {
constructor ( options ) {
2017-03-30 13:16:46 +00:00
this . config = options && options . hasOwnProperty ( 'config' ) ? options . config : { } ;
this . env = options && options . hasOwnProperty ( 'env' ) ? options . env : 'development' ;
2018-05-09 13:17:48 +00:00
this . isDev = options && options . hasOwnProperty ( 'isDev' ) ? options . isDev : ( this . env === 'development' ) ;
2017-03-30 11:12:39 +00:00
this . name = "Go-Ethereum (https://github.com/ethereum/go-ethereum)" ;
this . geth _bin = this . config . geth _bin || "geth" ;
}
2016-09-25 06:23:33 +00:00
2017-03-30 11:12:39 +00:00
commonOptions ( ) {
let config = this . config ;
2018-05-28 15:54:31 +00:00
let cmd = [ ] ;
2016-09-25 06:23:33 +00:00
2018-05-28 15:54:31 +00:00
cmd . push ( this . determineNetworkType ( config ) ) ;
2016-09-25 06:23:33 +00:00
2017-03-30 11:12:39 +00:00
if ( config . datadir ) {
2018-05-28 15:55:16 +00:00
cmd . push ( ` --datadir= ${ config . datadir } ` ) ;
2017-03-30 11:12:39 +00:00
}
2016-09-25 06:23:33 +00:00
2018-07-06 13:31:28 +00:00
if ( config . syncMode || config . syncmode ) {
2018-07-10 19:59:42 +00:00
cmd . push ( "--syncmode=" + ( config . syncmode || config . syncMode ) ) ;
2017-03-30 11:12:39 +00:00
}
2017-02-20 20:53:55 +00:00
2017-03-30 11:12:39 +00:00
if ( config . account && config . account . password ) {
2018-05-28 15:55:16 +00:00
cmd . push ( ` --password= ${ config . account . password } ` ) ;
2017-03-30 11:12:39 +00:00
}
2016-09-25 06:23:33 +00:00
2018-05-22 13:11:38 +00:00
if ( Number . isInteger ( config . verbosity ) && config . verbosity >= 0 && config . verbosity <= 5 ) {
2018-05-28 15:54:31 +00:00
cmd . push ( "--verbosity=" + config . verbosity ) ;
2018-05-22 13:11:38 +00:00
}
2017-03-30 11:12:39 +00:00
return cmd ;
2017-02-22 02:01:38 +00:00
}
2016-09-25 06:23:33 +00:00
2018-05-28 15:55:16 +00:00
determineVersionCommand ( ) {
2017-12-13 22:58:07 +00:00
return this . geth _bin + " version" ;
}
2017-03-30 11:12:39 +00:00
determineNetworkType ( config ) {
2018-05-28 15:54:31 +00:00
let cmd ;
2017-03-30 11:12:39 +00:00
if ( config . networkType === 'testnet' ) {
2018-06-14 19:22:01 +00:00
cmd = "--testnet" ;
2018-06-15 18:18:37 +00:00
} else if ( config . networkType === 'rinkeby' ) {
cmd = "--rinkeby" ;
2017-03-30 11:12:39 +00:00
} else if ( config . networkType === 'custom' ) {
2018-05-28 15:54:31 +00:00
cmd = "--networkid=" + config . networkId ;
2017-03-30 11:12:39 +00:00
}
return cmd ;
2016-09-25 06:23:33 +00:00
}
2017-03-30 11:12:39 +00:00
initGenesisCommmand ( ) {
let config = this . config ;
2018-05-28 15:54:31 +00:00
let cmd = this . geth _bin + " " + this . commonOptions ( ) . join ( ' ' ) ;
2017-03-30 11:12:39 +00:00
if ( config . genesisBlock ) {
2018-05-28 15:55:16 +00:00
cmd += " init \"" + config . genesisBlock + "\" " ;
2017-03-30 11:12:39 +00:00
}
2016-10-31 00:08:45 +00:00
2017-03-30 11:12:39 +00:00
return cmd ;
}
2016-10-31 00:21:28 +00:00
2017-03-30 11:12:39 +00:00
newAccountCommand ( ) {
2018-09-18 06:28:23 +00:00
if ( ! ( this . config . account && this . config . account . password ) ) {
console . warn ( _ _ ( 'Your blockchain is missing a password and creating an account may fail. Please consider updating ' ) . yellow + _ _ ( 'config/blockchain > account > password' ) . cyan + _ _ ( ' then re-run the command' ) . yellow ) ;
}
2018-05-28 15:55:16 +00:00
return this . geth _bin + " " + this . commonOptions ( ) . join ( ' ' ) + " account new " ;
2017-03-30 11:12:39 +00:00
}
2016-09-25 06:23:33 +00:00
2017-03-30 11:12:39 +00:00
listAccountsCommand ( ) {
2018-05-28 15:55:16 +00:00
return this . geth _bin + " " + this . commonOptions ( ) . join ( ' ' ) + " account list " ;
2017-03-30 11:12:39 +00:00
}
2016-09-25 06:23:33 +00:00
2017-03-30 11:12:39 +00:00
determineRpcOptions ( config ) {
2018-05-28 15:54:31 +00:00
let cmd = [ ] ;
2017-03-30 11:12:39 +00:00
2018-05-28 15:54:31 +00:00
cmd . push ( "--port=" + config . port ) ;
cmd . push ( "--rpc" ) ;
cmd . push ( "--rpcport=" + config . rpcPort ) ;
cmd . push ( "--rpcaddr=" + config . rpcHost ) ;
2017-03-30 11:12:39 +00:00
if ( config . rpcCorsDomain ) {
if ( config . rpcCorsDomain === '*' ) {
console . log ( '==================================' ) ;
2018-05-08 21:49:46 +00:00
console . log ( _ _ ( 'rpcCorsDomain set to *' ) ) ;
console . log ( _ _ ( 'make sure you know what you are doing' ) ) ;
2017-03-30 11:12:39 +00:00
console . log ( '==================================' ) ;
}
2018-05-28 14:16:07 +00:00
cmd . push ( "--rpccorsdomain=" + config . rpcCorsDomain ) ;
2017-03-30 11:12:39 +00:00
} else {
2016-10-07 11:15:29 +00:00
console . log ( '==================================' ) ;
2018-05-08 21:49:46 +00:00
console . log ( _ _ ( 'warning: cors is not set' ) ) ;
2016-10-07 11:15:29 +00:00
console . log ( '==================================' ) ;
}
2017-03-30 11:12:39 +00:00
return cmd ;
2016-09-26 00:51:00 +00:00
}
2016-09-25 06:23:33 +00:00
2017-10-19 22:55:49 +00:00
determineWsOptions ( config ) {
2018-05-28 15:54:31 +00:00
let cmd = [ ] ;
2017-10-19 22:55:49 +00:00
2018-01-11 13:55:28 +00:00
if ( config . wsRPC ) {
2018-05-28 15:54:31 +00:00
cmd . push ( "--ws" ) ;
cmd . push ( "--wsport=" + config . wsPort ) ;
cmd . push ( "--wsaddr=" + config . wsHost ) ;
2018-01-11 13:55:28 +00:00
if ( config . wsOrigins ) {
if ( config . wsOrigins === '*' ) {
console . log ( '==================================' ) ;
2018-05-08 21:49:46 +00:00
console . log ( _ _ ( 'wsOrigins set to *' ) ) ;
console . log ( _ _ ( 'make sure you know what you are doing' ) ) ;
2018-01-11 13:55:28 +00:00
console . log ( '==================================' ) ;
}
2018-05-28 14:16:07 +00:00
cmd . push ( "--wsorigins=" + config . wsOrigins ) ;
2018-01-11 13:55:28 +00:00
} else {
2017-10-19 22:55:49 +00:00
console . log ( '==================================' ) ;
2018-05-08 21:49:46 +00:00
console . log ( _ _ ( 'warning: wsOrigins is not set' ) ) ;
2017-10-19 22:55:49 +00:00
console . log ( '==================================' ) ;
}
}
return cmd ;
}
2017-03-30 11:12:39 +00:00
mainCommand ( address , done ) {
let self = this ;
let config = this . config ;
2018-06-12 16:21:46 +00:00
let rpc _api = ( this . config . rpcApi || [ 'eth' , 'web3' , 'net' , 'debug' ] ) ;
2018-07-19 07:58:47 +00:00
let defaultWsApi = [ 'eth' , 'web3' , 'net' , 'shh' , 'debug' ] ;
if ( this . isDev ) {
defaultWsApi . push ( 'personal' ) ;
}
let ws _api = ( this . config . wsApi || defaultWsApi ) ;
2017-03-30 11:12:39 +00:00
2018-05-28 15:54:31 +00:00
let args = [ ] ;
2017-03-30 11:12:39 +00:00
async . series ( [
function commonOptions ( callback ) {
let cmd = self . commonOptions ( ) ;
2018-05-28 15:54:31 +00:00
args = args . concat ( cmd ) ;
2017-03-30 11:12:39 +00:00
callback ( null , cmd ) ;
} ,
function rpcOptions ( callback ) {
let cmd = self . determineRpcOptions ( self . config ) ;
2018-05-28 15:54:31 +00:00
args = args . concat ( cmd ) ;
2017-03-30 11:12:39 +00:00
callback ( null , cmd ) ;
} ,
2017-10-19 22:55:49 +00:00
function wsOptions ( callback ) {
let cmd = self . determineWsOptions ( self . config ) ;
2018-05-28 15:54:31 +00:00
args = args . concat ( cmd ) ;
2017-10-19 22:55:49 +00:00
callback ( null , cmd ) ;
} ,
2017-03-30 11:12:39 +00:00
function dontGetPeers ( callback ) {
if ( config . nodiscover ) {
2018-05-28 15:54:31 +00:00
args . push ( "--nodiscover" ) ;
2017-03-30 11:12:39 +00:00
return callback ( null , "--nodiscover" ) ;
}
callback ( null , "" ) ;
} ,
function vmDebug ( callback ) {
if ( config . vmdebug ) {
2018-05-28 15:54:31 +00:00
args . push ( "--vmdebug" ) ;
2017-03-30 11:12:39 +00:00
return callback ( null , "--vmdebug" ) ;
}
callback ( null , "" ) ;
} ,
function maxPeers ( callback ) {
2018-05-28 15:54:31 +00:00
let cmd = "--maxpeers=" + config . maxpeers ;
args . push ( cmd ) ;
2017-03-30 11:12:39 +00:00
callback ( null , cmd ) ;
} ,
function mining ( callback ) {
if ( config . mineWhenNeeded || config . mine ) {
2018-05-28 15:54:31 +00:00
args . push ( "--mine" ) ;
return callback ( null , "--mine" ) ;
2017-03-30 11:12:39 +00:00
}
callback ( "" ) ;
} ,
function bootnodes ( callback ) {
if ( config . bootnodes && config . bootnodes !== "" && config . bootnodes !== [ ] ) {
2018-05-28 15:54:31 +00:00
args . push ( "--bootnodes=" + config . bootnodes ) ;
return callback ( null , "--bootnodes=" + config . bootnodes ) ;
2017-03-30 11:12:39 +00:00
}
callback ( "" ) ;
} ,
function whisper ( callback ) {
if ( config . whisper ) {
rpc _api . push ( 'shh' ) ;
2017-10-21 12:42:29 +00:00
if ( ws _api . indexOf ( 'shh' ) === - 1 ) {
ws _api . push ( 'shh' ) ;
}
2018-05-28 15:54:31 +00:00
args . push ( "--shh" ) ;
2017-03-30 11:12:39 +00:00
return callback ( null , "--shh " ) ;
}
callback ( "" ) ;
} ,
function rpcApi ( callback ) {
2018-05-28 14:16:07 +00:00
args . push ( '--rpcapi=' + rpc _api . join ( ',' ) ) ;
callback ( null , '--rpcapi=' + rpc _api . join ( ',' ) ) ;
2017-03-30 11:12:39 +00:00
} ,
2017-10-19 22:55:49 +00:00
function wsApi ( callback ) {
2018-05-28 14:16:07 +00:00
args . push ( '--wsapi=' + ws _api . join ( ',' ) ) ;
callback ( null , '--wsapi=' + ws _api . join ( ',' ) ) ;
2017-10-19 22:55:49 +00:00
} ,
2017-03-30 11:12:39 +00:00
function accountToUnlock ( callback ) {
2017-03-30 13:16:46 +00:00
let accountAddress = "" ;
2018-07-03 18:55:04 +00:00
if ( config . account && config . account . address ) {
2017-03-30 13:16:46 +00:00
accountAddress = config . account . address ;
} else {
accountAddress = address ;
}
2018-05-09 13:17:48 +00:00
if ( accountAddress && ! self . isDev ) {
2018-05-28 15:54:31 +00:00
args . push ( "--unlock=" + accountAddress ) ;
2017-03-30 11:12:39 +00:00
return callback ( null , "--unlock=" + accountAddress ) ;
}
callback ( null , "" ) ;
} ,
2018-01-17 16:23:32 +00:00
function gasLimit ( callback ) {
if ( config . targetGasLimit ) {
2018-05-28 15:54:31 +00:00
args . push ( "--targetgaslimit=" + config . targetGasLimit ) ;
return callback ( null , "--targetgaslimit=" + config . targetGasLimit ) ;
2018-01-17 16:23:32 +00:00
}
callback ( null , "" ) ;
} ,
2018-05-08 20:25:48 +00:00
function isDev ( callback ) {
2018-05-09 13:17:48 +00:00
if ( self . isDev ) {
2018-05-28 15:54:31 +00:00
args . push ( '--dev' ) ;
2018-05-08 20:25:48 +00:00
return callback ( null , '--dev' ) ;
}
callback ( null , '' ) ;
2017-03-02 13:15:35 +00:00
}
2018-05-28 15:54:31 +00:00
] , function ( err ) {
2017-03-30 11:12:39 +00:00
if ( err ) {
throw new Error ( err . message ) ;
2017-03-02 13:15:35 +00:00
}
2018-05-28 15:54:31 +00:00
return done ( self . geth _bin , args ) ;
2017-03-30 11:12:39 +00:00
} ) ;
}
}
2016-09-25 06:23:33 +00:00
module . exports = GethCommands ;