Skip to content
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

fix: use @libp2p/config to load private key from keystore #714

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/helia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,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
Loading