2014-12-08 05:43:33 -06:00
|
|
|
package whisper
|
|
|
|
|
|
|
|
import (
|
2014-12-10 07:17:32 -06:00
|
|
|
"crypto/ecdsa"
|
2014-12-08 05:43:33 -06:00
|
|
|
"encoding/binary"
|
2014-12-10 07:17:32 -06:00
|
|
|
"fmt"
|
2014-12-08 05:43:33 -06:00
|
|
|
"time"
|
|
|
|
|
2015-03-19 09:18:31 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2014-12-08 05:43:33 -06:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-02-13 17:25:47 -06:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/ecies"
|
2014-12-08 05:43:33 -06:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-12-08 07:25:52 -06:00
|
|
|
DefaultPow = 50 * time.Millisecond
|
2014-12-08 05:43:33 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type Envelope struct {
|
2014-12-09 17:03:50 -06:00
|
|
|
Expiry uint32 // Whisper protocol specifies int32, really should be int64
|
2015-03-20 18:57:18 -05:00
|
|
|
TTL uint32 // ^^^^^^
|
2014-12-08 05:43:33 -06:00
|
|
|
Topics [][]byte
|
|
|
|
Data []byte
|
|
|
|
Nonce uint32
|
|
|
|
|
2015-03-20 18:57:18 -05:00
|
|
|
hash common.Hash
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
|
|
|
|
2015-03-20 18:57:18 -05:00
|
|
|
func (self *Envelope) Hash() common.Hash {
|
|
|
|
if (self.hash == common.Hash{}) {
|
2015-03-20 18:49:58 -05:00
|
|
|
enc, _ := rlp.EncodeToBytes(self)
|
2015-03-20 18:57:18 -05:00
|
|
|
self.hash = crypto.Sha3Hash(enc)
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
|
|
|
return self.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEnvelope(ttl time.Duration, topics [][]byte, data *Message) *Envelope {
|
|
|
|
exp := time.Now().Add(ttl)
|
2015-03-20 18:57:18 -05:00
|
|
|
return &Envelope{
|
|
|
|
Expiry: uint32(exp.Unix()),
|
|
|
|
TTL: uint32(ttl.Seconds()),
|
|
|
|
Topics: topics,
|
2015-04-10 07:53:21 -05:00
|
|
|
Data: data.bytes(),
|
2015-03-20 18:57:18 -05:00
|
|
|
Nonce: 0,
|
|
|
|
}
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
|
|
|
|
2014-12-09 17:03:50 -06:00
|
|
|
func (self *Envelope) Seal(pow time.Duration) {
|
|
|
|
self.proveWork(pow)
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
|
|
|
|
2015-04-10 07:53:21 -05:00
|
|
|
func (self *Envelope) Open(key *ecdsa.PrivateKey) (msg *Message, err error) {
|
2014-12-10 07:17:32 -06:00
|
|
|
data := self.Data
|
2015-04-10 07:53:21 -05:00
|
|
|
|
|
|
|
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")
|
2014-12-12 15:23:42 -06:00
|
|
|
}
|
2015-04-10 07:53:21 -05:00
|
|
|
message.Signature, data = data[:65], data[65:]
|
2014-12-12 15:23:42 -06:00
|
|
|
}
|
2015-04-10 07:53:21 -05:00
|
|
|
message.Payload = data
|
2014-12-16 12:55:57 -06:00
|
|
|
|
2015-04-10 07:53:21 -05:00
|
|
|
if key != nil {
|
|
|
|
message.Payload, err = crypto.Decrypt(key, message.Payload)
|
2014-12-15 14:54:34 -06:00
|
|
|
switch err {
|
2015-01-02 06:00:25 -06:00
|
|
|
case nil: // OK
|
2014-12-15 14:54:34 -06:00
|
|
|
case ecies.ErrInvalidPublicKey: // Payload isn't encrypted
|
|
|
|
return &message, err
|
|
|
|
default:
|
2014-12-10 07:17:32 -06:00
|
|
|
return nil, fmt.Errorf("unable to open envelope. Decrypt failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 15:23:42 -06:00
|
|
|
return &message, nil
|
2014-12-10 07:17:32 -06:00
|
|
|
}
|
|
|
|
|
2014-12-08 05:43:33 -06:00
|
|
|
func (self *Envelope) proveWork(dura time.Duration) {
|
|
|
|
var bestBit int
|
|
|
|
d := make([]byte, 64)
|
2015-03-20 18:49:58 -05:00
|
|
|
enc, _ := rlp.EncodeToBytes(self.withoutNonce())
|
|
|
|
copy(d[:32], enc)
|
2014-12-08 05:43:33 -06:00
|
|
|
|
|
|
|
then := time.Now().Add(dura).UnixNano()
|
|
|
|
for n := uint32(0); time.Now().UnixNano() < then; {
|
|
|
|
for i := 0; i < 1024; i++ {
|
|
|
|
binary.BigEndian.PutUint32(d[60:], n)
|
|
|
|
|
2015-03-16 05:27:38 -05:00
|
|
|
fbs := common.FirstBitSet(common.BigD(crypto.Sha3(d)))
|
2014-12-08 05:43:33 -06:00
|
|
|
if fbs > bestBit {
|
|
|
|
bestBit = fbs
|
|
|
|
self.Nonce = n
|
|
|
|
}
|
|
|
|
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Envelope) valid() bool {
|
|
|
|
d := make([]byte, 64)
|
2015-03-20 18:49:58 -05:00
|
|
|
enc, _ := rlp.EncodeToBytes(self.withoutNonce())
|
|
|
|
copy(d[:32], enc)
|
2014-12-08 05:43:33 -06:00
|
|
|
binary.BigEndian.PutUint32(d[60:], self.Nonce)
|
2015-03-16 05:27:38 -05:00
|
|
|
return common.FirstBitSet(common.BigD(crypto.Sha3(d))) > 0
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Envelope) withoutNonce() interface{} {
|
2015-03-20 18:57:18 -05:00
|
|
|
return []interface{}{self.Expiry, self.TTL, self.Topics, self.Data}
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
|
|
|
|
2015-03-20 18:49:58 -05:00
|
|
|
// rlpenv is an Envelope but is not an rlp.Decoder.
|
|
|
|
// It is used for decoding because we need to
|
|
|
|
type rlpenv Envelope
|
2015-01-13 06:36:44 -06:00
|
|
|
|
|
|
|
func (self *Envelope) DecodeRLP(s *rlp.Stream) error {
|
2015-03-20 18:49:58 -05:00
|
|
|
raw, err := s.Raw()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-01-13 06:36:44 -06:00
|
|
|
}
|
2015-03-20 18:49:58 -05:00
|
|
|
if err := rlp.DecodeBytes(raw, (*rlpenv)(self)); err != nil {
|
2015-01-13 06:36:44 -06:00
|
|
|
return err
|
|
|
|
}
|
2015-03-20 18:57:18 -05:00
|
|
|
self.hash = crypto.Sha3Hash(raw)
|
2015-01-13 06:36:44 -06:00
|
|
|
return nil
|
|
|
|
}
|