Merge pull request #3 from diasdavid/update

tests, browser tests, move to RSA based key (instead of ECDH), conform with go-ipfs
This commit is contained in:
David Dias 2015-11-05 18:51:21 +00:00
commit e3cc273a14
7 changed files with 24342 additions and 30 deletions

1
.zuul.yml Normal file
View File

@ -0,0 +1 @@
ui: tape

View File

@ -1,9 +1,8 @@
peer-id JavaScript implementation
==============================
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [[![](https://img.shields.io/badge/freejs-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) ![Build Status](https://travis-ci.org/diasdavid/js-peer-id.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-peer-id) ![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square) [![Dependency Status](https://david-dm.org/diasdavid/js-peer-id.svg?style=flat-square)](https://david-dm.org/diasdavid/js-peer-id) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
> IPFS Peer Id implementation in Node.js
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) ![Build Status](https://travis-ci.org/diasdavid/js-peer-id.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-peer-id) ![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square) [![Dependency Status](https://david-dm.org/diasdavid/js-peer-id.svg?style=flat-square)](https://david-dm.org/diasdavid/js-peer-id) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
> IPFS Peer Id implementation in JavaScript
# Description
@ -11,34 +10,54 @@ A IPFS Peer Id is based on a sha256 has of the peer public key, using [multihash
# Usage
### Installing
### In Node.js through npm
```bash
$ npm install --save peer-id
```
$ npm install peer-id
```javascript
var PeerId = require('peer-id')
```
### In the Browser through browserify
Same as in Node.js, you just have to [browserify](https://github.com/substack/node-browserify) the code before serving it. See the browserify repo for how to do that.
### In the Browser through `<script>` tag
Make the [peer-id.min.js](/dist/peer-id.min.js) available through your server and load it using a normal `<script>` tag, this will export the `peerId` constructor on the `window` object, such that:
```JavaScript
var PeerId = window.PeerId
```
#### Gotchas
You will need to use Node.js `Buffer` API compatible, if you are running inside the browser, you can access it by `PeerId.Buffer` or you can install Feross's [Buffer](https://github.com/feross/buffer).
### Creating a new Id
```
var PeerId = require('ipfs-peer')
// Create a new Id
var id = new PeerId.create()
var id = PeerId.create()
// Recreate an Id from Hex string
var id = new PeerId.createFromHexString(str)
var id = PeerId.createFromHexString(str)
// Recreate an Id from a Buffer
var id = new PeerId.createFromBytes(buf)
var id = PeerId.createFromBytes(buf)
// Recreate an B58 String
var id = new PeerId.createFromB58String(str)
var id = PeerId.createFromB58String(str)
// Recreate from a Public Key
var id = new PeerId.createFromPubKey(pubKey)
var id = PeerId.createFromPubKey(pubKey)
// Recreate from a Private Key
var id = new PeerId.createFromPrivKey(privKey)
var id = PeerId.createFromPrivKey(privKey)
```
### Exporting an Id

24225
dist/peer-id.js vendored Normal file

File diff suppressed because it is too large Load Diff

11
dist/peer-id.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -4,11 +4,12 @@
"description": "IPFS Peer Id implementation in Node.js",
"main": "src/index.js",
"scripts": {
"test": "./node_modules/.bin/lab tests/*-test.js",
"coverage": "./node_modules/.bin/lab -t 100 tests/*-test.js",
"codestyle": "./node_modules/.bin/standard --format",
"lint": "jshint .",
"validate": "npm ls"
"test:node": "node tests/*-test.js",
"lint": "standard",
"test": "npm run test:node",
"test:browser": "./node_modules/.bin/zuul --browser-version $BROWSER_VERSION --browser-name $BROWSER_NAME -- tests/id-test.js",
"test:browser:q": "BROWSER_VERSION=46 BROWSER_NAME=chrome npm run test:browser",
"build": "./node_modules/.bin/browserify -s PeerId -e ./src/index.js | tee dist/peer-id.js | ./node_modules/.bin/uglifyjs -m > dist/peer-id.min.js"
},
"keywords": [
"IPFS"
@ -16,24 +17,30 @@
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"pre-commit": [
"codestyle",
"lint",
"test"
],
"standard": {
"ignore": [
"dist"
]
},
"engines": {
"node": "^4.0.0"
"node": "^4.2.0"
},
"bugs": {
"url": "https://github.com/diasdavid/js-peer-id/issues"
},
"homepage": "https://github.com/diasdavid/js-peer-id",
"devDependencies": {
"code": "^1.4.1",
"lab": "^5.13.0",
"pre-commit": "^1.1.1",
"standard": "^4.5.2"
"standard": "^5.3.1",
"tape": "^4.2.2",
"zuul": "^3.7.2"
},
"dependencies": {
"bs58": "^2.0.1",
"multihashing": "^0.1.3"
"bs58": "^3.0.0",
"keypair": "^1.0.0",
"multihashing": "^0.2.0"
}
}

View File

@ -4,10 +4,12 @@
var multihashing = require('multihashing')
var base58 = require('bs58')
var crypto = require('crypto')
var keypair = require('keypair')
exports = module.exports = Id
exports.Buffer = Buffer
function Id (id, privKey, pubKey) {
var self = this
@ -23,7 +25,7 @@ function Id (id, privKey, pubKey) {
self.toPrint = function () {
return {
id: id.toHexString(),
id: self.toHexString(),
privKey: privKey.toString('hex'),
pubKey: pubKey.toString('hex')
}
@ -42,18 +44,16 @@ function Id (id, privKey, pubKey) {
self.toB58String = function () {
return base58.encode(self.id)
}
}
// generation
exports.create = function () {
var ecdh = crypto.createECDH('secp256k1')
ecdh.generateKeys()
var pair = keypair()
var mhId = multihashing(ecdh.getPublicKey(), 'sha2-256')
var mhId = multihashing(pair.public, 'sha2-256')
return new Id(mhId, ecdh.getPrivateKey(), ecdh.getPublicKey())
return new Id(mhId, pair.private, pair.public)
}
exports.createFromHexString = function (str) {

View File

@ -0,0 +1,49 @@
var test = require('tape')
var PeerId = require('../src')
var testId = {
id: '1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d',
privKey: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAgPEi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C\nqANV1okl2r1Cor0aMOxYW4J6YVpOatAUl/kLcnjw1lw1pnqPBODQ006zoHjEUwsdvUMz/KR2\nHf/rn8hhcGcS+ajwfuOOS8Rx5tYt6vvf9U6QsRKpmeNj1x06K4vsyMKtU3/iZdwZEz8b7MWY\n44AxcCgNSX+A8icJAoGAI3VnVV+gjD7NdnBcNAZv66Fe/qv24J6WeOAMzvxOkS4sVx7HOnCu\nqAkgvM37hyrIp0phRZerEkTuai3TErpRFE2mZgqTQlbtvsMGN7jXVYmDt6Yt5BuRLaFCiteZ\nzi/U0ybsSu+p/OpjRGrbnvwWCekXUJDo4W2t5QCM27XHP1ECgYBbKYbcswxeBha2au4Cj3cf\nz7oW5khGNziJELJSG8ulUNtR0LkfS429JXWjo3qYVYPBeEPePoYE1qcrDnSQesYt2yq1y5Uh\nJjwAxK1wVGw9UcQl0w/utuDxcGCArlszFcRNPX1g/5e0F07OXM8bF9gQcom1HZBKgLaoLStI\nz94OOA==\n-----END RSA PRIVATE KEY-----\n',
pubKey: '-----BEGIN RSA PUBLIC KEY-----\nMIIBCgKCAQEAgPEiGHOwFEUdo95/DaALH69umbFI4xD3Jmla0hiHbkcW535arBfFd8nJs5VP\nt49sgdSgn1ZmiqmHLgMwMz6mKplu4GsmWj5mjdyxiNl5z6R2rF+ZziuiwRTeHVX/8zR8M7Cb\nh0QXmzpoq6LcNOFHbg495zsbmT9QAtjVsS1KyF5324mxbTZtjaD6hxJkAL8aVi0ikvhAL4Hu\nZ1m3brjSSZ0+epFCFL7UIoJlFfOvap2sAyxdOrSvY2PXKTE00s51YTin5+CrvofRLCJPROls\n1oFkhXIDGfuTGTxMxe3hSUlNj0LjQi6RXPGat/5XH0nCuFTODmyhrnnnx51OdgT9vwIDAQAB\n-----END RSA PUBLIC KEY-----\n'
}
var testIdHex = '1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d'
var testIdBytes = new Buffer('1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d', 'hex')
var testIdB58String = 'QmPm2sunRFpswBAByqunK5Yk8PLj7mxL5HpCS4Qg6p7LdS'
test('create a new Id', function (t) {
var id = PeerId.create()
t.ok(id)
t.end()
})
test('recreate an Id from Hex string', function (t) {
var id = PeerId.createFromHexString(testIdHex)
t.ok(id)
t.end()
})
test('Recreate an Id from a Buffer', function (t) {
var id = PeerId.createFromBytes(testIdBytes)
t.ok(id)
t.end()
})
test('Recreate an B58 String', function (t) {
var id = PeerId.createFromB58String(testIdB58String)
t.ok(id)
t.end()
})
test('Recreate from a Public Key', function (t) {
var id = PeerId.createFromPubKey(testId.pubKey)
t.ok(id)
t.end()
})
test('Recreate from a Private Key', function (t) {
// TODO
t.end()
})