Skip to content

Commit bae32ba

Browse files
authored
fix!: remove strings from fetch and hangUp (#1495)
Only accept PeerId or Multiaddr objects as arguments to fetch and hangUp the same as ping, dial, etc. This was missed during the typescript refactor. BREAKING CHANGE: libp2p.hangUp and libp2p.fetch require PeerId or Multiaddr objects the same as other methods
1 parent 9fd58c8 commit bae32ba

File tree

6 files changed

+15
-16
lines changed

6 files changed

+15
-16
lines changed

examples/auto-relay/dialer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createLibp2p } from 'libp2p'
22
import { webSockets } from '@libp2p/websockets'
33
import { noise } from '@chainsafe/libp2p-noise'
44
import { mplex } from '@libp2p/mplex'
5+
import { multiaddr } from '@multiformats/multiaddr'
56

67
async function main () {
78
const autoRelayNodeAddr = process.argv[2]
@@ -24,7 +25,7 @@ async function main () {
2425
await node.start()
2526
console.log(`Node started with id ${node.peerId.toString()}`)
2627

27-
const conn = await node.dial(autoRelayNodeAddr)
28+
const conn = await node.dial(multiaddr(autoRelayNodeAddr))
2829
console.log(`Connected to the auto relay node via ${conn.remoteAddr.toString()}`)
2930
}
3031

examples/auto-relay/listener.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createLibp2p } from 'libp2p'
22
import { webSockets } from '@libp2p/websockets'
33
import { noise } from '@chainsafe/libp2p-noise'
44
import { mplex } from '@libp2p/mplex'
5+
import { multiaddr } from '@multiformats/multiaddr'
56

67
async function main () {
78
const relayAddr = process.argv[2]
@@ -31,7 +32,7 @@ async function main () {
3132
await node.start()
3233
console.log(`Node started with id ${node.peerId.toString()}`)
3334

34-
const conn = await node.dial(relayAddr)
35+
const conn = await node.dial(multiaddr(relayAddr))
3536

3637
console.log(`Connected to the HOP relay ${conn.remotePeer.toString()}`)
3738

examples/echo/src/dialer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import idl from './id-l.js'
1111
import { createFromJSON } from '@libp2p/peer-id-factory'
1212
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
1313
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
14+
import { multiaddr } from '@multiformats/multiaddr'
1415

1516
async function run() {
1617
const [dialerId, listenerId] = await Promise.all([
@@ -27,7 +28,7 @@ async function run() {
2728
})
2829

2930
// Add peer to Dial (the listener) into the PeerStore
30-
const listenerMultiaddr = '/ip4/127.0.0.1/tcp/10333/p2p/' + listenerId.toString()
31+
const listenerMultiaddr = multiaddr('/ip4/127.0.0.1/tcp/10333/p2p/' + listenerId.toString())
3132

3233
// Start the dialer libp2p node
3334
await dialerNode.start()

src/get-peer.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { peerIdFromString } from '@libp2p/peer-id'
22
import type { Multiaddr } from '@multiformats/multiaddr'
3-
import { multiaddr, isMultiaddr } from '@multiformats/multiaddr'
3+
import { isMultiaddr } from '@multiformats/multiaddr'
44
import errCode from 'err-code'
55
import { codes } from './errors.js'
66
import { isPeerId } from '@libp2p/interface-peer-id'
@@ -28,9 +28,9 @@ function peerIdFromMultiaddr (ma: Multiaddr) {
2828
}
2929

3030
/**
31-
* Converts the given `peer` to a `Peer` object.
31+
* Converts the given `peer` to a `PeerInfo` object.
3232
*/
33-
export function getPeer (peer: PeerId | Multiaddr | string): PeerInfo {
33+
export function getPeer (peer: PeerId | Multiaddr): PeerInfo {
3434
if (isPeerId(peer)) {
3535
return {
3636
id: peer,
@@ -39,10 +39,6 @@ export function getPeer (peer: PeerId | Multiaddr | string): PeerInfo {
3939
}
4040
}
4141

42-
if (typeof peer === 'string') {
43-
peer = multiaddr(peer)
44-
}
45-
4642
let addr
4743

4844
if (isMultiaddr(peer)) {

src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
178178
/**
179179
* Disconnects all connections to the given `peer`
180180
*/
181-
hangUp: (peer: PeerId | Multiaddr | string) => Promise<void>
181+
hangUp: (peer: PeerId | Multiaddr) => Promise<void>
182182

183183
/**
184184
* Registers the `handler` for each protocol
@@ -194,12 +194,12 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
194194
/**
195195
* Pings the given peer in order to obtain the operation latency
196196
*/
197-
ping: (peer: Multiaddr | PeerId, options?: AbortOptions) => Promise<number>
197+
ping: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<number>
198198

199199
/**
200200
* Sends a request to fetch the value associated with the given key from the given peer.
201201
*/
202-
fetch: (peer: PeerId | Multiaddr | string, key: string, options?: AbortOptions) => Promise<Uint8Array | null>
202+
fetch: (peer: PeerId | Multiaddr, key: string, options?: AbortOptions) => Promise<Uint8Array | null>
203203

204204
/**
205205
* Returns the public key for the passed PeerId. If the PeerId is of the 'RSA' type

src/libp2p.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
373373
return this.components.addressManager.getAddresses()
374374
}
375375

376-
async hangUp (peer: PeerId | Multiaddr | string): Promise<void> {
376+
async hangUp (peer: PeerId | Multiaddr): Promise<void> {
377377
const { id } = getPeer(peer)
378378

379379
await this.components.connectionManager.closeConnections(id)
@@ -418,7 +418,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
418418
throw errCode(new Error(`Node not responding with its public key: ${peer.toString()}`), codes.ERR_INVALID_RECORD)
419419
}
420420

421-
async fetch (peer: PeerId | Multiaddr | string, key: string, options: AbortOptions = {}): Promise<Uint8Array | null> {
421+
async fetch (peer: PeerId | Multiaddr, key: string, options: AbortOptions = {}): Promise<Uint8Array | null> {
422422
const { id, multiaddrs } = getPeer(peer)
423423

424424
if (multiaddrs != null) {
@@ -428,7 +428,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
428428
return await this.fetchService.fetch(id, key, options)
429429
}
430430

431-
async ping (peer: PeerId | Multiaddr | string, options: AbortOptions = {}): Promise<number> {
431+
async ping (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<number> {
432432
const { id, multiaddrs } = getPeer(peer)
433433

434434
if (multiaddrs.length > 0) {

0 commit comments

Comments
 (0)