Skip to content

Commit

Permalink
Merge pull request #11 from prateekreddy/js-libp2p-v0.27.4
Browse files Browse the repository at this point in the history
WIP Update Libp2p to v0.27.6
  • Loading branch information
shresthagrawal authored Aug 1, 2020
2 parents 0d3da57 + 5ea6bb9 commit 36cfda2
Show file tree
Hide file tree
Showing 41 changed files with 2,256 additions and 1,361 deletions.
14 changes: 10 additions & 4 deletions assets/1/1.1-finished-code.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
'use strict'

const util = require('util');
const { createLibp2p } = require('libp2p')
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')

let options = {}
// NOTE: Transport and id are mandatory parameters to create a libp2p instance. Id if not specified will be generated automatically
let options = {
modules: {
transport: [ TCP ]
}
}

async function main() {
// Create a libp2p instance
let libp2p = await util.promisify(createLibp2p)(options)
let libp2p = await Libp2p.create(options)
console.log(libp2p.peerInfo.id.toJSON())
}

main()
14 changes: 10 additions & 4 deletions assets/1/1.1-template-code.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
'use-strict'

// Include `util` from the standard lib
// Include `createLibp2p` from the libp2p
// Require `libp2p` module
// Require `libp2p-tcp` module


// Create an empty map options
// Add mandatory transport options needed to create libp2p instance
let options = {
modules: {
transport: [ /* TODO: add `libp2p-tcp` instance */ ]
}
}

// Declare async main
// Inside main declare libp2p node using promisified createLibp2p and pass options
// Inside main declare libp2p node using `create` method of Libp2p module and pass options
// See for youself the id of the libp2p node we have created by using libp2p node's peerInfo.id.toJSON()

// Call main
16 changes: 11 additions & 5 deletions assets/1/1.2-finished-code.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
'use strict'

const util = require('util');
const { createLibp2p } = require('libp2p')
const Libp2p = require('libp2p')

const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const WStar = require('libp2p-webrtc-star')
const Wrtc = require('wrtc')

const WebrtcStar = new WStar({ wrtc: Wrtc })
const transportKey = WStar.prototype[Symbol.toStringTag]

let options = {
modules: {
transport: [ TCP, WS, WebrtcStar ]
transport: [ TCP, WS, WStar ]
},
config: {
transport: {
[transportKey]: {
Wrtc
}
}
}
}

async function main() {
// Create a libp2p instance
let libp2p = await util.promisify(createLibp2p)(options)
let libp2p = await Libp2p.create(options)
}

main()
22 changes: 15 additions & 7 deletions assets/1/1.2-template-code.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
'use strict'

const util = require('util');
const { createLibp2p } = require('libp2p')
// TODO: require `libp2p-tcp`, `libp2p-websockets`, and `libp2p-webrtc-star`
const Libp2p = require('libp2p')

const TCP = require('libp2p-tcp')
// TODO: require `libp2p-websockets` and `libp2p-webrtc-star`
// TODO: require `wrtc`

// TODO: Create a new instance of `libp2p-webrtc-star`, and pass it { wrtc }
// TODO: Get the toString tag for `libp2p-webrtc-star` class using `.prototype[Symbol.toStringTag]` method

let options = {
modules: {
transport: [/* TODO: add `libp2p-tcp`, `libp2p-websockets`, and your `libp2p-webrtc-star` instance */]
}
transport: [ TCP, /* TODO: add `libp2p-websockets`, and `libp2p-webrtc-star` instance */ ],
},
config: {
transport: {
// TODO: add map with key as key for `libp2p-webrtc-star` and value as the arguments to be sent to the module while initializing
// NOTE: The elements specified here are passed as arguments for the modules specified. As here we are using wrtc implementation for webRTC
}
}
}

async function main() {
// Create a libp2p instance
let libp2p = await util.promisify(createLibp2p)(options)
let libp2p = await Libp2p.create(options)
console.log(libp2p.peerInfo.id.toJSON())
}

main()
21 changes: 12 additions & 9 deletions assets/1/1.3-finished-code.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
'use strict'

const util = require('util');
const { createLibp2p } = require('libp2p')
const Libp2p = require('libp2p')

const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const WStar = require('libp2p-webrtc-star')
const Wrtc = require('wrtc')

const WebrtcStar = new WStar({ wrtc: Wrtc })
const transportKey = WStar.prototype[Symbol.toStringTag]

let options = {
modules: {
transport: [ TCP, WS, WebrtcStar ]
transport: [ TCP, WS, WStar ]
},
config: {
transport: {
[transportKey]: {
Wrtc
}
}
}
}

