Skip to content
Snippets Groups Projects
Commit 933c3a26 authored by Josh Brooks's avatar Josh Brooks
Browse files

Clean up IdList in bindings

parent be881be5
No related branches found
No related tags found
2 merge requests!510Release,!302Restore BackupReport to original glory
......@@ -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,
}
......
......@@ -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.
......
......@@ -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 //
////////////////////////////////////////////////////////////////////////////////
// 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
// IdList is a wrapper for a list of marshalled id.ID objects.
type IdList struct {
Ids [][]byte
}
return storageObj.Data, nil
// 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()
}
return IdList{Ids: convertedIds}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment