diff --git a/bindings/backup.go b/bindings/backup.go index 9934907d22d5a649039c471c85145a0bec2f6b6e..95418357db3fbc3829b90c209452ac42ce33ebea 100644 --- a/bindings/backup.go +++ b/bindings/backup.go @@ -68,15 +68,9 @@ func NewCmixFromBackup(ndfJSON, storageDir, backupPassphrase string, return nil, err } - // Serialize list of IDs into bytes - serializedIdList := make([][]byte, len(backupIdList)) - for i, partnerId := range backupIdList { - serializedIdList[i] = partnerId.Marshal() - } - // Construct report report := BackupReport{ - RestoredContacts: IdList{Ids: serializedIdList}, + RestoredContacts: makeIdList(backupIdList), Params: backupParams, } diff --git a/bindings/e2eHandler.go b/bindings/e2eHandler.go index 11b14e0e3888ae017973dd4010fb331c69daec6b..bfd439c133b468c4b0296ec09ae67b7f9045c169 100644 --- a/bindings/e2eHandler.go +++ b/bindings/e2eHandler.go @@ -19,11 +19,6 @@ import ( "gitlab.com/xx_network/primitives/id" ) -// IdList is a wrapper for a list of marshalled id.ID objects. -type IdList struct { - Ids [][]byte -} - // E2ESendReport is the bindings' representation of the return values of // SendE2E. // @@ -52,11 +47,7 @@ func (e *E2e) GetReceptionID() []byte { // - []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}) + return json.Marshal(makeIdList(partnerIds)) } // PayloadSize returns the max payload size for a partitionable E2E message. diff --git a/bindings/identity.go b/bindings/identity.go index 6ffd270664d9b16db77a2bbb3d21970379b4991c..bda224053da1dc8c557f48cfb6a577be9e00b8cd 100644 --- a/bindings/identity.go +++ b/bindings/identity.go @@ -9,12 +9,17 @@ package bindings import ( "encoding/json" + "gitlab.com/xx_network/primitives/id" "gitlab.com/elixxir/client/xxdk" "gitlab.com/elixxir/crypto/contact" "gitlab.com/elixxir/primitives/fact" ) +//////////////////////////////////////////////////////////////////////////////// +// ReceptionIdentity // +//////////////////////////////////////////////////////////////////////////////// + // ReceptionIdentity struct. // // JSON example: @@ -31,6 +36,37 @@ type ReceptionIdentity struct { DHKeyPrivate []byte // DH Private key } +// StoreReceptionIdentity stores the given identity in Cmix storage with the +// given key. This is the ideal way to securely store identities, as the caller +// of this function is only required to store the given key separately rather +// than the keying material. +func StoreReceptionIdentity(key string, identity []byte, cmixId int) error { + cmix, err := cmixTrackerSingleton.get(cmixId) + if err != nil { + return err + } + receptionIdentity, err := xxdk.UnmarshalReceptionIdentity(identity) + if err != nil { + return err + } + return xxdk.StoreReceptionIdentity(key, receptionIdentity, cmix.api) +} + +// LoadReceptionIdentity loads the given identity in Cmix storage with the given +// key. +func LoadReceptionIdentity(key string, cmixId int) ([]byte, error) { + cmix, err := cmixTrackerSingleton.get(cmixId) + if err != nil { + return nil, err + } + storageObj, err := cmix.api.GetStorage().Get(key) + if err != nil { + return nil, err + } + + return storageObj.Data, nil +} + // MakeReceptionIdentity generates a new cryptographic identity for receiving // messages. func (c *Cmix) MakeReceptionIdentity() ([]byte, error) { @@ -53,6 +89,10 @@ func (c *Cmix) MakeLegacyReceptionIdentity() ([]byte, error) { return ident.Marshal() } +//////////////////////////////////////////////////////////////////////////////// +// Contact Functions // +//////////////////////////////////////////////////////////////////////////////// + // GetIDFromContact accepts a marshalled contact.Contact object and returns a // marshalled id.ID object. func GetIDFromContact(marshaled []byte) ([]byte, error) { @@ -75,6 +115,10 @@ func GetPubkeyFromContact(marshaled []byte) ([]byte, error) { return json.Marshal(cnt.DhPubKey) } +//////////////////////////////////////////////////////////////////////////////// +// Fact Functions // +//////////////////////////////////////////////////////////////////////////////// + // Fact is an internal fact type for use in the bindings layer. // // JSON example: @@ -140,33 +184,23 @@ func GetFactsFromContact(marshaled []byte) ([]byte, error) { return factsListMarshaled, nil } -// StoreReceptionIdentity stores the given identity in Cmix storage with the -// given key. This is the ideal way to securely store identities, as the caller -// of this function is only required to store the given key separately rather -// than the keying material. -func StoreReceptionIdentity(key string, identity []byte, cmixId int) error { - cmix, err := cmixTrackerSingleton.get(cmixId) - if err != nil { - return err - } - receptionIdentity, err := xxdk.UnmarshalReceptionIdentity(identity) - if err != nil { - return err - } - return xxdk.StoreReceptionIdentity(key, receptionIdentity, cmix.api) +//////////////////////////////////////////////////////////////////////////////// +// IdList Functions // +//////////////////////////////////////////////////////////////////////////////// + +// IdList is a wrapper for a list of marshalled id.ID objects. +type IdList struct { + Ids [][]byte } -// LoadReceptionIdentity loads the given identity in Cmix storage with the given -// key. -func LoadReceptionIdentity(key string, cmixId int) ([]byte, error) { - cmix, err := cmixTrackerSingleton.get(cmixId) - if err != nil { - return nil, err +// makeIdList is a helper function which creates an IdList object +// given a list of id.ID's. It serializes each element of the +// given list of id.ID's, places that into a list of []byte's (ie [][]byte) +// and places that in the IdList. +func makeIdList(ids []*id.ID) IdList { + convertedIds := make([][]byte, len(ids)) + for i, partnerId := range ids { + convertedIds[i] = partnerId.Marshal() } - storageObj, err := cmix.api.GetStorage().Get(key) - if err != nil { - return nil, err - } - - return storageObj.Data, nil + return IdList{Ids: convertedIds} }