-
Notifications
You must be signed in to change notification settings - Fork 265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(p2p): persist node private p2p keys #10324
Changes from 1 commit
b58cd99
f267d97
dcf6489
babb968
ac694dc
cb13ced
61d46b3
5ce7bcd
1222100
adff1ad
07353c1
c16626a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,4 +102,4 @@ | |
"engines": { | ||
"node": ">=18" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,4 +99,4 @@ | |
] | ||
] | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ import { resolve } from 'dns/promises'; | |
import type { Libp2p } from 'libp2p'; | ||
|
||
import { type P2PConfig } from './config.js'; | ||
import { createFromJSON, createSecp256k1PeerId } from '@libp2p/peer-id-factory'; | ||
import { type AztecKVStore, type AztecSingleton } from '@aztec/kv-store'; | ||
import { type PeerId } from '@libp2p/interface'; | ||
|
||
export interface PubSubLibp2p extends Libp2p { | ||
services: { | ||
|
@@ -141,3 +144,44 @@ export async function configureP2PClientAddresses( | |
|
||
return config; | ||
} | ||
|
||
/** | ||
* Get the peer id private key | ||
* | ||
* 1. Check we have a peer id private key persisted in the node | ||
* 2. If not, check if we have a peer id private key in the config | ||
* 3. If not, create a new one, then persist it in the node | ||
* | ||
*/ | ||
export async function getPeerIdPrivateKey(config: P2PConfig, store: AztecKVStore): Promise<string> { | ||
const peerIdPrivateKeySingleton: AztecSingleton<string> = store.openSingleton('peerIdPrivateKey'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we prioritise a configured value over a stored one? Currently, once a key is stored, it's impossible to change that key without wiping the data directory. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, i was thinking this today, will update |
||
const storedPeerIdPrivateKey = peerIdPrivateKeySingleton.get(); | ||
if (storedPeerIdPrivateKey) { | ||
return storedPeerIdPrivateKey; | ||
} | ||
|
||
if (config.peerIdPrivateKey) { | ||
await peerIdPrivateKeySingleton.set(config.peerIdPrivateKey); | ||
return config.peerIdPrivateKey; | ||
} | ||
|
||
const newPeerIdPrivateKey = (await createSecp256k1PeerId()).privateKey!.toString(); | ||
await peerIdPrivateKeySingleton.set(newPeerIdPrivateKey); | ||
return newPeerIdPrivateKey; | ||
} | ||
|
||
/** | ||
* Create a libp2p peer ID from the private key. | ||
* @param privateKey - peer ID private key as hex string | ||
* @returns The peer ID. | ||
*/ | ||
export async function createLibP2PPeerIdFromPrivateKey(privateKey?: string): Promise<PeerId> { | ||
if (!privateKey?.length) { | ||
throw new Error('No peer private key provided'); | ||
} | ||
const base64 = Buffer.from(privateKey, 'hex').toString('base64'); | ||
return await createFromJSON({ | ||
id: '', | ||
privKey: base64, | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rediscovery test is not longer provided the same private keys, it is expected to store it on its own