From 7f48eb8737878e352a65475382532db26f9fbc52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Tue, 21 Apr 2015 11:43:11 +0300 Subject: whisper, xeth/whisper: surface TTL and hash to the API --- whisper/envelope.go | 3 ++- whisper/envelope_test.go | 6 +++++- whisper/message.go | 10 +++++++--- whisper/message_test.go | 4 ++++ 4 files changed, 18 insertions(+), 5 deletions(-) (limited to 'whisper') diff --git a/whisper/envelope.go b/whisper/envelope.go index c1d84df78..a4e2fa031 100644 --- a/whisper/envelope.go +++ b/whisper/envelope.go @@ -72,7 +72,8 @@ func (self *Envelope) Open(key *ecdsa.PrivateKey) (msg *Message, err error) { message := &Message{ Flags: data[0], - Sent: int64(self.Expiry - self.TTL), + Sent: time.Unix(int64(self.Expiry-self.TTL), 0), + TTL: time.Duration(self.TTL) * time.Second, Hash: self.Hash(), } data = data[1:] diff --git a/whisper/envelope_test.go b/whisper/envelope_test.go index ed1f08365..3117284f1 100644 --- a/whisper/envelope_test.go +++ b/whisper/envelope_test.go @@ -3,6 +3,7 @@ package whisper import ( "bytes" "testing" + "time" ) func TestEnvelopeOpen(t *testing.T) { @@ -26,9 +27,12 @@ func TestEnvelopeOpen(t *testing.T) { if bytes.Compare(opened.Payload, message.Payload) != 0 { t.Fatalf("payload mismatch: have 0x%x, want 0x%x", opened.Payload, message.Payload) } - if opened.Sent != message.Sent { + if opened.Sent.Unix() != message.Sent.Unix() { t.Fatalf("send time mismatch: have %d, want %d", opened.Sent, message.Sent) } + if opened.TTL/time.Second != DefaultTTL/time.Second { + t.Fatalf("message TTL mismatch: have %v, want %v", opened.TTL, DefaultTTL) + } if opened.Hash != envelope.Hash() { t.Fatalf("message hash mismatch: have 0x%x, want 0x%x", opened.Hash, envelope.Hash()) diff --git a/whisper/message.go b/whisper/message.go index 69d85b894..2b92d515c 100644 --- a/whisper/message.go +++ b/whisper/message.go @@ -21,10 +21,12 @@ type Message struct { Flags byte // First bit is signature presence, rest reserved and should be random Signature []byte Payload []byte - Sent int64 + + Sent time.Time // Time when the message was posted into the network + TTL time.Duration // Maximum time to live allowed for the message To *ecdsa.PublicKey // Message recipient (identity used to decode the message) - Hash common.Hash // Message envelope hash to act as a unique id in de-duplication + Hash common.Hash // Message envelope hash to act as a unique id } // Options specifies the exact way a message should be wrapped into an Envelope. @@ -45,7 +47,7 @@ func NewMessage(payload []byte) *Message { return &Message{ Flags: flags, Payload: payload, - Sent: time.Now().Unix(), + Sent: time.Now(), } } @@ -66,6 +68,8 @@ func (self *Message) Wrap(pow time.Duration, options Options) (*Envelope, error) if options.TTL == 0 { options.TTL = DefaultTTL } + self.TTL = options.TTL + // Sign and encrypt the message if requested if options.From != nil { if err := self.sign(options.From); err != nil { diff --git a/whisper/message_test.go b/whisper/message_test.go index 18a254e5c..0b4a24c24 100644 --- a/whisper/message_test.go +++ b/whisper/message_test.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/elliptic" "testing" + "time" "github.com/ethereum/go-ethereum/crypto" ) @@ -25,6 +26,9 @@ func TestMessageSimpleWrap(t *testing.T) { if bytes.Compare(msg.Payload, payload) != 0 { t.Fatalf("payload mismatch after wrapping: have 0x%x, want 0x%x", msg.Payload, payload) } + if msg.TTL/time.Second != DefaultTTL/time.Second { + t.Fatalf("message TTL mismatch: have %v, want %v", msg.TTL, DefaultTTL) + } } // Tests whether a message can be signed, and wrapped in plain-text. -- cgit v1.2.3