Skip to content

Commit

Permalink
feat: async await
Browse files Browse the repository at this point in the history
BREAKING CHANGE: API refactored to use async/await

fix: remove async from non-async methods
fix: lint errors, use Boolean instead of !!
feat: re-add some timeouts to the tests
chore: rebase branch
chore: update libp2p-crypto dep

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
  • Loading branch information
hacdias authored and achingbrain committed Jul 11, 2019
1 parent bbabd74 commit 24923c9
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 337 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
- [API](#api)
- [Create](#create)
- [`new PeerId(id[, privKey, pubKey])`](#new-peeridid-privkey-pubkey)
- [`create([opts], callback)`](#createopts-callback)
- [`create([opts])`](#createopts)
- [Import](#import)
- [`createFromHexString(str)`](#createfromhexstringstr)
- [`createFromBytes(buf)`](#createfrombytesbuf)
Expand Down Expand Up @@ -57,11 +57,10 @@ 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) => {
if (err) { throw err }
console.log(JSON.stringify(id.toJSON(), null, 2))
})
const id = await PeerId.create({ bits: 1024 })
console.log(JSON.stringify(id.toJSON(), null, 2))
```

```bash
{
"id": "Qma9T5YraSnpRDZqRR4krcSJabThc8nwZuJV3LercPHufi",
Expand Down Expand Up @@ -124,47 +123,58 @@ const PeerId = require('peer-id')

The key format is detailed in [libp2p-crypto](https://github.com/libp2p/js-libp2p-crypto).

### `create([opts], callback)`
### `create([opts])`

Generates a new Peer ID, complete with public/private keypair.

- `opts: Object`: Default: `{bits: 2048}`
- `callback: Function`

Calls back `callback` with `err, id`.
Returns `Promise<PeerId>`.

## Import

### `createFromHexString(str)`

Creates a Peer ID from hex string representing the key's multihash.

Returns `PeerId.

### `createFromBytes(buf)`

Creates a Peer ID from a buffer representing the key's multihash.

Returns `PeerId`.

### `createFromB58String(str)`

Creates a Peer ID from a Base58 string representing the key's multihash.

Returns `PeerId`.

### `createFromPubKey(pubKey)`

- `publicKey: Buffer`

Creates a Peer ID from a buffer containing a public key.

Returns `Promise<PeerId>`.

### `createFromPrivKey(privKey)`

- `privKey: Buffer`

Creates a Peer ID from a buffer containing a private key.

Returns `Promise<PeerId>`.

### `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 `base64`

Returns `Promise<PeerId>`.

## Export

### `toHexString()`
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@
},
"homepage": "https://github.com/libp2p/js-peer-id",
"devDependencies": {
"aegir": "^18.2.2",
"bundlesize": "~0.17.1",
"aegir": "^19.0.5",
"bundlesize": "~0.18.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1"
},
"dependencies": {
"async": "^2.6.2",
"class-is": "^1.1.0",
"libp2p-crypto": "~0.16.1",
"libp2p-crypto": "~0.17.0",
"multihashes": "~0.4.14"
},
"repository": {
Expand Down
11 changes: 5 additions & 6 deletions src/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

const PeerId = require('./index.js')

PeerId.create((err, id) => {
if (err) {
throw err
}
async function main () {
const id = await PeerId.create()
console.log(JSON.stringify(id.toJSON(), null, 2)) // eslint-disable-line no-console
}

console.log(JSON.stringify(id.toJSON(), null, 2))
})
main()
180 changes: 57 additions & 123 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
const mh = require('multihashes')
const cryptoKeys = require('libp2p-crypto/src/keys')
const assert = require('assert')
const waterfall = require('async/waterfall')
const withIs = require('class-is')

class PeerId {
Expand Down Expand Up @@ -119,176 +118,111 @@ class PeerId {
/*
* Check if this PeerId instance is valid (privKey -> pubKey -> Id)
*/
isValid (callback) {
// TODO Needs better checking
if (this.privKey &&
isValid () {
// TODO: needs better checking
return Boolean(this.privKey &&
this.privKey.public &&
this.privKey.public.bytes &&
Buffer.isBuffer(this.pubKey.bytes) &&
this.privKey.public.bytes.equals(this.pubKey.bytes)) {
callback()
} else {
callback(new Error('Keys not match'))
}
this.privKey.public.bytes.equals(this.pubKey.bytes))
}
}

const PeerIdWithIs = withIs(PeerId, { className: 'PeerId', symbolName: '@libp2p/js-peer-id/PeerId' })
const PeerIdWithIs = withIs(PeerId, {
className: 'PeerId',
symbolName: '@libp2p/js-peer-id/PeerId'
})

exports = module.exports = PeerIdWithIs

// generation
exports.create = function (opts, callback) {
if (typeof opts === 'function') {
callback = opts
opts = {}
}
exports.create = async (opts) => {
opts = opts || {}
opts.bits = opts.bits || 2048

waterfall([
(cb) => cryptoKeys.generateKeyPair('RSA', opts.bits, cb),
(privKey, cb) => privKey.public.hash((err, digest) => {
cb(err, digest, privKey)
})
], (err, digest, privKey) => {
if (err) {
return callback(err)
}
const key = await cryptoKeys.generateKeyPair('RSA', opts.bits)
const digest = await key.public.hash()

callback(null, new PeerIdWithIs(digest, privKey))
})
return new PeerIdWithIs(digest, key)
}

exports.createFromHexString = function (str) {
exports.createFromHexString = (str) => {
return new PeerIdWithIs(mh.fromHexString(str))
}

exports.createFromBytes = function (buf) {
exports.createFromBytes = (buf) => {
return new PeerIdWithIs(buf)
}

exports.createFromB58String = function (str) {
exports.createFromB58String = (str) => {
return new PeerIdWithIs(mh.fromB58String(str))
}

// Public Key input will be a buffer
exports.createFromPubKey = function (key, callback) {
if (typeof callback !== 'function') {
throw new Error('callback is required')
}

let pubKey

try {
let buf = key
if (typeof buf === 'string') {
buf = Buffer.from(key, 'base64')
}

if (!Buffer.isBuffer(buf)) throw new Error('Supplied key is neither a base64 string nor a buffer')
exports.createFromPubKey = async (key) => {
let buf = key

pubKey = cryptoKeys.unmarshalPublicKey(buf)
} catch (err) {
return callback(err)
if (typeof buf === 'string') {
buf = Buffer.from(key, 'base64')
}

pubKey.hash((err, digest) => {
if (err) {
return callback(err)
}
if (!Buffer.isBuffer(buf)) {
throw new Error('Supplied key is neither a base64 string nor a buffer')
}

callback(null, new PeerIdWithIs(digest, null, pubKey))
})
const pubKey = await cryptoKeys.unmarshalPublicKey(buf)
const digest = await pubKey.hash()
return new PeerIdWithIs(digest, null, pubKey)
}

// Private key input will be a string
exports.createFromPrivKey = function (key, callback) {
if (typeof callback !== 'function') {
throw new Error('callback is required')
}

exports.createFromPrivKey = async (key) => {
let buf = key

try {
if (typeof buf === 'string') {
buf = Buffer.from(key, 'base64')
}
if (typeof buf === 'string') {
buf = Buffer.from(key, 'base64')
}

if (!Buffer.isBuffer(buf)) throw new Error('Supplied key is neither a base64 string nor a buffer')
} catch (err) {
return callback(err)
if (!Buffer.isBuffer(buf)) {
throw new Error('Supplied key is neither a base64 string nor a buffer')
}

waterfall([
(cb) => cryptoKeys.unmarshalPrivateKey(buf, cb),
(privKey, cb) => privKey.public.hash((err, digest) => {
cb(err, digest, privKey)
})
], (err, digest, privKey) => {
if (err) {
return callback(err)
}
const privKey = await cryptoKeys.unmarshalPrivateKey(buf)
const digest = await privKey.public.hash()

callback(null, new PeerIdWithIs(digest, privKey, privKey.public))
})
return new PeerIdWithIs(digest, privKey, privKey.public)
}

exports.createFromJSON = function (obj, callback) {
if (typeof callback !== 'function') {
throw new Error('callback is required')
exports.createFromJSON = async (obj) => {
let id = mh.fromB58String(obj.id)
let rawPrivKey = obj.privKey && Buffer.from(obj.privKey, 'base64')
let rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64')
let pub = rawPubKey && await cryptoKeys.unmarshalPublicKey(rawPubKey)

if (!rawPrivKey) {
return new PeerIdWithIs(id, null, pub)
}

let id
let rawPrivKey
let rawPubKey
let pub

try {
id = mh.fromB58String(obj.id)
rawPrivKey = obj.privKey && Buffer.from(obj.privKey, 'base64')
rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64')
pub = rawPubKey && cryptoKeys.unmarshalPublicKey(rawPubKey)
} catch (err) {
return callback(err)
const privKey = await cryptoKeys.unmarshalPrivateKey(rawPrivKey)
const privDigest = await privKey.public.hash()
let pubDigest

if (pub) {
pubDigest = await pub.hash()
}

if (rawPrivKey) {
waterfall([
(cb) => cryptoKeys.unmarshalPrivateKey(rawPrivKey, cb),
(priv, cb) => priv.public.hash((err, digest) => {
cb(err, digest, priv)
}),
(privDigest, priv, cb) => {
if (pub) {
pub.hash((err, pubDigest) => {
cb(err, privDigest, priv, pubDigest)
})
} else {
cb(null, privDigest, priv)
}
}
], (err, privDigest, priv, pubDigest) => {
if (err) {
return callback(err)
}

if (pub && !privDigest.equals(pubDigest)) {
return callback(new Error('Public and private key do not match'))
}

if (id && !privDigest.equals(id)) {
return callback(new Error('Id and private key do not match'))
}

callback(null, new PeerIdWithIs(id, priv, pub))
})
} else {
callback(null, new PeerIdWithIs(id, null, pub))
if (pub && !privDigest.equals(pubDigest)) {
throw new Error('Public and private key do not match')
}

if (id && !privDigest.equals(id)) {
throw new Error('Id and private key do not match')
}

return new PeerIdWithIs(id, privKey, pub)
}

exports.isPeerId = function (peerId) {
exports.isPeerId = (peerId) => {
return Boolean(typeof peerId === 'object' &&
peerId._id &&
peerId._idB58String)
Expand Down
Loading

0 comments on commit 24923c9

Please sign in to comment.