Skip to content

Commit

Permalink
fix: use @libp2p/config to load private key from keystore (#714)
Browse files Browse the repository at this point in the history
Simplifies the construction of the libp2p options to use the helpers
from `@libp2p/config` to load a private key from the keystore.
  • Loading branch information
achingbrain authored Jan 13, 2025
1 parent 5784ceb commit b4f9d4f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 32 deletions.
3 changes: 1 addition & 2 deletions packages/helia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@
"@libp2p/autonat": "^2.0.12",
"@libp2p/bootstrap": "^11.0.13",
"@libp2p/circuit-relay-v2": "^3.1.3",
"@libp2p/crypto": "^5.0.7",
"@libp2p/config": "^1.0.1",
"@libp2p/dcutr": "^2.0.12",
"@libp2p/identify": "^3.0.12",
"@libp2p/interface": "^2.2.1",
"@libp2p/kad-dht": "^14.1.3",
"@libp2p/keychain": "^5.0.10",
"@libp2p/logger": "^5.1.4",
"@libp2p/mdns": "^11.0.13",
"@libp2p/mplex": "^11.0.13",
"@libp2p/ping": "^2.0.12",
Expand Down
37 changes: 7 additions & 30 deletions packages/helia/src/utils/libp2p.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { generateKeyPair } from '@libp2p/crypto/keys'
import { keychain } from '@libp2p/keychain'
import { defaultLogger } from '@libp2p/logger'
import { Key } from 'interface-datastore'
import { loadOrCreateSelfKey } from '@libp2p/config'
import { createLibp2p as create } from 'libp2p'
import { libp2pDefaults } from './libp2p-defaults.js'
import type { DefaultLibp2pServices } from './libp2p-defaults.js'
import type { ComponentLogger, Libp2p, PrivateKey } from '@libp2p/interface'
import type { Keychain, KeychainInit } from '@libp2p/keychain'
import type { KeychainInit } from '@libp2p/keychain'
import type { DNS } from '@multiformats/dns'
import type { Datastore } from 'interface-datastore'
import type { Libp2pOptions } from 'libp2p'
Expand All @@ -26,40 +23,20 @@ export interface Libp2pDefaultsOptions {
}

export async function createLibp2p <T extends Record<string, unknown> = DefaultLibp2pServices> (options: CreateLibp2pOptions<T>): Promise<Libp2p<T>> {
const privateKey = options.libp2p?.privateKey
const logger = options.logger ?? defaultLogger()
const selfKey = new Key('/pkcs8/self')
let chain: Keychain | undefined
const libp2pOptions = options.libp2p ?? {}

// if no peer id was passed, try to load it from the keychain
if (privateKey == null && options.datastore != null) {
chain = keychain(options.keychain)({
datastore: options.datastore,
logger
})

options.libp2p = options.libp2p ?? {}

if (await options.datastore.has(selfKey)) {
// load the peer id from the keychain
options.libp2p.privateKey = await chain.exportKey('self')
} else {
const privateKey = await generateKeyPair('Ed25519')
options.libp2p.privateKey = privateKey

// persist the peer id in the keychain for next time
await chain.importKey('self', privateKey)
}
if (libp2pOptions.privateKey == null && options.datastore != null) {
libp2pOptions.privateKey = await loadOrCreateSelfKey(options.datastore, options.keychain)
}

const defaults = libp2pDefaults(options)
const defaults = libp2pDefaults(libp2pOptions)
defaults.datastore = defaults.datastore ?? options.datastore
options = options ?? {}

// @ts-expect-error derived ServiceMap is not compatible with ServiceFactoryMap
const node = await create({
...defaults,
...options.libp2p,
...libp2pOptions,
start: false
})

Expand Down

0 comments on commit b4f9d4f

Please sign in to comment.