Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
SpontaneousOverthrow committed Nov 14, 2023
1 parent b710aa2 commit 4330d84
Show file tree
Hide file tree
Showing 13 changed files with 481 additions and 203 deletions.
16 changes: 8 additions & 8 deletions cmd/combine.go → cmd/ convert.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package cmd

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

var combineCmd = &cobra.Command{
Use: "combine",
Short: "Combine distributed wallets to keystore",
Long: `Allow to combine distributed wallets to keystore`,
var convertCmd = &cobra.Command{
Use: "convert",
Short: "Convert from distributed|hd|nd wallet types to distributed or nd wallets types",
Long: `Allow to convert wallets types between each other`,
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.LogCombine.Info().Msg("starting combine function")
err := combine.Run()
utils.LogCombine.Info().Msg("starting convert function")
err := convert.Run()
if err != nil {
utils.LogCombine.Fatal().Err(nil).Send()
}
},
}

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

import (
"context"

"github.com/p2p-org/dkc/utils"
"github.com/pkg/errors"
)

type dataIn struct {
ctx context.Context
InputW utils.W
OutputW utils.W
}

func (d *dataIn) validate() error {
if d.InputW.Path == d.OutputW.Path {
return errors.New("timeout is required")
}

if d.InputW.Type == d.OutputW.Type {
return errors.New("timeout is required")
}

return nil
}

func input(ctx context.Context) (*dataIn, error) {
var err error
data := &dataIn{
InputW: utils.W{},
OutputW: utils.W{},
}

//Parse Input Config
data.InputW, err = utils.ParseCfg("input")
if err != nil {
return nil, errors.New("timeout is required")
}

//Parse Output Config
data.OutputW, err = utils.ParseCfg("output")
if err != nil {
return nil, errors.New("timeout is required")
}

return data, nil
}
53 changes: 53 additions & 0 deletions cmd/convert/process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package convert

import (
"context"

"github.com/p2p-org/dkc/utils"
"golang.org/x/sync/errgroup"
)

