mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-04-26 22:52:30 +00:00
feat: use next libp2p-crypto (#64)
* feat: use next libp2p-crypto * chore: update deps
This commit is contained in:
parent
156088db99
commit
442df13a11
@ -41,7 +41,7 @@
|
|||||||
},
|
},
|
||||||
"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.5"
|
||||||
},
|
},
|
||||||
|
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/
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user