npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@arve.knudsen/peer-id

v0.13.1

Published

IPFS Peer Id implementation in Node.js

Downloads

12

Readme

peer-id

Discourse posts Dependency Status js-standard-style

IPFS Peer ID implementation in JavaScript.

Lead Maintainer

Pedro Teixeira

Table of Contents

Description

Generate, import, and export PeerIDs, for use with IPFS.

A Peer ID is the SHA-256 multihash of a public key.

The public key is a base64 encoded string of a protobuf containing an RSA DER buffer. This uses a node buffer to pass the base64 encoded public key protobuf to the multihash for ID generation.

Example

const PeerId = require('peer-id')

PeerId.create({ bits: 1024 }, (err, id) => {
  if (err) { throw err }
  console.log(JSON.stringify(id.toJSON(), null, 2))
})
{
  "id": "Qma9T5YraSnpRDZqRR4krcSJabThc8nwZuJV3LercPHufi",
  "privKey": "CAAS4AQwggJcAgEAAoGBAMBgbIqyOL26oV3nGPBYrdpbv..",
  "pubKey": "CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMBgbIqyOL26oV3nGPBYrdpbvzCY..."
}

Installation

npm

> npm i peer-id

Setup

Node.js

const PeerId = require('peer-id')

Browser: Browserify, Webpack, other bundlers

The code published to npm that gets loaded on require is in fact a ES5 transpiled version with the right shims added. This means that you can require it and use with your favourite bundler without having to adjust asset management process.

const PeerId = require('peer-id')

Browser: <script> Tag

Loading this module through a script tag will make the PeerId obj available in the global namespace.

<script src="https://unpkg.com/peer-id/dist/index.min.js"></script>
<!-- OR -->
<script src="https://unpkg.com/peer-id/dist/index.js"></script>

API

const PeerId = require('peer-id')

Create

new PeerId(id[, privKey, pubKey])

  • id: Buffer - The multihash of the publick key as Buffer
  • privKey: RsaPrivateKey - The private key
  • pubKey: RsaPublicKey - The public key

The key format is detailed in libp2p-crypto.

create([opts], callback)

Generates a new Peer ID, complete with public/private keypair.

  • opts: Object: Default: {bits: 2048}
  • callback: Function

Calls back callback with err, id.

Import

createFromHexString(str)

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

createFromBytes(buf)

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

createFromB58String(str)

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

createFromPubKey(pubKey)

  • publicKey: Buffer

Creates a Peer ID from a buffer containing a public key.

createFromPrivKey(privKey)

  • privKey: Buffer

Creates a Peer ID from a buffer containing a private key.

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

Export

toHexString()

Returns the Peer ID's id as a hex string.

1220d6243998f2fc56343ad7ed0342ab7886a4eb18d736f1b67d44b37fcc81e0f39f

toBytes()

Returns the Peer ID's id as a buffer.

<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>

toB58String()

Returns the Peer ID's id as a base58 string.

QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi

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()

Returns the Peer ID as a printable string without the Qm prefix.

Example: <peer.ID xxxxxx>

isEqual(id)

  • id can be a PeerId or a Buffer containing the id

License

MIT