Skip to content
Snippets Groups Projects
Select Git revision
  • 6cec75481e99a5cbcc1c39ffc7981e19aeaa6e49
  • release default protected
  • 11-22-implement-kv-interface-defined-in-collectiveversionedkvgo
  • hotfix/TestHostPool_UpdateNdf_AddFilter
  • XX-4719/announcementChannels
  • xx-4717/logLevel
  • jonah/noob-channel
  • master protected
  • XX-4707/tagDiskJson
  • xx-4698/notification-retry
  • hotfix/notifylockup
  • syncNodes
  • hotfix/localCB
  • XX-4677/NewChanManagerMobile
  • XX-4689/DmSync
  • duplicatePrefix
  • XX-4601/HavenInvites
  • finalizedUICallbacks
  • XX-4673/AdminKeySync
  • debugNotifID
  • anne/test
  • v4.7.5
  • v4.7.4
  • v4.7.3
  • v4.7.2
  • v4.7.1
  • v4.6.3
  • v4.6.1
  • v4.5.0
  • v4.4.4
  • v4.3.11
  • v4.3.8
  • v4.3.7
  • v4.3.6
  • v4.3.5
  • v4.2.0
  • v4.3.0
  • v4.3.4
  • v4.3.3
  • v4.3.2
  • v4.3.1
41 results

sentRequestHandler.go

Blame
  • message.go 3.13 KiB
    ////////////////////////////////////////////////////////////////////////////////
    // Copyright © 2018 Privategrity Corporation                                   /
    //                                                                             /
    // All rights reserved.                                                        /
    ////////////////////////////////////////////////////////////////////////////////
    
    package parse
    
    import (
    	"crypto/sha256"
    	"gitlab.com/elixxir/primitives/id"
    )
    
    const MessageHashLenBits = 256
    const MessageHashLen = MessageHashLenBits / 8
    
    type MessageHash [MessageHashLen]byte
    
    type Message struct {
    	TypedBody
    	// The crypto type is inferred from the message's contents
    	InferredType CryptoType
    	Sender       *id.User
    	Receiver     *id.User
    	Nonce        []byte
    }
    
    type CryptoType int32
    
    const (
    	None CryptoType = iota
    	Unencrypted
    	Rekey
    	E2E
    )
    
    var cryptoTypeStrArr = []string{"None", "Unencrypted", "Rekey", "E2E"}
    
    func (ct CryptoType) String() string {
    	return cryptoTypeStrArr[ct]
    }
    
    // Interface used to standardize message definitions
    type MessageInterface interface {
    	// Returns the message's sender ID
    	// (uint64) BigEndian serialized into a byte slice
    	GetSender() *id.User
    	// Returns the message payload, without packed type
    	GetPayload() []byte
    	// Returns the message's recipient ID
    	// (uint64) BigEndian serialized into a byte slice
    	GetRecipient() *id.User
    	// Return the message's inner type
    	GetMessageType() int32
    	// Returns the message's outer type
    	GetCryptoType() CryptoType
    	// Return the message fully serialized including the type prefix
    	// Does this really belong in the interface?
    	Pack() []byte
    }
    
    func (m Message) Hash() MessageHash {
    	var mh MessageHash
    
    	h := sha256.New()
    
    	h.Write(TypeAsBytes(int32(m.MessageType)))
    	h.Write(m.Body)
    	h.Write(m.Sender.Bytes())
    	h.Write(m.Receiver.Bytes())
    	//h.Write(m.Nonce)
    
    	hashed := h.Sum(nil)
    
    	copy(mh[:], hashed[:MessageHashLen])
    
    	return mh
    }
    
    func (m *Message) GetSender() *id.User {
    	return m.Sender
    }
    
    func (m *Message) GetRecipient() *id.User {
    	return m.Receiver
    }
    
    func (m *Message) GetPayload() []byte {
    	return m.Body
    }
    
    func (m *Message) GetMessageType() int32 {
    	return m.MessageType
    }
    
    func (m *Message) GetCryptoType() CryptoType {
    	return m.InferredType
    }
    
    func (m *Message) Pack() []byte {
    	return Pack(&m.TypedBody)
    }
    
    // Implements Message type compatibility with bindings package's limited types
    type BindingsMessageProxy struct {
    	Proxy *Message
    }
    
    func (p *BindingsMessageProxy) GetSender() []byte {
    	return p.Proxy.GetSender().Bytes()
    }
    
    func (p *BindingsMessageProxy) GetRecipient() []byte {
    	return p.Proxy.GetRecipient().Bytes()
    }
    
    // TODO Should we actually pass this over the boundary as a byte slice?
    // It's essentially a binary blob, so probably yes.
    func (p *BindingsMessageProxy) GetPayload() []byte {
    	return p.Proxy.GetPayload()
    }
    
    func (p *BindingsMessageProxy) GetMessageType() int32 {
    	return int32(p.Proxy.GetMessageType())
    }
    
    // Includes the type. Not sure if this is the right way to approach this.
    func (p *BindingsMessageProxy) Pack() []byte {
    	return Pack(&TypedBody{
    		MessageType: p.Proxy.GetMessageType(),
    		Body:        p.Proxy.GetPayload(),
    	})
    }