Skip to content

Commit

Permalink
Refactor code to use new package structure
Browse files Browse the repository at this point in the history
  • Loading branch information
nitronit committed Dec 19, 2023
1 parent 88f0a31 commit d0ff41f
Show file tree
Hide file tree
Showing 40 changed files with 533 additions and 502 deletions.
13 changes: 7 additions & 6 deletions cmd/horcrux/cmd/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
cometprivval "github.com/cometbft/cometbft/privval"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/spf13/cobra"
"github.com/strangelove-ventures/horcrux/signer"
cconfig "github.com/strangelove-ventures/horcrux/pkg/config"
"github.com/strangelove-ventures/horcrux/pkg/thresholdTemP"
)

type AddressCmdOutput struct {
Expand All @@ -34,7 +35,7 @@ func addressCmd() *cobra.Command {
chainID := args[0]

switch config.Config.SignMode {
case signer.SignModeThreshold:
case cconfig.SignModeThreshold:
err := config.Config.ValidateThresholdModeConfig()
if err != nil {
return err
Expand All @@ -45,13 +46,13 @@ func addressCmd() *cobra.Command {
return err
}

key, err := signer.LoadThresholdSignerEd25519Key(keyFile)
key, err := thresholdTemP.LoadThresholdSignerEd25519Key(keyFile)
if err != nil {
return fmt.Errorf("error reading threshold key: %w, check that key is present for chain id: %s", err, chainID)
}

pubKey = key.PubKey
case signer.SignModeSingle:
case cconfig.SignModeSingle:
err := config.Config.ValidateSingleSignerConfig()
if err != nil {
return err
Expand All @@ -70,7 +71,7 @@ func addressCmd() *cobra.Command {

pubKeyAddress := pubKey.Address()

pubKeyJSON, err := signer.PubKey("", pubKey)
pubKeyJSON, err := cconfig.PubKey("", pubKey)
if err != nil {
return err
}
Expand All @@ -86,7 +87,7 @@ func addressCmd() *cobra.Command {
return err
}
output.ValConsAddress = bech32ValConsAddress
pubKeyBech32, err := signer.PubKey(args[1], pubKey)
pubKeyBech32, err := cconfig.PubKey(args[1], pubKey)
if err != nil {
return err
}
Expand Down
24 changes: 12 additions & 12 deletions cmd/horcrux/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/strangelove-ventures/horcrux/signer"
cconfig "github.com/strangelove-ventures/horcrux/pkg/config"
)

const (
Expand Down Expand Up @@ -48,7 +48,7 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.
bare, _ := cmdFlags.GetBool(flagBare)
nodes, _ := cmdFlags.GetStringSlice(flagNode)

cn, err := signer.ChainNodesFromFlag(nodes)
cn, err := cconfig.ChainNodesFromFlag(nodes)
if err != nil {
return err
}
Expand All @@ -60,7 +60,7 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.
config.ConfigFile)
}

var cfg signer.Config
var cfg cconfig.Config

signMode, _ := cmdFlags.GetString(flagSignMode)
keyDirFlag, _ := cmdFlags.GetString(flagKeyDir)
Expand All @@ -70,21 +70,21 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.
}
debugAddr, _ := cmdFlags.GetString(flagDebugAddr)
grpcAddr, _ := cmdFlags.GetString(flagGRPCAddress)
if signMode == string(signer.SignModeThreshold) {
if signMode == string(cconfig.SignModeThreshold) {
// Threshold Mode Config
cosignersFlag, _ := cmdFlags.GetStringSlice(flagCosigner)
threshold, _ := cmdFlags.GetInt(flagThreshold)
raftTimeout, _ := cmdFlags.GetString(flagRaftTimeout)
grpcTimeout, _ := cmdFlags.GetString(flagGRPCTimeout)
cosigners, err := signer.CosignersFromFlag(cosignersFlag)
cosigners, err := cconfig.CosignersFromFlag(cosignersFlag)
if err != nil {
return err
}

cfg = signer.Config{
SignMode: signer.SignModeThreshold,
cfg = cconfig.Config{
SignMode: cconfig.SignModeThreshold,
PrivValKeyDir: keyDir,
ThresholdModeConfig: &signer.ThresholdModeConfig{
ThresholdModeConfig: &cconfig.ThresholdModeConfig{
Threshold: threshold,
Cosigners: cosigners,
GRPCTimeout: grpcTimeout,
Expand All @@ -102,8 +102,8 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.
}
} else {
// Single Signer Config
cfg = signer.Config{
SignMode: signer.SignModeSingle,
cfg = cconfig.Config{
SignMode: cconfig.SignModeSingle,
PrivValKeyDir: keyDir,
ChainNodes: cn,
DebugAddr: debugAddr,
Expand Down Expand Up @@ -134,8 +134,8 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.
}

f := cmd.Flags()
f.StringP(flagSignMode, "m", string(signer.SignModeThreshold),
`sign mode, "threshold" (recommended) or "single" (unsupported). threshold mode requires --cosigner (multiple) and --threshold`, //nolint
f.StringP(flagSignMode, "m", string(cconfig.SignModeThreshold),
`sign mode, "threshold" (recommended) or "single" (unsupported). threshold mode requires --cosigner (multiple) and --threshold`, // nolint
)
f.StringSliceP(flagNode, "n", []string{}, "chain nodes in format tcp://{node-addr}:{privval-port} \n"+
"(e.g. --node tcp://sentry-1:1234 --node tcp://sentry-2:1234 --node tcp://sentry-3:1234 )")
Expand Down
7 changes: 4 additions & 3 deletions cmd/horcrux/cmd/leader_election.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"fmt"
"time"

"github.com/strangelove-ventures/horcrux/pkg/nodes/nodesecurity"

grpcretry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
"github.com/spf13/cobra"
"github.com/strangelove-ventures/horcrux/client"
"github.com/strangelove-ventures/horcrux/signer"
"github.com/strangelove-ventures/horcrux/signer/multiresolver"
"github.com/strangelove-ventures/horcrux/signer/proto"
"google.golang.org/grpc"
Expand Down Expand Up @@ -116,14 +117,14 @@ func getLeaderCmd() *cobra.Command {
return fmt.Errorf("cosigner encryption keys not found (%s) - (%s): %w", keyFileECIES, keyFileRSA, err)
}

key, err := signer.LoadCosignerRSAKey(keyFileRSA)
key, err := nodesecurity.LoadCosignerRSAKey(keyFileRSA)
if err != nil {
return fmt.Errorf("error reading cosigner key (%s): %w", keyFileRSA, err)
}

id = key.ID
} else {
key, err := signer.LoadCosignerECIESKey(keyFileECIES)
key, err := nodesecurity.LoadCosignerECIESKey(keyFileECIES)
if err != nil {
return fmt.Errorf("error reading cosigner key (%s): %w", keyFileECIES, err)
}
Expand Down
25 changes: 14 additions & 11 deletions cmd/horcrux/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import (
"os"
"path/filepath"

cconfig "github.com/strangelove-ventures/horcrux/pkg/config"
"github.com/strangelove-ventures/horcrux/pkg/nodes/nodesecurity"
"github.com/strangelove-ventures/horcrux/pkg/thresholdTemP"

cometcrypto "github.com/cometbft/cometbft/crypto"
cometcryptoed25519 "github.com/cometbft/cometbft/crypto/ed25519"
cometcryptoencoding "github.com/cometbft/cometbft/crypto/encoding"
cometprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
"github.com/spf13/cobra"
"github.com/strangelove-ventures/horcrux/signer"
amino "github.com/tendermint/go-amino"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -218,7 +221,7 @@ func migrateCmd() *cobra.Command {
return err
}

newEd25519Key := signer.CosignerEd25519Key{
newEd25519Key := thresholdTemP.CosignerEd25519Key{
PubKey: legacyCosignerKey.PubKey,
PrivateShard: legacyCosignerKey.ShareKey,
ID: legacyCosignerKey.ID,
Expand All @@ -234,7 +237,7 @@ func migrateCmd() *cobra.Command {
return fmt.Errorf("failed to write new Ed25519 key to %s: %w", newEd25519Path, err)
}

newRSAKey := signer.CosignerRSAKey{
newRSAKey := nodesecurity.CosignerRSAKey{
RSAKey: legacyCosignerKey.RSAKey,
ID: legacyCosignerKey.ID,
RSAPubs: legacyCosignerKey.RSAPubs,
Expand All @@ -252,42 +255,42 @@ func migrateCmd() *cobra.Command {

// only attempt config migration if legacy config exists
if legacyCfgErr == nil {
var migratedNodes signer.ChainNodes
var migratedNodes cconfig.ChainNodes

for _, n := range legacyCfg.ChainNodes {
migratedNodes = append(migratedNodes, signer.ChainNode{
migratedNodes = append(migratedNodes, cconfig.ChainNode{
PrivValAddr: n.PrivValAddr,
})
}

config.Config.ChainNodes = migratedNodes
config.Config.DebugAddr = legacyCfg.DebugAddr

signMode := signer.SignModeSingle
signMode := cconfig.SignModeSingle

if legacyCfg.Cosigner != nil {
signMode = signer.SignModeThreshold
signMode = cconfig.SignModeThreshold

var migratedCosigners signer.CosignersConfig
var migratedCosigners cconfig.CosignersConfig

if legacyCfg.Cosigner.P2PListen != "" {
migratedCosigners = append(
migratedCosigners,
signer.CosignerConfig{
cconfig.CosignerConfig{
ShardID: legacyCosignerKey.ID,
P2PAddr: legacyCfg.Cosigner.P2PListen,
},
)
}

for _, c := range legacyCfg.Cosigner.Peers {
migratedCosigners = append(migratedCosigners, signer.CosignerConfig{
migratedCosigners = append(migratedCosigners, cconfig.CosignerConfig{
ShardID: c.ShareID,
P2PAddr: c.P2PAddr,
})
}

config.Config.ThresholdModeConfig = &signer.ThresholdModeConfig{
config.Config.ThresholdModeConfig = &cconfig.ThresholdModeConfig{
Threshold: legacyCfg.Cosigner.Threshold,
Cosigners: migratedCosigners,
GRPCTimeout: legacyCfg.Cosigner.Timeout,
Expand Down
6 changes: 3 additions & 3 deletions cmd/horcrux/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/strangelove-ventures/horcrux/signer"
cconfig "github.com/strangelove-ventures/horcrux/pkg/config"
"gopkg.in/yaml.v2"
)

var config signer.RuntimeConfig
var config cconfig.RuntimeConfig

func rootCmd() *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -74,7 +74,7 @@ func initConfig() {
} else {
home = config.HomeDir
}
config = signer.RuntimeConfig{
config = cconfig.RuntimeConfig{
HomeDir: home,
ConfigFile: filepath.Join(home, "config.yaml"),
StateDir: filepath.Join(home, "state"),
Expand Down
16 changes: 9 additions & 7 deletions cmd/horcrux/cmd/shards.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"os"
"path/filepath"

"github.com/strangelove-ventures/horcrux/pkg/nodes/nodesecurity"
"github.com/strangelove-ventures/horcrux/pkg/thresholdTemP"

"github.com/spf13/cobra"
"github.com/strangelove-ventures/horcrux/signer"
)

func createCosignerDirectoryIfNecessary(out string, id int) (string, error) {
Expand Down Expand Up @@ -112,7 +114,7 @@ func createCosignerEd25519ShardsCmd() *cobra.Command {
return nil
}

csKeys, err := signer.CreateEd25519ThresholdSignShardsFromFile(keyFile, threshold, shards)
csKeys, err := thresholdTemP.CreateEd25519ThresholdSignShardsFromFile(keyFile, threshold, shards)
if err != nil {
return err
}
Expand All @@ -133,7 +135,7 @@ func createCosignerEd25519ShardsCmd() *cobra.Command {
return err
}
filename := filepath.Join(dir, fmt.Sprintf("%s_shard.json", chainID))
if err = signer.WriteCosignerEd25519ShardFile(c, filename); err != nil {
if err = thresholdTemP.WriteCosignerEd25519ShardFile(c, filename); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Created Ed25519 Shard %s\n", filename)
Expand Down Expand Up @@ -170,7 +172,7 @@ func createCosignerECIESShardsCmd() *cobra.Command {
return fmt.Errorf("shards must be greater than zero (%d): %w", shards, err)
}

csKeys, err := signer.CreateCosignerECIESShards(int(shards))
csKeys, err := nodesecurity.CreateCosignerECIESShards(int(shards))
if err != nil {
return err
}
Expand All @@ -191,7 +193,7 @@ func createCosignerECIESShardsCmd() *cobra.Command {
return err
}
filename := filepath.Join(dir, "ecies_keys.json")
if err = signer.WriteCosignerECIESShardFile(c, filename); err != nil {
if err = nodesecurity.WriteCosignerECIESShardFile(c, filename); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Created ECIES Shard %s\n", filename)
Expand All @@ -218,7 +220,7 @@ func createCosignerRSAShardsCmd() *cobra.Command {
return fmt.Errorf("shards must be greater than zero (%d): %w", shards, err)
}

csKeys, err := signer.CreateCosignerRSAShards(int(shards))
csKeys, err := nodesecurity.CreateCosignerRSAShards(int(shards))
if err != nil {
return err
}
Expand All @@ -239,7 +241,7 @@ func createCosignerRSAShardsCmd() *cobra.Command {
return err
}
filename := filepath.Join(dir, "rsa_keys.json")
if err = signer.WriteCosignerRSAShardFile(c, filename); err != nil {
if err = nodesecurity.WriteCosignerRSAShardFile(c, filename); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Created RSA Shard %s\n", filename)
Expand Down
5 changes: 3 additions & 2 deletions cmd/horcrux/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
cometlog "github.com/cometbft/cometbft/libs/log"
"github.com/cometbft/cometbft/libs/service"
"github.com/spf13/cobra"
cconfig "github.com/strangelove-ventures/horcrux/pkg/config"
"github.com/strangelove-ventures/horcrux/signer"
)

Expand Down Expand Up @@ -48,12 +49,12 @@ func startCmd() *cobra.Command {
var services []service.Service

switch config.Config.SignMode {
case signer.SignModeThreshold:
case cconfig.SignModeThreshold:
services, val, err = NewThresholdValidator(cmd.Context(), logger)
if err != nil {
return err
}
case signer.SignModeSingle:
case cconfig.SignModeSingle:
val, err = NewSingleSignerValidator(out, acceptRisk)
if err != nil {
return err
Expand Down
9 changes: 5 additions & 4 deletions cmd/horcrux/cmd/threshold.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

cometlog "github.com/cometbft/cometbft/libs/log"
cometservice "github.com/cometbft/cometbft/libs/service"
"github.com/strangelove-ventures/horcrux/pkg/nodes"
"github.com/strangelove-ventures/horcrux/signer"
)

Expand All @@ -24,11 +25,11 @@ func NewThresholdValidator(

thresholdCfg := config.Config.ThresholdModeConfig

remoteCosigners := make([]signer.Cosigner, 0, len(thresholdCfg.Cosigners)-1)
remoteCosigners := make([]nodes.Cosigner, 0, len(thresholdCfg.Cosigners)-1)

var p2pListen string

var security signer.CosignerSecurity
var security nodes.ICosignerSecurity
var eciesErr error
security, eciesErr = config.CosignerSecurityECIES()
if eciesErr != nil {
Expand All @@ -41,7 +42,7 @@ func NewThresholdValidator(

for _, c := range thresholdCfg.Cosigners {
if c.ShardID != security.GetID() {
rc, err := signer.NewRemoteCosigner(c.ShardID, c.P2PAddr)
rc, err := nodes.NewRemoteCosigner(c.ShardID, c.P2PAddr)
if err != nil {
return nil, nil, fmt.Errorf("failed to initialize remote cosigner: %w", err)
}
Expand All @@ -58,7 +59,7 @@ func NewThresholdValidator(
return nil, nil, fmt.Errorf("cosigner config does not exist for our shard Index %d", security.GetID())
}

localCosigner := signer.NewLocalCosigner(
localCosigner := nodes.NewLocalCosigner(
logger,
&config,
security,
Expand Down
Loading

0 comments on commit d0ff41f

Please sign in to comment.