Compare commits

...

9 Commits

Author SHA1 Message Date
Vasco Santos
9922f85693 chore: release version v0.12.5 2019-09-25 16:12:25 +02:00
Vasco Santos
843d35e1da chore: update contributors 2019-09-25 16:12:25 +02:00
Topper Bowers
ceeff130fa feat: inline public key handling for the 0.12.x line (#102) 2019-09-25 16:03:01 +02:00
Jacob Heun
8f4027b136
chore: release version v0.12.4 2019-07-23 14:38:20 +02:00
Jacob Heun
90835484fe
chore: update contributors 2019-07-23 14:38:20 +02:00
Jacob Heun
8bcd9a880e
chore: revert async dep change (#97)
* chore: revert async dep change

* chore: update async to 2.6.3
2019-07-23 13:32:46 +01:00
Jacob Heun
0bce8f7a87
chore: release version v0.12.3 2019-07-11 18:17:27 +01:00
Jacob Heun
8f15adbce5
chore: update contributors 2019-07-11 18:17:27 +01:00
Arve Knudsen
fd072213b7
feat(peerid): support creating from secp256k1; harmonize algo with Go 2019-07-11 18:11:05 +01:00
7 changed files with 163 additions and 66 deletions

View File

@ -22,7 +22,6 @@ jobs:
- stage: check - stage: check
script: script:
- npx aegir commitlint --travis
- npx aegir dep-check - npx aegir dep-check
- npm run lint - npm run lint

View File

@ -1,3 +1,28 @@
<a name="0.12.5"></a>
## [0.12.5](https://github.com/libp2p/js-peer-id/compare/v0.12.4...v0.12.5) (2019-09-25)
### Features
* inline public key handling for the 0.12.x line ([#102](https://github.com/libp2p/js-peer-id/issues/102)) ([ceeff13](https://github.com/libp2p/js-peer-id/commit/ceeff13))
<a name="0.12.4"></a>
## [0.12.4](https://github.com/libp2p/js-peer-id/compare/v0.12.3...v0.12.4) (2019-07-23)
<a name="0.12.3"></a>
## [0.12.3](https://github.com/libp2p/js-peer-id/compare/v0.12.2...v0.12.3) (2019-07-11)
### Features
* **peerid:** support creating from secp256k1; harmonize algo with Go ([fd07221](https://github.com/libp2p/js-peer-id/commit/fd07221))
<a name="0.12.2"></a> <a name="0.12.2"></a>
## [0.12.2](https://github.com/libp2p/js-peer-id/compare/v0.12.1...v0.12.2) (2019-01-09) ## [0.12.2](https://github.com/libp2p/js-peer-id/compare/v0.12.1...v0.12.2) (2019-01-09)

View File

@ -57,7 +57,7 @@ The public key is a base64 encoded string of a protobuf containing an RSA DER bu
```JavaScript ```JavaScript
const PeerId = require('peer-id') const PeerId = require('peer-id')
PeerId.create({ bits: 1024 }, (err, id) => { PeerId.create({ bits: 1024, keyType: 'rsa' }, (err, id) => {
if (err) { throw err } if (err) { throw err }
console.log(JSON.stringify(id.toJSON(), null, 2)) console.log(JSON.stringify(id.toJSON(), null, 2))
}) })
@ -128,7 +128,7 @@ The key format is detailed in [libp2p-crypto](https://github.com/libp2p/js-libp2
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, keyType: 'rsa'}`
- `callback: Function` - `callback: Function`
Calls back `callback` with `err, id`. Calls back `callback` with `err, id`.

View File

@ -1,6 +1,6 @@
{ {
"name": "peer-id", "name": "peer-id",
"version": "0.12.2", "version": "0.12.5",
"description": "IPFS Peer Id implementation in Node.js", "description": "IPFS Peer Id implementation in Node.js",
"leadMaintainer": "Pedro Teixeira <i@pgte.me>", "leadMaintainer": "Pedro Teixeira <i@pgte.me>",
"main": "src/index.js", "main": "src/index.js",
@ -34,26 +34,28 @@
}, },
"homepage": "https://github.com/libp2p/js-peer-id", "homepage": "https://github.com/libp2p/js-peer-id",
"devDependencies": { "devDependencies": {
"aegir": "^18.2.2", "aegir": "^20.0.0",
"bundlesize": "~0.17.1", "bundlesize": "~0.17.1",
"chai": "^4.2.0", "chai": "^4.2.0",
"dirty-chai": "^2.0.1" "dirty-chai": "^2.0.1"
}, },
"dependencies": { "dependencies": {
"async": "^2.6.2",
"class-is": "^1.1.0",
"libp2p-crypto": "~0.16.1", "libp2p-crypto": "~0.16.1",
"multihashes": "~0.4.14" "async": "^2.6.3",
"class-is": "^1.1.0",
"multihashes": "~0.4.15"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/libp2p/js-peer-id.git" "url": "https://github.com/libp2p/js-peer-id.git"
}, },
"contributors": [ "contributors": [
"Arve Knudsen <arve.knudsen@gmail.com>",
"David Dias <daviddias.p@gmail.com>", "David Dias <daviddias.p@gmail.com>",
"David Dias <mail@daviddias.me>", "David Dias <mail@daviddias.me>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>", "Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Hugo Dias <hugomrdias@gmail.com>", "Hugo Dias <hugomrdias@gmail.com>",
"Jacob Heun <jacobheun@gmail.com>",
"Maciej Krüger <mkg20001@gmail.com>", "Maciej Krüger <mkg20001@gmail.com>",
"Michael Garvin <gar+gh@danger.computer>", "Michael Garvin <gar+gh@danger.computer>",
"Pedro Teixeira <i@pgte.me>", "Pedro Teixeira <i@pgte.me>",
@ -61,6 +63,8 @@
"Richard Littauer <richard.littauer@gmail.com>", "Richard Littauer <richard.littauer@gmail.com>",
"Richard Schneider <makaretu@gmail.com>", "Richard Schneider <makaretu@gmail.com>",
"Stephen Whitmore <stephen.whitmore@gmail.com>", "Stephen Whitmore <stephen.whitmore@gmail.com>",
"Topper Bowers <topper@toppingdesign.com>",
"Vasco Santos <vasco.santos@ua.pt>",
"Vasco Santos <vasco.santos@moxy.studio>", "Vasco Santos <vasco.santos@moxy.studio>",
"Yahya <ya7yaz@gmail.com>", "Yahya <ya7yaz@gmail.com>",
"greenkeeperio-bot <support@greenkeeper.io>", "greenkeeperio-bot <support@greenkeeper.io>",

View File

@ -9,5 +9,6 @@ PeerId.create((err, id) => {
throw err throw err
} }
// eslint-disable-next-line
console.log(JSON.stringify(id.toJSON(), null, 2)) console.log(JSON.stringify(id.toJSON(), null, 2))
}) })

View File

@ -48,6 +48,13 @@ class PeerId {
if (this._privKey) { if (this._privKey) {
return this._privKey.public return this._privKey.public
} }
const decoded = mh.decode(this.id)
if (decoded.name === 'identity') {
this._pubKey = cryptoKeys.unmarshalPublicKey(decoded.digest)
return this._pubKey
}
} }
set pubKey (pubKey) { set pubKey (pubKey) {
@ -137,6 +144,27 @@ const PeerIdWithIs = withIs(PeerId, { className: 'PeerId', symbolName: '@libp2p/
exports = module.exports = PeerIdWithIs exports = module.exports = PeerIdWithIs
const computeDigest = (pubKey, cb) => {
if (pubKey.bytes.length <= 42) {
const digest = mh.encode(pubKey.bytes, 'identity')
cb(null, digest)
} else {
pubKey.hash((err, digest) => {
cb(err, digest)
})
}
}
const computePeerId = (privKey, pubKey, cb) => {
computeDigest(pubKey, (err, digest) => {
if (err != null) {
cb(err)
} else {
cb(null, new PeerIdWithIs(digest, privKey, pubKey))
}
})
}
// generation // generation
exports.create = function (opts, callback) { exports.create = function (opts, callback) {
if (typeof opts === 'function') { if (typeof opts === 'function') {
@ -145,18 +173,14 @@ exports.create = function (opts, callback) {
} }
opts = opts || {} opts = opts || {}
opts.bits = opts.bits || 2048 opts.bits = opts.bits || 2048
opts.keyType = opts.keyType || 'RSA'
waterfall([ cryptoKeys.generateKeyPair(opts.keyType, opts.bits, (err, privKey) => {
(cb) => cryptoKeys.generateKeyPair('RSA', opts.bits, cb), if (err != null) {
(privKey, cb) => privKey.public.hash((err, digest) => { callback(err)
cb(err, digest, privKey) } else {
}) computePeerId(privKey, privKey.public, callback)
], (err, digest, privKey) => {
if (err) {
return callback(err)
} }
callback(null, new PeerIdWithIs(digest, privKey))
}) })
} }
@ -193,13 +217,7 @@ exports.createFromPubKey = function (key, callback) {
return callback(err) return callback(err)
} }
pubKey.hash((err, digest) => { computePeerId(null, pubKey, callback)
if (err) {
return callback(err)
}
callback(null, new PeerIdWithIs(digest, null, pubKey))
})
} }
// Private key input will be a string // Private key input will be a string
@ -220,17 +238,12 @@ exports.createFromPrivKey = function (key, callback) {
return callback(err) return callback(err)
} }
waterfall([ cryptoKeys.unmarshalPrivateKey(buf, (err, privKey) => {
(cb) => cryptoKeys.unmarshalPrivateKey(buf, cb), if (err != null) {
(privKey, cb) => privKey.public.hash((err, digest) => { callback(err)
cb(err, digest, privKey) } else {
}) computePeerId(privKey, privKey.public, callback)
], (err, digest, privKey) => {
if (err) {
return callback(err)
} }
callback(null, new PeerIdWithIs(digest, privKey, privKey.public))
}) })
} }
@ -253,39 +266,42 @@ exports.createFromJSON = function (obj, callback) {
return callback(err) return callback(err)
} }
if (rawPrivKey) { if (!rawPrivKey) {
waterfall([
(cb) => cryptoKeys.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 PeerIdWithIs(id, priv, pub))
})
} else {
callback(null, new PeerIdWithIs(id, null, pub)) callback(null, new PeerIdWithIs(id, null, pub))
return
} }
waterfall([
(cb) => cryptoKeys.unmarshalPrivateKey(rawPrivKey, cb),
(priv, cb) => {
computeDigest(priv.public, (err, digest) => {
cb(err, digest, priv)
})
},
(privDigest, priv, cb) => {
if (pub) {
computeDigest(pub, (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 PeerIdWithIs(id, priv, pub))
})
} }
exports.isPeerId = function (peerId) { exports.isPeerId = function (peerId) {

View File

@ -9,6 +9,7 @@ const expect = 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 parallel = require('async/parallel')
const waterfall = require('async/waterfall')
const PeerId = require('../src') const PeerId = require('../src')
@ -40,6 +41,25 @@ describe('PeerId', () => {
}) })
}) })
it('can be created for a Secp256k1 key', (done) => {
PeerId.create({ keyType: 'secp256k1', bits: 256 }, (err, id) => {
const expB58 = mh.toB58String(mh.encode(id.pubKey.bytes, 'identity'))
expect(err).to.not.exist()
expect(id.toB58String()).to.equal(expB58)
done()
})
})
it('can get the public key from a Secp256k1 key', (done) => {
PeerId.create({ keyType: 'secp256k1', bits: 256 }, (err, original) => {
expect(err).to.not.exist()
const newId = PeerId.createFromB58String(original.toB58String())
expect(original.pubKey.bytes).to.eql(newId.pubKey.bytes)
done()
})
})
it('isPeerId', (done) => { it('isPeerId', (done) => {
PeerId.create(testOpts, (err, id) => { PeerId.create(testOpts, (err, id) => {
expect(err).to.not.exist() expect(err).to.not.exist()
@ -85,6 +105,22 @@ describe('PeerId', () => {
}) })
}) })
it('can be created from a Secp256k1 public key', (done) => {
waterfall([
(cb) => {
crypto.keys.generateKeyPair('secp256k1', 256, cb)
},
(privKey, cb) => {
PeerId.createFromPubKey(privKey.public.bytes, cb)
}
], (err, id) => {
expect(err).to.not.exist()
const expB58 = mh.toB58String(mh.encode(id.pubKey.bytes, 'identity'))
expect(id.toB58String()).to.equal(expB58)
done()
})
})
it('Recreate from a Private Key', (done) => { it('Recreate from a Private Key', (done) => {
PeerId.createFromPrivKey(testId.privKey, (err, id) => { PeerId.createFromPrivKey(testId.privKey, (err, id) => {
expect(err).to.not.exist() expect(err).to.not.exist()
@ -100,6 +136,22 @@ describe('PeerId', () => {
}) })
}) })
it('can be created from a Secp256k1 private key', (done) => {
waterfall([
(cb) => {
crypto.keys.generateKeyPair('secp256k1', 256, cb)
},
(privKey, cb) => {
PeerId.createFromPrivKey(privKey.bytes, cb)
}
], (err, id) => {
expect(err).to.not.exist()
const expB58 = mh.toB58String(mh.encode(id.pubKey.bytes, 'identity'))
expect(id.toB58String()).to.equal(expB58)
done()
})
})
it('Compare generated ID with one created from PubKey', (done) => { it('Compare generated ID with one created from PubKey', (done) => {
PeerId.create(testOpts, (err, id1) => { PeerId.create(testOpts, (err, id1) => {
expect(err).to.not.exist() expect(err).to.not.exist()