Introduction
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.
+white-space: nowrap; +border-radius: 4px;}.hljs-meta{color: #666;}.hljs-comment{color: #BBB;}.hljs-number{color: #9B80FF;}.hljs-string{color: #75DE00;}.hljs-keyword, .hljs-function{color: #F95FAA;}.hljs-symbol, .hljs-class, .hljs-title, .hljs-literal{color: #EEBA00;}.hljs-params{color: #444;}.hljs-built_in{font-weight: 500;}.hljs-subst{color: #666;}.hljs-name{color: #EEBA00;}.hljs-attr{font-weight: 500;}Introduction
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
var PeerId = require('peer-id')
PeerId.create({ bits: 1024 }, (err, id) => {
console.log(JSON.stringify(id.toJSON(), null, 2)
})
-{
- "id": "Qma9T5YraSnpRDZqRR4krcSJabThc8nwZuJV3LercPHufi",
- "privKey": "CAAS4AQwggJcAgEAAoGBAMBgbIqyOL26oV3nGPBYrdpbv..",
- "pubKey": "CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMBgbIqyOL26oV3nGPBYrdpbvzCY..."
-}
-
+{
+ "id": "Qma9T5YraSnpRDZqRR4krcSJabThc8nwZuJV3LercPHufi",
+ "privKey": "CAAS4AQwggJcAgEAAoGBAMBgbIqyOL26oV3nGPBYrdpbv..",
+ "pubKey": "CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMBgbIqyOL26oV3nGPBYrdpbvzCY..."
+}
Installation
npm
> npm i peer-id@@ -66,11 +75,11 @@ 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>
-PeerId
A Peer ID is the SHA-256 multihash of a public key.
See
static
PeerId.create
create(opts: Object?, callback: function (Error, PeerId))
Create a new PeerId
by generating a new public/private keypair.
static
PeerId.create
create(opts: Object?, callback: function (Error, PeerId))
Create a new PeerId
by generating a new public/private keypair.
Parameters
Name | Description | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
opts: Object? | Configuration object. | ||||||||||||||||
opts.bits: number? (=2048) | How many bits to use for the RSA key generation. | ||||||||||||||||
callback: function (Error, PeerId) | Node.js style callback.
@@ -81,44 +90,52 @@ PeerId.create((err, idthrow err
}
console.log('id', JSON.stringify(id))
-})PeerId.createFromHexString
Creates a Peer ID from hex string representing the key's multihash. +})PeerId.createFromHexString
Creates a Peer ID from hex string representing the key's multihash. Parameters
ReturnsPeerId
-PeerId.createFromBytes
Creates a Peer ID from a buffer representing the key's multihash. +PeerId.createFromBytes
Creates a Peer ID from a buffer representing the key's multihash. Parameters
ReturnsPeerId
-PeerId.createFromB58String
Creates a Peer ID from a PeerId.createFromB58String
Creates a Peer ID from a Parameters
ReturnsPeerId
-PeerId.createFromPubKey
Creates a Peer ID from a buffer containing a public key. -PeerId.createFromPrivKey
Creates a Peer ID from a buffer containing a private key. +PeerId.createFromPubKey
Creates a Peer ID from a buffer containing a public key. +PeerId.createFromPrivKey
Creates a Peer ID from a buffer containing a private key. PeerId.createFromJSON
Import a Parameters
|
PeerId.createFromJSON
createFromJSON(obj: PeerIdJson, callback: function (Error, PeerId))
Import a PeerId
from a serialized JSON object.
Parameters
Name | Description |
---|---|
obj: PeerIdJson |
instance
constructor
constructor(id: Buffer, privKey: RSAPrivateKey?, pubKey: RSAPublickKey?)
Parameters
Name | Description |
---|---|
id: Buffer | |
privKey: RSAPrivateKey? | |
pubKey: RSAPublickKey? |
PeerId.prototype.id
id: Buffer
PeerId.prototype.privKey
privKey: (RSAPrivateKey | undefined)
The private key of this id, if it exists.
+PeerId.prototype.pubKey
pubKey: (RSAPublicKey | undefined)
The private key of this id, if it exists.
+PeerId.prototype.toJSON
toJSON(): PeerIdJson
Return the jsonified version of the key, matching the formatting of go-ipfs for its config file.