async function main() {
// Create a libp2p instance
let libp2p = await util.promisify(createLibp2p)(options)

libp2p.on('start', () => {
console.info(`Libp2p Started`)
})
let libp2p = await Libp2p.create(options)

await libp2p.start();
console.info(`Libp2p Started`)
}

main()
18 changes: 11 additions & 7 deletions assets/1/1.3-template-code.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
'use strict'

const util = require('util');
const { createLibp2p } = require('libp2p')
const Libp2p = require('libp2p')

const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const WStar = require('libp2p-webrtc-star')
const Wrtc = require('wrtc')

const WebrtcStar = new WStar({ wrtc: Wrtc })
const transportKey = WStar.prototype[Symbol.toStringTag]

let options = {
modules: {
transport: [ TCP, WS, WebrtcStar ]
transport: [ TCP, WS, WStar ]
},
config: {
transport: {
[transportKey]: {
Wrtc
}
}
}
}

async function main() {
// Create a libp2p instance
let libp2p = await util.promisify(createLibp2p)(options)

// Initialise a listener for libp2p to start
let libp2p = await Libp2p.create(options)

// Start libp2p
}
Expand Down
23 changes: 12 additions & 11 deletions assets/2/2.0-finished-code.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
'use strict'

const util = require('util');
const { createLibp2p } = require('libp2p')
const Libp2p = require('libp2p')

const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const WStar = require('libp2p-webrtc-star')
const Wrtc = require('wrtc')

const multiaddr = require('multiaddr')

const WebrtcStar = new WStar({ wrtc: Wrtc })
const transportKey = WStar.prototype[Symbol.toStringTag]

let options = {
modules: {
transport: [ TCP, WS, WebrtcStar ]
transport: [ TCP, WS, WStar ]
},
config: {
transport: {
[transportKey]: {
Wrtc
}
}
}
}

async function main() {
// Create a libp2p instance
let libp2p = await util.promisify(createLibp2p)(options)

libp2p.on('start', () => {
console.info(`Libp2p Started`)
})
let libp2p = await Libp2p.create(options)

libp2p.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
libp2p.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0/ws')

await libp2p.start();
console.info(`Libp2p Started`)
}

main()
23 changes: 12 additions & 11 deletions assets/2/2.0-template-code.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
'use strict'

const util = require('util');
const { createLibp2p } = require('libp2p')
const Libp2p = require('libp2p')

const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const WStar = require('libp2p-webrtc-star')
const Wrtc = require('wrtc')

// Include multiaddr

const WebrtcStar = new WStar({ wrtc: Wrtc })
const transportKey = WStar.prototype[Symbol.toStringTag]

let options = {
modules: {
transport: [ TCP, WS, WebrtcStar ]
transport: [ TCP, WS, WStar ]
},
config: {
transport: {
[transportKey]: {
Wrtc
}
}
}
}

async function main() {
// Create a libp2p instance
let libp2p = await util.promisify(createLibp2p)(options)

libp2p.on('start', () => {
console.info(`Libp2p Started`)
})
let libp2p = await Libp2p.create(options)

// Add multiaddr for TCP
// Add multiaddr for ws

await libp2p.start();
console.info(`Libp2p Started`)
}

main()
31 changes: 17 additions & 14 deletions assets/2/2.1-finished-code.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
'use strict'

const util = require('util')
const { createLibp2p } = require('libp2p')
const Libp2p = require('libp2p')

const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const WStar = require('libp2p-webrtc-star')
const Wrtc = require('wrtc')
const Plaintext = require('libp2p/src/insecure/plaintext')

const multiaddr = require('multiaddr')

const WebrtcStar = new WStar({ wrtc: Wrtc })
const transportKey = WStar.prototype[Symbol.toStringTag]

let options = {
modules: {
transport: [ TCP, WS, WebrtcStar ]
transport: [ TCP, WS, WStar ],
connEncryption: [ Plaintext ]
},
config: {
transport: {
[transportKey]: {
Wrtc
}
}
}
}

async function main() {
// Create a libp2p instance
let libp2p = await util.promisify(createLibp2p)(options)

libp2p.on('start', () => {
console.info(`Libp2p Started`)
libp2p.peerInfo.multiaddrs.forEach(ma => console.log(ma.toString()))
});
let libp2p = await Libp2p.create(options)

libp2p.on('connection:start', (peerInfo) => {
libp2p.on('peer:connect', (peerInfo) => {
console.info(`Connected to ${peerInfo.id.toB58String()}!`)
})

libp2p.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
libp2p.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0/ws')

await libp2p.start()
await libp2p.start();
console.info(`Libp2p Started`)
libp2p.peerInfo.multiaddrs.forEach(ma => console.log(ma.toString()))
}

main()
Loading

1 comment on commit 36cfda2

@vercel
Copy link

@vercel vercel bot commented on 36cfda2 Aug 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.