diff --git a/cmd/combine/helpers.go b/cmd/combine/helpers.go index 6e5b1cc..c2e4060 100644 --- a/cmd/combine/helpers.go +++ b/cmd/combine/helpers.go @@ -21,11 +21,10 @@ type CombineRuntime struct { ctx context.Context dWalletsPath string ndWalletsPath string - passphrasesIn [][]byte passphrasesOut [][]byte accountDatas map[string]AccountExtends stores []utils.DirkStore - peers utils.Peers + peers map[uint64]utils.Peer wallet utils.NDWallet store types.Store } @@ -61,11 +60,7 @@ func newCombineRuntime() (*CombineRuntime, error) { cr.ctx = context.Background() cr.dWalletsPath = dWalletConfig.Path cr.ndWalletsPath = ndWalletConfig.Path - utils.LogCombine.Debug().Msgf("getting input passwords form file %s", dWalletConfig.Passphrases) - cr.passphrasesIn, err = utils.GetAccountsPasswords(dWalletConfig.Passphrases) - if err != nil { - return nil, err - } + utils.LogCombine.Debug().Msgf("getting output passwords form file %s", ndWalletConfig.Passphrases) cr.passphrasesOut, err = utils.GetAccountsPasswords(ndWalletConfig.Passphrases) if err != nil { @@ -73,12 +68,15 @@ func newCombineRuntime() (*CombineRuntime, error) { } cr.accountDatas = make(map[string]AccountExtends) utils.LogCombine.Debug().Msgf("loading stores form %s", cr.dWalletsPath) - cr.stores, err = utils.LoadStores(cr.ctx, cr.dWalletsPath, cr.passphrasesIn) + cr.stores, err = utils.LoadStores(cr.ctx, cr.dWalletsPath) if err != nil { return nil, err } - cr.peers = dWalletConfig.Peers + cr.peers = make(map[uint64]utils.Peer, 0) + for _, peer := range dWalletConfig.Peers { + cr.peers[peer.ID] = peer + } return cr, nil } @@ -158,8 +156,8 @@ func (cr *CombineRuntime) checkSignature() error { func (cr *CombineRuntime) storeUpdater() error { for _, store := range cr.stores { var participantID uint64 - for id := range cr.peers { - peerExists, err := regexp.MatchString(filepath.Base(store.Location)+":.*", cr.peers[id]) + for id, peer := range cr.peers { + peerExists, err := regexp.MatchString(filepath.Base(store.Location)+":.*", peer.Host) if err != nil { return err } @@ -169,16 +167,18 @@ func (cr *CombineRuntime) storeUpdater() error { participantID = id for _, wallet := range store.Wallets { - utils.LogCombine.Debug().Msgf("loading data for wallet %s", wallet.Name()) + utils.LogCombine.Debug().Msgf("loading data for wallet %s peer ID %d and host %s", wallet.Name(), peer.ID, peer.Host) for account := range wallet.Accounts(cr.ctx) { + passArr := make([][]byte,1) + passArr[0] = []byte(peer.Passphrase) utils.LogCombine.Debug().Msgf("get private key for account %s", account.Name()) - key, err := utils.GetAccountKey(cr.ctx, account, cr.passphrasesOut) + key, err := utils.GetAccountKey(cr.ctx, account, passArr) if err != nil { return err } utils.LogCombine.Debug().Msgf("sign message from account %s", account.Name()) - initialSignature, err := utils.AccountSign(cr.ctx, account, cr.passphrasesOut) + initialSignature, err := utils.AccountSign(cr.ctx, account, passArr) if err != nil { return err } diff --git a/cmd/split/helpers.go b/cmd/split/helpers.go index 8131612..4aec098 100644 --- a/cmd/split/helpers.go +++ b/cmd/split/helpers.go @@ -16,9 +16,8 @@ type SplitRuntime struct { dWalletsPath string ndWalletsPath string passphrasesIn [][]byte - passphrasesOut [][]byte accountDatas map[string]AccountExtends - peers utils.Peers + peers map[uint64]utils.Peer threshold uint32 walletsMap map[uint64]utils.DWallet peersIDs []uint64 @@ -69,18 +68,17 @@ func newSplitRuntime() (*SplitRuntime, error) { if err != nil { return nil, err } - utils.LogSplit.Debug().Msgf("getting input passwords from %s", dWalletConfig.Passphrases) - sr.passphrasesOut, err = utils.GetAccountsPasswords(dWalletConfig.Passphrases) - if err != nil { - return nil, err - } + sr.accountDatas = make(map[string]AccountExtends) sr.walletsMap = make(map[uint64]utils.DWallet) - sr.peers = dWalletConfig.Peers + sr.peers = make(map[uint64]utils.Peer, 0) + for _, peer := range dWalletConfig.Peers { + sr.peers[peer.ID] = peer + } utils.LogSplit.Debug().Msg("generating peersIDs") - for id := range sr.peers { + for id,_ := range sr.peers { sr.peersIDs = append(sr.peersIDs, id) } @@ -96,13 +94,13 @@ func (sr *SplitRuntime) validate() error { func (sr *SplitRuntime) createWallets() error { walletName := uuid.New().String() - for id, peer := range sr.peers { + for id,peer := range sr.peers { res, err := regexp.Compile(`:.*`) if err != nil { return err } utils.LogSplit.Debug().Msgf("creating store for peer: %d", id) - storePath := sr.dWalletsPath + "/" + res.ReplaceAllString(peer, "") + storePath := sr.dWalletsPath + "/" + res.ReplaceAllString(peer.Host, "") store, err := utils.CreateStore(storePath) if err != nil { return err @@ -119,7 +117,7 @@ func (sr *SplitRuntime) createWallets() error { func (sr *SplitRuntime) loadWallets() error { utils.LogSplit.Debug().Msgf("load store %s", sr.ndWalletsPath) - s, err := utils.LoadStore(sr.ctx, sr.ndWalletsPath, sr.passphrasesIn) + s, err := utils.LoadStore(sr.ctx, sr.ndWalletsPath) if err != nil { return err } @@ -172,7 +170,8 @@ func (sr *SplitRuntime) saveAccounts() error { for accountName, account := range sr.accountDatas { utils.LogSplit.Debug().Msgf("saving account %s ", accountName) for i, acc := range account.Accounts { - utils.LogSplit.Debug().Msgf("creating account with id %d ", acc.ID) + utils.LogSplit.Debug().Msgf("creating account with id %d", acc.ID) + finalAccount, err := utils.CreateDAccount( sr.walletsMap[acc.ID], accountName, @@ -180,14 +179,16 @@ func (sr *SplitRuntime) saveAccounts() error { acc.Key, sr.threshold, sr.peers, - sr.passphrasesOut[0], + sr.peers[acc.ID].Passphrase, ) if err != nil { return err } utils.LogSplit.Debug().Msgf("generating signature for account with id %d ", acc.ID) - account.Accounts[i].Signature, err = utils.AccountSign(sr.ctx, finalAccount, sr.passphrasesOut) + passArr := make([][]byte,1) + passArr[0] = []byte(sr.peers[acc.ID].Passphrase) + account.Accounts[i].Signature, err = utils.AccountSign(sr.ctx, finalAccount, passArr) if err != nil { return err } diff --git a/utils/accounts.go b/utils/accounts.go index 139cd2b..85e6f54 100644 --- a/utils/accounts.go +++ b/utils/accounts.go @@ -46,8 +46,8 @@ func CreateDAccount( masterPKs [][]byte, masterSK []byte, threshold uint32, - peers map[uint64]string, - passphrase []byte, + peers map[uint64]Peer, + passphrase string, ) (types.Account, error) { err := wallet.Unlock(context.Background(), nil) @@ -59,13 +59,20 @@ func CreateDAccount( err = wallet.(types.WalletLocker).Lock(context.Background()) }() + peerMap := make(map[uint64]string, 0) + for id, peer := range peers { + peerMap[id] = peer.Host + } + + passBytes := []byte(passphrase) + account, err := wallet.ImportDistributedAccount(context.Background(), name, masterSK, threshold, masterPKs, - peers, - passphrase) + peerMap, + passBytes) if err != nil { return nil, errors.Wrap(err, ErrorImportWrapper) } diff --git a/utils/config.go b/utils/config.go index fa32f92..79463dc 100644 --- a/utils/config.go +++ b/utils/config.go @@ -13,10 +13,15 @@ type NDWalletConfig struct { } type DWalletConfig struct { - Path string - Passphrases string - Peers Peers - Threshold uint32 + Path string + Peers []Peer + Threshold uint32 +} + +type Peer struct { + ID uint64 + Host string + Passphrase string } func GetAccountsPasswords(path string) ([][]byte, error) { @@ -54,11 +59,6 @@ func (data *DWalletConfig) Validate() error { return errors.Wrap(err, ErrorDWalletStructWrapper) } - if data.Passphrases == "" { - err := ErrorPassphrasesField - return errors.Wrap(err, ErrorDWalletStructWrapper) - } - if len(data.Peers) == 0 { err := ErrorPeersField return errors.Wrap(err, ErrorDWalletStructWrapper) diff --git a/utils/stores.go b/utils/stores.go index 62ac42e..b154510 100644 --- a/utils/stores.go +++ b/utils/stores.go @@ -15,8 +15,6 @@ type DirkStore struct { Wallets []types.Wallet } -type Peers = map[uint64]string - type Account struct { ID uint64 Key []byte @@ -28,7 +26,7 @@ func CreateStore(path string) (types.Store, error) { return store, nil } -func LoadStores(ctx context.Context, walletDir string, passphrases [][]byte) ([]DirkStore, error) { +func LoadStores(ctx context.Context, walletDir string) ([]DirkStore, error) { var stores []DirkStore dirs, err := os.ReadDir(walletDir) @@ -37,7 +35,7 @@ func LoadStores(ctx context.Context, walletDir string, passphrases [][]byte) ([] } for _, f := range dirs { if f.IsDir() { - store, err := LoadStore(ctx, walletDir+"/"+f.Name(), passphrases) + store, err := LoadStore(ctx, walletDir+"/"+f.Name()) if err != nil { return nil, errors.Wrap(err, ErrorLoadStoreWrapper) } @@ -47,7 +45,7 @@ func LoadStores(ctx context.Context, walletDir string, passphrases [][]byte) ([] return stores, nil } -func LoadStore(ctx context.Context, location string, passphrases [][]byte) (*DirkStore, error) { +func LoadStore(ctx context.Context, location string) (*DirkStore, error) { dirkStore := DirkStore{} dirkStore.Location = location var wallets []types.Wallet