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 # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules node_modules
lib
dist dist

View File

@ -1,9 +1,16 @@
sudo: false sudo: false
language: node_js language: node_js
node_js:
- 4 matrix:
- 5 include:
- stable - 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. # Make sure we have new NPM.
before_install: before_install:
@ -14,12 +21,17 @@ script:
- npm test - npm test
- npm run coverage - npm run coverage
addons:
firefox: 'latest'
before_script: before_script:
- export DISPLAY=:99.0 - export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start - sh -e /etc/init.d/xvfb start
after_success: after_success:
- npm run coverage-publish - 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) [![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) [![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) [![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. > [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 # Description
Generate, import, and export PeerIDs, for use with [IPFS](https://github.com/ipfs/ipfs). 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 PeerId = require('peer-id')
var bs58 = require('bs58') var bs58 = require('bs58')
var id = PeerId.create({ bits: 32 }) PeerId.create({ bits: 1024 }, (err, id) => {
console.log(JSON.stringify(id.toJSON(), null, 2)
console.log('id ', id.toB58String()) })
console.log('priv key ', bs58.encode(id.privKey.bytes))
console.log('pub key ', bs58.encode(id.pubKey.bytes))
``` ```
``` ```
id QmeeLFb92nkZJGj3gXLqXrEMzCMYs6uBgQLVNbrcXEvYXk {
priv key 6ibrcPAbevzvPpkq6EA6XmLyuhmUrJrEvUfgQDtEiSEPzGnGU8Ejwf6b11DVm6opnFGo "id": "Qma9T5YraSnpRDZqRR4krcSJabThc8nwZuJV3LercPHufi",
pub key 2BeBZVKJ9RQs4i4LbGv4ReEeuBA5dck2Gje3wt67e44XuyyPq5jE "privKey": "CAAS4AQwggJcAgEAAoGBAMBgbIqyOL26oV3nGPBYrdpbv..",
"pubKey": "CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMBgbIqyOL26oV3nGPBYrdpbvzCY..."
}
``` ```
# Installation # 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). 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. Generates a new Peer ID, complete with public/private keypair.
- `opts: Object`: Default: `{bits: 2048}` - `opts: Object`: Default: `{bits: 2048}`
- `callback: Function`
Calls back `callback` with `err, id`.
## Import ## Import
@ -114,10 +148,14 @@ Creates a Peer ID from a Base58 string representing the key's multihash.
### `createFromPubKey(pubKey)` ### `createFromPubKey(pubKey)`
- `publicKey: Buffer`
Creates a Peer ID from a buffer containing a public key. Creates a Peer ID from a buffer containing a public key.
### `createFromPrivKey(privKey)` ### `createFromPrivKey(privKey)`
- `privKey: Buffer`
Creates a Peer ID from a buffer containing a private key. Creates a Peer ID from a buffer containing a private key.
### `createFromJSON(obj)` ### `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.pubKey: String` - The public key in protobuf format, encoded in 'base64'
- `obj.privKey: String` - The private key in protobuf format, encoded in 'base 64' - `obj.privKey: String` - The private key in protobuf format, encoded in 'base 64'
## Export ## Export
### `toHexString()` ### `toHexString()`

View File

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

View File

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

View File

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