Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
refactor: return peer IDs as strings not CIDs (#2729)
Browse files Browse the repository at this point in the history
Also updates all examples to use the new API.

Depends on:

- [x] ipfs-inactive/interface-js-ipfs-core#581
- [x] ipfs-inactive/js-ipfs-http-client#1226
- [x] libp2p/js-libp2p#545

BREAKING CHANGE:

Where `PeerID`s were previously [CID](https://www.npmjs.com/package/cids)s, now they are Strings

- `ipfs.bitswap.stat().peers[n]` is now a String (was a CID)
- `ipfs.dht.findPeer().id` is now a String (was a CID)
- `ipfs.dht.findProvs()[n].id` is now a String (was a CID)
- `ipfs.dht.provide()[n].id` is now a String (was a CID)
- `ipfs.dht.put()[n].id` is now a String (was a CID)
- `ipfs.dht.query()[n].id` is now a String (was a CID)
- `ipfs.id().id` is now a String (was a CID)
- `ipfs.id().addresses[n]` are now [Multiaddr](https://www.npmjs.com/package/multiaddr)s (were Strings)
  • Loading branch information
achingbrain authored Feb 3, 2020
1 parent 93ad92d commit 80f0057
Show file tree
Hide file tree
Showing 22 changed files with 164 additions and 137 deletions.
2 changes: 1 addition & 1 deletion browser-video-streaming/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<body>
<video id="video" controls></video>
<script src="../../dist/index.js"></script>
<script src="https://unpkg.com/hlsjs-ipfs-loader@0.1.4/dist/index.js"></script>
<script src="https://unpkg.com/hlsjs-ipfs-loader@0.2.3/dist/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script src="streaming.js"></script>
</body>
Expand Down
1 change: 0 additions & 1 deletion browser-video-streaming/streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

/* global Hls Ipfs HlsjsIpfsLoader */
/* eslint-env browser */

document.addEventListener('DOMContentLoaded', async () => {
const testHash = 'QmdpAidwAsBGptFB3b6A9Pyi5coEbgjHrL3K2Qrsutmj9K'
const repoPath = 'ipfs-' + Math.random()
Expand Down
2 changes: 1 addition & 1 deletion circuit-relaying/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"license": "MIT",
"dependencies": {
"ipfs": "file:../../",
"ipfs-pubsub-room": "^1.4.0"
"ipfs-pubsub-room": "^2.0.1"
},
"devDependencies": {
"aegir": "^20.0.0",
Expand Down
2 changes: 1 addition & 1 deletion circuit-relaying/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const mkRoomName = (name) => {

module.exports = (ipfs, peersSet) => {
const createRoom = (name) => {
const room = Room(ipfs, mkRoomName(name))
const room = new Room(ipfs, mkRoomName(name))

room.on('peer joined', (peer) => {
console.log('peer ' + peer + ' joined')
Expand Down
4 changes: 3 additions & 1 deletion circuit-relaying/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ async function runTest () {

try {
const id = await ipfsd.api.id()
const address = id.addresses.filter(addr => addr.includes('/ws/ipfs/Qm')).pop()
const address = id.addresses
.map(ma => ma.toString())
.find(addr => addr.includes('/ws/p2p/Qm'))

if (!address) {
throw new Error(`Could not find web socket address in ${id.addresses}`)
Expand Down
19 changes: 10 additions & 9 deletions custom-ipfs-repo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const IPFS = require('ipfs')
const Repo = require('ipfs-repo')
const fsLock = require('ipfs-repo/src/lock')
const all = require('it-all')

// Create our custom options
const customRepositoryOptions = {
Expand Down Expand Up @@ -79,19 +80,19 @@ async function main () {
console.log('Version:', version)

// Once we have the version, let's add a file to IPFS
const filesAdded = await node.add({
for await (const file of node.add({
path: 'test-data.txt',
content: Buffer.from('We are using a customized repo!')
})

// Log out the added files metadata and cat the file from IPFS
console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash)
})) {
// Log out the added files metadata and cat the file from IPFS
console.log('\nAdded file:', file.path, file.cid)

const data = await node.cat(filesAdded[0].hash)
const data = Buffer.concat(await all(node.cat(file.cid)))

// Print out the files contents to console
console.log('\nFetched file content:')
process.stdout.write(data)
// Print out the files contents to console
console.log('\nFetched file content:')
process.stdout.write(data)
}

// After everything is done, shut the node down
console.log('\n\nStopping the node')
Expand Down
3 changes: 2 additions & 1 deletion custom-ipfs-repo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"dependencies": {
"datastore-fs": "^0.9.1",
"ipfs": "file:../../",
"ipfs-repo": "^0.28.0"
"ipfs-repo": "^0.28.0",
"it-all": "^1.0.1"
},
"devDependencies": {
"execa": "^3.2.0"
Expand Down
16 changes: 4 additions & 12 deletions custom-libp2p/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict'

const Libp2p = require('libp2p')
const IPFS = require('ipfs')
const IPFS = require('../../')
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const WebSocketStar = require('libp2p-websocket-star')
const Bootstrap = require('libp2p-bootstrap')
const SPDY = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
const MPLEX = require('pull-mplex')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')

/**
Expand All @@ -32,11 +31,6 @@ const libp2pBundle = (opts) => {
const peerBook = opts.peerBook
const bootstrapList = opts.config.Bootstrap

// Create our WebSocketStar transport and give it our PeerId, straight from the ipfs node
const wsstar = new WebSocketStar({
id: peerInfo.id
})

// Build and return our libp2p node
return new Libp2p({
peerInfo,
Expand All @@ -49,8 +43,7 @@ const libp2pBundle = (opts) => {
},
modules: {
transport: [
TCP,
wsstar
TCP
],
streamMuxer: [
MPLEX,
Expand All @@ -61,8 +54,7 @@ const libp2pBundle = (opts) => {
],
peerDiscovery: [
MulticastDNS,
Bootstrap,
wsstar.discovery
Bootstrap
],
dht: KadDHT
},
Expand Down
20 changes: 9 additions & 11 deletions custom-libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@
},
"license": "MIT",
"dependencies": {
"ipfs": "file:../../",
"libp2p": "^0.26.2",
"libp2p-bootstrap": "~0.9.7",
"libp2p-kad-dht": "~0.16.0",
"libp2p-mdns": "~0.12.2",
"libp2p-secio": "~0.11.1",
"libp2p-spdy": "~0.13.3",
"libp2p-tcp": "~0.13.0",
"libp2p-websocket-star": "~0.10.2",
"pull-mplex": "~0.1.0"
"libp2p": "^0.27.0-rc.0",
"libp2p-bootstrap": "^0.10.3",
"libp2p-kad-dht": "^0.18.3",
"libp2p-mdns": "^0.13.1",
"libp2p-mplex": "^0.9.3",
"libp2p-secio": "^0.12.2",
"libp2p-spdy": "^0.13.3",
"libp2p-tcp": "^0.14.3"
},
"devDependencies": {
"execa": "^3.2.0"
"execa": "^4.0.0"
}
}
7 changes: 2 additions & 5 deletions custom-libp2p/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ const execa = require('execa')
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const SPDY = require('libp2p-spdy')
const MPLEX = require('pull-mplex')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const multiaddr = require('multiaddr')
const promisify = require('promisify-es6')
const PeerBook = require('peer-book')

async function test () {
let output = ''
Expand All @@ -31,12 +29,11 @@ async function test () {

console.info('Dialling', address)

const peerInfo = new PeerInfo(await promisify(PeerId.create)())
const peerInfo = new PeerInfo(await PeerId.create())
peerInfo.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/0'))

const libp2p = new Libp2p({
peerInfo,
peerBook: new PeerBook(),
modules: {
transport: [
TCP
Expand Down
33 changes: 26 additions & 7 deletions exchange-files-in-browser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This tutorial will help you exchange files between browser nodes and go-ipfs or js-ipfs nodes!

**Note:** As `js-ipfs@0.33.x` currently doesn't support DHT peer discovery, the peer from which you are fetching data should be within the reach (local or in public IP) of the browser node.
**Note:** As `js-ipfs@0.41.x` currently doesn't support DHT peer discovery, the peer from which you are fetching data should be within the reach (local or in public IP) of the browser node.

That being said, we will explain how to circumvent these caveats and once they are fixed, we'll update the tutorial as well.

Expand Down Expand Up @@ -35,9 +35,10 @@ Here's what we are going to be doing:

1. Install a `go-ipfs` or `js-ipfs` node in your machine
2. Make your daemons listen on WebSockets
3. Start the app
4. Dial to a node using WebSockets (your desktop ones)
5. Transfer files between all of your nodes!
3. Start a `libp2p-webrtc-star` signaling server
4. Start the app
5. Dial to a node using WebSockets (your desktop ones)
6. Transfer files between all of your nodes!

Just follow the instructions below and it will be up and running in no time!

Expand Down Expand Up @@ -121,7 +122,25 @@ Daemon is ready

Check the `/ws` in line 5, that means it is listening. Cool.

### 3. Start the app
### 3. Start a `libp2p-webrtc-star` signaling server

This server allows the two browser nodes to talk to each other by doing the initial handshake and network introductions.

First install the `libp2p-webrtc-star` module globally:

```sh
> npm install -g libp2p-webrtc-star
```

This will give you the `webrtc-star` command. Use this to start a signaling server:

```sh
> webrtc-star
```

By default it will listen to all incoming connections on port 13579. Override this with the `--host` and/or `--port` options.

### 4. Start the app

Make sure you're in `js-ipfs/examples/exchange-files-in-browser`.

Expand All @@ -147,7 +166,7 @@ Hit CTRL-C to stop the server

Now go to http://127.0.0.1:12345 in a modern browser and you're on!

### 4. Dial to a node using WebSockets (your desktop ones)
### 5. Dial to a node using WebSockets (your desktop ones)

Make sure you have a daemon running. If you don't, run:

Expand Down Expand Up @@ -179,7 +198,7 @@ Check that you got connected:
[js-libp2p-crypto#105]: https://github.com/libp2p/js-libp2p-crypto/issues/105

### 5. Transfer files between all of your nodes!
### 6. Transfer files between all of your nodes!

Now you can add files through the CLI with:

Expand Down
Loading

0 comments on commit 80f0057

Please sign in to comment.