mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-07-05 09:41:37 +00:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
6442bb4b59 | |||
33ff2aa608 | |||
bbeb6fc0f6 | |||
f518ced776 | |||
73b42ff2aa | |||
b3e3dbe3c0 | |||
b2b90bfbb5 | |||
d9113d55e0 | |||
59b89039b6 |
@ -1 +0,0 @@
|
|||||||
node_modules
|
|
10
.jshintrc
10
.jshintrc
@ -1,10 +0,0 @@
|
|||||||
{
|
|
||||||
"node": true,
|
|
||||||
|
|
||||||
"curly": true,
|
|
||||||
"latedef": true,
|
|
||||||
"quotmark": true,
|
|
||||||
"undef": true,
|
|
||||||
"unused": true,
|
|
||||||
"trailing": true
|
|
||||||
}
|
|
33
README.md
33
README.md
@ -1,7 +1,12 @@
|
|||||||
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 JavaScript
|
||||||
|
|
||||||
# Description
|
# Description
|
||||||
@ -16,23 +21,23 @@ The public key is a base64 encoded string of a protobuf containing an RSA DER bu
|
|||||||
### In Node.js through npm
|
### In Node.js through npm
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ npm install --save peer-id
|
> npm install --save peer-id
|
||||||
```
|
```
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var PeerId = require('peer-id')
|
const PeerId = require('peer-id')
|
||||||
```
|
```
|
||||||
|
|
||||||
### In the Browser through browserify
|
### 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.
|
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')```.
|
||||||
|
|
||||||
### In the Browser through `<script>` tag
|
### 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:
|
Make the [peer-id.js](/dist/peer-id.js) available through your server and load it using a normal `<script>` tag, this will export the `PeerId` object, such that:
|
||||||
|
|
||||||
```JavaScript
|
```JavaScript
|
||||||
var PeerId = window.PeerId
|
const Id = PeerId
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Gotchas
|
#### Gotchas
|
||||||
@ -42,25 +47,25 @@ You will need to use Node.js `Buffer` API compatible, if you are running inside
|
|||||||
### Creating a new Id
|
### Creating a new Id
|
||||||
|
|
||||||
```
|
```
|
||||||
var PeerId = require('ipfs-peer')
|
const PeerId = require('ipfs-peer')
|
||||||
|
|
||||||
// Create a new Id
|
// Create a new Id
|
||||||
var id = PeerId.create()
|
const id = PeerId.create()
|
||||||
|
|
||||||
// Recreate an Id from Hex string
|
// Recreate an Id from Hex string
|
||||||
var id = PeerId.createFromHexString(str)
|
const id = PeerId.createFromHexString(str)
|
||||||
|
|
||||||
// Recreate an Id from a Buffer
|
// Recreate an Id from a Buffer
|
||||||
var id = PeerId.createFromBytes(buf)
|
const id = PeerId.createFromBytes(buf)
|
||||||
|
|
||||||
// Recreate an B58 String
|
// Recreate an B58 String
|
||||||
var id = PeerId.createFromB58String(str)
|
const id = PeerId.createFromB58String(str)
|
||||||
|
|
||||||
// Recreate from a Public Key
|
// Recreate from a Public Key
|
||||||
var id = PeerId.createFromPubKey(pubKey)
|
const id = PeerId.createFromPubKey(pubKey)
|
||||||
|
|
||||||
// Recreate from a Private Key
|
// Recreate from a Private Key
|
||||||
var id = PeerId.createFromPrivKey(privKey)
|
const id = PeerId.createFromPrivKey(privKey)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Exporting an Id
|
### Exporting an Id
|
||||||
|
79162
dist/peer-id.js
vendored
79162
dist/peer-id.js
vendored
File diff suppressed because one or more lines are too long
11
dist/peer-id.min.js
vendored
11
dist/peer-id.min.js
vendored
File diff suppressed because one or more lines are too long
@ -1,7 +1,7 @@
|
|||||||
module.exports = function (config) {
|
module.exports = (config) => {
|
||||||
var path = require('path')
|
const path = require('path')
|
||||||
var node_modules_dir = path.join(__dirname, 'node_modules')
|
const node_modules_dir = path.join(__dirname, 'node_modules')
|
||||||
var deps = [
|
const deps = [
|
||||||
'deps/forge.bundle.js'
|
'deps/forge.bundle.js'
|
||||||
]
|
]
|
||||||
config.set({
|
config.set({
|
||||||
@ -17,9 +17,13 @@ module.exports = function (config) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
webpack: {
|
webpack: {
|
||||||
|
output: {
|
||||||
|
path: path.join(__dirname, 'dist'),
|
||||||
|
filename: 'bundle.js'
|
||||||
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
extensions: ['', '.js', '.json'],
|
extensions: ['', '.js', '.json'],
|
||||||
alias: { 'node-forge': __dirname + '/deps/forge.bundle.js' }
|
alias: { 'node-forge': path.resolve(__dirname, 'deps/forge.bundle.js') }
|
||||||
},
|
},
|
||||||
externals: {
|
externals: {
|
||||||
fs: '{}'
|
fs: '{}'
|
||||||
@ -50,8 +54,8 @@ module.exports = function (config) {
|
|||||||
singleRun: true
|
singleRun: true
|
||||||
})
|
})
|
||||||
|
|
||||||
deps.forEach(function (dep) {
|
deps.forEach((dep) => {
|
||||||
var depPath = path.resolve(node_modules_dir, dep)
|
const depPath = path.resolve(node_modules_dir, dep)
|
||||||
config.webpack.module.noParse.push(depPath)
|
config.webpack.module.noParse.push(depPath)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
28
package.json
28
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "peer-id",
|
"name": "peer-id",
|
||||||
"version": "0.5.0",
|
"version": "0.5.3",
|
||||||
"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": {
|
||||||
@ -8,7 +8,14 @@
|
|||||||
"test": "npm run test:node && npm run test:browser",
|
"test": "npm run test:node && npm run test:browser",
|
||||||
"test:node": "mocha tests/test.js",
|
"test:node": "mocha tests/test.js",
|
||||||
"test:browser": "karma start karma.conf.js",
|
"test:browser": "karma start karma.conf.js",
|
||||||
"coverage": "istanbul cover --print both -- _mocha tests/test.js"
|
"coverage": "istanbul cover --print both -- _mocha tests/test.js",
|
||||||
|
"build": "webpack"
|
||||||
|
},
|
||||||
|
"standard": {
|
||||||
|
"ignore": [
|
||||||
|
"dist",
|
||||||
|
"deps"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"IPFS"
|
"IPFS"
|
||||||
@ -19,20 +26,15 @@
|
|||||||
"lint",
|
"lint",
|
||||||
"test"
|
"test"
|
||||||
],
|
],
|
||||||
"standard": {
|
|
||||||
"ignore": [
|
|
||||||
"dist",
|
|
||||||
"deps"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "^4.2.0"
|
"node": "^4.3.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": {
|
||||||
|
"browserify": "^13.0.0",
|
||||||
"buffer-loader": "0.0.1",
|
"buffer-loader": "0.0.1",
|
||||||
"chai": "^3.5.0",
|
"chai": "^3.5.0",
|
||||||
"istanbul": "^0.4.2",
|
"istanbul": "^0.4.2",
|
||||||
@ -46,13 +48,17 @@
|
|||||||
"karma-webpack": "^1.7.0",
|
"karma-webpack": "^1.7.0",
|
||||||
"mocha": "^2.4.5",
|
"mocha": "^2.4.5",
|
||||||
"pre-commit": "^1.1.1",
|
"pre-commit": "^1.1.1",
|
||||||
"standard": "^5.3.1",
|
"standard": "^6.0.7",
|
||||||
"webpack": "^1.12.13"
|
"webpack": "^1.12.14"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bs58": "^3.0.0",
|
"bs58": "^3.0.0",
|
||||||
"multihashing": "^0.2.0",
|
"multihashing": "^0.2.0",
|
||||||
"node-forge": "^0.6.38",
|
"node-forge": "^0.6.38",
|
||||||
"protocol-buffers": "^3.1.4"
|
"protocol-buffers": "^3.1.4"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/diasdavid/js-peer-id.git"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
66
src/index.js
66
src/index.js
@ -2,24 +2,25 @@
|
|||||||
* 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
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var fs = require('fs')
|
const fs = require('fs')
|
||||||
var multihashing = require('multihashing')
|
const multihashing = require('multihashing')
|
||||||
var base58 = require('bs58')
|
const base58 = require('bs58')
|
||||||
var forge = require('node-forge')
|
const forge = require('node-forge')
|
||||||
var protobuf = require('protocol-buffers')
|
const protobuf = require('protocol-buffers')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
var isNode = !global.window
|
const isNode = !global.window
|
||||||
|
|
||||||
// protobuf read from file
|
// protobuf read from file
|
||||||
var messages = isNode ? protobuf(fs.readFileSync(__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
|
||||||
|
|
||||||
exports.Buffer = Buffer
|
exports.Buffer = Buffer
|
||||||
|
|
||||||
function Id (id, privKey, pubKey) {
|
function Id (id, privKey, pubKey) {
|
||||||
var self = this
|
const self = this
|
||||||
|
|
||||||
if (!(self instanceof Id)) {
|
if (!(self instanceof Id)) {
|
||||||
throw new Error('Id must be called with new')
|
throw new Error('Id must be called with new')
|
||||||
@ -56,8 +57,7 @@ function Id (id, privKey, pubKey) {
|
|||||||
|
|
||||||
// unwrap the private key protobuf
|
// unwrap the private key protobuf
|
||||||
function unmarshal (key) {
|
function unmarshal (key) {
|
||||||
var dpb = messages.PrivateKey.decode(key)
|
return messages.PrivateKey.decode(key)
|
||||||
return dpb
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a public key protobuf to be base64 string stored in config
|
// create a public key protobuf to be base64 string stored in config
|
||||||
@ -83,36 +83,36 @@ function marshal (data, type) {
|
|||||||
// this returns a base64 encoded protobuf of the public key
|
// this returns a base64 encoded protobuf of the public key
|
||||||
function formatKey (key, type) {
|
function formatKey (key, type) {
|
||||||
// create der buffer of public key asn.1 object
|
// create der buffer of public key asn.1 object
|
||||||
var der = forge.asn1.toDer(key)
|
const der = forge.asn1.toDer(key)
|
||||||
|
|
||||||
// create forge buffer of der public key buffer
|
// create forge buffer of der public key buffer
|
||||||
var fDerBuf = forge.util.createBuffer(der.data, 'binary')
|
const fDerBuf = forge.util.createBuffer(der.data, 'binary')
|
||||||
|
|
||||||
// convert forge buffer to node buffer public key
|
// convert forge buffer to node buffer public key
|
||||||
var 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
|
||||||
var marshalKey = marshal(nDerBuf, type)
|
const marshalKey = marshal(nDerBuf, type)
|
||||||
|
|
||||||
// encode the protobuf public key to base64 string
|
// encode the protobuf public key to base64 string
|
||||||
var b64 = marshalKey.toString('base64')
|
const b64 = marshalKey.toString('base64')
|
||||||
return b64
|
return b64
|
||||||
}
|
}
|
||||||
|
|
||||||
// generation
|
// generation
|
||||||
exports.create = function () {
|
exports.create = function () {
|
||||||
// generate keys
|
// generate keys
|
||||||
var pair = forge.rsa.generateKeyPair({ bits: 2048, e: 0x10001 })
|
const pair = forge.rsa.generateKeyPair({ bits: 2048, e: 0x10001 })
|
||||||
|
|
||||||
// return the RSA public/private key to asn1 object
|
// return the RSA public/private key to asn1 object
|
||||||
var asnPub = forge.pki.publicKeyToAsn1(pair.publicKey)
|
const asnPub = forge.pki.publicKeyToAsn1(pair.publicKey)
|
||||||
var asnPriv = forge.pki.privateKeyToAsn1(pair.privateKey)
|
const asnPriv = forge.pki.privateKeyToAsn1(pair.privateKey)
|
||||||
|
|
||||||
// format the keys to protobuf base64 encoded string
|
// format the keys to protobuf base64 encoded string
|
||||||
var protoPublic64 = formatKey(asnPub, 'Public')
|
const protoPublic64 = formatKey(asnPub, 'Public')
|
||||||
var protoPrivate64 = formatKey(asnPriv, 'Private')
|
const protoPrivate64 = formatKey(asnPriv, 'Private')
|
||||||
|
|
||||||
var 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, protoPrivate64, protoPublic64)
|
||||||
}
|
}
|
||||||
@ -130,35 +130,35 @@ exports.createFromB58String = function (str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromPubKey = function (pubKey) {
|
exports.createFromPubKey = function (pubKey) {
|
||||||
var buf = new Buffer(pubKey, 'base64')
|
const buf = new Buffer(pubKey, 'base64')
|
||||||
var mhId = multihashing(buf, 'sha2-256')
|
const mhId = multihashing(buf, 'sha2-256')
|
||||||
return new Id(mhId, null, pubKey)
|
return new Id(mhId, null, pubKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromPrivKey = function (privKey) {
|
exports.createFromPrivKey = function (privKey) {
|
||||||
// create a buffer from the base64 encoded string
|
// create a buffer from the base64 encoded string
|
||||||
var 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
|
||||||
var mpk = unmarshal(buf)
|
const mpk = unmarshal(buf)
|
||||||
|
|
||||||
// create a forge buffer
|
// create a forge buffer
|
||||||
var fbuf = forge.util.createBuffer(mpk.Data.toString('binary'))
|
const fbuf = forge.util.createBuffer(mpk.Data.toString('binary'))
|
||||||
|
|
||||||
// create an asn1 object from the private key bytes saved in the protobuf Data: field
|
// create an asn1 object from the private key bytes saved in the protobuf Data: field
|
||||||
var asnPriv = forge.asn1.fromDer(fbuf)
|
const asnPriv = forge.asn1.fromDer(fbuf)
|
||||||
|
|
||||||
// get the RSA privatekey data from the asn1 object
|
// get the RSA privatekey data from the asn1 object
|
||||||
var privateKey = forge.pki.privateKeyFromAsn1(asnPriv)
|
const privateKey = forge.pki.privateKeyFromAsn1(asnPriv)
|
||||||
|
|
||||||
// set the RSA public key to the modulus and exponent of the private key
|
// set the RSA public key to the modulus and exponent of the private key
|
||||||
var publicKey = forge.pki.rsa.setPublicKey(privateKey.n, privateKey.e)
|
const publicKey = forge.pki.rsa.setPublicKey(privateKey.n, privateKey.e)
|
||||||
|
|
||||||
// return the RSA public key to asn1 object
|
// return the RSA public key to asn1 object
|
||||||
var asnPub = forge.pki.publicKeyToAsn1(publicKey)
|
const asnPub = forge.pki.publicKeyToAsn1(publicKey)
|
||||||
|
|
||||||
// format the public key
|
// format the public key
|
||||||
var protoPublic64 = formatKey(asnPub, 'Public')
|
const protoPublic64 = formatKey(asnPub, 'Public')
|
||||||
var 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, protoPublic64)
|
||||||
}
|
}
|
||||||
|
@ -20,38 +20,38 @@ const testIdB58String = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A'
|
|||||||
describe('id', function (done) {
|
describe('id', function (done) {
|
||||||
this.timeout(30000)
|
this.timeout(30000)
|
||||||
|
|
||||||
it('create a new id', done => {
|
it('create a new id', (done) => {
|
||||||
var id = PeerId.create()
|
const id = PeerId.create()
|
||||||
expect(id.toB58String().length).to.equal(46)
|
expect(id.toB58String().length).to.equal(46)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('recreate an Id from Hex string', done => {
|
it('recreate an Id from Hex string', (done) => {
|
||||||
var id = PeerId.createFromHexString(testIdHex)
|
const id = PeerId.createFromHexString(testIdHex)
|
||||||
expect(testIdBytes).to.deep.equal(id.id)
|
expect(testIdBytes).to.deep.equal(id.id)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Recreate an Id from a Buffer', done => {
|
it('Recreate an Id from a Buffer', (done) => {
|
||||||
var id = PeerId.createFromBytes(testIdBytes)
|
const id = PeerId.createFromBytes(testIdBytes)
|
||||||
expect(testId.id).to.equal(id.toHexString())
|
expect(testId.id).to.equal(id.toHexString())
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Recreate a B58 String', done => {
|
it('Recreate a B58 String', (done) => {
|
||||||
var id = PeerId.createFromB58String(testIdB58String)
|
const id = PeerId.createFromB58String(testIdB58String)
|
||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Recreate from a Public Key', done => {
|
it('Recreate from a Public Key', (done) => {
|
||||||
var id = PeerId.createFromPubKey(testId.pubKey)
|
const id = PeerId.createFromPubKey(testId.pubKey)
|
||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Recreate from a Private Key', done => {
|
it('Recreate from a Private Key', (done) => {
|
||||||
var id = PeerId.createFromPrivKey(testId.privKey)
|
const id = PeerId.createFromPrivKey(testId.privKey)
|
||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
29
webpack.config.js
Normal file
29
webpack.config.js
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
var path = require('path')
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
name: 'peerid',
|
||||||
|
context: __dirname,
|
||||||
|
entry: './src/index.js',
|
||||||
|
output: {
|
||||||
|
path: path.join(__dirname, 'dist'),
|
||||||
|
filename: 'peer-id.js',
|
||||||
|
libraryTarget: 'var',
|
||||||
|
library: 'PeerId'
|
||||||
|
},
|
||||||
|
resolve: {
|
||||||
|
extensions: ['', '.js', '.json'],
|
||||||
|
alias: { 'node-forge': path.resolve(__dirname, 'deps/forge.bundle.js') }
|
||||||
|
},
|
||||||
|
externals: {
|
||||||
|
fs: '{}'
|
||||||
|
},
|
||||||
|
node: {
|
||||||
|
Buffer: true
|
||||||
|
},
|
||||||
|
module: {
|
||||||
|
loaders: [
|
||||||
|
{ test: /\.json$/, loader: 'json' }
|
||||||
|
],
|
||||||
|
noParse: []
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user