aboutsummaryrefslogtreecommitdiffstats
path: root/whisper/envelope.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2015-04-10 20:53:21 +0800
committerPéter Szilágyi <peterke@gmail.com>2015-04-10 20:53:21 +0800
commit7d8ce53eca90a6e93e49d584a2cff5e39aea40c0 (patch)
tree1f42e76be9c66a55407741aa15aa659fdf408850 /whisper/envelope.go
parent7e54a9c07f10ff16aabfe8bd3944c9309d4efc06 (diff)
downloadgo-tangerine-7d8ce53eca90a6e93e49d584a2cff5e39aea40c0.tar.gz
go-tangerine-7d8ce53eca90a6e93e49d584a2cff5e39aea40c0.tar.zst
go-tangerine-7d8ce53eca90a6e93e49d584a2cff5e39aea40c0.zip
whisper: polish the messages, fix some bugs, tests
Bugs fixed: - Use randomly generated flags as the spec required. - During envelope opening check the first bit only for signature.
Diffstat (limited to 'whisper/envelope.go')
-rw-r--r--whisper/envelope.go30
1 files changed, 15 insertions, 15 deletions
diff --git a/whisper/envelope.go b/whisper/envelope.go
index 20e3e6d39..65dc89936 100644
--- a/whisper/envelope.go
+++ b/whisper/envelope.go
@@ -40,7 +40,7 @@ func NewEnvelope(ttl time.Duration, topics [][]byte, data *Message) *Envelope {
Expiry: uint32(exp.Unix()),
TTL: uint32(ttl.Seconds()),
Topics: topics,
- Data: data.Bytes(),
+ Data: data.bytes(),
Nonce: 0,
}
}
@@ -49,32 +49,32 @@ func (self *Envelope) Seal(pow time.Duration) {
self.proveWork(pow)
}
-func (self *Envelope) Open(prv *ecdsa.PrivateKey) (msg *Message, err error) {
+func (self *Envelope) Open(key *ecdsa.PrivateKey) (msg *Message, err error) {
data := self.Data
- var message Message
- dataStart := 1
- if data[0] > 0 {
- if len(data) < 66 {
- return nil, fmt.Errorf("unable to open envelope. First bit set but len(data) < 66")
+
+ message := Message{
+ Flags: data[0],
+ }
+ data = data[1:]
+
+ if message.Flags&128 == 128 {
+ if len(data) < 65 {
+ return nil, fmt.Errorf("unable to open envelope. First bit set but len(data) < 65")
}
- dataStart = 66
- message.Flags = data[0]
- message.Signature = data[1:66]
+ message.Signature, data = data[:65], data[65:]
}
+ message.Payload = data
- payload := data[dataStart:]
- if prv != nil {
- message.Payload, err = crypto.Decrypt(prv, payload)
+ if key != nil {
+ message.Payload, err = crypto.Decrypt(key, message.Payload)
switch err {
case nil: // OK
case ecies.ErrInvalidPublicKey: // Payload isn't encrypted
- message.Payload = payload
return &message, err
default:
return nil, fmt.Errorf("unable to open envelope. Decrypt failed: %v", err)
}
}
-
return &message, nil
}