Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #27

Merged
merged 8 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions cmd/combine/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package combine

import (
"bytes"
"context"
"os"
"path/filepath"
"regexp"

"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
distributedWalletsPath string
ndWalletsPath string
passphrases [][]byte
accountDatas map[string]AccountExtends
stores []utils.DirkStore
peers utils.Peers
wallet utils.NDWallet
store types.Store
}

func getAccountsPasswords() [][]byte {
accountsPasswordPath := viper.GetString("passphrases")

content, err := os.ReadFile(accountsPasswordPath)
if err != nil {
panic(err)
}

accountsPasswords := bytes.Split(content, []byte{'\n'})
return accountsPasswords
}

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

cr.ctx = context.Background()
cr.distributedWalletsPath = viper.GetString("distributed-wallets")
cr.ndWalletsPath = viper.GetString("nd-wallets")
cr.passphrases = getAccountsPasswords()
cr.accountDatas = make(map[string]AccountExtends)
cr.stores, err = utils.LoadStores(cr.ctx, cr.distributedWalletsPath, cr.passphrases)
if err != nil {
return &CombineRuntime{}, err
}

err = viper.UnmarshalKey("peers", &peers)
if err != nil {
return &CombineRuntime{}, err
}

cr.peers = peers

return cr, nil
}

func (cr *CombineRuntime) createWalletAndStore() error {
cr.store = utils.CreateStore(cr.ndWalletsPath)
cr.wallet = utils.CreateNDWallet(cr.store)
return nil
}

func (cr *CombineRuntime) checkSignature() error {
for accountName, account := range cr.accountDatas {
key, err := bls.Recover(cr.ctx, account.Accounts)
if err != nil {
return err
}

initialSignature := bls.Sign(cr.ctx, account.Accounts)

finalAccount := utils.CreateNDAccount(cr.wallet, accountName, key, cr.passphrases[0])
SpontaneousOverthrow marked this conversation as resolved.
Show resolved Hide resolved

finalSignature := utils.AccountSign(cr.ctx, finalAccount, cr.passphrases)

if !bytes.Equal(finalSignature, initialSignature) {
return err
}

pubkey, err := utils.GetAccountPubkey(finalAccount)
if err != nil {
return err
}

for _, compositeKey := range account.CompositePubKeys {
if !bytes.Equal(compositeKey, pubkey) {
panic("test")
SpontaneousOverthrow marked this conversation as resolved.
Show resolved Hide resolved
}

}
}
return nil
}

func (cr *CombineRuntime) storeUpdater() error {
for _, store := range cr.stores {
var participantID uint64
for id := range cr.peers {
peerExists, _ := regexp.MatchString(filepath.Base(store.Location)+":.*", cr.peers[id])
SpontaneousOverthrow marked this conversation as resolved.
Show resolved Hide resolved
if !peerExists {
continue
}
participantID = id

for _, wallet := range store.Wallets {
for account := range wallet.Accounts(cr.ctx) {
key, err := utils.GetAccountKey(cr.ctx, account, cr.passphrases)
if err != nil {
return err
}

initialSignature := utils.AccountSign(cr.ctx, account, cr.passphrases)
compositePubKey, err := utils.GetAccountCompositePubkey(account)
if err != nil {
return err
}

cr.accountDatas[account.Name()] = AccountExtends{
Accounts: append(cr.accountDatas[account.Name()].Accounts,
utils.Account{
Key: key,
Signature: initialSignature,
ID: participantID,
},
),
CompositePubKeys: append(cr.accountDatas[account.Name()].CompositePubKeys, compositePubKey),
}
}
}
}
}
return nil
}
104 changes: 10 additions & 94 deletions cmd/combine/run.go
Original file line number Diff line number Diff line change
@@ -1,107 +1,23 @@
package combine

import (
"bytes"
"context"
"fmt"
"path/filepath"
"regexp"

"github.com/p2p-org/dkc/service"
"github.com/p2p-org/dkc/service/crypto/bls"
"github.com/spf13/viper"
)

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

func Run() {
ctx := context.Background()
signString := "testingStringABC"
distributedWalletsPath := viper.GetString("distributed-wallets")
ndWalletsPath := viper.GetString("nd-wallets")
var peers service.Peers
passphrases := service.GetAccountsPasswords()
accountDatas := make(map[string]AccountExtends)
stores, err := service.LoadStores(ctx, distributedWalletsPath, passphrases)
combineRuntime, err := newCombineRuntime()
if err != nil {
fmt.Println(err)
panic(err)
}

err = viper.UnmarshalKey("peers", &peers)
err = combineRuntime.createWalletAndStore()
if err != nil {
fmt.Println(err)
panic(err)
}

for _, store := range stores {
var participantID uint64
for id := range peers {
peerExists, _ := regexp.MatchString(filepath.Base(store.Location)+":.*", peers[id])
if peerExists {
participantID = id

for _, wallet := range store.Wallets {
for account := range wallet.Accounts(ctx) {
key, err := service.GetAccountKey(ctx, account, passphrases)
if err != nil {
fmt.Println("Error")
}

initialSignature := service.AccountSign(ctx, account, []byte(signString), passphrases)
compositePubKey, err := service.GetAccountCompositePubkey(account)
if err != nil {
panic(err)
}

accountDatas[account.Name()] = AccountExtends{
Accounts: append(accountDatas[account.Name()].Accounts,
service.Account{
Key: key,
Signature: initialSignature,
ID: participantID,
},
),
CompositePubKeys: append(accountDatas[account.Name()].CompositePubKeys, compositePubKey),
}
}
}
}
}
err = combineRuntime.storeUpdater()
if err != nil {
panic(err)
}

store := service.CreateStore(ndWalletsPath)
wallet := service.CreateWallet(store, "non-deterministic")
for accountName, account := range accountDatas {
key, err := bls.Recover(ctx, account.Accounts)
if err != nil {
panic(err)
}

initialSignature := bls.Sign(ctx, account.Accounts)

finalAccount := service.CreateNDAccount(key, accountName, passphrases[0], wallet)

finalSignature := service.AccountSign(ctx, finalAccount, []byte(signString), passphrases)

if !bytes.Equal(finalSignature, initialSignature) {
panic("test")
}

pubkey, err := service.GetAccountPubkey(finalAccount)
if err != nil {
panic(err)
}

for _, compositeKey := range account.CompositePubKeys {
if !bytes.Equal(compositeKey, pubkey) {
panic("test")
}

}
err = combineRuntime.checkSignature()
if err != nil {
panic(err)
}

return
}
Loading