Skip to content

Commit 092861e

Browse files
wemeetagainachingbrainmaschad
authoredJan 16, 2024
feat: add private key to libp2p components (#2348)
- Add `privateKey` to `Components` - Non-breaking version of #2303 --------- Co-authored-by: Alex Potsides <alex@achingbrain.net> Co-authored-by: chad <chad.nehemiah94@gmail.com>
1 parent 3c96210 commit 092861e

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed
 

‎packages/libp2p/src/components.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { CodeError } from '@libp2p/interface'
2-
import { isStartable, type Startable, type Libp2pEvents, type ComponentLogger, type NodeInfo, type ConnectionProtector, type ConnectionGater, type ContentRouting, type TypedEventTarget, type Metrics, type PeerId, type PeerRouting, type PeerStore, type Upgrader } from '@libp2p/interface'
2+
import { isStartable, type Startable, type Libp2pEvents, type ComponentLogger, type NodeInfo, type ConnectionProtector, type ConnectionGater, type ContentRouting, type TypedEventTarget, type Metrics, type PeerId, type PeerRouting, type PeerStore, type PrivateKey, type Upgrader } from '@libp2p/interface'
33
import { defaultLogger } from '@libp2p/logger'
44
import type { AddressManager, ConnectionManager, Registrar, TransportManager } from '@libp2p/interface-internal'
55
import type { Datastore } from 'interface-datastore'
66

77
export interface Components extends Record<string, any>, Startable {
88
peerId: PeerId
9+
privateKey: PrivateKey
910
nodeInfo: NodeInfo
1011
logger: ComponentLogger
1112
events: TypedEventTarget<Libp2pEvents>
@@ -25,6 +26,7 @@ export interface Components extends Record<string, any>, Startable {
2526

2627
export interface ComponentsInit {
2728
peerId?: PeerId
29+
privateKey?: PrivateKey
2830
nodeInfo?: NodeInfo
2931
logger?: ComponentLogger
3032
events?: TypedEventTarget<Libp2pEvents>

‎packages/libp2p/src/config.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CodeError, FaultTolerance } from '@libp2p/interface'
2+
import { peerIdFromKeys } from '@libp2p/peer-id'
23
import { defaultAddressSort } from '@libp2p/utils/address-sort'
34
import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
45
import mergeOptions from 'merge-options'
@@ -25,12 +26,16 @@ const DefaultConfig: Partial<Libp2pInit> = {
2526
}
2627
}
2728

28-
export function validateConfig <T extends ServiceMap = Record<string, unknown>> (opts: RecursivePartial<Libp2pInit<T>>): Libp2pInit<T> {
29+
export async function validateConfig <T extends ServiceMap = Record<string, unknown>> (opts: RecursivePartial<Libp2pInit<T>>): Promise<Libp2pInit<T>> {
2930
const resultingOptions: Libp2pInit<T> = mergeOptions(DefaultConfig, opts)
3031

3132
if (resultingOptions.connectionProtector === null && globalThis.process?.env?.LIBP2P_FORCE_PNET != null) { // eslint-disable-line no-undef
3233
throw new CodeError(messages.ERR_PROTECTOR_REQUIRED, codes.ERR_PROTECTOR_REQUIRED)
3334
}
3435

36+
if (!(await peerIdFromKeys(resultingOptions.privateKey.public.bytes, resultingOptions.privateKey.bytes)).equals(resultingOptions.peerId)) {
37+
throw new CodeError('Private key doesn\'t match peer id', codes.ERR_INVALID_KEY)
38+
}
39+
3540
return resultingOptions
3641
}

‎packages/libp2p/src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type { AddressManagerInit } from './address-manager/index.js'
1919
import type { Components } from './components.js'
2020
import type { ConnectionManagerInit } from './connection-manager/index.js'
2121
import type { TransportManagerInit } from './transport-manager.js'
22-
import type { Libp2p, ServiceMap, RecursivePartial, ComponentLogger, NodeInfo, ConnectionProtector, ConnectionEncrypter, ConnectionGater, ContentRouting, Metrics, PeerDiscovery, PeerId, PeerRouting, StreamMuxerFactory, Transport } from '@libp2p/interface'
22+
import type { Libp2p, ServiceMap, RecursivePartial, ComponentLogger, NodeInfo, ConnectionProtector, ConnectionEncrypter, ConnectionGater, ContentRouting, Metrics, PeerDiscovery, PeerId, PeerRouting, StreamMuxerFactory, Transport, PrivateKey } from '@libp2p/interface'
2323
import type { PersistentPeerStoreInit } from '@libp2p/peer-store'
2424
import type { Datastore } from 'interface-datastore'
2525

@@ -36,6 +36,11 @@ export interface Libp2pInit<T extends ServiceMap = { x: Record<string, unknown>
3636
*/
3737
peerId: PeerId
3838

39+
/**
40+
* Private key associated with the peerId
41+
*/
42+
privateKey: PrivateKey
43+
3944
/**
4045
* Metadata about the node - implementation name, version number, etc
4146
*/

‎packages/libp2p/src/libp2p.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { unmarshalPublicKey } from '@libp2p/crypto/keys'
1+
import { unmarshalPrivateKey, unmarshalPublicKey } from '@libp2p/crypto/keys'
22
import { contentRoutingSymbol, CodeError, TypedEventEmitter, CustomEvent, setMaxListeners, peerDiscoverySymbol, peerRoutingSymbol } from '@libp2p/interface'
33
import { defaultLogger } from '@libp2p/logger'
44
import { PeerSet } from '@libp2p/peer-collections'
@@ -67,6 +67,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
6767
this.services = {}
6868
const components = this.components = defaultComponents({
6969
peerId: init.peerId,
70+
privateKey: init.privateKey,
7071
nodeInfo: init.nodeInfo ?? {
7172
name: pkg.name,
7273
version: pkg.version
@@ -397,7 +398,13 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
397398
* libp2p interface and is useful for testing and debugging.
398399
*/
399400
export async function createLibp2pNode <T extends ServiceMap = Record<string, unknown>> (options: Libp2pOptions<T> = {}): Promise<Libp2pNode<T>> {
400-
options.peerId ??= await createEd25519PeerId()
401+
const peerId = options.peerId ??= await createEd25519PeerId()
401402

402-
return new Libp2pNode(validateConfig(options))
403+
if (peerId.privateKey == null) {
404+
throw new CodeError('peer id was missing private key', 'ERR_MISSING_PRIVATE_KEY')
405+
}
406+
407+
options.privateKey ??= await unmarshalPrivateKey(peerId.privateKey as Uint8Array)
408+
409+
return new Libp2pNode(await validateConfig(options))
403410
}

0 commit comments

Comments
 (0)
Please sign in to comment.