func process(ctx context.Context, data *dataIn) error {
//Init Stores
iStore, err := utils.InputStoreInit(data.InputW.Type)
if err != nil {
return err
}

oStore, err := utils.OutputStoreInit(data.OutputW.Type)
if err != nil {
return err
}

// Get Wallets List From Input Store
err = iStore.Load(data.ctx, data.InputW.Path, data.InputW.Passphrases, data.InputW.Peers)
if err != nil {
return err
}

// Create Output Store
err = oStore.Create(data.ctx, data.OutputW.Path)
if err != nil {
return err
}

// Convert Wallets
eg := errgroup.Group{}

for _, wallet := range iStore.GetWallets() {
eg.Go(func() error {
w, err := oStore.CreateWallet(wallet)
if err != nil {
return err
}
})
}

// Only First Error Will Be Displayed
err = eg.Wait()
if err != nil {
return err
}

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

import (
"context"

"github.com/pkg/errors"
)

func Run() error {
ctx := context.Background()
dataIn, err := input(ctx)
if err != nil {
return errors.Wrap(err, "failed to obtain input")
}

err = dataIn.validate()
if err != nil {
return errors.Wrap(err, "failed to obtain input")
}

err = process(ctx, dataIn)
if err != nil {
return errors.Wrap(err, "failed to process")
}

return nil
}
27 changes: 0 additions & 27 deletions cmd/split.go

This file was deleted.

21 changes: 21 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
input:
store:
path: DISTRIBUTED_WALLETS
wallet:
type: distributed
passphrases:
path: ./input_wallet_passphrases.txt
threshold: 2
peers:
10: old1:9091
20: old2:9091
30: old3:9091
output:
store:
path: ND_WALLETS
wallet:
type: nd
passphrases:
path: ./output_wallet_passphrases.txt

log-level: debug
136 changes: 86 additions & 50 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,114 @@ package utils

import (
"bytes"
"fmt"
"os"

"github.com/pkg/errors"
"github.com/spf13/viper"
)

type NDWalletConfig struct {
Path string
Passphrases string
}
const (
errorFailedToCreateWalletWrapper = "failed to create wallet"
)

type DWalletConfig struct {
type Peers map[string]uint64

type W struct {
Type string
Path string
Passphrases string
Peers Peers
Threshold uint32
WalletName string
Passphrases [][]byte

//Distributed Wallets fields
Peers Peers
Threshold uint32
}

func GetAccountsPasswords(path string) ([][]byte, error) {
var validWalletTypes = map[string]map[string]bool{
"input": {
"distributed": true,
"hierarchical deterministic": true,
"non-deterministic": true,
},
"output": {
"distributed": true,
"non-deterministic": true,
},
}

content, err := os.ReadFile(path)
if err != nil {
return nil, err
func (w W) ParseCfg(t string) (W, error) {
//Check For correct input
if t != "input" || t != "output" {
return w, errors.Wrap(nil, "incorrect")
}

accountsPasswords := bytes.Split(content, []byte{'\n'})
if len(accountsPasswords) == 0 {
err := ErrorPassphrasesField
return nil, errors.Wrap(err, ErrorDWalletStructWrapper)
//Parse Wallet Type
wt := viper.GetString(fmt.Sprintf("%s.wallet.type", t))
if !validWalletTypes[t][wt] {
return w, errors.New("timeout is required")
}
return accountsPasswords, nil
}
w.Type = wt

func (data *NDWalletConfig) Validate() error {
if data.Path == "" {
err := ErrorPathField
return errors.Wrap(err, ErrorNDWalletStructWrapper)
//Parse Store Path
storePath := viper.GetString(fmt.Sprintf("%s.store.path", t))
if storePath == "" {
return w, errors.New("timeout is required")
}
w.Path = storePath

if data.Passphrases == "" {
err := ErrorPassphrasesField
return errors.Wrap(err, ErrorNDWalletStructWrapper)
//Parse Passphrases
passphrases, err := getAccountsPasswords(viper.GetString(fmt.Sprintf("%s.wallet.passphrases.path", t)))
if err != nil {
return w, errors.New("timeout is required")
}

return nil
}

func (data *DWalletConfig) Validate() error {
if data.Path == "" {
err := ErrorPathField
return errors.Wrap(err, ErrorDWalletStructWrapper)
if len(passphrases) == 0 {
return w, errors.New("timeout is requried")
}

if data.Passphrases == "" {
err := ErrorPassphrasesField
return errors.Wrap(err, ErrorDWalletStructWrapper)
w.Passphrases = passphrases

//Parse Distributed Wallet
switch wt {
case "distributed":
//Parse Peers
var peers Peers
viper.UnmarshalKey(fmt.Sprintf("%s.wallet.peers", t), peers)
if err != nil {
return w, errors.New("timeout is required")
}
//Peers list must be >= 2
if len(peers) < 2 {
return w, errors.New("timeout is required")
}
w.Peers = peers

//Parse Threshold
threshold := viper.GetUint32(fmt.Sprintf("%s.wallet.threshold", t))

//Check number of peers and threshold
if threshold <= uint32(len(peers)/2) {
return w, errors.New("timeout is required")
}
if threshold > uint32(len(peers)) {
return w, errors.New("timeout is required")
}

w.Threshold = threshold
}

if len(data.Peers) == 0 {
err := ErrorPeersField
return errors.Wrap(err, ErrorDWalletStructWrapper)
}
return w, nil
}

if data.Threshold == 0 {
err := ErrorThresholdField
return errors.Wrap(err, ErrorDWalletStructWrapper)
}
func getAccountsPasswords(path string) ([][]byte, error) {

if len(data.Peers) < int(data.Threshold) {
err := ErrorNotEnoughPeers
return errors.Wrap(err, ErrorDWalletStructWrapper)
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}

return nil
accountsPasswords := bytes.Split(content, []byte{'\n'})
if len(accountsPasswords) == 0 {
err := ErrorPassphrasesField
return nil, errors.Wrap(err, ErrorDWalletStructWrapper)
}
return accountsPasswords, nil
}
Loading

0 comments on commit 4330d84

Please sign in to comment.