Giter VIP home page Giter VIP logo

js-peer-id's Introduction

⛔️ DEPRECATED: peer-id is not used anymore in favour of libp2p-peer-id from [email protected].

peer-id

Discourse posts Dependency Status js-standard-style

IPFS Peer ID implementation in JavaScript.

Lead Maintainer

Vasco Santos

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

const id = await PeerId.create({ bits: 1024, keyType: 'RSA' })
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>

CLI

After installing peer-id, npm install peer-id, you can leverage the cli to generate keys exported as JSON. You can specify the type for the key and size, as detailed in create([opts]). The defaults are shown here.

> peer-id --type rsa --bits 2048

API

const PeerId = require('peer-id')

Create

new PeerId(id[, privKey, pubKey])

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

The key format is detailed in libp2p-crypto.

create([opts])

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

  • opts.bits: number - The size of the key. Default: 2048
  • opts.keyType: string - The key type, one of: ['RSA', 'Ed25519', 'secp256k1']. Default: RSA

Returns Promise<PeerId>.

Import

createFromHexString(str)

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

Returns PeerId.

createFromBytes(buf)

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

Returns PeerId.

createFromCID(cid)

  • cid: CID - The multihash encoded as CID object

Creates a Peer ID from a CID representation of the key's multihash (RFC 0001).

Returns PeerId.

createFromB58String(str)

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

Returns PeerId.

createFromPubKey(pubKey)

  • publicKey: Buffer

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

Returns Promise<PeerId>.

createFromPrivKey(privKey)

  • privKey: Buffer

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

Returns Promise<PeerId>.

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

Returns Promise<PeerId>.

createFromProtobuf(buf)

buf is a protocol-buffers encoded PeerId (see marshal())

parse(str)

Attempts to create a PeerId from a base58btc encoded string or a CID encoded as a string.

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>

toString()

Returns the Peer ID's id as a self-describing CIDv1 in Base32 (RFC 0001)

bafzbeigweq4zr4x4ky2dvv7nanbkw6egutvrrvzw6g3h2rftp7gidyhtt4

toB58String()

Returns the Peer ID's id as a base58 string (multihash/CIDv0).

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'

marshal(excludePrivateKey)

Returns a protocol-buffers encoded version of the id, public key and, if excludePrivateKey is not set, the private key.

marshalPubKey()

Returns a protobuf of just the public key, compatible with libp2p-crypto (unlike marshal above).

For example:

const crypto = require('libp2p-crypto')

