Compare commits

...

11 Commits

Author SHA1 Message Date
f62023f8a7 Release v0.6.1. 2016-03-15 23:28:07 +00:00
95367871ac Merge pull request #11 from noffle/bits
exposes opts + bits
2016-03-15 23:22:41 +00:00
352a6cfa17 tests # of bits 2016-03-15 12:15:44 -07:00
727355c4f5 exposes opts + bits 2016-03-14 17:10:02 -07:00
bd185f8081 Release v0.6.0. 2016-03-10 19:43:32 +00:00
d4c3de1ecb codestyle 2016-03-10 19:43:23 +00:00
b692394116 chore: dist 2016-03-10 19:40:52 +00:00
a8e591525b update readme 2016-03-10 19:36:47 +00:00
6ad5d85f76 Merge pull request #10 from nginnever/master
buffer the public key for consistency
2016-03-10 19:31:47 +00:00
ca9cef6de7 comparing buffers 2016-03-10 11:25:59 -08:00
f55eebcef7 buffer the public key for consistency 2016-03-10 10:32:48 -08:00
5 changed files with 21139 additions and 550 deletions

View File

@ -28,9 +28,13 @@ The public key is a base64 encoded string of a protobuf containing an RSA DER bu
const PeerId = require('peer-id')
```
### In the Browser through Webpack
Follow our [webpack config](/webpack.config.js) example.
### In the Browser through browserify
TODO: Figure out how to get this working with browserify and webpack. Browserify can't bundle our replacement of ```fs.readFileSync``` with ```require('buffer!./file')```.
> **WIP** Doesn't work out yet
### In the Browser through `<script>` tag

21637
dist/peer-id.js vendored

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "peer-id",
"version": "0.5.3",
"version": "0.6.1",
"description": "IPFS Peer Id implementation in Node.js",
"main": "src/index.js",
"scripts": {
@ -9,7 +9,7 @@
"test:node": "mocha tests/test.js",
"test:browser": "karma start karma.conf.js",
"coverage": "istanbul cover --print both -- _mocha tests/test.js",
"build": "webpack"
"dist": "webpack"
},
"standard": {
"ignore": [

View File

@ -12,8 +12,7 @@ const path = require('path')
const isNode = !global.window
// protobuf read from file
const messages = isNode ? protobuf(fs.readFileSync(path.resolve(__dirname, 'pb/crypto.proto')))
: protobuf(require('buffer!./pb/crypto.proto'))
const messages = isNode ? protobuf(fs.readFileSync(path.resolve(__dirname, 'pb/crypto.proto'))) : protobuf(require('buffer!./pb/crypto.proto'))
exports = module.exports = Id
@ -31,7 +30,6 @@ function Id (id, privKey, pubKey) {
self.id = id // multihash - sha256 - buffer
// pretty print
self.toPrint = function () {
return {
id: self.toB58String(),
@ -41,7 +39,6 @@ function Id (id, privKey, pubKey) {
}
// encode/decode functions
self.toHexString = function () {
return self.id.toString('hex')
}
@ -100,9 +97,15 @@ function formatKey (key, type) {
}
// generation
exports.create = function () {
exports.create = function (opts) {
opts = opts || {}
opts.bits = opts.bits || 2048
// generate keys
const pair = forge.rsa.generateKeyPair({ bits: 2048, e: 0x10001 })
const pair = forge.rsa.generateKeyPair({
bits: opts.bits,
e: 0x10001
})
// return the RSA public/private key to asn1 object
const asnPub = forge.pki.publicKeyToAsn1(pair.publicKey)
@ -112,9 +115,13 @@ exports.create = function () {
const protoPublic64 = formatKey(asnPub, 'Public')
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')
return new Id(mhId, protoPrivate64, protoPublic64)
return new Id(mhId, bufProtoPriv64, bufProtoPub64)
}
exports.createFromHexString = function (str) {
@ -129,12 +136,14 @@ exports.createFromB58String = function (str) {
return new Id(new Buffer(base58.decode(str)))
}
// Public Key input will be a buffer
exports.createFromPubKey = function (pubKey) {
const buf = new Buffer(pubKey, 'base64')
const mhId = multihashing(buf, 'sha2-256')
return new Id(mhId, null, pubKey)
}
// Private key input will be a string
exports.createFromPrivKey = function (privKey) {
// create a buffer from the base64 encoded string
const buf = new Buffer(privKey, 'base64')
@ -159,6 +168,9 @@ exports.createFromPrivKey = function (privKey) {
// format the public key
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')
return new Id(mhId, privKey, protoPublic64)
return new Id(mhId, privKey, bufProtoPub64)
}

View File

@ -55,5 +55,19 @@ describe('id', function (done) {
expect(testIdB58String).to.equal(id.toB58String())
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()
})
it('Non-default # of bits', (done) => {
const shortId = PeerId.create({ bits: 128 })
const longId = PeerId.create({ bits: 256 })
expect(shortId.privKey.length).is.below(longId.privKey.length)
done()
})
})