Compare commits

...

37 Commits

Author SHA1 Message Date
7635cfcfda chore: release version v0.6.7 2016-05-23 15:32:57 +01:00
afbd96edda chore: update contributors 2016-05-23 15:32:57 +01:00
c1e18ee420 Merge pull request #24 from diasdavid/feat/marshal-unmarshal
Feat/marshal unmarshal
2016-05-23 15:31:38 +01:00
b358530dfb add toJSON and fromJSOn and cli to quickly generate 2016-05-23 15:25:30 +01:00
4afbedb525 update dependencies 2016-05-23 14:16:30 +01:00
e9b107fc55 Merge pull request #21 from diasdavid/aegir
refactor: use aegir
2016-04-15 14:13:57 +01:00
855a97956d refactor: use aegir 2016-04-15 13:20:05 +02:00
dee54d3dcc Merge pull request #20 from noffle/circle
circle-ci config
2016-04-14 00:01:25 +01:00
167b726dad Merge pull request #19 from noffle/readme-formatting
fixes README formatting
2016-04-14 00:01:05 +01:00
3b9f8af2e7 circle-ci config 2016-04-13 10:11:39 -07:00
771b994d7e fixes README formatting 2016-04-13 10:00:26 -07:00
524fc37877 chore: release version v0.6.6 2016-04-12 22:14:58 +01:00
3c55473a45 Merge pull request #18 from diasdavid/fix-jsnext
fix jsnext:main
2016-04-12 22:13:03 +01:00
8771627ce4 fix it 2016-04-12 22:06:12 +01:00
bbf6bae073 chore: release version v0.6.5 2016-04-12 21:28:27 +01:00
e45a9f4083 Merge pull request #17 from diasdavid/jsnext-add
jsnext add
2016-04-12 21:24:11 +01:00
13f98c8913 jsnext add 2016-04-12 21:09:41 +01:00
f374ab4411 chore: release version v0.6.4 2016-04-12 17:31:47 +01:00
2f4cdd3725 Merge pull request #16 from noffle/docs-and-examples
readme <3
2016-04-12 17:28:37 +01:00
d2f90ff447 Merge pull request #15 from noffle/coverage
100% coverage
2016-04-12 17:27:29 +01:00
f2a75f1188 100% coverage badge 2016-04-12 09:07:59 -07:00
dab90bd3b6 other bundlers 2016-04-12 09:04:02 -07:00
e9fdc0a27e applies readme love 2016-04-11 15:23:34 -07:00
f0a17a9bd9 100% coverage 2016-04-11 14:43:50 -07:00
57ab8406c0 chore: release version v0.6.3 2016-04-07 19:47:53 -04:00
f16c08e7fd update the readme 2016-04-07 19:45:21 -04:00
be5d4f9b41 chore: release version v0.6.2 2016-04-07 19:41:09 -04:00
35a3ccca3d update coverage script 2016-04-07 19:38:43 -04:00
989c3fb7a5 add .npmignore 2016-04-07 19:30:19 -04:00
bbe67df1c8 Merge pull request #12 from dignifiedquire/dignified
Use dignified.js
2016-04-07 19:27:13 -04:00
8849d90d6a Use dignified.js 1.0.0 2016-04-07 21:22:53 +02:00
2552871bcc Update travis 2016-03-22 17:03:51 +01:00
ab76788771 Use dignified.js 2016-03-22 17:01:46 +01:00
f62023f8a7 Release v0.6.1. 2016-03-15 23:28:07 +00:00
95367871ac Merge pull request #11 from noffle/bits
exposes opts + bits
2016-03-15 23:22:41 +00:00
352a6cfa17 tests # of bits 2016-03-15 12:15:44 -07:00
727355c4f5 exposes opts + bits 2016-03-14 17:10:02 -07:00
14 changed files with 302 additions and 75938 deletions

3
.gitignore vendored
View File

@ -25,3 +25,6 @@ build/Release
# Dependency directory # Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules node_modules
lib
dist

28
.npmignore Normal file
View File

@ -0,0 +1,28 @@
# Logs
logs
*.log
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history
test

