diff --git a/bindings/e2eHandler.go b/bindings/e2eHandler.go index 79f306c83bc5a4376041471655514e1731fc5451..043b69ce352fb51653608f56e1dce09af7a62c02 100644 --- a/bindings/e2eHandler.go +++ b/bindings/e2eHandler.go @@ -6,6 +6,17 @@ package bindings +import ( + "encoding/json" + "fmt" + "gitlab.com/elixxir/client/catalog" + "gitlab.com/elixxir/client/cmix/identity/receptionID" + "gitlab.com/elixxir/client/cmix/rounds" + "gitlab.com/elixxir/client/e2e" + "gitlab.com/elixxir/primitives/format" + "gitlab.com/xx_network/primitives/id" +) + // IdList is a wrapper for a list of marshalled id.ID objects type IdList struct { Ids [][]byte @@ -22,168 +33,167 @@ type E2ESendReport struct { Timestamp int64 } -// -//// GetReceptionID returns the marshalled default IDs -//// Returns: -//// - []byte - the marshalled bytes of the id.ID object. -//func (e *E2e) GetReceptionID() []byte { -// return e.api.GetE2E().GetReceptionID().Marshal() -//} -// -//// GetAllPartnerIDs returns a marshalled list of all partner IDs that the user has -//// an E2E relationship with. -//// Returns: -//// - []byte - the marshalled bytes of the IdList object. -//func (e *E2e) GetAllPartnerIDs() ([]byte, error) { -// partnerIds := e.api.GetE2E().GetAllPartnerIDs() -// convertedIds := make([][]byte, len(partnerIds)) -// for i, partnerId := range partnerIds { -// convertedIds[i] = partnerId.Marshal() -// } -// return json.Marshal(IdList{Ids: convertedIds}) -//} -// -//// PayloadSize Returns the max payload size for a partitionable E2E -//// message -//func (e *E2e) PayloadSize() int { -// return int(e.api.GetE2E().PayloadSize()) -//} -// -//// SecondPartitionSize returns the max partition payload size for all -//// payloads after the first payload -//func (e *E2e) SecondPartitionSize() int { -// return int(e.api.GetE2E().SecondPartitionSize()) -//} -// -//// PartitionSize returns the partition payload size for the given -//// payload index. The first payload is index 0. -//func (e *E2e) PartitionSize(payloadIndex int) int { -// return int(e.api.GetE2E().PartitionSize(uint(payloadIndex))) -//} -// -//// FirstPartitionSize returns the max partition payload size for the -//// first payload -//func (e *E2e) FirstPartitionSize() int { -// return int(e.api.GetE2E().FirstPartitionSize()) -//} -// -//// GetHistoricalDHPrivkey returns the user's marshalled Historical DH Private Key -//// Returns: -//// - []byte - the marshalled bytes of the cyclic.Int object. -//func (e *E2e) GetHistoricalDHPrivkey() ([]byte, error) { -// return e.api.GetE2E().GetHistoricalDHPrivkey().MarshalJSON() -//} -// -//// GetHistoricalDHPubkey returns the user's marshalled Historical DH -//// Public Key -//// Returns: -//// - []byte - the marshalled bytes of the cyclic.Int object. -//func (e *E2e) GetHistoricalDHPubkey() ([]byte, error) { -// return e.api.GetE2E().GetHistoricalDHPubkey().MarshalJSON() -//} -// -//// HasAuthenticatedChannel returns true if an authenticated channel with the -//// partner exists, otherwise returns false -//// Parameters: -//// - partnerId - the marshalled bytes of the id.ID object. -//func (e *E2e) HasAuthenticatedChannel(partnerId []byte) (bool, error) { -// partner, err := id.Unmarshal(partnerId) -// if err != nil { -// return false, err -// } -// return e.api.GetE2E().HasAuthenticatedChannel(partner), nil -//} -// -//// RemoveService removes all services for the given tag -//func (e *E2e) RemoveService(tag string) error { -// return e.api.GetE2E().RemoveService(tag) -//} -// -//// SendE2E send a message containing the payload to the -//// recipient of the passed message type, per the given -//// parameters - encrypted with end-to-end encryption. -//// Default parameters can be retrieved through -//// Parameters: -//// - recipientId - the marshalled bytes of the id.ID object. -//// - e2eParams - the marshalled bytes of the e2e.Params object. -//// Returns: -//// - []byte - the marshalled bytes of the E2ESendReport object. -//func (e *E2e) SendE2E(messageType int, recipientId, payload, -// e2eParams []byte) ([]byte, error) { -// -// params := e2e.GetDefaultParams() -// err := params.UnmarshalJSON(e2eParams) -// if err != nil { -// return nil, err -// } -// recipient, err := id.Unmarshal(recipientId) -// if err != nil { -// return nil, err -// } -// -// roundIds, messageId, ts, err := e.api.GetE2E().SendE2E(catalog.MessageType(messageType), recipient, payload, params) -// if err != nil { -// return nil, err -// } -// -// result := E2ESendReport{ -// RoundsList: makeRoundsList(roundIds), -// MessageID: messageId.Marshal(), -// Timestamp: ts.UnixNano(), -// } -// return json.Marshal(result) -//} -// -//// AddService adds a service for all partners of the given -//// tag, which will call back on the given processor. These can -//// be sent to using the tag fields in the Params Object -//// Passing nil for the processor allows you to create a -//// service which is never called but will be visible by -//// notifications. Processes added this way are generally not -//// end-to-end encrypted messages themselves, but other -//// protocols which piggyback on e2e relationships to start -//// communication -//func (e *E2e) AddService(tag string, processor Processor) error { -// return e.api.GetE2E().AddService(tag, &messageProcessor{bindingsCbs: processor}) -//} -// -//// Processor is the bindings-specific interface for message.Processor methods. -//type Processor interface { -// Process(message []byte, receptionId []byte, ephemeralId int64, roundId int64) -// fmt.Stringer -//} -// -//// messageProcessor implements Processor as a way of obtaining -//// a message.Processor over the bindings -//type messageProcessor struct { -// bindingsCbs Processor -//} -// -//// convertAuthCallbacks turns an auth.Callbacks into an AuthCallbacks -//func convertProcessor(msg format.Message, -// receptionID receptionID.EphemeralIdentity, -// round rounds.Round) (message []byte, receptionId []byte, ephemeralId int64, roundId int64) { -// -// message = msg.Marshal() -// receptionId = receptionID.Source.Marshal() -// ephemeralId = int64(receptionID.EphId.UInt64()) -// roundId = int64(round.ID) -// return -//} -// -//// Process decrypts and hands off the message to its internal down stream -//// message processing system. -//// CRITICAL: Fingerprints should never be used twice. Process must denote, -//// in long term storage, usage of a fingerprint and that fingerprint must -//// not be added again during application load. -//// It is a security vulnerability to reuse a fingerprint. It leaks privacy -//// and can lead to compromise of message contents and integrity. -//func (m *messageProcessor) Process(msg format.Message, -// receptionID receptionID.EphemeralIdentity, roundId rounds.Round) { -// m.bindingsCbs(convertProcessor(msg, receptionID, roundId)) -//} -// -//// Stringer interface for debugging -//func (m *messageProcessor) String() string { -// return m.bindingsCbs.String() -//} +// GetReceptionID returns the marshalled default IDs +// Returns: +// - []byte - the marshalled bytes of the id.ID object. +func (e *E2e) GetReceptionID() []byte { + return e.api.GetE2E().GetReceptionID().Marshal() +} + +// GetAllPartnerIDs returns a marshalled list of all partner IDs that the user has +// an E2E relationship with. +// Returns: +// - []byte - the marshalled bytes of the IdList object. +func (e *E2e) GetAllPartnerIDs() ([]byte, error) { + partnerIds := e.api.GetE2E().GetAllPartnerIDs() + convertedIds := make([][]byte, len(partnerIds)) + for i, partnerId := range partnerIds { + convertedIds[i] = partnerId.Marshal() + } + return json.Marshal(IdList{Ids: convertedIds}) +} + +// PayloadSize Returns the max payload size for a partitionable E2E +// message +func (e *E2e) PayloadSize() int { + return int(e.api.GetE2E().PayloadSize()) +} + +// SecondPartitionSize returns the max partition payload size for all +// payloads after the first payload +func (e *E2e) SecondPartitionSize() int { + return int(e.api.GetE2E().SecondPartitionSize()) +} + +// PartitionSize returns the partition payload size for the given +// payload index. The first payload is index 0. +func (e *E2e) PartitionSize(payloadIndex int) int { + return int(e.api.GetE2E().PartitionSize(uint(payloadIndex))) +} + +// FirstPartitionSize returns the max partition payload size for the +// first payload +func (e *E2e) FirstPartitionSize() int { + return int(e.api.GetE2E().FirstPartitionSize()) +} + +// GetHistoricalDHPrivkey returns the user's marshalled Historical DH Private Key +// Returns: +// - []byte - the marshalled bytes of the cyclic.Int object. +func (e *E2e) GetHistoricalDHPrivkey() ([]byte, error) { + return e.api.GetE2E().GetHistoricalDHPrivkey().MarshalJSON() +} + +// GetHistoricalDHPubkey returns the user's marshalled Historical DH +// Public Key +// Returns: +// - []byte - the marshalled bytes of the cyclic.Int object. +func (e *E2e) GetHistoricalDHPubkey() ([]byte, error) { + return e.api.GetE2E().GetHistoricalDHPubkey().MarshalJSON() +} + +// HasAuthenticatedChannel returns true if an authenticated channel with the +// partner exists, otherwise returns false +// Parameters: +// - partnerId - the marshalled bytes of the id.ID object. +func (e *E2e) HasAuthenticatedChannel(partnerId []byte) (bool, error) { + partner, err := id.Unmarshal(partnerId) + if err != nil { + return false, err + } + return e.api.GetE2E().HasAuthenticatedChannel(partner), nil +} + +// RemoveService removes all services for the given tag +func (e *E2e) RemoveService(tag string) error { + return e.api.GetE2E().RemoveService(tag) +} + +// SendE2E send a message containing the payload to the +// recipient of the passed message type, per the given +// parameters - encrypted with end-to-end encryption. +// Default parameters can be retrieved through +// Parameters: +// - recipientId - the marshalled bytes of the id.ID object. +// - e2eParams - the marshalled bytes of the e2e.Params object. +// Returns: +// - []byte - the marshalled bytes of the E2ESendReport object. +func (e *E2e) SendE2E(messageType int, recipientId, payload, + e2eParams []byte) ([]byte, error) { + + params := e2e.GetDefaultParams() + err := params.UnmarshalJSON(e2eParams) + if err != nil { + return nil, err + } + recipient, err := id.Unmarshal(recipientId) + if err != nil { + return nil, err + } + + roundIds, messageId, ts, err := e.api.GetE2E().SendE2E(catalog.MessageType(messageType), recipient, payload, params) + if err != nil { + return nil, err + } + + result := E2ESendReport{ + RoundsList: makeRoundsList(roundIds), + MessageID: messageId.Marshal(), + Timestamp: ts.UnixNano(), + } + return json.Marshal(result) +} + +// AddService adds a service for all partners of the given +// tag, which will call back on the given processor. These can +// be sent to using the tag fields in the Params Object +// Passing nil for the processor allows you to create a +// service which is never called but will be visible by +// notifications. Processes added this way are generally not +// end-to-end encrypted messages themselves, but other +// protocols which piggyback on e2e relationships to start +// communication +func (e *E2e) AddService(tag string, processor Processor) error { + return e.api.GetE2E().AddService(tag, &messageProcessor{bindingsCbs: processor}) +} + +// Processor is the bindings-specific interface for message.Processor methods. +type Processor interface { + Process(message []byte, receptionId []byte, ephemeralId int64, roundId int64) + fmt.Stringer +} + +// messageProcessor implements Processor as a way of obtaining +// a message.Processor over the bindings +type messageProcessor struct { + bindingsCbs Processor +} + +// convertAuthCallbacks turns an auth.Callbacks into an AuthCallbacks +func convertProcessor(msg format.Message, + receptionID receptionID.EphemeralIdentity, + round rounds.Round) (message []byte, receptionId []byte, ephemeralId int64, roundId int64) { + + message = msg.Marshal() + receptionId = receptionID.Source.Marshal() + ephemeralId = int64(receptionID.EphId.UInt64()) + roundId = int64(round.ID) + return +} + +// Process decrypts and hands off the message to its internal down stream +// message processing system. +// CRITICAL: Fingerprints should never be used twice. Process must denote, +// in long term storage, usage of a fingerprint and that fingerprint must +// not be added again during application load. +// It is a security vulnerability to reuse a fingerprint. It leaks privacy +// and can lead to compromise of message contents and integrity. +func (m *messageProcessor) Process(msg format.Message, + receptionID receptionID.EphemeralIdentity, roundId rounds.Round) { + m.bindingsCbs.Process(convertProcessor(msg, receptionID, roundId)) +} + +// Stringer interface for debugging +func (m *messageProcessor) String() string { + return m.bindingsCbs.String() +}