diff --git a/api/client.go b/api/client.go index 24d5ec94041a0b4aba3f07ef742ab13f21d75292..cabfb3240f642939e93c1f2609805038718a46b8 100644 --- a/api/client.go +++ b/api/client.go @@ -145,9 +145,9 @@ func NewPrecannedClient(precannedID uint, defJSON, storageDir string, password [ return nil } -// Login initalizes a client object from existing storage. -func Login(storageDir string, password []byte) (*Client, error) { - jww.INFO.Printf("Login()") +// OpenClient session, but don't connect to the network or log in +func OpenClient(storageDir string, password []byte) (*Client, error) { + jww.INFO.Printf("OpenClient()") // Use fastRNG for RNG ops (AES fortuna based RNG using system RNG) rngStreamGen := fastRNG.NewStreamGenerator(12, 3, csprng.NewSystemRNG) @@ -159,16 +159,9 @@ func Login(storageDir string, password []byte) (*Client, error) { return nil, err } - //execute the rest of the loading as normal - return loadClient(storageSess, rngStreamGen) -} - -// Login initalizes a client object from existing storage. -func loadClient(session *storage.Session, rngStreamGen *fastRNG.StreamGenerator) (c *Client, err error) { - // Set up a new context - c = &Client{ - storage: session, + c := &Client{ + storage: storageSess, switchboard: switchboard.New(), rng: rngStreamGen, comms: nil, @@ -177,6 +170,20 @@ func loadClient(session *storage.Session, rngStreamGen *fastRNG.StreamGenerator) status: newStatusTracker(), } + return c, nil +} + +// Login initalizes a client object from existing storage. +func Login(storageDir string, password []byte) (*Client, error) { + jww.INFO.Printf("Login()") + + c, err := OpenClient(storageDir, password) + + if err != nil { + return nil, err + } + + //execute the rest of the loading as normal c.services = newServiceProcessiesList(c.runner) //get the user from session @@ -193,7 +200,7 @@ func loadClient(session *storage.Session, rngStreamGen *fastRNG.StreamGenerator) } //get the NDF to pass into permissioning and the network manager - def := session.GetBaseNDF() + def := c.storage.GetBaseNDF() //initialize permissioning c.permissioning, err = permissioning.Init(c.comms, def) diff --git a/cmd/init.go b/cmd/init.go new file mode 100644 index 0000000000000000000000000000000000000000..19bde0e0e71f990674eb69d49414d3dd4f605b1a --- /dev/null +++ b/cmd/init.go @@ -0,0 +1,33 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +// Package cmd initializes the CLI and config parsers as well as the logger. +package cmd + +import ( + "fmt" + "github.com/spf13/cobra" + jww "github.com/spf13/jwalterweatherman" +) + +// initCmd creates a new user object with the given NDF +var initCmd = &cobra.Command{ + Use: "init", + Short: ("Initialize a user ID but do not connect to the network"), + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + client := createClient() + user := client.GetUser() + jww.INFO.Printf("User: %s", user.ID) + writeContact(user.GetContact()) + fmt.Printf("%s\n", user.ID) + }, +} + +func init() { + rootCmd.AddCommand(initCmd) +} diff --git a/cmd/root.go b/cmd/root.go index a08ad1d0c795b0b8d7bbbad6812fb8fbed84ebfc..b8b4818e0c41257e4a48ac577ff90b4525c13fea 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -181,7 +181,7 @@ var rootCmd = &cobra.Command{ }, } -func initClient() *api.Client { +func createClient() *api.Client { initLog(viper.GetBool("verbose"), viper.GetString("log")) jww.INFO.Printf(Version()) @@ -212,6 +212,19 @@ func initClient() *api.Client { } } + client, err := api.OpenClient(storeDir, []byte(pass)) + if err != nil { + jww.FATAL.Panicf("%+v", err) + } + return client +} + +func initClient() *api.Client { + createClient() + + pass := viper.GetString("password") + storeDir := viper.GetString("session") + //load the client client, err := api.Login(storeDir, []byte(pass)) if err != nil {