Compare commits

...

37 Commits

Author SHA1 Message Date
9922f85693 chore: release version v0.12.5 2019-09-25 16:12:25 +02:00
843d35e1da chore: update contributors 2019-09-25 16:12:25 +02:00
ceeff130fa feat: inline public key handling for the 0.12.x line (#102) 2019-09-25 16:03:01 +02:00
8f4027b136 chore: release version v0.12.4 2019-07-23 14:38:20 +02:00
90835484fe chore: update contributors 2019-07-23 14:38:20 +02:00
8bcd9a880e chore: revert async dep change (#97)
* chore: revert async dep change

* chore: update async to 2.6.3
2019-07-23 13:32:46 +01:00
0bce8f7a87 chore: release version v0.12.3 2019-07-11 18:17:27 +01:00
8f15adbce5 chore: update contributors 2019-07-11 18:17:27 +01:00
fd072213b7 feat(peerid): support creating from secp256k1; harmonize algo with Go 2019-07-11 18:11:05 +01:00
bbabd7451e chore: add discourse badge (#93) 2019-04-18 19:32:14 +02:00
1624b0f70a chore: release version v0.12.2 2019-01-09 15:14:15 +00:00
ce8a6ff77e chore: update contributors 2019-01-09 15:14:15 +00:00
41ce1d4671 chore: fix lint 2019-01-09 15:03:49 +00:00
231e553a22 chore: fix lint 2019-01-09 15:03:49 +00:00
cd2099305e fix: clean repo and bundle size reduction 2019-01-09 15:03:49 +00:00
8e030b24d4 chore: release version v0.12.1 2019-01-03 17:10:26 +00:00
9f69e1ba74 chore: update contributors 2019-01-03 17:10:26 +00:00
cbc8c5e2ba Merge pull request #91 from libp2p/chore/update-deps-20190103
chore: update dependencies
2019-01-03 17:05:15 +00:00
77000dbd8b chore: update dependencies 2019-01-03 16:46:46 +00:00
eb062759c6 chore: release version v0.12.0 2018-10-18 11:00:01 +01:00
39e17c0c0b chore: update contributors 2018-10-18 11:00:01 +01:00
e03a4098df chore: update contributors 2018-10-18 10:57:19 +01:00
d7e633a90b Merge pull request #84 from libp2p/feat/add-class-is-module
feat: add class-is module
2018-10-18 10:50:45 +01:00
2e5e666140 fix: add peerIdWithIs to the API functions using the instance 2018-10-17 10:51:42 +01:00
b388257b69 Merge pull request #86 from libp2p/docs/readme-tweaks
docs: various readme tweaks
2018-10-12 19:58:47 +01:00
6ecd9a8cb0 docs: various readme tweaks 2018-10-12 18:01:55 +02:00
6513a02e1e feat: add class-is module 2018-10-04 11:29:31 +01:00
bd1dc9bda0 chore: release version v0.11.0 2018-07-02 19:28:04 +02:00
31cbb5d94c chore: update contributors 2018-07-02 19:28:03 +02:00
35556c3a37 chore: update deps 2018-07-02 19:26:36 +02:00
e8ab1b9281 feat: change toPrint output to match go implementation 2018-07-02 19:26:03 +02:00
a786018528 chore: remove pre-commit module 2018-06-04 09:48:30 +01:00
417fd330b9 docs: add lead maintainer 2018-06-04 09:48:30 +01:00
d74bdebcc8 chore: update deps 2018-06-04 09:48:30 +01:00
ffed046595 chore: release version v0.10.7 2018-04-05 16:41:39 +01:00
1727a135aa chore: update contributors 2018-04-05 16:41:39 +01:00
466b3a030d chore: update deps 2018-04-05 16:39:53 +01:00
11 changed files with 308 additions and 199 deletions

View File

@ -1,28 +0,0 @@
# 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,32 +1,45 @@
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
sudo: false
language: node_js
cache: npm
stages:
- check
- test
- cov
matrix:
node_js:
- '10'
os:
- linux
- osx
script: npx nyc -s npm run test:node -- --bail
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov
jobs:
include:
- node_js: 6
env: CXX=g++-4.8
- node_js: 8
env: CXX=g++-4.8
# - node_js: stable
# env: CXX=g++-4.8
- os: windows
cache: false
- stage: check
script:
- npx aegir dep-check
- npm run lint
- npm run test
- npm run coverage
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
after_success:
- npm run coverage-publish
- stage: test
name: chrome
addons:
firefox: 'latest'
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
chrome: stable
script:
- npx aegir test -t browser
- npx aegir test -t webworker
- stage: test
name: firefox
addons:
firefox: latest
script:
- npx aegir test -t browser -- --browsers FirefoxHeadless
- npx aegir test -t webworker -- --browsers FirefoxHeadless
notifications:
email: false

View File

@ -1,3 +1,73 @@
<a name="0.12.5"></a>
## [0.12.5](https://github.com/libp2p/js-peer-id/compare/v0.12.4...v0.12.5) (2019-09-25)
### Features
* inline public key handling for the 0.12.x line ([#102](https://github.com/libp2p/js-peer-id/issues/102)) ([ceeff13](https://github.com/libp2p/js-peer-id/commit/ceeff13))
<a name="0.12.4"></a>
## [0.12.4](https://github.com/libp2p/js-peer-id/compare/v0.12.3...v0.12.4) (2019-07-23)
<a name="0.12.3"></a>
## [0.12.3](https://github.com/libp2p/js-peer-id/compare/v0.12.2...v0.12.3) (2019-07-11)
### Features
* **peerid:** support creating from secp256k1; harmonize algo with Go ([fd07221](https://github.com/libp2p/js-peer-id/commit/fd07221))
<a name="0.12.2"></a>
## [0.12.2](https://github.com/libp2p/js-peer-id/compare/v0.12.1...v0.12.2) (2019-01-09)
### Bug Fixes
* clean repo and bundle size reduction ([cd20993](https://github.com/libp2p/js-peer-id/commit/cd20993))
<a name="0.12.1"></a>
## [0.12.1](https://github.com/libp2p/js-peer-id/compare/v0.12.0...v0.12.1) (2019-01-03)
<a name="0.12.0"></a>
# [0.12.0](https://github.com/libp2p/js-peer-id/compare/v0.11.0...v0.12.0) (2018-10-18)
### Bug Fixes
* add peerIdWithIs to the API functions using the instance ([2e5e666](https://github.com/libp2p/js-peer-id/commit/2e5e666))
### Features
* add class-is module ([6513a02](https://github.com/libp2p/js-peer-id/commit/6513a02))
<a name="0.11.0"></a>
# [0.11.0](https://github.com/libp2p/js-peer-id/compare/v0.10.7...v0.11.0) (2018-07-02)
### Features
* change toPrint output to match go implementation ([e8ab1b9](https://github.com/libp2p/js-peer-id/commit/e8ab1b9))
<a name="0.10.7"></a>
## [0.10.7](https://github.com/libp2p/js-peer-id/compare/v0.10.6...v0.10.7) (2018-04-05)
<a name="0.10.6"></a>
## [0.10.6](https://github.com/libp2p/js-peer-id/compare/v0.10.5...v0.10.6) (2018-02-12)

View File

@ -1,16 +1,22 @@
# peer-id
[![](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)
[![Build Status](https://travis-ci.org/libp2p/js-peer-id.svg?style=flat-square)](https://travis-ci.org/libp2p/js-peer-id)
[![Coverage Status](https://coveralls.io/repos/github/libp2p/js-peer-id/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-peer-id?branch=master)
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://protocol.ai)
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
[![](https://img.shields.io/codecov/c/github/libp2p/js-peer-id.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-peer-id)
[![](https://img.shields.io/travis/libp2p/js-peer-id.svg?style=flat-square)](https://travis-ci.com/libp2p/js-peer-id)
[![Dependency Status](https://david-dm.org/libp2p/js-peer-id.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-id)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/Node.js-%3E%3D6.0.0-orange.svg?style=flat-square)
> [IPFS](https://github.com/ipfs/ipfs) Peer ID implementation in JavaScript.
## Lead Maintainer
[Pedro Teixeira](https://github.com/pgte)
## Table of Contents
- [Description](#description)
- [Example](#example)
- [Installation](#installation)
@ -51,7 +57,7 @@ The public key is a base64 encoded string of a protobuf containing an RSA DER bu
```JavaScript
const PeerId = require('peer-id')
PeerId.create({ bits: 1024 }, (err, id) => {
PeerId.create({ bits: 1024, keyType: 'rsa' }, (err, id) => {
if (err) { throw err }
console.log(JSON.stringify(id.toJSON(), null, 2))
})
@ -122,7 +128,7 @@ The key format is detailed in [libp2p-crypto](https://github.com/libp2p/js-libp2
Generates a new Peer ID, complete with public/private keypair.
- `opts: Object`: Default: `{bits: 2048}`
- `opts: Object`: Default: `{bits: 2048, keyType: 'rsa'}`
- `callback: Function`
Calls back `callback` with `err, id`.
@ -138,6 +144,7 @@ Creates a Peer ID from hex string representing the key's multihash.
Creates a Peer ID from a buffer representing the key's multihash.
### `createFromB58String(str)`
Creates a Peer ID from a Base58 string representing the key's multihash.
### `createFromPubKey(pubKey)`
@ -155,8 +162,8 @@ Creates a Peer ID from a buffer containing a private key.
### `createFromJSON(obj)`
- `obj.id: String` - The multihash encoded in `base58`
- `obj.pubKey: String` - The public key in protobuf format, encoded in 'base64'
- `obj.privKey: String` - The private key in protobuf format, encoded in 'base 64'
- `obj.pubKey: String` - The public key in protobuf format, encoded in `base64`
- `obj.privKey: String` - The private key in protobuf format, encoded in `base64`
## Export
@ -192,10 +199,11 @@ Returns an `obj` of the form
- `obj.pubKey: String` - The public key in protobuf format, encoded in 'base64'
- `obj.privKey: String` - The private key in protobuf format, encoded in 'base 64'
### `toPrint()`
Alias for `.toJSON()`.
Returns the Peer ID as a printable string without the `Qm` prefix.
Example: `<peer.ID xxxxxx>`
### `isEqual(id)`

View File

@ -1,29 +0,0 @@
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
version: "{build}"
environment:
matrix:
- nodejs_version: "6"
- nodejs_version: "8"
matrix:
fast_finish: true
install:
# Install Node.js
- ps: Install-Product node $env:nodejs_version
# Upgrade npm
- npm install -g npm
# Output our current versions for debugging
- node --version
- npm --version
# Install our package dependencies
- npm install
test_script:
- npm run test:node
build: off

2
ci/Jenkinsfile vendored
View File

@ -1,2 +0,0 @@
// Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
javascript()

View File

@ -1,15 +0,0 @@
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
machine:
node:
version: stable
dependencies:
pre:
- google-chrome --version
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
- sudo dpkg -i google-chrome.deb || true
- sudo apt-get update
- sudo apt-get install -f
- sudo apt-get install --only-upgrade lsb-base
- sudo dpkg -i google-chrome.deb
- google-chrome --version

View File

@ -1,7 +1,8 @@
{
"name": "peer-id",
"version": "0.10.6",
"version": "0.12.5",
"description": "IPFS Peer Id implementation in Node.js",
"leadMaintainer": "Pedro Teixeira <i@pgte.me>",
"main": "src/index.js",
"bin": "src/bin.js",
"scripts": {
@ -13,50 +14,58 @@
"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
"coverage": "aegir coverage"
"coverage": "aegir coverage",
"size": "bundlesize -f dist/index.min.js -s 140kB"
},
"files": [
"src",
"dist"
],
"keywords": [
"IPFS"
],
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"pre-commit": [
"lint",
"test"
],
"engines": {
"node": ">=6.0.0",
"npm": ">=3.0.0"
"node": ">=10.0.0",
"npm": ">=6.0.0"
},
"bugs": {
"url": "https://github.com/libp2p/js-peer-id/issues"
},
"homepage": "https://github.com/libp2p/js-peer-id",
"devDependencies": {
"aegir": "^12.4.0",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"pre-commit": "^1.2.2"
"aegir": "^20.0.0",
"bundlesize": "~0.17.1",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1"
},
"dependencies": {
"async": "^2.6.0",
"libp2p-crypto": "~0.12.1",
"lodash": "^4.17.5",
"multihashes": "~0.4.13"
"libp2p-crypto": "~0.16.1",
"async": "^2.6.3",
"class-is": "^1.1.0",
"multihashes": "~0.4.15"
},
"repository": {
"type": "git",
"url": "https://github.com/libp2p/js-peer-id.git"
},
"contributors": [
"Arve Knudsen <arve.knudsen@gmail.com>",
"David Dias <daviddias.p@gmail.com>",
"David Dias <mail@daviddias.me>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Hugo Dias <hugomrdias@gmail.com>",
"Jacob Heun <jacobheun@gmail.com>",
"Maciej Krüger <mkg20001@gmail.com>",
"Michael Garvin <gar+gh@danger.computer>",
"Pedro Teixeira <i@pgte.me>",
"Prashanth Chandra <coolshanth94@gmail.com>",
"Richard Littauer <richard.littauer@gmail.com>",
"Richard Schneider <makaretu@gmail.com>",
"Stephen Whitmore <stephen.whitmore@gmail.com>",
"Topper Bowers <topper@toppingdesign.com>",
"Vasco Santos <vasco.santos@ua.pt>",
"Vasco Santos <vasco.santos@moxy.studio>",
"Yahya <ya7yaz@gmail.com>",
"greenkeeperio-bot <support@greenkeeper.io>",
"nginnever <ginneversource@gmail.com>",

View File

@ -9,5 +9,6 @@ PeerId.create((err, id) => {
throw err
}
// eslint-disable-next-line
console.log(JSON.stringify(id.toJSON(), null, 2))
})

View File

@ -5,9 +5,10 @@
'use strict'
const mh = require('multihashes')
const crypto = require('libp2p-crypto')
const cryptoKeys = require('libp2p-crypto/src/keys')
const assert = require('assert')
const waterfall = require('async/waterfall')
const withIs = require('class-is')
class PeerId {
constructor (id, privKey, pubKey) {
@ -47,6 +48,13 @@ class PeerId {
if (this._privKey) {
return this._privKey.public
}
const decoded = mh.decode(this.id)
if (decoded.name === 'identity') {
this._pubKey = cryptoKeys.unmarshalPublicKey(decoded.digest)
return this._pubKey
}
}
set pubKey (pubKey) {
@ -56,20 +64,30 @@ class PeerId {
// Return the protobuf version of the public key, matching go ipfs formatting
marshalPubKey () {
if (this.pubKey) {
return crypto.keys.marshalPublicKey(this.pubKey)
return cryptoKeys.marshalPublicKey(this.pubKey)
}
}
// Return the protobuf version of the private key, matching go ipfs formatting
marshalPrivKey () {
if (this.privKey) {
return crypto.keys.marshalPrivateKey(this.privKey)
return cryptoKeys.marshalPrivateKey(this.privKey)
}
}
// pretty print
toPrint () {
return this.toJSON()
let pid = this.toB58String()
// All sha256 nodes start with Qm
// We can skip the Qm to make the peer.ID more useful
if (pid.startsWith('Qm')) {
pid = pid.slice(2)
}
let maxRunes = 6
if (pid.length < maxRunes) {
maxRunes = pid.length
}
return '<peer.ID ' + pid.substr(0, maxRunes) + '>'
}
// return the jsonified version of the key, matching the formatting
@ -122,7 +140,30 @@ class PeerId {
}
}
exports = module.exports = PeerId
const PeerIdWithIs = withIs(PeerId, { className: 'PeerId', symbolName: '@libp2p/js-peer-id/PeerId' })
exports = module.exports = PeerIdWithIs
const computeDigest = (pubKey, cb) => {
if (pubKey.bytes.length <= 42) {
const digest = mh.encode(pubKey.bytes, 'identity')
cb(null, digest)
} else {
pubKey.hash((err, digest) => {
cb(err, digest)
})
}
}
const computePeerId = (privKey, pubKey, cb) => {
computeDigest(pubKey, (err, digest) => {
if (err != null) {
cb(err)
} else {
cb(null, new PeerIdWithIs(digest, privKey, pubKey))
}
})
}
// generation
exports.create = function (opts, callback) {
@ -132,31 +173,27 @@ exports.create = function (opts, callback) {
}
opts = opts || {}
opts.bits = opts.bits || 2048
opts.keyType = opts.keyType || 'RSA'
waterfall([
(cb) => crypto.keys.generateKeyPair('RSA', opts.bits, cb),
(privKey, cb) => privKey.public.hash((err, digest) => {
cb(err, digest, privKey)
})
], (err, digest, privKey) => {
if (err) {
return callback(err)
cryptoKeys.generateKeyPair(opts.keyType, opts.bits, (err, privKey) => {
if (err != null) {
callback(err)
} else {
computePeerId(privKey, privKey.public, callback)
}
callback(null, new PeerId(digest, privKey))
})
}
exports.createFromHexString = function (str) {
return new PeerId(mh.fromHexString(str))
return new PeerIdWithIs(mh.fromHexString(str))
}
exports.createFromBytes = function (buf) {
return new PeerId(buf)
return new PeerIdWithIs(buf)
}
exports.createFromB58String = function (str) {
return new PeerId(mh.fromB58String(str))
return new PeerIdWithIs(mh.fromB58String(str))
}
// Public Key input will be a buffer
@ -175,18 +212,12 @@ exports.createFromPubKey = function (key, callback) {
if (!Buffer.isBuffer(buf)) throw new Error('Supplied key is neither a base64 string nor a buffer')
pubKey = crypto.keys.unmarshalPublicKey(buf)
pubKey = cryptoKeys.unmarshalPublicKey(buf)
} catch (err) {
return callback(err)
}
pubKey.hash((err, digest) => {
if (err) {
return callback(err)
}
callback(null, new PeerId(digest, null, pubKey))
})
computePeerId(null, pubKey, callback)
}
// Private key input will be a string
@ -207,17 +238,12 @@ exports.createFromPrivKey = function (key, callback) {
return callback(err)
}
waterfall([
(cb) => crypto.keys.unmarshalPrivateKey(buf, cb),
(privKey, cb) => privKey.public.hash((err, digest) => {
cb(err, digest, privKey)
})
], (err, digest, privKey) => {
if (err) {
return callback(err)
cryptoKeys.unmarshalPrivateKey(buf, (err, privKey) => {
if (err != null) {
callback(err)
} else {
computePeerId(privKey, privKey.public, callback)
}
callback(null, new PeerId(digest, privKey, privKey.public))
})
}
@ -235,20 +261,26 @@ exports.createFromJSON = function (obj, callback) {
id = mh.fromB58String(obj.id)
rawPrivKey = obj.privKey && Buffer.from(obj.privKey, 'base64')
rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64')
pub = rawPubKey && crypto.keys.unmarshalPublicKey(rawPubKey)
pub = rawPubKey && cryptoKeys.unmarshalPublicKey(rawPubKey)
} catch (err) {
return callback(err)
}
if (rawPrivKey) {
if (!rawPrivKey) {
callback(null, new PeerIdWithIs(id, null, pub))
return
}
waterfall([
(cb) => crypto.keys.unmarshalPrivateKey(rawPrivKey, cb),
(priv, cb) => priv.public.hash((err, digest) => {
(cb) => cryptoKeys.unmarshalPrivateKey(rawPrivKey, cb),
(priv, cb) => {
computeDigest(priv.public, (err, digest) => {
cb(err, digest, priv)
}),
})
},
(privDigest, priv, cb) => {
if (pub) {
pub.hash((err, pubDigest) => {
computeDigest(pub, (err, pubDigest) => {
cb(err, privDigest, priv, pubDigest)
})
} else {
@ -268,11 +300,8 @@ exports.createFromJSON = function (obj, callback) {
return callback(new Error('Id and private key do not match'))
}
callback(null, new PeerId(id, priv, pub))
callback(null, new PeerIdWithIs(id, priv, pub))
})
} else {
callback(null, new PeerId(id, null, pub))
}
}
exports.isPeerId = function (peerId) {

View File

@ -9,6 +9,7 @@ const expect = chai.expect
const crypto = require('libp2p-crypto')
const mh = require('multihashes')
const parallel = require('async/parallel')
const waterfall = require('async/waterfall')
const PeerId = require('../src')
@ -40,6 +41,25 @@ describe('PeerId', () => {
})
})
it('can be created for a Secp256k1 key', (done) => {
PeerId.create({ keyType: 'secp256k1', bits: 256 }, (err, id) => {
const expB58 = mh.toB58String(mh.encode(id.pubKey.bytes, 'identity'))
expect(err).to.not.exist()
expect(id.toB58String()).to.equal(expB58)
done()
})
})
it('can get the public key from a Secp256k1 key', (done) => {
PeerId.create({ keyType: 'secp256k1', bits: 256 }, (err, original) => {
expect(err).to.not.exist()
const newId = PeerId.createFromB58String(original.toB58String())
expect(original.pubKey.bytes).to.eql(newId.pubKey.bytes)
done()
})
})
it('isPeerId', (done) => {
PeerId.create(testOpts, (err, id) => {
expect(err).to.not.exist()
@ -85,6 +105,22 @@ describe('PeerId', () => {
})
})
it('can be created from a Secp256k1 public key', (done) => {
waterfall([
(cb) => {
crypto.keys.generateKeyPair('secp256k1', 256, cb)
},
(privKey, cb) => {
PeerId.createFromPubKey(privKey.public.bytes, cb)
}
], (err, id) => {
expect(err).to.not.exist()
const expB58 = mh.toB58String(mh.encode(id.pubKey.bytes, 'identity'))
expect(id.toB58String()).to.equal(expB58)
done()
})
})
it('Recreate from a Private Key', (done) => {
PeerId.createFromPrivKey(testId.privKey, (err, id) => {
expect(err).to.not.exist()
@ -100,6 +136,22 @@ describe('PeerId', () => {
})
})
it('can be created from a Secp256k1 private key', (done) => {
waterfall([
(cb) => {
crypto.keys.generateKeyPair('secp256k1', 256, cb)
},
(privKey, cb) => {
PeerId.createFromPrivKey(privKey.bytes, cb)
}
], (err, id) => {
expect(err).to.not.exist()
const expB58 = mh.toB58String(mh.encode(id.pubKey.bytes, 'identity'))
expect(id.toB58String()).to.equal(expB58)
done()
})
})
it('Compare generated ID with one created from PubKey', (done) => {
PeerId.create(testOpts, (err, id1) => {
expect(err).to.not.exist()
@ -136,9 +188,10 @@ describe('PeerId', () => {
it('Pretty printing', (done) => {
PeerId.create(testOpts, (err, id1) => {
expect(err).to.not.exist()
PeerId.createFromPrivKey(id1.toPrint().privKey, (err, id2) => {
PeerId.createFromPrivKey(id1.toJSON().privKey, (err, id2) => {
expect(err).to.not.exist()
expect(id1.toPrint()).to.be.eql(id2.toPrint())
expect(id1.toPrint()).to.equal('<peer.ID ' + id1.toB58String().substr(2, 6) + '>')
done()
})
})