Skip to content

Commit

Permalink
feat: init refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SpontaneousOverthrow committed Nov 3, 2023
1 parent b710aa2 commit cb75364
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 416 deletions.
27 changes: 0 additions & 27 deletions cmd/combine.go

This file was deleted.

41 changes: 0 additions & 41 deletions cmd/combine/run.go

This file was deleted.

27 changes: 27 additions & 0 deletions cmd/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"github.com/p2p-org/dkc/cmd/convert"
"github.com/p2p-org/dkc/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var convertCmd = &cobra.Command{
Use: "convert",
Short: "Convert wallets from different types",
Long: `Allow to convert wallets between different types. Supported types are: nd, hd, distributed`,
Run: func(cmd *cobra.Command, args []string) {
utils.Log.Info().Msgf("starting DKC-%s", viper.Get("version"))
utils.Log.Info().Msgf("using config file: %s", viper.ConfigFileUsed())
utils.LogConvert.Info().Msg("starting convert function")
err := convert.Run()
if err != nil {
utils.LogConvert.Fatal().Err(nil).Send()
}
},
}

func init() {
rootCmd.AddCommand(convertCmd)
}
34 changes: 34 additions & 0 deletions cmd/convert/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package convert

import "github.com/p2p-org/dkc/utils"

func Run() error {
utils.LogConvert.Info().Msg("init and validate config")
convertRuntime, err := newConvertRuntime()
if err != nil {
utils.LogConvert.Err(err).Send()
return err
}

err = convertRuntime.validate()
if err != nil {
utils.LogConvert.Err(err).Send()
return err
}

utils.LogConvert.Info().Msg("creating wallets")
err = convertRuntime.createWalletAndStore()
if err != nil {
utils.LogConvert.Err(err).Send()
return err
}

utils.LogConvert.Info().Msg("checking signatures")
err = convertRuntime.checkSignature()
if err != nil {
utils.LogConvert.Err(err).Send()
return err
}

return nil
}
44 changes: 12 additions & 32 deletions cmd/combine/helpers.go → cmd/convert/helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package combine
package convert

import (
"context"
Expand All @@ -8,52 +8,32 @@ import (
"github.com/p2p-org/dkc/utils"
"github.com/p2p-org/dkc/utils/crypto/bls"
"github.com/spf13/viper"
types "github.com/wealdtech/go-eth2-wallet-types/v2"
)

type AccountExtends struct {
PubKey []byte
CompositePubKeys [][]byte
Accounts []utils.Account
}

type CombineRuntime struct {
ctx context.Context
dWalletsPath string
ndWalletsPath string
passphrasesIn [][]byte
passphrasesOut [][]byte
accountDatas map[string]AccountExtends
stores []utils.DirkStore
peers utils.Peers
wallet utils.NDWallet
store types.Store
}

func newCombineRuntime() (*CombineRuntime, error) {
cr := &CombineRuntime{}
func newConvertRuntime() (*ConvertRuntime, error) {
cr := &utils.ConvertRuntime{}
var err error

utils.LogCombine.Debug().Msg("validating nd-wallets field")
var ndWalletConfig utils.NDWalletConfig
err = viper.UnmarshalKey("nd-wallets", &ndWalletConfig)
utils.LogConvert.Debug().Msg("validating input wallet")
var inputW utils.W
err = viper.UnmarshalKey("input", &inputW)
if err != nil {
return nil, err
}

err = ndWalletConfig.Validate()
err = inputW.Validate()
if err != nil {
return nil, err
}

utils.LogCombine.Debug().Msg("validating distributed-wallets field")
var dWalletConfig utils.DWalletConfig
err = viper.UnmarshalKey("distributed-wallets", &dWalletConfig)
utils.LogConvert.Debug().Msg("validating output field")
var outputW utils.W
err = viper.UnmarshalKey("output", &outputW)
if err != nil {
return nil, err
}

err = dWalletConfig.Validate()
err = outputW.Validate()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -83,7 +63,7 @@ func newCombineRuntime() (*CombineRuntime, error) {
return cr, nil
}

func (cr *CombineRuntime) validate() error {
func (cr *utils.ConvertRuntime) validate() error {
if cr.dWalletsPath == cr.ndWalletsPath {
return utils.ErrorSameDirs
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
var rootCmd = &cobra.Command{
Use: "dkc",
Short: "Dirk Key Converter",
Long: `Allow to split and combine keystores and distributed wallets for Dirk`,
Long: `Allow to convert wallets between different types`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
},
}
Expand Down
27 changes: 0 additions & 27 deletions cmd/split.go

This file was deleted.

Loading

0 comments on commit cb75364

Please sign in to comment.