# Keypairs for publishing to IPNS?

**URL:** <https://discuss.ipfs.tech/t/keypairs-for-publishing-to-ipns/13220>\
**Category:** Kubo\
**Tags:** go-ipfs, ipns, libp2p\
**Created:** [January 20, 2022, 8:07pm UTC](https://discuss.ipfs.tech/t/keypairs-for-publishing-to-ipns/13220 "2022-01-20T20:07:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrodriguez3313](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/mrodriguez3313/32/5759_2.png) [@mrodriguez3313](https://discuss.ipfs.tech/u/mrodriguez3313)\
**Post date:** [January 20, 2022, 8:07pm UTC](https://discuss.ipfs.tech/t/keypairs-for-publishing-to-ipns/13220/1 "2022-01-20T20:07:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mrodriguez3313](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/mrodriguez3313/32/5759_2.png) [@mrodriguez3313](https://discuss.ipfs.tech/u/mrodriguez3313)\
**Post date:** [January 20, 2022, 8:19pm UTC](https://discuss.ipfs.tech/t/keypairs-for-publishing-to-ipns/13220/2 "2022-01-20T20:19:01Z")

</div>

I already tried looking through the libraries:  
[go-libp2p-core/crypto](https://github.com/libp2p/go-libp2p-core/tree/master/crypto) && [ipfs/go-ipns](https://github.com/ipfs/go-ipns)

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

---

<div class="post-metadata">

**Author:** ![robinportigliatti](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/robinportigliatti/32/5903_2.png) [@robinportigliatti](https://discuss.ipfs.tech/u/robinportigliatti)\
**Post date:** [January 22, 2022, 7:07pm UTC](https://discuss.ipfs.tech/t/keypairs-for-publishing-to-ipns/13220/3 "2022-01-22T19:07:16Z")

</div>

Hello,

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

```auto
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

---

<div class="post-metadata">

**Author:** ![robinportigliatti](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/robinportigliatti/32/5903_2.png) [@robinportigliatti](https://discuss.ipfs.tech/u/robinportigliatti)\
**Post date:** [January 22, 2022, 8:29pm UTC](https://discuss.ipfs.tech/t/keypairs-for-publishing-to-ipns/13220/5 "2022-01-22T20:29:28Z")

</div>

Just looked at [go-libp2p-core/crypto](https://github.com/libp2p/go-libp2p-core/tree/master/crypto)

What you are looking for is [crypto.GenerateKeyPair](https://github.com/libp2p/go-libp2p-core/blob/master/crypto/key.go#L103)

It returns a [PrivKey](https://github.com/libp2p/go-libp2p-core/blob/master/crypto/key.go#L81), [Pubkey](https://github.com/libp2p/go-libp2p-core/blob/master/crypto/key.go#L92) and error

Both PrivKey and PubKey implement [Key](https://github.com/libp2p/go-libp2p-core/blob/master/crypto/key.go#L66) interface and what you are looking for is the [Raw](https://github.com/libp2p/go-libp2p-core/blob/master/crypto/key.go#L74) method.

A `main.go` file as example:

```auto
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:

```auto
go run main.go

```

or to get base64 strings

```auto
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` .

---

<div class="post-metadata">

**Author:** ![mrodriguez3313](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/mrodriguez3313/32/5759_2.png) [@mrodriguez3313](https://discuss.ipfs.tech/u/mrodriguez3313)\
**Post date:** [January 24, 2022, 6:05pm UTC](https://discuss.ipfs.tech/t/keypairs-for-publishing-to-ipns/13220/6 "2022-01-24T18:05:19Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![robinportigliatti](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/robinportigliatti/32/5903_2.png) [@robinportigliatti](https://discuss.ipfs.tech/u/robinportigliatti)\
**Post date:** [January 24, 2022, 6:40pm UTC](https://discuss.ipfs.tech/t/keypairs-for-publishing-to-ipns/13220/7 "2022-01-24T18:40:33Z")

</div>

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

For your information, the code of `ipfs key gen` is [keyGenCmd](https://github.com/ipfs/go-ipfs/blob/master/core/commands/keystore.go#L79)

From there you can go to the implementation you are looking for [Generate](https://github.com/ipfs/go-ipfs/blob/master/core/coreapi/key.go#L42)

Don’t try to reinvent the wheel 😃

Hope this helped
