mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-07-04 03:31:50 +00:00
Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
ffed046595 | |||
1727a135aa | |||
466b3a030d | |||
464c92a53b | |||
8a608def32 | |||
19deeff432 | |||
b18c0082ff | |||
2b050bcaa1 | |||
b19edf2ab2 | |||
35b384ed15 | |||
655ceec29b | |||
8b9a134276 | |||
a637297112 | |||
e666b7284f | |||
156911e162 | |||
f91f2b6506 | |||
8f6935499e | |||
80da1a54b2 | |||
300266ad51 | |||
3abdcdabbb |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
docs
|
||||
**/node_modules/
|
||||
**/*.log
|
||||
test/repo-tests*
|
||||
@ -41,4 +42,4 @@ test/test-data/go-ipfs-repo/LOG.old
|
||||
|
||||
# while testing npm5
|
||||
package-lock.json
|
||||
yarn.lock
|
||||
yarn.lock
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
|
||||
sudo: false
|
||||
language: node_js
|
||||
|
||||
@ -13,7 +14,7 @@ matrix:
|
||||
script:
|
||||
- npm run lint
|
||||
- npm run test
|
||||
- npm run coverage -- --upload
|
||||
- npm run coverage
|
||||
|
||||
before_script:
|
||||
- export DISPLAY=:99.0
|
||||
|
45
CHANGELOG.md
45
CHANGELOG.md
@ -1,3 +1,48 @@
|
||||
<a name="0.10.7"></a>
|
||||
## [0.10.7](https://github.com/libp2p/js-peer-id/compare/v0.10.6...v0.10.7) (2018-04-05)
|
||||
|
||||
|
||||
|
||||
<a name="0.10.6"></a>
|
||||
## [0.10.6](https://github.com/libp2p/js-peer-id/compare/v0.10.5...v0.10.6) (2018-02-12)
|
||||
|
||||
|
||||
|
||||
<a name="0.10.5"></a>
|
||||
## [0.10.5](https://github.com/libp2p/js-peer-id/compare/v0.10.4...v0.10.5) (2018-01-28)
|
||||
|
||||
|
||||
|
||||
<a name="0.10.4"></a>
|
||||
## [0.10.4](https://github.com/libp2p/js-peer-id/compare/v0.10.3...v0.10.4) (2017-12-20)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* update dependencies ([#73](https://github.com/libp2p/js-peer-id/issues/73)) ([8b9a134](https://github.com/libp2p/js-peer-id/commit/8b9a134))
|
||||
|
||||
|
||||
|
||||
<a name="0.10.3"></a>
|
||||
## [0.10.3](https://github.com/libp2p/js-peer-id/compare/v0.10.2...v0.10.3) (2017-12-01)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* catch error when unmarshaling instead of crashing ([#72](https://github.com/libp2p/js-peer-id/issues/72)) ([156911e](https://github.com/libp2p/js-peer-id/commit/156911e))
|
||||
|
||||
|
||||
|
||||
<a name="0.10.2"></a>
|
||||
## [0.10.2](https://github.com/libp2p/js-peer-id/compare/v0.10.1...v0.10.2) (2017-10-12)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* Always add public key to constructor if possible, Fix for undefined pubKey in remote peers ([#68](https://github.com/libp2p/js-peer-id/issues/68)) ([3abdcda](https://github.com/libp2p/js-peer-id/commit/3abdcda))
|
||||
|
||||
|
||||
|
||||
<a name="0.10.1"></a>
|
||||
## [0.10.1](https://github.com/libp2p/js-peer-id/compare/v0.10.0...v0.10.1) (2017-09-07)
|
||||
|
||||
|
29
appveyor.yml
Normal file
29
appveyor.yml
Normal file
@ -0,0 +1,29 @@
|
||||
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
|
||||
version: "{build}"
|
||||
|
||||
environment:
|
||||
matrix:
|
||||
- nodejs_version: "6"
|
||||
- nodejs_version: "8"
|
||||
|
||||
matrix:
|
||||
fast_finish: true
|
||||
|
||||
install:
|
||||
# Install Node.js
|
||||
- ps: Install-Product node $env:nodejs_version
|
||||
|
||||
# Upgrade npm
|
||||
- npm install -g npm
|
||||
|
||||
# Output our current versions for debugging
|
||||
- node --version
|
||||
- npm --version
|
||||
|
||||
# Install our package dependencies
|
||||
- npm install
|
||||
|
||||
test_script:
|
||||
- npm run test:node
|
||||
|
||||
build: off
|
2
ci/Jenkinsfile
vendored
Normal file
2
ci/Jenkinsfile
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
// Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
|
||||
javascript()
|
@ -1,3 +1,4 @@
|
||||
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
|
||||
machine:
|
||||
node:
|
||||
version: stable
|
||||
|
18
package.json
18
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "peer-id",
|
||||
"version": "0.10.1",
|
||||
"version": "0.10.7",
|
||||
"description": "IPFS Peer Id implementation in Node.js",
|
||||
"main": "src/index.js",
|
||||
"bin": "src/bin.js",
|
||||
@ -33,16 +33,16 @@
|
||||
},
|
||||
"homepage": "https://github.com/libp2p/js-peer-id",
|
||||
"devDependencies": {
|
||||
"aegir": "^12.0.6",
|
||||
"aegir": "^13.0.6",
|
||||
"chai": "^4.1.2",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"pre-commit": "^1.2.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"async": "^2.5.0",
|
||||
"libp2p-crypto": "~0.10.3",
|
||||
"lodash": "^4.17.4",
|
||||
"multihashes": "~0.4.9"
|
||||
"async": "^2.6.0",
|
||||
"libp2p-crypto": "~0.12.1",
|
||||
"lodash": "^4.17.5",
|
||||
"multihashes": "~0.4.13"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -52,11 +52,15 @@
|
||||
"David Dias <daviddias.p@gmail.com>",
|
||||
"David Dias <mail@daviddias.me>",
|
||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||
"Maciej Krüger <mkg20001@gmail.com>",
|
||||
"Prashanth Chandra <coolshanth94@gmail.com>",
|
||||
"Richard Littauer <richard.littauer@gmail.com>",
|
||||
"Richard Schneider <makaretu@gmail.com>",
|
||||
"Stephen Whitmore <stephen.whitmore@gmail.com>",
|
||||
"Yahya <ya7yaz@gmail.com>",
|
||||
"greenkeeperio-bot <support@greenkeeper.io>",
|
||||
"nginnever <ginneversource@gmail.com>",
|
||||
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>"
|
||||
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>",
|
||||
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>"
|
||||
]
|
||||
}
|
||||
|
54
src/index.js
54
src/index.js
@ -165,12 +165,20 @@ exports.createFromPubKey = function (key, callback) {
|
||||
throw new Error('callback is required')
|
||||
}
|
||||
|
||||
let buf = key
|
||||
if (typeof buf === 'string') {
|
||||
buf = Buffer.from(key, 'base64')
|
||||
}
|
||||
let pubKey
|
||||
|
||||
const pubKey = crypto.keys.unmarshalPublicKey(buf)
|
||||
try {
|
||||
let buf = key
|
||||
if (typeof buf === 'string') {
|
||||
buf = Buffer.from(key, 'base64')
|
||||
}
|
||||
|
||||
if (!Buffer.isBuffer(buf)) throw new Error('Supplied key is neither a base64 string nor a buffer')
|
||||
|
||||
pubKey = crypto.keys.unmarshalPublicKey(buf)
|
||||
} catch (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
pubKey.hash((err, digest) => {
|
||||
if (err) {
|
||||
@ -183,15 +191,22 @@ exports.createFromPubKey = function (key, callback) {
|
||||
|
||||
// Private key input will be a string
|
||||
exports.createFromPrivKey = function (key, callback) {
|
||||
let buf = key
|
||||
if (typeof buf === 'string') {
|
||||
buf = Buffer.from(key, 'base64')
|
||||
}
|
||||
|
||||
if (typeof callback !== 'function') {
|
||||
throw new Error('callback is required')
|
||||
}
|
||||
|
||||
let buf = key
|
||||
|
||||
try {
|
||||
if (typeof buf === 'string') {
|
||||
buf = Buffer.from(key, 'base64')
|
||||
}
|
||||
|
||||
if (!Buffer.isBuffer(buf)) throw new Error('Supplied key is neither a base64 string nor a buffer')
|
||||
} catch (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
waterfall([
|
||||
(cb) => crypto.keys.unmarshalPrivateKey(buf, cb),
|
||||
(privKey, cb) => privKey.public.hash((err, digest) => {
|
||||
@ -202,7 +217,7 @@ exports.createFromPrivKey = function (key, callback) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
callback(null, new PeerId(digest, privKey))
|
||||
callback(null, new PeerId(digest, privKey, privKey.public))
|
||||
})
|
||||
}
|
||||
|
||||
@ -211,10 +226,19 @@ exports.createFromJSON = function (obj, callback) {
|
||||
throw new Error('callback is required')
|
||||
}
|
||||
|
||||
const id = mh.fromB58String(obj.id)
|
||||
const rawPrivKey = obj.privKey && Buffer.from(obj.privKey, 'base64')
|
||||
const rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64')
|
||||
const pub = rawPubKey && crypto.keys.unmarshalPublicKey(rawPubKey)
|
||||
let id
|
||||
let rawPrivKey
|
||||
let rawPubKey
|
||||
let pub
|
||||
|
||||
try {
|
||||
id = mh.fromB58String(obj.id)
|
||||
rawPrivKey = obj.privKey && Buffer.from(obj.privKey, 'base64')
|
||||
rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64')
|
||||
pub = rawPubKey && crypto.keys.unmarshalPublicKey(rawPubKey)
|
||||
} catch (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
if (rawPrivKey) {
|
||||
waterfall([
|
||||
|
@ -12,6 +12,8 @@ const parallel = require('async/parallel')
|
||||
|
||||
const PeerId = require('../src')
|
||||
|
||||
const util = require('util')
|
||||
|
||||
const testId = require('./fixtures/sample-id')
|
||||
const testIdHex = testId.id
|
||||
const testIdBytes = mh.fromHexString(testId.id)
|
||||
@ -19,13 +21,19 @@ const testIdB58String = mh.toB58String(testIdBytes)
|
||||
|
||||
const goId = require('./fixtures/go-private-key')
|
||||
|
||||
// Test options for making PeerId.create faster
|
||||
// INSECURE, only use when testing
|
||||
const testOpts = {
|
||||
bits: 512
|
||||
}
|
||||
|
||||
describe('PeerId', () => {
|
||||
it('create an id without \'new\'', () => {
|
||||
expect(PeerId).to.throw(Error)
|
||||
})
|
||||
|
||||
it('create a new id', (done) => {
|
||||
PeerId.create((err, id) => {
|
||||
PeerId.create(testOpts, (err, id) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(id.toB58String().length).to.equal(46)
|
||||
done()
|
||||
@ -33,7 +41,7 @@ describe('PeerId', () => {
|
||||
})
|
||||
|
||||
it('isPeerId', (done) => {
|
||||
PeerId.create((err, id) => {
|
||||
PeerId.create(testOpts, (err, id) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(PeerId.isPeerId(id)).to.equal(true)
|
||||
expect(PeerId.isPeerId('aaa')).to.equal(false)
|
||||
@ -42,8 +50,9 @@ describe('PeerId', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('throws on changing the id', (done) => {
|
||||
PeerId.create((err, id) => {
|
||||
it('throws on changing the id', function (done) {
|
||||
this.timeout(10000)
|
||||
PeerId.create(testOpts, (err, id) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(id.toB58String().length).to.equal(46)
|
||||
expect(() => {
|
||||
@ -85,13 +94,14 @@ describe('PeerId', () => {
|
||||
PeerId.createFromPrivKey(encoded, (err, id2) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(testIdB58String).to.equal(id2.toB58String())
|
||||
expect(id.marshalPubKey()).to.deep.equal(id2.marshalPubKey())
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('Compare generated ID with one created from PubKey', (done) => {
|
||||
PeerId.create((err, id1) => {
|
||||
PeerId.create(testOpts, (err, id1) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
PeerId.createFromPubKey(id1.marshalPubKey(), (err, id2) => {
|
||||
@ -102,12 +112,20 @@ describe('PeerId', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('Non-default # of bits', function (done) {
|
||||
// rsa is slow atm
|
||||
this.timeout(20000)
|
||||
PeerId.create({ bits: 1024 }, (err, shortId) => {
|
||||
it('Works with default options', function (done) {
|
||||
this.timeout(10000)
|
||||
PeerId.create((err, id) => {
|
||||
expect(err).to.not.exist()
|
||||
PeerId.create({ bits: 4096 }, (err, longId) => {
|
||||
expect(id.toB58String().length).to.equal(46)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('Non-default # of bits', function (done) {
|
||||
this.timeout(1000 * 60)
|
||||
PeerId.create(testOpts, (err, shortId) => {
|
||||
expect(err).to.not.exist()
|
||||
PeerId.create({ bits: 1024 }, (err, longId) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(shortId.privKey.bytes.length).is.below(longId.privKey.bytes.length)
|
||||
done()
|
||||
@ -116,7 +134,7 @@ describe('PeerId', () => {
|
||||
})
|
||||
|
||||
it('Pretty printing', (done) => {
|
||||
PeerId.create((err, id1) => {
|
||||
PeerId.create(testOpts, (err, id1) => {
|
||||
expect(err).to.not.exist()
|
||||
PeerId.createFromPrivKey(id1.toPrint().privKey, (err, id2) => {
|
||||
expect(err).to.not.exist()
|
||||
@ -133,8 +151,8 @@ describe('PeerId', () => {
|
||||
|
||||
it('isEqual', (done) => {
|
||||
parallel([
|
||||
(cb) => PeerId.create(cb),
|
||||
(cb) => PeerId.create(cb)
|
||||
(cb) => PeerId.create(testOpts, cb),
|
||||
(cb) => PeerId.create(testOpts, cb)
|
||||
], (err, ids) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(ids[0].isEqual(ids[0])).to.equal(true)
|
||||
@ -147,7 +165,7 @@ describe('PeerId', () => {
|
||||
|
||||
describe('fromJSON', () => {
|
||||
it('full node', (done) => {
|
||||
PeerId.create({ bits: 1024 }, (err, id) => {
|
||||
PeerId.create(testOpts, (err, id) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
PeerId.createFromJSON(id.toJSON(), (err, other) => {
|
||||
@ -192,7 +210,7 @@ describe('PeerId', () => {
|
||||
})
|
||||
|
||||
it('set privKey (valid)', (done) => {
|
||||
PeerId.create((err, peerId) => {
|
||||
PeerId.create(testOpts, (err, peerId) => {
|
||||
expect(err).to.not.exist()
|
||||
peerId.privKey = peerId._privKey
|
||||
peerId.isValid(done)
|
||||
@ -200,7 +218,7 @@ describe('PeerId', () => {
|
||||
})
|
||||
|
||||
it('set pubKey (valid)', (done) => {
|
||||
PeerId.create((err, peerId) => {
|
||||
PeerId.create(testOpts, (err, peerId) => {
|
||||
expect(err).to.not.exist()
|
||||
peerId.pubKey = peerId._pubKey
|
||||
peerId.isValid(done)
|
||||
@ -208,7 +226,7 @@ describe('PeerId', () => {
|
||||
})
|
||||
|
||||
it('set privKey (invalid)', (done) => {
|
||||
PeerId.create((err, peerId) => {
|
||||
PeerId.create(testOpts, (err, peerId) => {
|
||||
expect(err).to.not.exist()
|
||||
peerId.privKey = Buffer.from('bufff')
|
||||
peerId.isValid((err) => {
|
||||
@ -219,7 +237,7 @@ describe('PeerId', () => {
|
||||
})
|
||||
|
||||
it('set pubKey (invalid)', (done) => {
|
||||
PeerId.create((err, peerId) => {
|
||||
PeerId.create(testOpts, (err, peerId) => {
|
||||
expect(err).to.not.exist()
|
||||
peerId.pubKey = Buffer.from('buffff')
|
||||
peerId.isValid((err) => {
|
||||
@ -229,6 +247,24 @@ describe('PeerId', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('returns error via cb instead of crashing', () => {
|
||||
const garbage = [Buffer.from('00010203040506070809', 'hex'), {}, null, false, undefined, true, 1, 0, Buffer.from(''), 'aGVsbG93b3JsZA==', 'helloworld', '']
|
||||
|
||||
const fncs = ['createFromPubKey', 'createFromPrivKey', 'createFromJSON']
|
||||
|
||||
garbage.forEach(garbage => {
|
||||
fncs.forEach(fnc => {
|
||||
it(fnc + '(' + util.inspect(garbage) + ')', cb => {
|
||||
PeerId[fnc](garbage, (err, res) => {
|
||||
expect(err).to.exist()
|
||||
expect(res).to.not.exist()
|
||||
cb()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('throws on inconsistent data', () => {
|
||||
let k1
|
||||
let k2
|
||||
@ -236,9 +272,9 @@ describe('PeerId', () => {
|
||||
|
||||
before((done) => {
|
||||
parallel([
|
||||
(cb) => crypto.keys.generateKeyPair('RSA', 1024, cb),
|
||||
(cb) => crypto.keys.generateKeyPair('RSA', 1024, cb),
|
||||
(cb) => crypto.keys.generateKeyPair('RSA', 1024, cb)
|
||||
(cb) => crypto.keys.generateKeyPair('RSA', 512, cb),
|
||||
(cb) => crypto.keys.generateKeyPair('RSA', 512, cb),
|
||||
(cb) => crypto.keys.generateKeyPair('RSA', 512, cb)
|
||||
], (err, keys) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
|
Reference in New Issue
Block a user