Skip to content
Snippets Groups Projects
Commit 0727aad4 authored by Benjamin Wenger's avatar Benjamin Wenger
Browse files

finished new bindings

parent 346eddd8
No related branches found
No related tags found
3 merge requests!510Release,!226WIP: Api2.0,!207WIP: Client Restructure
...@@ -28,7 +28,7 @@ func (c *Client) ConnectWithAuthentication(recipientContact []byte, myIdentity [ ...@@ -28,7 +28,7 @@ func (c *Client) ConnectWithAuthentication(recipientContact []byte, myIdentity [
if err != nil { if err != nil {
return nil, err return nil, err
} }
myID, rsaPriv, salt, myDHPriv, err := unmarshalIdentity(myIdentity) myID, rsaPriv, salt, myDHPriv, err := c.unmarshalIdentity(myIdentity)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -35,7 +35,7 @@ func (c *Client) Connect(recipientContact []byte, myIdentity []byte) ( ...@@ -35,7 +35,7 @@ func (c *Client) Connect(recipientContact []byte, myIdentity []byte) (
if err != nil { if err != nil {
return nil, err return nil, err
} }
myID, _, _, myDHPriv, err := unmarshalIdentity(myIdentity) myID, _, _, myDHPriv, err := c.unmarshalIdentity(myIdentity)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -2,8 +2,10 @@ package bindings ...@@ -2,8 +2,10 @@ package bindings
import ( import (
"encoding/json" "encoding/json"
"gitlab.com/elixxir/crypto/contact"
"gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/elixxir/crypto/diffieHellman" "gitlab.com/elixxir/crypto/diffieHellman"
"gitlab.com/elixxir/primitives/fact"
"gitlab.com/xx_network/crypto/signature/rsa" "gitlab.com/xx_network/crypto/signature/rsa"
"gitlab.com/xx_network/crypto/xx" "gitlab.com/xx_network/crypto/xx"
"gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/id"
...@@ -16,11 +18,6 @@ type Identity struct { ...@@ -16,11 +18,6 @@ type Identity struct {
DHKeyPrivate []byte DHKeyPrivate []byte
} }
type Fact struct {
Fact string
Type string
}
// MakeIdentity generates a new cryptographic identity for receving // MakeIdentity generates a new cryptographic identity for receving
// messages // messages
func (c *Client) MakeIdentity() ([]byte, error) { func (c *Client) MakeIdentity() ([]byte, error) {
...@@ -46,7 +43,11 @@ func (c *Client) MakeIdentity() ([]byte, error) { ...@@ -46,7 +43,11 @@ func (c *Client) MakeIdentity() ([]byte, error) {
//make the ID //make the ID
id, err := xx.NewID(rsaKey.GetPublic(), id, err := xx.NewID(rsaKey.GetPublic(),
salt, id.User) salt, id.User)
if err != nil {
return nil, err
}
dhPrivJson, err := privkey.MarshalJSON()
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -56,35 +57,125 @@ func (c *Client) MakeIdentity() ([]byte, error) { ...@@ -56,35 +57,125 @@ func (c *Client) MakeIdentity() ([]byte, error) {
ID: id.Marshal(), ID: id.Marshal(),
RSAPrivatePem: rsa.CreatePrivateKeyPem(rsaKey), RSAPrivatePem: rsa.CreatePrivateKeyPem(rsaKey),
Salt: salt, Salt: salt,
DHKeyPrivate: privkey.Bytes(), DHKeyPrivate: dhPrivJson,
} }
return json.Marshal(&I) return json.Marshal(&I)
} }
func GetContactFromIdentity(identity string) []byte { func (c *Client) GetContactFromIdentity(identity []byte) ([]byte, error) {
I := Identity{} uID, _, _, dhKey, err := c.unmarshalIdentity(identity)
if err != nil {
return nil, err
}
grp := c.api.GetStorage().GetE2EGroup()
dhPub := grp.ExpG(dhKey, grp.NewInt(1))
ct := contact.Contact{
ID: uID,
DhPubKey: dhPub,
OwnershipProof: nil,
Facts: nil,
}
return ct.Marshal(), nil
} }
func unmarshalIdentity(marshaled []byte) (*id.ID, *rsa.PrivateKey, []byte, func (c *Client) unmarshalIdentity(marshaled []byte) (*id.ID, *rsa.PrivateKey, []byte,
*cyclic.Int, error) { *cyclic.Int, error) {
return nil, nil, nil, nil, nil I := Identity{}
err := json.Unmarshal(marshaled, &I)
if err != nil {
return nil, nil, nil, nil, err
}
uID, err := id.Unmarshal(I.ID)
if err != nil {
return nil, nil, nil, nil, err
}
dhkey := c.api.GetStorage().GetE2EGroup().NewInt(1)
err = dhkey.UnmarshalJSON(I.DHKeyPrivate)
if err != nil {
return nil, nil, nil, nil, err
}
rsaPriv, err := rsa.LoadPrivateKeyFromPem(I.RSAPrivatePem)
if err != nil {
return nil, nil, nil, nil, err
}
return uID, rsaPriv, I.Salt, dhkey, nil
} }
// SetFactsOnContact replaces the facts on the contact with the passed in facts func GetIDFromContact(marshaled []byte) ([]byte, error) {
// pass in empty facts in order to clear the facts cnt, err := contact.Unmarshal(marshaled)
func SetFactsOnContact(contact []byte, facts []byte) []byte { if err != nil {
I := Identity{} return nil, err
}
return cnt.ID.Marshal(), nil
} }
func GetIDFromContact(contact []byte) []byte { func GetPubkeyFromContact(marshaled []byte) ([]byte, error) {
cnt, err := contact.Unmarshal(marshaled)
if err != nil {
return nil, err
}
return cnt.ID.Marshal(), nil
} }
func GetPubkeyFromContact(contact []byte) []byte { type Fact struct {
Fact string
Type int
}
// SetFactsOnContact replaces the facts on the contact with the passed in facts
// pass in empty facts in order to clear the facts
func SetFactsOnContact(marshaled []byte, facts []byte) ([]byte, error) {
cnt, err := contact.Unmarshal(marshaled)
if err != nil {
return nil, err
}
factsList := make([]Fact, 0)
err = json.Unmarshal(facts, &factsList)
if err != nil {
return nil, err
}
realFactList := make(fact.FactList, 0, len(factsList))
for i := range factsList {
realFactList = append(realFactList, fact.Fact{
Fact: factsList[i].Fact,
T: fact.FactType(factsList[i].Type),
})
}
cnt.Facts = realFactList
return cnt.Marshal(), nil
} }
func GetFactsFromContact(contact []byte) []byte { func GetFactsFromContact(marshaled []byte) ([]byte, error) {
cnt, err := contact.Unmarshal(marshaled)
if err != nil {
return nil, err
}
factsList := make([]Fact, len(cnt.Facts))
for i := range cnt.Facts {
factsList = append(factsList, Fact{
Fact: cnt.Facts[i].Fact,
Type: int(cnt.Facts[i].T),
})
}
factsListMarshaled, err := json.Marshal(&factsList)
if err != nil {
return nil, err
}
return factsListMarshaled, nil
} }
package bindings package bindings
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/cmix" "gitlab.com/elixxir/client/cmix"
"gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/id"
"time" "time"
) )
type roundsList struct { type roundsList []int
rounds []int
func (rl roundsList) Marshal() ([]byte, error) {
return json.Marshal(&rl)
} }
func (rl roundsList) Marshal() []byte { func unmarshalRoundsList(marshaled []byte) ([]id.Round, error) {
rl := roundsList{}
err := json.Unmarshal(marshaled, &rl)
if err != nil {
return nil, err
}
} realRl := make([]id.Round, len(rl))
func unmarshalRoundsList(marshaled []byte) []id.Round { for _, rid := range rl {
realRl = append(realRl, id.Round(rid))
}
return realRl, nil
} }
func makeRoundsList(rounds []id.Round) roundsList { func makeRoundsList(rounds []id.Round) roundsList {
rl := make(roundsList, 0, len(rounds))
for _, rid := range rounds {
rl = append(rl, int(rid))
}
return rl
}
// MessageDeliveryCallback gets called on the determination if all events
// related to a message send were successful.
type MessageDeliveryCallback interface {
EventCallback(delivered, timedOut bool, roundResults []byte)
} }
// WaitForMessageDelivery allows the caller to get notified if the rounds a // WaitForMessageDelivery allows the caller to get notified if the rounds a
...@@ -34,39 +57,39 @@ func makeRoundsList(rounds []id.Round) roundsList { ...@@ -34,39 +57,39 @@ func makeRoundsList(rounds []id.Round) roundsList {
// This function takes the marshaled send report to ensure a memory leak does // This function takes the marshaled send report to ensure a memory leak does
// not occur as a result of both sides of the bindings holding a reference to // not occur as a result of both sides of the bindings holding a reference to
// the same pointer. // the same pointer.
func (c *Client) WaitForMessageDelivery(marshaledSendReport []byte, func (c *Client) WaitForMessageDelivery(roundList []byte,
mdc MessageDeliveryCallback, timeoutMS int) error { mdc MessageDeliveryCallback, timeoutMS int) error {
jww.INFO.Printf("WaitForMessageDelivery(%v, _, %v)", jww.INFO.Printf("WaitForMessageDelivery(%v, _, %v)",
marshaledSendReport, timeoutMS) roundList, timeoutMS)
sr, err := UnmarshalSendReport(marshaledSendReport) rl, err := unmarshalRoundsList(roundList)
if err != nil { if err != nil {
return errors.New(fmt.Sprintf("Failed to "+ return errors.New(fmt.Sprintf("Failed to "+
"WaitForMessageDelivery callback due to bad Send Report: %+v", err)) "WaitForMessageDelivery callback due to bad Send Report: %+v", err))
} }
if sr == nil || sr.rl == nil || len(sr.rl.list) == 0 { if rl == nil || len(rl) == 0 {
return errors.New(fmt.Sprintf("Failed to "+ return errors.New(fmt.Sprintf("Failed to "+
"WaitForMessageDelivery callback due to invalid Send Report "+ "WaitForMessageDelivery callback due to invalid Send Report "+
"unmarshal: %s", string(marshaledSendReport))) "unmarshal: %s", string(roundList)))
} }
f := func(allRoundsSucceeded, timedOut bool, rounds map[id.Round]cmix.RoundLookupStatus) { f := func(allRoundsSucceeded, timedOut bool, rounds map[id.Round]cmix.RoundResult) {
results := make([]byte, len(sr.rl.list)) results := make([]byte, len(rl))
jww.INFO.Printf("Processing WaitForMessageDelivery report "+ jww.INFO.Printf("Processing WaitForMessageDelivery report "+
"for %v, success: %v, timedout: %v", sr.mid, allRoundsSucceeded, "success: %v, timedout: %v", allRoundsSucceeded,
timedOut) timedOut)
for i, r := range sr.rl.list { for i, r := range rl {
if result, exists := rounds[r]; exists { if result, exists := rounds[r]; exists {
results[i] = byte(result) results[i] = byte(result.Status)
} }
} }
mdc.EventCallback(sr.mid.Marshal(), allRoundsSucceeded, timedOut, results) mdc.EventCallback(allRoundsSucceeded, timedOut, results)
} }
timeout := time.Duration(timeoutMS) * time.Millisecond timeout := time.Duration(timeoutMS) * time.Millisecond
err = c.api.GetRoundResults(sr.rl.list, timeout, f) err = c.api.GetCmix().GetRoundResults(timeout, f, rl...)
return err return err
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment