Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Add usage example to README #9

Closed
Mr0grog opened this issue Jun 18, 2018 · 4 comments
Closed

Add usage example to README #9

Mr0grog opened this issue Jun 18, 2018 · 4 comments

Comments

@Mr0grog
Copy link
Contributor

Mr0grog commented Jun 18, 2018

I think it would be really helpful to add one or two usage examples to the README, e.g:

import (
	"time"

	util "github.com/ipfs/go-ipfs-util"
	ipns "github.com/ipfs/go-ipns"
	crypto "github.com/libp2p/go-libp2p-crypto"
)

// Is there a more generic way to create the needed private key than requiring go-ipfs-util/go-libp2p-crypto?
seed := util.NewTimeSeededRand()
privateKey, publicKey, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 1024, sr)

// Create an IPNS record that expires in one hour and points to the IPFS address
// /ipfs/Qme1knMqwt1hKZbc1BmQFmnm9f36nyQGwXxPGVpVJ9rMK5
ipnsEntry, err := ipns.Create(privateKey, []byte("/ipfs/Qme1knMqwt1hKZbc1BmQFmnm9f36nyQGwXxPGVpVJ9rMK5"), 0, time.Now().Add(1*time.Hour))
if err != nil {
	panic(err)
}
@Mr0grog
Copy link
Contributor Author

Mr0grog commented Jun 18, 2018

Happy to propose a PR with the above if that’s good, but it would be nice if we could do the key generation without relying on two other packages.

@Mr0grog Mr0grog mentioned this issue Jun 18, 2018
@Stebalien
Copy link
Member

Well, we don't really need go-ipfs-util (can use crypto/rand.Reader). However, key generation is a separate concern so will probably require the additional import.

@Mr0grog
Copy link
Contributor Author

Mr0grog commented Jun 18, 2018

we don't really need go-ipfs-util (can use crypto/rand.Reader)

Oh, I just looked and realized libp2p-crypto already has GenerateKeyPair(), which does exactly that :P
Are there any concerns with that, or is this perfectly fine?

privateKey, publicKey, err := crypto.GenerateKeyPair(crypto.RSA, 1024)

However, key generation is a separate concern so will probably require the additional import.

Oh, d'oh, I guess it has to be a Libp2p PrivKey, so you can’t really get away from that requirement. Boo to me for not looking that up first. 👍

@Stebalien
Copy link
Member

Oh, I just looked and realized libp2p-crypto already has GenerateKeyPair(), which does exactly that :P
Are there any concerns with that, or is this perfectly fine?

That should work. We often specify a "test" source of randomness to make the tests faster but we really shouldn't be doing that in examples.

Also, we should probably use a 2048 bit key (instead of 1024 bits). Every time we use 1024, we should explicitly say "not safe for production use".

Mr0grog added a commit to Mr0grog/go-ipns that referenced this issue Jun 18, 2018
We could still use some more examples of other operations, but this should be a good start.

License: MIT
Signed-off-by: Rob Brackett <rob@robbrackett.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants