mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-04-29 14:52:31 +00:00
Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
0bce8f7a87 | ||
|
8f15adbce5 | ||
|
fd072213b7 |
10
CHANGELOG.md
10
CHANGELOG.md
@ -1,3 +1,13 @@
|
||||
<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>
|
||||
## [0.12.2](https://github.com/libp2p/js-peer-id/compare/v0.12.1...v0.12.2) (2019-01-09)
|
||||
|
||||
|
@ -57,7 +57,7 @@ The public key is a base64 encoded string of a protobuf containing an RSA DER bu
|
||||
```JavaScript
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
PeerId.create({ bits: 1024 }, (err, id) => {
|
||||
PeerId.create({ bits: 1024, keyType: 'rsa' }, (err, id) => {
|
||||
if (err) { throw err }
|
||||
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.
|
||||
|
||||
- `opts: Object`: Default: `{bits: 2048}`
|
||||
- `opts: Object`: Default: `{bits: 2048, keyType: 'rsa'}`
|
||||
- `callback: Function`
|
||||
|
||||
Calls back `callback` with `err, id`.
|
||||
|
10
package.json
10
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "peer-id",
|
||||
"version": "0.12.2",
|
||||
"version": "0.12.3",
|
||||
"description": "IPFS Peer Id implementation in Node.js",
|
||||
"leadMaintainer": "Pedro Teixeira <i@pgte.me>",
|
||||
"main": "src/index.js",
|
||||
@ -40,16 +40,17 @@
|
||||
"dirty-chai": "^2.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"async": "^2.6.2",
|
||||
"class-is": "^1.1.0",
|
||||
"libp2p-crypto": "~0.16.1",
|
||||
"multihashes": "~0.4.14"
|
||||
"async": "^3.0.1",
|
||||
"class-is": "^1.1.0",
|
||||
"multihashes": "~0.4.15"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/libp2p/js-peer-id.git"
|
||||
},
|
||||
"contributors": [
|
||||
"Arve Knudsen <arve.knudsen@gmail.com>",
|
||||
"David Dias <daviddias.p@gmail.com>",
|
||||
"David Dias <mail@daviddias.me>",
|
||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||
@ -61,6 +62,7 @@
|
||||
"Richard Littauer <richard.littauer@gmail.com>",
|
||||
"Richard Schneider <makaretu@gmail.com>",
|
||||
"Stephen Whitmore <stephen.whitmore@gmail.com>",
|
||||
"Vasco Santos <vasco.santos@ua.pt>",
|
||||
"Vasco Santos <vasco.santos@moxy.studio>",
|
||||
"Yahya <ya7yaz@gmail.com>",
|
||||
"greenkeeperio-bot <support@greenkeeper.io>",
|
||||
|
125
src/index.js
125
src/index.js
@ -137,6 +137,27 @@ const PeerIdWithIs = withIs(PeerId, { className: 'PeerId', symbolName: '@libp2p/
|
||||
|
||||
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
|
||||
exports.create = function (opts, callback) {
|
||||
if (typeof opts === 'function') {
|
||||
@ -145,18 +166,14 @@ exports.create = function (opts, callback) {
|
||||
}
|
||||
opts = opts || {}
|
||||
opts.bits = opts.bits || 2048
|
||||
opts.keyType = opts.keyType || 'RSA'
|
||||
|
||||
waterfall([
|
||||
(cb) => cryptoKeys.generateKeyPair('RSA', opts.bits, cb),
|
||||
(privKey, cb) => privKey.public.hash((err, digest) => {
|
||||
cb(err, digest, privKey)
|
||||
})
|
||||
], (err, digest, privKey) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
cryptoKeys.generateKeyPair(opts.keyType, opts.bits, (err, privKey) => {
|
||||
if (err != null) {
|
||||
callback(err)
|
||||
} else {
|
||||
computePeerId(privKey, privKey.public, callback)
|
||||
}
|
||||
|
||||
callback(null, new PeerIdWithIs(digest, privKey))
|
||||
})
|
||||
}
|
||||
|
||||
@ -193,13 +210,7 @@ exports.createFromPubKey = function (key, callback) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
pubKey.hash((err, digest) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
callback(null, new PeerIdWithIs(digest, null, pubKey))
|
||||
})
|
||||
computePeerId(null, pubKey, callback)
|
||||
}
|
||||
|
||||
// Private key input will be a string
|
||||
@ -220,17 +231,12 @@ exports.createFromPrivKey = function (key, callback) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
waterfall([
|
||||
(cb) => cryptoKeys.unmarshalPrivateKey(buf, cb),
|
||||
(privKey, cb) => privKey.public.hash((err, digest) => {
|
||||
cb(err, digest, privKey)
|
||||
})
|
||||
], (err, digest, privKey) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
cryptoKeys.unmarshalPrivateKey(buf, (err, privKey) => {
|
||||
if (err != null) {
|
||||
callback(err)
|
||||
} else {
|
||||
computePeerId(privKey, privKey.public, callback)
|
||||
}
|
||||
|
||||
callback(null, new PeerIdWithIs(digest, privKey, privKey.public))
|
||||
})
|
||||
}
|
||||
|
||||
@ -253,39 +259,42 @@ exports.createFromJSON = function (obj, callback) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
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 {
|
||||
if (!rawPrivKey) {
|
||||
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) {
|
||||
|
@ -9,6 +9,7 @@ const expect = chai.expect
|
||||
const crypto = require('libp2p-crypto')
|
||||
const mh = require('multihashes')
|
||||
const parallel = require('async/parallel')
|
||||
const waterfall = require('async/waterfall')
|
||||
|
||||
const PeerId = require('../src')
|
||||
|
||||
@ -40,6 +41,15 @@ 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('isPeerId', (done) => {
|
||||
PeerId.create(testOpts, (err, id) => {
|
||||
expect(err).to.not.exist()
|
||||
@ -85,6 +95,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) => {
|
||||
PeerId.createFromPrivKey(testId.privKey, (err, id) => {
|
||||
expect(err).to.not.exist()
|
||||
@ -100,6 +126,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) => {
|
||||
PeerId.create(testOpts, (err, id1) => {
|
||||
expect(err).to.not.exist()
|
||||
|
Loading…
x
Reference in New Issue
Block a user