mirror of https://github.com/ethereum/go-ethereum
commit
faa2747809
@ -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.Wrap(whisper.DefaultProofOfWork, whisper.Options{ |
||||
From: id, |
||||
To: &id.PublicKey, |
||||
TTL: whisper.DefaultTimeToLive, |
||||
}) |
||||
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 |
||||
} |
||||
|
@ -0,0 +1,138 @@ |
||||
package whisper |
||||
|
||||
import ( |
||||
"bytes" |
||||
"crypto/elliptic" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
) |
||||
|
||||
// Tests whether a message can be wrapped without any identity or encryption.
|
||||
func TestMessageSimpleWrap(t *testing.T) { |
||||
payload := []byte("hello world") |
||||
|
||||
msg := NewMessage(payload) |
||||
if _, err := msg.Wrap(DefaultProofOfWork, Options{}); err != nil { |
||||
t.Fatalf("failed to wrap message: %v", err) |
||||
} |
||||
if msg.Flags&128 != 0 { |
||||
t.Fatalf("signature flag mismatch: have %d, want %d", (msg.Flags&128)>>7, 0) |
||||
} |
||||
if len(msg.Signature) != 0 { |
||||
t.Fatalf("signature found for simple wrapping: 0x%x", msg.Signature) |
||||
} |
||||
if bytes.Compare(msg.Payload, payload) != 0 { |
||||
t.Fatalf("payload mismatch after wrapping: have 0x%x, want 0x%x", msg.Payload, payload) |
||||
} |
||||
} |
||||
|
||||
// Tests whether a message can be signed, and wrapped in plain-text.
|
||||
func TestMessageCleartextSignRecover(t *testing.T) { |
||||
key, err := crypto.GenerateKey() |
||||
if err != nil { |
||||
t.Fatalf("failed to create crypto key: %v", err) |
||||
} |
||||
payload := []byte("hello world") |
||||
|
||||
msg := NewMessage(payload) |
||||
if _, err := msg.Wrap(DefaultProofOfWork, Options{ |
||||
From: key, |
||||
}); err != nil { |
||||
t.Fatalf("failed to sign message: %v", err) |
||||
} |
||||
if msg.Flags&128 != 128 { |
||||
t.Fatalf("signature flag mismatch: have %d, want %d", (msg.Flags&128)>>7, 1) |
||||
} |
||||
if bytes.Compare(msg.Payload, payload) != 0 { |
||||
t.Fatalf("payload mismatch after signing: have 0x%x, want 0x%x", msg.Payload, payload) |
||||
} |
||||
|
||||
pubKey := msg.Recover() |
||||
if pubKey == nil { |
||||
t.Fatalf("failed to recover public key") |
||||
} |
||||
p1 := elliptic.Marshal(crypto.S256(), key.PublicKey.X, key.PublicKey.Y) |
||||
p2 := elliptic.Marshal(crypto.S256(), pubKey.X, pubKey.Y) |
||||
if !bytes.Equal(p1, p2) { |
||||
t.Fatalf("public key mismatch: have 0x%x, want 0x%x", p2, p1) |
||||
} |
||||
} |
||||
|
||||
// Tests whether a message can be encrypted and decrypted using an anonymous
|
||||
// sender (i.e. no signature).
|
||||
func TestMessageAnonymousEncryptDecrypt(t *testing.T) { |
||||
key, err := crypto.GenerateKey() |
||||
if err != nil { |
||||
t.Fatalf("failed to create recipient crypto key: %v", err) |
||||
} |
||||
payload := []byte("hello world") |
||||
|
||||
msg := NewMessage(payload) |
||||
envelope, err := msg.Wrap(DefaultProofOfWork, Options{ |
||||
To: &key.PublicKey, |
||||
}) |
||||
if err != nil { |
||||
t.Fatalf("failed to encrypt message: %v", err) |
||||
} |
||||
if msg.Flags&128 != 0 { |
||||
t.Fatalf("signature flag mismatch: have %d, want %d", (msg.Flags&128)>>7, 0) |
||||
} |
||||
if len(msg.Signature) != 0 { |
||||
t.Fatalf("signature found for anonymous message: 0x%x", msg.Signature) |
||||
} |
||||
|
||||
out, err := envelope.Open(key) |
||||
if err != nil { |
||||
t.Fatalf("failed to open encrypted message: %v", err) |
||||
} |
||||
if !bytes.Equal(out.Payload, payload) { |
||||
t.Error("payload mismatch: have 0x%x, want 0x%x", out.Payload, payload) |
||||
} |
||||
} |
||||
|
||||
// Tests whether a message can be properly signed and encrypted.
|
||||
func TestMessageFullCrypto(t *testing.T) { |
||||
fromKey, err := crypto.GenerateKey() |
||||
if err != nil { |
||||
t.Fatalf("failed to create sender crypto key: %v", err) |
||||
} |
||||
toKey, err := crypto.GenerateKey() |
||||
if err != nil { |
||||
t.Fatalf("failed to create recipient crypto key: %v", err) |
||||
} |
||||
|
||||
payload := []byte("hello world") |
||||
msg := NewMessage(payload) |
||||
envelope, err := msg.Wrap(DefaultProofOfWork, Options{ |
||||
From: fromKey, |
||||
To: &toKey.PublicKey, |
||||
}) |
||||
if err != nil { |
||||
t.Fatalf("failed to encrypt message: %v", err) |
||||
} |
||||
if msg.Flags&128 != 128 { |
||||
t.Fatalf("signature flag mismatch: have %d, want %d", (msg.Flags&128)>>7, 1) |
||||
} |
||||
if len(msg.Signature) == 0 { |
||||
t.Fatalf("no signature found for signed message") |
||||
} |
||||
|
||||
out, err := envelope.Open(toKey) |
||||
if err != nil { |
||||
t.Fatalf("failed to open encrypted message: %v", err) |
||||
} |
||||
if !bytes.Equal(out.Payload, payload) { |
||||
t.Error("payload mismatch: have 0x%x, want 0x%x", out.Payload, payload) |
||||
} |
||||
|
||||
pubKey := out.Recover() |
||||
if pubKey == nil { |
||||
t.Fatalf("failed to recover public key") |
||||
} |
||||
p1 := elliptic.Marshal(crypto.S256(), fromKey.PublicKey.X, fromKey.PublicKey.Y) |
||||
p2 := elliptic.Marshal(crypto.S256(), pubKey.X, pubKey.Y) |
||||
if !bytes.Equal(p1, p2) { |
||||
t.Fatalf("public key mismatch: have 0x%x, want 0x%x", p2, p1) |
||||
} |
||||
} |
@ -1,50 +0,0 @@ |
||||
package whisper |
||||
|
||||
import ( |
||||
"bytes" |
||||
"crypto/elliptic" |
||||
"fmt" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
) |
||||
|
||||
func TestSign(t *testing.T) { |
||||
prv, _ := crypto.GenerateKey() |
||||
msg := NewMessage([]byte("hello world")) |
||||
msg.sign(prv) |
||||
|
||||
pubKey := msg.Recover() |
||||
p1 := elliptic.Marshal(crypto.S256(), prv.PublicKey.X, prv.PublicKey.Y) |
||||
p2 := elliptic.Marshal(crypto.S256(), pubKey.X, pubKey.Y) |
||||
|
||||
if !bytes.Equal(p1, p2) { |
||||
t.Error("recovered pub key did not match") |
||||
} |
||||
} |
||||
|
||||
func TestMessageEncryptDecrypt(t *testing.T) { |
||||
prv1, _ := crypto.GenerateKey() |
||||
prv2, _ := crypto.GenerateKey() |
||||
|
||||
data := []byte("hello world") |
||||
msg := NewMessage(data) |
||||
envelope, err := msg.Seal(DefaultPow, Opts{ |
||||
From: prv1, |
||||
To: &prv2.PublicKey, |
||||
}) |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
t.FailNow() |
||||
} |
||||
|
||||
msg1, err := envelope.Open(prv2) |
||||
if err != nil { |
||||
t.Error(err) |
||||
t.FailNow() |
||||
} |
||||
|
||||
if !bytes.Equal(msg1.Payload, data) { |
||||
t.Error("encryption error. data did not match") |
||||
} |
||||
} |
Loading…
Reference in new issue