mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-07-04 23:41:48 +00:00
Compare commits
57 Commits
Author | SHA1 | Date | |
---|---|---|---|
f62023f8a7 | |||
95367871ac | |||
352a6cfa17 | |||
727355c4f5 | |||
bd185f8081 | |||
d4c3de1ecb | |||
b692394116 | |||
a8e591525b | |||
6ad5d85f76 | |||
ca9cef6de7 | |||
f55eebcef7 | |||
6442bb4b59 | |||
33ff2aa608 | |||
bbeb6fc0f6 | |||
f518ced776 | |||
73b42ff2aa | |||
b3e3dbe3c0 | |||
b2b90bfbb5 | |||
d9113d55e0 | |||
59b89039b6 | |||
1cdbf19857 | |||
845bd088fe | |||
1f72aaf7b7 | |||
effa21b892 | |||
91bc56eb05 | |||
3cc3cb31f0 | |||
de572bd15f | |||
1a336f9289 | |||
77790571aa | |||
d9b9ca8812 | |||
67c26ec546 | |||
58fe038e07 | |||
61c0c663d6 | |||
8adbdd0d6e | |||
7ae9be8f71 | |||
16c77433f9 | |||
6ce01ab434 | |||
5cb511c646 | |||
370e5c55b1 | |||
e3cc273a14 | |||
2c1874a689 | |||
3c0c9a3b19 | |||
d7ab4bd8fb | |||
9aee5bf670 | |||
5e9bf9837d | |||
f82d0d6ac5 | |||
0b30a0964c | |||
9b2cb012e9 | |||
35102b4ad5 | |||
192c65f09a | |||
dda0204ec0 | |||
1096d15a16 | |||
615a966d6a | |||
171a561537 | |||
fa62838777 | |||
bb2f23633d | |||
d472664924 |
@ -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
|
|
||||||
}
|
|
21
.travis.yml
Normal file
21
.travis.yml
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
language: node_js
|
||||||
|
node_js:
|
||||||
|
- "4.0"
|
||||||
|
|
||||||
|
branches:
|
||||||
|
only:
|
||||||
|
- master
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
- npm i -g npm
|
||||||
|
# Workaround for a permissions issue with Travis virtual machine images
|
||||||
|
|
||||||
|
addons:
|
||||||
|
firefox: 'latest'
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- export DISPLAY=:99.0
|
||||||
|
- sh -e /etc/init.d/xvfb start
|
||||||
|
|
||||||
|
script:
|
||||||
|
- npm test
|
59
README.md
59
README.md
@ -1,44 +1,75 @@
|
|||||||
ipfs-peer-id Node.js implementation
|
peer-id JavaScript implementation
|
||||||
===================================
|
=================================
|
||||||
|
|
||||||
|
[](http://ipn.io)
|
||||||
[](http://webchat.freenode.net/?channels=%23ipfs)
|
[](http://webchat.freenode.net/?channels=%23ipfs)
|
||||||
|
[](https://travis-ci.org/diasdavid/js-peer-id)
|
||||||
> IPFS Peer Id implementation in Node.js
|

|
||||||
|
[](https://david-dm.org/diasdavid/js-peer-id)
|
||||||
|
[](https://github.com/feross/standard)
|
||||||
|
> IPFS Peer Id implementation in JavaScript
|
||||||
|
|
||||||
# Description
|
# Description
|
||||||
|
|
||||||
A IPFS Peer Id is based on a sha256 has of the peer public key, using [multihash](https://github.com/jbenet/multihash)
|
An IPFS Peer Id is based on a sha256 hash of the peer public key, using [multihash](https://github.com/jbenet/multihash)
|
||||||
|
|
||||||
|
The public key is a base64 encoded string of a protobuf containing an RSA DER buffer. This uses a node buffer to pass the base64 encoded public key protobuf to the multihash for ID generation.
|
||||||
|
|
||||||
|
|
||||||
# Usage
|
# Usage
|
||||||
|
|
||||||
### Installing
|
### In Node.js through npm
|
||||||
|
|
||||||
|
```bash
|
||||||
|
> npm install --save peer-id
|
||||||
```
|
```
|
||||||
$ npm install ipfs-peer-id
|
|
||||||
|
```javascript
|
||||||
|
const PeerId = require('peer-id')
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### In the Browser through Webpack
|
||||||
|
|
||||||
|
Follow our [webpack config](/webpack.config.js) example.
|
||||||
|
|
||||||
|
### In the Browser through browserify
|
||||||
|
|
||||||
|
> **WIP** Doesn't work out yet
|
||||||
|
|
||||||
|
### In the Browser through `<script>` tag
|
||||||
|
|
||||||
|
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
|
||||||
|
const Id = 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')
|
const PeerId = require('ipfs-peer')
|
||||||
|
|
||||||
// Create a new Id
|
// Create a new Id
|
||||||
var id = new Id.create()
|
const id = PeerId.create()
|
||||||
|
|
||||||
// Recreate an Id from Hex string
|
// Recreate an Id from Hex string
|
||||||
var id = new Id.createFromHexString(str)
|
const id = PeerId.createFromHexString(str)
|
||||||
|
|
||||||
// Recreate an Id from a Buffer
|
// Recreate an Id from a Buffer
|
||||||
var id = new Id.createFromBytes(buf)
|
const id = PeerId.createFromBytes(buf)
|
||||||
|
|
||||||
// Recreate an B58 String
|
// Recreate an B58 String
|
||||||
var id = new Id.createFromB58String(str)
|
const id = PeerId.createFromB58String(str)
|
||||||
|
|
||||||
// Recreate from a Public Key
|
// Recreate from a Public Key
|
||||||
var id = new Id.createFromPubKey(pubKey)
|
const id = PeerId.createFromPubKey(pubKey)
|
||||||
|
|
||||||
// Recreate from a Private Key
|
// Recreate from a Private Key
|
||||||
var id = new Id.createFromPrivKey(privKey)
|
const id = PeerId.createFromPrivKey(privKey)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Exporting an Id
|
### Exporting an Id
|
||||||
|
29552
deps/forge.bundle.js
vendored
Normal file
29552
deps/forge.bundle.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
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
61
karma.conf.js
Normal file
61
karma.conf.js
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
module.exports = (config) => {
|
||||||
|
const path = require('path')
|
||||||
|
const node_modules_dir = path.join(__dirname, 'node_modules')
|
||||||
|
const deps = [
|
||||||
|
'deps/forge.bundle.js'
|
||||||
|
]
|
||||||
|
config.set({
|
||||||
|
basePath: '',
|
||||||
|
frameworks: ['mocha'],
|
||||||
|
|
||||||
|
files: [
|
||||||
|
'tests/test.js'
|
||||||
|
],
|
||||||
|
|
||||||
|
preprocessors: {
|
||||||
|
'tests/*': ['webpack']
|
||||||
|
},
|
||||||
|
|
||||||
|
webpack: {
|
||||||
|
output: {
|
||||||
|
path: path.join(__dirname, 'dist'),
|
||||||
|
filename: 'bundle.js'
|
||||||
|
},
|
||||||
|
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: []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
webpackMiddleware: {
|
||||||
|
noInfo: true,
|
||||||
|
stats: {
|
||||||
|
colors: true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
reporters: ['spec'],
|
||||||
|
port: 9876,
|
||||||
|
colors: true,
|
||||||
|
logLevel: config.LOG_INFO,
|
||||||
|
autoWatch: false,
|
||||||
|
browsers: process.env.TRAVIS ? ['Firefox'] : ['Chrome'],
|
||||||
|
singleRun: true
|
||||||
|
})
|
||||||
|
|
||||||
|
deps.forEach((dep) => {
|
||||||
|
const depPath = path.resolve(node_modules_dir, dep)
|
||||||
|
config.webpack.module.noParse.push(depPath)
|
||||||
|
})
|
||||||
|
}
|
60
package.json
60
package.json
@ -1,12 +1,21 @@
|
|||||||
{
|
{
|
||||||
"name": "ipfs-peer-id",
|
"name": "peer-id",
|
||||||
"version": "0.3.0",
|
"version": "0.6.1",
|
||||||
"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",
|
"lint": "standard",
|
||||||
"coverage": "./node_modules/.bin/lab -t 100 tests/*-test.js",
|
"test": "npm run test:node && npm run test:browser",
|
||||||
"codestyle": "./node_modules/.bin/standard --format"
|
"test:node": "mocha tests/test.js",
|
||||||
|
"test:browser": "karma start karma.conf.js",
|
||||||
|
"coverage": "istanbul cover --print both -- _mocha tests/test.js",
|
||||||
|
"dist": "webpack"
|
||||||
|
},
|
||||||
|
"standard": {
|
||||||
|
"ignore": [
|
||||||
|
"dist",
|
||||||
|
"deps"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"IPFS"
|
"IPFS"
|
||||||
@ -14,21 +23,42 @@
|
|||||||
"author": "David Dias <daviddias@ipfs.io>",
|
"author": "David Dias <daviddias@ipfs.io>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"pre-commit": [
|
"pre-commit": [
|
||||||
"codestyle",
|
"lint",
|
||||||
"test"
|
"test"
|
||||||
],
|
],
|
||||||
"bugs": {
|
"engines": {
|
||||||
"url": "https://github.com/diasdavid/node-ipfs-peer-id/issues"
|
"node": "^4.3.0"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/diasdavid/node-ipfs-peer-id",
|
"bugs": {
|
||||||
|
"url": "https://github.com/diasdavid/js-peer-id/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/diasdavid/js-peer-id",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"code": "^1.4.1",
|
"browserify": "^13.0.0",
|
||||||
"lab": "^5.13.0",
|
"buffer-loader": "0.0.1",
|
||||||
"precommit-hook": "^3.0.0",
|
"chai": "^3.5.0",
|
||||||
"standard": "^4.5.2"
|
"istanbul": "^0.4.2",
|
||||||
|
"json-loader": "^0.5.4",
|
||||||
|
"karma": "^0.13.19",
|
||||||
|
"karma-chrome-launcher": "^0.2.2",
|
||||||
|
"karma-cli": "^0.1.2",
|
||||||
|
"karma-firefox-launcher": "^0.1.7",
|
||||||
|
"karma-mocha": "^0.2.1",
|
||||||
|
"karma-spec-reporter": "0.0.24",
|
||||||
|
"karma-webpack": "^1.7.0",
|
||||||
|
"mocha": "^2.4.5",
|
||||||
|
"pre-commit": "^1.1.1",
|
||||||
|
"standard": "^6.0.7",
|
||||||
|
"webpack": "^1.12.14"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bs58": "^2.0.1",
|
"bs58": "^3.0.0",
|
||||||
"multihashing": "^0.1.3"
|
"multihashing": "^0.2.0",
|
||||||
|
"node-forge": "^0.6.38",
|
||||||
|
"protocol-buffers": "^3.1.4"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/diasdavid/js-peer-id.git"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
130
src/index.js
130
src/index.js
@ -2,14 +2,24 @@
|
|||||||
* 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 multihashing = require('multihashing')
|
const fs = require('fs')
|
||||||
var base58 = require('bs58')
|
const multihashing = require('multihashing')
|
||||||
var crypto = require('crypto')
|
const base58 = require('bs58')
|
||||||
|
const forge = require('node-forge')
|
||||||
|
const protobuf = require('protocol-buffers')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
|
const isNode = !global.window
|
||||||
|
|
||||||
|
// protobuf read from file
|
||||||
|
const messages = isNode ? protobuf(fs.readFileSync(path.resolve(__dirname, 'pb/crypto.proto'))) : protobuf(require('buffer!./pb/crypto.proto'))
|
||||||
|
|
||||||
exports = module.exports = Id
|
exports = module.exports = Id
|
||||||
|
|
||||||
|
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')
|
||||||
@ -20,17 +30,15 @@ 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: id.toHexString(),
|
id: self.toB58String(),
|
||||||
privKey: privKey.toString('hex'),
|
privKey: privKey.toString('hex'),
|
||||||
pubKey: pubKey.toString('hex')
|
pubKey: 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')
|
||||||
}
|
}
|
||||||
@ -42,22 +50,82 @@ function Id (id, privKey, pubKey) {
|
|||||||
self.toB58String = function () {
|
self.toB58String = function () {
|
||||||
return base58.encode(self.id)
|
return base58.encode(self.id)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// unwrap the private key protobuf
|
||||||
|
function unmarshal (key) {
|
||||||
|
return messages.PrivateKey.decode(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a public key protobuf to be base64 string stored in config
|
||||||
|
function marshal (data, type) {
|
||||||
|
var epb
|
||||||
|
if (type === 'Public') {
|
||||||
|
epb = messages.PublicKey.encode({
|
||||||
|
Type: 0,
|
||||||
|
Data: data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === 'Private') {
|
||||||
|
epb = messages.PrivateKey.encode({
|
||||||
|
Type: 0,
|
||||||
|
Data: data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return epb
|
||||||
|
}
|
||||||
|
|
||||||
|
// this returns a base64 encoded protobuf of the public key
|
||||||
|
function formatKey (key, type) {
|
||||||
|
// create der buffer of public key asn.1 object
|
||||||
|
const der = forge.asn1.toDer(key)
|
||||||
|
|
||||||
|
// create forge buffer of der public key buffer
|
||||||
|
const fDerBuf = forge.util.createBuffer(der.data, 'binary')
|
||||||
|
|
||||||
|
// convert forge buffer to node buffer public key
|
||||||
|
const nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary')
|
||||||
|
|
||||||
|
// protobuf the new DER bytes to the PublicKey Data: field
|
||||||
|
const marshalKey = marshal(nDerBuf, type)
|
||||||
|
|
||||||
|
// encode the protobuf public key to base64 string
|
||||||
|
const b64 = marshalKey.toString('base64')
|
||||||
|
return b64
|
||||||
}
|
}
|
||||||
|
|
||||||
// generation
|
// generation
|
||||||
|
exports.create = function (opts) {
|
||||||
|
opts = opts || {}
|
||||||
|
opts.bits = opts.bits || 2048
|
||||||
|
|
||||||
exports.create = function () {
|
// generate keys
|
||||||
var ecdh = crypto.createECDH('secp256k1')
|
const pair = forge.rsa.generateKeyPair({
|
||||||
ecdh.generateKeys()
|
bits: opts.bits,
|
||||||
|
e: 0x10001
|
||||||
|
})
|
||||||
|
|
||||||
var mhId = multihashing(ecdh.getPublicKey(), 'sha2-256')
|
// return the RSA public/private key to asn1 object
|
||||||
|
const asnPub = forge.pki.publicKeyToAsn1(pair.publicKey)
|
||||||
|
const asnPriv = forge.pki.privateKeyToAsn1(pair.privateKey)
|
||||||
|
|
||||||
return new Id(mhId, ecdh.getPrivateKey(), ecdh.getPublicKey())
|
// format the keys to protobuf base64 encoded string
|
||||||
|
const protoPublic64 = formatKey(asnPub, 'Public')
|
||||||
|
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')
|
||||||
|
|
||||||
|
return new Id(mhId, bufProtoPriv64, bufProtoPub64)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromHexString = function (str) {
|
exports.createFromHexString = function (str) {
|
||||||
return new Id(new Buffer(str), 'hex')
|
return new Id(new Buffer(str, 'hex'))
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromBytes = function (buf) {
|
exports.createFromBytes = function (buf) {
|
||||||
@ -68,11 +136,41 @@ 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) {
|
||||||
var mhId = multihashing(pubKey, 'sha2-256')
|
const buf = new Buffer(pubKey, 'base64')
|
||||||
|
const mhId = multihashing(buf, 'sha2-256')
|
||||||
return new Id(mhId, null, pubKey)
|
return new Id(mhId, null, pubKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromPrivKey = function () {
|
// Private key input will be a string
|
||||||
// TODO(daviddias) derive PubKey from priv
|
exports.createFromPrivKey = function (privKey) {
|
||||||
|
// create a buffer from the base64 encoded string
|
||||||
|
const buf = new Buffer(privKey, 'base64')
|
||||||
|
|
||||||
|
// get the private key data from the protobuf
|
||||||
|
const mpk = unmarshal(buf)
|
||||||
|
|
||||||
|
// create a forge buffer
|
||||||
|
const fbuf = forge.util.createBuffer(mpk.Data.toString('binary'))
|
||||||
|
|
||||||
|
// create an asn1 object from the private key bytes saved in the protobuf Data: field
|
||||||
|
const asnPriv = forge.asn1.fromDer(fbuf)
|
||||||
|
|
||||||
|
// get the RSA privatekey data from the asn1 object
|
||||||
|
const privateKey = forge.pki.privateKeyFromAsn1(asnPriv)
|
||||||
|
|
||||||
|
// set the RSA public key to the modulus and exponent of the private key
|
||||||
|
const publicKey = forge.pki.rsa.setPublicKey(privateKey.n, privateKey.e)
|
||||||
|
|
||||||
|
// return the RSA public key to asn1 object
|
||||||
|
const asnPub = forge.pki.publicKeyToAsn1(publicKey)
|
||||||
|
|
||||||
|
// format the public key
|
||||||
|
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')
|
||||||
|
return new Id(mhId, privKey, bufProtoPub64)
|
||||||
}
|
}
|
||||||
|
13
src/pb/crypto.proto
Normal file
13
src/pb/crypto.proto
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
enum KeyType {
|
||||||
|
RSA = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
message PublicKey {
|
||||||
|
required KeyType Type = 1;
|
||||||
|
required bytes Data = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message PrivateKey {
|
||||||
|
required KeyType Type = 1;
|
||||||
|
required bytes Data = 2;
|
||||||
|
}
|
73
tests/test.js
Normal file
73
tests/test.js
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
/* globals describe, it */
|
||||||
|
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const expect = require('chai').expect
|
||||||
|
const PeerId = require('../src')
|
||||||
|
|
||||||
|
const testId = {
|
||||||
|
id: '1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d',
|
||||||
|
privKey: 'CAASpgkwggSiAgEAAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAECggEAZtju/bcKvKFPz0mkHiaJcpycy9STKphorpCT83srBVQi59CdFU6Mj+aL/xt0kCPMVigJw8P3/YCEJ9J+rS8BsoWE+xWUEsJvtXoT7vzPHaAtM3ci1HZd302Mz1+GgS8Epdx+7F5p80XAFLDUnELzOzKftvWGZmWfSeDnslwVONkL/1VAzwKy7Ce6hk4SxRE7l2NE2OklSHOzCGU1f78ZzVYKSnS5Ag9YrGjOAmTOXDbKNKN/qIorAQ1bovzGoCwx3iGIatQKFOxyVCyO1PsJYT7JO+kZbhBWRRE+L7l+ppPER9bdLFxs1t5CrKc078h+wuUr05S1P1JjXk68pk3+kQKBgQDeK8AR11373Mzib6uzpjGzgNRMzdYNuExWjxyxAzz53NAR7zrPHvXvfIqjDScLJ4NcRO2TddhXAfZoOPVH5k4PJHKLBPKuXZpWlookCAyENY7+Pd55S8r+a+MusrMagYNljb5WbVTgN8cgdpim9lbbIFlpN6SZaVjLQL3J8TWH6wKBgQDSChzItkqWX11CNstJ9zJyUE20I7LrpyBJNgG1gtvz3ZMUQCn3PxxHtQzN9n1P0mSSYs+jBKPuoSyYLt1wwe10/lpgL4rkKWU3/m1Myt0tveJ9WcqHh6tzcAbb/fXpUFT/o4SWDimWkPkuCb+8j//2yiXk0a/T2f36zKMuZvujqQKBgC6B7BAQDG2H2B/ijofp12ejJU36nL98gAZyqOfpLJ+FeMz4TlBDQ+phIMhnHXA5UkdDapQ+zA3SrFk+6yGk9Vw4Hf46B+82SvOrSbmnMa+PYqKYIvUzR4gg34rL/7AhwnbEyD5hXq4dHwMNsIDq+l2elPjwm/U9V0gdAl2+r50HAoGALtsKqMvhv8HucAMBPrLikhXP/8um8mMKFMrzfqZ+otxfHzlhI0L08Bo3jQrb0Z7ByNY6M8epOmbCKADsbWcVre/AAY0ZkuSZK/CaOXNX/AhMKmKJh8qAOPRY02LIJRBCpfS4czEdnfUhYV/TYiFNnKRj57PPYZdTzUsxa/yVTmECgYBr7slQEjb5Onn5mZnGDh+72BxLNdgwBkhO0OCdpdISqk0F0Pxby22DFOKXZEpiyI9XYP1C8wPiJsShGm2yEwBPWXnrrZNWczaVuCbXHrZkWQogBDG3HGXNdU4MAWCyiYlyinIBpPpoAJZSzpGLmWbMWh28+RJS6AQX6KHrK1o2uw==',
|
||||||
|
pubKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE='
|
||||||
|
}
|
||||||
|
|
||||||
|
const testIdHex = '1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d'
|
||||||
|
|
||||||
|
const testIdBytes = new Buffer('1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d', 'hex')
|
||||||
|
|
||||||
|
const testIdB58String = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A'
|
||||||
|
|
||||||
|
describe('id', function (done) {
|
||||||
|
this.timeout(30000)
|
||||||
|
|
||||||
|
it('create a new id', (done) => {
|
||||||
|
const id = PeerId.create()
|
||||||
|
expect(id.toB58String().length).to.equal(46)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('recreate an Id from Hex string', (done) => {
|
||||||
|
const id = PeerId.createFromHexString(testIdHex)
|
||||||
|
expect(testIdBytes).to.deep.equal(id.id)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Recreate an Id from a Buffer', (done) => {
|
||||||
|
const id = PeerId.createFromBytes(testIdBytes)
|
||||||
|
expect(testId.id).to.equal(id.toHexString())
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Recreate a B58 String', (done) => {
|
||||||
|
const id = PeerId.createFromB58String(testIdB58String)
|
||||||
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Recreate from a Public Key', (done) => {
|
||||||
|
const id = PeerId.createFromPubKey(testId.pubKey)
|
||||||
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Recreate from a Private Key', (done) => {
|
||||||
|
const id = PeerId.createFromPrivKey(testId.privKey)
|
||||||
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
|
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()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Non-default # of bits', (done) => {
|
||||||
|
const shortId = PeerId.create({ bits: 128 })
|
||||||
|
const longId = PeerId.create({ bits: 256 })
|
||||||
|
expect(shortId.privKey.length).is.below(longId.privKey.length)
|
||||||
|
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