Skip to content

Commit 8ca92ba

Browse files
client: save private rlpx key to disk
1 parent ea2a09e commit 8ca92ba

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

packages/client/lib/config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,11 @@ export class Config {
205205
const dataDir = `${this.datadir}/${networkDirName}/state`
206206
return dataDir
207207
}
208+
209+
getNetworkDir(): string {
210+
const networkDirName = this.common.chainName()
211+
const dataDir = `${this.datadir}/${networkDirName}`
212+
213+
return dataDir
214+
}
208215
}

packages/client/lib/net/server/rlpxserver.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { randomBytes } from 'crypto'
22
import { RLPx as Devp2pRLPx, Peer as Devp2pRLPxPeer, DPT as Devp2pDPT } from '@ethereumjs/devp2p'
33
import { RlpxPeer } from '../peer/rlpxpeer'
44
import { Server, ServerOptions } from './server'
5+
import fs from 'fs'
56

67
export interface RlpxServerOptions extends ServerOptions {
78
/* Local port to listen on (default: 30303) */
@@ -51,6 +52,20 @@ export class RlpxServer extends Server {
5152
constructor(options: RlpxServerOptions) {
5253
super(options)
5354

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+
5469
// TODO: get the external ip from the upnp service
5570
this.ip = '::'
5671
this.port = options.port ?? 30303

0 commit comments

Comments
 (0)