@helia/libp2p 2.0.0-c9f35dab → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ import { loadOrCreateSelfKey } from '@libp2p/config'
2
+ import { createLibp2p as create } from 'libp2p'
1
3
  import { libp2pDefaults } from './libp2p-defaults.ts'
2
4
  import type { Helia } from '@helia/interface'
3
5
  import type { Libp2p, PrivateKey } from '@libp2p/interface'
@@ -17,21 +19,25 @@ export interface Libp2pDefaultsOptions {
17
19
  version?: string
18
20
  }
19
21
 
20
- export async function createLibp2pOptions <T extends Record<string, unknown>> (helia: Helia, options: CreateLibp2pOptions<T>): Promise<Libp2p<T>> {
22
+ export async function createLibp2p <T extends Record<string, unknown>> (helia: Helia, options: CreateLibp2pOptions<T>): Promise<Libp2p<T>> {
21
23
  const libp2pOptions = options ?? {}
22
24
 
25
+ // if no peer id was passed, try to load it from the keychain
26
+ if (libp2pOptions.privateKey == null && options.datastore != null) {
27
+ libp2pOptions.privateKey = await loadOrCreateSelfKey(options.datastore, options.keychain)
28
+ }
29
+
23
30
  const defaults: any = libp2pDefaults({
24
31
  ...libp2pOptions,
25
- nodeInfo: {
26
- ...libp2pOptions.nodeInfo,
27
- name: libp2pOptions.nodeInfo?.name ?? helia.info.name,
28
- version: libp2pOptions.nodeInfo?.version ?? helia.info.version
29
- }
32
+ name: libp2pOptions.nodeInfo?.name ?? helia.info.name,
33
+ version: libp2pOptions.nodeInfo?.version ?? helia.info.version
30
34
  })
31
35
  defaults.datastore = defaults.datastore ?? options.datastore
32
36
 
33
- return {
37
+ const node = await create<T>({
34
38
  ...defaults,
35
39
  ...libp2pOptions
36
- }
40
+ })
41
+
42
+ return node
37
43
  }