bsc/whisper/envelope.go

130 lines
2.8 KiB
Go
Raw Normal View History

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