From 3d0720e312f45a78a9fc0c23a24172d4316bd6a3 Mon Sep 17 00:00:00 2001
From: Tigran Avakyan <tigran.avakyan@icloud.com>
Date: Tue, 9 Apr 2019 10:39:27 -0700
Subject: [PATCH] Made changes to pass in group as JSON

---
 bindings/client.go      | 10 +++++++++-
 bindings/client_test.go | 19 ++++++++++++++++---
 cmd/root.go             | 11 ++---------
 3 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/bindings/client.go b/bindings/client.go
index 0fa0b1c4d..cf1dce64e 100644
--- a/bindings/client.go
+++ b/bindings/client.go
@@ -14,6 +14,7 @@ import (
 	"gitlab.com/elixxir/client/user"
 	"gitlab.com/elixxir/crypto/certs"
 	"gitlab.com/elixxir/crypto/cyclic"
+	"gitlab.com/elixxir/crypto/large"
 	"gitlab.com/elixxir/primitives/format"
 	"gitlab.com/elixxir/primitives/id"
 	"gitlab.com/elixxir/primitives/switchboard"
@@ -146,12 +147,19 @@ func InitClient(storage Storage, loc string) error {
 // “Jono”
 // OIF3OJ5I
 func Register(registrationCode string, gwAddr string, numNodes int,
-	mint bool, grp *cyclic.Group) ([]byte, error) {
+	mint bool, grpJSON string) ([]byte, error) {
 
 	if numNodes < 1 {
 		return id.ZeroID[:], errors.New("invalid number of nodes")
 	}
 
+	// Unmarshal group JSON
+	grp := cyclic.NewGroup(large.NewMaxInt(), large.NewMaxInt(), large.NewMaxInt())
+	err := grp.UnmarshalJSON([]byte(grpJSON))
+	if err != nil {
+		return id.ZeroID[:], err
+	}
+
 	UID, err := api.Register(registrationCode, gwAddr, uint(numNodes), mint, grp)
 
 	if err != nil {
diff --git a/bindings/client_test.go b/bindings/client_test.go
index d27994016..341d38802 100644
--- a/bindings/client_test.go
+++ b/bindings/client_test.go
@@ -147,8 +147,12 @@ func TestRegister(t *testing.T) {
 	g := large.NewInt(2)
 	q := large.NewInt(3)
 	grp := cyclic.NewGroup(p, g, q)
+	grpJSON, err := grp.MarshalJSON()
+	if err != nil {
+		t.Errorf("Failed to marshal group JSON: %s", err)
+	}
 
-	regRes, err := Register(registrationCode, gwAddress, 1, false, grp)
+	regRes, err := Register(registrationCode, gwAddress, 1, false, string(grpJSON))
 	if err != nil {
 		t.Errorf("Registration failed: %s", err.Error())
 	}
@@ -170,8 +174,12 @@ func TestRegisterBadNumNodes(t *testing.T) {
 	g := large.NewInt(int64(2))
 	q := large.NewInt(int64(3))
 	grp := cyclic.NewGroup(p, g, q)
+	grpJSON, err := grp.MarshalJSON()
+	if err != nil {
+		t.Errorf("Failed to marshal group JSON: %s", err)
+	}
 
-	_, err = Register(registrationCode, gwAddress, 0, false, grp)
+	_, err = Register(registrationCode, gwAddress, 0, false, string(grpJSON))
 	if err == nil {
 		t.Errorf("Registration worked with bad numnodes! %s", err.Error())
 	}
@@ -202,8 +210,12 @@ func TestLoginLogout(t *testing.T) {
 	g := large.NewInt(2)
 	q := large.NewInt(3)
 	grp := cyclic.NewGroup(p, g, q)
+	grpJSON, err := grp.MarshalJSON()
+	if err != nil {
+		t.Errorf("Failed to marshal group JSON: %s", err)
+	}
 
-	regRes, err := Register(registrationCode, gwAddress, 1, false, grp)
+	regRes, err := Register(registrationCode, gwAddress, 1, false, string(grpJSON))
 	loginRes, err2 := Login(regRes, gwAddress, "")
 	if err2 != nil {
 		t.Errorf("Login failed: %s", err.Error())
@@ -216,6 +228,7 @@ func TestLoginLogout(t *testing.T) {
 	if err3 != nil {
 		t.Errorf("Logoutfailed: %s", err.Error())
 	}
+
 	globals.LocalStorage = nil
 }
 
diff --git a/cmd/root.go b/cmd/root.go
index 0fe6b3d0b..09f44335b 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -22,7 +22,6 @@ import (
 	"gitlab.com/elixxir/client/parse"
 	"gitlab.com/elixxir/client/user"
 	"gitlab.com/elixxir/comms/connect"
-	"gitlab.com/elixxir/crypto/cyclic"
 	"gitlab.com/elixxir/primitives/format"
 	"gitlab.com/elixxir/primitives/id"
 	"gitlab.com/elixxir/primitives/switchboard"
@@ -122,16 +121,10 @@ func sessionInitialization() {
 		// FIXME Use a different encoding for the user ID command line argument,
 		// to allow testing with IDs that are long enough to exercise more than
 		// 64 bits
-		grp := cyclic.Group{}
-		grpBuff := []byte(viper.GetString("group"))
-		err := grp.UnmarshalJSON(grpBuff)
-		if err != nil {
-			fmt.Printf("Could Not Decode group from JSON: %s\n", err.Error())
-			return
-		}
+		grpJSON := viper.GetString("group")
 
 		regCode := new(id.User).SetUints(&[4]uint64{0, 0, 0, userId}).RegistrationCode()
-		_, err = bindings.Register(regCode, gwAddr, int(numNodes), mint, &grp)
+		_, err = bindings.Register(regCode, gwAddr, int(numNodes), mint, grpJSON)
 		if err != nil {
 			fmt.Printf("Could Not Register User: %s\n", err.Error())
 			return
-- 
GitLab