mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-07-04 06:01:38 +00:00
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
bd185f8081 | |||
d4c3de1ecb | |||
b692394116 | |||
a8e591525b | |||
6ad5d85f76 | |||
ca9cef6de7 | |||
f55eebcef7 | |||
6442bb4b59 | |||
33ff2aa608 | |||
bbeb6fc0f6 | |||
f518ced776 | |||
73b42ff2aa |
10
README.md
10
README.md
@ -28,16 +28,20 @@ The public key is a base64 encoded string of a protobuf containing an RSA DER bu
|
|||||||
const PeerId = require('peer-id')
|
const PeerId = require('peer-id')
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### In the Browser through Webpack
|
||||||
|
|
||||||
|
Follow our [webpack config](/webpack.config.js) example.
|
||||||
|
|
||||||
### 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.
|
> **WIP** Doesn't work out yet
|
||||||
|
|
||||||
### 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
|
||||||
const PeerId = window.PeerId
|
const Id = PeerId
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Gotchas
|
#### Gotchas
|
||||||
|
75722
dist/peer-id.js
vendored
Normal file
75722
dist/peer-id.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -17,6 +17,10 @@ module.exports = (config) => {
|
|||||||
},
|
},
|
||||||
|
|
||||||
webpack: {
|
webpack: {
|
||||||
|
output: {
|
||||||
|
path: path.join(__dirname, 'dist'),
|
||||||
|
filename: 'bundle.js'
|
||||||
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
extensions: ['', '.js', '.json'],
|
extensions: ['', '.js', '.json'],
|
||||||
alias: { 'node-forge': path.resolve(__dirname, 'deps/forge.bundle.js') }
|
alias: { 'node-forge': path.resolve(__dirname, 'deps/forge.bundle.js') }
|
||||||
|
24
package.json
24
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "peer-id",
|
"name": "peer-id",
|
||||||
"version": "0.5.1",
|
"version": "0.6.0",
|
||||||
"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",
|
||||||
|
"dist": "webpack"
|
||||||
|
},
|
||||||
|
"standard": {
|
||||||
|
"ignore": [
|
||||||
|
"dist",
|
||||||
|
"deps"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"IPFS"
|
"IPFS"
|
||||||
@ -19,12 +26,6 @@
|
|||||||
"lint",
|
"lint",
|
||||||
"test"
|
"test"
|
||||||
],
|
],
|
||||||
"standard": {
|
|
||||||
"ignore": [
|
|
||||||
"dist",
|
|
||||||
"deps"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "^4.3.0"
|
"node": "^4.3.0"
|
||||||
},
|
},
|
||||||
@ -33,6 +34,7 @@
|
|||||||
},
|
},
|
||||||
"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",
|
||||||
@ -47,12 +49,16 @@
|
|||||||
"mocha": "^2.4.5",
|
"mocha": "^2.4.5",
|
||||||
"pre-commit": "^1.1.1",
|
"pre-commit": "^1.1.1",
|
||||||
"standard": "^6.0.7",
|
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
18
src/index.js
18
src/index.js
@ -12,8 +12,7 @@ const path = require('path')
|
|||||||
const isNode = !global.window
|
const isNode = !global.window
|
||||||
|
|
||||||
// protobuf read from file
|
// protobuf read from file
|
||||||
const messages = isNode ? protobuf(fs.readFileSync(path.resolve(__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
|
||||||
|
|
||||||
@ -31,7 +30,6 @@ function Id (id, privKey, pubKey) {
|
|||||||
self.id = id // multihash - sha256 - buffer
|
self.id = id // multihash - sha256 - buffer
|
||||||
|
|
||||||
// pretty print
|
// pretty print
|
||||||
|
|
||||||
self.toPrint = function () {
|
self.toPrint = function () {
|
||||||
return {
|
return {
|
||||||
id: self.toB58String(),
|
id: self.toB58String(),
|
||||||
@ -41,7 +39,6 @@ function Id (id, privKey, pubKey) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// encode/decode functions
|
// encode/decode functions
|
||||||
|
|
||||||
self.toHexString = function () {
|
self.toHexString = function () {
|
||||||
return self.id.toString('hex')
|
return self.id.toString('hex')
|
||||||
}
|
}
|
||||||
@ -112,9 +109,13 @@ exports.create = function () {
|
|||||||
const protoPublic64 = formatKey(asnPub, 'Public')
|
const protoPublic64 = formatKey(asnPub, 'Public')
|
||||||
const protoPrivate64 = formatKey(asnPriv, 'Private')
|
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')
|
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) {
|
exports.createFromHexString = function (str) {
|
||||||
@ -129,12 +130,14 @@ exports.createFromB58String = function (str) {
|
|||||||
return new Id(new Buffer(base58.decode(str)))
|
return new Id(new Buffer(base58.decode(str)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 Id(mhId, null, pubKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Private key input will be a string
|
||||||
exports.createFromPrivKey = function (privKey) {
|
exports.createFromPrivKey = function (privKey) {
|
||||||
// create a buffer from the base64 encoded string
|
// create a buffer from the base64 encoded string
|
||||||
const buf = new Buffer(privKey, 'base64')
|
const buf = new Buffer(privKey, 'base64')
|
||||||
@ -159,6 +162,9 @@ exports.createFromPrivKey = function (privKey) {
|
|||||||
|
|
||||||
// format the public key
|
// format the public key
|
||||||
const protoPublic64 = formatKey(asnPub, 'Public')
|
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')
|
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
|
||||||
return new Id(mhId, privKey, protoPublic64)
|
return new Id(mhId, privKey, bufProtoPub64)
|
||||||
}
|
}
|
||||||
|
@ -55,5 +55,12 @@ describe('id', function (done) {
|
|||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
done()
|
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()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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