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
This commit is contained in:
Friedel Ziegelmayer 2016-11-03 08:51:29 +01:00 committed by David Dias
parent e08907ff94
commit 31701e236d
8 changed files with 322 additions and 164 deletions

View File

@ -1,16 +0,0 @@
'use strict'
const path = require('path')
module.exports = {
webpack: {
resolve: {
alias: {
'node-forge': path.resolve(
path.dirname(require.resolve('libp2p-crypto')),
'../vendor/forge.bundle.js'
)
}
}
}
}

1
.gitignore vendored
View File

@ -26,5 +26,4 @@ build/Release
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
lib
dist

View File

@ -1,9 +1,16 @@
sudo: false
language: node_js
node_js:
- 4
- 5
- stable
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:
@ -14,12 +21,17 @@ script:
- npm test
- npm run coverage
addons:
firefox: 'latest'
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
after_success:
- npm run coverage-publish
addons:
firefox: latest
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8

View File

@ -6,9 +6,40 @@
[![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)
[![Dependency Status](https://david-dm.org/libp2p/js-peer-id.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-id)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
![](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-id.svg)](https://saucelabs.com/u/ipfs-js-peer-id)
> [IPFS](https://github.com/ipfs/ipfs) Peer ID implementation in JavaScript.
- [Description](#description)
- [Example](#example)
- [Installation](#installation)
- [npm](#npm)
- [Setup](#setup)
- [Node.js](#nodejs)
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
- [Browser: `<script>` Tag](#browser-script-tag)
- [API](#api)
- [Create](#create)
- [`new PeerId(id[, privKey, pubKey])`](#new-peeridid-privkey-pubkey)
- [`create([opts], callback)`](#createopts-callback)
- [Import](#import)
- [`createFromHexString(str)`](#createfromhexstringstr)
- [`createFromBytes(buf)`](#createfrombytesbuf)
- [`createFromB58String(str)`](#createfromb58stringstr)
- [`createFromPubKey(pubKey)`](#createfrompubkeypubkey)
- [`createFromPrivKey(privKey)`](#createfromprivkeyprivkey)
- [`createFromJSON(obj)`](#createfromjsonobj)
- [Export](#export)
- [`toHexString()`](#tohexstring)
- [`toBytes()`](#tobytes)
- [`toB58String()`](#tob58string)
- [`toJSON()`](#tojson)
- [`toPrint()`](#toprint)
- [License](#license)
# Description
Generate, import, and export PeerIDs, for use with [IPFS](https://github.com/ipfs/ipfs).
@ -26,17 +57,17 @@ to the multihash for ID generation.*
var PeerId = require('peer-id')
var bs58 = require('bs58')
var id = PeerId.create({ bits: 32 })
console.log('id ', id.toB58String())
console.log('priv key ', bs58.encode(id.privKey.bytes))
console.log('pub key ', bs58.encode(id.pubKey.bytes))
PeerId.create({ bits: 1024 }, (err, id) => {
console.log(JSON.stringify(id.toJSON(), null, 2)
})
```
```
id QmeeLFb92nkZJGj3gXLqXrEMzCMYs6uBgQLVNbrcXEvYXk
priv key 6ibrcPAbevzvPpkq6EA6XmLyuhmUrJrEvUfgQDtEiSEPzGnGU8Ejwf6b11DVm6opnFGo
pub key 2BeBZVKJ9RQs4i4LbGv4ReEeuBA5dck2Gje3wt67e44XuyyPq5jE
{
"id": "Qma9T5YraSnpRDZqRR4krcSJabThc8nwZuJV3LercPHufi",
"privKey": "CAAS4AQwggJcAgEAAoGBAMBgbIqyOL26oV3nGPBYrdpbv..",
"pubKey": "CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMBgbIqyOL26oV3nGPBYrdpbvzCY..."
}
```
# Installation
@ -93,11 +124,14 @@ const PeerId = require('peer-id')
The key format is detailed in [libp2p-crypto](https://github.com/libp2p/js-libp2p-crypto).
### `create([opts])`
### `create([opts], callback)`
Generates a new Peer ID, complete with public/private keypair.
- `opts: Object`: Default: `{bits: 2048}`
- `callback: Function`
Calls back `callback` with `err, id`.
## Import
@ -114,10 +148,14 @@ Creates a Peer ID from a Base58 string representing the key's multihash.
### `createFromPubKey(pubKey)`
- `publicKey: Buffer`
Creates a Peer ID from a buffer containing a public key.
### `createFromPrivKey(privKey)`
- `privKey: Buffer`
Creates a Peer ID from a buffer containing a private key.
### `createFromJSON(obj)`
@ -126,7 +164,6 @@ Creates a Peer ID from a buffer containing a private key.
- `obj.pubKey: String` - The public key in protobuf format, encoded in 'base64'
- `obj.privKey: String` - The private key in protobuf format, encoded in 'base 64'
## Export
### `toHexString()`

View File

@ -2,9 +2,8 @@
"name": "peer-id",
"version": "0.7.0",
"description": "IPFS Peer Id implementation in Node.js",
"main": "lib/index.js",
"main": "src/index.js",
"bin": "src/bin.js",
"jsnext:main": "src/index.js",
"scripts": {
"lint": "aegir-lint",
"build": "aegir-build",
@ -27,24 +26,25 @@
"test"
],
"engines": {
"node": "^4.3.0"
"node": ">=4.0.0"
},
"bugs": {
"url": "https://github.com/diasdavid/js-peer-id/issues"
"url": "https://github.com/libp2p/js-peer-id/issues"
},
"homepage": "https://github.com/diasdavid/js-peer-id",
"homepage": "https://github.com/libp2p/js-peer-id",
"devDependencies": {
"aegir": "^8.0.0",
"aegir": "^9.0.1",
"chai": "^3.5.0",
"pre-commit": "^1.1.3"
},
"dependencies": {
"libp2p-crypto": "^0.6.1",
"async": "^2.0.1",
"libp2p-crypto": "^0.7.0",
"multihashes": "^0.2.2"
},
"repository": {
"type": "git",
"url": "https://github.com/diasdavid/js-peer-id.git"
"url": "https://github.com/libp2p/js-peer-id.git"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",

View File

@ -4,4 +4,10 @@
const PeerId = require('./index.js')
console.log(JSON.stringify(PeerId.create().toJSON(), null, ' '))
PeerId.create((err, id) => {
if (err) {
throw err
}
console.log(JSON.stringify(id.toJSON(), null, 2))
})

View File

@ -7,28 +7,25 @@
const mh = require('multihashes')
const crypto = require('libp2p-crypto')
const assert = require('assert')
const waterfall = require('async/waterfall')
class PeerId {
constructor (id, privKey, pubKey) {
assert(Buffer.isBuffer(id), 'invalid id provided')
if (pubKey) {
assert(id.equals(pubKey.hash()), 'inconsistent arguments')
}
if (privKey) {
assert(id.equals(privKey.public.hash()), 'inconsistent arguments')
}
if (privKey && pubKey) {
assert(privKey.public.bytes.equals(pubKey.bytes), 'inconsistent arguments')
}
this.id = id
this.privKey = privKey
this._privKey = privKey
this._pubKey = pubKey
}
get privKey () {
return this._privKey
}
get pubKey () {
if (this._pubKey) {
return this._pubKey
@ -88,13 +85,26 @@ exports = module.exports = PeerId
exports.Buffer = Buffer
// generation
exports.create = function (opts) {
exports.create = function (opts, callback) {
if (typeof opts === 'function') {
callback = opts
opts = {}
}
opts = opts || {}
opts.bits = opts.bits || 2048
const privKey = crypto.generateKeyPair('RSA', opts.bits)
waterfall([
(cb) => crypto.generateKeyPair('RSA', opts.bits, cb),
(privKey, cb) => privKey.public.hash((err, digest) => {
cb(err, digest, privKey)
})
], (err, digest, privKey) => {
if (err) {
return callback(err)
}
return new PeerId(privKey.public.hash(), privKey)
callback(null, new PeerId(digest, privKey))
})
}
exports.createFromHexString = function (str) {
@ -110,39 +120,94 @@ exports.createFromB58String = function (str) {
}
// Public Key input will be a buffer
exports.createFromPubKey = function (key) {
exports.createFromPubKey = function (key, callback) {
let buf = key
if (typeof buf === 'string') {
buf = new Buffer(key, 'base64')
}
if (typeof callback !== 'function') {
throw new Error('callback is required')
}
const pubKey = crypto.unmarshalPublicKey(buf)
return new PeerId(pubKey.hash(), null, pubKey)
pubKey.hash((err, digest) => {
if (err) {
return callback(err)
}
callback(null, new PeerId(digest, null, pubKey))
})
}
// Private key input will be a string
exports.createFromPrivKey = function (key) {
exports.createFromPrivKey = function (key, callback) {
let buf = key
if (typeof buf === 'string') {
buf = new Buffer(key, 'base64')
}
const privKey = crypto.unmarshalPrivateKey(buf)
return new PeerId(privKey.public.hash(), privKey)
if (typeof callback !== 'function') {
throw new Error('callback is required')
}
exports.createFromJSON = function (obj) {
let priv
let pub
if (obj.privKey) {
priv = crypto.unmarshalPrivateKey(new Buffer(obj.privKey, 'base64'))
waterfall([
(cb) => crypto.unmarshalPrivateKey(buf, cb),
(privKey, cb) => privKey.public.hash((err, digest) => {
cb(err, digest, privKey)
})
], (err, digest, privKey) => {
if (err) {
return callback(err)
}
if (obj.pubKey) {
pub = crypto.unmarshalPublicKey(new Buffer(obj.pubKey, 'base64'))
callback(null, new PeerId(digest, privKey))
})
}
return new PeerId(mh.fromB58String(obj.id), priv, pub)
exports.createFromJSON = function (obj, callback) {
if (typeof callback !== 'function') {
throw new Error('callback is required')
}
const id = mh.fromB58String(obj.id)
const rawPrivKey = obj.privKey && new Buffer(obj.privKey, 'base64')
const rawPubKey = obj.pubKey && new Buffer(obj.pubKey, 'base64')
const pub = rawPubKey && crypto.unmarshalPublicKey(rawPubKey)
if (rawPrivKey) {
waterfall([
(cb) => crypto.unmarshalPrivateKey(rawPrivKey, cb),
(priv, cb) => priv.public.hash((err, digest) => {
cb(err, digest, priv)
}),
(privDigest, priv, cb) => {
if (pub) {
pub.hash((err, pubDigest) => {
cb(err, privDigest, priv, pubDigest)
})
} else {
cb(null, privDigest, priv)
}
}
], (err, privDigest, priv, pubDigest) => {
if (err) {
return callback(err)
}
if (pub && !privDigest.equals(pubDigest)) {
return callback(new Error('Public and private key do not match'))
}
if (id && !privDigest.equals(id)) {
return callback(new Error('Id and private key do not match'))
}
callback(null, new PeerId(id, priv, pub))
})
} else {
callback(null, new PeerId(id, null, pub))
}
}
function toB64Opt (val) {

View File

@ -1,9 +1,11 @@
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'
const expect = require('chai').expect
const crypto = require('libp2p-crypto')
const mh = require('multihashes')
const parallel = require('async/parallel')
const PeerId = require('../src')
@ -19,9 +21,12 @@ describe('PeerId', () => {
expect(PeerId).to.throw(Error)
})
it('create a new id', () => {
const id = PeerId.create()
it('create a new id', (done) => {
PeerId.create((err, id) => {
expect(err).to.not.exist
expect(id.toB58String().length).to.equal(46)
done()
})
})
it('recreate an Id from Hex string', () => {
@ -39,38 +44,60 @@ describe('PeerId', () => {
expect(testIdB58String).to.equal(id.toB58String())
})
it('Recreate from a Public Key', () => {
const id = PeerId.createFromPubKey(testId.pubKey)
it('Recreate from a Public Key', (done) => {
PeerId.createFromPubKey(testId.pubKey, (err, id) => {
expect(err).to.not.exist
expect(testIdB58String).to.equal(id.toB58String())
done()
})
})
it('Recreate from a Private Key', () => {
const id = PeerId.createFromPrivKey(testId.privKey)
it('Recreate from a Private Key', (done) => {
PeerId.createFromPrivKey(testId.privKey, (err, id) => {
expect(err).to.not.exist
expect(testIdB58String).to.equal(id.toB58String())
const id2 = PeerId.createFromPrivKey(new Buffer(testId.privKey, 'base64'))
const encoded = new Buffer(testId.privKey, 'base64')
PeerId.createFromPrivKey(encoded, (err, id2) => {
expect(err).to.not.exist
expect(testIdB58String).to.equal(id2.toB58String())
done()
})
})
})
it('Compare generated ID with one created from PubKey', () => {
const id1 = PeerId.create()
const id2 = PeerId.createFromPubKey(id1.marshalPubKey())
it('Compare generated ID with one created from PubKey', (done) => {
PeerId.create((err, id1) => {
expect(err).to.not.exist
PeerId.createFromPubKey(id1.marshalPubKey(), (err, id2) => {
expect(err).to.not.exist
expect(id1.id).to.be.eql(id2.id)
done()
})
})
})
it('Non-default # of bits', () => {
const shortId = PeerId.create({ bits: 128 })
const longId = PeerId.create({ bits: 256 })
it('Non-default # of bits', (done) => {
PeerId.create({ bits: 1024 }, (err, shortId) => {
expect(err).to.not.exist
PeerId.create({ bits: 4096 }, (err, longId) => {
expect(err).to.not.exist
expect(shortId.privKey.bytes.length).is.below(longId.privKey.bytes.length)
done()
})
})
})
it('Pretty printing', () => {
const id = PeerId.createFromPrivKey(testId.privKey)
const out = id.toPrint()
expect(out.id).to.equal(testIdB58String)
expect(out.privKey).to.equal(testId.privKey)
expect(out.pubKey).to.equal(testId.pubKey)
it('Pretty printing', (done) => {
PeerId.create((err, id1) => {
expect(err).to.not.exist
PeerId.createFromPrivKey(id1.toPrint().privKey, (err, id2) => {
expect(err).to.not.exist
expect(id1.toPrint()).to.be.eql(id2.toPrint())
done()
})
})
})
it('toBytes', () => {
@ -78,90 +105,118 @@ describe('PeerId', () => {
expect(id.toBytes().toString('hex')).to.equal(testIdBytes.toString('hex'))
})
describe('toJSON', () => {
it('full node', () => {
const id = PeerId.create({bits: 64})
describe('fromJSON', () => {
it('full node', (done) => {
PeerId.create({bits: 1024}, (err, id) => {
expect(err).to.not.exist
PeerId.createFromJSON(id.toJSON(), (err, other) => {
expect(err).to.not.exist
expect(
id.toB58String()
).to.equal(
PeerId.createFromJSON(id.toJSON()).toB58String()
other.toB58String()
)
expect(
id.privKey.bytes
).to.deep.equal(
PeerId.createFromJSON(id.toJSON()).privKey.bytes
other.privKey.bytes
)
expect(
id.pubKey.bytes
).to.deep.equal(
PeerId.createFromJSON(id.toJSON()).pubKey.bytes
other.pubKey.bytes
)
done()
})
})
})
it('only id', () => {
const key = crypto.generateKeyPair('RSA', 64)
const id = PeerId.createFromBytes(key.public.hash())
it('only id', (done) => {
crypto.generateKeyPair('RSA', 1024, (err, key) => {
expect(err).to.not.exist
key.public.hash((err, digest) => {
expect(err).to.not.exist
const id = PeerId.createFromBytes(digest)
expect(id.privKey).to.not.exist
expect(id.pubKey).to.not.exist
PeerId.createFromJSON(id.toJSON(), (err, other) => {
expect(err).to.not.exist
expect(
id.toB58String()
).to.equal(
PeerId.createFromJSON(id.toJSON()).toB58String()
other.toB58String()
)
expect(id.privKey).to.not.exist
expect(id.pubKey).to.not.exist
done()
})
})
})
})
it('go interop', () => {
const id = PeerId.createFromJSON(goId)
it('go interop', (done) => {
PeerId.createFromJSON(goId, (err, id) => {
expect(err).to.not.exist
id.privKey.public.hash((err, digest) => {
expect(err).to.not.exist
expect(
mh.toB58String(id.privKey.public.hash())
mh.toB58String(digest)
).to.be.eql(
goId.id
)
done()
})
})
})
})
describe('throws on inconsistent data', () => {
const k1 = crypto.generateKeyPair('RSA', 64)
const k2 = crypto.generateKeyPair('RSA', 64)
const k3 = crypto.generateKeyPair('RSA', 64)
let k1, k2, k3
before((done) => {
parallel([
(cb) => crypto.generateKeyPair('RSA', 1024, cb),
(cb) => crypto.generateKeyPair('RSA', 1024, cb),
(cb) => crypto.generateKeyPair('RSA', 1024, cb)
], (err, keys) => {
if (err) {
return done(err)
}
it('missmatch id - private key', () => {
expect(
() => new PeerId(k1.public.hash(), k2)
).to.throw(
/inconsistent arguments/
)
k1 = keys[0]
k2 = keys[1]
k3 = keys[2]
done()
})
})
it('missmatch id - public key', () => {
it('missmatch private - public key', (done) => {
k1.public.hash((err, digest) => {
expect(err).to.not.exist
expect(
() => new PeerId(k1.public.hash(), null, k2.public)
() => new PeerId(digest, k1, k2.public)
).to.throw(
/inconsistent arguments/
)
done()
})
})
it('missmatch private - public key', () => {
it('missmatch id - private - public key', (done) => {
k1.public.hash((err, digest) => {
expect(err).to.not.exist
expect(
() => new PeerId(k1.public.hash(), k1, k2.public)
() => new PeerId(digest, k1, k3.public)
).to.throw(
/inconsistent arguments/
)
done()
})
it('missmatch id - private - public key', () => {
expect(
() => new PeerId(k1.public.hash(), k1, k3.public)
).to.throw(
/inconsistent arguments/
)
})
it('invalid id', () => {
expect(
() => new PeerId(k1.public.hash().toString())
() => new PeerId('hello world')
).to.throw(
/invalid id/
)