description: Tutorial on how to send a message on whisper with Go.

Sending Messages on Whisper

Before we’re able to create a message, we must first have a public key to encrypt the message. In the previous section we learned how to generate a public and private key pair using the NewKeyPair function which returned a key ID that references this key pair. We now have to call the PublicKey function to read the key pair’s public key in bytes format which we’ll be using to encrypt the message.

  1. publicKey, err := client.PublicKey(context.Background(), keyID)
  2. if err != nil {
  3. log.Print(err)
  4. }
  5. fmt.Println(hexutil.Encode(publicKey)) // 0x04f17356fd52b0d13e5ede84f998d26276f1fc9d08d9e73dcac6ded5f3553405db38c2f257c956f32a0c1fca4c3ff6a38a2c277c1751e59a574aecae26d3bf5d1d

Now we’ll construct our whisper message by initializing the NewMessage struct from the go-ethereum whisper/whisperv6 package, which requires the following properties:

  • Payload as the message content in bytes format
  • PublicKey as the key we’ll use for encryption
  • TTL as the time-to-live in seconds for the message
  • PowTime as maximal time in seconds to be spent on proof of work.
  • PowTarget as the minimal PoW target required for this message.
  1. message := whisperv6.NewMessage{
  2. Payload: []byte("Hello"),
  3. PublicKey: publicKey,
  4. TTL: 60,
  5. PowTime: 2,
  6. PowTarget: 2.5,
  7. }

We can now broadcast to the network by invoking the client’s Post function giving it the message, will it’ll return a hash of the message.

  1. messageHash, err := client.Post(context.Background(), message)
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. fmt.Println(messageHash) // 0xdbfc815d3d122a90d7fb44d1fc6a46f3d76ec752f3f3d04230fe5f1b97d2209a

In the next section we’ll see how we can create a message subscription to be able to receive the messages in real time.

Full code

Commands

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

whisper_send.go

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "github.com/ethereum/go-ethereum/common/hexutil"
  7. "github.com/ethereum/go-ethereum/whisper/shhclient"
  8. "github.com/ethereum/go-ethereum/whisper/whisperv6"
  9. )
  10. func main() {
  11. client, err := shhclient.Dial("ws://127.0.0.1:8546")
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. keyID, err := client.NewKeyPair(context.Background())
  16. if err != nil {
  17. log.Fatal(err)
  18. }
  19. fmt.Println(keyID) // 0ec5cfe4e215239756054992dbc2e10f011db1cdfc88b9ba6301e2f9ea1b58d2
  20. publicKey, err := client.PublicKey(context.Background(), keyID)
  21. if err != nil {
  22. log.Print(err)
  23. }
  24. fmt.Println(hexutil.Encode(publicKey)) // 0x04f17356fd52b0d13e5ede84f998d26276f1fc9d08d9e73dcac6ded5f3553405db38c2f257c956f32a0c1fca4c3ff6a38a2c277c1751e59a574aecae26d3bf5d1d
  25. message := whisperv6.NewMessage{
  26. Payload: []byte("Hello"),
  27. PublicKey: publicKey,
  28. TTL: 60,
  29. PowTime: 2,
  30. PowTarget: 2.5,
  31. }
  32. messageHash, err := client.Post(context.Background(), message)
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. fmt.Println(messageHash) // 0xdbfc815d3d122a90d7fb44d1fc6a46f3d76ec752f3f3d04230fe5f1b97d2209a
  37. }