Skip to content

Commit 58bf45e

Browse files
committed
chore: update interfaces and multiaddr
1 parent 2bff878 commit 58bf45e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+208
-197
lines changed

package.json

+9-10
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,23 @@
9494
"it-drain": "^1.0.3",
9595
"it-filter": "^1.0.1",
9696
"it-first": "^1.0.4",
97-
"it-handshake": "^1.0.2",
98-
"it-length-prefixed": "^4.0.0",
97+
"it-handshake": "^2.0.0",
98+
"it-length-prefixed": "^5.0.2",
9999
"it-map": "^1.0.4",
100100
"it-merge": "1.0.0",
101101
"it-pipe": "^1.1.0",
102102
"it-protocol-buffers": "^0.2.0",
103103
"it-take": "1.0.0",
104104
"libp2p-crypto": "^0.19.0",
105-
"libp2p-interfaces": "^0.9.0",
106-
"libp2p-utils": "^0.3.0",
107-
"mafmt": "^8.0.0",
105+
"libp2p-interfaces": "^0.10.0",
106+
"libp2p-utils": "^0.3.1",
107+
"mafmt": "^9.0.0",
108108
"merge-options": "^3.0.4",
109109
"moving-average": "^1.0.0",
110-
"multiaddr": "^8.1.2",
110+
"multiaddr": "^9.0.1",
111111
"multicodec": "^3.0.1",
112112
"multihashing-async": "^2.1.2",
113-
"multistream-select": "^1.0.0",
113+
"multistream-select": "^2.0.0",
114114
"mutable-proxy": "^1.0.0",
115115
"node-forge": "^0.10.0",
116116
"p-any": "^3.0.0",
@@ -149,13 +149,12 @@
149149
"libp2p-bootstrap": "^0.12.0",
150150
"libp2p-delegated-content-routing": "^0.9.0",
151151
"libp2p-delegated-peer-routing": "^0.8.0",
152-
"libp2p-floodsub": "^0.24.0",
153-
"libp2p-gossipsub": "^0.8.0",
152+
"libp2p-floodsub": "libp2p/js-libp2p-floodsub#chore/update-deps-and-remove-protons",
153+
"libp2p-gossipsub": "ChainSafe/js-libp2p-gossipsub#chore/update-deps-and-remove-protons",
154154
"libp2p-kad-dht": "^0.21.0",
155155
"libp2p-mdns": "^0.15.0",
156156
"libp2p-mplex": "^0.10.1",
157157
"libp2p-noise": "^2.0.0",
158-
"libp2p-secio": "^0.13.1",
159158
"libp2p-tcp": "^0.15.1",
160159
"libp2p-webrtc-star": "^0.21.2",
161160
"libp2p-websockets": "^0.15.0",

src/address-manager/index.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
/** @typedef {import('../types').EventEmitterFactory} Events */
44
/** @type Events */
55
const EventEmitter = require('events')
6-
const multiaddr = require('multiaddr')
6+
const { Multiaddr } = require('multiaddr')
77
const PeerId = require('peer-id')
88

9-
/**
10-
* @typedef {import('multiaddr')} Multiaddr
11-
*/
12-
139
/**
1410
* @typedef {Object} AddressManagerOptions
1511
* @property {string[]} [listen = []] - list of multiaddrs string representation to listen.
@@ -47,7 +43,7 @@ class AddressManager extends EventEmitter {
4743
* @returns {Multiaddr[]}
4844
*/
4945
getListenAddrs () {
50-
return Array.from(this.listen).map((a) => multiaddr(a))
46+
return Array.from(this.listen).map((a) => new Multiaddr(a))
5147
}
5248

5349
/**
@@ -56,7 +52,7 @@ class AddressManager extends EventEmitter {
5652
* @returns {Multiaddr[]}
5753
*/
5854
getAnnounceAddrs () {
59-
return Array.from(this.announce).map((a) => multiaddr(a))
55+
return Array.from(this.announce).map((a) => new Multiaddr(a))
6056
}
6157

