Skip to content

Commit

Permalink
wip: async await
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Oct 30, 2018
1 parent eb06275 commit 383d9df
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 253 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
},
"homepage": "https://github.com/libp2p/js-peer-id",
"devDependencies": {
"aegir": "^14.0.0",
"chai": "^4.1.2",
"aegir": "^17.0.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1"
},
"dependencies": {
"async": "^2.6.1",
"class-is": "^1.1.0",
"libp2p-crypto": "~0.13.0",
"lodash": "^4.17.10",
"multihashes": "~0.4.13"
"libp2p-crypto": "libp2p/js-libp2p-crypto#371bd05",
"lodash": "^4.17.11",
"multihashes": "~0.4.14"
},
"repository": {
"type": "git",
Expand Down
165 changes: 50 additions & 115 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 crypto = require('libp2p-crypto')
const assert = require('assert')
const waterfall = require('async/waterfall')
const withIs = require('class-is')

class PeerId {
Expand Down Expand Up @@ -55,14 +54,14 @@ class PeerId {
}

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

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

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

Expand Down Expand Up @@ -138,26 +137,14 @@ const PeerIdWithIs = withIs(PeerId, { className: 'PeerId', symbolName: '@libp2p/
exports = module.exports = PeerIdWithIs

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

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)
}
const key = await crypto.keys.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) {
Expand All @@ -173,119 +160,67 @@ exports.createFromB58String = function (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 function (key) {
let buf = key

pubKey = crypto.keys.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 crypto.keys.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 function (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) => crypto.keys.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 crypto.keys.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 function (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 crypto.keys.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 && crypto.keys.unmarshalPublicKey(rawPubKey)
} catch (err) {
return callback(err)
const privKey = await crypto.keys.unmarshalPrivateKey(rawPrivKey)
const privDigest = await privKey.public.hash()
let pubDigest

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

if (rawPrivKey) {
waterfall([
(cb) => crypto.keys.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) {
Expand Down
Loading

0 comments on commit 383d9df

Please sign in to comment.