Keypairs for publishing to IPNS?

I am working with IPNS and I want to return to users a generated keypair from the go-libp2p library. From what I understand that library is used by the go implementation to create records? I wanted to return the keys over an API endpoint, but I don’t understand what is their datatype is and how they look.

I figure knowing this, I can return this to the front end and give to the user to store somewhere. Helpful so that for the time being I don’t have to manage their keys that will let them make changes to their IPNS records, created through our service. (The thought process is they are not running their own instance of IPFS/IPNS).

1 Like

I already tried looking through the libraries:
go-libp2p-core/crypto && ipfs/go-ipns

To try and find out how I can see the data type and console print it and just understand it.

Hello,

Just a question by curiosity and it might not help you but why don’t you use the following ?

ipfs key gen --type=rsa --size=2048 trains

Edit: I understand the users won’t run their own ipfs node but why the API node can’t

Just looked at go-libp2p-core/crypto

What you are looking for is crypto.GenerateKeyPair

It returns a PrivKey, Pubkey and error

Both PrivKey and PubKey implement Key interface and what you are looking for is the Raw method.

A main.go file as example:

package main

import (
  crypto "github.com/libp2p/go-libp2p-core/crypto"
  "os"
)

func main() {
  privateKey, publicKey, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
  privateByte, err := privateKey.Raw()
  publicByte, err := publicKey.Raw()
  os.Stdout.Write(privateByte)
  os.Stdout.Write(publicByte)
}

And you can call it with:

go run main.go

or to get base64 strings

go run main.go | base64 -w 0

You can also decode the byte to string using hex.EncodeToString but don’t forget to import encoding/hex .

2 Likes

I hadn’t thought about that actually! Im pretty novice and I don’t know how I would go from accepting a key request, to generating it in the cli, and returning it to user? Using the library I can accept the request, generate it, and return it all in one go file which is straight forward to me.

Also thank you so much for the example!

You are welcome, don’t forget to mark as solution.

For your information, the code of ipfs key gen is keyGenCmd

From there you can go to the implementation you are looking for Generate

Don’t try to reinvent the wheel :smiley:

Hope this helped

1 Like