Skip to content

Commit 31701e2

Browse files
dignifiedquiredaviddias
authored andcommitted
Async Crypto Endeavour (#33)
* refactor: make import and creation async - This allows the use of native key generation in the browser BREAKING CHANGE: This changes the interface of .create, .createFromPrivKey, .createFromPubKey, .createFromJSON
1 parent e08907f commit 31701e2

File tree

8 files changed

+325
-167
lines changed

8 files changed

+325
-167
lines changed

.aegir.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,4 @@ build/Release
2626
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
2727
node_modules
2828

29-
lib
30-
dist
29+
dist

.travis.yml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
sudo: false
22
language: node_js
3-
node_js:
4-
- 4
5-
- 5
6-
- stable
3+
4+
matrix:
5+
include:
6+
- node_js: 4
7+
env: CXX=g++-4.8
8+
- node_js: 6
9+
env:
10+
- SAUCE=true
11+
- CXX=g++-4.8
12+
- node_js: stable
13+
env: CXX=g++-4.8
714

815
# Make sure we have new NPM.
916
before_install:
@@ -14,12 +21,17 @@ script:
1421
- npm test
1522
- npm run coverage
1623

17-
addons:
18-
firefox: 'latest'
19-
2024
before_script:
2125
- export DISPLAY=:99.0
2226
- sh -e /etc/init.d/xvfb start
2327

2428
after_success:
2529
- npm run coverage-publish
30+
31+
addons:
32+
firefox: latest
33+
apt:
34+
sources:
35+
- ubuntu-toolchain-r-test
36+
packages:
37+
- g++-4.8

README.md

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,40 @@
66
[![Coverage Status](https://coveralls.io/repos/github/libp2p/js-peer-id/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-peer-id?branch=master)
77
[![Dependency Status](https://david-dm.org/libp2p/js-peer-id.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-id)
88
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
9+
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
10+
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square)
11+
12+
[![Sauce Test Status](https://saucelabs.com/browser-matrix/ipfs-js-peer-id.svg)](https://saucelabs.com/u/ipfs-js-peer-id)
913

1014
> [IPFS](https://github.com/ipfs/ipfs) Peer ID implementation in JavaScript.
1115
16+
- [Description](#description)
17+
- [Example](#example)
18+
- [Installation](#installation)
19+
- [npm](#npm)
20+
- [Setup](#setup)
21+
- [Node.js](#nodejs)
22+
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
23+
- [Browser: `<script>` Tag](#browser-script-tag)
24+
- [API](#api)
25+
- [Create](#create)
26+
- [`new PeerId(id[, privKey, pubKey])`](#new-peeridid-privkey-pubkey)
27+
- [`create([opts], callback)`](#createopts-callback)
28+
- [Import](#import)
29+
- [`createFromHexString(str)`](#createfromhexstringstr)
30+
- [`createFromBytes(buf)`](#createfrombytesbuf)
31+
- [`createFromB58String(str)`](#createfromb58stringstr)
32+
- [`createFromPubKey(pubKey)`](#createfrompubkeypubkey)
33+
- [`createFromPrivKey(privKey)`](#createfromprivkeyprivkey)
34+
- [`createFromJSON(obj)`](#createfromjsonobj)
35+
- [Export](#export)
36+
- [`toHexString()`](#tohexstring)
37+
- [`toBytes()`](#tobytes)
38+
- [`toB58String()`](#tob58string)
39+
- [`toJSON()`](#tojson)
40+
- [`toPrint()`](#toprint)
41+
- [License](#license)
42+
1243
# Description
1344

1445
Generate, import, and export PeerIDs, for use with [IPFS](https://github.com/ipfs/ipfs).
@@ -26,17 +57,17 @@ to the multihash for ID generation.*
2657
var PeerId = require('peer-id')
2758
var bs58 = require('bs58')
2859

29-
var id = PeerId.create({ bits: 32 })
30-
31-
console.log('id ', id.toB58String())
32-
console.log('priv key ', bs58.encode(id.privKey.bytes))
33-
console.log('pub key ', bs58.encode(id.pubKey.bytes))
60+
PeerId.create({ bits: 1024 }, (err, id) => {
61+
console.log(JSON.stringify(id.toJSON(), null, 2)
62+
})
3463
```
3564
3665
```
37-
id QmeeLFb92nkZJGj3gXLqXrEMzCMYs6uBgQLVNbrcXEvYXk
38-
priv key 6ibrcPAbevzvPpkq6EA6XmLyuhmUrJrEvUfgQDtEiSEPzGnGU8Ejwf6b11DVm6opnFGo
39-
pub key 2BeBZVKJ9RQs4i4LbGv4ReEeuBA5dck2Gje3wt67e44XuyyPq5jE
66+
{
67+
"id": "Qma9T5YraSnpRDZqRR4krcSJabThc8nwZuJV3LercPHufi",
68+
"privKey": "CAAS4AQwggJcAgEAAoGBAMBgbIqyOL26oV3nGPBYrdpbv..",
69+
"pubKey": "CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMBgbIqyOL26oV3nGPBYrdpbvzCY..."
70+
}
4071
```
4172
4273
# Installation
@@ -93,11 +124,14 @@ const PeerId = require('peer-id')
93124
94125
The key format is detailed in [libp2p-crypto](https://github.com/libp2p/js-libp2p-crypto).
95126
96-
### `create([opts])`
127+
### `create([opts], callback)`
97128
98129
Generates a new Peer ID, complete with public/private keypair.
99130
100131
- `opts: Object`: Default: `{bits: 2048}`
132+
- `callback: Function`
133+
134+
Calls back `callback` with `err, id`.
101135
102136
## Import
103137
@@ -114,10 +148,14 @@ Creates a Peer ID from a Base58 string representing the key's multihash.
114148
115149
### `createFromPubKey(pubKey)`
116150
151+
- `publicKey: Buffer`
152+
117153
Creates a Peer ID from a buffer containing a public key.
118154
119155
### `createFromPrivKey(privKey)`
120156
157+
- `privKey: Buffer`
158+
121159
Creates a Peer ID from a buffer containing a private key.
122160
123161
### `createFromJSON(obj)`
@@ -126,7 +164,6 @@ Creates a Peer ID from a buffer containing a private key.
126164
- `obj.pubKey: String` - The public key in protobuf format, encoded in 'base64'
127165
- `obj.privKey: String` - The private key in protobuf format, encoded in 'base 64'
128166
129-
130167
## Export
131168
132169
### `toHexString()`

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
"name": "peer-id",
33
"version": "0.7.0",
44
"description": "IPFS Peer Id implementation in Node.js",
5-
"main": "lib/index.js",
5+
"main": "src/index.js",
66
"bin": "src/bin.js",
7-
"jsnext:main": "src/index.js",
87
"scripts": {
98
"lint": "aegir-lint",
109
"build": "aegir-build",
@@ -27,24 +26,25 @@
2726
"test"
2827
],
2928
"engines": {
30-
"node": "^4.3.0"
29+
"node": ">=4.0.0"
3130
},
3231
"bugs": {
33-
"url": "https://github.com/diasdavid/js-peer-id/issues"
32+
"url": "https://github.com/libp2p/js-peer-id/issues"
3433
},
35-
"homepage": "https://github.com/diasdavid/js-peer-id",
34+
"homepage": "https://github.com/libp2p/js-peer-id",
3635
"devDependencies": {
37-
"aegir": "^8.0.0",
36+
"aegir": "^9.0.1",
3837
"chai": "^3.5.0",
3938
"pre-commit": "^1.1.3"
4039
},
4140
"dependencies": {
42-
"libp2p-crypto": "^0.6.1",
41+
"async": "^2.0.1",
42+
"libp2p-crypto": "^0.7.0",
4343
"multihashes": "^0.2.2"
4444
},
4545
"repository": {
4646
"type": "git",
47-
"url": "https://github.com/diasdavid/js-peer-id.git"
47+
"url": "https://github.com/libp2p/js-peer-id.git"
4848
},
4949
"contributors": [
5050
"David Dias <[email protected]>",
@@ -53,4 +53,4 @@
5353
"dignifiedquire <[email protected]>",
5454
"nginnever <[email protected]>"
5555
]
56-
}
56+
}

src/bin.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44

55
const PeerId = require('./index.js')
66

7-
console.log(JSON.stringify(PeerId.create().toJSON(), null, ' '))
7+
PeerId.create((err, id) => {
8+
if (err) {
9+
throw err
10+
}
11+
12+
console.log(JSON.stringify(id.toJSON(), null, 2))
13+
})

0 commit comments

Comments
 (0)