1

In geth --shh --rpc console, for example I type

f = shh.newMessageFilter({privateKeyId: id}, function(error, messages){console.log(JSON.stringify(messages))})  

to receive a posted message by callback, and this works fine.

I am trying by golang to receive a posted message but this not work

example:

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/ethereum/go-ethereum/common/hexutil"
    shh "github.com/ethereum/go-ethereum/whisper/shhclient"
    whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
)

func main() {
    ctx := context.TODO()

    c, err := shh.Dial("http://127.0.0.1:8545")
    if err != nil {
        log.Fatalln(err)
    }

    id, _ := c.NewKeyPair(ctx)
    fmt.Println("id:", id)

    fID, err := c.NewMessageFilter(ctx, whisper.Criteria{PrivateKeyID: id})
    if err != nil {
        log.Panicln(err)
    }
    fmt.Println("FilterId:", fID)

    puKey, err := c.PublicKey(ctx, id)
    if err != nil {
        log.Println(err)
    }
    fmt.Println("PublicKey:", hexutil.Encode(puKey))

    blk, err := c.Post(ctx, whisper.NewMessage{
        TTL:       60,
        PowTime:   2,
        PowTarget: 2.5,
        Payload:   []byte("Hello"),
        PublicKey: puKey})
    if err != nil {
        log.Println(err)
    }
    fmt.Println("Blk:", blk)

    m, err := c.FilterMessages(ctx, fID)
    if err != nil {
        log.Panicln(err)
    }
    fmt.Println(m)

}

How I can to fix this issue?

1

Try restarting the geth client. I tried that following and it worked successfully.

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/ethereum/go-ethereum/common/hexutil"
    shh "github.com/ethereum/go-ethereum/whisper/shhclient"
    whisper6 "github.com/ethereum/go-ethereum/whisper/whisperv6"
)

func main() {
    client, err := shh.Dial("http://127.0.0.1:8545")
    if err != nil {
        log.Fatal(err)
    }

    privateKeyID, err := client.NewKeyPair(context.Background())
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("private key id: %s\n", privateKeyID)

    filterID, err := client.NewMessageFilter(context.Background(), whisper6.Criteria{PrivateKeyID: privateKeyID})
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("filter id: %s\n", filterID)

    publicKey, err := client.PublicKey(context.Background(), privateKeyID)
    if err != nil {
        log.Print(err)
    }
    fmt.Printf("public key: %s\n", hexutil.Encode(publicKey))

    messageHash, err := client.Post(context.Background(), whisper6.NewMessage{
        TTL:       60,
        PowTime:   2,
        PowTarget: 2.5,
        Payload:   []byte("Hello"),
        PublicKey: publicKey,
    })
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("message hash: %s\n", messageHash)

    messages, err := client.FilterMessages(context.Background(), filterID)
    if err != nil {
        log.Fatal(err)
    }
    for _, message := range messages {
        fmt.Printf("message: %s", string(message.Payload))
    }
}

Output

private key id: 321025e4cc85b6d111f7a2128dd16508f9d26b50294e2bd8e89100d289987d4c
filter id: 2fd816b47ad2af291d75ce72fb19d81e33e469c29961abfd7e3c019603dd1800
public key: 0x04f5fd9240bebcd6e41d41712a51ba4775dd721963dcc92c15bddaef3da9125b298d376d884ad140da70ff456028bf2499b714042c726a64e577ee334c68956d6b
message hash: 0xb72fe6d945f2fb2a349f217d8bbccbc255014d0b0b015dedbfe3c6d4119d30f5
message: Hello

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.