diff options
author | obscuren <geffobscura@gmail.com> | 2014-12-10 21:17:32 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-12-10 21:17:32 +0800 |
commit | dda778eda7ad9b94acf14c3c91c1c29e711e170f (patch) | |
tree | dfbb71a45399455fd2c2bb77ce72c8ce9d79ba45 /whisper/envelope.go | |
parent | 0f5c6c5e2daa9fbf3a0bb753debd8989a872823c (diff) | |
download | dexon-dda778eda7ad9b94acf14c3c91c1c29e711e170f.tar dexon-dda778eda7ad9b94acf14c3c91c1c29e711e170f.tar.gz dexon-dda778eda7ad9b94acf14c3c91c1c29e711e170f.tar.bz2 dexon-dda778eda7ad9b94acf14c3c91c1c29e711e170f.tar.lz dexon-dda778eda7ad9b94acf14c3c91c1c29e711e170f.tar.xz dexon-dda778eda7ad9b94acf14c3c91c1c29e711e170f.tar.zst dexon-dda778eda7ad9b94acf14c3c91c1c29e711e170f.zip |
Updated whisper messages to new crypto api + added tests
Diffstat (limited to 'whisper/envelope.go')
-rw-r--r-- | whisper/envelope.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/whisper/envelope.go b/whisper/envelope.go index eb80098ad..359fa1568 100644 --- a/whisper/envelope.go +++ b/whisper/envelope.go @@ -2,7 +2,9 @@ package whisper import ( "bytes" + "crypto/ecdsa" "encoding/binary" + "fmt" "io" "time" @@ -59,6 +61,24 @@ func (self *Envelope) Seal(pow time.Duration) { self.proveWork(pow) } +func (self *Envelope) Open(prv *ecdsa.PrivateKey) (*Message, error) { + data := self.Data + if data[0] > 0 && len(data) < 66 { + return nil, fmt.Errorf("unable to open envelope. First bit set but len(data) < 66") + } + + if data[0] > 0 { + payload, err := crypto.Decrypt(prv, data[66:]) + if err != nil { + return nil, fmt.Errorf("unable to open envelope. Decrypt failed: %v", err) + } + + return NewMessage(payload), nil + } + + return NewMessage(data[1:]), nil +} + func (self *Envelope) proveWork(dura time.Duration) { var bestBit int d := make([]byte, 64) |