Skip to content

Commit

Permalink
provide means to use different passphrase for each distributed shard
Browse files Browse the repository at this point in the history
  • Loading branch information
schonex committed Sep 3, 2023
1 parent 953f9ce commit 09fdf64
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 47 deletions.
28 changes: 14 additions & 14 deletions cmd/combine/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -61,24 +60,23 @@ 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 {
return nil, err
}
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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
31 changes: 16 additions & 15 deletions cmd/split/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -172,22 +170,25 @@ 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,
account.MasterPKs,
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
}
Expand Down
15 changes: 11 additions & 4 deletions utils/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
18 changes: 9 additions & 9 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions utils/stores.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ type DirkStore struct {
Wallets []types.Wallet
}

type Peers = map[uint64]string

type Account struct {
ID uint64
Key []byte
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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
Expand Down

0 comments on commit 09fdf64

Please sign in to comment.