feat: use next libp2p-crypto (#64)

* feat: use next libp2p-crypto

* chore: update deps
This commit is contained in:
David Dias 2017-07-22 13:37:01 -07:00 committed by GitHub
parent 156088db99
commit 442df13a11
3 changed files with 27 additions and 37 deletions

View File

@ -41,7 +41,7 @@
},
"dependencies": {
"async": "^2.5.0",
"libp2p-crypto": "~0.8.8",
"libp2p-crypto": "~0.9.4",
"lodash": "^4.17.4",
"multihashes": "~0.4.5"
},
@ -60,4 +60,4 @@
"nginnever <ginneversource@gmail.com>",
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>"
]
}
}

View File

@ -8,6 +8,7 @@ const mh = require('multihashes')
const crypto = require('libp2p-crypto')
const assert = require('assert')
const waterfall = require('async/waterfall')
const Buffer = require('safe-buffer').Buffer
class PeerId {
constructor (id, privKey, pubKey) {
@ -56,14 +57,14 @@ class PeerId {
// Return the protobuf version of the public key, matching go ipfs formatting
marshalPubKey () {
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
marshalPrivKey () {
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
waterfall([
(cb) => crypto.generateKeyPair('RSA', opts.bits, cb),
(cb) => crypto.keys.generateKeyPair('RSA', opts.bits, cb),
(privKey, cb) => privKey.public.hash((err, digest) => {
cb(err, digest, privKey)
})
@ -167,10 +168,10 @@ exports.createFromPubKey = function (key, callback) {
let buf = key
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) => {
if (err) {
@ -185,7 +186,7 @@ exports.createFromPubKey = function (key, callback) {
exports.createFromPrivKey = function (key, callback) {
let buf = key
if (typeof buf === 'string') {
buf = new Buffer(key, 'base64')
buf = Buffer.from(key, 'base64')
}
if (typeof callback !== 'function') {
@ -193,7 +194,7 @@ exports.createFromPrivKey = function (key, callback) {
}
waterfall([
(cb) => crypto.unmarshalPrivateKey(buf, cb),
(cb) => crypto.keys.unmarshalPrivateKey(buf, cb),
(privKey, cb) => privKey.public.hash((err, digest) => {
cb(err, digest, privKey)
})
@ -212,13 +213,13 @@ exports.createFromJSON = function (obj, callback) {
}
const id = mh.fromB58String(obj.id)
const rawPrivKey = obj.privKey && new Buffer(obj.privKey, 'base64')
const rawPubKey = obj.pubKey && new Buffer(obj.pubKey, 'base64')
const pub = rawPubKey && crypto.unmarshalPublicKey(rawPubKey)
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)
if (rawPrivKey) {
waterfall([
(cb) => crypto.unmarshalPrivateKey(rawPrivKey, cb),
(cb) => crypto.keys.unmarshalPrivateKey(rawPrivKey, cb),
(priv, cb) => priv.public.hash((err, digest) => {
cb(err, digest, priv)
}),

View File

@ -9,6 +9,7 @@ const expect = chai.expect
const crypto = require('libp2p-crypto')
const mh = require('multihashes')
const parallel = require('async/parallel')
const Buffer = require('safe-buffer').Buffer
const PeerId = require('../src')
@ -37,7 +38,7 @@ describe('PeerId', () => {
expect(err).to.not.exist()
expect(PeerId.isPeerId(id)).to.equal(true)
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()
})
})
@ -47,7 +48,7 @@ describe('PeerId', () => {
expect(err).to.not.exist()
expect(id.toB58String().length).to.equal(46)
expect(() => {
id.id = new Buffer('hello')
id.id = Buffer.from('hello')
}).to.throw(/immutable/)
done()
})
@ -81,7 +82,7 @@ describe('PeerId', () => {
expect(err).to.not.exist()
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) => {
expect(err).to.not.exist()
expect(testIdB58String).to.equal(id2.toB58String())
@ -159,7 +160,7 @@ describe('PeerId', () => {
})
it('only id', (done) => {
crypto.generateKeyPair('RSA', 1024, (err, key) => {
crypto.keys.generateKeyPair('RSA', 1024, (err, key) => {
expect(err).to.not.exist()
key.public.hash((err, digest) => {
expect(err).to.not.exist()
@ -170,11 +171,7 @@ describe('PeerId', () => {
PeerId.createFromJSON(id.toJSON(), (err, other) => {
expect(err).to.not.exist()
expect(
id.toB58String()
).to.equal(
other.toB58String()
)
expect(id.toB58String()).to.equal(other.toB58String())
done()
})
})
@ -186,11 +183,7 @@ describe('PeerId', () => {
expect(err).to.not.exist()
id.privKey.public.hash((err, digest) => {
expect(err).to.not.exist()
expect(
mh.toB58String(digest)
).to.be.eql(
goId.id
)
expect(mh.toB58String(digest)).to.eql(goId.id)
done()
})
})
@ -216,7 +209,7 @@ describe('PeerId', () => {
it('set privKey (invalid)', (done) => {
PeerId.create((err, peerId) => {
expect(err).to.not.exist()
peerId.privKey = new Buffer('bufff')
peerId.privKey = Buffer.from('bufff')
peerId.isValid((err) => {
expect(err).to.exist()
done()
@ -227,7 +220,7 @@ describe('PeerId', () => {
it('set pubKey (invalid)', (done) => {
PeerId.create((err, peerId) => {
expect(err).to.not.exist()
peerId.pubKey = new Buffer('buffff')
peerId.pubKey = Buffer.from('buffff')
peerId.isValid((err) => {
expect(err).to.exist()
done()
@ -242,9 +235,9 @@ describe('PeerId', () => {
before((done) => {
parallel([
(cb) => crypto.generateKeyPair('RSA', 1024, cb),
(cb) => crypto.generateKeyPair('RSA', 1024, cb),
(cb) => crypto.generateKeyPair('RSA', 1024, cb)
(cb) => crypto.keys.generateKeyPair('RSA', 1024, cb),
(cb) => crypto.keys.generateKeyPair('RSA', 1024, cb),
(cb) => crypto.keys.generateKeyPair('RSA', 1024, cb)
], (err, keys) => {
expect(err).to.not.exist()
@ -274,11 +267,7 @@ describe('PeerId', () => {
})
it('invalid id', () => {
expect(
() => new PeerId('hello world')
).to.throw(
/invalid id/
)
expect(() => new PeerId('hello world')).to.throw(/invalid id/)
})
})
})