2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2014-12-08 05:43:33 -06:00
|
|
|
// +build none
|
|
|
|
|
2015-04-10 05:42:49 -05:00
|
|
|
// Contains a simple whisper peer setup and self messaging to allow playing
|
|
|
|
// around with the protocol and API without a fancy client implementation.
|
|
|
|
|
2014-12-08 05:43:33 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2015-04-10 05:42:49 -05:00
|
|
|
"time"
|
2014-12-08 05:43:33 -06:00
|
|
|
|
2015-04-10 05:42:49 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2014-12-08 05:43:33 -06:00
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2015-04-10 05:42:49 -05:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
2014-12-08 05:43:33 -06:00
|
|
|
"github.com/ethereum/go-ethereum/whisper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
logger.AddLogSystem(logger.NewStdLogSystem(os.Stdout, log.LstdFlags, logger.InfoLevel))
|
|
|
|
|
2015-04-10 05:42:49 -05:00
|
|
|
// 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()
|
2014-12-08 05:43:33 -06:00
|
|
|
|
2015-04-10 05:42:49 -05:00
|
|
|
// Create an Ethereum peer to communicate through
|
|
|
|
server := p2p.Server{
|
|
|
|
PrivateKey: key,
|
2014-12-08 05:43:33 -06:00
|
|
|
MaxPeers: 10,
|
2015-04-10 05:42:49 -05:00
|
|
|
Name: name,
|
|
|
|
Protocols: []p2p.Protocol{shh.Protocol()},
|
2014-12-15 10:14:02 -06:00
|
|
|
ListenAddr: ":30300",
|
2015-04-10 05:42:49 -05:00
|
|
|
NAT: nat.Any(),
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
2015-04-10 05:42:49 -05:00
|
|
|
fmt.Println("Starting Ethereum peer...")
|
|
|
|
if err := server.Start(); err != nil {
|
|
|
|
fmt.Printf("Failed to start Ethereum peer: %v.\n", err)
|
2014-12-08 05:43:33 -06:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2015-04-10 05:42:49 -05:00
|
|
|
// 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)
|
2015-04-14 07:16:02 -05:00
|
|
|
envelope, err := msg.Wrap(whisper.DefaultPoW, whisper.Options{
|
2015-04-10 05:42:49 -05:00
|
|
|
From: id,
|
|
|
|
To: &id.PublicKey,
|
2015-04-14 07:16:02 -05:00
|
|
|
TTL: whisper.DefaultTTL,
|
2015-04-10 05:42:49 -05:00
|
|
|
})
|
|
|
|
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
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|