|
2 | 2 |
|
3 | 3 | const identify = require('libp2p-identify')
|
4 | 4 | const multistream = require('multistream-select')
|
5 |
| -const waterfall = require('async/waterfall') |
6 | 5 | const debug = require('debug')
|
7 | 6 | const log = debug('libp2p:switch:conn-manager')
|
8 | 7 | const once = require('once')
|
9 | 8 | const ConnectionFSM = require('../connection')
|
| 9 | +const { msHandle, msSelect, identifyDialer } = require('../utils') |
10 | 10 |
|
11 | 11 | const Circuit = require('libp2p-circuit')
|
12 | 12 |
|
@@ -136,34 +136,33 @@ class ConnectionManager {
|
136 | 136 | }
|
137 | 137 |
|
138 | 138 | // overload peerInfo to use Identify instead
|
139 |
| - conn.getPeerInfo = (callback) => { |
| 139 | + conn.getPeerInfo = async (callback) => { |
140 | 140 | const conn = muxedConn.newStream()
|
141 | 141 | const ms = new multistream.Dialer()
|
142 | 142 | callback = once(callback)
|
143 | 143 |
|
144 |
| - waterfall([ |
145 |
| - (cb) => ms.handle(conn, cb), |
146 |
| - (cb) => ms.select(identify.multicodec, cb), |
147 |
| - // run identify and verify the peer has the same info from crypto |
148 |
| - (conn, cb) => identify.dialer(conn, cryptoPI, cb), |
149 |
| - (peerInfo, observedAddrs, cb) => { |
150 |
| - observedAddrs.forEach((oa) => { |
151 |
| - this.switch._peerInfo.multiaddrs.addSafe(oa) |
152 |
| - }) |
153 |
| - cb(null, peerInfo) |
154 |
| - } |
155 |
| - ], (err, peerInfo) => { |
156 |
| - if (err) { |
157 |
| - return muxedConn.end(() => { |
158 |
| - callback(err, null) |
159 |
| - }) |
160 |
| - } |
161 |
| - |
162 |
| - if (peerInfo) { |
163 |
| - conn.setPeerInfo(peerInfo) |
164 |
| - } |
165 |
| - callback(err, peerInfo) |
166 |
| - }) |
| 144 | + let results |
| 145 | + try { |
| 146 | + await msHandle(ms, conn) |
| 147 | + const msConn = await msSelect(ms, identify.multicodec) |
| 148 | + results = await identifyDialer(msConn, cryptoPI) |
| 149 | + } catch (err) { |
| 150 | + return muxedConn.end(() => { |
| 151 | + callback(err, null) |
| 152 | + }) |
| 153 | + } |
| 154 | + |
| 155 | + const { peerInfo, observedAddrs } = results |
| 156 | + |
| 157 | + for (var i = 0; i < observedAddrs.length; i++) { |
| 158 | + var addr = observedAddrs[i] |
| 159 | + this.switch._peerInfo.multiaddrs.addSafe(addr) |
| 160 | + } |
| 161 | + |
| 162 | + if (peerInfo) { |
| 163 | + conn.setPeerInfo(peerInfo) |
| 164 | + } |
| 165 | + callback(null, peerInfo) |
167 | 166 | }
|
168 | 167 |
|
169 | 168 | conn.getPeerInfo((err, peerInfo) => {
|
|
0 commit comments