2015-07-08 14:51:49 -07:00
|
|
|
/*
|
|
|
|
* Id is an object representation of a peer Id. a peer Id is a multihash
|
|
|
|
*/
|
2016-05-23 14:16:30 +01:00
|
|
|
|
2016-03-22 17:01:46 +01:00
|
|
|
'use strict'
|
2015-07-08 14:51:49 -07:00
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
const mh = require('multihashes')
|
|
|
|
const crypto = require('libp2p-crypto')
|
|
|
|
const assert = require('assert')
|
2016-11-03 08:51:29 +01:00
|
|
|
const waterfall = require('async/waterfall')
|
2016-05-23 22:06:25 +02:00
|
|
|
|
|
|
|
class PeerId {
|
|
|
|
constructor (id, privKey, pubKey) {
|
2016-05-24 14:03:31 +02:00
|
|
|
assert(Buffer.isBuffer(id), 'invalid id provided')
|
2016-02-10 13:55:59 -08:00
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
if (privKey && pubKey) {
|
|
|
|
assert(privKey.public.bytes.equals(pubKey.bytes), 'inconsistent arguments')
|
|
|
|
}
|
2015-07-08 14:51:49 -07:00
|
|
|
|
2016-12-14 09:05:07 +01:00
|
|
|
this._id = id
|
|
|
|
this._idB58String = ''
|
2016-11-03 08:51:29 +01:00
|
|
|
this._privKey = privKey
|
2016-05-23 22:06:25 +02:00
|
|
|
this._pubKey = pubKey
|
2015-07-08 14:51:49 -07:00
|
|
|
}
|
|
|
|
|
2016-12-14 09:05:07 +01:00
|
|
|
get id () {
|
|
|
|
return this._id
|
|
|
|
}
|
|
|
|
|
2016-11-03 08:51:29 +01:00
|
|
|
get privKey () {
|
|
|
|
return this._privKey
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
get pubKey () {
|
|
|
|
if (this._pubKey) {
|
|
|
|
return this._pubKey
|
|
|
|
}
|
2015-07-08 14:51:49 -07:00
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
if (this.privKey) {
|
|
|
|
return this.privKey.public
|
2015-07-08 14:51:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
// Return the protobuf version of the public key,
|
|
|
|
// matching go ipfs formatting
|
2016-05-23 22:06:25 +02:00
|
|
|
marshalPubKey () {
|
|
|
|
if (this.pubKey) {
|
|
|
|
return crypto.marshalPublicKey(this.pubKey)
|
2016-05-23 15:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
// Return the protobuf version of the private key,
|
|
|
|
// matching go ipfs formatting
|
2016-05-23 22:06:25 +02:00
|
|
|
marshalPrivKey () {
|
|
|
|
if (this.privKey) {
|
|
|
|
return crypto.marshalPrivateKey(this.privKey)
|
|
|
|
}
|
2015-07-08 14:51:49 -07:00
|
|
|
}
|
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
// pretty print
|
|
|
|
toPrint () {
|
2016-05-24 14:03:31 +02:00
|
|
|
return this.toJSON()
|
2015-07-08 14:51:49 -07:00
|
|
|
}
|
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
// return the jsonified version of the key, matching the formatting
|
|
|
|
// of go-ipfs for its config file
|
2016-05-23 22:06:25 +02:00
|
|
|
toJSON () {
|
|
|
|
return {
|
2016-12-14 09:05:07 +01:00
|
|
|
id: this.toB58String(),
|
2016-05-24 14:03:31 +02:00
|
|
|
privKey: toB64Opt(this.marshalPrivKey()),
|
|
|
|
pubKey: toB64Opt(this.marshalPubKey())
|
2016-05-23 22:06:25 +02:00
|
|
|
}
|
2015-07-08 14:51:49 -07:00
|
|
|
}
|
2015-11-05 18:51:53 +00:00
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
// encode/decode functions
|
|
|
|
toHexString () {
|
|
|
|
return mh.toHexString(this.id)
|
2016-02-02 15:50:45 -08:00
|
|
|
}
|
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
toBytes () {
|
|
|
|
return this.id
|
2016-02-02 15:50:45 -08:00
|
|
|
}
|
2015-07-08 14:51:49 -07:00
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
toB58String () {
|
2016-12-14 09:05:07 +01:00
|
|
|
if (!this._idB58String) {
|
|
|
|
this._idB58String = mh.toB58String(this.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._idB58String
|
2016-05-23 22:06:25 +02:00
|
|
|
}
|
2016-02-02 15:50:45 -08:00
|
|
|
}
|
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
exports = module.exports = PeerId
|
|
|
|
exports.Buffer = Buffer
|
2016-02-02 15:50:45 -08:00
|
|
|
|
2016-02-10 13:55:59 -08:00
|
|
|
// generation
|
2016-11-03 08:51:29 +01:00
|
|
|
exports.create = function (opts, callback) {
|
|
|
|
if (typeof opts === 'function') {
|
|
|
|
callback = opts
|
|
|
|
opts = {}
|
|
|
|
}
|
2016-03-14 17:10:02 -07:00
|
|
|
opts = opts || {}
|
|
|
|
opts.bits = opts.bits || 2048
|
|
|
|
|
2016-11-03 08:51:29 +01:00
|
|
|
waterfall([
|
|
|
|
(cb) => crypto.generateKeyPair('RSA', opts.bits, cb),
|
|
|
|
(privKey, cb) => privKey.public.hash((err, digest) => {
|
|
|
|
cb(err, digest, privKey)
|
|
|
|
})
|
|
|
|
], (err, digest, privKey) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
2016-02-02 15:50:45 -08:00
|
|
|
|
2016-11-03 08:51:29 +01:00
|
|
|
callback(null, new PeerId(digest, privKey))
|
|
|
|
})
|
2015-07-08 14:51:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.createFromHexString = function (str) {
|
2016-05-23 22:06:25 +02:00
|
|
|
return new PeerId(mh.fromHexString(str))
|
2015-07-08 14:51:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.createFromBytes = function (buf) {
|
2016-05-23 15:25:30 +01:00
|
|
|
return new PeerId(buf)
|
2015-07-08 14:51:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.createFromB58String = function (str) {
|
2016-05-23 22:06:25 +02:00
|
|
|
return new PeerId(mh.fromB58String(str))
|
2015-07-08 14:51:49 -07:00
|
|
|
}
|
|
|
|
|
2016-03-10 10:32:48 -08:00
|
|
|
// Public Key input will be a buffer
|
2016-11-03 08:51:29 +01:00
|
|
|
exports.createFromPubKey = function (key, callback) {
|
2016-05-23 22:06:25 +02:00
|
|
|
let buf = key
|
|
|
|
if (typeof buf === 'string') {
|
|
|
|
buf = new Buffer(key, 'base64')
|
|
|
|
}
|
2016-11-03 08:51:29 +01:00
|
|
|
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw new Error('callback is required')
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
const pubKey = crypto.unmarshalPublicKey(buf)
|
2016-11-03 08:51:29 +01:00
|
|
|
pubKey.hash((err, digest) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, new PeerId(digest, null, pubKey))
|
|
|
|
})
|
2015-07-08 14:51:49 -07:00
|
|
|
}
|
|
|
|
|
2016-03-10 10:32:48 -08:00
|
|
|
// Private key input will be a string
|
2016-11-03 08:51:29 +01:00
|
|
|
exports.createFromPrivKey = function (key, callback) {
|
2016-05-23 22:06:25 +02:00
|
|
|
let buf = key
|
|
|
|
if (typeof buf === 'string') {
|
|
|
|
buf = new Buffer(key, 'base64')
|
|
|
|
}
|
2016-02-02 15:50:45 -08:00
|
|
|
|
2016-11-03 08:51:29 +01:00
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw new Error('callback is required')
|
|
|
|
}
|
2016-02-02 15:50:45 -08:00
|
|
|
|
2016-11-03 08:51:29 +01:00
|
|
|
waterfall([
|
|
|
|
(cb) => crypto.unmarshalPrivateKey(buf, cb),
|
|
|
|
(privKey, cb) => privKey.public.hash((err, digest) => {
|
|
|
|
cb(err, digest, privKey)
|
|
|
|
})
|
|
|
|
], (err, digest, privKey) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
2016-02-02 15:50:45 -08:00
|
|
|
|
2016-11-03 08:51:29 +01:00
|
|
|
callback(null, new PeerId(digest, privKey))
|
|
|
|
})
|
|
|
|
}
|
2016-02-02 15:50:45 -08:00
|
|
|
|
2016-11-03 08:51:29 +01:00
|
|
|
exports.createFromJSON = function (obj, callback) {
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw new Error('callback is required')
|
2016-05-23 22:06:25 +02:00
|
|
|
}
|
2016-03-10 10:32:48 -08:00
|
|
|
|
2016-11-03 08:51:29 +01:00
|
|
|
const id = mh.fromB58String(obj.id)
|
|
|
|
const rawPrivKey = obj.privKey && new Buffer(obj.privKey, 'base64')
|
|
|
|
const rawPubKey = obj.pubKey && new Buffer(obj.pubKey, 'base64')
|
|
|
|
const pub = rawPubKey && crypto.unmarshalPublicKey(rawPubKey)
|
|
|
|
|
|
|
|
if (rawPrivKey) {
|
|
|
|
waterfall([
|
|
|
|
(cb) => crypto.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 PeerId(id, priv, pub))
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
callback(null, new PeerId(id, null, pub))
|
|
|
|
}
|
2016-05-23 15:25:30 +01:00
|
|
|
}
|
|
|
|
|
2016-05-24 14:03:31 +02:00
|
|
|
function toB64Opt (val) {
|
2016-05-23 22:06:25 +02:00
|
|
|
if (val) {
|
2016-05-24 14:03:31 +02:00
|
|
|
return val.toString('base64')
|
2016-05-23 22:06:25 +02:00
|
|
|
}
|
2015-07-08 14:51:49 -07:00
|
|
|
}
|