diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-04-21 16:43:11 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-04-28 15:49:04 +0800 |
commit | 7f48eb8737878e352a65475382532db26f9fbc52 (patch) | |
tree | 7db869690505978a4f111b996e7357572f64211c | |
parent | 19bc4624eaefc2c8201260e7afa1a5893159bffc (diff) | |
download | go-tangerine-7f48eb8737878e352a65475382532db26f9fbc52.tar.gz go-tangerine-7f48eb8737878e352a65475382532db26f9fbc52.tar.zst go-tangerine-7f48eb8737878e352a65475382532db26f9fbc52.zip |
whisper, xeth/whisper: surface TTL and hash to the API
-rw-r--r-- | whisper/envelope.go | 3 | ||||
-rw-r--r-- | whisper/envelope_test.go | 6 | ||||
-rw-r--r-- | whisper/message.go | 10 | ||||
-rw-r--r-- | whisper/message_test.go | 4 | ||||
-rw-r--r-- | xeth/whisper_message.go | 8 |
5 files changed, 25 insertions, 6 deletions
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. diff --git a/xeth/whisper_message.go b/xeth/whisper_message.go index 14796cfbc..c8195cec1 100644 --- a/xeth/whisper_message.go +++ b/xeth/whisper_message.go @@ -3,6 +3,8 @@ package xeth import ( + "time" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/whisper" @@ -16,6 +18,8 @@ type WhisperMessage struct { To string `json:"to"` From string `json:"from"` Sent int64 `json:"sent"` + TTL int64 `json:"ttl"` + Hash string `json:"hash"` } // NewWhisperMessage converts an internal message into an API version. @@ -26,6 +30,8 @@ func NewWhisperMessage(message *whisper.Message) WhisperMessage { Payload: common.ToHex(message.Payload), From: common.ToHex(crypto.FromECDSAPub(message.Recover())), To: common.ToHex(crypto.FromECDSAPub(message.To)), - Sent: message.Sent, + Sent: message.Sent.Unix(), + TTL: int64(message.TTL / time.Second), + Hash: common.ToHex(message.Hash.Bytes()), } } |