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 @@ -294,4 +294,11 @@ export class Config {
294294 if ( option !== undefined ) return option
295295 return this . chainCommon . chainName ( ) === 'mainnet'
296296 }
297+
298+ getNetworkDir ( ) : string {
299+ const networkDirName = this . common . chainName ( )
300+ const dataDir = `${ this . datadir } /${ networkDirName } `
301+
302+ return dataDir
303+ }
297304}
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import { randomBytes } from 'crypto'
22import { RLPx as Devp2pRLPx , Peer as Devp2pRLPxPeer , DPT as Devp2pDPT } from '@ethereumjs/devp2p'
33import { RlpxPeer } from '../peer/rlpxpeer'
44import { Server , ServerOptions } from './server'
5+ import fs from 'fs'
56
67export interface RlpxServerOptions extends ServerOptions {
78 /* Local port to listen on (default: 30303) */
@@ -64,6 +65,20 @@ export class RlpxServer extends Server {
6465 constructor ( options : RlpxServerOptions ) {
6566 super ( options )
6667
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+
6782 // TODO: get the external ip from the upnp service
6883 this . ip = '::'
6984 this . port = options . port ?? 30303
You can’t perform that action at this time.
0 commit comments