@@ -14,6 +14,7 @@ var schema = require('../schema/delegates.js');
1414var slots = require ( '../helpers/slots.js' ) ;
1515var sql = require ( '../sql/delegates.js' ) ;
1616var transactionTypes = require ( '../helpers/transactionTypes.js' ) ;
17+ var crypto = require ( 'crypto' )
1718
1819// Private fields
1920var modules , library , self , __private = { } , shared = { } ;
@@ -62,7 +63,7 @@ __private.attachApi = function () {
6263 'get /fee' : 'getFee' ,
6364 'get /forging/getForgedByAccount' : 'getForgedByAccount' ,
6465 'put /' : 'addDelegate' ,
65- 'get /getNextForgers' : 'getNextForgers'
66+ 'get /getNextForgers' : 'getNextForgers'
6667 } ) ;
6768
6869 if ( process . env . DEBUG ) {
@@ -87,85 +88,85 @@ __private.attachApi = function () {
8788 tmpKepairs = { } ;
8889 return res . json ( { success : true } ) ;
8990 } ) ;
90- }
91-
92- router . post ( '/forging/enable' , function ( req , res ) {
93- library . schema . validate ( req . body , schema . enableForging , function ( err ) {
94- if ( err ) {
95- return res . json ( { success : false , error : err [ 0 ] . message } ) ;
96- }
97-
98- var ip = req . headers [ 'x-forwarded-for' ] || req . connection . remoteAddress ;
9991
100- if ( ! checkIpInList ( library . config . forging . access . whiteList , ip ) ) {
101- return res . json ( { success : false , error : 'Access denied' } ) ;
102- }
92+ router . post ( '/forging/enable' , function ( req , res ) {
93+ library . schema . validate ( req . body , schema . enableForging , function ( err ) {
94+ if ( err ) {
95+ return res . json ( { success : false , error : err [ 0 ] . message } ) ;
96+ }
10397
104- var keypair = library . crypto . makeKeypair ( crypto . createHash ( 'sha256' ) . update ( req . body . secret , 'utf8' ) . digest ( ) ) ;
98+ var ip = req . headers [ 'x-forwarded-for' ] || req . connection . remoteAddress ;
10599
106- if ( req . body . publicKey ) {
107- if ( keypair . publicKey . toString ( 'hex' ) !== req . body . publicKey ) {
108- return res . json ( { success : false , error : 'Invalid passphrase' } ) ;
100+ if ( ! checkIpInList ( library . config . forging . access . whiteList , ip ) ) {
101+ return res . json ( { success : false , error : 'Access denied' } ) ;
109102 }
110- }
111103
112- if ( __private . keypairs [ keypair . publicKey . toString ( 'hex' ) ] ) {
113- return res . json ( { success : false , error : 'Forging is already enabled' } ) ;
114- }
104+ var keypair = library . crypto . makeKeypair ( crypto . createHash ( 'sha256' ) . update ( req . body . secret , 'utf8' ) . digest ( ) ) ;
115105
116- modules . accounts . getAccount ( { publicKey : keypair . publicKey . toString ( 'hex' ) } , function ( err , account ) {
117- if ( err ) {
118- return res . json ( { success : false , error : err } ) ;
106+ if ( req . body . publicKey ) {
107+ if ( keypair . publicKey . toString ( 'hex' ) !== req . body . publicKey ) {
108+ return res . json ( { success : false , error : 'Invalid passphrase' } ) ;
109+ }
119110 }
120- if ( account && account . isDelegate ) {
121- __private . keypairs [ keypair . publicKey . toString ( 'hex' ) ] = keypair ;
122- library . logger . info ( 'Forging enabled on account: ' + account . address ) ;
123- return res . json ( { success : true , address : account . address } ) ;
124- } else {
125- return res . json ( { success : false , error : 'Delegate not found' } ) ;
111+
112+ if ( __private . keypairs [ keypair . publicKey . toString ( 'hex' ) ] ) {
113+ return res . json ( { success : false , error : 'Forging is already enabled' } ) ;
126114 }
115+
116+ modules . accounts . getAccount ( { publicKey : keypair . publicKey . toString ( 'hex' ) } , function ( err , account ) {
117+ if ( err ) {
118+ return res . json ( { success : false , error : err } ) ;
119+ }
120+ if ( account && account . isDelegate ) {
121+ __private . keypairs [ keypair . publicKey . toString ( 'hex' ) ] = keypair ;
122+ library . logger . info ( 'Forging enabled on account: ' + account . address ) ;
123+ return res . json ( { success : true , address : account . address } ) ;
124+ } else {
125+ return res . json ( { success : false , error : 'Delegate not found' } ) ;
126+ }
127+ } ) ;
127128 } ) ;
128129 } ) ;
129- } ) ;
130130
131- router . post ( '/forging/disable' , function ( req , res ) {
132- library . schema . validate ( req . body , schema . disableForging , function ( err ) {
133- if ( err ) {
134- return res . json ( { success : false , error : err [ 0 ] . message } ) ;
135- }
136-
137- var ip = req . headers [ 'x-forwarded-for' ] || req . connection . remoteAddress ;
138-
139- if ( ! checkIpInList ( library . config . forging . access . whiteList , ip ) ) {
140- return res . json ( { success : false , error : 'Access denied' } ) ;
141- }
131+ router . post ( '/forging/disable' , function ( req , res ) {
132+ library . schema . validate ( req . body , schema . disableForging , function ( err ) {
133+ if ( err ) {
134+ return res . json ( { success : false , error : err [ 0 ] . message } ) ;
135+ }
142136
143- var keypair = library . crypto . makeKeypair ( crypto . createHash ( 'sha256' ) . update ( req . body . secret , 'utf8' ) . digest ( ) ) ;
137+ var ip = req . headers [ 'x-forwarded-for' ] || req . connection . remoteAddress ;
144138
145- if ( req . body . publicKey ) {
146- if ( keypair . publicKey . toString ( 'hex' ) !== req . body . publicKey ) {
147- return res . json ( { success : false , error : 'Invalid passphrase' } ) ;
139+ if ( ! checkIpInList ( library . config . forging . access . whiteList , ip ) ) {
140+ return res . json ( { success : false , error : 'Access denied' } ) ;
148141 }
149- }
150142
151- if ( ! __private . keypairs [ keypair . publicKey . toString ( 'hex' ) ] ) {
152- return res . json ( { success : false , error : 'Delegate not found' } ) ;
153- }
143+ var keypair = library . crypto . makeKeypair ( crypto . createHash ( 'sha256' ) . update ( req . body . secret , 'utf8' ) . digest ( ) ) ;
154144
155- modules . accounts . getAccount ( { publicKey : keypair . publicKey . toString ( 'hex' ) } , function ( err , account ) {
156- if ( err ) {
157- return res . json ( { success : false , error : err } ) ;
145+ if ( req . body . publicKey ) {
146+ if ( keypair . publicKey . toString ( 'hex' ) !== req . body . publicKey ) {
147+ return res . json ( { success : false , error : 'Invalid passphrase' } ) ;
148+ }
158149 }
159- if ( account && account . isDelegate ) {
160- delete __private . keypairs [ keypair . publicKey . toString ( 'hex' ) ] ;
161- library . logger . info ( 'Forging disabled on account: ' + account . address ) ;
162- return res . json ( { success : true , address : account . address } ) ;
163- } else {
150+
151+ if ( ! __private . keypairs [ keypair . publicKey . toString ( 'hex' ) ] ) {
164152 return res . json ( { success : false , error : 'Delegate not found' } ) ;
165153 }
154+
155+ modules . accounts . getAccount ( { publicKey : keypair . publicKey . toString ( 'hex' ) } , function ( err , account ) {
156+ if ( err ) {
157+ return res . json ( { success : false , error : err } ) ;
158+ }
159+ if ( account && account . isDelegate ) {
160+ delete __private . keypairs [ keypair . publicKey . toString ( 'hex' ) ] ;
161+ library . logger . info ( 'Forging disabled on account: ' + account . address ) ;
162+ return res . json ( { success : true , address : account . address } ) ;
163+ } else {
164+ return res . json ( { success : false , error : 'Delegate not found' } ) ;
165+ }
166+ } ) ;
166167 } ) ;
167168 } ) ;
168- } ) ;
169+ }
169170
170171 router . get ( '/forging/status' , function ( req , res ) {
171172 library . schema . validate ( req . query , schema . forgingStatus , function ( err ) {
@@ -514,7 +515,7 @@ Delegates.prototype.getDelegates = function (query, cb) {
514515 var realLimit = Math . min ( offset + limit , count ) ;
515516
516517 var lastBlock = modules . blockchain . getLastBlock ( ) ,
517- totalSupply = __private . blockReward . calcSupply ( lastBlock . height ) ;
518+ totalSupply = __private . blockReward . calcSupply ( lastBlock . height ) ;
518519
519520 for ( var i = 0 ; i < delegates . length ; i ++ ) {
520521 delegates [ i ] . rate = i + 1 ;
@@ -737,7 +738,7 @@ __private.toggleForgingOnReceipt = function () {
737738
738739
739740 // if (lastReceipt.secondsAgo > timeOut) {
740- // return self.disableForging('timeout');
741+ // return self.disableForging('timeout');
741742 // } else {
742743 return self . enableForging ( ) ;
743744 // }
@@ -879,7 +880,7 @@ shared.getDelegates = function (req, cb) {
879880 if ( data . sortMethod === 'ASC' ) {
880881 return sorta - sortb ;
881882 } else {
882- return sortb - sorta ;
883+ return sortb - sorta ;
883884 }
884885 }
885886
0 commit comments