github.com/status-im/status-go@v1.1.0/wakuv2/persistence/dbkey.go (about) 1 package persistence 2 3 import ( 4 "encoding/binary" 5 "errors" 6 7 "github.com/waku-org/go-waku/waku/v2/hash" 8 ) 9 10 const ( 11 TimestampLength = 8 12 HashLength = 32 13 DigestLength = HashLength 14 PubsubTopicLength = HashLength 15 DBKeyLength = TimestampLength + PubsubTopicLength + DigestLength 16 ) 17 18 type Hash [HashLength]byte 19 20 var ( 21 // ErrInvalidByteSize is returned when DBKey can't be created 22 // from a byte slice because it has invalid length. 23 ErrInvalidByteSize = errors.New("byte slice has invalid length") 24 ) 25 26 // DBKey key to be stored in a db. 27 type DBKey struct { 28 raw []byte 29 } 30 31 // Bytes returns a bytes representation of the DBKey. 32 func (k *DBKey) Bytes() []byte { 33 return k.raw 34 } 35 36 // NewDBKey creates a new DBKey with the given values. 37 func NewDBKey(senderTimestamp uint64, receiverTimestamp uint64, pubsubTopic string, digest []byte) *DBKey { 38 pubSubHash := make([]byte, PubsubTopicLength) 39 if pubsubTopic != "" { 40 pubSubHash = hash.SHA256([]byte(pubsubTopic)) 41 } 42 43 var k DBKey 44 k.raw = make([]byte, DBKeyLength) 45 46 if senderTimestamp == 0 { 47 binary.BigEndian.PutUint64(k.raw, receiverTimestamp) 48 } else { 49 binary.BigEndian.PutUint64(k.raw, senderTimestamp) 50 } 51 52 copy(k.raw[TimestampLength:], pubSubHash[:]) 53 copy(k.raw[TimestampLength+PubsubTopicLength:], digest) 54 55 return &k 56 }