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')
|
|
|
|
|
|
|
|
class PeerId {
|
|
|
|
constructor (id, privKey, pubKey) {
|
|
|
|
if (Buffer.isBuffer(id)) {
|
|
|
|
this.id = id
|
|
|
|
} else {
|
|
|
|
throw new Error('invalid id provided')
|
|
|
|
}
|
2016-02-10 13:55:59 -08:00
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
if (pubKey) {
|
|
|
|
assert(this.id.equals(pubKey.hash()), 'inconsistent arguments')
|
|
|
|
}
|
2015-07-08 14:51:49 -07:00
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
if (privKey) {
|
|
|
|
assert(this.id.equals(privKey.public.hash()), 'inconsistent arguments')
|
|
|
|
}
|
2015-11-05 17:47:44 +00: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-05-23 22:06:25 +02:00
|
|
|
this.privKey = privKey
|
|
|
|
this._pubKey = pubKey
|
2015-07-08 14:51:49 -07:00
|
|
|
}
|
|
|
|
|
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-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-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 () {
|
|
|
|
return {
|
|
|
|
id: mh.toB58String(this.id),
|
|
|
|
privKey: toHexOpt(this.marshalPrivKey()),
|
|
|
|
pubKey: toHexOpt(this.marshalPubKey())
|
|
|
|
}
|
2015-07-08 14:51:49 -07:00
|
|
|
}
|
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
toJSON () {
|
|
|
|
return {
|
|
|
|
id: mh.toHexString(this.id),
|
|
|
|
privKey: toHexOpt(this.marshalPrivKey()),
|
|
|
|
pubKey: toHexOpt(this.marshalPubKey())
|
|
|
|
}
|
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 () {
|
|
|
|
return mh.toB58String(this.id)
|
|
|
|
}
|
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-03-14 17:10:02 -07:00
|
|
|
exports.create = function (opts) {
|
|
|
|
opts = opts || {}
|
|
|
|
opts.bits = opts.bits || 2048
|
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
const privKey = crypto.generateKeyPair('RSA', opts.bits)
|
2016-02-02 15:50:45 -08:00
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
return new PeerId(privKey.public.hash(), 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-05-23 22:06:25 +02:00
|
|
|
exports.createFromPubKey = function (key) {
|
|
|
|
let buf = key
|
|
|
|
if (typeof buf === 'string') {
|
|
|
|
buf = new Buffer(key, 'base64')
|
|
|
|
}
|
|
|
|
const pubKey = crypto.unmarshalPublicKey(buf)
|
|
|
|
return new PeerId(pubKey.hash(), 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-05-23 22:06:25 +02:00
|
|
|
exports.createFromPrivKey = function (key) {
|
|
|
|
let buf = key
|
|
|
|
if (typeof buf === 'string') {
|
|
|
|
buf = new Buffer(key, 'base64')
|
|
|
|
}
|
2016-02-02 15:50:45 -08:00
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
const privKey = crypto.unmarshalPrivateKey(buf)
|
|
|
|
return new PeerId(privKey.public.hash(), privKey)
|
|
|
|
}
|
2016-02-02 15:50:45 -08:00
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
exports.createFromJSON = function (obj) {
|
|
|
|
let priv
|
|
|
|
let pub
|
2016-02-02 15:50:45 -08:00
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
if (obj.privKey) {
|
|
|
|
priv = crypto.unmarshalPrivateKey(new Buffer(obj.privKey, 'hex'))
|
|
|
|
}
|
2016-02-02 15:50:45 -08:00
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
if (obj.pubKey) {
|
|
|
|
pub = crypto.unmarshalPublicKey(new Buffer(obj.pubKey, 'hex'))
|
|
|
|
}
|
2016-03-10 10:32:48 -08:00
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
return new PeerId(mh.fromHexString(obj.id), priv, pub)
|
2016-05-23 15:25:30 +01:00
|
|
|
}
|
|
|
|
|
2016-05-23 22:06:25 +02:00
|
|
|
function toHexOpt (val) {
|
|
|
|
if (val) {
|
|
|
|
return val.toString('hex')
|
|
|
|
}
|
2015-07-08 14:51:49 -07:00
|
|
|
}
|