geth --rpc --shh --ws
Created
November 5, 2018 07:46
-
-
Save qbig/64552299c16b2f78e13ec284fa9bd591 to your computer and use it in GitHub Desktop.
Whisper: Whisper is a simple peer-to-peer identity-based messaging system designed to be a building block in the next generation of decentralized applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"github.com/ethereum/go-ethereum/whisper/shhclient" | |
) | |
func main() { | |
client, err := shhclient.Dial("ws://127.0.0.1:8546") | |
if err != nil { | |
log.Fatal(err) | |
} | |
_ = client // we'll be using this in the next section | |
fmt.Println("we have a whisper connection") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"github.com/ethereum/go-ethereum/whisper/shhclient" | |
) | |
func main() { | |
client, err := shhclient.Dial("ws://127.0.0.1:8546") | |
if err != nil { | |
log.Fatal(err) | |
} | |
keyID, err := client.NewKeyPair(context.Background()) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(keyID) // 0ec5cfe4e215239756054992dbc2e10f011db1cdfc88b9ba6301e2f9ea1b58d2 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"github.com/ethereum/go-ethereum/common/hexutil" | |
"github.com/ethereum/go-ethereum/whisper/shhclient" | |
"github.com/ethereum/go-ethereum/whisper/whisperv6" | |
) | |
func main() { | |
client, err := shhclient.Dial("ws://127.0.0.1:8546") | |
if err != nil { | |
log.Fatal(err) | |
} | |
keyID, err := client.NewKeyPair(context.Background()) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(keyID) // 0ec5cfe4e215239756054992dbc2e10f011db1cdfc88b9ba6301e2f9ea1b58d2 | |
publicKey, err := client.PublicKey(context.Background(), keyID) | |
if err != nil { | |
log.Print(err) | |
} | |
fmt.Println(hexutil.Encode(publicKey)) // 0x04f17356fd52b0d13e5ede84f998d26276f1fc9d08d9e73dcac6ded5f3553405db38c2f257c956f32a0c1fca4c3ff6a38a2c277c1751e59a574aecae26d3bf5d1d | |
message := whisperv6.NewMessage{ | |
Payload: []byte("Hello"), | |
PublicKey: publicKey, | |
TTL: 60, | |
PowTime: 2, | |
PowTarget: 2.5, | |
} | |
messageHash, err := client.Post(context.Background(), message) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(messageHash) // 0xdbfc815d3d122a90d7fb44d1fc6a46f3d76ec752f3f3d04230fe5f1b97d2209a | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"os" | |
"runtime" | |
"github.com/ethereum/go-ethereum/common/hexutil" | |
"github.com/ethereum/go-ethereum/whisper/shhclient" | |
"github.com/ethereum/go-ethereum/whisper/whisperv6" | |
) | |
func main() { | |
client, err := shhclient.Dial("ws://127.0.0.1:8546") | |
if err != nil { | |
log.Fatal(err) | |
} | |
keyID, err := client.NewKeyPair(context.Background()) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(keyID) // 0ec5cfe4e215239756054992dbc2e10f011db1cdfc88b9ba6301e2f9ea1b58d2 | |
messages := make(chan *whisperv6.Message) | |
criteria := whisperv6.Criteria{ | |
PrivateKeyID: keyID, | |
} | |
sub, err := client.SubscribeMessages(context.Background(), criteria, messages) | |
if err != nil { | |
log.Fatal(err) | |
} | |
go func() { | |
for { | |
select { | |
case err := <-sub.Err(): | |
log.Fatal(err) | |
case message := <-messages: | |
fmt.Printf(string(message.Payload)) // "Hello" | |
os.Exit(0) | |
} | |
} | |
}() | |
publicKey, err := client.PublicKey(context.Background(), keyID) | |
if err != nil { | |
log.Print(err) | |
} | |
fmt.Println(hexutil.Encode(publicKey)) // 0x04f17356fd52b0d13e5ede84f998d26276f1fc9d08d9e73dcac6ded5f3553405db38c2f257c956f32a0c1fca4c3ff6a38a2c277c1751e59a574aecae26d3bf5d1d | |
message := whisperv6.NewMessage{ | |
Payload: []byte("Hello"), | |
PublicKey: publicKey, | |
TTL: 60, | |
PowTime: 2, | |
PowTarget: 2.5, | |
} | |
messageHash, err := client.Post(context.Background(), message) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(messageHash) // 0xdbfc815d3d122a90d7fb44d1fc6a46f3d76ec752f3f3d04230fe5f1b97d2209a | |
runtime.Goexit() // wait for goroutines to finish | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment