From b4f9d4fc234cc66e9cdf444900e365b1c63b9920 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Mon, 13 Jan 2025 17:45:39 +0100 Subject: [PATCH] fix: use @libp2p/config to load private key from keystore (#714) Simplifies the construction of the libp2p options to use the helpers from `@libp2p/config` to load a private key from the keystore. --- packages/helia/package.json | 3 +-- packages/helia/src/utils/libp2p.ts | 37 ++++++------------------------ 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/packages/helia/package.json b/packages/helia/package.json index dfdd5c0e..b73552c8 100644 --- a/packages/helia/package.json +++ b/packages/helia/package.json @@ -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", diff --git a/packages/helia/src/utils/libp2p.ts b/packages/helia/src/utils/libp2p.ts index c906722f..93af0adc 100644 --- a/packages/helia/src/utils/libp2p.ts +++ b/packages/helia/src/utils/libp2p.ts @@ -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' @@ -26,40 +23,20 @@ export interface Libp2pDefaultsOptions { } export async function createLibp2p = DefaultLibp2pServices> (options: CreateLibp2pOptions): Promise> { - 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 })