PeerId.create({ bits: 256, keyType: 'ed25519' }).then( pid => {
  let pk = crypto.keys.unmarshalPublicKey(pid.marshalPubKey())
  // your code here
}

toPrint()

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

Example: <peer.ID xxxxxx>

equals(id)

Returns true if the given PeerId is equal to the current instance.

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

isEqual(id)

Deprecation Notice: Use equals, isEqual will be removed in 0.14.0.

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

Others

isPeerId(id)

Returns true if the given id is an instance of PeerId

  • id should be an instance of PeerId

License

MIT

js-peer-id's People

Contributors

a1300 avatar achingbrain avatar acolytec3 avatar daviddias avatar dependabot-preview[bot] avatar dignifiedquire avatar greenkeeperio-bot avatar hacdias avatar hackergrrl avatar hugomrdias avatar jacobheun avatar jaller94 avatar libp2p-mgmt-read-write[bot] avatar lidel avatar mkg20001 avatar nginnever avatar npfoss avatar npmcdn-to-unpkg-bot avatar pgte avatar prashcr avatar richardlitt avatar richardschneider avatar tobowers avatar vasco-santos avatar victorb avatar vojtechsimetka avatar web-flow avatar wemeetagain avatar wraithgar avatar ya7ya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-peer-id's Issues

Feature Request: Out-of band cryptographic signatures

A signing API would be useful when libp2p is used with non-libp2p transports or for persistent storage of a message that some other entity can verify the origin of. It could look e.g. like:

const myId = PeerId.createFromPrivKey(..)
myId.sign(nonce, message)  // Nonce for preventing replay attacks.
..
const theirId = PeerId.createFromPubKey(..)
theirId.verify(message)

// TODO: Maybe some algorithm selection?

Verifying would require the presence of a public key (not just a B58String). From a quick experiment, it seems the public key is populated on handle but not upon a successful dial, when peer id is created from a B58String. This could probably be added in some way in libp2p since the public key is available during a handshake anyway.

Would y'all be open to such a feature? Happy to put in some work if so.

PeerId param type mismatch between spec and implementation

  • Version: 0.28.2
  • Platform: Darwin Kernel Version 17.7.0: Wed May 27 17:00:02 PDT 2020; root:xnu-4570.71.80.1~1/RELEASE_X86_64 x86_64,
    nodejs v12.13.0
  • Subsystem: libp2p/peerId

Type: Bug

Severity: Low

Description:

The ts type declaration for the id param of PeerId disagrees with the implementation.

Steps to reproduce the error:

peer-id/src/index.d.ts has this (claims to accept Buffer or string for id):

declare class PeerId {
  constructor(id: Buffer | string, privKey?: PrivateKey, pubKey?: PublicKey);

but the actual implementation does this:

class PeerId {
  constructor (id, privKey, pubKey) {
    if (!Buffer.isBuffer(id)) {
      throw new Error('invalid id provided')
    }

It throws when given a string id param

So, which is it? Remove string from the declaration, or change the class to convert string to buffer?

Improve docs around creating peerId from private key and improve support

Doc Updates

The docs for createFromPrivKey currently reads:

createFromPrivKey(privKey)

  • privKey: Buffer
    Creates a Peer ID from a buffer containing a private key.

This is missleading as it requires a ProtoBuf.

We should clarify the documentation for this as users may try to use simple Buffer.

Improved Support

It would be nice if we improved the support for creating a peerid from a private key to accept a simple buffer. This could allow users to generate a keypair and use that privatekey to create their peerid. A use case of this might be an EC2 instance where the private key is stored in AWS KMS. It's possible to do this leveraging libp2p-crypto, but it would be nice if a factory method existed in peer-id to handle this for users.

Breaking change with 0.12.3 & secio

This took me a bit to track down. So effectively, the last working version for me in https://github.com/polkadot-js/client in 0.12.2, as soon as I try to upgrade to 0.12.3, I have issues.

So effectively what happens -

It gets the publicKey from the remote node and then calls to create the id string. Previously this was created via hash, now it gets created via identity. This means that the equality check here fails and we get back dialed to the wrong peer, Ids do not match

For now fixed peer-id to 0.12.2 and all is well again.

Anyway type of config option to use old `Qm...` style IDs?

I have data like this:

"peerID": "QmUWYRp3mkQUUVeyGVjcM1fC7kbVxmDieGpGsQzopXivyk",
"pubkey": "CAESILYoTcsl9HZVSyyvDmIIGXAPMtXxv0YHI1etWS1l9Nn1",

and I'd like to be able to validate that the pubkey and peerID match, but using the latest code createFromPubKey will get me a peerID of 12D3KooWN5S1ouS8cNLk5vhdKwQE3VUdxfLm89gW8NW9yS95KFtY.

Is there any type of config option where I could have it generate the old Qm style key so that I could validate old data?

implementation specs

Hello,

I am interested in writing a python implementation of peer-id, i was wondering if you have the tech specs for that.

Separate private keys from the peer id representation

PeerId's may or may not have a private key and we may need to detect this in other code.

The type of privKey is: public privKey: Uint8Array; , however currently the only way to workout if the peer id has a private key is to query peerId.privKey === undefined.

Alternatively we could document that checking whether peerId.privKey === undefined is a stable part of the API and update the types.

How to get version of peerId with an inline public key?

The Go implementation of our app is switching to using peerId's with inline public keys (i.e. instead of prefaced with qM, they'll be prefaced with 12D3Koo).

I'm not seeing any method in the library to output a peerId in that format. Is there any way to do it?

Instantiating CreateLibp2p function with Proper Assessment to PeerIds

Current Method

  1. Made a peerId using the createEd25519PeerId.
    const peerId = await createEd25519PeerId({ privateKey: privateKey });

  2. Store into json, which eliminates the formats of ed25519, converts the values into string.

  3. Retrieving the data of peerId to instantiate the CreateLibp2p.


Process

  • When I store the data as hex values, because Uint8Array doesn't store the format, only the values, I'm trying to revert the hex values into a Uint8Array to extract the same peerId using the public and private key.

Error

  • CreateLibp2p doesn't instantiate the node. Instead, I'm given these error messages: if (publicKey.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
    ^

TypeError: Cannot read properties of undefined (reading 'length')


The pasted details below is the data structure of the peer.meta that I'm using, in order to retrieve the peerId.

  • Before inputting the meta into createFromPrivKey function, I'm converting the hex values into Uint8Array

CodeBase
const peerIdForm = await createFromPrivKey(peer.meta);
"meta": {
"id": {
"code": 0,
"size": 36,
"digest": "0x080112202a43350be8249d8a381f85da2e7b0ca661ceb2b3d727ad967cd4903ac3fa5341",
"bytes": "0x0024080112202a43350be8249d8a381f85da2e7b0ca661ceb2b3d727ad967cd4903ac3fa5341"
},
"key": "0x080112401330d9b33b2f6de93088ab2424a36394fa8dc67c706b2f1cac5df80eb66c2a6b2a43350be8249d8a381f85da2e7b0ca661ceb2b3d727ad967cd4903ac3fa5341",
"public": "0x080112202a43350be8249d8a381f85da2e7b0ca661ceb2b3d727ad967cd4903ac3fa5341",
"CID": {
"/": "bafzaajaiaejcaksdguf6qje5ri4b7bo2fz5qzjtbz2zlhvzhvwlhzveqhlb7uu2b"
},
"type": "Ed25519"
},

If I'm missing any details that could help figure out this issue, please let me know. Thank you.

TextDecoder support for node 10

/home/circleci/project/node_modules/peer-id/node_modules/multihashes/node_modules/multibase/src/util.js:3
const textDecoder = new TextDecoder()
                    ^
ReferenceError: TextDecoder is not defined
    at Object.<anonymous> (/home/circleci/project/node_modules/peer-id/node_modules/multihashes/node_modules/multibase/src/util.js:3:21)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Module.replacementCompile (/home/circleci/project/node_modules/append-transform/index.js:58:13)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.<anonymous> (/home/circleci/project/node_modules/append-transform/index.js:62:4)
    at Module.load (internal/modules/cjs/loader.js:653:32)

Error when using on Node v10

peer-id questions

Heya! I had some questions about peer id:

  1. Is there a spec for PeerId?
  2. What is the mandatory API for a PeerId to expose?
  3. Since js-peer-id implements a IPFS PeerID, shouldn't this repo be js-ipfs-peer-id?
  4. What steps would I take to create a PeerId that wrapped some other identity system (e.g. ssb-keys)

Import metamask web3j keys to Libp2p

Hello,

How can I create a libp2p key from the metamask ethereum's public key
I tried:

const web3 = window.web3
const accounts = await web3.eth.getAccounts()
this.setState({ account: accounts[0] })
console.log(accounts)

let temp = await PeerId.createFromPubKey(accounts[0])

I am getting error in the last line.

Help please.

npm package peer-id 0.12.3 incompatible with other 0.12.x

I just tried to update the ipfs npm package, and it bumped my version of peer-id from 0.12.2 to 0.12.3 (because many things in the ipfs tree rely on ~0.12.x or ^0.12.x), but then I started getting this error upon building with browserify:

Cannot find module 'babelify' from '/path/to/my/app/node_modules/ipfs/node_modules/peer-id/node_modules/async'

Could you please remove version 0.12.3 or replace it with something compatible with all the other packages asking for ~0.12.0?

Update:

Steps to reproduce the error:

package.json

{
  "scripts": {
    "build": "browserify index.js -o app.js -d"
  },
  "dependencies": {
    "browserify": "^16.3.0",
    "ipfs": "^0.36.4"
  }
}

index.js

require('ipfs');

run

npm install
npm run build

Also available to be cloned here: https://github.com/npfoss/js-ipfs-bug-code

Improve isValid

  • check that privKey leads to pubKey
  • check that pubKey leads to id

Could you help remove the high severity vulnerability introduced by package node-forge?

Hi, @vasco-santos @jacobheun, there is a high severity vulnerability introduced in your package peer-id:

Issue Description

A vulnerability CVE-2020-7720 detected in package node-forge<0.10.0 is transitively referenced by [email protected]. We noticed that such a vulnerability has been removed since [email protected].

However, peer-id's popular previous version [email protected] (5,680 downloads per week) is still transitively referenced by a large amount of latest versions of active and popular downstream projects (about 225 downstream projects, e.g., @aragon/cli 7.1.6, snet-sdk-web 2.0.0-beta.0, @gny/cli 1.0.90, @augurproject/tools 2.1.13, @charged-particles/protocol-subgraph 1.2.7, @51nodes/[email protected], etc.).
As such, issue CVE-2020-7720 can be propagated into these downstream projects and expose security threats to them.

These projects cannot easily upgrade peer-id from version 0.12.5 to (>=0.14.3). For instance, [email protected] is introduced into the above projects via the following package dependency paths:
(1)@51nodes/[email protected] ➔ @evan.network/[email protected] ➔ @evan.network/[email protected][email protected][email protected][email protected][email protected]
......

The projects such as ipfs-api, which introduced [email protected], are not maintained anymore. These unmaintained packages can neither upgrade peer-id nor be easily migrated by the large amount of affected downstream projects.
On behalf the downstream users, could you help us remove the vulnerability from package [email protected]?

Suggested Solution

Since these inactive projects set a version constaint 0.12.* for peer-id on the above vulnerable dependency paths, if peer-id removes the vulnerability from 0.12.5 and releases a new patched version [email protected], such a vulnerability patch can be automatically propagated into the 225 affected downstream projects.

In [email protected], you can kindly try to perform the following upgrade:
libp2p-crypto ~0.16.1 ➔ ~0.19.0;
Note:
[email protected](>=0.19.0) directly depends on [email protected] (a vulnerability CVE-2020-7720 patched version)

Thank you for your help.

Best regards,
Paimon

Calling toString on a peer id breaks deep comparison

const PeerId = require('peer-id')

const peerId1 = PeerId.createFromB58String('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
const peerId2 = PeerId.createFromB58String('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')

assert.deepEqual(peerId1, peerId2)
// 'bafzbeifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4'

peerId1.toString()

assert.deepEqual(peerId1, peerId2)
// Uncaught AssertionError [ERR_ASSERTION]: Expected values to be loosely deep-equal:

This is because .toString() has the side effect of setting a ._idCIDString property on this which the other peer ID doesn't have.

Is there some reason Protobuf is being used?

It seems like this library is jumping through a lot of hoops to get and set keys instead of just using raw/jwk/pem/pkcs8/spki.

Is there a technical reason for this, or is it just technical debt from being created during a time where the crypto containers were actively evolving?

peer-id CLI does not exist

Documentation states:

> peer-id --type rsa --bits 2048

However, its not available globally nor is it in the local installation:

$ npm install -g peer-id
$ npm install peer-id
$ peer-id --type rsa --bits 2048
bash: peer-id: command not found
$ ls node_modules/.bin/peer-id
ls: node_modules/.bin/peer-id: No such file or directory
$ npx peer-id --type rsa --bits 2048
npm ERR! could not determine executable to run

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/mgwidmann/.npm/_logs/2022-01-13T21_13_44_189Z-debug.log

This is because there is no peer-id executable in the project.

$ tree node_modules/peer-id/ -a
node_modules/peer-id/
├── LICENSE
├── README.md
├── dist
│   └── index.min.js
├── node_modules
│   └── .bin
│       ├── pbjs -> ../../../protobufjs/bin/pbjs
│       └── pbts -> ../../../protobufjs/bin/pbts
├── package.json
└── src
    ├── index.d.ts
    ├── index.js
    ├── proto.d.ts
    ├── proto.js
    └── proto.proto

4 directories, 11 files

Where is the CLI?

Missing ID validation

Shouldn't there be some more validation of the ID passed to PeerId?

Currently it's checked to ensure it's a buffer...shouldn't it be checked to ensure it's a valid CID?

Also applies to the commonly used factory functions like createFromB58String, which will ensure the string is base 58 encoded but nothing else.

dist version

@nginnever noticed that we the move to WebPack, we lost our minified dist version (the one that was present was from and earlier version). This is not urgent, but would you mind adding a script (npm script or gulp script) to make it available again?

Thank you!

PeerId.createXXXX not interoperable with Go implementation when public key is embedded inline with peer id

The Go implementation embeds public key in the peer id when key length is <= 42 bytes. This is the case, for example, with secp256k1 keys.

This results in an error in the browser where ids generated from the same public key with Go implementation do not match ones generated by the js implementation.

Error: dialed to the wrong peer, Ids do not match
    at PeerId.createFromPubKey (crypto.js:80)
    at pubKey.hash (index.js:188)
    at Multihashing.Multihashing.digest (index.js:33)
    at index.js:15
    at run (setImmediate.js:40)
    at runIfPresent (setImmediate.js:69)
    at onGlobalMessage (setImmediate.js:109)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
    at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:496)

Go implementation:

// IDFromPublicKey returns the Peer ID corresponding to pk
func IDFromPublicKey(pk ic.PubKey) (ID, error) {
	b, err := pk.Bytes()
	if err != nil {
		return "", err
	}
	var alg uint64 = mh.SHA2_256
	if len(b) <= MaxInlineKeyLength {  // <<<< Hash algorithem is set to ID when key is 42 bytes or less
		alg = mh.ID
	}
	hash, _ := mh.Sum(b, alg, -1)
	return ID(hash), nil
}

I have made changes to my local version to correct this but as a prerequisite I had to make changes to multihash in order to support 'id' function code. Please see: multiformats/js-multihash#60

Below is a patch of changes I made against master branch:

Secp256k1 test id json to be placed in test/fixtures/go-secp256k1-id.js

'use strict'

module.exports = {
  id: '16Uiu2HAmAJv2Y6DjTBdqvFak6NX2iNTTXHyMkvd1p5XxU7DH1GS9',
  privKey: 'CAISIBXKcM3bhjJ2cQJyWEBiWNq7apuYW8oh/+UivQcINOz9',
  pubKey: 'CAISIQLdJB8exFCd65lMn7xAoC9evPwVgf0hSrNRMWuhP9ALTg=='
}

A patch of changes to support ids with inline public key

diff --git a/src/index.js b/src/index.js
index 5197c6b..ba130fc 100644
--- a/src/index.js
+++ b/src/index.js
@@ -134,6 +134,7 @@ class PeerId {
 }
 
 const PeerIdWithIs = withIs(PeerId, { className: 'PeerId', symbolName: '@libp2p/js-peer-id/PeerId' })
+const MaxInlineKeyLength = 42
 
 exports = module.exports = PeerIdWithIs
 
@@ -179,9 +180,9 @@ exports.createFromPubKey = function (key, callback) {
   }
 
   let pubKey
-
+  let buf = key
+  
   try {
-    let buf = key
     if (typeof buf === 'string') {
       buf = Buffer.from(key, 'base64')
     }
@@ -193,13 +194,18 @@ exports.createFromPubKey = function (key, callback) {
     return callback(err)
   }
 
-  pubKey.hash((err, digest) => {
-    if (err) {
-      return callback(err)
-    }
+  if (buf.length > MaxInlineKeyLength) {
+    pubKey.hash((err, digest) => {
+      if (err) {
+        return callback(err)
+      }
 
+      callback(null, new PeerIdWithIs(digest, null, pubKey))
+    })
+  } else {
+    let digest = mh.encode(buf, 'id', buf.length)
     callback(null, new PeerIdWithIs(digest, null, pubKey))
-  })
+  }
 }
 
 // Private key input will be a string
@@ -222,9 +228,18 @@ exports.createFromPrivKey = function (key, callback) {
 
   waterfall([
     (cb) => crypto.keys.unmarshalPrivateKey(buf, cb),
-    (privKey, cb) => privKey.public.hash((err, digest) => {
-      cb(err, digest, privKey)
-    })
+    (privKey, c1, c2) => {
+      let cb = c1 ? c1 : c2 // unmarshalSecp256k1PrivateKey sets an extra null param on the callback 
+      let bytes = privKey.public.bytes
+      if (bytes.length > MaxInlineKeyLength) {
+        privKey.public.hash((err, digest) => {
+          cb(err, digest, privKey)
+        })
+      } else {
+        let digest = mh.encode(bytes, 'id', bytes.length)
+        cb(null, digest, privKey)
+      }
+    }
   ], (err, digest, privKey) => {
     if (err) {
       return callback(err)
@@ -256,14 +271,29 @@ exports.createFromJSON = function (obj, callback) {
   if (rawPrivKey) {
     waterfall([
       (cb) => crypto.keys.unmarshalPrivateKey(rawPrivKey, cb),
-      (priv, cb) => priv.public.hash((err, digest) => {
-        cb(err, digest, priv)
-      }),
+      (privKey, c1, c2) => {
+        let cb = c1 ? c1 : c2 // unmarshalSecp256k1PrivateKey sets an extra null param on the callback 
+        let bytes = privKey.public.bytes
+        if (bytes.length > MaxInlineKeyLength) {
+          privKey.public.hash((err, digest) => {
+            cb(err, digest, privKey)
+          })
+        } else {
+          let digest = mh.encode(bytes, 'id', bytes.length)
+          cb(null, digest, privKey)
+        }
+      },
       (privDigest, priv, cb) => {
         if (pub) {
-          pub.hash((err, pubDigest) => {
-            cb(err, privDigest, priv, pubDigest)
-          })
+          let bytes = pub.bytes
+          if (bytes.length > MaxInlineKeyLength) {
+            pub.hash((err, pubDigest) => {
+              cb(err, privDigest, priv, pubDigest)
+            })
+          } else {
+            let pubDigest = mh.encode(bytes, 'id', bytes.length)
+            cb(null, privDigest, priv, pubDigest)
+          }
         } else {
           cb(null, privDigest, priv)
         }
diff --git a/test/peer-id.spec.js b/test/peer-id.spec.js
index 5d04c92..c9e14c5 100644
--- a/test/peer-id.spec.js
+++ b/test/peer-id.spec.js
@@ -21,6 +21,8 @@ const testIdB58String = mh.toB58String(testIdBytes)
 
 const goId = require('./fixtures/go-private-key')
 
+const goSecp256k1Id = require('./fixtures/go-secp256k1-id')
+
 // Test options for making PeerId.create faster
 // INSECURE, only use when testing
 const testOpts = {
@@ -208,6 +210,30 @@ describe('PeerId', () => {
         })
       })
     })
+
+    it('go interop secp2561k pubKey', (done) => {
+      PeerId.createFromPubKey(goSecp256k1Id.pubKey, (err, id) => {
+        expect(err).to.not.exist()
+        expect(id.toB58String()).to.eql(goSecp256k1Id.id)
+        done()
+      })
+    })
+
+    it('go interop secp2561k privKey', (done) => {
+      PeerId.createFromPrivKey(goSecp256k1Id.privKey, (err, id) => {
+        expect(err).to.not.exist()
+        expect(id.toB58String()).to.eql(goSecp256k1Id.id)
+        done()
+      })
+    })
+
+    it('go interop secp2561k json', (done) => {
+      PeerId.createFromJSON(goSecp256k1Id, (err, id) => {
+        expect(err).to.not.exist()
+        expect(id.toB58String()).to.eql(goSecp256k1Id.id)
+        done()
+      })
+    })
   })
 
   it('set privKey (valid)', (done) => {

fix: CID string incorrectly parsed to PeerId while go-libp2p-core refuses it

Feeding bafykbzacecesdmus2x37xyvtxdwlpx3vu5cjecxed473f66fezh6tdoccqhqq to PeerId.parse(str) generate a valid PeerId object, but the same string will generate an error after unmarshalling to CID in github.com/libp2p/go-libp2p-core/peer.

I created the a small repo to reproduce the error, you can try here: https://github.com/nicobao/bug-libp2p-peerid

Please take a look :).

It outputs:

=== JS VERSION ===

> [email protected] start /home/nicolas/nicobao/bug-libp2p-peerid/js
> node with-js-lib.js

Parsing bafykbzacecesdmus2x37xyvtxdwlpx3vu5cjecxed473f66fezh6tdoccqhqq...
peerId is valid! PeerId {
  _id: Uint8Array(36) [
    160, 228,   2,  32, 137,  33, 178, 146,
    213, 247, 251, 226, 179, 184, 236, 183,
    223, 117, 167,  68, 146,  10, 228,  31,
     63, 178, 251, 197,  38,  79, 233, 141,
    194,  20,  15,   8
  ],
  _idB58String: '2DrjgbDVCNmAACH2oLHRPSg874iiE13cTnHK9dvVhXJqN2UQXR',
  _privKey: undefined,
  _pubKey: undefined
}

=== GO VERSION ===
Parsing bafykbzacecesdmus2x37xyvtxdwlpx3vu5cjecxed473f66fezh6tdoccqhqq ...
Error can't convert CID of type "dag-pb" to a peer ID

I tried using https://github.com/libp2p/js-libp2p-peer-id/tree/master/packages/libp2p-peer-id but it doesn't contain enough validation function it seems.

I want to avoid importing the whole js-libp2p package just for validating peerId.

In this library (js-peer-id) still maintained? What is the idiomatic way to validate peerId in frontend?

Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.