Updated whisper messages to new crypto api + added tests

This commit is contained in:
obscuren
2014-12-10 14:17:32 +01:00
parent 0f5c6c5e2d
commit dda778eda7
5 changed files with 89 additions and 11 deletions

View File

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