View File

@ -1,14 +1,17 @@
sudo: false
language: node_js language: node_js
node_js: node_js:
- "4.0" - 4
- 5
branches:
only:
- master
# 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'
@ -17,5 +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 test - npm run coverage-publish

177
README.md
View File

@ -1,97 +1,158 @@
peer-id JavaScript implementation # peer-id
=================================
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Build Status](https://travis-ci.org/diasdavid/js-peer-id.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-peer-id) [![Build Status](https://travis-ci.org/diasdavid/js-peer-id.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-peer-id)
![](https://img.shields.io/badge/coverage-95%25-yellow.svg?style=flat-square) [![Coverage Status](https://coveralls.io/repos/github/diasdavid/js-peer-id/badge.svg?branch=master)](https://coveralls.io/github/diasdavid/js-peer-id?branch=master)
[![Dependency Status](https://david-dm.org/diasdavid/js-peer-id.svg?style=flat-square)](https://david-dm.org/diasdavid/js-peer-id) [![Dependency Status](https://david-dm.org/diasdavid/js-peer-id.svg?style=flat-square)](https://david-dm.org/diasdavid/js-peer-id)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
> IPFS Peer Id implementation in JavaScript
> [IPFS](https://github.com/ipfs/ipfs) Peer ID implementation in JavaScript.
# Description # Description
An IPFS Peer Id is based on a sha256 hash of the peer public key, using [multihash](https://github.com/jbenet/multihash) Generate, import, and export PeerIDs, for use with [IPFS](https://github.com/ipfs/ipfs).
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. *A Peer ID is the SHA-256 [multihash](https://github.com/jbenet/multihash) of a
public key.*
*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 # Example
### In Node.js through npm ```js
var PeerId = require('peer-id')
var bs58 = require('bs58')
```bash var id = PeerId.create({ bits: 32 })
> npm install --save peer-id
console.log('id ', id.toB58String())
console.log('priv key ', bs58.encode(id.privKey))
console.log('pub key ', bs58.encode(id.pubKey))
``` ```
```javascript ```
id QmeeLFb92nkZJGj3gXLqXrEMzCMYs6uBgQLVNbrcXEvYXk
priv key 6ibrcPAbevzvPpkq6EA6XmLyuhmUrJrEvUfgQDtEiSEPzGnGU8Ejwf6b11DVm6opnFGo
pub key 2BeBZVKJ9RQs4i4LbGv4ReEeuBA5dck2Gje3wt67e44XuyyPq5jE
```
# Installation
## npm
```sh
> npm i peer-id
```
# Setup
## Node.js
```JavaScript
var PeerId = require('peer-id')
```
## Browser: Browserify, Webpack, other bundlers
The code published to npm that gets loaded on require is in fact a ES5
transpiled version with the right shims added. This means that you can require
it and use with your favourite bundler without having to adjust asset management
process.
```JavaScript
var PeerId = require('peer-id')
```
## Browser: `<script>` Tag
Loading this module through a script tag will make the `PeerId` obj available in
the global namespace.
```html
<script src="https://npmcdn.com/peer-id/dist/index.min.js"></script>
<!-- OR -->
<script src="https://npmcdn.com/peer-id/dist/index.js"></script>
```
# API
```js
const PeerId = require('peer-id') const PeerId = require('peer-id')
``` ```
### In the Browser through Webpack ## Create
Follow our [webpack config](/webpack.config.js) example. ### PeerId.create()
### In the Browser through browserify Generates a new Peer ID, complete with public/private keypair. A Peer ID has the
following properties:
> **WIP** Doesn't work out yet - `pubKey` - Buffer containing the public key bytes
- `privKey` - Buffer containing the private key bytes
- `id` - Buffer containing the multihash bytes
### In the Browser through `<script>` tag ## Import
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: ### PeerId.createFromHexString(str)
```JavaScript Creates a Peer ID from hex string representing the key's multihash.
const Id = PeerId
### PeerId.createFromBytes(buf)
Creates a Peer ID from a buffer representing the key's multihash.
### PeerId.createFromB58String(str)
Creates a Peer ID from a Base58 string representing the key's multihash.
### PeerId.createFromPubKey(pubKey)
Creates a Peer ID from a buffer containing a public key.
### PeerId.createFromPrivKey(privKey)
Creates a Peer ID from a buffer containing a private key.
## Export
### id.toPrint()
Returns an object with the ID's properties in hex format:
```js
{
id: 'QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi',
privKey: '080012a609308204a20201000282010100a608889914da08959d3a3db0734cee812c96...',
pubKey: '080012a60230820122300d06092a864886f70d01010105000382010f003082010a0282010...'
}
``` ```
#### Gotchas ### id.toHexString()
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). Returns the Peer ID's `id` as a hex string.
### Creating a new Id
``` ```
const PeerId = require('ipfs-peer') 1220d6243998f2fc56343ad7ed0342ab7886a4eb18d736f1b67d44b37fcc81e0f39f
// Create a new Id
const id = PeerId.create()
// Recreate an Id from Hex string
const id = PeerId.createFromHexString(str)
// Recreate an Id from a Buffer
const id = PeerId.createFromBytes(buf)
// Recreate an B58 String
const id = PeerId.createFromB58String(str)
// Recreate from a Public Key
const id = PeerId.createFromPubKey(pubKey)
// Recreate from a Private Key
const id = PeerId.createFromPrivKey(privKey)
``` ```
### Exporting an Id ### id.toBytes()
Returns the Peer ID's `id` as a buffer.
``` ```
// Print friendly format <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>
id.toPrint() // returns an object with id, privKey and pubKey in hex format
// Export to an hex string
id.toHexString()
// Export to Buffer
id.toBytes() (same as id.id)
// Export to a B58 string
id.toB58String()
``` ```
### Id format ### id.toB58String()
Returns the Peer ID's `id` as a base58 string.
``` ```
id.pubKey // Buffer containing the Public Key QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi
id.privKey // Buffer containing the Private Key
id.id // Buffer containing the multihash
``` ```
# License
MIT

12
circle.yml Normal file
View 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

75722
dist/peer-id.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,61 +0,0 @@
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)
})
}

View File

@ -1,21 +1,19 @@
{ {
"name": "peer-id", "name": "peer-id",
"version": "0.6.0", "version": "0.6.7",
"description": "IPFS Peer Id implementation in Node.js", "description": "IPFS Peer Id implementation in Node.js",
"main": "src/index.js", "main": "lib/index.js",
"bin": "src/bin.js",
"jsnext:main": "src/index.js",
"scripts": { "scripts": {
"lint": "standard", "lint": "aegir-lint",
"test": "npm run test:node && npm run test:browser", "build": "aegir-build",
"test:node": "mocha tests/test.js", "test": "aegir-test",
"test:browser": "karma start karma.conf.js", "test:node": "aegir-test node",
"coverage": "istanbul cover --print both -- _mocha tests/test.js", "test:browser": "aegir-test browser",
"dist": "webpack" "release": "aegir-release",
}, "coverage": "aegir-coverage",
"standard": { "coverage-publish": "aegir-coverage publish"
"ignore": [
"dist",
"deps"
]
}, },
"keywords": [ "keywords": [
"IPFS" "IPFS"
@ -34,22 +32,10 @@
}, },
"homepage": "https://github.com/diasdavid/js-peer-id", "homepage": "https://github.com/diasdavid/js-peer-id",
"devDependencies": { "devDependencies": {
"browserify": "^13.0.0", "aegir": "^3.0.4",
"buffer-loader": "0.0.1", "buffer-loader": "0.0.1",
"chai": "^3.5.0", "chai": "^3.5.0",
"istanbul": "^0.4.2", "pre-commit": "^1.1.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": "^3.0.0", "bs58": "^3.0.0",
@ -60,5 +46,21 @@
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/diasdavid/js-peer-id.git" "url": "https://github.com/diasdavid/js-peer-id.git"
},
"aegir": {
"webpack": {
"resolve": {
"alias": {
"node-forge": "../vendor/forge.bundle.js"
} }
} }
}
},
"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
View File

@ -0,0 +1,7 @@
#!/usr/local/bin/node
'use strict'
const PeerId = require('./index.js')
console.log(JSON.stringify(PeerId.create().toJSON(), null, ' '))

View File

@ -2,6 +2,8 @@
* 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'
const fs = require('fs') const fs = require('fs')
const multihashing = require('multihashing') const multihashing = require('multihashing')
const base58 = require('bs58') const base58 = require('bs58')
@ -9,19 +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')
const isNode = !global.window const pbCrypto = protobuf(fs.readFileSync(path.resolve(__dirname, '../protos/crypto.proto')))
// protobuf read from file exports = module.exports = PeerId
const messages = isNode ? protobuf(fs.readFileSync(path.resolve(__dirname, 'pb/crypto.proto'))) : protobuf(require('buffer!./pb/crypto.proto'))
exports = module.exports = Id
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')
} }
@ -38,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')
@ -53,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
}) })
} }
@ -89,17 +98,23 @@ 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
} }
// generation // generation
exports.create = function () { exports.create = function (opts) {
opts = opts || {}
opts.bits = opts.bits || 2048
// generate keys // generate keys
const pair = forge.rsa.generateKeyPair({ bits: 2048, e: 0x10001 }) const pair = forge.rsa.generateKeyPair({
bits: opts.bits,
e: 0x10001
})
// return the RSA public/private key to asn1 object // return the RSA public/private key to asn1 object
const asnPub = forge.pki.publicKeyToAsn1(pair.publicKey) const asnPub = forge.pki.publicKeyToAsn1(pair.publicKey)
@ -115,26 +130,26 @@ exports.create = function () {
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
@ -143,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'))
@ -166,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'))
} }

View File

@ -1,5 +1,4 @@
/* globals describe, it */ /* eslint-env mocha */
'use strict' 'use strict'
const expect = require('chai').expect const expect = require('chai').expect
@ -20,6 +19,11 @@ const testIdB58String = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A'
describe('id', function (done) { describe('id', function (done) {
this.timeout(30000) this.timeout(30000)
it('create an id without \'new\'', (done) => {
expect(PeerId).to.throw(Error)
done()
})
it('create a new id', (done) => { it('create a new id', (done) => {
const id = PeerId.create() const id = PeerId.create()
expect(id.toB58String().length).to.equal(46) expect(id.toB58String().length).to.equal(46)
@ -62,5 +66,39 @@ describe('id', function (done) {
expect(id2.id).to.deep.equal(id1.id) expect(id2.id).to.deep.equal(id1.id)
done() 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()
}) })
it('Pretty printing', (done) => {
const id = PeerId.createFromPrivKey(testId.privKey)
var out = id.toPrint()
var expected = {
id: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A',
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: '080012a60230820122300d06092a864886f70d01010105000382010f003082010a0282010100b648aa3f1cc1597819a5d401775e28f3af1adf417749ce378f05901b771a8a47531cea3b911d78a3e875d83e3940934d41845d52dcb9782f08b47001e18207f8e7bb0c839e545b278629e52fd2e720bc2a41c25479710d36d22d0c8338cf58e2d6ab5aedbd26cd7008b6644567ebe43611c1e8df052f591b4b78acfe0d94997f0d8f1030be0c63c93e5edff20ef3979e98ca69a6cc7f658992cdaf383faa2768914bf9bb5a5d1ab7292ee3cd79338393472a281f8e51bb8a8fd1928581020848dac9b24397ddbbea86a52fd82106d49e12fdb492e81ab53bd8cb9f74c05949924bf297e9cfc481f410460c28af5745696ef57627a127dba22c1cbfc3374a5b230203010001'
}
expect(out.id).to.equal(expected.id)
expect(out.privKey).to.equal(expected.privKey)
expect(out.pubKey).to.equal(expected.pubKey)
done()
})
it('toBytes', (done) => {
const id = PeerId.createFromHexString(testIdHex)
expect(id.toBytes().toString('hex')).to.equal(testIdBytes.toString('hex'))
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()
})
})

View File

@ -1,29 +0,0 @@
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: []
}
}