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

Refactor cmd to use singular code paths for all initialization routes

parent 5b095fc0
No related branches found
No related tags found
2 merge requests!510Release,!269Hotfix/split cmd
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package cmd
import (
"encoding/json"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
"gitlab.com/elixxir/client/backup"
"gitlab.com/elixxir/client/xxdk"
backupCrypto "gitlab.com/elixxir/crypto/backup"
"gitlab.com/xx_network/primitives/utils"
"io/fs"
"io/ioutil"
"os"
)
// loadOrInitBackup will build a new xxdk.E2e from existing storage
// or from a new storage that it will create if none already exists
func loadOrInitBackup(backupPath string, backupPass string, password []byte, storeDir string,
cmixParams xxdk.CMIXParams, e2eParams xxdk.E2EParams) *xxdk.E2e {
// create a new client if none exist
var baseClient *xxdk.Cmix
var identity xxdk.ReceptionIdentity
if _, err := os.Stat(storeDir); errors.Is(err, fs.ErrNotExist) {
// Initialize from scratch
ndfJson, err := ioutil.ReadFile(viper.GetString("ndf"))
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
b, backupFile := loadBackup(backupPath, backupPass)
// Marshal the backup object in JSON
backupJson, err := json.Marshal(b)
if err != nil {
jww.FATAL.Panicf("Failed to JSON Marshal backup: %+v", err)
}
// Write the backup JSON to file
err = utils.WriteFileDef(viper.GetString("backupJsonOut"), backupJson)
if err != nil {
jww.FATAL.Panicf("Failed to write backup to file: %+v", err)
}
// Construct client from backup data
backupIdList, _, err := backup.NewClientFromBackup(string(ndfJson), storeDir,
password, []byte(backupPass), backupFile)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
backupIdListPath := viper.GetString("backupIdList")
if backupIdListPath != "" {
// Marshal backed up ID list to JSON
backedUpIdListJson, err := json.Marshal(backupIdList)
if err != nil {
jww.FATAL.Panicf("Failed to JSON Marshal backed up IDs: %+v", err)
}
// Write backed up ID list to file
err = utils.WriteFileDef(backupIdListPath, backedUpIdListJson)
if err != nil {
jww.FATAL.Panicf("Failed to write backed up IDs to file %q: %+v",
backupIdListPath, err)
}
}
baseClient, err = xxdk.LoadCmix(storeDir, password, cmixParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
// TODO: Get proper identity
identity, err = xxdk.MakeReceptionIdentity(baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
err = xxdk.StoreReceptionIdentity(identityStorageKey, identity, baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
} else {
// Initialize from storage
baseClient, err = xxdk.LoadCmix(storeDir, password, cmixParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
identity, err = xxdk.LoadReceptionIdentity(identityStorageKey, baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
}
jww.INFO.Printf("Using LoginLegacy for precan sender")
client, err := xxdk.LoginLegacy(baseClient, e2eParams, authCbs)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
return client
}
func loadBackup(backupPath, backupPass string) (backupCrypto.Backup, []byte) {
jww.INFO.Printf("Loading backup from path %q", backupPath)
backupFile, err := utils.ReadFile(backupPath)
if err != nil {
jww.FATAL.Panicf("%v", err)
}
var b backupCrypto.Backup
err = b.Decrypt(backupPass, backupFile)
if err != nil {
jww.FATAL.Panicf("Failed to decrypt backup: %+v", err)
}
return b, backupFile
}
......@@ -9,9 +9,14 @@
package cmd
import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
"gitlab.com/elixxir/client/xxdk"
"io/fs"
"io/ioutil"
"os"
)
// initCmd creates a new user object with the given NDF
......@@ -20,10 +25,35 @@ var initCmd = &cobra.Command{
Short: "Initialize a user ID but do not connect to the network",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
_, receptionIdentity := initCmix()
// TODO: Handle userid-prefix argument
storePassword := parsePassword(viper.GetString("password"))
storeDir := viper.GetString("session")
regCode := viper.GetString("regcode")
jww.INFO.Printf("User: %s", receptionIdentity.ID)
writeContact(receptionIdentity.GetContact())
// Initialize from scratch
ndfJson, err := ioutil.ReadFile(viper.GetString("ndf"))
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
err = xxdk.NewCmix(string(ndfJson), storeDir, storePassword, regCode)
baseClient, err := xxdk.OpenCmix(storeDir, storePassword)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
identity, err := xxdk.MakeReceptionIdentity(baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
err = xxdk.StoreReceptionIdentity(identityStorageKey, identity, baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
jww.INFO.Printf("User: %s", identity.ID)
writeContact(identity.GetContact())
},
}
......@@ -34,3 +64,105 @@ func init() {
rootCmd.AddCommand(initCmd)
}
// loadOrInitClient will build a new xxdk.E2e from existing storage
// or from a new storage that it will create if none already exists
func loadOrInitClient(password []byte, storeDir, regCode string,
cmixParams xxdk.CMIXParams, e2eParams xxdk.E2EParams) *xxdk.E2e {
// create a new client if none exist
var baseClient *xxdk.Cmix
var identity xxdk.ReceptionIdentity
if _, err := os.Stat(storeDir); errors.Is(err, fs.ErrNotExist) {
// Initialize from scratch
ndfJson, err := ioutil.ReadFile(viper.GetString("ndf"))
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
err = xxdk.NewCmix(string(ndfJson), storeDir, password, regCode)
baseClient, err = xxdk.LoadCmix(storeDir, password, cmixParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
identity, err = xxdk.MakeReceptionIdentity(baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
err = xxdk.StoreReceptionIdentity(identityStorageKey, identity, baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
} else {
// Initialize from storage
baseClient, err = xxdk.LoadCmix(storeDir, password, cmixParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
identity, err = xxdk.LoadReceptionIdentity(identityStorageKey, baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
}
jww.INFO.Printf("Using Login for normal sender")
client, err := xxdk.Login(baseClient, authCbs, identity, e2eParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
return client
}
// loadOrInitVanity will build a new xxdk.E2e from existing storage
// or from a new storage that it will create if none already exists
func loadOrInitVanity(password []byte, storeDir, regCode, userIdPrefix string,
cmixParams xxdk.CMIXParams, e2eParams xxdk.E2EParams) *xxdk.E2e {
// create a new client if none exist
var baseClient *xxdk.Cmix
var identity xxdk.ReceptionIdentity
if _, err := os.Stat(storeDir); errors.Is(err, fs.ErrNotExist) {
// Initialize precan from scratch
ndfJson, err := ioutil.ReadFile(viper.GetString("ndf"))
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
err = xxdk.NewVanityClient(string(ndfJson), storeDir,
password, regCode, userIdPrefix)
baseClient, err = xxdk.LoadCmix(storeDir, password, cmixParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
// TODO: Get proper identity
identity, err = xxdk.MakeReceptionIdentity(baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
err = xxdk.StoreReceptionIdentity(identityStorageKey, identity, baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
} else {
// Initialize precan from storage
baseClient, err = xxdk.LoadCmix(storeDir, password, cmixParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
identity, err = xxdk.LoadReceptionIdentity(identityStorageKey, baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
}
jww.INFO.Printf("Using LoginLegacy for vanity sender")
client, err := xxdk.LoginLegacy(baseClient, e2eParams, authCbs)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
return client
}
......@@ -10,15 +10,63 @@
package cmd
import (
"encoding/binary"
"gitlab.com/elixxir/client/xxdk"
"strconv"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
"gitlab.com/elixxir/client/xxdk"
"gitlab.com/elixxir/crypto/contact"
"gitlab.com/xx_network/primitives/id"
"io/fs"
"io/ioutil"
"os"
)
// loadOrInitPrecan will build a new xxdk.E2e from existing storage
// or from a new storage that it will create if none already exists
func loadOrInitPrecan(precanId uint, password []byte, storeDir string,
cmixParams xxdk.CMIXParams, e2eParams xxdk.E2EParams) *xxdk.E2e {
// create a new client if none exist
var baseClient *xxdk.Cmix
var identity xxdk.ReceptionIdentity
if _, err := os.Stat(storeDir); errors.Is(err, fs.ErrNotExist) {
// Initialize from scratch
ndfJson, err := ioutil.ReadFile(viper.GetString("ndf"))
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
// TODO: Separate identity from this call
identity, err = xxdk.NewPrecannedClient(precanId, string(ndfJson), storeDir, password)
baseClient, err = xxdk.LoadCmix(storeDir, password, cmixParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
err = xxdk.StoreReceptionIdentity(identityStorageKey, identity, baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
} else {
// Initialize from storage
baseClient, err = xxdk.LoadCmix(storeDir, password, cmixParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
identity, err = xxdk.LoadReceptionIdentity(identityStorageKey, baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
}
jww.INFO.Printf("Using Login for precan sender")
client, err := xxdk.Login(baseClient, authCbs, identity, e2eParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
return client
}
func isPrecanID(id *id.ID) bool {
// check if precanned
rBytes := id.Bytes()
......@@ -33,36 +81,6 @@ func isPrecanID(id *id.ID) bool {
return false
}
// returns a simple numerical id if the user is a precanned user, otherwise
// returns the normal string of the userID
func printIDNice(uid *id.ID) string {
for index, puid := range precannedIDList {
if uid.Cmp(puid) {
return strconv.Itoa(index + 1)
}
}
return uid.String()
}
// build a list of precanned ids to use for comparision for nicer user id output
var precannedIDList = buildPrecannedIDList()
func buildPrecannedIDList() []*id.ID {
idList := make([]*id.ID, 40)
for i := 0; i < 40; i++ {
uid := new(id.ID)
binary.BigEndian.PutUint64(uid[:], uint64(i+1))
uid.SetType(id.User)
idList[i] = uid
}
return idList
}
func getPrecanID(recipientID *id.ID) uint {
return uint(recipientID.Bytes()[7])
}
......
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package cmd
import (
"encoding/json"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
"gitlab.com/elixxir/client/storage/user"
"gitlab.com/elixxir/client/xxdk"
"gitlab.com/xx_network/primitives/utils"
"io/fs"
"io/ioutil"
"os"
)
// loadOrInitProto will build a new xxdk.E2e from existing storage
// or from a new storage that it will create if none already exists
func loadOrInitProto(protoUserPath string, password []byte, storeDir string,
cmixParams xxdk.CMIXParams, e2eParams xxdk.E2EParams) *xxdk.E2e {
// create a new client if none exist
var baseClient *xxdk.Cmix
var identity xxdk.ReceptionIdentity
if _, err := os.Stat(storeDir); errors.Is(err, fs.ErrNotExist) {
// Initialize from scratch
ndfJson, err := ioutil.ReadFile(viper.GetString("ndf"))
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
protoUserJson, err := utils.ReadFile(protoUserPath)
if err != nil {
jww.FATAL.Panicf("%v", err)
}
protoUser := &user.Proto{}
err = json.Unmarshal(protoUserJson, protoUser)
if err != nil {
jww.FATAL.Panicf("%v", err)
}
// TODO: Separate identity from this call
identity, err = xxdk.NewProtoClient_Unsafe(string(ndfJson), storeDir,
password, protoUser)
baseClient, err = xxdk.LoadCmix(storeDir, password, cmixParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
err = xxdk.StoreReceptionIdentity(identityStorageKey, identity, baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
} else {
// Initialize from storage
baseClient, err = xxdk.LoadCmix(storeDir, password, cmixParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
identity, err = xxdk.LoadReceptionIdentity(identityStorageKey, baseClient)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
}
jww.INFO.Printf("Using LoginLegacy for proto sender")
client, err := xxdk.LoginLegacy(baseClient, e2eParams, authCbs)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
return client
}
......@@ -13,9 +13,7 @@ import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
......@@ -25,8 +23,6 @@ import (
"sync"
"time"
"gitlab.com/elixxir/client/storage/user"
"gitlab.com/elixxir/client/backup"
"gitlab.com/elixxir/client/xxdk"
......@@ -510,10 +506,10 @@ var rootCmd = &cobra.Command{
// wait an extra 5 seconds to make sure no messages were missed
done = false
waitTime := time.Duration(5 * time.Second)
waitTime := 5 * time.Second
if expectedCnt == 0 {
// Wait longer if we didn't expect to receive anything
waitTime = time.Duration(15 * time.Second)
waitTime = 15 * time.Second
}
timer := time.NewTimer(waitTime)
for !done {
......@@ -548,130 +544,6 @@ var rootCmd = &cobra.Command{
},
}
// initCmix returns a newly-initialized xxdk.Cmix object and its stored xxdk.ReceptionIdentity
func initCmix() (*xxdk.Cmix, xxdk.ReceptionIdentity) {
logLevel := viper.GetUint("logLevel")
initLog(logLevel, viper.GetString("log"))
jww.INFO.Printf(Version())
pass := parsePassword(viper.GetString("password"))
storeDir := viper.GetString("session")
regCode := viper.GetString("regcode")
precannedID := viper.GetUint("sendid")
userIDprefix := viper.GetString("userid-prefix")
protoUserPath := viper.GetString("protoUserPath")
backupPath := viper.GetString("backupIn")
backupPass := []byte(viper.GetString("backupPass"))
// FIXME: All branches of the upcoming path
var knownReception xxdk.ReceptionIdentity
// create a new client if none exist
if _, err := os.Stat(storeDir); errors.Is(err, fs.ErrNotExist) {
// Load NDF
ndfJSON, err := ioutil.ReadFile(viper.GetString("ndf"))
if err != nil {
jww.FATAL.Panicf(err.Error())
}
if precannedID != 0 {
knownReception, err = xxdk.NewPrecannedClient(precannedID,
string(ndfJSON), storeDir, pass)
} else if protoUserPath != "" {
protoUserJson, err := utils.ReadFile(protoUserPath)
if err != nil {
jww.FATAL.Panicf("%v", err)
}
protoUser := &user.Proto{}
err = json.Unmarshal(protoUserJson, protoUser)
if err != nil {
jww.FATAL.Panicf("%v", err)
}
knownReception, err = xxdk.NewProtoClient_Unsafe(string(ndfJSON), storeDir,
pass, protoUser)
} else if userIDprefix != "" {
err = xxdk.NewVanityClient(string(ndfJSON), storeDir,
pass, regCode, userIDprefix)
} else if backupPath != "" {
b, backupFile := loadBackup(backupPath, string(backupPass))
// Marshal the backup object in JSON
backupJson, err := json.Marshal(b)
if err != nil {
jww.ERROR.Printf("Failed to JSON Marshal backup: %+v", err)
}
// Write the backup JSON to file
err = utils.WriteFileDef(viper.GetString("backupJsonOut"), backupJson)
if err != nil {
jww.FATAL.Panicf("Failed to write backup to file: %+v", err)
}
// Construct client from backup data
backupIdList, _, err := backup.NewClientFromBackup(string(ndfJSON), storeDir,
pass, backupPass, backupFile)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
backupIdListPath := viper.GetString("backupIdList")
if backupIdListPath != "" {
// Marshal backed up ID list to JSON
backedUpIdListJson, err := json.Marshal(backupIdList)
if err != nil {
jww.ERROR.Printf("Failed to JSON Marshal backed up IDs: %+v", err)
}
// Write backed up ID list to file
err = utils.WriteFileDef(backupIdListPath, backedUpIdListJson)
if err != nil {
jww.FATAL.Panicf("Failed to write backed up IDs to file %q: %+v",
backupIdListPath, err)
}
}
} else {
err = xxdk.NewCmix(string(ndfJSON), storeDir,
pass, regCode)
}
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
}
client, err := xxdk.OpenCmix(storeDir, pass)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
// If there is a known xxdk.ReceptionIdentity, store it now
if knownReception.ID != nil {
err = xxdk.StoreReceptionIdentity(identityStorageKey, knownReception, client)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
}
// Attempt to load extant xxdk.ReceptionIdentity
identity, err := xxdk.LoadReceptionIdentity(identityStorageKey, client)
if err != nil {
// If no extant xxdk.ReceptionIdentity, generate and store a new one
identity, err = xxdk.MakeReceptionIdentity(client)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
err = xxdk.StoreReceptionIdentity(identityStorageKey, identity, client)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
}
return client, identity
}
func initParams() (xxdk.CMIXParams, xxdk.E2EParams) {
e2eParams := xxdk.GetDefaultE2EParams()
e2eParams.Session.MinKeys = uint16(viper.GetUint("e2eMinKeys"))
......@@ -701,40 +573,40 @@ func initParams() (xxdk.CMIXParams, xxdk.E2EParams) {
// initE2e returns a fully-formed xxdk.E2e object
func initE2e(cmixParams xxdk.CMIXParams, e2eParams xxdk.E2EParams) *xxdk.E2e {
_, receptionIdentity := initCmix()
initLog(viper.GetUint("logLevel"), viper.GetString("log"))
jww.INFO.Printf(Version())
pass := parsePassword(viper.GetString("password"))
// Intake parameters for client initialization
precanId := viper.GetUint("sendid")
protoUserPath := viper.GetString("protoUserPath")
userIdPrefix := viper.GetString("userid-prefix")
backupPath := viper.GetString("backupIn")
backupPass := viper.GetString("backupPass")
storePassword := parsePassword(viper.GetString("password"))
storeDir := viper.GetString("session")
jww.DEBUG.Printf("sessionDur: %v", storeDir)
// load the client
baseClient, err := xxdk.LoadCmix(storeDir, pass, cmixParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
regCode := viper.GetString("regcode")
jww.DEBUG.Printf("sessionDir: %v", storeDir)
// TODO: This probably shouldn't be initialized globally.
authCbs = makeAuthCallbacks(
viper.GetBool("unsafe-channel-creation"), e2eParams)
// Force LoginLegacy for precanned senderID
// Initialize the client of the proper type
var client *xxdk.E2e
if isPrecanID(receptionIdentity.ID) {
jww.INFO.Printf("Using LoginLegacy for precan sender")
client, err = xxdk.LoginLegacy(baseClient, e2eParams, authCbs)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
if precanId != 0 {
client = loadOrInitPrecan(precanId, storePassword, storeDir, cmixParams, e2eParams)
} else if protoUserPath != "" {
client = loadOrInitProto(protoUserPath, storePassword, storeDir, cmixParams, e2eParams)
} else if userIdPrefix != "" {
client = loadOrInitVanity(storePassword, storeDir, regCode, userIdPrefix, cmixParams, e2eParams)
} else if backupPath != "" {
client = loadOrInitBackup(backupPath, backupPass, storePassword, storeDir, cmixParams, e2eParams)
} else {
jww.INFO.Printf("Using Login for non-precan sender")
client, err = xxdk.Login(baseClient, authCbs, receptionIdentity,
e2eParams)
if err != nil {
jww.FATAL.Panicf("%+v", err)
}
client = loadOrInitClient(storePassword, storeDir, regCode, cmixParams, e2eParams)
}
// Handle protoUser output
if protoUser := viper.GetString("protoUserOut"); protoUser != "" {
jsonBytes, err := client.ConstructProtoUserFile()
if err != nil {
jww.FATAL.Panicf("cannot construct proto user file: %v",
......@@ -746,16 +618,15 @@ func initE2e(cmixParams xxdk.CMIXParams, e2eParams xxdk.E2EParams) *xxdk.E2e {
jww.FATAL.Panicf("cannot write proto user to file: %v",
err)
}
}
// Handle backup output
if backupOut := viper.GetString("backupOut"); backupOut != "" {
backupPass := viper.GetString("backupPass")
updateBackupCb := func(encryptedBackup []byte) {
jww.INFO.Printf("Backup update received, size %d",
len(encryptedBackup))
fmt.Println("Backup update received.")
err = utils.WriteFileDef(backupOut, encryptedBackup)
err := utils.WriteFileDef(backupOut, encryptedBackup)
if err != nil {
jww.FATAL.Panicf("cannot write backup: %+v",
err)
......@@ -781,7 +652,7 @@ func initE2e(cmixParams xxdk.CMIXParams, e2eParams xxdk.E2EParams) *xxdk.E2e {
}
}
}
_, err = backup.InitializeBackup(backupPass, updateBackupCb,
_, err := backup.InitializeBackup(backupPass, updateBackupCb,
client.GetBackupContainer(), client.GetE2E(), client.GetStorage(),
nil, client.GetStorage().GetKV(), client.GetRng())
if err != nil {
......@@ -1055,16 +926,6 @@ func waitUntilConnected(connected chan bool) {
}()
}
func parsePassword(pwStr string) []byte {
if strings.HasPrefix(pwStr, "0x") {
return getPWFromHexString(pwStr[2:])
} else if strings.HasPrefix(pwStr, "b64:") {
return getPWFromb64String(pwStr[4:])
} else {
return []byte(pwStr)
}
}
func parseRecipient(idStr string) *id.ID {
if idStr == "0" {
jww.FATAL.Panicf("No recipient specified")
......
......@@ -8,28 +8,20 @@ import (
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
"gitlab.com/elixxir/client/cmix"
backupCrypto "gitlab.com/elixxir/crypto/backup"
"gitlab.com/elixxir/crypto/contact"
"gitlab.com/xx_network/primitives/id"
"gitlab.com/xx_network/primitives/utils"
)
// todo: go through cmd package and organize utility functions
func loadBackup(backupPath, backupPass string) (backupCrypto.Backup, []byte) {
jww.INFO.Printf("Loading backup from path %q with password %q", backupPath, backupPass)
backupFile, err := utils.ReadFile(backupPath)
if err != nil {
jww.FATAL.Panicf("%v", err)
}
var b backupCrypto.Backup
err = b.Decrypt(backupPass, backupFile)
if err != nil {
jww.ERROR.Printf("Failed to decrypt backup: %+v", err)
func parsePassword(pwStr string) []byte {
if strings.HasPrefix(pwStr, "0x") {
return getPWFromHexString(pwStr[2:])
} else if strings.HasPrefix(pwStr, "b64:") {
return getPWFromb64String(pwStr[4:])
} else {
return []byte(pwStr)
}
return b, backupFile
}
/////////////////////////////////////////////////////////////////
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment