Skip to content

Instantly share code, notes, and snippets.

@Pierozi
Created August 22, 2019 18:42
Show Gist options
  • Save Pierozi/47b0e392dda9c0f70b6aff823e438174 to your computer and use it in GitHub Desktop.
Save Pierozi/47b0e392dda9c0f70b6aff823e438174 to your computer and use it in GitHub Desktop.
LibP2P Dialer Circuit Relay
'use strict'
/* eslint-disable no-console */
const relayId = "QmemquW1rBrxZ4nq1KvNSKgHiRqKhXQdcW4ZvWSMftVAuy"
const relayPort = "54783"
const Node = require('./node-circuit')
const PeerInfo = require('peer-info')
const PeerBook = require('peer-book')
const PeerId = require('peer-id')
const peerBook = new PeerBook()
const relayPeerId = PeerId.createFromB58String(relayId)
const relayPeerInfo = new PeerInfo(relayPeerId)
relayPeerInfo.multiaddrs.add(`/ip4/127.0.0.1/tcp/${relayPort}`)
peerBook.put(relayPeerInfo)
const ListenerPeerIdB58 = "QmY3ncp5hnDcTNb52WbBUbt4Dh2NgxbtAakXikh3oYbbh3"
const listenerPeerId = PeerId.createFromB58String(ListenerPeerIdB58)
const listenerPeerInfo = new PeerInfo(listenerPeerId)
const listenerMultiaddr = `/p2p-circuit/ipfs/${relayId}/ipfs/${ListenerPeerIdB58}`
listenerPeerInfo.multiaddrs.add(listenerMultiaddr)
peerBook.put(listenerPeerInfo)
PeerInfo.create((err, dialerPeerInfo) => {
// Dialer
//dialerPeerInfo.multiaddrs.add(`/ip4/127.0.0.1/tcp/${relayPort}/ipfs/${relayId}/p2p-circuit`);
const dialerNode = new Node({
peerInfo: dialerPeerInfo,
peerBook,
});
console.log('Dialer Peer ID: ', dialerPeerInfo.id.toB58String());
dialerNode.start((err) => {
if (err) { throw err }
console.log('Dialer ready, listening on:')
dialerPeerInfo.multiaddrs.forEach((ma) => console.log(ma.toString()))
console.log('Dialing to peer:', listenerMultiaddr)
dialerNode.dialProtocol(listenerPeerInfo, '/echo/1.0.0', (err, conn) => {
if (err) { throw err }
console.log('nodeA dialed to nodeB on protocol: /echo/1.0.0')
pull(
pull.values(['hey']),
conn,
pull.collect((err, data) => {
if (err) { throw err }
console.log('received echo:', data.toString())
})
)
})
})
})
/*
Error: No available transports to dial peer QmY3ncp5hnDcTNb52WbBUbt4Dh2NgxbtAakXikh3oYbbh3!
at createError (/Users/pierozi/Project/github/plab/peertrust-client/node_modules/err-code/index.js:4:34)
at CONNECTION_FAILED (/Users/pierozi/Project/github/plab/peertrust-client/node_modules/libp2p-switch/src/errors.js:6:31)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment