diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-04-10 18:42:49 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-04-10 18:42:49 +0800 |
commit | a4c8e947b0f4a07e851f7c8b420874ac411fdb69 (patch) | |
tree | 2ff2869f4d178166b9b0db5e843a536965acdeda /whisper/main.go | |
parent | fc1d1f9afd155fab1f614c6a0340233f90afafd6 (diff) | |
download | dexon-a4c8e947b0f4a07e851f7c8b420874ac411fdb69.tar.gz dexon-a4c8e947b0f4a07e851f7c8b420874ac411fdb69.tar.zst dexon-a4c8e947b0f4a07e851f7c8b420874ac411fdb69.zip |
whisper: make the test app runnable & do something inside
Diffstat (limited to 'whisper/main.go')
-rw-r--r-- | whisper/main.go | 77 |
1 files changed, 65 insertions, 12 deletions
diff --git a/whisper/main.go b/whisper/main.go index 9f35dbb8d..643644ee6 100644 --- a/whisper/main.go +++ b/whisper/main.go @@ -1,37 +1,90 @@ // +build none +// Contains a simple whisper peer setup and self messaging to allow playing +// around with the protocol and API without a fancy client implementation. + package main import ( "fmt" "log" "os" + "time" - "github.com/ethereum/go-ethereum/crypto/secp256k1" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/p2p/nat" "github.com/ethereum/go-ethereum/whisper" ) func main() { logger.AddLogSystem(logger.NewStdLogSystem(os.Stdout, log.LstdFlags, logger.InfoLevel)) - pub, _ := secp256k1.GenerateKeyPair() - - whisper := whisper.New() + // Generate the peer identity + key, err := crypto.GenerateKey() + if err != nil { + fmt.Printf("Failed to generate peer key: %v.\n", err) + os.Exit(-1) + } + name := common.MakeName("whisper-go", "1.0") + shh := whisper.New() - srv := p2p.Server{ + // Create an Ethereum peer to communicate through + server := p2p.Server{ + PrivateKey: key, MaxPeers: 10, - Identity: p2p.NewSimpleClientIdentity("whisper-go", "1.0", "", string(pub)), + Name: name, + Protocols: []p2p.Protocol{shh.Protocol()}, ListenAddr: ":30300", - NAT: p2p.UPNP(), - - Protocols: []p2p.Protocol{whisper.Protocol()}, + NAT: nat.Any(), } - if err := srv.Start(); err != nil { - fmt.Println("could not start server:", err) + fmt.Println("Starting Ethereum peer...") + if err := server.Start(); err != nil { + fmt.Printf("Failed to start Ethereum peer: %v.\n", err) os.Exit(1) } - select {} + // Send a message to self to check that something works + payload := fmt.Sprintf("Hello world, this is %v. In case you're wondering, the time is %v", name, time.Now()) + if err := selfSend(shh, []byte(payload)); err != nil { + fmt.Printf("Failed to self message: %v.\n", err) + os.Exit(-1) + } +} + +// SendSelf wraps a payload into a Whisper envelope and forwards it to itself. +func selfSend(shh *whisper.Whisper, payload []byte) error { + ok := make(chan struct{}) + + // Start watching for self messages, output any arrivals + id := shh.NewIdentity() + shh.Watch(whisper.Filter{ + To: &id.PublicKey, + Fn: func(msg *whisper.Message) { + fmt.Printf("Message received: %s, signed with 0x%x.\n", string(msg.Payload), msg.Signature) + close(ok) + }, + }) + // Wrap the payload and encrypt it + msg := whisper.NewMessage(payload) + envelope, err := msg.Seal(whisper.DefaultPow, whisper.Opts{ + Ttl: whisper.DefaultTtl, + From: id, + To: &id.PublicKey, + }) + if err != nil { + return fmt.Errorf("failed to seal message: %v", err) + } + // Dump the message into the system and wait for it to pop back out + if err := shh.Send(envelope); err != nil { + return fmt.Errorf("failed to send self-message: %v", err) + } + select { + case <-ok: + case <-time.After(time.Second): + return fmt.Errorf("failed to receive message in time") + } + return nil } |