Skip to content
Snippets Groups Projects
Commit c9f683fa authored by Jake Taylor's avatar Jake Taylor :lips:
Browse files

Merge branch 'RefactorCmd' into 'release'

Refactor login logic

See merge request elixxir/user-discovery-bot!51
parents 2d160867 087e3d9c
No related branches found
No related tags found
2 merge requests!68Release,!51Refactor login logic
package cmd
import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
"gitlab.com/elixxir/client/storage/user"
"gitlab.com/elixxir/client/xxdk"
"gitlab.com/elixxir/comms/mixmessages"
"gitlab.com/elixxir/user-discovery-bot/banned"
......@@ -31,8 +33,6 @@ var (
sessionPass string
)
type manager struct{}
// RootCmd represents the base command when called without any sub-commands
var rootCmd = &cobra.Command{
Use: "UDB",
......@@ -113,29 +113,31 @@ var rootCmd = &cobra.Command{
break
}
// Pass NDF directly into client library
var client *xxdk.E2e
nwParams := xxdk.GetDefaultParams()
nwParams.CMix = nwParams.CMix.SetRealtimeOnlyAll()
var messenger *xxdk.E2e
cMixParams := xxdk.GetDefaultCMixParams()
cMixParams.Network = cMixParams.Network.SetRealtimeOnlyAll()
e2eParams := xxdk.GetDefaultE2EParams()
if p.SessionPath != "" && utils.Exists(p.SessionPath) {
client, err = xxdk.LoginWithNewBaseNDF_UNSAFE(p.SessionPath,
[]byte(sessionPass), string(returnedNdf.GetNdf()), nwParams)
// Construct a messenger using the NDF as a base
messenger, err = LoginWithNDF(p.SessionPath, []byte(sessionPass),
string(returnedNdf.GetNdf()),
cMixParams, e2eParams)
if err != nil {
jww.FATAL.Fatalf("Failed to create cMix client: %+v", err)
jww.FATAL.Fatalf("Failed to create messenger: %+v", err)
}
} else {
client, err = xxdk.LoginWithProtoClient(p.SessionPath,
[]byte(sessionPass), p.ProtoUserJson,
string(returnedNdf.GetNdf()), nil, nwParams)
messenger, err = LoginWithProto(p.SessionPath, []byte(sessionPass),
p.ProtoUserJson, string(returnedNdf.GetNdf()),
cMixParams, e2eParams)
if err != nil {
jww.FATAL.Fatalf("Failed to create cMix client: %+v", err)
jww.FATAL.Fatalf("Failed to create messenger: %+v", err)
}
}
m := cmix.NewManager(client, storage)
m := cmix.NewManager(messenger, storage)
m.Start()
err = client.StartNetworkFollower(5 * time.Second)
err = messenger.StartNetworkFollower(5 * time.Second)
if err != nil {
jww.FATAL.Fatal(err)
}
......@@ -149,6 +151,75 @@ var rootCmd = &cobra.Command{
},
}
// LoginWithProto is a login function which constructs an xxdk.E2e object
// using a user.Proto which has been JSON marshalled.
func LoginWithProto(statePath string, statePass []byte,
protoJson []byte, baseNdf string,
cmixParams xxdk.CMIXParams, e2eParams xxdk.E2EParams) (*xxdk.E2e, error) {
// Unmarshal the proto user JSON
protoUser := &user.Proto{}
err := json.Unmarshal(protoJson, protoUser)
if err != nil {
return nil, err
}
// Construct a network object
err = xxdk.NewProtoClient_Unsafe(baseNdf, statePath,
statePass, protoUser)
net, err := xxdk.LoadCmix(statePath,
statePass, cmixParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
// Store the updated base NDF
def, err := xxdk.ParseNDF(baseNdf)
if err != nil {
return nil, err
}
net.GetStorage().SetNDF(def)
// Create a legacy identity
identity, err := xxdk.MakeLegacyReceptionIdentity(net)
if err != nil {
return nil, err
}
// Create and return a messenger
return xxdk.Login(net, xxdk.DefaultAuthCallbacks{}, identity, e2eParams)
}
// LoginWithNDF is a login function which creates an
// xxdk.E2e instance using a base NDF.
func LoginWithNDF(statePath string, statePass []byte, baseNdf string,
cmixParams xxdk.CMIXParams, e2eParams xxdk.E2EParams) (*xxdk.E2e, error) {
jww.INFO.Printf("LoginWithNDF()")
// Construct a network object
net, err := xxdk.LoadCmix(statePath, statePass, cmixParams)
if err != nil {
return nil, err
}
// Store the base NDF
def, err := xxdk.ParseNDF(baseNdf)
if err != nil {
return nil, err
}
net.GetStorage().SetNDF(def)
// Create a legacy identity
identity, err := xxdk.MakeLegacyReceptionIdentity(net)
if err != nil {
return nil, err
}
// Create and return a messenger
return xxdk.Login(net, xxdk.DefaultAuthCallbacks{}, identity, e2eParams)
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
jww.ERROR.Println(err)
......
......@@ -9,13 +9,13 @@ require (
github.com/spf13/jwalterweatherman v1.1.0
github.com/spf13/viper v1.11.0
github.com/ttacon/libphonenumber v1.2.1
gitlab.com/elixxir/client v1.5.1-0.20220705185904-adfa37db8da3
gitlab.com/elixxir/client v1.5.1-0.20220713231458-50298ec6246b
gitlab.com/elixxir/comms v0.0.4-0.20220603231314-e47e4af13326
gitlab.com/elixxir/crypto v0.0.7-0.20220606201132-c370d5039cea
gitlab.com/elixxir/primitives v0.0.3-0.20220606195757-40f7a589347f
gitlab.com/xx_network/comms v0.0.4-0.20220630163702-f3d372ef6acd
gitlab.com/xx_network/crypto v0.0.5-0.20220606200528-3f886fe49e81
gitlab.com/xx_network/primitives v0.0.4-0.20220630163313-7890038258c6
gitlab.com/xx_network/primitives v0.0.4-0.20220712193914-aebd8544396e
gorm.io/driver/postgres v1.1.2
gorm.io/gorm v1.21.15
)
......
......@@ -544,6 +544,8 @@ gitlab.com/elixxir/client v1.5.1-0.20220701205550-b2d8e0808ea8 h1:J1EFbAcVh6sT1o
gitlab.com/elixxir/client v1.5.1-0.20220701205550-b2d8e0808ea8/go.mod h1:GCevkp2aa+vpIsSrlF/HsRlTjf107jgCglZ/0JKqW7E=
gitlab.com/elixxir/client v1.5.1-0.20220705185904-adfa37db8da3 h1:fpVxMkJh5jALHuaiWbNR4+MGmJwQ+CC7XPzsz90D/Ks=
gitlab.com/elixxir/client v1.5.1-0.20220705185904-adfa37db8da3/go.mod h1:GCevkp2aa+vpIsSrlF/HsRlTjf107jgCglZ/0JKqW7E=
gitlab.com/elixxir/client v1.5.1-0.20220713231458-50298ec6246b h1:UZYFghKFAfMwxmASRao0cMKVe1deVB7yVkWvcWlOu3c=
gitlab.com/elixxir/client v1.5.1-0.20220713231458-50298ec6246b/go.mod h1:dRCeFJrIj5zuxSfcnsAYOCodyPrKVparJUREBIWKRcw=
gitlab.com/elixxir/comms v0.0.4-0.20220603231314-e47e4af13326 h1:Zid8oNHtbOqF6ebrcGIccvIMabFNGh9dzY1b7mgIcF0=
gitlab.com/elixxir/comms v0.0.4-0.20220603231314-e47e4af13326/go.mod h1:tlHSrtSliKWUxsck8z/Ql/VJkMdSONV2BeWaUAAXzgk=
gitlab.com/elixxir/crypto v0.0.0-20200804182833-984246dea2c4/go.mod h1:ucm9SFKJo+K0N2GwRRpaNr+tKXMIOVWzmyUD0SbOu2c=
......@@ -581,6 +583,8 @@ gitlab.com/xx_network/primitives v0.0.4-0.20220324193139-b292d1ae6e7e h1:F651Ddb
gitlab.com/xx_network/primitives v0.0.4-0.20220324193139-b292d1ae6e7e/go.mod h1:AXVVFt7dDAeIUpOGPiStCcUIKsBXLWbmV/BgZ4T+tOo=
gitlab.com/xx_network/primitives v0.0.4-0.20220630163313-7890038258c6 h1:3It6ILDHn/9J/Oi7MfMjkidKPe7vbFCy5JQtXx8EfYM=
gitlab.com/xx_network/primitives v0.0.4-0.20220630163313-7890038258c6/go.mod h1:AXVVFt7dDAeIUpOGPiStCcUIKsBXLWbmV/BgZ4T+tOo=
gitlab.com/xx_network/primitives v0.0.4-0.20220712193914-aebd8544396e h1:zRRo/v3KUo3MtpjNJaB03LR+Zi2g1afYF8yKWl1t19o=
gitlab.com/xx_network/primitives v0.0.4-0.20220712193914-aebd8544396e/go.mod h1:AXVVFt7dDAeIUpOGPiStCcUIKsBXLWbmV/BgZ4T+tOo=
gitlab.com/xx_network/ring v0.0.3-0.20220222211904-da613960ad93 h1:eJZrXqHsMmmejEPWw8gNAt0I8CGAMNO/7C339Zco3TM=
gitlab.com/xx_network/ring v0.0.3-0.20220222211904-da613960ad93/go.mod h1:aLzpP2TiZTQut/PVHR40EJAomzugDdHXetbieRClXIM=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment