diff --git a/bindings/e2e.go b/bindings/e2e.go index fbe8ceb3487c05eb951a854f16469007d25bd454..289de4218cad4a4fd485b3524826e0f702e05f3c 100644 --- a/bindings/e2e.go +++ b/bindings/e2e.go @@ -32,6 +32,7 @@ type E2e struct { id int } +// GetID returns the e2eTracker ID for the E2e object func (e *E2e) GetID() int { return e.id } @@ -64,6 +65,59 @@ func LoginE2e(cmixId int, callbacks AuthCallbacks, identity []byte) (*E2e, error return e2eTrackerSingleton.make(newE2e), nil } +// LoginE2eEphemeral creates and returns a new ephemeral E2e object and adds it to the e2eTrackerSingleton +// identity should be created via MakeIdentity() and passed in here +// If callbacks is left nil, a default auth.Callbacks will be used +func LoginE2eEphemeral(cmixId int, callbacks AuthCallbacks, identity []byte) (*E2e, error) { + cmix, err := cmixTrackerSingleton.get(cmixId) + if err != nil { + return nil, err + } + + newIdentity, err := unmarshalIdentity(identity, cmix.api.GetStorage().GetE2EGroup()) + if err != nil { + return nil, err + } + + var authCallbacks auth.Callbacks + if callbacks == nil { + authCallbacks = auth.DefaultAuthCallbacks{} + } else { + authCallbacks = &authCallback{bindingsCbs: callbacks} + } + + newE2e, err := xxdk.LoginEphemeral(cmix.api, authCallbacks, newIdentity) + if err != nil { + return nil, err + } + return e2eTrackerSingleton.make(newE2e), nil +} + +// LoginE2eLegacy creates a new E2e backed by the xxdk.Cmix persistent versioned.KV +// Uses the pre-generated transmission ID used by xxdk.Cmix +// If callbacks is left nil, a default auth.Callbacks will be used +// This function is designed to maintain backwards compatibility with previous xx messenger designs +// and should not be used for other purposes +func LoginE2eLegacy(cmixId int, callbacks AuthCallbacks) (*E2e, error) { + cmix, err := cmixTrackerSingleton.get(cmixId) + if err != nil { + return nil, err + } + + var authCallbacks auth.Callbacks + if callbacks == nil { + authCallbacks = auth.DefaultAuthCallbacks{} + } else { + authCallbacks = &authCallback{bindingsCbs: callbacks} + } + + newE2e, err := xxdk.LoginLegacy(cmix.api, authCallbacks) + if err != nil { + return nil, err + } + return e2eTrackerSingleton.make(newE2e), nil +} + // GetContact returns a marshalled contact.Contact object for the E2e ReceptionIdentity func (e *E2e) GetContact() []byte { return e.api.GetReceptionIdentity().GetContact(e.api.GetStorage().GetE2EGroup()).Marshal() diff --git a/bindings/e2eAuth.go b/bindings/e2eAuth.go index b974526cfc3556b2c1365e2fed74b361e910867a..b483671d90d3deb616bc0bd2e13018882e859ec1 100644 --- a/bindings/e2eAuth.go +++ b/bindings/e2eAuth.go @@ -8,7 +8,6 @@ package bindings import ( - "gitlab.com/elixxir/client/auth" "gitlab.com/elixxir/crypto/contact" "gitlab.com/elixxir/primitives/fact" "gitlab.com/xx_network/primitives/id" @@ -216,15 +215,7 @@ func (e *E2e) AddPartnerCallback(partnerID []byte, cb AuthCallbacks) error { return err } - var authCallbacks auth.Callbacks - if cb == nil { - authCallbacks = auth.DefaultAuthCallbacks{} - } else { - authCallbacks = &authCallback{bindingsCbs: cb} - } - - e.api.GetAuth().AddPartnerCallback(partnerId, authCallbacks) - + e.api.GetAuth().AddPartnerCallback(partnerId, &authCallback{bindingsCbs: cb}) return nil } diff --git a/bindings/e2eHandler.go b/bindings/e2eHandler.go new file mode 100644 index 0000000000000000000000000000000000000000..9ab0d62a2760cc90bca925efe8b68c39ad0649fd --- /dev/null +++ b/bindings/e2eHandler.go @@ -0,0 +1,111 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2022 Privategrity Corporation / +// / +// All rights reserved. / +//////////////////////////////////////////////////////////////////////////////// + +package bindings + +import ( + "encoding/json" + "gitlab.com/elixxir/client/catalog" + "gitlab.com/elixxir/client/e2e" + "gitlab.com/xx_network/primitives/id" +) + +// IdList is a wrapper for a list of marshalled id.ID objects +type IdList struct { + Ids [][]byte +} + +// GetReceptionID returns the marshalled default IDs +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. +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 +func (e *E2e) GetHistoricalDHPrivkey() ([]byte, error) { + return e.api.GetE2E().GetHistoricalDHPrivkey().MarshalJSON() +} + +// GetHistoricalDHPubkey returns the user's marshalled Historical DH +// Public Key +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 +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 +func (e *E2e) SendE2E(messageType int, recipientId, payload, + e2eParams []byte) ([]byte, []byte, int64, error) { + + params := e2e.GetDefaultParams() + err := params.UnmarshalJSON(e2eParams) + if err != nil { + return nil, nil, 0, err + } + recipient, err := id.Unmarshal(recipientId) + if err != nil { + return nil, nil, 0, err + } + + roundIds, messageId, ts, err := e.api.GetE2E().SendE2E(catalog.MessageType(messageType), recipient, payload, params) + if err != nil { + return nil, nil, 0, err + } + + convertedRids, err := json.Marshal(roundIds) + return convertedRids, messageId[:], ts.UnixNano(), err +} diff --git a/e2e/interface.go b/e2e/interface.go index 328220f419e33dfadb7fcf9455390eabb18a50e6..d9710650594926f528985ddf5f7aa1c41365d74c 100644 --- a/e2e/interface.go +++ b/e2e/interface.go @@ -22,7 +22,7 @@ type Handler interface { // 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. + // parameters - encrypted with end-to-end encryption. // Default parameters can be retrieved through // GetDefaultParams() // If too long, it will chunk a message up into its messages @@ -165,7 +165,7 @@ type Handler interface { /* === Utility ====================================================== */ - // GetGroup returns the cyclic group used for end to end encruption + // GetGroup returns the cyclic group used for end-to-end encryption GetGroup() *cyclic.Group // GetHistoricalDHPubkey returns the user's Historical DH