libp2p / js-peer-id

peer-id implementation in JavaScript. Deprecated; use https://github.com/libp2p/js-libp2p-peer-id instead.
https://github.com/libp2p/js-libp2p-peer-id
MIT License
81 stars 44 forks source link

PeerId param type mismatch between spec and implementation #125

Closed dthorpe closed 4 years ago

dthorpe commented 4 years ago

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?

vasco-santos commented 4 years ago

Thanks for raising this. The correct is only accepting a Buffer, as detailed in libp2p/js-peer-id#new-peeridid-privkey-pubkey.

Would you like to open a PR in peer-id to fix the typescript declaration file? It is important pointing out that our work to better support typescript users is still in its beginning. We will move away from the declaration files, and leverage JS docs to achieve them in a cleaner way while also guarantee that they are updated with the code. More Info

vasco-santos commented 4 years ago

@jacobheun can you transfer this issue to the peer-id repo. I don't have permissions for it