Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

Commit

Permalink
Async Crypto Endeavour (#22)
Browse files Browse the repository at this point in the history
Async Crypto Endeavour
  • Loading branch information
dignifiedquire authored and daviddias committed Nov 3, 2016
1 parent 35c7ae4 commit 5dc1162
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 112 deletions.
16 changes: 0 additions & 16 deletions .aegir.js

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ build/Release
node_modules

dist
lib
22 changes: 19 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
sudo: false
language: node_js
node_js:
- 4
- 5

matrix:
include:
- node_js: 4
env: CXX=g++-4.8
- node_js: 6
env:
- SAUCE=true
- CXX=g++-4.8
- node_js: stable
env: CXX=g++-4.8

# Make sure we have new NPM.
before_install:
Expand All @@ -22,3 +30,11 @@ before_script:

after_success:
- npm run coverage-publish

addons:
firefox: latest
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
122 changes: 82 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,66 @@
peer-info JavaScript implementation
===================================
# js-peer-info

[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Build Status](https://travis-ci.org/diasdavid/js-peer-info.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-peer-info)
[![Coverage Status](https://coveralls.io/repos/github/diasdavid/js-peer-info/badge.svg?branch=master)](https://coveralls.io/github/diasdavid/js-peer-info?branch=master)
[![Dependency Status](https://david-dm.org/diasdavid/js-peer-info.svg?style=flat-square)](https://david-dm.org/diasdavid/js-peer-info)
[![Build Status](https://travis-ci.org/libp2p/js-peer-info.svg?style=flat-square)](https://travis-ci.org/libp2p/js-peer-info)
[![Coverage Status](https://coveralls.io/repos/github/libp2p/js-peer-info/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-peer-info?branch=master)
[![Dependency Status](https://david-dm.org/libp2p/js-peer-info.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-info)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square)

[![Sauce Test Status](https://saucelabs.com/browser-matrix/ipfs-js-peer-info.svg)](https://saucelabs.com/u/ipfs-js-peer-info)

> A PeerInfo object contains information about a
> [PeerID](https://github.com/libp2p/js-peer-id) and its
> [multiaddrs](https://github.com/libp2p/js-multiaddr). This module is used by
> [IPFS](https://github.com/ipfs/ipfs) and
> [libp2p](https://github.com/libp2p/js-libp2p).
# Example
## Table of Contents

- [Installation](#installation)
- [npm](#npm)
- [Node.JS, Browserify, Webpack](#nodejs-browserify-webpack)
- [Browser: `<script>` Tag](#browser-script-tag)
- [Usage](#usage)
- [API](#api)
- [`PeerInfo.create([id, ] callback)`](#peerinfocreateid-callback)
- [`new PeerInfo(id)`](#new-peerinfoid)
- [`multiaddrs`](#multiaddrs)
- [`multiaddr.add(addr)`](#multiaddraddaddr)
- [`multiaddr.addSafe(addr)`](#multiaddraddsafeaddr)
- [`multiaddr.rm(addr)`](#multiaddrrmaddr)
- [`multiaddr.replace(existing, fresh)`](#multiaddrreplaceexisting-fresh)
- [Contribute](#contribute)
- [License](#license)

## Installation

### npm

```sh
> npm i peer-info
```

### Node.JS, Browserify, Webpack

```js
const PeerInfo = require('peer-info')
```

### Browser: `<script>` Tag

Loading this module through a script tag will make the `PeerInfo` obj available in the global namespace.

```html
<script src="https://unpkg.com/peer-info/dist/index.min.js"></script>
<!-- OR -->
<script src="https://unpkg.com/peer-info/dist/index.js"></script>
```

## Usage

```js
const PeerInfo = require('peer-info')
Expand All @@ -32,31 +78,41 @@ peer.multiaddr.add(multiaddr('/ip4/1.2.3.4/udp/8001'))
peer.multiaddr.add(multiaddr('/sonic/bfsk/697/1209'))
```

# API
## API

```js
const PeerInfo = require('peer-info')
```

## const peer = new PeerInfo()
### `PeerInfo.create([id, ] callback)`

Creates a new PeerInfo instance and also generates a new underlying
[PeerID](https://github.com/libp2p/js-peer-id) for it.
- `id: PeerID`, optional
- `callback: Function`

## const peer = new PeerInfo(peerId)
Creates a new PeerInfo instance and if no `id` is passed it
generates a new underlying [PeerID](https://github.com/libp2p/js-peer-id)
for it.

### `new PeerInfo(id)`

- `id: PeerID`

Creates a new PeerInfo instance from an existing PeerID.

## peer.multiaddrs
### `multiaddrs`

A list of multiaddresses instances that `peer` can be reached at.

## peer.multiaddr.add(addr)
### `multiaddr.add(addr)`

- `addr: Multiaddr`

Adds a new multiaddress that `peer` can be reached at. `addr` is an instance of
a [multiaddr](https://github.com/libp2p/js-multiaddr).

## peer.multiaddr.addSafe(addr)
### `multiaddr.addSafe(addr)`

- `addr: Multiaddr`

The `addSafe` call, in comparison to `add`, will only add the multiaddr to
`multiaddrs` if the same multiaddr tries to be added twice.
Expand All @@ -67,40 +123,26 @@ peers which will not provide a useful multiaddr to be shared to the rest of the
network (e.g. a multiaddr referring to a peer inside a LAN being shared to the
outside world).

## peer.multiaddr.rm(addr)
### `multiaddr.rm(addr)`

- `addr: Multiaddr`

Removes a multiaddress instance `addr` from `peer`.

## peer.multiaddr.replace(existing, fresh)
### `multiaddr.replace(existing, fresh)`

- `existing: Multiaddr`
- `fresh: Multiaddr`

Removes the array of multiaddresses `existing` from `peer`, and adds the array
of multiaddresses `fresh`.

## Contribute

# Installation

## npm

```sh
> npm i peer-info
```

## Node.JS, Browserify, Webpack

```JavaScript
var PeerInfo = require('peer-info')
```

## Browser: `<script>` Tag

Loading this module through a script tag will make the `PeerInfo` obj available in the global namespace.
PRs accepted.

```html
<script src="https://unpkg.com/peer-info/dist/index.min.js"></script>
<!-- OR -->
<script src="https://unpkg.com/peer-info/dist/index.js"></script>
```
Small note: If editing the Readme, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.

# License
## License

MIT
[MIT © David Dias](LICENSE)
22 changes: 10 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
"name": "peer-info",
"version": "0.7.1",
"description": "IPFS Peer abstraction JavaScript implementation",
"main": "lib/index.js",
"jsnext:main": "src/index.js",
"main": "src/index.js",
"scripts": {
"lint": "aegir-lint",
"build": "aegir-build",
"test": "aegir-test",
"test:node": "aegir-test node",
"test:browser": "aegir-test browser",
"test:node": "aegir-test --env node",
"test:browser": "aegir-test --env browser",
"release": "aegir-release",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
Expand All @@ -18,34 +17,33 @@
},
"repository": {
"type": "git",
"url": "https://github.com/diasdavid/js-peer-info.git"
"url": "https://github.com/libp2p/js-peer-info.git"
},
"keywords": [
"IPFS"
],
"engines": {
"node": "^4.3.0"
"node": ">=4.0.0"
},
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"bugs": {
"url": "https://github.com/diasdavid/js-peer-info/issues"
"url": "https://github.com/libp2p/js-peer-info/issues"
},
"homepage": "https://github.com/diasdavid/js-peer-info",
"homepage": "https://github.com/libp2p/js-peer-info",
"pre-commit": [
"lint",
"test"
],
"devDependencies": {
"aegir": "^8.0.0",
"aegir": "^9.0.1",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
"pre-commit": "^1.1.3"
},
"dependencies": {
"babel-runtime": "^6.11.6",
"multiaddr": "^2.0.3",
"peer-id": "^0.7.0"
"peer-id": "^0.8.0"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
Expand All @@ -55,4 +53,4 @@
"dignifiedquire <dignifiedquire@gmail.com>",
"greenkeeperio-bot <support@greenkeeper.io>"
]
}
}
32 changes: 25 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const Id = require('peer-id')
const multiaddr = require('multiaddr')

exports = module.exports = Peer
exports = module.exports = PeerInfo

function ensureMultiaddr (addr) {
if (multiaddr.isMultiaddr(addr)) {
Expand All @@ -14,17 +14,17 @@ function ensureMultiaddr (addr) {
}

// Peer represents a peer on the IPFS network
function Peer (peerId) {
if (!(this instanceof Peer)) {
return new Peer(peerId)
function PeerInfo (peerId) {
if (!(this instanceof PeerInfo)) {
return new PeerInfo(peerId)
}

if (!peerId) {
this.id = Id.create()
} else {
this.id = peerId
throw new Error('Missing peerId. Use Peer.create(cb) to create one')

This comment has been minimized.

Copy link
@parkan

parkan Nov 15, 2016

I am guessing this was changed because this function would need to be made async otherwise (breaks docs here: https://github.com/libp2p/js-peer-info#usage)

However, that surfaced a potentially bigger issue. What are the semantics of adding a multiaddr containing a public key to a PeerInfo? I previously assumed, based on the API, that

const peer = new PeerInfo()
peer.multiaddr.add("/ip4/54.146.227.119/tcp/9001/ipfs/QmTVsocFCSEdPyM8dZ734GRhjvvmYyL9fShyezVkbPj17E")

Would populate the pubkey on the peerInfo. However, at least with the current version this doesn't seem to be the case:

> function doit(err, peerId){
... const pi = PeerInfo(peerId)
... pi.multiaddr.add(multiaddr('/ip4/54.146.227.119/tcp/9001/ipfs/QmTVsocFCSEdPyM8dZ734GRhjvvmYyL9fShyezVkbPj17E'))
... console.log(pi)
... }
undefined
> PeerId.create(doit)
undefined
> PeerInfo {
  id:
   PeerId {
     id: <Buffer 12 20 7e 2c bc 8d 10 a9 68 af b7 e4 f5 dc 73 16 80 c9 3a 6f be 30 4a 7e b6 85 e2 d5 b7 3c 3e ba da 83>,
     _privKey: RsaPrivateKey { _key: [Object], _publicKey: [Object] },
     _pubKey: undefined },
  multiaddrs: [ <Multiaddr 043692e377062329a5032212204ca777e3b2d5d6dc5524ddb742b3b3fd3ce19e8ef11e3e6172dcc42f73638859 - /ip4/54.146.227.119/tcp/9001/ipfs/QmTVsocFCSEdPyM8dZ734GRhjvvmYyL9fShyezVkbPj17E> ],
  multiaddr:
   { add: [Function],
     addSafe: [Function],
     rm: [Function],
     replace: [Function] } }

What's the correct way to inflate a multiaddr into a PeerInfo?

This comment has been minimized.

Copy link
@parkan

parkan Nov 15, 2016

(I realize that I can myself split on slashes, find the bit after ipfs, manually createFromB58String the next token, create the PeerInfo from that, then add the multiaddr prefix, but that definitely doesn't seem like the right way to peform a super common operation)

This comment has been minimized.

Copy link
@parkan

parkan Nov 15, 2016

Ok, checked a bit more and it seems like this is the wrong way to create a peerid from a multiaddr, because the actual peerid bytes in id.id remain unchanged from the random new one passed to the constructor after we add the multiaddress. So we have a totally unrelated key's signature associated with the multiaddress... this seems weird/bad? Clearly at least one of the two keys has to be ignored, but which one?

}

this.id = peerId

this.multiaddrs = []
const observedMultiaddrs = []

Expand Down Expand Up @@ -91,3 +91,21 @@ function Peer (peerId) {
// TODO: add features to fetch multiaddr using filters
// look at https://github.com/whyrusleeping/js-mafmt/blob/master/src/index.js
}

PeerInfo.create = (id, callback) => {
if (typeof id === 'function') {
callback = id
id = null

Id.create((err, id) => {
if (err) {
return callback(err)
}

callback(null, new PeerInfo(id))
})
return
}

callback(null, new PeerInfo(id))
}
Loading

0 comments on commit 5dc1162

Please sign in to comment.