Merge pull request #10 from nginnever/master

buffer the public key for consistency
This commit is contained in:
David Dias
2016-03-10 19:31:47 +00:00
2 changed files with 19 additions and 6 deletions

View File

@@ -12,8 +12,7 @@ const path = require('path')
const isNode = !global.window const isNode = !global.window
// protobuf read from file // protobuf read from file
const messages = isNode ? protobuf(fs.readFileSync(path.resolve(__dirname, 'pb/crypto.proto'))) const messages = isNode ? protobuf(fs.readFileSync(path.resolve(__dirname, 'pb/crypto.proto'))) : protobuf(require('buffer!./pb/crypto.proto'))
: protobuf(require('buffer!./pb/crypto.proto'))
exports = module.exports = Id exports = module.exports = Id
@@ -31,7 +30,6 @@ function Id (id, privKey, pubKey) {
self.id = id // multihash - sha256 - buffer self.id = id // multihash - sha256 - buffer
// pretty print // pretty print
self.toPrint = function () { self.toPrint = function () {
return { return {
id: self.toB58String(), id: self.toB58String(),
@@ -41,7 +39,6 @@ function Id (id, privKey, pubKey) {
} }
// encode/decode functions // encode/decode functions
self.toHexString = function () { self.toHexString = function () {
return self.id.toString('hex') return self.id.toString('hex')
} }
@@ -112,9 +109,13 @@ exports.create = function () {
const protoPublic64 = formatKey(asnPub, 'Public') const protoPublic64 = formatKey(asnPub, 'Public')
const protoPrivate64 = formatKey(asnPriv, 'Private') const protoPrivate64 = formatKey(asnPriv, 'Private')
// store the keys as a buffer
const bufProtoPub64 = new Buffer(protoPublic64, 'base64')
const bufProtoPriv64 = new Buffer(protoPrivate64, 'base64')
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256') const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
return new Id(mhId, protoPrivate64, protoPublic64) return new Id(mhId, bufProtoPriv64, bufProtoPub64)
} }
exports.createFromHexString = function (str) { exports.createFromHexString = function (str) {
@@ -129,12 +130,14 @@ exports.createFromB58String = function (str) {
return new Id(new Buffer(base58.decode(str))) return new Id(new Buffer(base58.decode(str)))
} }
// Public Key input will be a buffer
exports.createFromPubKey = function (pubKey) { exports.createFromPubKey = function (pubKey) {
const buf = new Buffer(pubKey, 'base64') const buf = new Buffer(pubKey, 'base64')
const mhId = multihashing(buf, 'sha2-256') const mhId = multihashing(buf, 'sha2-256')
return new Id(mhId, null, pubKey) return new Id(mhId, null, pubKey)
} }
// Private key input will be a string
exports.createFromPrivKey = function (privKey) { exports.createFromPrivKey = function (privKey) {
// create a buffer from the base64 encoded string // create a buffer from the base64 encoded string
const buf = new Buffer(privKey, 'base64') const buf = new Buffer(privKey, 'base64')
@@ -159,6 +162,9 @@ exports.createFromPrivKey = function (privKey) {
// format the public key // format the public key
const protoPublic64 = formatKey(asnPub, 'Public') const protoPublic64 = formatKey(asnPub, 'Public')
// buffer the public key for consistency before storing
const bufProtoPub64 = new Buffer(protoPublic64, 'base64')
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256') const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
return new Id(mhId, privKey, protoPublic64) return new Id(mhId, privKey, bufProtoPub64)
} }

View File

@@ -55,5 +55,12 @@ describe('id', function (done) {
expect(testIdB58String).to.equal(id.toB58String()) expect(testIdB58String).to.equal(id.toB58String())
done() done()
}) })
it('Compare generated ID with one created from PubKey', (done) => {
const id1 = PeerId.create()
const id2 = PeerId.createFromPubKey(id1.pubKey)
expect(id2.id).to.deep.equal(id1.id)
done()
})
}) })