Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.on('connect', async () => {
// console.log('[initiator] connected')
const peerInfo = await PeerInfo.create(await PeerId.createFromB58String(multiaddr.getPeerId()))
peerInfo.multiaddrs.add(multiaddr)
peerInfo.connect(multiaddr)
conn.setPeerInfo(peerInfo)
resolve(conn)
})
.then((storedPeerInfo) => {
if (!storedPeerInfo) {
log.info(`${MODULE_NAME}: GET ${peerId} (No info found)`)
return null
}
log.info(`${MODULE_NAME}: GET ${peerId}`)
// const peerIdObj = new PeerId(new Buffer(storedPeerInfo.id))
// const peerInfo = new PeerInfo(peerIdObj)
const peerInfo = new PeerInfo(PeerId.createFromB58String(storedPeerInfo.id))
// add multiaddrs to the peer
// storedPeerInfo.multiaddrs.forEach((mAddr) => {
// console.log('madr:', mAddr)
// peerInfo.multiaddr.add(mAddr)
// })
// peerInfo.multiaddr.add('/ip4/10.2.2.164/tcp/4002')
// peerInfo.multiaddr.add('/ip4/10.2.2.164/tcp/4002')
console.log(storedPeerInfo.multiaddrs[1])
console.log('/ip4/10.2.2.164/tcp/4002')
console.log('/ip4/10.2.2.164/tcp/4002' === storedPeerInfo.multiaddrs[1])
// peerInfo.multiaddr.add(storedPeerInfo.multiaddrs[0])
peerInfo.multiaddr.add(storedPeerInfo.multiaddrs[1])
console.log('GOT FROM SHIM SERVER:', storedPeerInfo)
async function checkPeerIdInput(query) {
let peerId
try {
// Throws an error if the Id is invalid
Multihash.decode(bs58.decode(query))
peerId = await addPubKey(PeerId.createFromB58String(query))
} catch (err) {
throw Error(chalk.red(`Invalid peerId. ${err.message}`))
}
return peerId
}
beforeEach(() => {
dialer.swarm = {
_peerInfo: peer
}
dialer.utils = utilsFactory({})
dialer.relayConns = new Map()
dialer._negotiateRelay.callThrough()
dialer._dialRelayHelper.callThrough()
peer = new PeerInfo(PeerId.createFromB58String('QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE'))
p = pair()
conn = new Connection(p[1])
})
function peerDiscovered (maStr) {
log('Peer Discovered:', maStr)
const id = peerId.createFromB58String(maStr.split('/')[8])
const peer = new PeerInfo(id)
peer.multiaddr.add(multiaddr(maStr))
this.discovery.emit('peer', peer)
}
}
callback(null, result.Peers.map((p) => {
const res = {
addr: multiaddr(p.Addr),
peer: PeerId.createFromB58String(p.Peer),
muxer: p.Muxer
}
if (p.Latency) {
res.latency = p.Latency
}
if (p.Streams) {
res.streams = p.Streams
}
return res
}))
}
(cb) => node.peerRouting.findPeer(PeerId.createFromB58String(peerId), cb),
(peerInfo, cb) => node.dialProtocol(peerInfo, PROTOCOL_CRAWLING, cb),
async _fetchPeer (peerId) {
const pr = this._node.peerRouting
const lookFor = PeerId.createFromB58String(peerId)
const start = Date.now()
let peer = null
let waitTime = 1
while (!peer && Date.now() - start < 5000) {
try {
if (this._peerCache.has(peerId)) {
return this._peerCache.get(peerId)
}
peer = await $p(pr.findPeer.bind(pr, lookFor))
} catch (e) {
peer = null
await $sleep(waitTime)
waitTime *= 2
if (waitTime > 200) {
waitTime = 200
}
return (res.Peers || []).map(peer => {
const info = {}
try {
info.addr = multiaddr(peer.Addr)
info.peer = PeerId.createFromB58String(peer.Peer)
} catch (error) {
info.error = error
info.rawPeerInfo = peer
}
if (peer.Muxer) {
info.muxer = peer.Muxer
}
if (peer.Latency) {
info.latency = peer.Latency
}
if (peer.Streams) {
info.streams = peer.Streams
}
if (peer.Direction != null) {
info.direction = peer.Direction
}
async _fetchPeer (peerId) {
if (this._peerCache.has(peerId)) {
return this._peerCache.get(peerId)
}
const pr = this._p2p.peerRouting
const start = Date.now()
let peer = null
while (Date.now() - start < 5000) {
try {
peer = await $p(pr.findPeer.bind(
pr, PeerId.createFromB58String(peerId)))
} catch (e) {
peer = null
await _sleep(100)
}
}
if (!peer) {
throw new Error('could not find peer')
}
if (peer.id.toB58String() !== peerId) {
throw new Error('wtf')
}
this._peerCache.set(peerId, peer)
return peer
}