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 @@ -205,4 +205,11 @@ export class Config {
205
205
const dataDir = `${ this . datadir } /${ networkDirName } /state`
206
206
return dataDir
207
207
}
208
+
209
+ getNetworkDir ( ) : string {
210
+ const networkDirName = this . common . chainName ( )
211
+ const dataDir = `${ this . datadir } /${ networkDirName } `
212
+
213
+ return dataDir
214
+ }
208
215
}
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) */
@@ -51,6 +52,20 @@ export class RlpxServer extends Server {
51
52
constructor ( options : RlpxServerOptions ) {
52
53
super ( options )
53
54
55
+ if ( this . key === undefined ) {
56
+ const dataDir = this . config . getNetworkDir ( )
57
+ const fileName = dataDir + '/nodekey'
58
+ if ( fs . existsSync ( fileName ) ) {
59
+ this . key = Buffer . from ( fs . readFileSync ( fileName , { encoding : 'binary' } ) , 'binary' )
60
+ } else {
61
+ const key = randomBytes ( 32 )
62
+ this . key = key
63
+ fs . writeFileSync ( fileName , key . toString ( 'binary' ) , {
64
+ encoding : 'binary' ,
65
+ } )
66
+ }
67
+ }
68
+
54
69
// TODO: get the external ip from the upnp service
55
70
this . ip = '::'
56
71
this . port = options . port ?? 30303
You can’t perform that action at this time.
0 commit comments