File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -282,4 +282,11 @@ export class Config {
282
282
if ( option !== undefined ) return option
283
283
return this . chainCommon . chainName ( ) === 'mainnet'
284
284
}
285
+
286
+ getNetworkDir ( ) : string {
287
+ const networkDirName = this . common . chainName ( )
288
+ const dataDir = `${ this . datadir } /${ networkDirName } `
289
+
290
+ return dataDir
291
+ }
285
292
}
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import { randomBytes } from 'crypto'
2
2
import { RLPx as Devp2pRLPx , Peer as Devp2pRLPxPeer , DPT as Devp2pDPT } from '@ethereumjs/devp2p'
3
3
import { RlpxPeer } from '../peer/rlpxpeer'
4
4
import { Server , ServerOptions } from './server'
5
+ import fs from 'fs'
5
6
6
7
export interface RlpxServerOptions extends ServerOptions {
7
8
/* Local port to listen on (default: 30303) */
@@ -64,6 +65,20 @@ export class RlpxServer extends Server {
64
65
constructor ( options : RlpxServerOptions ) {
65
66
super ( options )
66
67
68
+ if ( this . key === undefined ) {
69
+ const dataDir = this . config . getNetworkDir ( )
70
+ const fileName = dataDir + '/nodekey'
71
+ if ( fs . existsSync ( fileName ) ) {
72
+ this . key = Buffer . from ( fs . readFileSync ( fileName , { encoding : 'binary' } ) , 'binary' )
73
+ } else {
74
+ const key = randomBytes ( 32 )
75
+ this . key = key
76
+ fs . writeFileSync ( fileName , key . toString ( 'binary' ) , {
77
+ encoding : 'binary' ,
78
+ } )
79
+ }
80
+ }
81
+
67
82
// TODO: get the external ip from the upnp service
68
83
this . ip = '::'
69
84
this . port = options . port ?? 30303
You can’t perform that action at this time.
0 commit comments