mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-04-25 18:32:20 +00:00
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:
commit
e3cc273a14
41
README.md
41
README.md
@ -1,9 +1,8 @@
|
|||||||
peer-id JavaScript implementation
|
peer-id JavaScript implementation
|
||||||
==============================
|
==============================
|
||||||
|
|
||||||
[](http://ipn.io) [[](http://webchat.freenode.net/?channels=%23ipfs) ](https://travis-ci.org/diasdavid/js-peer-id)  [](https://david-dm.org/diasdavid/js-peer-id) [](https://github.com/feross/standard)
|
[](http://ipn.io) [[](http://webchat.freenode.net/?channels=%23ipfs) ](https://travis-ci.org/diasdavid/js-peer-id)  [](https://david-dm.org/diasdavid/js-peer-id) [](https://github.com/feross/standard)
|
||||||
|
> IPFS Peer Id implementation in JavaScript
|
||||||
> IPFS Peer Id implementation in Node.js
|
|
||||||
|
|
||||||
# Description
|
# Description
|
||||||
|
|
||||||
@ -11,34 +10,54 @@ A IPFS Peer Id is based on a sha256 has of the peer public key, using [multihash
|
|||||||
|
|
||||||
# Usage
|
# 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
|
### Creating a new Id
|
||||||
|
|
||||||
```
|
```
|
||||||
var PeerId = require('ipfs-peer')
|
var PeerId = require('ipfs-peer')
|
||||||
|
|
||||||
// Create a new Id
|
// Create a new Id
|
||||||
var id = new PeerId.create()
|
var id = PeerId.create()
|
||||||
|
|
||||||
// Recreate an Id from Hex string
|
// Recreate an Id from Hex string
|
||||||
var id = new PeerId.createFromHexString(str)
|
var id = PeerId.createFromHexString(str)
|
||||||
|
|
||||||
// Recreate an Id from a Buffer
|
// Recreate an Id from a Buffer
|
||||||
var id = new PeerId.createFromBytes(buf)
|
var id = PeerId.createFromBytes(buf)
|
||||||
|
|
||||||
// Recreate an B58 String
|
// Recreate an B58 String
|
||||||
var id = new PeerId.createFromB58String(str)
|
var id = PeerId.createFromB58String(str)
|
||||||
|
|
||||||
// Recreate from a Public Key
|
// Recreate from a Public Key
|
||||||
var id = new PeerId.createFromPubKey(pubKey)
|
var id = PeerId.createFromPubKey(pubKey)
|
||||||
|
|
||||||
// Recreate from a Private Key
|
// Recreate from a Private Key
|
||||||
var id = new PeerId.createFromPrivKey(privKey)
|
var id = PeerId.createFromPrivKey(privKey)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Exporting an Id
|
### Exporting an Id
|
||||||
|
24225
dist/peer-id.js
vendored
Normal file
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
11
dist/peer-id.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
31
package.json
31
package.json
@ -4,11 +4,12 @@
|
|||||||
"description": "IPFS Peer Id implementation in Node.js",
|
"description": "IPFS Peer Id implementation in Node.js",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "./node_modules/.bin/lab tests/*-test.js",
|
"test:node": "node tests/*-test.js",
|
||||||
"coverage": "./node_modules/.bin/lab -t 100 tests/*-test.js",
|
"lint": "standard",
|
||||||
"codestyle": "./node_modules/.bin/standard --format",
|
"test": "npm run test:node",
|
||||||
"lint": "jshint .",
|
"test:browser": "./node_modules/.bin/zuul --browser-version $BROWSER_VERSION --browser-name $BROWSER_NAME -- tests/id-test.js",
|
||||||
"validate": "npm ls"
|
"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": [
|
"keywords": [
|
||||||
"IPFS"
|
"IPFS"
|
||||||
@ -16,24 +17,30 @@
|
|||||||
"author": "David Dias <daviddias@ipfs.io>",
|
"author": "David Dias <daviddias@ipfs.io>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"pre-commit": [
|
"pre-commit": [
|
||||||
"codestyle",
|
"lint",
|
||||||
"test"
|
"test"
|
||||||
],
|
],
|
||||||
|
"standard": {
|
||||||
|
"ignore": [
|
||||||
|
"dist"
|
||||||
|
]
|
||||||
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "^4.0.0"
|
"node": "^4.2.0"
|
||||||
},
|
},
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/diasdavid/js-peer-id/issues"
|
"url": "https://github.com/diasdavid/js-peer-id/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/diasdavid/js-peer-id",
|
"homepage": "https://github.com/diasdavid/js-peer-id",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"code": "^1.4.1",
|
|
||||||
"lab": "^5.13.0",
|
|
||||||
"pre-commit": "^1.1.1",
|
"pre-commit": "^1.1.1",
|
||||||
"standard": "^4.5.2"
|
"standard": "^5.3.1",
|
||||||
|
"tape": "^4.2.2",
|
||||||
|
"zuul": "^3.7.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bs58": "^2.0.1",
|
"bs58": "^3.0.0",
|
||||||
"multihashing": "^0.1.3"
|
"keypair": "^1.0.0",
|
||||||
|
"multihashing": "^0.2.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
14
src/index.js
14
src/index.js
@ -4,10 +4,12 @@
|
|||||||
|
|
||||||
var multihashing = require('multihashing')
|
var multihashing = require('multihashing')
|
||||||
var base58 = require('bs58')
|
var base58 = require('bs58')
|
||||||
var crypto = require('crypto')
|
var keypair = require('keypair')
|
||||||
|
|
||||||
exports = module.exports = Id
|
exports = module.exports = Id
|
||||||
|
|
||||||
|
exports.Buffer = Buffer
|
||||||
|
|
||||||
function Id (id, privKey, pubKey) {
|
function Id (id, privKey, pubKey) {
|
||||||
var self = this
|
var self = this
|
||||||
|
|
||||||
@ -23,7 +25,7 @@ function Id (id, privKey, pubKey) {
|
|||||||
|
|
||||||
self.toPrint = function () {
|
self.toPrint = function () {
|
||||||
return {
|
return {
|
||||||
id: id.toHexString(),
|
id: self.toHexString(),
|
||||||
privKey: privKey.toString('hex'),
|
privKey: privKey.toString('hex'),
|
||||||
pubKey: pubKey.toString('hex')
|
pubKey: pubKey.toString('hex')
|
||||||
}
|
}
|
||||||
@ -42,18 +44,16 @@ function Id (id, privKey, pubKey) {
|
|||||||
self.toB58String = function () {
|
self.toB58String = function () {
|
||||||
return base58.encode(self.id)
|
return base58.encode(self.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// generation
|
// generation
|
||||||
|
|
||||||
exports.create = function () {
|
exports.create = function () {
|
||||||
var ecdh = crypto.createECDH('secp256k1')
|
var pair = keypair()
|
||||||
ecdh.generateKeys()
|
|
||||||
|
|
||||||
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) {
|
exports.createFromHexString = function (str) {
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
var test = require('tape')
|
||||||
|
var PeerId = require('../src')
|
||||||
|
|
||||||
|
var testId = {
|
||||||
|
id: '1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d',
|
||||||
|
privKey: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAgPEiGHOwFEUdo95/DaALH69umbFI4xD3Jmla0hiHbkcW535arBfFd8nJ\ns5VPt49sgdSgn1ZmiqmHLgMwMz6mKplu4GsmWj5mjdyxiNl5z6R2rF+ZziuiwRTeHVX/8zR8\nM7Cbh0QXmzpoq6LcNOFHbg495zsbmT9QAtjVsS1KyF5324mxbTZtjaD6hxJkAL8aVi0ikvhA\nL4HuZ1m3brjSSZ0+epFCFL7UIoJlFfOvap2sAyxdOrSvY2PXKTE00s51YTin5+CrvofRLCJP\nROls1oFkhXIDGfuTGTxMxe3hSUlNj0LjQi6RXPGat/5XH0nCuFTODmyhrnnnx51OdgT9vwID\nAQABAoIBAQAIAnKSwET0zWJM9po/12w5eKVPKMMVT816dlrs6Bcpk4LpuGCbhhJ/IWrFHAZK\nqb8cxX+AxlYyUNuT0SDiXgbmaIeJqz5DptKqB0aD8LZvXpD8nieote8zPT+a5Oe0TNNWRqcy\nnNk2jEdKOiChrEjKnlncDkDloRgwRRXpHp4hmh3XrZwygAekxC+LFhO5YS4fuc5tQAzGyl/O\nGKnEmOtRqz4bYQRTrrfhwtAWdMOC90AEtoIPapLnJPBUujHNn7KLktdrmlPSqxFilIIe3jJH\nH0oG9Nr9ueSNat54NQZr2BBrXliFqXu/SomiAfN0jmouB+8lzNYSoUK25JmbJQExAoGBANLE\nLqJS1DpLa3Mg/GMCeCbjWG6qwpLj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mzpoq6LcNOFHbg495zsbmT9QAtjVsS1KyF5324mxbTZtjaD6hxJkAL8aVi0ikvhAL4Hu\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()
|
||||||
|
})
|
Loading…
x
Reference in New Issue
Block a user