diff options
author | obscuren <geffobscura@gmail.com> | 2014-12-10 07:03:50 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-12-10 07:03:50 +0800 |
commit | 984c7e6689f720e0b60a462e00793364de397909 (patch) | |
tree | 44ed06cf585efc3348d63a465b96c2dc84fb2bdf /whisper/message.go | |
parent | 87adff7e189ee0a1fd50a3ef30ba22482e60f314 (diff) | |
download | go-tangerine-984c7e6689f720e0b60a462e00793364de397909.tar.gz go-tangerine-984c7e6689f720e0b60a462e00793364de397909.tar.zst go-tangerine-984c7e6689f720e0b60a462e00793364de397909.zip |
Added encryption for messages better API for sealing messages
Diffstat (limited to 'whisper/message.go')
-rw-r--r-- | whisper/message.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/whisper/message.go b/whisper/message.go index 21cf163e6..408b9f7df 100644 --- a/whisper/message.go +++ b/whisper/message.go @@ -1,5 +1,11 @@ package whisper +import ( + "time" + + "github.com/ethereum/go-ethereum/crypto" +) + type Message struct { Flags byte Signature []byte @@ -10,6 +16,49 @@ func NewMessage(payload []byte) *Message { return &Message{Flags: 0, Payload: payload} } +func (self *Message) hash() []byte { + return crypto.Sha3(append([]byte{self.Flags}, self.Payload...)) +} + +func (self *Message) sign(key []byte) (err error) { + self.Flags = 1 + self.Signature, err = crypto.Sign(self.hash(), key) + return +} + +func (self *Message) Encrypt(from, to []byte) (err error) { + err = self.sign(from) + if err != nil { + return err + } + + self.Payload, err = crypto.Encrypt(to, self.Payload) + if err != nil { + return err + } + + return nil +} + func (self *Message) Bytes() []byte { return append([]byte{self.Flags}, append(self.Signature, self.Payload...)...) } + +type Opts struct { + From, To []byte // private(sender), public(receiver) key + Ttl time.Duration + Topics [][]byte +} + +func (self *Message) Seal(pow time.Duration, opts Opts) (*Envelope, error) { + if len(opts.To) > 0 && len(opts.From) > 0 { + if err := self.Encrypt(opts.From, opts.To); err != nil { + return nil, err + } + } + + envelope := NewEnvelope(DefaultTtl, opts.Topics, self) + envelope.Seal(pow) + + return envelope, nil +} |