Skip to content

Commit

Permalink
wip: async await
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
  • Loading branch information
hacdias committed Oct 30, 2018
1 parent e3211e0 commit 5d408bf
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 92 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ language: node_js

matrix:
include:
- node_js: 6
env: CXX=g++-4.8
- node_js: 8
env: CXX=g++-4.8
- node_js: 10
env: CXX=g++-4.8
# - node_js: stable
# env: CXX=g++-4.8

Expand Down
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 `Promise<PeerId>`.

### `createFromBytes(buf)`

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

Returns `Promise<PeerId>`.

### `createFromB58String(str)`

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

Returns `Promise<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
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ version: "{build}"

environment:
matrix:
- nodejs_version: "6"
- nodejs_version: "8"
- nodejs_version: "10"

matrix:
fast_finish: true
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, 1))
}

console.log(JSON.stringify(id.toJSON(), null, 2))
})
main()
33 changes: 18 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ class PeerId {
}

// Return the protobuf version of the public key, matching go ipfs formatting
async marshalPubKey () {
marshalPubKey () {
if (this.pubKey) {
return crypto.keys.marshalPublicKey(this.pubKey)
}
}

// Return the protobuf version of the private key, matching go ipfs formatting
async marshalPrivKey () {
marshalPrivKey () {
if (this.privKey) {
return crypto.keys.marshalPrivateKey(this.privKey)
}
Expand All @@ -84,11 +84,11 @@ class PeerId {

// return the jsonified version of the key, matching the formatting
// of go-ipfs for its config file
async toJSON () {
toJSON () {
return {
id: this.toB58String(),
privKey: toB64Opt(await this.marshalPrivKey()),
pubKey: toB64Opt(await this.marshalPubKey())
privKey: toB64Opt(this.marshalPrivKey()),
pubKey: toB64Opt(this.marshalPubKey())
}
}

Expand Down Expand Up @@ -118,7 +118,7 @@ class PeerId {
/*
* Check if this PeerId instance is valid (privKey -> pubKey -> Id)
*/
async isValid () {
isValid () {
// TODO Needs better checking
if (this.privKey &&
this.privKey.public &&
Expand All @@ -132,12 +132,15 @@ class PeerId {
}
}

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 = async function (opts) {
exports.create = async (opts) => {
opts = opts || {}
opts.bits = opts.bits || 2048

Expand All @@ -147,20 +150,20 @@ exports.create = async function (opts) {
return new PeerIdWithIs(digest, key)
}

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

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

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

// Public Key input will be a buffer
exports.createFromPubKey = async function (key) {
exports.createFromPubKey = async (key) => {
let buf = key

if (typeof buf === 'string') {
Expand All @@ -177,7 +180,7 @@ exports.createFromPubKey = async function (key) {
}

// Private key input will be a string
exports.createFromPrivKey = async function (key) {
exports.createFromPrivKey = async (key) => {
let buf = key

if (typeof buf === 'string') {
Expand All @@ -194,7 +197,7 @@ exports.createFromPrivKey = async function (key) {
return new PeerIdWithIs(digest, privKey, privKey.public)
}

exports.createFromJSON = async function (obj) {
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')
Expand Down Expand Up @@ -223,7 +226,7 @@ exports.createFromJSON = async function (obj) {
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 5d408bf

Please sign in to comment.