main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // +build none
  17. // Contains a simple whisper peer setup and self messaging to allow playing
  18. // around with the protocol and API without a fancy client implementation.
  19. package main
  20. import (
  21. "fmt"
  22. "log"
  23. "os"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/ethereum/go-ethereum/logger"
  28. "github.com/ethereum/go-ethereum/p2p"
  29. "github.com/ethereum/go-ethereum/p2p/nat"
  30. "github.com/ethereum/go-ethereum/whisper"
  31. )
  32. func main() {
  33. logger.AddLogSystem(logger.NewStdLogSystem(os.Stdout, log.LstdFlags, logger.InfoLevel))
  34. // Generate the peer identity
  35. key, err := crypto.GenerateKey()
  36. if err != nil {
  37. fmt.Printf("Failed to generate peer key: %v.\n", err)
  38. os.Exit(-1)
  39. }
  40. name := common.MakeName("whisper-go", "1.0")
  41. shh := whisper.New()
  42. // Create an Ethereum peer to communicate through
  43. server := p2p.Server{
  44. PrivateKey: key,
  45. MaxPeers: 10,
  46. Name: name,
  47. Protocols: []p2p.Protocol{shh.Protocol()},
  48. ListenAddr: ":30300",
  49. NAT: nat.Any(),
  50. }
  51. fmt.Println("Starting Ethereum peer...")
  52. if err := server.Start(); err != nil {
  53. fmt.Printf("Failed to start Ethereum peer: %v.\n", err)
  54. os.Exit(1)
  55. }
  56. // Send a message to self to check that something works
  57. payload := fmt.Sprintf("Hello world, this is %v. In case you're wondering, the time is %v", name, time.Now())
  58. if err := selfSend(shh, []byte(payload)); err != nil {
  59. fmt.Printf("Failed to self message: %v.\n", err)
  60. os.Exit(-1)
  61. }
  62. }
  63. // SendSelf wraps a payload into a Whisper envelope and forwards it to itself.
  64. func selfSend(shh *whisper.Whisper, payload []byte) error {
  65. ok := make(chan struct{})
  66. // Start watching for self messages, output any arrivals
  67. id := shh.NewIdentity()
  68. shh.Watch(whisper.Filter{
  69. To: &id.PublicKey,
  70. Fn: func(msg *whisper.Message) {
  71. fmt.Printf("Message received: %s, signed with 0x%x.\n", string(msg.Payload), msg.Signature)
  72. close(ok)
  73. },
  74. })
  75. // Wrap the payload and encrypt it
  76. msg := whisper.NewMessage(payload)
  77. envelope, err := msg.Wrap(whisper.DefaultPoW, whisper.Options{
  78. From: id,
  79. To: &id.PublicKey,
  80. TTL: whisper.DefaultTTL,
  81. })
  82. if err != nil {
  83. return fmt.Errorf("failed to seal message: %v", err)
  84. }
  85. // Dump the message into the system and wait for it to pop back out
  86. if err := shh.Send(envelope); err != nil {
  87. return fmt.Errorf("failed to send self-message: %v", err)
  88. }
  89. select {
  90. case <-ok:
  91. case <-time.After(time.Second):
  92. return fmt.Errorf("failed to receive message in time")
  93. }
  94. return nil
  95. }