Skip to content

Commit

Permalink
whisper: get wnode to work with v6 (ethereum#16051)
Browse files Browse the repository at this point in the history
The bulk of the issue was to adapt to the new requirement
that a v6 filter has to either contain a symmertric key or
an asymmetric one.

This commits revert one of the fixes that I made to remove
a linter warning: unexporting NewSentMessage. This is not
really a problem as I have a cleanup in the pipe that will
solve this issue.
  • Loading branch information
gballet authored and mariameda committed Aug 19, 2018
1 parent d26a1fe commit 565f1ef
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 70 deletions.
75 changes: 50 additions & 25 deletions cmd/wnode/main.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ import (
"strings"
"time"

"github.com/NiluPlatform/go-nilu/cmd/utils"
"github.com/NiluPlatform/go-nilu/common"
"github.com/NiluPlatform/go-nilu/console"
"github.com/NiluPlatform/go-nilu/crypto"
"github.com/NiluPlatform/go-nilu/log"
"github.com/NiluPlatform/go-nilu/p2p"
"github.com/NiluPlatform/go-nilu/p2p/discover"
"github.com/NiluPlatform/go-nilu/p2p/nat"
"github.com/NiluPlatform/go-nilu/whisper/mailserver"
whisper "github.com/NiluPlatform/go-nilu/whisper/whisperv5"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/console"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/whisper/mailserver"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
"golang.org/x/crypto/pbkdf2"
)

Expand All @@ -61,15 +61,17 @@ var (

// encryption
var (
symKey []byte
pub *ecdsa.PublicKey
asymKey *ecdsa.PrivateKey
nodeid *ecdsa.PrivateKey
topic whisper.TopicType
asymKeyID string
filterID string
symPass string
msPassword string
symKey []byte
pub *ecdsa.PublicKey
asymKey *ecdsa.PrivateKey
nodeid *ecdsa.PrivateKey
topic whisper.TopicType

asymKeyID string
asymFilterID string
symFilterID string
symPass string
msPassword string
)

// cmd arguments
Expand Down Expand Up @@ -363,13 +365,22 @@ func configureNode() {
}
}

filter := whisper.Filter{
symFilter := whisper.Filter{
KeySym: symKey,
Topics: [][]byte{topic[:]},
AllowP2P: p2pAccept,
}
symFilterID, err = shh.Subscribe(&symFilter)
if err != nil {
utils.Fatalf("Failed to install filter: %s", err)
}

asymFilter := whisper.Filter{
KeyAsym: asymKey,
Topics: [][]byte{topic[:]},
AllowP2P: p2pAccept,
}
filterID, err = shh.Subscribe(&filter)
asymFilterID, err = shh.Subscribe(&asymFilter)
if err != nil {
utils.Fatalf("Failed to install filter: %s", err)
}
Expand Down Expand Up @@ -522,17 +533,31 @@ func sendMsg(payload []byte) common.Hash {
}

func messageLoop() {
f := shh.GetFilter(filterID)
if f == nil {
utils.Fatalf("filter is not installed")
sf := shh.GetFilter(symFilterID)
if sf == nil {
utils.Fatalf("symmetric filter is not installed")
}

af := shh.GetFilter(asymFilterID)
if af == nil {
utils.Fatalf("asymmetric filter is not installed")
}

ticker := time.NewTicker(time.Millisecond * 50)

for {
select {
case <-ticker.C:
messages := f.Retrieve()
messages := sf.Retrieve()
for _, msg := range messages {
if *fileExMode || len(msg.Payload) > 2048 {
writeMessageToFile(*argSaveDir, msg)
} else {
printMessageInfo(msg)
}
}

messages = af.Retrieve()
for _, msg := range messages {
if *fileExMode || len(msg.Payload) > 2048 {
writeMessageToFile(*argSaveDir, msg)
Expand Down
12 changes: 6 additions & 6 deletions whisper/mailserver/mailserver.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"encoding/binary"
"fmt"

"github.com/NiluPlatform/go-nilu/cmd/utils"
"github.com/NiluPlatform/go-nilu/common"
"github.com/NiluPlatform/go-nilu/crypto"
"github.com/NiluPlatform/go-nilu/log"
"github.com/NiluPlatform/go-nilu/rlp"
whisper "github.com/NiluPlatform/go-nilu/whisper/whisperv5"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/util"
)
Expand Down
6 changes: 3 additions & 3 deletions whisper/mailserver/server_test.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"testing"
"time"

"github.com/NiluPlatform/go-nilu/common"
"github.com/NiluPlatform/go-nilu/crypto"
whisper "github.com/NiluPlatform/go-nilu/whisper/whisperv5"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
)

const powRequirement = 0.00001
Expand Down
2 changes: 1 addition & 1 deletion whisper/whisperv6/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (api *PublicWhisperAPI) Post(ctx context.Context, req NewMessage) (bool, er
}

// encrypt and sent message
whisperMsg, err := newSentMessage(params)
whisperMsg, err := NewSentMessage(params)
if err != nil {
return false, err
}
Expand Down
14 changes: 7 additions & 7 deletions whisper/whisperv6/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func BenchmarkEncryptionSym(b *testing.B) {
}

for i := 0; i < b.N; i++ {
msg, _ := newSentMessage(params)
msg, _ := NewSentMessage(params)
_, err := msg.Wrap(params)
if err != nil {
b.Errorf("failed Wrap with seed %d: %s.", seed, err)
Expand All @@ -64,7 +64,7 @@ func BenchmarkEncryptionAsym(b *testing.B) {
params.Dst = &key.PublicKey

for i := 0; i < b.N; i++ {
msg, _ := newSentMessage(params)
msg, _ := NewSentMessage(params)
_, err := msg.Wrap(params)
if err != nil {
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
Expand All @@ -79,7 +79,7 @@ func BenchmarkDecryptionSymValid(b *testing.B) {
if err != nil {
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
}
msg, _ := newSentMessage(params)
msg, _ := NewSentMessage(params)
env, err := msg.Wrap(params)
if err != nil {
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
Expand All @@ -101,7 +101,7 @@ func BenchmarkDecryptionSymInvalid(b *testing.B) {
if err != nil {
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
}
msg, _ := newSentMessage(params)
msg, _ := NewSentMessage(params)
env, err := msg.Wrap(params)
if err != nil {
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
Expand Down Expand Up @@ -130,7 +130,7 @@ func BenchmarkDecryptionAsymValid(b *testing.B) {
f := Filter{KeyAsym: key}
params.KeySym = nil
params.Dst = &key.PublicKey
msg, _ := newSentMessage(params)
msg, _ := NewSentMessage(params)
env, err := msg.Wrap(params)
if err != nil {
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
Expand All @@ -157,7 +157,7 @@ func BenchmarkDecryptionAsymInvalid(b *testing.B) {
}
params.KeySym = nil
params.Dst = &key.PublicKey
msg, _ := newSentMessage(params)
msg, _ := NewSentMessage(params)
env, err := msg.Wrap(params)
if err != nil {
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
Expand Down Expand Up @@ -199,7 +199,7 @@ func BenchmarkPoW(b *testing.B) {

for i := 0; i < b.N; i++ {
increment(params.Payload)
msg, _ := newSentMessage(params)
msg, _ := NewSentMessage(params)
_, err := msg.Wrap(params)
if err != nil {
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
Expand Down
2 changes: 1 addition & 1 deletion whisper/whisperv6/envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestEnvelopeOpenAcceptsOnlyOneKeyTypeInFilter(t *testing.T) {

mrand.Read(params.Payload)

msg, err := newSentMessage(&params)
msg, err := NewSentMessage(&params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand Down
16 changes: 8 additions & 8 deletions whisper/whisperv6/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func TestInstallIdenticalFilters(t *testing.T) {
filter1.Src = &params.Src.PublicKey
filter2.Src = &params.Src.PublicKey

sentMessage, err := newSentMessage(params)
sentMessage, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand Down Expand Up @@ -306,7 +306,7 @@ func TestMatchEnvelope(t *testing.T) {
params.Topic[0] = 0xFF // ensure mismatch

// mismatch with pseudo-random data
msg, err := newSentMessage(params)
msg, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand All @@ -327,7 +327,7 @@ func TestMatchEnvelope(t *testing.T) {
i := mrand.Int() % 4
fsym.Topics[i] = params.Topic[:]
fasym.Topics[i] = params.Topic[:]
msg, err = newSentMessage(params)
msg, err = NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand Down Expand Up @@ -372,7 +372,7 @@ func TestMatchEnvelope(t *testing.T) {
}
params.KeySym = nil
params.Dst = &key.PublicKey
msg, err = newSentMessage(params)
msg, err = NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand Down Expand Up @@ -453,7 +453,7 @@ func TestMatchMessageSym(t *testing.T) {
params.KeySym = f.KeySym
params.Topic = BytesToTopic(f.Topics[index])

sentMessage, err := newSentMessage(params)
sentMessage, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand Down Expand Up @@ -546,7 +546,7 @@ func TestMatchMessageAsym(t *testing.T) {
keySymOrig := params.KeySym
params.KeySym = nil

sentMessage, err := newSentMessage(params)
sentMessage, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand Down Expand Up @@ -630,7 +630,7 @@ func generateCompatibeEnvelope(t *testing.T, f *Filter) *Envelope {

params.KeySym = f.KeySym
params.Topic = BytesToTopic(f.Topics[2])
sentMessage, err := newSentMessage(params)
sentMessage, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand Down Expand Up @@ -806,7 +806,7 @@ func TestVariableTopics(t *testing.T) {
if err != nil {
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
}
msg, err := newSentMessage(params)
msg, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand Down
2 changes: 1 addition & 1 deletion whisper/whisperv6/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (msg *ReceivedMessage) isAsymmetricEncryption() bool {
}

// NewSentMessage creates and initializes a non-signed, non-encrypted Whisper message.
func newSentMessage(params *MessageParams) (*sentMessage, error) {
func NewSentMessage(params *MessageParams) (*sentMessage, error) {
const payloadSizeFieldMaxSize = 4
msg := sentMessage{}
msg.Raw = make([]byte, 1,
Expand Down
20 changes: 10 additions & 10 deletions whisper/whisperv6/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func singleMessageTest(t *testing.T, symmetric bool) {
text := make([]byte, 0, 512)
text = append(text, params.Payload...)

msg, err := newSentMessage(params)
msg, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestMessageWrap(t *testing.T) {
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
}

msg, err := newSentMessage(params)
msg, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand All @@ -149,7 +149,7 @@ func TestMessageWrap(t *testing.T) {
}

// set PoW target too high, expect error
msg2, err := newSentMessage(params)
msg2, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand All @@ -172,7 +172,7 @@ func TestMessageSeal(t *testing.T) {
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
}

msg, err := newSentMessage(params)
msg, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func singleEnvelopeOpenTest(t *testing.T, symmetric bool) {
text := make([]byte, 0, 512)
text = append(text, params.Payload...)

msg, err := newSentMessage(params)
msg, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand Down Expand Up @@ -289,7 +289,7 @@ func TestEncryptWithZeroKey(t *testing.T) {
if err != nil {
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
}
msg, err := newSentMessage(params)
msg, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand All @@ -303,7 +303,7 @@ func TestEncryptWithZeroKey(t *testing.T) {
if err != nil {
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
}
msg, err = newSentMessage(params)
msg, err = NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand All @@ -317,7 +317,7 @@ func TestEncryptWithZeroKey(t *testing.T) {
if err != nil {
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
}
msg, err = newSentMessage(params)
msg, err = NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand All @@ -335,7 +335,7 @@ func TestRlpEncode(t *testing.T) {
if err != nil {
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
}
msg, err := newSentMessage(params)
msg, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand Down Expand Up @@ -379,7 +379,7 @@ func singlePaddingTest(t *testing.T, padSize int) {
if n != padSize {
t.Fatalf("padding is not copied (seed %d): %s", seed, err)
}
msg, err := newSentMessage(params)
msg, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
Expand Down
Loading

0 comments on commit 565f1ef

Please sign in to comment.