whisper, xeth/whisper: surface TTL and hash to the API

This commit is contained in:
Péter Szilágyi 2015-04-21 11:43:11 +03:00
parent 19bc4624ea
commit 7f48eb8737
5 changed files with 25 additions and 6 deletions

View File

@ -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:]

View File

@ -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())

View File

@ -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 {

View File

@ -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.

View File

@ -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()),
}
}