github.com/status-im/status-go@v1.1.0/waku/common/envelope.go (about) 1 // Copyright 2019 The Waku Library Authors. 2 // 3 // The Waku library is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Lesser General Public License as published by 5 // the Free Software Foundation, either version 3 of the License, or 6 // (at your option) any later version. 7 // 8 // The Waku library is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty off 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Lesser General Public License for more details. 12 // 13 // You should have received a copy of the GNU Lesser General Public License 14 // along with the Waku library. If not, see <http://www.gnu.org/licenses/>. 15 // 16 // This software uses the go-ethereum library, which is licensed 17 // under the GNU Lesser General Public Library, version 3 or any later. 18 19 package common 20 21 import ( 22 "crypto/ecdsa" 23 "encoding/binary" 24 "fmt" 25 "math" 26 "math/big" 27 "time" 28 29 "github.com/ethereum/go-ethereum/common" 30 "github.com/ethereum/go-ethereum/crypto" 31 "github.com/ethereum/go-ethereum/crypto/ecies" 32 "github.com/ethereum/go-ethereum/rlp" 33 ) 34 35 // Envelope represents a clear-text data packet to transmit through the Waku 36 // network. Its contents may or may not be encrypted and signed. 37 type Envelope struct { 38 Expiry uint32 39 TTL uint32 40 Topic TopicType 41 Data []byte 42 Nonce uint64 43 44 pow float64 // Message-specific PoW as described in the Waku specification. 45 46 // the following variables should not be accessed directly, use the corresponding function instead: Hash(), Bloom() 47 hash common.Hash // Cached hash of the envelope to avoid rehashing every time. 48 bloom []byte 49 } 50 51 // Size returns the size of envelope as it is sent (i.e. public fields only) 52 func (e *Envelope) Size() int { 53 return EnvelopeHeaderLength + len(e.Data) 54 } 55 56 // rlpWithoutNonce returns the RLP encoded envelope contents, except the nonce. 57 func (e *Envelope) rlpWithoutNonce() []byte { 58 res, _ := rlp.EncodeToBytes([]interface{}{e.Expiry, e.TTL, e.Topic, e.Data}) 59 return res 60 } 61 62 // NewEnvelope wraps a Waku message with expiration and destination data 63 // included into an envelope for network forwarding. 64 func NewEnvelope(ttl uint32, topic TopicType, msg *SentMessage, now time.Time) *Envelope { 65 env := Envelope{ 66 Expiry: uint32(now.Add(time.Second * time.Duration(ttl)).Unix()), 67 TTL: ttl, 68 Topic: topic, 69 Data: msg.Raw, 70 Nonce: 0, 71 } 72 73 return &env 74 } 75 76 // Seal closes the envelope by spending the requested amount of time as a proof 77 // of work on hashing the data. 78 func (e *Envelope) Seal(options *MessageParams) error { 79 if options.PoW == 0 { 80 // PoW is not required 81 return nil 82 } 83 84 var target, bestLeadingZeros int 85 if options.PoW < 0 { 86 // target is not set - the function should run for a period 87 // of time specified in WorkTime param. Since we can predict 88 // the execution time, we can also adjust Expiry. 89 e.Expiry += options.WorkTime 90 } else { 91 target = e.powToFirstBit(options.PoW) 92 } 93 94 rwn := e.rlpWithoutNonce() 95 buf := make([]byte, len(rwn)+8) 96 copy(buf, rwn) 97 asAnInt := new(big.Int) 98 99 finish := time.Now().Add(time.Duration(options.WorkTime) * time.Second).UnixNano() 100 for nonce := uint64(0); time.Now().UnixNano() < finish; { 101 for i := 0; i < 1024; i++ { 102 binary.BigEndian.PutUint64(buf[len(rwn):], nonce) 103 h := crypto.Keccak256(buf) 104 asAnInt.SetBytes(h) 105 leadingZeros := 256 - asAnInt.BitLen() 106 if leadingZeros > bestLeadingZeros { 107 e.Nonce, bestLeadingZeros = nonce, leadingZeros 108 if target > 0 && bestLeadingZeros >= target { 109 return nil 110 } 111 } 112 nonce++ 113 } 114 } 115 116 if target > 0 && bestLeadingZeros < target { 117 return fmt.Errorf("failed to reach the PoW target, specified pow time (%d seconds) was insufficient", options.WorkTime) 118 } 119 120 return nil 121 } 122 123 // PoW computes (if necessary) and returns the proof of work target 124 // of the envelope. 125 func (e *Envelope) PoW() float64 { 126 if e.pow == 0 { 127 e.CalculatePoW(0) 128 } 129 return e.pow 130 } 131 132 func (e *Envelope) CalculatePoW(diff uint32) { 133 rwn := e.rlpWithoutNonce() 134 buf := make([]byte, len(rwn)+8) 135 copy(buf, rwn) 136 binary.BigEndian.PutUint64(buf[len(rwn):], e.Nonce) 137 powHash := new(big.Int).SetBytes(crypto.Keccak256(buf)) 138 leadingZeroes := 256 - powHash.BitLen() 139 x := math.Pow(2, float64(leadingZeroes)) 140 x /= float64(len(rwn)) 141 x /= float64(e.TTL + diff) 142 e.pow = x 143 } 144 145 func (e *Envelope) powToFirstBit(pow float64) int { 146 x := pow 147 x *= float64(e.Size()) 148 x *= float64(e.TTL) 149 bits := math.Log2(x) 150 bits = math.Ceil(bits) 151 res := int(bits) 152 if res < 1 { 153 res = 1 154 } 155 return res 156 } 157 158 // Hash returns the SHA3 hash of the envelope, calculating it if not yet done. 159 func (e *Envelope) Hash() common.Hash { 160 if (e.hash == common.Hash{}) { 161 encoded, _ := rlp.EncodeToBytes(e) 162 e.hash = crypto.Keccak256Hash(encoded) 163 } 164 return e.hash 165 } 166 167 // DecodeRLP decodes an Envelope from an RLP data stream. 168 func (e *Envelope) DecodeRLP(s *rlp.Stream) error { 169 raw, err := s.Raw() 170 if err != nil { 171 return err 172 } 173 // The decoding of Envelope uses the struct fields but also needs 174 // to compute the hash of the whole RLP-encoded envelope. This 175 // type has the same structure as Envelope but is not an 176 // rlp.Decoder (does not implement DecodeRLP function). 177 // Only public members will be encoded. 178 type rlpenv Envelope 179 if err := rlp.DecodeBytes(raw, (*rlpenv)(e)); err != nil { 180 return err 181 } 182 e.hash = crypto.Keccak256Hash(raw) 183 return nil 184 } 185 186 // OpenAsymmetric tries to decrypt an envelope, potentially encrypted with a particular key. 187 func (e *Envelope) OpenAsymmetric(key *ecdsa.PrivateKey) (*ReceivedMessage, error) { 188 message := &ReceivedMessage{Raw: e.Data} 189 err := message.decryptAsymmetric(key) 190 switch err { 191 case nil: 192 return message, nil 193 case ecies.ErrInvalidPublicKey: // addressed to somebody else 194 return nil, err 195 default: 196 return nil, fmt.Errorf("unable to open envelope, decrypt failed: %v", err) 197 } 198 } 199 200 // OpenSymmetric tries to decrypt an envelope, potentially encrypted with a particular key. 201 func (e *Envelope) OpenSymmetric(key []byte) (msg *ReceivedMessage, err error) { 202 msg = &ReceivedMessage{Raw: e.Data} 203 err = msg.decryptSymmetric(key) 204 if err != nil { 205 msg = nil 206 } 207 return msg, err 208 } 209 210 // Open tries to decrypt an envelope, and populates the message fields in case of success. 211 func (e *Envelope) Open(watcher *Filter) (msg *ReceivedMessage) { 212 if watcher == nil { 213 return nil 214 } 215 216 // The API interface forbids filters doing both symmetric and asymmetric encryption. 217 if watcher.expectsAsymmetricEncryption() && watcher.expectsSymmetricEncryption() { 218 return nil 219 } 220 221 if watcher.expectsAsymmetricEncryption() { 222 msg, _ = e.OpenAsymmetric(watcher.KeyAsym) 223 if msg != nil { 224 msg.Dst = &watcher.KeyAsym.PublicKey 225 } 226 } else if watcher.expectsSymmetricEncryption() { 227 msg, _ = e.OpenSymmetric(watcher.KeySym) 228 if msg != nil { 229 msg.SymKeyHash = crypto.Keccak256Hash(watcher.KeySym) 230 } 231 } 232 233 if msg != nil { 234 ok := msg.ValidateAndParse() 235 if !ok { 236 return nil 237 } 238 msg.Topic = e.Topic 239 msg.PoW = e.PoW() 240 msg.TTL = e.TTL 241 msg.Sent = e.Expiry - e.TTL 242 msg.EnvelopeHash = e.Hash() 243 } 244 return msg 245 } 246 247 // Bloom maps 4-bytes Topic into 64-byte bloom filter with 3 bits set (at most). 248 func (e *Envelope) Bloom() []byte { 249 if e.bloom == nil { 250 e.bloom = e.Topic.ToBloom() 251 } 252 return e.bloom 253 } 254 255 // EnvelopeError code and optional description of the error. 256 type EnvelopeError struct { 257 Hash common.Hash 258 Code uint 259 Description string 260 } 261 262 // ErrorToEnvelopeError converts common golang error into EnvelopeError with a code. 263 func ErrorToEnvelopeError(hash common.Hash, err error) EnvelopeError { 264 code := EnvelopeOtherError 265 switch err.(type) { 266 case TimeSyncError: 267 code = EnvelopeTimeNotSynced 268 } 269 return EnvelopeError{ 270 Hash: hash, 271 Code: code, 272 Description: err.Error(), 273 } 274 }