6258
/**
@@ -65,7 +61,7 @@ class AddressManager extends EventEmitter {
6561
* @returns {Array<Multiaddr>}
6662
*/
6763
getObservedAddrs () {
68-
return Array.from(this.observed).map((a) => multiaddr(a))
64+
return Array.from(this.observed).map((a) => new Multiaddr(a))
6965
}
7066

7167
/**
@@ -74,7 +70,7 @@ class AddressManager extends EventEmitter {
7470
* @param {string | Multiaddr} addr
7571
*/
7672
addObservedAddr (addr) {
77-
let ma = multiaddr(addr)
73+
let ma = new Multiaddr(addr)
7874
const remotePeer = ma.getPeerId()
7975

8076
// strip our peer id if it has been passed
@@ -83,7 +79,7 @@ class AddressManager extends EventEmitter {
8379

8480
// use same encoding for comparison
8581
if (remotePeerId.equals(this.peerId)) {
86-
ma = ma.decapsulate(multiaddr(`/p2p/${this.peerId}`))
82+
ma = ma.decapsulate(new Multiaddr(`/p2p/${this.peerId}`))
8783
}
8884
}
8985

src/circuit/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Libp2p circuit configuration can be seen at [Setup with Relay](../../doc/CONFIGU
3737
Once you have a circuit relay node running, you can configure other nodes to use it as a relay as follows:
3838

3939
```js
40-
const multiaddr = require('multiaddr')
40+
const { Multiaddr } = require('multiaddr')
4141
const Libp2p = require('libp2p')
4242
const TCP = require('libp2p-tcp')
4343
const MPLEX = require('libp2p-mplex')
@@ -47,7 +47,7 @@ const relayAddr = ...
4747

4848
const node = await Libp2p.create({
4949
addresses: {
50-
listen: [multiaddr(`${relayAddr}/p2p-circuit`)]
50+
listen: [new Multiaddr(`${relayAddr}/p2p-circuit`)]
5151
},
5252
modules: {
5353
transport: [TCP],

src/circuit/auto-relay.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const log = Object.assign(debug('libp2p:auto-relay'), {
77

88
const uint8ArrayFromString = require('uint8arrays/from-string')
99
const uint8ArrayToString = require('uint8arrays/to-string')
10-
const multiaddr = require('multiaddr')
10+
const { Multiaddr } = require('multiaddr')
1111
const PeerId = require('peer-id')
1212

1313
const { relay: multicodec } = require('./multicodec')
@@ -157,7 +157,7 @@ class AutoRelay {
157157

158158
// Attempt to listen on relay
159159
try {
160-
await this._transportManager.listen([multiaddr(listenAddr)])
160+
await this._transportManager.listen([new Multiaddr(listenAddr)])
161161
// Announce multiaddrs will update on listen success by TransportManager event being triggered
162162
} catch (err) {
163163
log.error(err)

src/circuit/circuit/stream-handler.js

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class StreamHandler {
2828
this.stream = stream
2929

3030
this.shake = handshake(this.stream)
31+
// @ts-ignore options are not optional
3132
this.decoder = lp.decode.fromReader(this.shake.reader, { maxDataLength: maxLength })
3233
}
3334

src/circuit/circuit/utils.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const multiaddr = require('multiaddr')
3+
const { Multiaddr } = require('multiaddr')
44
const { CircuitRelay } = require('../protocol')
55

66
/**
@@ -31,7 +31,7 @@ function validateAddrs (msg, streamHandler) {
3131
try {
3232
if (msg.dstPeer && msg.dstPeer.addrs) {
3333
msg.dstPeer.addrs.forEach((addr) => {
34-
return multiaddr(addr)
34+
return new Multiaddr(addr)
3535
})
3636
}
3737
} catch (err) {
@@ -44,7 +44,7 @@ function validateAddrs (msg, streamHandler) {
4444
try {
4545
if (msg.srcPeer && msg.srcPeer.addrs) {
4646
msg.srcPeer.addrs.forEach((addr) => {
47-
return multiaddr(addr)
47+
return new Multiaddr(addr)
4848
})
4949
}
5050
} catch (err) {

src/circuit/listener.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict'
22

33
const { EventEmitter } = require('events')
4-
const multiaddr = require('multiaddr')
4+
const { Multiaddr } = require('multiaddr')
55

66
/**
7-
* @typedef {import('multiaddr')} Multiaddr
87
* @typedef {import('libp2p-interfaces/src/transport/types').Listener} Listener
98
*/
109

@@ -24,7 +23,7 @@ module.exports = (libp2p) => {
2423
async function listen (addr) {
2524
const addrString = String(addr).split('/p2p-circuit').find(a => a !== '')
2625

27-
const relayConn = await libp2p.dial(multiaddr(addrString))
26+
const relayConn = await libp2p.dial(new Multiaddr(addrString))
2827
const relayedAddr = relayConn.remoteAddr.encapsulate('/p2p-circuit')
2928

3029
listeningAddrs.set(relayConn.remotePeer.toB58String(), relayedAddr)

src/circuit/transport.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ const log = Object.assign(debug('libp2p:circuit'), {
55
error: debug('libp2p:circuit:err')
66
})
77

8+
const errCode = require('err-code')
89
const mafmt = require('mafmt')
9-
const multiaddr = require('multiaddr')
10+
const { Multiaddr } = require('multiaddr')
1011
const PeerId = require('peer-id')
1112
const { CircuitRelay: CircuitPB } = require('./protocol')
13+
const { codes } = require('../errors')
1214

1315
const toConnection = require('libp2p-utils/src/stream-to-ma-conn')
1416

@@ -21,7 +23,6 @@ const StreamHandler = require('./circuit/stream-handler')
2123
const transportSymbol = Symbol.for('@libp2p/js-libp2p-circuit/circuit')
2224

2325
/**
24-
* @typedef {import('multiaddr')} Multiaddr
2526
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
2627
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
2728
*/
@@ -96,9 +97,9 @@ class Circuit {
9697

9798
if (virtualConnection) {
9899
// @ts-ignore dst peer will not be undefined
99-
const remoteAddr = multiaddr(request.dstPeer.addrs[0])
100+
const remoteAddr = new Multiaddr(request.dstPeer.addrs[0])
100101
// @ts-ignore src peer will not be undefined
101-
const localAddr = multiaddr(request.srcPeer.addrs[0])
102+
const localAddr = new Multiaddr(request.srcPeer.addrs[0])
102103
const maConn = toConnection({
103104
stream: virtualConnection,
104105
remoteAddr,
@@ -124,10 +125,19 @@ class Circuit {
124125
async dial (ma, options) {
125126
// Check the multiaddr to see if it contains a relay and a destination peer
126127
const addrs = ma.toString().split('/p2p-circuit')
127-
const relayAddr = multiaddr(addrs[0])
128-
const destinationAddr = multiaddr(addrs[addrs.length - 1])
129-
const relayPeer = PeerId.createFromCID(relayAddr.getPeerId())
130-
const destinationPeer = PeerId.createFromCID(destinationAddr.getPeerId())
128+
const relayAddr = new Multiaddr(addrs[0])
129+
const destinationAddr = new Multiaddr(addrs[addrs.length - 1])
130+
const relayId = relayAddr.getPeerId()
131+
const destinationId = destinationAddr.getPeerId()
132+
133+
if (!relayId || !destinationId) {
134+
const errMsg = 'Circuit relay dial failed as addresses did not have peer id'
135+
log.error(errMsg)
136+
throw errCode(new Error(errMsg), codes.ERR_RELAYED_DIAL)
137+
}
138+
139+
const relayPeer = PeerId.createFromCID(relayId)
140+
const destinationPeer = PeerId.createFromCID(destinationId)
131141

132142
let disconnectOnFailure = false
133143
let relayConnection = this._connectionManager.get(relayPeer)
@@ -147,7 +157,7 @@ class Circuit {
147157
},
148158
dstPeer: {
149159
id: destinationPeer.toBytes(),
150-
addrs: [multiaddr(destinationAddr).bytes]
160+
addrs: [new Multiaddr(destinationAddr).bytes]
151161
}
152162
}
153163
})

src/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')
1212
const { FaultTolerance } = require('./transport-manager')
1313

1414
/**
15-
* @typedef {import('multiaddr')} Multiaddr
15+
* @typedef {import('multiaddr').Multiaddr} Multiaddr
1616
* @typedef {import('.').Libp2pOptions} Libp2pOptions
1717
* @typedef {import('.').constructorOptions} constructorOptions
1818
*/

src/content-routing/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const { pipe } = require('it-pipe')
1414

1515
/**
1616
* @typedef {import('peer-id')} PeerId
17-
* @typedef {import('multiaddr')} Multiaddr
17+
* @typedef {import('multiaddr').Multiaddr} Multiaddr
1818
* @typedef {import('cids')} CID
1919
* @typedef {import('libp2p-interfaces/src/content-routing/types')} ContentRoutingModule
2020
*/

src/content-routing/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const take = require('it-take')
77

88
/**
99
* @typedef {import('peer-id')} PeerId
10-
* @typedef {import('multiaddr')} Multiaddr
10+
* @typedef {import('multiaddr').Multiaddr} Multiaddr
1111
*/
1212

1313
/**

src/dialer/dial-request.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const pAny = require('p-any')
1010
/**
1111
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
1212
* @typedef {import('./')} Dialer
13-
* @typedef {import('multiaddr')} Multiaddr
13+
* @typedef {import('multiaddr').Multiaddr} Multiaddr
1414
*/
1515

1616
/**

src/dialer/index.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const log = Object.assign(debug('libp2p:dialer'), {
55
error: debug('libp2p:dialer:err')
66
})
77
const errCode = require('err-code')
8-
const multiaddr = require('multiaddr')
8+
const { Multiaddr } = require('multiaddr')
99
// @ts-ignore timeout-abourt-controles does not export types
1010
const TimeoutController = require('timeout-abort-controller')
1111
const { anySignal } = require('any-signal')
@@ -23,7 +23,6 @@ const {
2323

2424
/**
2525
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
26-
* @typedef {import('multiaddr')} Multiaddr
2726
* @typedef {import('peer-id')} PeerId
2827
* @typedef {import('../peer-store')} PeerStore
2928
* @typedef {import('../peer-store/address-book').Address} Address
@@ -79,7 +78,7 @@ class Dialer {
7978
this._pendingDials = new Map()
8079

8180
for (const [key, value] of Object.entries(resolvers)) {
82-
multiaddr.resolvers.set(key, value)
81+
Multiaddr.resolvers.set(key, value)
8382
}
8483
}
8584

@@ -151,7 +150,7 @@ class Dialer {
151150

152151
// If received a multiaddr to dial, it should be the first to use
153152
// But, if we know other multiaddrs for the peer, we should try them too.
154-
if (multiaddr.isMultiaddr(peer)) {
153+
if (Multiaddr.isMultiaddr(peer)) {
155154
knownAddrs = knownAddrs.filter((addr) => !peer.equals(addr))
156155
knownAddrs.unshift(peer)
157156
}
@@ -180,7 +179,7 @@ class Dialer {
180179
*/
181180
_createPendingDial (dialTarget, options = {}) {
182181
/**
183-
* @param {multiaddr} addr
182+
* @param {Multiaddr} addr
184183
* @param {{ signal: { aborted: any; }; }} options
185184
*/
186185
const dialAction = (addr, options) => {
@@ -271,7 +270,7 @@ class Dialer {
271270
*/
272271
async _resolveRecord (ma) {
273272
try {
274-
ma = multiaddr(ma.toString()) // Use current multiaddr module
273+
ma = new Multiaddr(ma.toString()) // Use current multiaddr module
275274
const multiaddrs = await ma.resolve()
276275
return multiaddrs
277276
} catch (_) {

src/errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ exports.codes = {
1616
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',
1717
ERR_ALREADY_ABORTED: 'ERR_ALREADY_ABORTED',
1818
ERR_NO_VALID_ADDRESSES: 'ERR_NO_VALID_ADDRESSES',
19+
ERR_RELAYED_DIAL: 'ERR_RELAYED_DIAL',
1920
ERR_DIALED_SELF: 'ERR_DIALED_SELF',
2021
ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF',
2122
ERR_DUPLICATE_TRANSPORT: 'ERR_DUPLICATE_TRANSPORT',

0 commit comments

Comments
 (0)