description: Tutorial on how to generate whisper key pairs with Go.

Generating Whisper Key Pair

In whisper, messages have to be encrypted with either a symmetric or an asymmetric key to prevent them from being read by anyone other than the intended recipient.

After you’ve connected to the whisper client you’ll need to call the client’s NewKeyPair method to generate a new public and private pair that the node will manage. The result of this function will be a unique ID that references the key pair which we’ll be using for encrypting and decrypting the message in the next few sections.

  1. keyID, err := client.NewKeyPair(context.Background())
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. fmt.Println(keyID) // 0ec5cfe4e215239756054992dbc2e10f011db1cdfc88b9ba6301e2f9ea1b58d2

Let’s learn how to send an encrypted message in the next section.

Full code

Commands

  1. geth --rpc --shh --ws

whisper_keypair.go

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "github.com/ethereum/go-ethereum/whisper/shhclient"
  7. )
  8. func main() {
  9. client, err := shhclient.Dial("ws://127.0.0.1:8546")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. keyID, err := client.NewKeyPair(context.Background())
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. fmt.Println(keyID) // 0ec5cfe4e215239756054992dbc2e10f011db1cdfc88b9ba6301e2f9ea1b58d2
  18. }