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
80 stars 44 forks source link

Improve docs around creating peerId from private key and improve support #82

Open jacobheun opened 6 years ago

jacobheun commented 6 years ago

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.

daviddias commented 6 years ago

Treat documentation mistakes as bugs. That said, proposal sounds good :)

iamsaquib8 commented 5 years ago

And Please can you provide examples to start with in the docs, its tough to find out the starting point for many of the methods defined in the library. Thanks

vogdb commented 2 years ago

Hi! Is this still actual?

cc @vasco-santos

wemeetagain commented 2 years ago

Yes, createFromPrivKey still expects a protobuf

eg:

import { keys } from "libp2p-crypto"

const privateKey = await keys.generateKeypair('secp256k1')

// this doesn't work
const peerId = await createFromPrivKey(privateKey.marshal())

// this works
const peerId = await createFromPrivKey(privateKey.bytes)
vogdb commented 2 years ago

@wemeetagain can you please elaborate on your example? I see that everything works in the tests https://github.com/libp2p/js-peer-id/blob/master/test/peer-id.spec.js#L214. From what I see the API has changed:

const privKey = await crypto.keys.generateKeyPair('secp256k1', 256)
const id = await PeerId.createFromPrivKey(privKey.bytes)
wemeetagain commented 2 years ago

Ah, sorry, there are a bunch of bugs in my example. (I just updated it)

privKey.bytes returns a protobuf-wrapped private key. https://github.com/libp2p/js-libp2p-crypto/blob/master/src/keys/secp256k1-class.ts#L66 whereas privKey.marshal() returns the raw private key https://github.com/libp2p/js-libp2p-crypto/blob/master/src/keys/secp256k1-class.ts#L62

vogdb commented 2 years ago

@wemeetagain As a beginner I can create a PR that updates documentation. Please look at the branch of my fork https://github.com/libp2p/js-peer-id/compare/master...vogdb:readme-privkey?expand=1. If it is ok then I will create it.

If you want to change API to accept createFromPrivKey(privateKey.marshal()) then it would be better to discuss such change with the project's maintainers first.