mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-07-06 07:51:52 +00:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
c945d6ad48 | |||
0eee1a2a69 | |||
63428faae9 | |||
a38e3859d2 | |||
551fd710be | |||
442df13a11 |
17
.gitignore
vendored
17
.gitignore
vendored
@ -1,7 +1,14 @@
|
|||||||
|
**/node_modules/
|
||||||
|
**/*.log
|
||||||
|
test/repo-tests*
|
||||||
|
**/bundle.js
|
||||||
|
|
||||||
# Logs
|
# Logs
|
||||||
logs
|
logs
|
||||||
*.log
|
*.log
|
||||||
|
|
||||||
|
coverage
|
||||||
|
|
||||||
# Runtime data
|
# Runtime data
|
||||||
pids
|
pids
|
||||||
*.pid
|
*.pid
|
||||||
@ -19,11 +26,17 @@ coverage
|
|||||||
# node-waf configuration
|
# node-waf configuration
|
||||||
.lock-wscript
|
.lock-wscript
|
||||||
|
|
||||||
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
build
|
||||||
build/Release
|
|
||||||
|
|
||||||
# Dependency directory
|
# Dependency directory
|
||||||
# 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
|
||||||
|
test/test-data/go-ipfs-repo/LOCK
|
||||||
|
test/test-data/go-ipfs-repo/LOG
|
||||||
|
test/test-data/go-ipfs-repo/LOG.old
|
||||||
|
|
||||||
|
# while testing npm5
|
||||||
|
package-lock.json
|
||||||
|
15
.travis.yml
15
.travis.yml
@ -3,21 +3,16 @@ language: node_js
|
|||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
- node_js: 4
|
|
||||||
env: CXX=g++-4.8
|
|
||||||
- node_js: 6
|
- node_js: 6
|
||||||
env:
|
|
||||||
- CXX=g++-4.8
|
|
||||||
- node_js: stable
|
|
||||||
env: CXX=g++-4.8
|
env: CXX=g++-4.8
|
||||||
|
- node_js: 8
|
||||||
# Make sure we have new NPM.
|
env: CXX=g++-4.8
|
||||||
before_install:
|
# - node_js: stable
|
||||||
- npm install -g npm@4
|
# env: CXX=g++-4.8
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- npm run lint
|
- npm run lint
|
||||||
- npm test
|
- npm run test
|
||||||
- npm run coverage
|
- npm run coverage
|
||||||
- make test
|
- make test
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
[](https://david-dm.org/libp2p/js-peer-id)
|
[](https://david-dm.org/libp2p/js-peer-id)
|
||||||
[](https://github.com/feross/standard)
|
[](https://github.com/feross/standard)
|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
> [IPFS](https://github.com/ipfs/ipfs) Peer ID implementation in JavaScript.
|
> [IPFS](https://github.com/ipfs/ipfs) Peer ID implementation in JavaScript.
|
||||||
|
|
||||||
|
10
package.json
10
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "peer-id",
|
"name": "peer-id",
|
||||||
"version": "0.8.8",
|
"version": "0.10.0",
|
||||||
"description": "IPFS Peer Id implementation in Node.js",
|
"description": "IPFS Peer Id implementation in Node.js",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"bin": "src/bin.js",
|
"bin": "src/bin.js",
|
||||||
@ -26,7 +26,7 @@
|
|||||||
"test"
|
"test"
|
||||||
],
|
],
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=4.0.0",
|
"node": ">=6.0.0",
|
||||||
"npm": ">=3.0.0"
|
"npm": ">=3.0.0"
|
||||||
},
|
},
|
||||||
"bugs": {
|
"bugs": {
|
||||||
@ -35,15 +35,15 @@
|
|||||||
"homepage": "https://github.com/libp2p/js-peer-id",
|
"homepage": "https://github.com/libp2p/js-peer-id",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^11.0.2",
|
"aegir": "^11.0.2",
|
||||||
"chai": "^4.1.0",
|
"chai": "^4.1.2",
|
||||||
"dirty-chai": "^2.0.1",
|
"dirty-chai": "^2.0.1",
|
||||||
"pre-commit": "^1.2.2"
|
"pre-commit": "^1.2.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"async": "^2.5.0",
|
"async": "^2.5.0",
|
||||||
"libp2p-crypto": "~0.8.8",
|
"libp2p-crypto": "~0.9.4",
|
||||||
"lodash": "^4.17.4",
|
"lodash": "^4.17.4",
|
||||||
"multihashes": "~0.4.5"
|
"multihashes": "~0.4.9"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
23
src/index.js
23
src/index.js
@ -8,6 +8,7 @@ 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')
|
const waterfall = require('async/waterfall')
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
class PeerId {
|
class PeerId {
|
||||||
constructor (id, privKey, pubKey) {
|
constructor (id, privKey, pubKey) {
|
||||||
@ -56,14 +57,14 @@ class PeerId {
|
|||||||
// Return the protobuf version of the public key, matching go ipfs formatting
|
// Return the protobuf version of the public key, matching go ipfs formatting
|
||||||
marshalPubKey () {
|
marshalPubKey () {
|
||||||
if (this.pubKey) {
|
if (this.pubKey) {
|
||||||
return crypto.marshalPublicKey(this.pubKey)
|
return crypto.keys.marshalPublicKey(this.pubKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the protobuf version of the private key, matching go ipfs formatting
|
// Return the protobuf version of the private key, matching go ipfs formatting
|
||||||
marshalPrivKey () {
|
marshalPrivKey () {
|
||||||
if (this.privKey) {
|
if (this.privKey) {
|
||||||
return crypto.marshalPrivateKey(this.privKey)
|
return crypto.keys.marshalPrivateKey(this.privKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,7 +135,7 @@ exports.create = function (opts, callback) {
|
|||||||
opts.bits = opts.bits || 2048
|
opts.bits = opts.bits || 2048
|
||||||
|
|
||||||
waterfall([
|
waterfall([
|
||||||
(cb) => crypto.generateKeyPair('RSA', opts.bits, cb),
|
(cb) => crypto.keys.generateKeyPair('RSA', opts.bits, cb),
|
||||||
(privKey, cb) => privKey.public.hash((err, digest) => {
|
(privKey, cb) => privKey.public.hash((err, digest) => {
|
||||||
cb(err, digest, privKey)
|
cb(err, digest, privKey)
|
||||||
})
|
})
|
||||||
@ -167,10 +168,10 @@ 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 = Buffer.from(key, 'base64')
|
||||||
}
|
}
|
||||||
|
|
||||||
const pubKey = crypto.unmarshalPublicKey(buf)
|
const pubKey = crypto.keys.unmarshalPublicKey(buf)
|
||||||
|
|
||||||
pubKey.hash((err, digest) => {
|
pubKey.hash((err, digest) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@ -185,7 +186,7 @@ exports.createFromPubKey = function (key, callback) {
|
|||||||
exports.createFromPrivKey = function (key, callback) {
|
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 = Buffer.from(key, 'base64')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof callback !== 'function') {
|
if (typeof callback !== 'function') {
|
||||||
@ -193,7 +194,7 @@ exports.createFromPrivKey = function (key, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
waterfall([
|
waterfall([
|
||||||
(cb) => crypto.unmarshalPrivateKey(buf, cb),
|
(cb) => crypto.keys.unmarshalPrivateKey(buf, cb),
|
||||||
(privKey, cb) => privKey.public.hash((err, digest) => {
|
(privKey, cb) => privKey.public.hash((err, digest) => {
|
||||||
cb(err, digest, privKey)
|
cb(err, digest, privKey)
|
||||||
})
|
})
|
||||||
@ -212,13 +213,13 @@ exports.createFromJSON = function (obj, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const id = mh.fromB58String(obj.id)
|
const id = mh.fromB58String(obj.id)
|
||||||
const rawPrivKey = obj.privKey && new Buffer(obj.privKey, 'base64')
|
const rawPrivKey = obj.privKey && Buffer.from(obj.privKey, 'base64')
|
||||||
const rawPubKey = obj.pubKey && new Buffer(obj.pubKey, 'base64')
|
const rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64')
|
||||||
const pub = rawPubKey && crypto.unmarshalPublicKey(rawPubKey)
|
const pub = rawPubKey && crypto.keys.unmarshalPublicKey(rawPubKey)
|
||||||
|
|
||||||
if (rawPrivKey) {
|
if (rawPrivKey) {
|
||||||
waterfall([
|
waterfall([
|
||||||
(cb) => crypto.unmarshalPrivateKey(rawPrivKey, cb),
|
(cb) => crypto.keys.unmarshalPrivateKey(rawPrivKey, cb),
|
||||||
(priv, cb) => priv.public.hash((err, digest) => {
|
(priv, cb) => priv.public.hash((err, digest) => {
|
||||||
cb(err, digest, priv)
|
cb(err, digest, priv)
|
||||||
}),
|
}),
|
||||||
|
@ -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 Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
const PeerId = require('../src')
|
const PeerId = require('../src')
|
||||||
|
|
||||||
@ -37,7 +38,7 @@ describe('PeerId', () => {
|
|||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
expect(PeerId.isPeerId(id)).to.equal(true)
|
expect(PeerId.isPeerId(id)).to.equal(true)
|
||||||
expect(PeerId.isPeerId('aaa')).to.equal(false)
|
expect(PeerId.isPeerId('aaa')).to.equal(false)
|
||||||
expect(PeerId.isPeerId(new Buffer('batatas'))).to.equal(false)
|
expect(PeerId.isPeerId(Buffer.from('batatas'))).to.equal(false)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -47,7 +48,7 @@ describe('PeerId', () => {
|
|||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
expect(id.toB58String().length).to.equal(46)
|
expect(id.toB58String().length).to.equal(46)
|
||||||
expect(() => {
|
expect(() => {
|
||||||
id.id = new Buffer('hello')
|
id.id = Buffer.from('hello')
|
||||||
}).to.throw(/immutable/)
|
}).to.throw(/immutable/)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
@ -81,7 +82,7 @@ describe('PeerId', () => {
|
|||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
|
|
||||||
const encoded = new Buffer(testId.privKey, 'base64')
|
const encoded = Buffer.from(testId.privKey, 'base64')
|
||||||
PeerId.createFromPrivKey(encoded, (err, id2) => {
|
PeerId.createFromPrivKey(encoded, (err, id2) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
expect(testIdB58String).to.equal(id2.toB58String())
|
expect(testIdB58String).to.equal(id2.toB58String())
|
||||||
@ -159,7 +160,7 @@ describe('PeerId', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('only id', (done) => {
|
it('only id', (done) => {
|
||||||
crypto.generateKeyPair('RSA', 1024, (err, key) => {
|
crypto.keys.generateKeyPair('RSA', 1024, (err, key) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
key.public.hash((err, digest) => {
|
key.public.hash((err, digest) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
@ -170,11 +171,7 @@ describe('PeerId', () => {
|
|||||||
|
|
||||||
PeerId.createFromJSON(id.toJSON(), (err, other) => {
|
PeerId.createFromJSON(id.toJSON(), (err, other) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
expect(
|
expect(id.toB58String()).to.equal(other.toB58String())
|
||||||
id.toB58String()
|
|
||||||
).to.equal(
|
|
||||||
other.toB58String()
|
|
||||||
)
|
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -186,11 +183,7 @@ describe('PeerId', () => {
|
|||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
id.privKey.public.hash((err, digest) => {
|
id.privKey.public.hash((err, digest) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
expect(
|
expect(mh.toB58String(digest)).to.eql(goId.id)
|
||||||
mh.toB58String(digest)
|
|
||||||
).to.be.eql(
|
|
||||||
goId.id
|
|
||||||
)
|
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -216,7 +209,7 @@ describe('PeerId', () => {
|
|||||||
it('set privKey (invalid)', (done) => {
|
it('set privKey (invalid)', (done) => {
|
||||||
PeerId.create((err, peerId) => {
|
PeerId.create((err, peerId) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
peerId.privKey = new Buffer('bufff')
|
peerId.privKey = Buffer.from('bufff')
|
||||||
peerId.isValid((err) => {
|
peerId.isValid((err) => {
|
||||||
expect(err).to.exist()
|
expect(err).to.exist()
|
||||||
done()
|
done()
|
||||||
@ -227,7 +220,7 @@ describe('PeerId', () => {
|
|||||||
it('set pubKey (invalid)', (done) => {
|
it('set pubKey (invalid)', (done) => {
|
||||||
PeerId.create((err, peerId) => {
|
PeerId.create((err, peerId) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
peerId.pubKey = new Buffer('buffff')
|
peerId.pubKey = Buffer.from('buffff')
|
||||||
peerId.isValid((err) => {
|
peerId.isValid((err) => {
|
||||||
expect(err).to.exist()
|
expect(err).to.exist()
|
||||||
done()
|
done()
|
||||||
@ -242,9 +235,9 @@ describe('PeerId', () => {
|
|||||||
|
|
||||||
before((done) => {
|
before((done) => {
|
||||||
parallel([
|
parallel([
|
||||||
(cb) => crypto.generateKeyPair('RSA', 1024, cb),
|
(cb) => crypto.keys.generateKeyPair('RSA', 1024, cb),
|
||||||
(cb) => crypto.generateKeyPair('RSA', 1024, cb),
|
(cb) => crypto.keys.generateKeyPair('RSA', 1024, cb),
|
||||||
(cb) => crypto.generateKeyPair('RSA', 1024, cb)
|
(cb) => crypto.keys.generateKeyPair('RSA', 1024, cb)
|
||||||
], (err, keys) => {
|
], (err, keys) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
|
|
||||||
@ -274,11 +267,7 @@ describe('PeerId', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('invalid id', () => {
|
it('invalid id', () => {
|
||||||
expect(
|
expect(() => new PeerId('hello world')).to.throw(/invalid id/)
|
||||||
() => new PeerId('hello world')
|
|
||||||
).to.throw(
|
|
||||||
/invalid id/
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user