mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-07-04 20:51:42 +00:00
Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
7635cfcfda | |||
afbd96edda | |||
c1e18ee420 | |||
b358530dfb | |||
4afbedb525 | |||
e9b107fc55 | |||
855a97956d | |||
dee54d3dcc | |||
167b726dad | |||
3b9f8af2e7 | |||
771b994d7e | |||
524fc37877 | |||
3c55473a45 | |||
8771627ce4 |
15
.travis.yml
15
.travis.yml
@ -1,11 +1,17 @@
|
|||||||
|
sudo: false
|
||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
node_js:
|
||||||
- 4
|
- 4
|
||||||
- 5
|
- 5
|
||||||
|
|
||||||
|
# Make sure we have new NPM.
|
||||||
before_install:
|
before_install:
|
||||||
- npm i -g npm
|
- npm install -g npm
|
||||||
# Workaround for a permissions issue with Travis virtual machine images
|
|
||||||
|
script:
|
||||||
|
- npm run lint
|
||||||
|
- npm test
|
||||||
|
- npm run coverage
|
||||||
|
|
||||||
addons:
|
addons:
|
||||||
firefox: 'latest'
|
firefox: 'latest'
|
||||||
@ -14,6 +20,5 @@ before_script:
|
|||||||
- export DISPLAY=:99.0
|
- export DISPLAY=:99.0
|
||||||
- sh -e /etc/init.d/xvfb start
|
- sh -e /etc/init.d/xvfb start
|
||||||
|
|
||||||
script:
|
after_success:
|
||||||
- npm run lint
|
- npm run coverage-publish
|
||||||
- npm test
|
|
||||||
|
11
README.md
11
README.md
@ -3,7 +3,7 @@
|
|||||||
[](http://ipn.io)
|
[](http://ipn.io)
|
||||||
[](http://webchat.freenode.net/?channels=%23ipfs)
|
[](http://webchat.freenode.net/?channels=%23ipfs)
|
||||||
[](https://travis-ci.org/diasdavid/js-peer-id)
|
[](https://travis-ci.org/diasdavid/js-peer-id)
|
||||||

|
[](https://coveralls.io/github/diasdavid/js-peer-id?branch=master)
|
||||||
[](https://david-dm.org/diasdavid/js-peer-id)
|
[](https://david-dm.org/diasdavid/js-peer-id)
|
||||||
[](https://github.com/feross/standard)
|
[](https://github.com/feross/standard)
|
||||||
|
|
||||||
@ -132,15 +132,16 @@ Returns an object with the ID's properties in hex format:
|
|||||||
|
|
||||||
Returns the Peer ID's `id` as a hex string.
|
Returns the Peer ID's `id` as a hex string.
|
||||||
|
|
||||||
```js
|
```
|
||||||
1220d6243998f2fc56343ad7ed0342ab7886a4eb18d736f1b67d44b37fcc81e0f39f```
|
1220d6243998f2fc56343ad7ed0342ab7886a4eb18d736f1b67d44b37fcc81e0f39f
|
||||||
|
```
|
||||||
|
|
||||||
### id.toBytes()
|
### id.toBytes()
|
||||||
|
|
||||||
Returns the Peer ID's `id` as a buffer.
|
Returns the Peer ID's `id` as a buffer.
|
||||||
|
|
||||||
|
|
||||||
```js
|
```
|
||||||
<Buffer 12 20 d6 24 39 98 f2 fc 56 34 3a d7 ed 03 42 ab 78 86 a4 eb 18 d7 36 f1 b6 7d 44 b3 7f cc 81 e0 f3 9f>
|
<Buffer 12 20 d6 24 39 98 f2 fc 56 34 3a d7 ed 03 42 ab 78 86 a4 eb 18 d7 36 f1 b6 7d 44 b3 7f cc 81 e0 f3 9f>
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -148,7 +149,7 @@ Returns the Peer ID's `id` as a buffer.
|
|||||||
|
|
||||||
Returns the Peer ID's `id` as a base58 string.
|
Returns the Peer ID's `id` as a base58 string.
|
||||||
|
|
||||||
```js
|
```
|
||||||
QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi
|
QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi
|
||||||
```
|
```
|
||||||
|
|
||||||
|
12
circle.yml
Normal file
12
circle.yml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
machine:
|
||||||
|
node:
|
||||||
|
version: stable
|
||||||
|
|
||||||
|
dependencies:
|
||||||
|
pre:
|
||||||
|
- google-chrome --version
|
||||||
|
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
|
||||||
|
- sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
|
||||||
|
- sudo apt-get update
|
||||||
|
- sudo apt-get --only-upgrade install google-chrome-stable
|
||||||
|
- google-chrome --version
|
38
package.json
38
package.json
@ -1,17 +1,19 @@
|
|||||||
{
|
{
|
||||||
"name": "peer-id",
|
"name": "peer-id",
|
||||||
"version": "0.6.5",
|
"version": "0.6.7",
|
||||||
"description": "IPFS Peer Id implementation in Node.js",
|
"description": "IPFS Peer Id implementation in Node.js",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"jsnext": "src/index.js",
|
"bin": "src/bin.js",
|
||||||
|
"jsnext:main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "dignified-lint",
|
"lint": "aegir-lint",
|
||||||
"build": "dignified-build",
|
"build": "aegir-build",
|
||||||
"test": "dignified-test",
|
"test": "aegir-test",
|
||||||
"test:node": "dignified-test node",
|
"test:node": "aegir-test node",
|
||||||
"test:browser": "dignified-test browser",
|
"test:browser": "aegir-test browser",
|
||||||
"release": "dignified-release",
|
"release": "aegir-release",
|
||||||
"coverage": "dignified-coverage"
|
"coverage": "aegir-coverage",
|
||||||
|
"coverage-publish": "aegir-coverage publish"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"IPFS"
|
"IPFS"
|
||||||
@ -30,11 +32,10 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/diasdavid/js-peer-id",
|
"homepage": "https://github.com/diasdavid/js-peer-id",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"aegir": "^3.0.4",
|
||||||
"buffer-loader": "0.0.1",
|
"buffer-loader": "0.0.1",
|
||||||
"chai": "^3.5.0",
|
"chai": "^3.5.0",
|
||||||
"dignified.js": "^1.0.0",
|
"pre-commit": "^1.1.2"
|
||||||
"istanbul": "^0.4.2",
|
|
||||||
"pre-commit": "^1.1.1"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bs58": "^3.0.0",
|
"bs58": "^3.0.0",
|
||||||
@ -46,7 +47,7 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/diasdavid/js-peer-id.git"
|
"url": "https://github.com/diasdavid/js-peer-id.git"
|
||||||
},
|
},
|
||||||
"dignified": {
|
"aegir": {
|
||||||
"webpack": {
|
"webpack": {
|
||||||
"resolve": {
|
"resolve": {
|
||||||
"alias": {
|
"alias": {
|
||||||
@ -54,5 +55,12 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
"contributors": [
|
||||||
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
|
"David Dias <mail@daviddias.me>",
|
||||||
|
"Stephen Whitmore <stephen.whitmore@gmail.com>",
|
||||||
|
"dignifiedquire <dignifiedquire@gmail.com>",
|
||||||
|
"nginnever <ginneversource@gmail.com>"
|
||||||
|
]
|
||||||
|
}
|
7
src/bin.js
Executable file
7
src/bin.js
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#!/usr/local/bin/node
|
||||||
|
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const PeerId = require('./index.js')
|
||||||
|
|
||||||
|
console.log(JSON.stringify(PeerId.create().toJSON(), null, ' '))
|
61
src/index.js
61
src/index.js
@ -1,6 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* Id is an object representation of a peer Id. a peer Id is a multihash
|
* Id is an object representation of a peer Id. a peer Id is a multihash
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
@ -10,17 +11,16 @@ const forge = require('node-forge')
|
|||||||
const protobuf = require('protocol-buffers')
|
const protobuf = require('protocol-buffers')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
|
||||||
// protobuf read from file
|
const pbCrypto = protobuf(fs.readFileSync(path.resolve(__dirname, '../protos/crypto.proto')))
|
||||||
const messages = protobuf(fs.readFileSync(path.resolve(__dirname, '../protos/crypto.proto')))
|
|
||||||
|
|
||||||
exports = module.exports = Id
|
exports = module.exports = PeerId
|
||||||
|
|
||||||
exports.Buffer = Buffer
|
exports.Buffer = Buffer
|
||||||
|
|
||||||
function Id (id, privKey, pubKey) {
|
function PeerId (id, privKey, pubKey) {
|
||||||
const self = this
|
const self = this
|
||||||
|
|
||||||
if (!(self instanceof Id)) {
|
if (!(self instanceof PeerId)) {
|
||||||
throw new Error('Id must be called with new')
|
throw new Error('Id must be called with new')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,6 +37,14 @@ function Id (id, privKey, pubKey) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.toJSON = function () {
|
||||||
|
return {
|
||||||
|
id: self.id.toString('hex'),
|
||||||
|
privKey: self.privKey.toString('hex'),
|
||||||
|
pubKey: self.pubKey.toString('hex')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// encode/decode functions
|
// encode/decode functions
|
||||||
self.toHexString = function () {
|
self.toHexString = function () {
|
||||||
return self.id.toString('hex')
|
return self.id.toString('hex')
|
||||||
@ -52,23 +60,25 @@ function Id (id, privKey, pubKey) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// unwrap the private key protobuf
|
// unwrap the private key protobuf
|
||||||
function unmarshal (key) {
|
function keyUnmarshal (key) {
|
||||||
return messages.PrivateKey.decode(key)
|
return pbCrypto.PrivateKey.decode(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a public key protobuf to be base64 string stored in config
|
// create a public key protobuf to be base64 string stored in config
|
||||||
function marshal (data, type) {
|
function keyMarshal (data, type) {
|
||||||
var epb
|
const RSA = 0
|
||||||
|
|
||||||
|
let epb
|
||||||
if (type === 'Public') {
|
if (type === 'Public') {
|
||||||
epb = messages.PublicKey.encode({
|
epb = pbCrypto.PublicKey.encode({
|
||||||
Type: 0,
|
Type: RSA,
|
||||||
Data: data
|
Data: data
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'Private') {
|
if (type === 'Private') {
|
||||||
epb = messages.PrivateKey.encode({
|
epb = pbCrypto.PrivateKey.encode({
|
||||||
Type: 0,
|
Type: RSA,
|
||||||
Data: data
|
Data: data
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -88,10 +98,10 @@ function formatKey (key, type) {
|
|||||||
const nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary')
|
const nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary')
|
||||||
|
|
||||||
// protobuf the new DER bytes to the PublicKey Data: field
|
// protobuf the new DER bytes to the PublicKey Data: field
|
||||||
const marshalKey = marshal(nDerBuf, type)
|
const marsheledKey = keyMarshal(nDerBuf, type)
|
||||||
|
|
||||||
// encode the protobuf public key to base64 string
|
// encode the protobuf public key to base64 string
|
||||||
const b64 = marshalKey.toString('base64')
|
const b64 = marsheledKey.toString('base64')
|
||||||
return b64
|
return b64
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,26 +130,26 @@ exports.create = function (opts) {
|
|||||||
|
|
||||||
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
|
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
|
||||||
|
|
||||||
return new Id(mhId, bufProtoPriv64, bufProtoPub64)
|
return new PeerId(mhId, bufProtoPriv64, bufProtoPub64)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromHexString = function (str) {
|
exports.createFromHexString = function (str) {
|
||||||
return new Id(new Buffer(str, 'hex'))
|
return new PeerId(new Buffer(str, 'hex'))
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromBytes = function (buf) {
|
exports.createFromBytes = function (buf) {
|
||||||
return new Id(buf)
|
return new PeerId(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromB58String = function (str) {
|
exports.createFromB58String = function (str) {
|
||||||
return new Id(new Buffer(base58.decode(str)))
|
return new PeerId(new Buffer(base58.decode(str)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Public Key input will be a buffer
|
// 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 PeerId(mhId, null, pubKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private key input will be a string
|
// Private key input will be a string
|
||||||
@ -148,7 +158,7 @@ exports.createFromPrivKey = function (privKey) {
|
|||||||
const buf = new Buffer(privKey, 'base64')
|
const buf = new Buffer(privKey, 'base64')
|
||||||
|
|
||||||
// get the private key data from the protobuf
|
// get the private key data from the protobuf
|
||||||
const mpk = unmarshal(buf)
|
const mpk = keyUnmarshal(buf)
|
||||||
|
|
||||||
// create a forge buffer
|
// create a forge buffer
|
||||||
const fbuf = forge.util.createBuffer(mpk.Data.toString('binary'))
|
const fbuf = forge.util.createBuffer(mpk.Data.toString('binary'))
|
||||||
@ -171,5 +181,12 @@ exports.createFromPrivKey = function (privKey) {
|
|||||||
// buffer the public key for consistency before storing
|
// buffer the public key for consistency before storing
|
||||||
const bufProtoPub64 = new Buffer(protoPublic64, 'base64')
|
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, bufProtoPub64)
|
return new PeerId(mhId, privKey, bufProtoPub64)
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.createFromJSON = function (obj) {
|
||||||
|
return new PeerId(
|
||||||
|
new Buffer(obj.id, 'hex'),
|
||||||
|
new Buffer(obj.privKey, 'hex'),
|
||||||
|
new Buffer(obj.pubKey, 'hex'))
|
||||||
}
|
}
|
||||||
|
@ -93,4 +93,12 @@ describe('id', function (done) {
|
|||||||
expect(id.toBytes().toString('hex')).to.equal(testIdBytes.toString('hex'))
|
expect(id.toBytes().toString('hex')).to.equal(testIdBytes.toString('hex'))
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('toJSON', (done) => {
|
||||||
|
const id = PeerId.create()
|
||||||
|
expect(id.toB58String()).to.equal(PeerId.createFromJSON(id.toJSON()).toB58String())
|
||||||
|
expect(id.privKey).to.deep.equal(PeerId.createFromJSON(id.toJSON()).privKey)
|
||||||
|
expect(id.pubKey).to.deep.equal(PeerId.createFromJSON(id.toJSON()).pubKey)
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user