Skip to content

Commit

Permalink
gocryptfs: LoadPrivateKey() added
Browse files Browse the repository at this point in the history
  • Loading branch information
kripa432 committed Jul 11, 2024
1 parent 04aa482 commit ad3e7fe
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
28 changes: 27 additions & 1 deletion common/keys.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package wc_common

import (
"crypto/ecdsa"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

"github.com/ethereum/go-ethereum/crypto"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -200,11 +202,14 @@ func ValidEncryptedDir() bool {
func GetPrivateKeyFromFile(keyName string) string {
keyFile := DecryptedDir + "/" + keyName
data, err := os.ReadFile(keyFile)
CheckError(err, "Error reading key file")
CheckError(err, "Error reading key file" + keyFile)
return string(data)
}

func UseEncryptedKeys() {
if useEncryptedKeys == true {
return
}
useEncryptedKeys = true
ValidateAndMount()
}
Expand Down Expand Up @@ -240,3 +245,24 @@ func GetPrivateKey(key string) string {
}
return key
}

func LoadPrivateKey(path string) (*ecdsa.PrivateKey, error) {
fmt.Println("load " + path)
dir := filepath.Dir(path)
KeyfileName := filepath.Base(path)
EncryptedDir = dir

Mount()
data, err := os.ReadFile(DecryptedDir + "/" + KeyfileName)
CheckError(err, "Error reading key file" + path)
Unmount()

fmt.Println("privateKey" + string(data))
fmt.Println(data)

priv, err := crypto.HexToECDSA(string(data))
if err != nil {
return nil, err
}
return priv, nil
}
4 changes: 4 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func ValidateAndMount() {
}

func Mount() {
if isMounted == true {
return
}

mountCmd := exec.Command("gocryptfs", EncryptedDir, DecryptedDir)
RunCommandWithPassword(mountCmd, "mount", true)

Expand Down
1 change: 1 addition & 0 deletions watchtower-operator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.json
1 change: 0 additions & 1 deletion watchtower-operator/commands/register_watchtower.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/witnesschain-com/diligencewatchtower-client/keystore"
wc_common "github.com/witnesschain-com/operator-cli/common"
Expand Down
23 changes: 5 additions & 18 deletions watchtower-operator/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (
type OperatorConfig struct {
WatchtowerPrivateKeys []string `json:"watchtower_private_keys"`
WatchtowerAddresses []string `json:"watchtower_addresses"`
WatchtowerEncryptedKeys []string `json:"watchtower_encrypted_keys"`
OperatorPrivateKey string `json:"operator_private_key"`
OperatorAddress common.Address `json:"operator_address"`
OperatorEncryptedKey string `json:"operator_encrypted_key"`
OperatorRegistryAddress common.Address `json:"operator_registry_address"`
WitnessHubAddress common.Address `json:"witnesshub_address"`
AvsDirectoryAddress common.Address `json:"avs_directory_address"`
Expand All @@ -38,16 +40,15 @@ func GetConfigFromContext(cCtx *cli.Context) *OperatorConfig {
wc_common.CheckError(err, "Error reading json file")

// Parse the json data into a struct
var config OperatorConfig
var config OperatorConfig = OperatorConfig{ExpiryInDays: 1, TxReceiptTimeout: 300, GasLimit: 300000}
err = json.Unmarshal(data, &config)
wc_common.CheckError(err, "Error unmarshaling json data")

SetDefaultConfigValues(&config)

if config.UseEncryptedKeys {
if len(config.WatchtowerEncryptedKeys) != 0 {
// get the path from the first key, as others should be same
// will not work with different paths
wc_common.ProcessConfigKeyPath(config.WatchtowerPrivateKeys[0])
wc_common.ProcessConfigKeyPath(config.WatchtowerEncryptedKeys[0])
wc_common.UseEncryptedKeys()
}

Expand All @@ -69,17 +70,3 @@ func GetConfigFromContext(cCtx *cli.Context) *OperatorConfig {

return &config
}

func SetDefaultConfigValues(config *OperatorConfig) {
if config.ExpiryInDays == 0 {
config.ExpiryInDays = 1 // 1 day
}

if config.TxReceiptTimeout == 0 {
config.TxReceiptTimeout = 5 * 60 // 5 minutes
}

if config.GasLimit == 0 {
config.GasLimit = 300000
}
}

0 comments on commit ad3e7fe

Please sign in to comment.