Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

fix: accept multiaddr param when opening connections #336

Merged
merged 2 commits into from
Jan 14, 2023
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
2 changes: 1 addition & 1 deletion packages/interface-connection-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface ConnectionManager extends EventEmitter<ConnectionManagerEvents>
* const connection = await libp2p.connectionManager.openConnection(peerId)
* ```
*/
openConnection: (peer: PeerId, options?: AbortOptions) => Promise<Connection>
openConnection: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<Connection>

/**
* Close our connections to a peer
Expand Down
7 changes: 6 additions & 1 deletion packages/interface-mocks/src/connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { connectionPair } from './connection.js'
import errCode from 'err-code'
import type { Registrar } from '@libp2p/interface-registrar'
import type { PubSub } from '@libp2p/interface-pubsub'
import { isMultiaddr, Multiaddr } from '@multiformats/multiaddr'

export interface MockNetworkComponents {
peerId: PeerId
Expand Down Expand Up @@ -75,11 +76,15 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem
return this.connections
}

async openConnection (peerId: PeerId) {
async openConnection (peerId: PeerId | Multiaddr) {
if (this.components == null) {
throw errCode(new Error('Not initialized'), 'ERR_NOT_INITIALIZED')
}

if (isMultiaddr(peerId)) {
throw errCode(new Error('Dialing multiaddrs not supported'), 'ERR_NOT_SUPPORTED')
}

const existingConnections = this.getConnections(peerId)

if (existingConnections.length > 0) {
Expand Down