Socket
Socket
Sign inDemoInstall

peer-id

Package Overview
Dependencies
Maintainers
1
Versions
67
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

peer-id - npm Package Compare versions

Comparing version 0.6.7 to 0.7.0

.aegir.js

255

lib/index.js

@@ -7,103 +7,113 @@ /*

var fs = require('fs');
var multihashing = require('multihashing');
var base58 = require('bs58');
var forge = require('node-forge');
var protobuf = require('protocol-buffers');
var path = require('path');
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var pbCrypto = protobuf(fs.readFileSync(path.resolve(__dirname, '../protos/crypto.proto')));
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
exports = module.exports = PeerId;
var mh = require('multihashes');
var crypto = require('libp2p-crypto');
var assert = require('assert');
exports.Buffer = Buffer;
var PeerId = function () {
function PeerId(id, privKey, pubKey) {
_classCallCheck(this, PeerId);
function PeerId(id, privKey, pubKey) {
var self = this;
assert(Buffer.isBuffer(id), 'invalid id provided');
if (!(self instanceof PeerId)) {
throw new Error('Id must be called with new');
}
if (pubKey) {
assert(id.equals(pubKey.hash()), 'inconsistent arguments');
}
self.privKey = privKey;
self.pubKey = pubKey;
self.id = id; // multihash - sha256 - buffer
if (privKey) {
assert(id.equals(privKey.public.hash()), 'inconsistent arguments');
}
// pretty print
self.toPrint = function () {
return {
id: self.toB58String(),
privKey: privKey.toString('hex'),
pubKey: pubKey.toString('hex')
};
};
if (privKey && pubKey) {
assert(privKey.public.bytes.equals(pubKey.bytes), 'inconsistent arguments');
}
self.toJSON = function () {
return {
id: self.id.toString('hex'),
privKey: self.privKey.toString('hex'),
pubKey: self.pubKey.toString('hex')
};
};
this.id = id;
this.privKey = privKey;
this._pubKey = pubKey;
}
// encode/decode functions
self.toHexString = function () {
return self.id.toString('hex');
};
_createClass(PeerId, [{
key: 'marshalPubKey',
self.toBytes = function () {
return self.id;
};
self.toB58String = function () {
return base58.encode(self.id);
};
}
// Return the protobuf version of the public key,
// matching go ipfs formatting
value: function marshalPubKey() {
if (this.pubKey) {
return crypto.marshalPublicKey(this.pubKey);
}
}
// unwrap the private key protobuf
function keyUnmarshal(key) {
return pbCrypto.PrivateKey.decode(key);
}
// Return the protobuf version of the private key,
// matching go ipfs formatting
// create a public key protobuf to be base64 string stored in config
function keyMarshal(data, type) {
var RSA = 0;
}, {
key: 'marshalPrivKey',
value: function marshalPrivKey() {
if (this.privKey) {
return crypto.marshalPrivateKey(this.privKey);
}
}
var epb = void 0;
if (type === 'Public') {
epb = pbCrypto.PublicKey.encode({
Type: RSA,
Data: data
});
}
// pretty print
if (type === 'Private') {
epb = pbCrypto.PrivateKey.encode({
Type: RSA,
Data: data
});
}
}, {
key: 'toPrint',
value: function toPrint() {
return this.toJSON();
}
return epb;
}
// return the jsonified version of the key, matching the formatting
// of go-ipfs for its config file
// this returns a base64 encoded protobuf of the public key
function formatKey(key, type) {
// create der buffer of public key asn.1 object
var der = forge.asn1.toDer(key);
}, {
key: 'toJSON',
value: function toJSON() {
return {
id: mh.toB58String(this.id),
privKey: toB64Opt(this.marshalPrivKey()),
pubKey: toB64Opt(this.marshalPubKey())
};
}
// create forge buffer of der public key buffer
var fDerBuf = forge.util.createBuffer(der.data, 'binary');
// encode/decode functions
// convert forge buffer to node buffer public key
var nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary');
}, {
key: 'toHexString',
value: function toHexString() {
return mh.toHexString(this.id);
}
}, {
key: 'toBytes',
value: function toBytes() {
return this.id;
}
}, {
key: 'toB58String',
value: function toB58String() {
return mh.toB58String(this.id);
}
}, {
key: 'pubKey',
get: function get() {
if (this._pubKey) {
return this._pubKey;
}
// protobuf the new DER bytes to the PublicKey Data: field
var marsheledKey = keyMarshal(nDerBuf, type);
if (this.privKey) {
return this.privKey.public;
}
}
}]);
// encode the protobuf public key to base64 string
var b64 = marsheledKey.toString('base64');
return b64;
}
return PeerId;
}();
exports = module.exports = PeerId;
exports.Buffer = Buffer;
// generation

@@ -114,27 +124,9 @@ exports.create = function (opts) {

// generate keys
var pair = forge.rsa.generateKeyPair({
bits: opts.bits,
e: 0x10001
});
var privKey = crypto.generateKeyPair('RSA', opts.bits);
// return the RSA public/private key to asn1 object
var asnPub = forge.pki.publicKeyToAsn1(pair.publicKey);
var asnPriv = forge.pki.privateKeyToAsn1(pair.privateKey);
// format the keys to protobuf base64 encoded string
var protoPublic64 = formatKey(asnPub, 'Public');
var protoPrivate64 = formatKey(asnPriv, 'Private');
// store the keys as a buffer
var bufProtoPub64 = new Buffer(protoPublic64, 'base64');
var bufProtoPriv64 = new Buffer(protoPrivate64, 'base64');
var mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256');
return new PeerId(mhId, bufProtoPriv64, bufProtoPub64);
return new PeerId(privKey.public.hash(), privKey);
};
exports.createFromHexString = function (str) {
return new PeerId(new Buffer(str, 'hex'));
return new PeerId(mh.fromHexString(str));
};

@@ -147,46 +139,45 @@

exports.createFromB58String = function (str) {
return new PeerId(new Buffer(base58.decode(str)));
return new PeerId(mh.fromB58String(str));
};
// Public Key input will be a buffer
exports.createFromPubKey = function (pubKey) {
var buf = new Buffer(pubKey, 'base64');
var mhId = multihashing(buf, 'sha2-256');
return new PeerId(mhId, null, pubKey);
exports.createFromPubKey = function (key) {
var buf = key;
if (typeof buf === 'string') {
buf = new Buffer(key, 'base64');
}
var pubKey = crypto.unmarshalPublicKey(buf);
return new PeerId(pubKey.hash(), null, pubKey);
};
// Private key input will be a string
exports.createFromPrivKey = function (privKey) {
// create a buffer from the base64 encoded string
var buf = new Buffer(privKey, 'base64');
exports.createFromPrivKey = function (key) {
var buf = key;
if (typeof buf === 'string') {
buf = new Buffer(key, 'base64');
}
// get the private key data from the protobuf
var mpk = keyUnmarshal(buf);
var privKey = crypto.unmarshalPrivateKey(buf);
return new PeerId(privKey.public.hash(), privKey);
};
// create a forge buffer
var fbuf = forge.util.createBuffer(mpk.Data.toString('binary'));
exports.createFromJSON = function (obj) {
var priv = void 0;
var pub = void 0;
// create an asn1 object from the private key bytes saved in the protobuf Data: field
var asnPriv = forge.asn1.fromDer(fbuf);
if (obj.privKey) {
priv = crypto.unmarshalPrivateKey(new Buffer(obj.privKey, 'base64'));
}
// get the RSA privatekey data from the asn1 object
var privateKey = forge.pki.privateKeyFromAsn1(asnPriv);
if (obj.pubKey) {
pub = crypto.unmarshalPublicKey(new Buffer(obj.pubKey, 'base64'));
}
// set the RSA public key to the modulus and exponent of the private key
var publicKey = forge.pki.rsa.setPublicKey(privateKey.n, privateKey.e);
// return the RSA public key to asn1 object
var asnPub = forge.pki.publicKeyToAsn1(publicKey);
// format the public key
var protoPublic64 = formatKey(asnPub, 'Public');
// buffer the public key for consistency before storing
var bufProtoPub64 = new Buffer(protoPublic64, 'base64');
var mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256');
return new PeerId(mhId, privKey, bufProtoPub64);
return new PeerId(mh.fromB58String(obj.id), priv, pub);
};
exports.createFromJSON = function (obj) {
return new PeerId(new Buffer(obj.id, 'hex'), new Buffer(obj.privKey, 'hex'), new Buffer(obj.pubKey, 'hex'));
};
function toB64Opt(val) {
if (val) {
return val.toString('base64');
}
}
{
"name": "peer-id",
"version": "0.6.7",
"version": "0.7.0",
"description": "IPFS Peer Id implementation in Node.js",

@@ -15,2 +15,4 @@ "main": "lib/index.js",

"release": "aegir-release",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
"coverage": "aegir-coverage",

@@ -36,12 +38,9 @@ "coverage-publish": "aegir-coverage publish"

"devDependencies": {
"aegir": "^3.0.4",
"buffer-loader": "0.0.1",
"aegir": "^3.1.1",
"chai": "^3.5.0",
"pre-commit": "^1.1.2"
"pre-commit": "^1.1.3"
},
"dependencies": {
"bs58": "^3.0.0",
"multihashing": "^0.2.0",
"node-forge": "^0.6.38",
"protocol-buffers": "^3.1.4"
"libp2p-crypto": "^0.5.0",
"multihashes": "^0.2.2"
},

@@ -52,11 +51,2 @@ "repository": {

},
"aegir": {
"webpack": {
"resolve": {
"alias": {
"node-forge": "../vendor/forge.bundle.js"
}
}
}
},
"contributors": [

@@ -63,0 +53,0 @@ "David Dias <daviddias.p@gmail.com>",

@@ -32,4 +32,4 @@ # peer-id

console.log('id ', id.toB58String())
console.log('priv key ', bs58.encode(id.privKey))
console.log('pub key ', bs58.encode(id.pubKey))
console.log('priv key ', bs58.encode(id.privKey.bytes))
console.log('pub key ', bs58.encode(id.pubKey.bytes))
```

@@ -55,3 +55,3 @@

```JavaScript
```js
var PeerId = require('peer-id')

@@ -67,3 +67,3 @@ ```

```JavaScript
```js
var PeerId = require('peer-id')

@@ -91,47 +91,48 @@ ```

### PeerId.create()
### `new PeerId(id[, privKey, pubKey])`
Generates a new Peer ID, complete with public/private keypair. A Peer ID has the
following properties:
- `id: Buffer` - The multihash of the publick key as `Buffer`
- `privKey: RsaPrivateKey` - The private key
- `pubKey: RsaPublicKey` - The public key
- `pubKey` - Buffer containing the public key bytes
- `privKey` - Buffer containing the private key bytes
- `id` - Buffer containing the multihash bytes
The key format is detailed in [libp2p-crypto](https://github.com/ipfs/js-libp2p-crypto).
### `create([opts])`
Generates a new Peer ID, complete with public/private keypair.
- `opts: Object`: Default: `{bits: 2048}`
## Import
### PeerId.createFromHexString(str)
### `createFromHexString(str)`
Creates a Peer ID from hex string representing the key's multihash.
### PeerId.createFromBytes(buf)
### `createFromBytes(buf)`
Creates a Peer ID from a buffer representing the key's multihash.
### PeerId.createFromB58String(str)
### `createFromB58String(str)`
Creates a Peer ID from a Base58 string representing the key's multihash.
### PeerId.createFromPubKey(pubKey)
### `createFromPubKey(pubKey)`
Creates a Peer ID from a buffer containing a public key.
### PeerId.createFromPrivKey(privKey)
### `createFromPrivKey(privKey)`
Creates a Peer ID from a buffer containing a private key.
## Export
### `createFromJSON(obj)`
### id.toPrint()
- `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 'base 64'
Returns an object with the ID's properties in hex format:
```js
{
id: 'QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi',
privKey: '080012a609308204a20201000282010100a608889914da08959d3a3db0734cee812c96...',
pubKey: '080012a60230820122300d06092a864886f70d01010105000382010f003082010a0282010...'
}
```
### id.toHexString()
## Export
### `toHexString()`
Returns the Peer ID's `id` as a hex string.

@@ -143,7 +144,6 @@

### id.toBytes()
### `toBytes()`
Returns the Peer ID's `id` as a buffer.
```

@@ -153,3 +153,3 @@ <Buffer 12 20 d6 24 39 98 f2 fc 56 34 3a d7 ed 03 42 ab 78 86 a4 eb 18 d7 36 f1 b6 7d 44 b3 7f cc 81 e0 f3 9f>

### id.toB58String()
### `toB58String()`

@@ -162,4 +162,18 @@ Returns the Peer ID's `id` as a base58 string.

### `toJSON()`
Returns an `obj` of the form
- `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 'base 64'
### `toPrint()`
Alias for `.toJSON()`.
# License
MIT

@@ -7,103 +7,85 @@ /*

const fs = require('fs')
const multihashing = require('multihashing')
const base58 = require('bs58')
const forge = require('node-forge')
const protobuf = require('protocol-buffers')
const path = require('path')
const mh = require('multihashes')
const crypto = require('libp2p-crypto')
const assert = require('assert')
const pbCrypto = protobuf(fs.readFileSync(path.resolve(__dirname, '../protos/crypto.proto')))
class PeerId {
constructor (id, privKey, pubKey) {
assert(Buffer.isBuffer(id), 'invalid id provided')
exports = module.exports = PeerId
if (pubKey) {
assert(id.equals(pubKey.hash()), 'inconsistent arguments')
}
exports.Buffer = Buffer
if (privKey) {
assert(id.equals(privKey.public.hash()), 'inconsistent arguments')
}
function PeerId (id, privKey, pubKey) {
const self = this
if (privKey && pubKey) {
assert(privKey.public.bytes.equals(pubKey.bytes), 'inconsistent arguments')
}
if (!(self instanceof PeerId)) {
throw new Error('Id must be called with new')
this.id = id
this.privKey = privKey
this._pubKey = pubKey
}
self.privKey = privKey
self.pubKey = pubKey
self.id = id // multihash - sha256 - buffer
get pubKey () {
if (this._pubKey) {
return this._pubKey
}
// pretty print
self.toPrint = function () {
return {
id: self.toB58String(),
privKey: privKey.toString('hex'),
pubKey: pubKey.toString('hex')
if (this.privKey) {
return this.privKey.public
}
}
self.toJSON = function () {
return {
id: self.id.toString('hex'),
privKey: self.privKey.toString('hex'),
pubKey: self.pubKey.toString('hex')
// Return the protobuf version of the public key,
// matching go ipfs formatting
marshalPubKey () {
if (this.pubKey) {
return crypto.marshalPublicKey(this.pubKey)
}
}
// encode/decode functions
self.toHexString = function () {
return self.id.toString('hex')
// Return the protobuf version of the private key,
// matching go ipfs formatting
marshalPrivKey () {
if (this.privKey) {
return crypto.marshalPrivateKey(this.privKey)
}
}
self.toBytes = function () {
return self.id
// pretty print
toPrint () {
return this.toJSON()
}
self.toB58String = function () {
return base58.encode(self.id)
// return the jsonified version of the key, matching the formatting
// of go-ipfs for its config file
toJSON () {
return {
id: mh.toB58String(this.id),
privKey: toB64Opt(this.marshalPrivKey()),
pubKey: toB64Opt(this.marshalPubKey())
}
}
}
// unwrap the private key protobuf
function keyUnmarshal (key) {
return pbCrypto.PrivateKey.decode(key)
}
// encode/decode functions
toHexString () {
return mh.toHexString(this.id)
}
// create a public key protobuf to be base64 string stored in config
function keyMarshal (data, type) {
const RSA = 0
let epb
if (type === 'Public') {
epb = pbCrypto.PublicKey.encode({
Type: RSA,
Data: data
})
toBytes () {
return this.id
}
if (type === 'Private') {
epb = pbCrypto.PrivateKey.encode({
Type: RSA,
Data: data
})
toB58String () {
return mh.toB58String(this.id)
}
return epb
}
// this returns a base64 encoded protobuf of the public key
function formatKey (key, type) {
// create der buffer of public key asn.1 object
const der = forge.asn1.toDer(key)
exports = module.exports = PeerId
exports.Buffer = Buffer
// create forge buffer of der public key buffer
const fDerBuf = forge.util.createBuffer(der.data, 'binary')
// convert forge buffer to node buffer public key
const nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary')
// protobuf the new DER bytes to the PublicKey Data: field
const marsheledKey = keyMarshal(nDerBuf, type)
// encode the protobuf public key to base64 string
const b64 = marsheledKey.toString('base64')
return b64
}
// generation

@@ -114,27 +96,9 @@ exports.create = function (opts) {

// generate keys
const pair = forge.rsa.generateKeyPair({
bits: opts.bits,
e: 0x10001
})
const privKey = crypto.generateKeyPair('RSA', opts.bits)
// return the RSA public/private key to asn1 object
const asnPub = forge.pki.publicKeyToAsn1(pair.publicKey)
const asnPriv = forge.pki.privateKeyToAsn1(pair.privateKey)
// format the keys to protobuf base64 encoded string
const protoPublic64 = formatKey(asnPub, 'Public')
const protoPrivate64 = formatKey(asnPriv, 'Private')
// store the keys as a buffer
const bufProtoPub64 = new Buffer(protoPublic64, 'base64')
const bufProtoPriv64 = new Buffer(protoPrivate64, 'base64')
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
return new PeerId(mhId, bufProtoPriv64, bufProtoPub64)
return new PeerId(privKey.public.hash(), privKey)
}
exports.createFromHexString = function (str) {
return new PeerId(new Buffer(str, 'hex'))
return new PeerId(mh.fromHexString(str))
}

@@ -147,49 +111,45 @@

exports.createFromB58String = function (str) {
return new PeerId(new Buffer(base58.decode(str)))
return new PeerId(mh.fromB58String(str))
}
// Public Key input will be a buffer
exports.createFromPubKey = function (pubKey) {
const buf = new Buffer(pubKey, 'base64')
const mhId = multihashing(buf, 'sha2-256')
return new PeerId(mhId, null, pubKey)
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)
}
// Private key input will be a string
exports.createFromPrivKey = function (privKey) {
// create a buffer from the base64 encoded string
const buf = new Buffer(privKey, 'base64')
exports.createFromPrivKey = function (key) {
let buf = key
if (typeof buf === 'string') {
buf = new Buffer(key, 'base64')
}
// get the private key data from the protobuf
const mpk = keyUnmarshal(buf)
const privKey = crypto.unmarshalPrivateKey(buf)
return new PeerId(privKey.public.hash(), privKey)
}
// create a forge buffer
const fbuf = forge.util.createBuffer(mpk.Data.toString('binary'))
exports.createFromJSON = function (obj) {
let priv
let pub
// create an asn1 object from the private key bytes saved in the protobuf Data: field
const asnPriv = forge.asn1.fromDer(fbuf)
if (obj.privKey) {
priv = crypto.unmarshalPrivateKey(new Buffer(obj.privKey, 'base64'))
}
// get the RSA privatekey data from the asn1 object
const privateKey = forge.pki.privateKeyFromAsn1(asnPriv)
if (obj.pubKey) {
pub = crypto.unmarshalPublicKey(new Buffer(obj.pubKey, 'base64'))
}
// set the RSA public key to the modulus and exponent of the private key
const publicKey = forge.pki.rsa.setPublicKey(privateKey.n, privateKey.e)
// return the RSA public key to asn1 object
const asnPub = forge.pki.publicKeyToAsn1(publicKey)
// format the public key
const protoPublic64 = formatKey(asnPub, 'Public')
// buffer the public key for consistency before storing
const bufProtoPub64 = new Buffer(protoPublic64, 'base64')
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
return new PeerId(mhId, privKey, bufProtoPub64)
return new PeerId(mh.fromB58String(obj.id), priv, pub)
}
exports.createFromJSON = function (obj) {
return new PeerId(
new Buffer(obj.id, 'hex'),
new Buffer(obj.privKey, 'hex'),
new Buffer(obj.pubKey, 'hex'))
function toB64Opt (val) {
if (val) {
return val.toString('base64')
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc