diff --git a/cmd/horcrux/cmd/address.go b/cmd/horcrux/cmd/address.go index a5551c47..f327c615 100644 --- a/cmd/horcrux/cmd/address.go +++ b/cmd/horcrux/cmd/address.go @@ -6,7 +6,7 @@ import ( "fmt" "strings" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" "github.com/cometbft/cometbft/crypto" cometprivval "github.com/cometbft/cometbft/privval" @@ -35,24 +35,24 @@ func addressCmd() *cobra.Command { chainID := args[0] switch config.Config.SignMode { - case pcosigner.SignModeThreshold: + case cosigner.SignModeThreshold: err := config.Config.ValidateThresholdModeConfig() if err != nil { return err } - keyFile, err := config.KeyFileExistsCosigner(chainID) + keyFile, err := config.KeyFileExistsCosign(chainID) if err != nil { return err } - key, err := pcosigner.LoadCosignerEd25519Key(keyFile) + key, err := cosigner.LoadCosignerEd25519Key(keyFile) if err != nil { return fmt.Errorf("error reading cosigner key: %w, check that key is present for chain ID: %s", err, chainID) } pubKey = key.PubKey - case pcosigner.SignModeSingle: + case cosigner.SignModeSingle: err := config.Config.ValidateSingleSignerConfig() if err != nil { return err @@ -70,7 +70,7 @@ func addressCmd() *cobra.Command { pubKeyAddress := pubKey.Address() - pubKeyJSON, err := pcosigner.PubKey("", pubKey) + pubKeyJSON, err := cosigner.PubKey("", pubKey) if err != nil { return err } @@ -86,7 +86,7 @@ func addressCmd() *cobra.Command { return err } output.ValConsAddress = bech32ValConsAddress - pubKeyBech32, err := pcosigner.PubKey(args[1], pubKey) + pubKeyBech32, err := cosigner.PubKey(args[1], pubKey) if err != nil { return err } diff --git a/cmd/horcrux/cmd/config.go b/cmd/horcrux/cmd/config.go index 05a52121..0c0e6036 100644 --- a/cmd/horcrux/cmd/config.go +++ b/cmd/horcrux/cmd/config.go @@ -4,7 +4,7 @@ import ( "fmt" "os" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" "github.com/spf13/cobra" ) @@ -48,7 +48,7 @@ for threshold pkg mode, --cosigner flags and --threshold flag are required. bare, _ := cmdFlags.GetBool(flagBare) nodes, _ := cmdFlags.GetStringSlice(flagNode) - cn, err := pcosigner.ChainNodesFromFlag(nodes) + cn, err := cosigner.ChainNodesFromFlag(nodes) if err != nil { return err } @@ -60,7 +60,7 @@ for threshold pkg mode, --cosigner flags and --threshold flag are required. config.ConfigFile) } - var cfg pcosigner.Config + var cfg cosigner.Config signMode, _ := cmdFlags.GetString(flagSignMode) keyDirFlag, _ := cmdFlags.GetString(flagKeyDir) @@ -69,21 +69,21 @@ for threshold pkg mode, --cosigner flags and --threshold flag are required. keyDir = &keyDirFlag } debugAddr, _ := cmdFlags.GetString("debug-addr") - if signMode == string(pcosigner.SignModeThreshold) { + if signMode == string(cosigner.SignModeThreshold) { // Threshold Mode Config cosignersFlag, _ := cmdFlags.GetStringSlice(flagCosigner) threshold, _ := cmdFlags.GetInt(flagThreshold) raftTimeout, _ := cmdFlags.GetString(flagRaftTimeout) grpcTimeout, _ := cmdFlags.GetString(flagGRPCTimeout) - cosigners, err := pcosigner.CosignersFromFlag(cosignersFlag) + cosigners, err := cosigner.CosignersFromFlag(cosignersFlag) if err != nil { return err } - cfg = pcosigner.Config{ - SignMode: pcosigner.SignModeThreshold, + cfg = cosigner.Config{ + SignMode: cosigner.SignModeThreshold, PrivValKeyDir: keyDir, - ThresholdModeConfig: &pcosigner.ThresholdModeConfig{ + ThresholdModeConfig: &cosigner.ThresholdModeConfig{ Threshold: threshold, Cosigners: cosigners, GRPCTimeout: grpcTimeout, @@ -100,8 +100,8 @@ for threshold pkg mode, --cosigner flags and --threshold flag are required. } } else { // Single Signer Config - cfg = pcosigner.Config{ - SignMode: pcosigner.SignModeSingle, + cfg = cosigner.Config{ + SignMode: cosigner.SignModeSingle, PrivValKeyDir: keyDir, ChainNodes: cn, DebugAddr: debugAddr, @@ -132,7 +132,7 @@ for threshold pkg mode, --cosigner flags and --threshold flag are required. } f := cmd.Flags() - f.StringP(flagSignMode, "m", string(pcosigner.SignModeThreshold), + f.StringP(flagSignMode, "m", string(cosigner.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"+ diff --git a/cmd/horcrux/cmd/leader_election.go b/cmd/horcrux/cmd/leader_election.go index 8ec1d92e..363693b8 100644 --- a/cmd/horcrux/cmd/leader_election.go +++ b/cmd/horcrux/cmd/leader_election.go @@ -5,7 +5,7 @@ import ( "fmt" "time" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" grpcretry "github.com/grpc-ecosystem/go-grpc-middleware/retry" "github.com/spf13/cobra" @@ -110,21 +110,21 @@ func getLeaderCmd() *cobra.Command { var id int - keyFileECIES, err := config.KeyFileExistsCosignerECIES() + keyFileECIES, err := config.KeyFileExistsCosignECIES() if err != nil { - keyFileRSA, err := config.KeyFileExistsCosignerRSA() + keyFileRSA, err := config.KeyFileExistsCosignRSA() if err != nil { return fmt.Errorf("cosigner encryption keys not found (%s) - (%s): %w", keyFileECIES, keyFileRSA, err) } - key, err := pcosigner.LoadCosignerRSAKey(keyFileRSA) + key, err := cosigner.LoadCosignRSAKey(keyFileRSA) if err != nil { return fmt.Errorf("error reading cosigner key (%s): %w", keyFileRSA, err) } id = key.ID } else { - key, err := pcosigner.LoadCosignerECIESKey(keyFileECIES) + key, err := cosigner.LoadCosignerECIESKey(keyFileECIES) if err != nil { return fmt.Errorf("error reading cosigner key (%s): %w", keyFileECIES, err) } diff --git a/cmd/horcrux/cmd/migrate.go b/cmd/horcrux/cmd/migrate.go index 76ae29b4..28e2f257 100644 --- a/cmd/horcrux/cmd/migrate.go +++ b/cmd/horcrux/cmd/migrate.go @@ -8,7 +8,7 @@ import ( "os" "path/filepath" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" cometcrypto "github.com/cometbft/cometbft/crypto" cometcryptoed25519 "github.com/cometbft/cometbft/crypto/ed25519" @@ -104,7 +104,7 @@ func (key *v2CosignerKey) UnmarshalJSON(data []byte) error { // Prior to the tendermint protobuf migration, the public key bytes in key files // were encoded using the go-amino libraries via - // cdc.MarshalBinaryBare(CosignerEd25519Key.PubKey) + // cdc.MarshalBinaryBare(CosignEd25519Key.PubKey) // // To support reading the public key bytes from these key files, we fallback to // amino unmarshalling if the protobuf unmarshalling fails @@ -219,7 +219,7 @@ func migrateCmd() *cobra.Command { return err } - newEd25519Key := pcosigner.CosignerEd25519Key{ + newEd25519Key := cosigner.CosignEd25519Key{ PubKey: legacyCosignerKey.PubKey, PrivateShard: legacyCosignerKey.ShareKey, ID: legacyCosignerKey.ID, @@ -235,7 +235,7 @@ func migrateCmd() *cobra.Command { return fmt.Errorf("failed to write new Ed25519 key to %s: %w", newEd25519Path, err) } - newRSAKey := pcosigner.CosignerRSAKey{ + newRSAKey := cosigner.CosignRSAKey{ RSAKey: legacyCosignerKey.RSAKey, ID: legacyCosignerKey.ID, RSAPubs: legacyCosignerKey.RSAPubs, @@ -253,10 +253,10 @@ func migrateCmd() *cobra.Command { // only attempt config migration if legacy config exists if legacyCfgErr == nil { - var migratedNodes pcosigner.ChainNodes + var migratedNodes cosigner.ChainNodes for _, n := range legacyCfg.ChainNodes { - migratedNodes = append(migratedNodes, pcosigner.ChainNode{ + migratedNodes = append(migratedNodes, cosigner.ChainNode{ PrivValAddr: n.PrivValAddr, }) } @@ -264,17 +264,17 @@ func migrateCmd() *cobra.Command { config.Config.ChainNodes = migratedNodes config.Config.DebugAddr = legacyCfg.DebugAddr - signMode := pcosigner.SignModeSingle + signMode := cosigner.SignModeSingle if legacyCfg.Cosigner != nil { - signMode = pcosigner.SignModeThreshold + signMode = cosigner.SignModeThreshold - var migratedCosigners pcosigner.CosignersConfig + var migratedCosigners cosigner.CosignersConfig if legacyCfg.Cosigner.P2PListen != "" { migratedCosigners = append( migratedCosigners, - pcosigner.CosignerConfig{ + cosigner.CosignConfig{ ShardID: legacyCosignerKey.ID, P2PAddr: legacyCfg.Cosigner.P2PListen, }, @@ -282,13 +282,13 @@ func migrateCmd() *cobra.Command { } for _, c := range legacyCfg.Cosigner.Peers { - migratedCosigners = append(migratedCosigners, pcosigner.CosignerConfig{ + migratedCosigners = append(migratedCosigners, cosigner.CosignConfig{ ShardID: c.ShareID, P2PAddr: c.P2PAddr, }) } - config.Config.ThresholdModeConfig = &pcosigner.ThresholdModeConfig{ + config.Config.ThresholdModeConfig = &cosigner.ThresholdModeConfig{ Threshold: legacyCfg.Cosigner.Threshold, Cosigners: migratedCosigners, GRPCTimeout: legacyCfg.Cosigner.Timeout, diff --git a/cmd/horcrux/cmd/root.go b/cmd/horcrux/cmd/root.go index de80a03a..9ce6ca00 100644 --- a/cmd/horcrux/cmd/root.go +++ b/cmd/horcrux/cmd/root.go @@ -5,7 +5,7 @@ import ( "os" "path/filepath" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" @@ -13,7 +13,7 @@ import ( "gopkg.in/yaml.v2" ) -var config pcosigner.RuntimeConfig +var config cosigner.RuntimeConfig func rootCmd() *cobra.Command { cmd := &cobra.Command{ @@ -75,7 +75,7 @@ func initConfig() { } else { home = config.HomeDir } - config = pcosigner.RuntimeConfig{ + config = cosigner.RuntimeConfig{ HomeDir: home, ConfigFile: filepath.Join(home, "config.yaml"), StateDir: filepath.Join(home, "state"), diff --git a/cmd/horcrux/cmd/shards.go b/cmd/horcrux/cmd/shards.go index 6ed066c4..ddca071d 100644 --- a/cmd/horcrux/cmd/shards.go +++ b/cmd/horcrux/cmd/shards.go @@ -20,7 +20,7 @@ import ( "os" "path/filepath" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" "github.com/spf13/cobra" ) @@ -113,7 +113,7 @@ func createCosignerEd25519ShardsCmd() *cobra.Command { return nil } - csKeys, err := pcosigner.CreateCosignerEd25519ShardsFromFile(keyFile, threshold, shards) + csKeys, err := cosigner.CreateCosignerEd25519ShardsFromFile(keyFile, threshold, shards) if err != nil { return err } @@ -134,7 +134,7 @@ func createCosignerEd25519ShardsCmd() *cobra.Command { return err } filename := filepath.Join(dir, fmt.Sprintf("%s_shard.json", chainID)) - if err = pcosigner.WriteCosignerEd25519ShardFile(c, filename); err != nil { + if err = cosigner.WriteCosignerEd25519ShardFile(c, filename); err != nil { return err } fmt.Fprintf(cmd.OutOrStdout(), "Created Ed25519 Shard %s\n", filename) @@ -171,7 +171,7 @@ func createCosignerECIESShardsCmd() *cobra.Command { return fmt.Errorf("shards must be greater than zero (%d): %w", shards, err) } - csKeys, err := pcosigner.CreateCosignerECIESShards(int(shards)) + csKeys, err := cosigner.CreateCosignerECIESShards(int(shards)) if err != nil { return err } @@ -192,7 +192,7 @@ func createCosignerECIESShardsCmd() *cobra.Command { return err } filename := filepath.Join(dir, "ecies_keys.json") - if err = pcosigner.WriteCosignerECIESShardFile(c, filename); err != nil { + if err = cosigner.WriteCosignECIESShardFile(c, filename); err != nil { return err } fmt.Fprintf(cmd.OutOrStdout(), "Created ECIES Shard %s\n", filename) @@ -219,7 +219,7 @@ func createCosignerRSAShardsCmd() *cobra.Command { return fmt.Errorf("shards must be greater than zero (%d): %w", shards, err) } - csKeys, err := pcosigner.CreateCosignerRSAShards(int(shards)) + csKeys, err := cosigner.CreateCosignerRSAShards(int(shards)) if err != nil { return err } @@ -240,7 +240,7 @@ func createCosignerRSAShardsCmd() *cobra.Command { return err } filename := filepath.Join(dir, "rsa_keys.json") - if err = pcosigner.WriteCosignerRSAShardFile(c, filename); err != nil { + if err = cosigner.WriteCosignerRSAShardFile(c, filename); err != nil { return err } fmt.Fprintf(cmd.OutOrStdout(), "Created RSA Shard %s\n", filename) diff --git a/cmd/horcrux/cmd/start.go b/cmd/horcrux/cmd/start.go index 4b596d3e..0fe5d708 100644 --- a/cmd/horcrux/cmd/start.go +++ b/cmd/horcrux/cmd/start.go @@ -4,7 +4,7 @@ import ( "fmt" "os" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" "github.com/strangelove-ventures/horcrux/pkg/node" @@ -50,12 +50,12 @@ func startCmd() *cobra.Command { var services []service.Service switch config.Config.SignMode { - case pcosigner.SignModeThreshold: + case cosigner.SignModeThreshold: services, val, err = NewThresholdValidator(logger) if err != nil { return err } - case pcosigner.SignModeSingle: + case cosigner.SignModeSingle: val, err = NewSingleSignerValidator(out, acceptRisk) if err != nil { return err diff --git a/cmd/horcrux/cmd/state.go b/cmd/horcrux/cmd/state.go index 8d523fbf..3fba2a1e 100644 --- a/cmd/horcrux/cmd/state.go +++ b/cmd/horcrux/cmd/state.go @@ -59,7 +59,7 @@ func showStateCmd() *cobra.Command { return err } - cs, err := types.LoadSignState(config.CosignerStateFile(chainID)) + cs, err := types.LoadSignState(config.CosignStateFile(chainID)) if err != nil { return err } @@ -100,7 +100,7 @@ func setStateCmd() *cobra.Command { return err } - cs, err := types.LoadOrCreateSignState(config.CosignerStateFile(chainID)) + cs, err := types.LoadOrCreateSignState(config.CosignStateFile(chainID)) if err != nil { return err } @@ -165,7 +165,7 @@ func importStateCmd() *cobra.Command { } // shareStateFile does not exist during default config init, so create if necessary - cs, err := types.LoadOrCreateSignState(config.CosignerStateFile(chainID)) + cs, err := types.LoadOrCreateSignState(config.CosignStateFile(chainID)) if err != nil { return err } diff --git a/cmd/horcrux/cmd/threshold.go b/cmd/horcrux/cmd/threshold.go index bc973fbe..d33597aa 100644 --- a/cmd/horcrux/cmd/threshold.go +++ b/cmd/horcrux/cmd/threshold.go @@ -6,7 +6,7 @@ import ( "path/filepath" "time" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" "github.com/strangelove-ventures/horcrux/pkg/node" @@ -24,17 +24,16 @@ func NewThresholdValidator( } thresholdCfg := config.Config.ThresholdModeConfig - // NOTE: Shouldnt this be a list of concrete type instead of interface type? remoteCosigners := make([]node.ICosigner, 0, len(thresholdCfg.Cosigners)-1) var p2pListen string - var security pcosigner.ICosignerSecurity + var security cosigner.ICosignerSecurity var eciesErr error - security, eciesErr = config.CosignerSecurityECIES() + security, eciesErr = config.SecurityECIES() if eciesErr != nil { var rsaErr error - security, rsaErr = config.CosignerSecurityRSA() + security, rsaErr = config.SecurityRSA() if rsaErr != nil { return nil, nil, fmt.Errorf("failed to initialize cosigner ECIES / RSA security : %w / %w", eciesErr, rsaErr) } @@ -44,7 +43,7 @@ func NewThresholdValidator( if c.ShardID != security.GetID() { remoteCosigners = append( remoteCosigners, - pcosigner.NewRemoteCosigner(c.ShardID, c.P2PAddr), + cosigner.NewRemoteCosigner(c.ShardID, c.P2PAddr), ) } else { p2pListen = c.P2PAddr @@ -55,7 +54,7 @@ func NewThresholdValidator( return nil, nil, fmt.Errorf("cosigner config does not exist for our shard ID %d", security.GetID()) } - localCosigner := pcosigner.NewLocalCosigner( + localCosigner := cosigner.NewLocalCosigner( logger, &config, security, diff --git a/pkg/pcosigner/config.go b/pkg/cosigner/config.go similarity index 90% rename from pkg/pcosigner/config.go rename to pkg/cosigner/config.go index d4dcb694..04639ebb 100644 --- a/pkg/pcosigner/config.go +++ b/pkg/cosigner/config.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner import ( "fmt" @@ -106,8 +106,8 @@ type RuntimeConfig struct { Config Config } -func (c RuntimeConfig) CosignerSecurityECIES() (*CosignerSecurityECIES, error) { - keyFile, err := c.KeyFileExistsCosignerECIES() +func (c RuntimeConfig) SecurityECIES() (*CosignSecurityECIES, error) { + keyFile, err := c.KeyFileExistsCosignECIES() if err != nil { return nil, err } @@ -120,13 +120,13 @@ func (c RuntimeConfig) CosignerSecurityECIES() (*CosignerSecurityECIES, error) { return NewCosignerSecurityECIES(key), nil } -func (c RuntimeConfig) CosignerSecurityRSA() (*CosignerSecurityRSA, error) { - keyFile, err := c.KeyFileExistsCosignerRSA() +func (c RuntimeConfig) SecurityRSA() (*CosignSecurityRSA, error) { + keyFile, err := c.KeyFileExistsCosignRSA() if err != nil { return nil, err } - key, err := LoadCosignerRSAKey(keyFile) + key, err := LoadCosignRSAKey(keyFile) if err != nil { return nil, fmt.Errorf("error reading cosigner key (%s): %w", keyFile, err) } @@ -177,7 +177,7 @@ func (c RuntimeConfig) PrivValStateFile(chainID string) string { return filepath.Join(c.StateDir, fmt.Sprintf("%s_priv_validator_state.json", chainID)) } -func (c RuntimeConfig) CosignerStateFile(chainID string) string { +func (c RuntimeConfig) CosignStateFile(chainID string) string { return filepath.Join(c.StateDir, fmt.Sprintf("%s_share_sign_state.json", chainID)) } @@ -205,17 +205,17 @@ func (c RuntimeConfig) KeyFileExistsSingleSigner(chainID string) (string, error) return keyFile, fileExists(keyFile) } -func (c RuntimeConfig) KeyFileExistsCosigner(chainID string) (string, error) { +func (c RuntimeConfig) KeyFileExistsCosign(chainID string) (string, error) { keyFile := c.KeyFilePathCosigner(chainID) return keyFile, fileExists(keyFile) } -func (c RuntimeConfig) KeyFileExistsCosignerRSA() (string, error) { +func (c RuntimeConfig) KeyFileExistsCosignRSA() (string, error) { keyFile := c.KeyFilePathCosignerRSA() return keyFile, fileExists(keyFile) } -func (c RuntimeConfig) KeyFileExistsCosignerECIES() (string, error) { +func (c RuntimeConfig) KeyFileExistsCosignECIES() (string, error) { keyFile := c.KeyFilePathCosignerECIES() return keyFile, fileExists(keyFile) } @@ -236,13 +236,13 @@ func (cfg *ThresholdModeConfig) LeaderElectMultiAddress() (string, error) { return client.MultiAddress(addresses) } -// CosignerConfig is the on disk format representing a cosigner for threshold sign mode. -type CosignerConfig struct { +// CosignConfig is the on disk format representing a cosigner for threshold sign mode. +type CosignConfig struct { ShardID int `yaml:"shardID"` P2PAddr string `yaml:"p2pAddr"` } -type CosignersConfig []CosignerConfig +type CosignersConfig []CosignConfig func (cosigners CosignersConfig) Validate() error { // Check IDs to make sure none are duplicated @@ -283,7 +283,7 @@ func (cosigners CosignersConfig) Validate() error { return nil } -func duplicateCosigners(cosigners []CosignerConfig) (duplicates map[int][]string) { +func duplicateCosigners(cosigners []CosignConfig) (duplicates map[int][]string) { idAddrs := make(map[int][]string) for _, cosigner := range cosigners { // Collect all addresses assigned to each cosigner. @@ -306,10 +306,10 @@ func duplicateCosigners(cosigners []CosignerConfig) (duplicates map[int][]string return idAddrs } -func CosignersFromFlag(cosigners []string) (out []CosignerConfig, err error) { +func CosignersFromFlag(cosigners []string) (out []CosignConfig, err error) { var errs []error for i, c := range cosigners { - out = append(out, CosignerConfig{ShardID: i + 1, P2PAddr: c}) + out = append(out, CosignConfig{ShardID: i + 1, P2PAddr: c}) } if len(errs) > 0 { return nil, nil diff --git a/pkg/pcosigner/config_test.go b/pkg/cosigner/config_test.go similarity index 82% rename from pkg/pcosigner/config_test.go rename to pkg/cosigner/config_test.go index 70aef199..83b8b6e9 100644 --- a/pkg/pcosigner/config_test.go +++ b/pkg/cosigner/config_test.go @@ -1,4 +1,4 @@ -package pcosigner_test +package cosigner_test import ( "fmt" @@ -8,7 +8,7 @@ import ( "path/filepath" "testing" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" "github.com/stretchr/testify/require" ) @@ -16,8 +16,8 @@ import ( const testChainID = "test" func TestNodes(t *testing.T) { - c := pcosigner.Config{ - ChainNodes: pcosigner.ChainNodes{ + c := cosigner.Config{ + ChainNodes: cosigner.ChainNodes{ { PrivValAddr: "tcp://0.0.0.0:1234", }, @@ -33,15 +33,15 @@ func TestNodes(t *testing.T) { func TestValidateSingleSignerConfig(t *testing.T) { type testCase struct { name string - config pcosigner.Config + config cosigner.Config expectErr error } testCases := []testCase{ { name: "valid config", - config: pcosigner.Config{ - ChainNodes: []pcosigner.ChainNode{ + config: cosigner.Config{ + ChainNodes: []cosigner.ChainNode{ { PrivValAddr: "tcp://127.0.0.1:1234", }, @@ -51,15 +51,15 @@ func TestValidateSingleSignerConfig(t *testing.T) { }, { name: "no nodes configured", - config: pcosigner.Config{ + config: cosigner.Config{ ChainNodes: nil, }, expectErr: fmt.Errorf("need to have chainNodes configured for priv-val connection"), }, { name: "invalid node address", - config: pcosigner.Config{ - ChainNodes: []pcosigner.ChainNode{ + config: cosigner.Config{ + ChainNodes: []cosigner.ChainNode{ { PrivValAddr: "abc://\\invalid_addr", }, @@ -83,19 +83,19 @@ func TestValidateSingleSignerConfig(t *testing.T) { func TestValidateThresholdModeConfig(t *testing.T) { type testCase struct { name string - config pcosigner.Config + config cosigner.Config expectErr error } testCases := []testCase{ { name: "valid config", - config: pcosigner.Config{ - ThresholdModeConfig: &pcosigner.ThresholdModeConfig{ + config: cosigner.Config{ + ThresholdModeConfig: &cosigner.ThresholdModeConfig{ Threshold: 2, RaftTimeout: "1000ms", GRPCTimeout: "1000ms", - Cosigners: pcosigner.CosignersConfig{ + Cosigners: cosigner.CosignersConfig{ { ShardID: 1, P2PAddr: "tcp://127.0.0.1:2223", @@ -110,7 +110,7 @@ func TestValidateThresholdModeConfig(t *testing.T) { }, }, }, - ChainNodes: []pcosigner.ChainNode{ + ChainNodes: []cosigner.ChainNode{ { PrivValAddr: "tcp://127.0.0.1:1234", }, @@ -126,8 +126,8 @@ func TestValidateThresholdModeConfig(t *testing.T) { }, { name: "no cosigner config", - config: pcosigner.Config{ - ChainNodes: []pcosigner.ChainNode{ + config: cosigner.Config{ + ChainNodes: []cosigner.ChainNode{ { PrivValAddr: "tcp://127.0.0.1:1234", }, @@ -143,12 +143,12 @@ func TestValidateThresholdModeConfig(t *testing.T) { }, { name: "invalid p2p listen", - config: pcosigner.Config{ - ThresholdModeConfig: &pcosigner.ThresholdModeConfig{ + config: cosigner.Config{ + ThresholdModeConfig: &cosigner.ThresholdModeConfig{ Threshold: 2, RaftTimeout: "1000ms", GRPCTimeout: "1000ms", - Cosigners: pcosigner.CosignersConfig{ + Cosigners: cosigner.CosignersConfig{ { ShardID: 1, P2PAddr: ":2222", @@ -163,7 +163,7 @@ func TestValidateThresholdModeConfig(t *testing.T) { }, }, }, - ChainNodes: []pcosigner.ChainNode{ + ChainNodes: []cosigner.ChainNode{ { PrivValAddr: "tcp://127.0.0.1:1234", }, @@ -183,12 +183,12 @@ func TestValidateThresholdModeConfig(t *testing.T) { }, { name: "not enough cosigners", - config: pcosigner.Config{ - ThresholdModeConfig: &pcosigner.ThresholdModeConfig{ + config: cosigner.Config{ + ThresholdModeConfig: &cosigner.ThresholdModeConfig{ Threshold: 3, RaftTimeout: "1000ms", GRPCTimeout: "1000ms", - Cosigners: pcosigner.CosignersConfig{ + Cosigners: cosigner.CosignersConfig{ { ShardID: 1, P2PAddr: "tcp://127.0.0.1:2222", @@ -199,7 +199,7 @@ func TestValidateThresholdModeConfig(t *testing.T) { }, }, }, - ChainNodes: []pcosigner.ChainNode{ + ChainNodes: []cosigner.ChainNode{ { PrivValAddr: "tcp://127.0.0.1:1234", }, @@ -215,12 +215,12 @@ func TestValidateThresholdModeConfig(t *testing.T) { }, { name: "invalid raft timeout", - config: pcosigner.Config{ - ThresholdModeConfig: &pcosigner.ThresholdModeConfig{ + config: cosigner.Config{ + ThresholdModeConfig: &cosigner.ThresholdModeConfig{ Threshold: 2, GRPCTimeout: "1000ms", RaftTimeout: "1000", - Cosigners: pcosigner.CosignersConfig{ + Cosigners: cosigner.CosignersConfig{ { ShardID: 1, P2PAddr: "tcp://127.0.0.1:2222", @@ -235,7 +235,7 @@ func TestValidateThresholdModeConfig(t *testing.T) { }, }, }, - ChainNodes: []pcosigner.ChainNode{ + ChainNodes: []cosigner.ChainNode{ { PrivValAddr: "tcp://127.0.0.1:1234", }, @@ -251,12 +251,12 @@ func TestValidateThresholdModeConfig(t *testing.T) { }, { name: "invalid grpc timeout", - config: pcosigner.Config{ - ThresholdModeConfig: &pcosigner.ThresholdModeConfig{ + config: cosigner.Config{ + ThresholdModeConfig: &cosigner.ThresholdModeConfig{ Threshold: 2, GRPCTimeout: "1000", RaftTimeout: "1000ms", - Cosigners: pcosigner.CosignersConfig{ + Cosigners: cosigner.CosignersConfig{ { ShardID: 1, P2PAddr: "tcp://127.0.0.1:2222", @@ -271,7 +271,7 @@ func TestValidateThresholdModeConfig(t *testing.T) { }, }, }, - ChainNodes: []pcosigner.ChainNode{ + ChainNodes: []cosigner.ChainNode{ { PrivValAddr: "tcp://127.0.0.1:1234", }, @@ -287,12 +287,12 @@ func TestValidateThresholdModeConfig(t *testing.T) { }, { name: "no nodes configured", - config: pcosigner.Config{ - ThresholdModeConfig: &pcosigner.ThresholdModeConfig{ + config: cosigner.Config{ + ThresholdModeConfig: &cosigner.ThresholdModeConfig{ Threshold: 2, RaftTimeout: "1000ms", GRPCTimeout: "1000ms", - Cosigners: pcosigner.CosignersConfig{ + Cosigners: cosigner.CosignersConfig{ { ShardID: 1, P2PAddr: "tcp://127.0.0.1:2222", @@ -313,12 +313,12 @@ func TestValidateThresholdModeConfig(t *testing.T) { }, { name: "invalid node address", - config: pcosigner.Config{ - ThresholdModeConfig: &pcosigner.ThresholdModeConfig{ + config: cosigner.Config{ + ThresholdModeConfig: &cosigner.ThresholdModeConfig{ Threshold: 2, RaftTimeout: "1000ms", GRPCTimeout: "1000ms", - Cosigners: pcosigner.CosignersConfig{ + Cosigners: cosigner.CosignersConfig{ { ShardID: 1, P2PAddr: "tcp://127.0.0.1:2222", @@ -333,7 +333,7 @@ func TestValidateThresholdModeConfig(t *testing.T) { }, }, }, - ChainNodes: []pcosigner.ChainNode{ + ChainNodes: []cosigner.ChainNode{ { PrivValAddr: "abc://\\invalid_addr", }, @@ -356,7 +356,7 @@ func TestValidateThresholdModeConfig(t *testing.T) { func TestRuntimeConfigKeyFilePath(t *testing.T) { dir := t.TempDir() - c := pcosigner.RuntimeConfig{ + c := cosigner.RuntimeConfig{ HomeDir: dir, } @@ -370,7 +370,7 @@ func TestRuntimeConfigKeyFilePath(t *testing.T) { func TestRuntimeConfigPrivValStateFile(t *testing.T) { dir := t.TempDir() - c := pcosigner.RuntimeConfig{ + c := cosigner.RuntimeConfig{ StateDir: dir, } @@ -380,15 +380,15 @@ func TestRuntimeConfigPrivValStateFile(t *testing.T) { func TestRuntimeConfigWriteConfigFile(t *testing.T) { dir := t.TempDir() configFile := filepath.Join(dir, "config.yaml") - c := pcosigner.RuntimeConfig{ + c := cosigner.RuntimeConfig{ ConfigFile: configFile, - Config: pcosigner.Config{ - SignMode: pcosigner.SignModeThreshold, - ThresholdModeConfig: &pcosigner.ThresholdModeConfig{ + Config: cosigner.Config{ + SignMode: cosigner.SignModeThreshold, + ThresholdModeConfig: &cosigner.ThresholdModeConfig{ Threshold: 2, RaftTimeout: "1000ms", GRPCTimeout: "1000ms", - Cosigners: pcosigner.CosignersConfig{ + Cosigners: cosigner.CosignersConfig{ { ShardID: 1, P2PAddr: "tcp://127.0.0.1:2222", @@ -403,7 +403,7 @@ func TestRuntimeConfigWriteConfigFile(t *testing.T) { }, }, }, - ChainNodes: []pcosigner.ChainNode{ + ChainNodes: []cosigner.ChainNode{ { PrivValAddr: "tcp://127.0.0.1:1234", }, @@ -442,12 +442,12 @@ debugAddr: "" func TestRuntimeConfigKeyFileExists(t *testing.T) { dir := t.TempDir() - c := pcosigner.RuntimeConfig{ + c := cosigner.RuntimeConfig{ HomeDir: dir, } // Test cosigner - keyFile, err := c.KeyFileExistsCosigner(testChainID) + keyFile, err := c.KeyFileExistsCosign(testChainID) require.Error(t, err) require.Equal(t, fmt.Errorf( @@ -463,7 +463,7 @@ func TestRuntimeConfigKeyFileExists(t *testing.T) { err = os.WriteFile(keyFile, []byte{}, 0600) require.NoError(t, err) - _, err = c.KeyFileExistsCosigner(testChainID) + _, err = c.KeyFileExistsCosign(testChainID) require.NoError(t, err) // Test single pcosigner @@ -488,11 +488,11 @@ func TestRuntimeConfigKeyFileExists(t *testing.T) { } func TestThresholdModeConfigLeaderElectMultiAddress(t *testing.T) { - c := &pcosigner.ThresholdModeConfig{ + c := &cosigner.ThresholdModeConfig{ Threshold: 2, RaftTimeout: "1000ms", GRPCTimeout: "1000ms", - Cosigners: pcosigner.CosignersConfig{ + Cosigners: cosigner.CosignersConfig{ { ShardID: 1, P2PAddr: "tcp://127.0.0.1:2222", @@ -516,13 +516,13 @@ func TestThresholdModeConfigLeaderElectMultiAddress(t *testing.T) { func TestCosignerRSAPubKeysConfigValidate(t *testing.T) { type testCase struct { name string - cosigners pcosigner.CosignersConfig + cosigners cosigner.CosignersConfig expectErr error } testCases := []testCase{ { name: "valid config", - cosigners: pcosigner.CosignersConfig{ + cosigners: cosigner.CosignersConfig{ { ShardID: 1, P2PAddr: "tcp://127.0.0.1:2222", @@ -540,7 +540,7 @@ func TestCosignerRSAPubKeysConfigValidate(t *testing.T) { }, { name: "too many cosigners", - cosigners: pcosigner.CosignersConfig{ + cosigners: cosigner.CosignersConfig{ { ShardID: 2, P2PAddr: "tcp://127.0.0.1:2223", @@ -554,7 +554,7 @@ func TestCosignerRSAPubKeysConfigValidate(t *testing.T) { }, { name: "duplicate cosigner", - cosigners: pcosigner.CosignersConfig{ + cosigners: cosigner.CosignersConfig{ { ShardID: 2, P2PAddr: "tcp://127.0.0.1:2223", @@ -597,7 +597,7 @@ func TestCosignersFromFlag(t *testing.T) { } for _, tc := range testCases { - _, err := pcosigner.CosignersFromFlag(tc.cosigners) + _, err := cosigner.CosignersFromFlag(tc.cosigners) if tc.expectErr == nil { require.NoError(t, err, tc.name) } else { diff --git a/pkg/pcosigner/cosigner.go b/pkg/cosigner/cosigner.go similarity index 58% rename from pkg/pcosigner/cosigner.go rename to pkg/cosigner/cosigner.go index 997c3363..b57aa2fa 100644 --- a/pkg/pcosigner/cosigner.go +++ b/pkg/cosigner/cosigner.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner import ( "time" @@ -8,24 +8,24 @@ import ( "github.com/strangelove-ventures/horcrux/pkg/proto" ) -type CosignerSignBlockResponse struct { +type SignBlockResponse struct { Signature []byte } -// CosignerSignRequest is sent to a co-signer to obtain their signature for the SignBytes +// SignRequest is sent to a co-signer to obtain their signature for the SignBytes // The SignBytes should be a serialized block -type CosignerSignRequest struct { +type SignRequest struct { ChainID string SignBytes []byte } -type CosignerSignResponse struct { +type SignResponse struct { NoncePublic []byte Timestamp time.Time Signature []byte } -type CosignerNonce struct { +type CosignNonce struct { SourceID int DestinationID int PubKey []byte @@ -33,7 +33,7 @@ type CosignerNonce struct { Signature []byte } -func (secretPart *CosignerNonce) toProto() *proto.Nonce { +func (secretPart *CosignNonce) toProto() *proto.Nonce { return &proto.Nonce{ SourceID: int32(secretPart.SourceID), DestinationID: int32(secretPart.DestinationID), @@ -43,18 +43,18 @@ func (secretPart *CosignerNonce) toProto() *proto.Nonce { } } -// CosignerNonces is a list of CosignerNonce -type CosignerNonces []CosignerNonce +// CosignNonces is a list of CosignNonce +type CosignNonces []CosignNonce -func (secretParts CosignerNonces) ToProto() (out []*proto.Nonce) { +func (secretParts CosignNonces) ToProto() (out []*proto.Nonce) { for _, secretPart := range secretParts { out = append(out, secretPart.toProto()) } return } -func CosignerNonceFromProto(secretPart *proto.Nonce) CosignerNonce { - return CosignerNonce{ +func CosignNonceFromProto(secretPart *proto.Nonce) CosignNonce { + return CosignNonce{ SourceID: int(secretPart.SourceID), DestinationID: int(secretPart.DestinationID), PubKey: secretPart.PubKey, @@ -63,15 +63,15 @@ func CosignerNonceFromProto(secretPart *proto.Nonce) CosignerNonce { } } -func CosignerNoncesFromProto(secretParts []*proto.Nonce) []CosignerNonce { - out := make([]CosignerNonce, len(secretParts)) +func CosignNoncesFromProto(secretParts []*proto.Nonce) []CosignNonce { + out := make([]CosignNonce, len(secretParts)) for i, secretPart := range secretParts { - out[i] = CosignerNonceFromProto(secretPart) + out[i] = CosignNonceFromProto(secretPart) } return out } -type CosignerSetNonceRequest struct { +type SetNonceRequest struct { ChainID string SourceID int PubKey []byte @@ -83,13 +83,13 @@ type CosignerSetNonceRequest struct { Timestamp time.Time } -type CosignerNoncesResponse struct { - Nonces []CosignerNonce +type NoncesResponse struct { + Nonces []CosignNonce } -type CosignerSetNoncesAndSignRequest struct { +type SetNoncesAndSignRequest struct { ChainID string - Nonces []CosignerNonce + Nonces []CosignNonce HRST types.HRSTKey SignBytes []byte } diff --git a/pkg/pcosigner/cosigner_key.go b/pkg/cosigner/cosigner_key.go similarity index 80% rename from pkg/pcosigner/cosigner_key.go rename to pkg/cosigner/cosigner_key.go index b64f4fb3..3190ce19 100644 --- a/pkg/pcosigner/cosigner_key.go +++ b/pkg/cosigner/cosigner_key.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner import ( "encoding/json" @@ -11,15 +11,15 @@ import ( amino "github.com/tendermint/go-amino" ) -// CosignerEd25519Key is a single Ed255219 key shard for an m-of-n threshold signer. -type CosignerEd25519Key struct { +// CosignEd25519Key is a single Ed255219 key shard for an m-of-n threshold signer. +type CosignEd25519Key struct { PubKey cometcrypto.PubKey `json:"pubKey"` PrivateShard []byte `json:"privateShard"` ID int `json:"id"` } -func (key *CosignerEd25519Key) MarshalJSON() ([]byte, error) { - type Alias CosignerEd25519Key +func (key *CosignEd25519Key) MarshalJSON() ([]byte, error) { + type Alias CosignEd25519Key protoPubkey, err := cometcryptoencoding.PubKeyToProto(key.PubKey) if err != nil { @@ -41,8 +41,8 @@ func (key *CosignerEd25519Key) MarshalJSON() ([]byte, error) { }) } -func (key *CosignerEd25519Key) UnmarshalJSON(data []byte) error { - type Alias CosignerEd25519Key +func (key *CosignEd25519Key) UnmarshalJSON(data []byte) error { + type Alias CosignEd25519Key aux := &struct { PubkeyBytes []byte `json:"pubKey"` @@ -85,9 +85,9 @@ func (key *CosignerEd25519Key) UnmarshalJSON(data []byte) error { return nil } -// LoadCosignerEd25519Key loads a CosignerEd25519Key from file. -func LoadCosignerEd25519Key(file string) (CosignerEd25519Key, error) { - pvKey := CosignerEd25519Key{} +// LoadCosignerEd25519Key loads a CosignEd25519Key from file. +func LoadCosignerEd25519Key(file string) (CosignEd25519Key, error) { + pvKey := CosignEd25519Key{} keyJSONBytes, err := os.ReadFile(file) if err != nil { return pvKey, err diff --git a/pkg/pcosigner/cosigner_key_shares.go b/pkg/cosigner/cosigner_key_shares.go similarity index 74% rename from pkg/pcosigner/cosigner_key_shares.go rename to pkg/cosigner/cosigner_key_shares.go index 92bedc4e..68459989 100644 --- a/pkg/pcosigner/cosigner_key_shares.go +++ b/pkg/cosigner/cosigner_key_shares.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner import ( "crypto/rand" @@ -14,8 +14,8 @@ import ( "golang.org/x/sync/errgroup" ) -// CreateCosignerEd25519ShardsFromFile creates CosignerEd25519Key objects from a priv_validator_key.json file -func CreateCosignerEd25519ShardsFromFile(priv string, threshold, shards uint8) ([]CosignerEd25519Key, error) { +// CreateCosignerEd25519ShardsFromFile creates CosignEd25519Key objects from a priv_validator_key.json file +func CreateCosignerEd25519ShardsFromFile(priv string, threshold, shards uint8) ([]CosignEd25519Key, error) { pv, err := ReadPrivValidatorFile(priv) if err != nil { return nil, err @@ -23,15 +23,15 @@ func CreateCosignerEd25519ShardsFromFile(priv string, threshold, shards uint8) ( return CreateCosignerEd25519Shards(pv, threshold, shards), nil } -// CreateCosignerEd25519Shards creates CosignerEd25519Key objects from a privval.FilePVKey +// CreateCosignerEd25519Shards creates CosignEd25519Key objects from a privval.FilePVKey // by splitting the secret using Shamir secret sharing. -func CreateCosignerEd25519Shards(pv privval.FilePVKey, threshold, shards uint8) []CosignerEd25519Key { +func CreateCosignerEd25519Shards(pv privval.FilePVKey, threshold, shards uint8) []CosignEd25519Key { // tsed25519.DealShares splits the secret using Shamir Secret Sharing (Note its: no verifiable secret sharing) // privshards is shamir shares privShards := tsed25519.DealShares(tsed25519.ExpandSecret(pv.PrivKey.Bytes()[:32]), threshold, shards) - out := make([]CosignerEd25519Key, shards) + out := make([]CosignEd25519Key, shards) for i, shard := range privShards { - out[i] = CosignerEd25519Key{ + out[i] = CosignEd25519Key{ PubKey: pv.PubKey, PrivateShard: shard, ID: i + 1, @@ -40,15 +40,15 @@ func CreateCosignerEd25519Shards(pv privval.FilePVKey, threshold, shards uint8) return out } -// CreateCosignerRSAShards generate CosignerRSAKey objects. -func CreateCosignerRSAShards(shards int) ([]CosignerRSAKey, error) { +// CreateCosignerRSAShards generate CosignRSAKey objects. +func CreateCosignerRSAShards(shards int) ([]CosignRSAKey, error) { rsaKeys, pubKeys, err := makeRSAKeys(shards) if err != nil { return nil, err } - out := make([]CosignerRSAKey, shards) + out := make([]CosignRSAKey, shards) for i, key := range rsaKeys { - out[i] = CosignerRSAKey{ + out[i] = CosignRSAKey{ ID: i + 1, RSAKey: *key, RSAPubs: pubKeys, @@ -70,7 +70,7 @@ func ReadPrivValidatorFile(priv string) (out privval.FilePVKey, err error) { } // WriteCosignerEd25519ShardFile writes a cosigner Ed25519 key to a given file name. -func WriteCosignerEd25519ShardFile(cosigner CosignerEd25519Key, file string) error { +func WriteCosignerEd25519ShardFile(cosigner CosignEd25519Key, file string) error { jsonBytes, err := json.Marshal(&cosigner) if err != nil { return err @@ -79,7 +79,7 @@ func WriteCosignerEd25519ShardFile(cosigner CosignerEd25519Key, file string) err } // WriteCosignerRSAShardFile writes a cosigner RSA key to a given file name. -func WriteCosignerRSAShardFile(cosigner CosignerRSAKey, file string) error { +func WriteCosignerRSAShardFile(cosigner CosignRSAKey, file string) error { jsonBytes, err := json.Marshal(&cosigner) if err != nil { return err @@ -87,15 +87,15 @@ func WriteCosignerRSAShardFile(cosigner CosignerRSAKey, file string) error { return os.WriteFile(file, jsonBytes, 0600) } -// CreateCosignerECIESShards generates CosignerECIESKey objects. -func CreateCosignerECIESShards(shards int) ([]CosignerECIESKey, error) { +// CreateCosignerECIESShards generates CosignEciesKey objects. +func CreateCosignerECIESShards(shards int) ([]CosignEciesKey, error) { eciesKeys, pubKeys, err := makeECIESKeys(shards) if err != nil { return nil, err } - out := make([]CosignerECIESKey, shards) + out := make([]CosignEciesKey, shards) for i, key := range eciesKeys { - out[i] = CosignerECIESKey{ + out[i] = CosignEciesKey{ ID: i + 1, ECIESKey: key, ECIESPubs: pubKeys, @@ -104,8 +104,8 @@ func CreateCosignerECIESShards(shards int) ([]CosignerECIESKey, error) { return out, nil } -// WriteCosignerECIESShardFile writes a cosigner ECIES key to a given file name. -func WriteCosignerECIESShardFile(cosigner CosignerECIESKey, file string) error { +// WriteCosignECIESShardFile writes a cosigner ECIES key to a given file name. +func WriteCosignECIESShardFile(cosigner CosignEciesKey, file string) error { jsonBytes, err := json.Marshal(&cosigner) if err != nil { return err diff --git a/pkg/pcosigner/cosigner_security.go b/pkg/cosigner/cosigner_security.go similarity index 92% rename from pkg/pcosigner/cosigner_security.go rename to pkg/cosigner/cosigner_security.go index 3786833d..718e173a 100644 --- a/pkg/pcosigner/cosigner_security.go +++ b/pkg/cosigner/cosigner_security.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner // ICosignerSecurity is an interface for the security layer of the cosigner. type ICosignerSecurity interface { @@ -10,7 +10,7 @@ type ICosignerSecurity interface { id int, noncePub []byte, nonceShare []byte, - ) (CosignerNonce, error) + ) (CosignNonce, error) // DecryptAndVerify decrypts the nonce and verifies the signature to authenticate the source cosigner. DecryptAndVerify( diff --git a/pkg/pcosigner/cosigner_security_ecies.go b/pkg/cosigner/cosigner_security_ecies.go similarity index 77% rename from pkg/pcosigner/cosigner_security_ecies.go rename to pkg/cosigner/cosigner_security_ecies.go index 2fc50d39..d3499a3e 100644 --- a/pkg/pcosigner/cosigner_security_ecies.go +++ b/pkg/cosigner/cosigner_security_ecies.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner import ( "crypto/ecdsa" @@ -15,30 +15,30 @@ import ( "golang.org/x/sync/errgroup" ) -var _ ICosignerSecurity = &CosignerSecurityECIES{} +var _ ICosignerSecurity = &CosignSecurityECIES{} -// CosignerSecurityECIES is an implementation of CosignerSecurity +// CosignSecurityECIES is an implementation of CosignerSecurity // using ECIES for encryption and ECDSA for digital signature. -type CosignerSecurityECIES struct { - key CosignerECIESKey - eciesPubKeys map[int]CosignerECIESPubKey +type CosignSecurityECIES struct { + key CosignEciesKey + eciesPubKeys map[int]CosignECIESPubKey } -// CosignerECIESKey is a cosigner's ECIES public key. -type CosignerECIESPubKey struct { +// CosignEciesKey is a cosigner's ECIES public key. +type CosignECIESPubKey struct { ID int PublicKey *ecies.PublicKey } -// CosignerECIESKey is an ECIES key for an m-of-n threshold signer, composed of a private key and n public keys. -type CosignerECIESKey struct { +// CosignEciesKey is an ECIES key for an m-of-n threshold signer, composed of a private key and n public keys. +type CosignEciesKey struct { ECIESKey *ecies.PrivateKey `json:"eciesKey"` ID int `json:"id"` ECIESPubs []*ecies.PublicKey `json:"eciesPubs"` } -func (key *CosignerECIESKey) MarshalJSON() ([]byte, error) { - type Alias CosignerECIESKey +func (key *CosignEciesKey) MarshalJSON() ([]byte, error) { + type Alias CosignEciesKey // marshal our private key and all public keys privateBytes := key.ECIESKey.D.Bytes() @@ -62,8 +62,8 @@ func (key *CosignerECIESKey) MarshalJSON() ([]byte, error) { }) } -func (key *CosignerECIESKey) UnmarshalJSON(data []byte) error { - type Alias CosignerECIESKey +func (key *CosignEciesKey) UnmarshalJSON(data []byte) error { + type Alias CosignEciesKey aux := &struct { ECIESKey []byte `json:"eciesKey"` @@ -97,9 +97,9 @@ func (key *CosignerECIESKey) UnmarshalJSON(data []byte) error { return nil } -// LoadCosignerECIESKey loads a CosignerECIESKey from file. -func LoadCosignerECIESKey(file string) (CosignerECIESKey, error) { - pvKey := CosignerECIESKey{} +// LoadCosignerECIESKey loads a CosignEciesKey from file. +func LoadCosignerECIESKey(file string) (CosignEciesKey, error) { + pvKey := CosignEciesKey{} keyJSONBytes, err := os.ReadFile(file) if err != nil { return pvKey, err @@ -113,15 +113,15 @@ func LoadCosignerECIESKey(file string) (CosignerECIESKey, error) { return pvKey, nil } -// NewCosignerSecurityECIES creates a new CosignerSecurityECIES. -func NewCosignerSecurityECIES(key CosignerECIESKey) *CosignerSecurityECIES { - c := &CosignerSecurityECIES{ +// NewCosignerSecurityECIES creates a new CosignSecurityECIES. +func NewCosignerSecurityECIES(key CosignEciesKey) *CosignSecurityECIES { + c := &CosignSecurityECIES{ key: key, - eciesPubKeys: make(map[int]CosignerECIESPubKey, len(key.ECIESPubs)), + eciesPubKeys: make(map[int]CosignECIESPubKey, len(key.ECIESPubs)), } for i, pubKey := range key.ECIESPubs { - c.eciesPubKeys[i+1] = CosignerECIESPubKey{ + c.eciesPubKeys[i+1] = CosignECIESPubKey{ ID: i + 1, PublicKey: pubKey, } @@ -131,13 +131,13 @@ func NewCosignerSecurityECIES(key CosignerECIESKey) *CosignerSecurityECIES { } // GetID returns the ID of the cosigner. -func (c *CosignerSecurityECIES) GetID() int { +func (c *CosignSecurityECIES) GetID() int { return c.key.ID } // EncryptAndSign encrypts the nonce and signs it for authentication. -func (c *CosignerSecurityECIES) EncryptAndSign(id int, noncePub []byte, nonceShare []byte) (CosignerNonce, error) { - nonce := CosignerNonce{ +func (c *CosignSecurityECIES) EncryptAndSign(id int, noncePub []byte, nonceShare []byte) (CosignNonce, error) { + nonce := CosignNonce{ SourceID: c.key.ID, } @@ -195,7 +195,7 @@ func (c *CosignerSecurityECIES) EncryptAndSign(id int, noncePub []byte, nonceSha // DecryptAndVerify decrypts the nonce and verifies // the signature to authenticate the source cosigner. -func (c *CosignerSecurityECIES) DecryptAndVerify( +func (c *CosignSecurityECIES) DecryptAndVerify( id int, encryptedNoncePub []byte, encryptedNonceShare []byte, @@ -206,7 +206,7 @@ func (c *CosignerSecurityECIES) DecryptAndVerify( return nil, nil, fmt.Errorf("unknown cosigner: %d", id) } - digestMsg := CosignerNonce{ + digestMsg := CosignNonce{ SourceID: id, PubKey: encryptedNoncePub, Share: encryptedNonceShare, diff --git a/pkg/pcosigner/cosigner_security_ecies_test.go b/pkg/cosigner/cosigner_security_ecies_test.go similarity index 95% rename from pkg/pcosigner/cosigner_security_ecies_test.go rename to pkg/cosigner/cosigner_security_ecies_test.go index 5bf1e060..ae17ee3d 100644 --- a/pkg/pcosigner/cosigner_security_ecies_test.go +++ b/pkg/cosigner/cosigner_security_ecies_test.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner import ( "crypto/rand" @@ -28,7 +28,7 @@ func TestCosignerECIES(t *testing.T) { securities := make([]ICosignerSecurity, 3) for i := 0; i < 3; i++ { - key := CosignerECIESKey{ + key := CosignEciesKey{ ID: i + 1, ECIESKey: keys[i], ECIESPubs: pubs, @@ -38,7 +38,7 @@ func TestCosignerECIES(t *testing.T) { bz, err := json.Marshal(&key) require.NoError(t, err) - var key2 CosignerECIESKey + var key2 CosignEciesKey require.NoError(t, json.Unmarshal(bz, &key2)) require.Equal(t, key, key2) @@ -94,7 +94,7 @@ func TestConcurrentIterateCosignerECIES(t *testing.T) { securities := make([]ICosignerSecurity, 3) for i := 0; i < 3; i++ { - securities[i] = NewCosignerSecurityECIES(CosignerECIESKey{ + securities[i] = NewCosignerSecurityECIES(CosignEciesKey{ ID: i + 1, ECIESKey: keys[i], ECIESPubs: pubs, diff --git a/pkg/pcosigner/cosigner_security_rsa.go b/pkg/cosigner/cosigner_security_rsa.go similarity index 76% rename from pkg/pcosigner/cosigner_security_rsa.go rename to pkg/cosigner/cosigner_security_rsa.go index 72449864..5f618ebc 100644 --- a/pkg/pcosigner/cosigner_security_rsa.go +++ b/pkg/cosigner/cosigner_security_rsa.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner import ( "crypto" @@ -14,29 +14,29 @@ import ( "golang.org/x/sync/errgroup" ) -var _ ICosignerSecurity = &CosignerSecurityRSA{} +var _ ICosignerSecurity = &CosignSecurityRSA{} -// CosignerSecurityRSA is an implementation of CosignerSecurity using RSA for encryption and P5S for digital signature. -type CosignerSecurityRSA struct { - key CosignerRSAKey - rsaPubKeys map[int]CosignerRSAPubKey +// CosignSecurityRSA is an implementation of CosignerSecurity using RSA for encryption and P5S for digital signature. +type CosignSecurityRSA struct { + key CosignRSAKey + rsaPubKeys map[int]CosignRSAPubKey } -// CosignerRSAKey is a cosigner's RSA public key. -type CosignerRSAPubKey struct { +// CosignRSAKey is a cosigner's RSA public key. +type CosignRSAPubKey struct { ID int PublicKey rsa.PublicKey } -// CosignerRSAKey is an RSA key for an m-of-n threshold signer, composed of a private key and n public keys. -type CosignerRSAKey struct { +// CosignRSAKey is an RSA key for an m-of-n threshold signer, composed of a private key and n public keys. +type CosignRSAKey struct { RSAKey rsa.PrivateKey `json:"rsaKey"` ID int `json:"id"` RSAPubs []*rsa.PublicKey `json:"rsaPubs"` } -func (key *CosignerRSAKey) MarshalJSON() ([]byte, error) { - type Alias CosignerRSAKey +func (key *CosignRSAKey) MarshalJSON() ([]byte, error) { + type Alias CosignRSAKey // marshal our private key and all public keys privateBytes := x509.MarshalPKCS1PrivateKey(&key.RSAKey) @@ -57,8 +57,8 @@ func (key *CosignerRSAKey) MarshalJSON() ([]byte, error) { }) } -func (key *CosignerRSAKey) UnmarshalJSON(data []byte) error { - type Alias CosignerRSAKey +func (key *CosignRSAKey) UnmarshalJSON(data []byte) error { + type Alias CosignRSAKey aux := &struct { RSAKey []byte `json:"rsaKey"` @@ -89,9 +89,9 @@ func (key *CosignerRSAKey) UnmarshalJSON(data []byte) error { return nil } -// LoadCosignerRSAKey loads a CosignerRSAKey from file. -func LoadCosignerRSAKey(file string) (CosignerRSAKey, error) { - pvKey := CosignerRSAKey{} +// LoadCosignRSAKey loads a CosignRSAKey from file. +func LoadCosignRSAKey(file string) (CosignRSAKey, error) { + pvKey := CosignRSAKey{} keyJSONBytes, err := os.ReadFile(file) if err != nil { return pvKey, err @@ -106,14 +106,14 @@ func LoadCosignerRSAKey(file string) (CosignerRSAKey, error) { } // NewCosignerSecurityRSA creates a new CosignerSecurityRSA. -func NewCosignerSecurityRSA(key CosignerRSAKey) *CosignerSecurityRSA { - c := &CosignerSecurityRSA{ +func NewCosignerSecurityRSA(key CosignRSAKey) *CosignSecurityRSA { + c := &CosignSecurityRSA{ key: key, - rsaPubKeys: make(map[int]CosignerRSAPubKey), + rsaPubKeys: make(map[int]CosignRSAPubKey), } for i, pubKey := range key.RSAPubs { - c.rsaPubKeys[i+1] = CosignerRSAPubKey{ + c.rsaPubKeys[i+1] = CosignRSAPubKey{ ID: i + 1, PublicKey: *pubKey, } @@ -123,13 +123,13 @@ func NewCosignerSecurityRSA(key CosignerRSAKey) *CosignerSecurityRSA { } // GetID returns the ID of the cosigner. -func (c *CosignerSecurityRSA) GetID() int { +func (c *CosignSecurityRSA) GetID() int { return c.key.ID } // EncryptAndSign encrypts the nonce and signs it for authentication. -func (c *CosignerSecurityRSA) EncryptAndSign(id int, noncePub []byte, nonceShare []byte) (CosignerNonce, error) { - nonce := CosignerNonce{ +func (c *CosignSecurityRSA) EncryptAndSign(id int, noncePub []byte, nonceShare []byte) (CosignNonce, error) { + nonce := CosignNonce{ SourceID: c.key.ID, } @@ -183,7 +183,7 @@ func (c *CosignerSecurityRSA) EncryptAndSign(id int, noncePub []byte, nonceShare // DecryptAndVerify decrypts the nonce and verifies // the signature to authenticate the source cosigner. -func (c *CosignerSecurityRSA) DecryptAndVerify( +func (c *CosignSecurityRSA) DecryptAndVerify( id int, encryptedNoncePub []byte, encryptedNonceShare []byte, @@ -194,7 +194,7 @@ func (c *CosignerSecurityRSA) DecryptAndVerify( return nil, nil, fmt.Errorf("unknown cosigner: %d", id) } - digestMsg := CosignerNonce{ + digestMsg := CosignNonce{ SourceID: id, PubKey: encryptedNoncePub, Share: encryptedNonceShare, diff --git a/pkg/pcosigner/cosigner_security_rsa_test.go b/pkg/cosigner/cosigner_security_rsa_test.go similarity index 94% rename from pkg/pcosigner/cosigner_security_rsa_test.go rename to pkg/cosigner/cosigner_security_rsa_test.go index dfe44dd0..00b97090 100644 --- a/pkg/pcosigner/cosigner_security_rsa_test.go +++ b/pkg/cosigner/cosigner_security_rsa_test.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner import ( "crypto/rand" @@ -27,7 +27,7 @@ func TestCosignerRSA(t *testing.T) { securities := make([]ICosignerSecurity, 3) for i := 0; i < 3; i++ { - key := CosignerRSAKey{ + key := CosignRSAKey{ ID: i + 1, RSAKey: *keys[i], RSAPubs: pubKeys, @@ -37,7 +37,7 @@ func TestCosignerRSA(t *testing.T) { bz, err := json.Marshal(&key) require.NoError(t, err) - var key2 CosignerRSAKey + var key2 CosignRSAKey require.NoError(t, json.Unmarshal(bz, &key2)) require.Equal(t, key, key2) @@ -70,7 +70,7 @@ func TestConcurrentIterateCosignerRSA(t *testing.T) { securities := make([]ICosignerSecurity, 3) for i := 0; i < 3; i++ { - securities[i] = NewCosignerSecurityRSA(CosignerRSAKey{ + securities[i] = NewCosignerSecurityRSA(CosignRSAKey{ ID: i + 1, RSAKey: *keys[i], RSAPubs: pubKeys, diff --git a/pkg/pcosigner/cosigner_signer.go b/pkg/cosigner/cosigner_signer.go similarity index 98% rename from pkg/pcosigner/cosigner_signer.go rename to pkg/cosigner/cosigner_signer.go index b4730833..d5021dd8 100644 --- a/pkg/pcosigner/cosigner_signer.go +++ b/pkg/cosigner/cosigner_signer.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner // IThresholdSigner is interface for the cosigner_signer whether it's a soft sign or HSM type IThresholdSigner interface { diff --git a/pkg/pcosigner/cosigner_signer_soft.go b/pkg/cosigner/cosigner_signer_soft.go similarity index 98% rename from pkg/pcosigner/cosigner_signer_soft.go rename to pkg/cosigner/cosigner_signer_soft.go index f2bef489..3a8354f2 100644 --- a/pkg/pcosigner/cosigner_signer_soft.go +++ b/pkg/cosigner/cosigner_signer_soft.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner import ( "bytes" @@ -22,7 +22,7 @@ type ThresholdSignerSoft struct { } func NewThresholdSignerSoft(config *RuntimeConfig, id int, chainID string) (*ThresholdSignerSoft, error) { - keyFile, err := config.KeyFileExistsCosigner(chainID) + keyFile, err := config.KeyFileExistsCosign(chainID) if err != nil { return nil, err } diff --git a/pkg/pcosigner/cosigner_signer_soft_test.go b/pkg/cosigner/cosigner_signer_soft_test.go similarity index 99% rename from pkg/pcosigner/cosigner_signer_soft_test.go rename to pkg/cosigner/cosigner_signer_soft_test.go index 3f0b781b..f2d3b886 100644 --- a/pkg/pcosigner/cosigner_signer_soft_test.go +++ b/pkg/cosigner/cosigner_signer_soft_test.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner import ( "crypto/ed25519" diff --git a/pkg/pcosigner/local_cosigner.go b/pkg/cosigner/local_cosigner.go similarity index 70% rename from pkg/pcosigner/local_cosigner.go rename to pkg/cosigner/local_cosigner.go index fa88f466..f5c4a8c4 100644 --- a/pkg/pcosigner/local_cosigner.go +++ b/pkg/cosigner/local_cosigner.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner import ( "errors" @@ -85,7 +85,7 @@ func (ccs *ChainState) combinedNonces(myID int, threshold uint8, hrst types.HRST return combinedNonces, nil } -type CosignerGetNonceRequest struct { +type GetNonceRequest struct { ChainID string ID int Height int64 @@ -98,43 +98,43 @@ type CosignerGetNonceRequest struct { // than the current high watermark. A mutex is used to avoid concurrent state updates. // The disk write is scheduled in a separate goroutine which will perform an atomic write. // pendingDiskWG is used upon termination in pendingDiskWG to ensure all writes have completed. -func (cosigner *LocalCosigner) SaveLastSignedState(chainID string, signState types.SignStateConsensus) error { - ccs, err := cosigner.getChainState(chainID) +func (cosign *LocalCosigner) SaveLastSignedState(chainID string, signState types.SignStateConsensus) error { + ccs, err := cosign.getChainState(chainID) if err != nil { return err } return ccs.lastSignState.Save( signState, - &cosigner.pendingDiskWG, + &cosign.pendingDiskWG, ) } // WaitForSignStatesToFlushToDisk waits for all state file writes queued // in SaveLastSignedState to complete before termination. -func (cosigner *LocalCosigner) waitForSignStatesToFlushToDisk() { - cosigner.pendingDiskWG.Wait() +func (cosign *LocalCosigner) waitForSignStatesToFlushToDisk() { + cosign.pendingDiskWG.Wait() } -func (cosigner *LocalCosigner) WaitForSignStatesToFlushToDisk() { - cosigner.waitForSignStatesToFlushToDisk() +func (cosign *LocalCosigner) WaitForSignStatesToFlushToDisk() { + cosign.waitForSignStatesToFlushToDisk() } // GetID returns the id of the cosigner // Implements Cosigner interface -func (cosigner *LocalCosigner) GetID() int { - return cosigner.security.GetID() +func (cosign *LocalCosigner) GetID() int { + return cosign.security.GetID() } // GetAddress returns the RPC URL of the cosigner // Implements Cosigner interface -func (cosigner *LocalCosigner) GetAddress() string { - return cosigner.address +func (cosign *LocalCosigner) GetAddress() string { + return cosign.address } -func (cosigner *LocalCosigner) getChainState(chainID string) (*ChainState, error) { - cs, ok := cosigner.chainStateMap.Load(chainID) +func (cosign *LocalCosigner) getChainState(chainID string) (*ChainState, error) { + cs, ok := cosign.chainStateMap.Load(chainID) if !ok { return nil, fmt.Errorf("failed to load chain state for %s", chainID) } @@ -149,12 +149,12 @@ func (cosigner *LocalCosigner) getChainState(chainID string) (*ChainState, error // GetPubKey returns public key of the validator. // Implements Cosigner interface -func (cosigner *LocalCosigner) GetPubKey(chainID string) (cometcrypto.PubKey, error) { - if err := cosigner.LoadSignStateIfNecessary(chainID); err != nil { +func (cosign *LocalCosigner) GetPubKey(chainID string) (cometcrypto.PubKey, error) { + if err := cosign.LoadSignStateIfNecessary(chainID); err != nil { return nil, err } - ccs, err := cosigner.getChainState(chainID) + ccs, err := cosign.getChainState(chainID) if err != nil { return nil, err } @@ -163,8 +163,8 @@ func (cosigner *LocalCosigner) GetPubKey(chainID string) (cometcrypto.PubKey, er } // CombineSignatures combines partial signatures into a full signature. -func (cosigner *LocalCosigner) CombineSignatures(chainID string, signatures []PartialSignature) ([]byte, error) { - ccs, err := cosigner.getChainState(chainID) +func (cosign *LocalCosigner) CombineSignatures(chainID string, signatures []PartialSignature) ([]byte, error) { + ccs, err := cosign.getChainState(chainID) if err != nil { return nil, err } @@ -174,12 +174,12 @@ func (cosigner *LocalCosigner) CombineSignatures(chainID string, signatures []Pa // VerifySignature validates a signed payload against the (persistent) public key. // Implements Cosigner interface -func (cosigner *LocalCosigner) VerifySignature(chainID string, payload, signature []byte) bool { - if err := cosigner.LoadSignStateIfNecessary(chainID); err != nil { +func (cosign *LocalCosigner) VerifySignature(chainID string, payload, signature []byte) bool { + if err := cosign.LoadSignStateIfNecessary(chainID); err != nil { return false } - ccs, err := cosigner.getChainState(chainID) + ccs, err := cosign.getChainState(chainID) if err != nil { return false } @@ -189,12 +189,12 @@ func (cosigner *LocalCosigner) VerifySignature(chainID string, payload, signatur // Sign the sign request using the cosigner's shard // Return the signed bytes or an error -func (cosigner *LocalCosigner) sign(req CosignerSignRequest) (CosignerSignResponse, error) { +func (cosign *LocalCosigner) sign(req SignRequest) (SignResponse, error) { chainID := req.ChainID - res := CosignerSignResponse{} + res := SignResponse{} - ccs, err := cosigner.getChainState(chainID) + ccs, err := cosign.getChainState(chainID) if err != nil { return res, err } @@ -217,7 +217,7 @@ func (cosigner *LocalCosigner) sign(req CosignerSignRequest) (CosignerSignRespon return res, nil } - nonces, err := ccs.combinedNonces(cosigner.GetID(), uint8(cosigner.Config.Config.ThresholdModeConfig.Threshold), hrst) + nonces, err := ccs.combinedNonces(cosign.GetID(), uint8(cosign.Config.Config.ThresholdModeConfig.Threshold), hrst) if err != nil { return res, err } @@ -233,7 +233,7 @@ func (cosigner *LocalCosigner) sign(req CosignerSignRequest) (CosignerSignRespon Step: hrst.Step, Signature: sig, SignBytes: req.SignBytes, - }, &cosigner.pendingDiskWG) + }, &cosign.pendingDiskWG) if err != nil { if _, isSameHRSError := err.(*types.SameHRSError); !isSameHRSError { @@ -259,48 +259,48 @@ func (cosigner *LocalCosigner) sign(req CosignerSignRequest) (CosignerSignRespon return res, nil } -func (cosigner *LocalCosigner) dealShares(req CosignerGetNonceRequest) ([]Nonces, error) { +func (cosign *LocalCosigner) dealShares(req GetNonceRequest) ([]Nonces, error) { chainID := req.ChainID - ccs, err := cosigner.getChainState(chainID) + ccs, err := cosign.getChainState(chainID) if err != nil { return nil, err } - meta := make([]Nonces, len(cosigner.Config.Config.ThresholdModeConfig.Cosigners)) + meta := make([]Nonces, len(cosign.Config.Config.ThresholdModeConfig.Cosigners)) nonces, err := ccs.signer.GenerateNonces() if err != nil { return nil, err } - meta[cosigner.GetID()-1] = nonces + meta[cosign.GetID()-1] = nonces return meta, nil } -func (cosigner *LocalCosigner) LoadSignStateIfNecessary(chainID string) error { +func (cosign *LocalCosigner) LoadSignStateIfNecessary(chainID string) error { if chainID == "" { return fmt.Errorf("chain id cannot be empty") } - if _, ok := cosigner.chainStateMap.Load(chainID); ok { + if _, ok := cosign.chainStateMap.Load(chainID); ok { return nil } - // TODO: spew.Dump(cosigner.Config) - signState, err := types.LoadOrCreateSignState(cosigner.Config.CosignerStateFile(chainID)) + // TODO: spew.Dump(cosign.Config) + signState, err := types.LoadOrCreateSignState(cosign.Config.CosignStateFile(chainID)) if err != nil { return err } var signer IThresholdSigner - signer, err = NewThresholdSignerSoft(cosigner.Config, cosigner.GetID(), chainID) + signer, err = NewThresholdSignerSoft(cosign.Config, cosign.GetID(), chainID) if err != nil { return err } - cosigner.chainStateMap.Store(chainID, &ChainState{ + cosign.chainStateMap.Store(chainID, &ChainState{ lastSignState: signState, nonces: make(map[types.HRSTKey][]Nonces), signer: signer, @@ -312,26 +312,26 @@ func (cosigner *LocalCosigner) LoadSignStateIfNecessary(chainID string) error { // GetNonces implements the ICosigner interface. // // GetNonces returns the nonces for the given HRS -func (cosigner *LocalCosigner) GetNonces( +func (cosign *LocalCosigner) GetNonces( chainID string, hrst types.HRSTKey, -) (*CosignerNoncesResponse, error) { +) (*NoncesResponse, error) { metrics.MetricsTimeKeeper.SetPreviousLocalNonce(time.Now()) - if err := cosigner.LoadSignStateIfNecessary(chainID); err != nil { + if err := cosign.LoadSignStateIfNecessary(chainID); err != nil { return nil, err } - total := len(cosigner.Config.Config.ThresholdModeConfig.Cosigners) + total := len(cosign.Config.Config.ThresholdModeConfig.Cosigners) - res := &CosignerNoncesResponse{ - Nonces: make([]CosignerNonce, total-1), // an empty list of nonces for each cosigner except for ourselves + res := &NoncesResponse{ + Nonces: make([]CosignNonce, total-1), // an empty list of nonces for each cosign except for ourselves } - id := cosigner.GetID() + id := cosign.GetID() var eg errgroup.Group - // getting nonces requires encrypting and signing for each cosigner, + // getting nonces requires encrypting and signing for each cosign, // so we perform these operations in parallel. for i := 0; i < total; i++ { @@ -343,7 +343,7 @@ func (cosigner *LocalCosigner) GetNonces( i := i eg.Go(func() error { - secretPart, err := cosigner.getNonce(CosignerGetNonceRequest{ + secretPart, err := cosign.getNonce(GetNonceRequest{ ChainID: chainID, ID: peerID, Height: hrst.Height, @@ -366,7 +366,7 @@ func (cosigner *LocalCosigner) GetNonces( return nil, err } - cosigner.logger.Debug( + cosign.logger.Debug( "Generated nonces", "chain_id", chainID, "height", hrst.Height, @@ -377,8 +377,8 @@ func (cosigner *LocalCosigner) GetNonces( return res, nil } -func (cosigner *LocalCosigner) dealSharesIfNecessary(chainID string, hrst types.HRSTKey) ([]Nonces, error) { - ccs, err := cosigner.getChainState(chainID) +func (cosign *LocalCosigner) dealSharesIfNecessary(chainID string, hrst types.HRSTKey) ([]Nonces, error) { + ccs, err := cosign.getChainState(chainID) if err != nil { return nil, err } @@ -392,7 +392,7 @@ func (cosigner *LocalCosigner) dealSharesIfNecessary(chainID string, hrst types. return nonces, nil } - newNonces, err := cosigner.dealShares(CosignerGetNonceRequest{ + newNonces, err := cosign.dealShares(GetNonceRequest{ ChainID: chainID, Height: hrst.Height, Round: hrst.Round, @@ -410,12 +410,12 @@ func (cosigner *LocalCosigner) dealSharesIfNecessary(chainID string, hrst types. // Get the ephemeral secret part for an ephemeral share // The ephemeral secret part is encrypted for the receiver -func (cosigner *LocalCosigner) getNonce( - req CosignerGetNonceRequest, -) (CosignerNonce, error) { +func (cosign *LocalCosigner) getNonce( + req GetNonceRequest, +) (CosignNonce, error) { chainID := req.ChainID - zero := CosignerNonce{} + zero := CosignNonce{} hrst := types.HRSTKey{ Height: req.Height, Round: req.Round, @@ -423,15 +423,15 @@ func (cosigner *LocalCosigner) getNonce( Timestamp: req.Timestamp.UnixNano(), } - id := cosigner.GetID() + id := cosign.GetID() - meta, err := cosigner.dealSharesIfNecessary(chainID, hrst) + meta, err := cosign.dealSharesIfNecessary(chainID, hrst) if err != nil { return zero, err } ourCosignerMeta := meta[id-1] - nonce, err := cosigner.security.EncryptAndSign(req.ID, ourCosignerMeta.PubKey, ourCosignerMeta.Shares[req.ID-1]) + nonce, err := cosign.security.EncryptAndSign(req.ID, ourCosignerMeta.PubKey, ourCosignerMeta.Shares[req.ID-1]) if err != nil { return zero, err } @@ -440,10 +440,10 @@ func (cosigner *LocalCosigner) getNonce( } // setNonce stores a nonce provided by another cosigner -func (cosigner *LocalCosigner) setNonce(req CosignerSetNonceRequest) error { +func (cosign *LocalCosigner) setNonce(req SetNonceRequest) error { chainID := req.ChainID - ccs, err := cosigner.getChainState(chainID) + ccs, err := cosign.getChainState(chainID) if err != nil { return err } @@ -453,7 +453,7 @@ func (cosigner *LocalCosigner) setNonce(req CosignerSetNonceRequest) error { return errors.New("signature field is required") } - noncePub, nonceShare, err := cosigner.security.DecryptAndVerify( + noncePub, nonceShare, err := cosign.security.DecryptAndVerify( req.SourceID, req.PubKey, req.Share, req.Signature) if err != nil { return err @@ -484,32 +484,32 @@ func (cosigner *LocalCosigner) setNonce(req CosignerSetNonceRequest) error { // set slot if nonces[req.SourceID-1].Shares == nil { - nonces[req.SourceID-1].Shares = make([][]byte, len(cosigner.Config.Config.ThresholdModeConfig.Cosigners)) + nonces[req.SourceID-1].Shares = make([][]byte, len(cosign.Config.Config.ThresholdModeConfig.Cosigners)) } - nonces[req.SourceID-1].Shares[cosigner.GetID()-1] = nonceShare + nonces[req.SourceID-1].Shares[cosign.GetID()-1] = nonceShare nonces[req.SourceID-1].PubKey = noncePub return nil } -func (cosigner *LocalCosigner) SetNoncesAndSign( - req CosignerSetNoncesAndSignRequest) (*CosignerSignResponse, error) { +func (cosign *LocalCosigner) SetNoncesAndSign( + req SetNoncesAndSignRequest) (*SignResponse, error) { chainID := req.ChainID - if err := cosigner.LoadSignStateIfNecessary(chainID); err != nil { + if err := cosign.LoadSignStateIfNecessary(chainID); err != nil { return nil, err } var eg errgroup.Group - // setting nonces requires decrypting and verifying signature from each cosigner, + // setting nonces requires decrypting and verifying signature from each cosign, // so we perform these operations in parallel. for _, secretPart := range req.Nonces { secretPart := secretPart eg.Go(func() error { - return cosigner.setNonce(CosignerSetNonceRequest{ + return cosign.setNonce(SetNonceRequest{ ChainID: chainID, SourceID: secretPart.SourceID, PubKey: secretPart.PubKey, @@ -527,7 +527,7 @@ func (cosigner *LocalCosigner) SetNoncesAndSign( return nil, err } - res, err := cosigner.sign(CosignerSignRequest{ + res, err := cosign.sign(SignRequest{ ChainID: chainID, SignBytes: req.SignBytes, }) diff --git a/pkg/pcosigner/local_cosigner_test.go b/pkg/cosigner/local_cosigner_test.go similarity index 93% rename from pkg/pcosigner/local_cosigner_test.go rename to pkg/cosigner/local_cosigner_test.go index 83bc8c87..de89cd1e 100644 --- a/pkg/pcosigner/local_cosigner_test.go +++ b/pkg/cosigner/local_cosigner_test.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner import ( "crypto/rand" @@ -52,7 +52,7 @@ func testLocalCosignerSignRSA(t *testing.T, threshold, total uint8) { for i, k := range keys { security[i] = NewCosignerSecurityRSA( - CosignerRSAKey{ + CosignRSAKey{ ID: i + 1, RSAKey: *k, RSAPubs: pubKeys, @@ -88,7 +88,7 @@ func testLocalCosignerSignECIES(t *testing.T, threshold, total uint8) { for i, k := range keys { security[i] = NewCosignerSecurityECIES( - CosignerECIESKey{ + CosignEciesKey{ ID: i + 1, ECIESKey: k, ECIESPubs: pubKeys, @@ -117,7 +117,7 @@ func testLocalCosignerSign(t *testing.T, threshold, total uint8, security []ICos tmpDir := t.TempDir() thresholdCosigners := make([]*LocalCosigner, threshold) - nonces := make([][]CosignerNonce, threshold) + nonces := make([][]CosignNonce, threshold) now := time.Now() @@ -131,13 +131,13 @@ func testLocalCosignerSign(t *testing.T, threshold, total uint8, security []ICos for i := 0; i < int(total); i++ { id := i + 1 - key := CosignerEd25519Key{ + key := CosignEd25519Key{ PubKey: pubKey, PrivateShard: privShards[i], ID: id, } - cfg.ThresholdModeConfig.Cosigners[i] = CosignerConfig{ + cfg.ThresholdModeConfig.Cosigners[i] = CosignConfig{ ShardID: id, } @@ -190,7 +190,7 @@ func testLocalCosignerSign(t *testing.T, threshold, total uint8, security []ICos sigs := make([]PartialSignature, threshold) for i, cosigner := range thresholdCosigners { - cosignerNonces := make([]CosignerNonce, 0, threshold-1) + cosignerNonces := make([]CosignNonce, 0, threshold-1) for j, nonce := range nonces { if i == j { @@ -204,7 +204,7 @@ func testLocalCosignerSign(t *testing.T, threshold, total uint8, security []ICos } } - sigRes, err := cosigner.SetNoncesAndSign(CosignerSetNoncesAndSignRequest{ + sigRes, err := cosigner.SetNoncesAndSign(SetNoncesAndSignRequest{ ChainID: testChainID, Nonces: cosignerNonces, HRST: hrst, diff --git a/pkg/pcosigner/remote_cosigner.go b/pkg/cosigner/remote_cosigner.go similarity index 92% rename from pkg/pcosigner/remote_cosigner.go rename to pkg/cosigner/remote_cosigner.go index 41b31159..e5fd0f31 100644 --- a/pkg/pcosigner/remote_cosigner.go +++ b/pkg/cosigner/remote_cosigner.go @@ -1,4 +1,4 @@ -package pcosigner +package cosigner // RemoteCosigner is a Cosigner implementation that uses gRPC make to request from other Cosigners import ( @@ -88,7 +88,7 @@ func (cosigner *RemoteCosigner) getGRPCClient() (proto.ICosignerGRPCServerClient func (cosigner *RemoteCosigner) GetNonces( chainID string, req types.HRSTKey, -) (*CosignerNoncesResponse, error) { +) (*NoncesResponse, error) { client, conn, err := cosigner.getGRPCClient() if err != nil { @@ -107,8 +107,8 @@ func (cosigner *RemoteCosigner) GetNonces( if err != nil { return nil, err } - return &CosignerNoncesResponse{ - Nonces: CosignerNoncesFromProto(res.GetNonces()), + return &NoncesResponse{ + Nonces: CosignNoncesFromProto(res.GetNonces()), }, nil } @@ -116,7 +116,7 @@ func (cosigner *RemoteCosigner) GetNonces( // It acts as a client(!) and requests via gRPC the other // "node's" LocalCosigner to set the nonces and sign the payload. func (cosigner *RemoteCosigner) SetNoncesAndSign( - req CosignerSetNoncesAndSignRequest) (*CosignerSignResponse, error) { + req SetNoncesAndSignRequest) (*SignResponse, error) { client, conn, err := cosigner.getGRPCClient() if err != nil { return nil, err @@ -127,14 +127,14 @@ func (cosigner *RemoteCosigner) SetNoncesAndSign( res, err := client.SetNoncesAndSign(context, &proto.CosignerGRPCSetNoncesAndSignRequest{ ChainID: req.ChainID, - Nonces: CosignerNonces(req.Nonces).ToProto(), + Nonces: CosignNonces(req.Nonces).ToProto(), Hrst: req.HRST.ToProto(), SignBytes: req.SignBytes, }) if err != nil { return nil, err } - return &CosignerSignResponse{ + return &SignResponse{ NoncePublic: res.GetNoncePublic(), Timestamp: time.Unix(0, res.GetTimestamp()), Signature: res.GetSignature(), diff --git a/pkg/node/grpc_server.go b/pkg/node/grpc_server.go index 13db2a01..d1e6ecf3 100644 --- a/pkg/node/grpc_server.go +++ b/pkg/node/grpc_server.go @@ -5,7 +5,7 @@ import ( "fmt" "time" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" "github.com/strangelove-ventures/horcrux/pkg/types" "github.com/hashicorp/raft" @@ -16,21 +16,20 @@ import ( var _ proto.ICosignerGRPCServerServer = &GRPCServer{} type GRPCServer struct { - cosigner *pcosigner.LocalCosigner // The "node's" LocalCosigner - thresholdValidator *ThresholdValidator // The "node's" ThresholdValidator - raftStore *RaftStore // The "node's" RaftStore + cosigner *cosigner.LocalCosigner // The "node's" LocalCosigner + thresholdValidator *ThresholdValidator // The "node's" ThresholdValidator + raftStore *RaftStore // The "node's" RaftStore // Promoted Fields is embedded to have forward compatiblitity proto.UnimplementedICosignerGRPCServerServer } // NewGRPCServer returns a new GRPCServer. func NewGRPCServer( - cosigner *pcosigner.LocalCosigner, + cosigner *cosigner.LocalCosigner, thresholdValidator *ThresholdValidator, raftStore *RaftStore, ) *GRPCServer { return &GRPCServer{ - // TODO: This is a hack to get around the fact that the cosigner is not a? cosigner: cosigner, thresholdValidator: thresholdValidator, raftStore: raftStore, @@ -64,9 +63,9 @@ func (rpc *GRPCServer) SetNoncesAndSign( req *proto.CosignerGRPCSetNoncesAndSignRequest, ) (*proto.CosignerGRPCSetNoncesAndSignResponse, error) { res, err := rpc.cosigner.SetNoncesAndSign( - pcosigner.CosignerSetNoncesAndSignRequest{ + cosigner.SetNoncesAndSignRequest{ ChainID: req.ChainID, - Nonces: pcosigner.CosignerNoncesFromProto(req.GetNonces()), + Nonces: cosigner.CosignNoncesFromProto(req.GetNonces()), HRST: types.HRSTKeyFromProto(req.GetHrst()), SignBytes: req.GetSignBytes(), }) @@ -108,7 +107,7 @@ func (rpc *GRPCServer) GetNonces( return nil, err } return &proto.CosignerGRPCGetNoncesResponse{ - Nonces: pcosigner.CosignerNonces(res.Nonces).ToProto(), + Nonces: cosigner.CosignNonces(res.Nonces).ToProto(), }, nil } diff --git a/pkg/node/icosigner.go b/pkg/node/icosigner.go index 105cfec5..4c7dc4b5 100644 --- a/pkg/node/icosigner.go +++ b/pkg/node/icosigner.go @@ -2,7 +2,7 @@ package node import ( cometcrypto "github.com/cometbft/cometbft/crypto" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" "github.com/strangelove-ventures/horcrux/pkg/types" ) @@ -23,8 +23,8 @@ type ICosigner interface { VerifySignature(chainID string, payload, signature []byte) bool // GetNonces requests nonce frpm the peer cosigners - GetNonces(chainID string, hrst types.HRSTKey) (*pcosigner.CosignerNoncesResponse, error) + GetNonces(chainID string, hrst types.HRSTKey) (*cosigner.NoncesResponse, error) // Sign the requested bytes - SetNoncesAndSign(req pcosigner.CosignerSetNoncesAndSignRequest) (*pcosigner.CosignerSignResponse, error) + SetNoncesAndSign(req cosigner.SetNoncesAndSignRequest) (*cosigner.SignResponse, error) } diff --git a/pkg/node/raft_events.go b/pkg/node/raft_events.go index 43230d6c..ec4b388b 100644 --- a/pkg/node/raft_events.go +++ b/pkg/node/raft_events.go @@ -5,7 +5,7 @@ import ( "errors" "time" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" "github.com/strangelove-ventures/horcrux/pkg/types" "github.com/strangelove-ventures/horcrux/pkg/metrics" @@ -77,7 +77,7 @@ func (s *RaftStore) SignBlock(req ValidatorSignBlockRequest) (*ValidatorSignBloc return nil, err } defer conn.Close() - context, cancelFunc := pcosigner.GetContext() + context, cancelFunc := cosigner.GetContext() defer cancelFunc() res, err := client.SignBlock(context, &proto.CosignerGRPCSignBlockRequest{ ChainID: req.ChainID, diff --git a/pkg/node/raft_store.go b/pkg/node/raft_store.go index 527ec0c2..df064f64 100644 --- a/pkg/node/raft_store.go +++ b/pkg/node/raft_store.go @@ -116,7 +116,7 @@ func (s *RaftStore) OnStart() error { go func() { err := s.init() if err != nil { - fmt.Println("RAFTSTORE") + s.logger.Error("OnStart %v", err) panic(err) } }() diff --git a/pkg/node/raft_store_test.go b/pkg/node/raft_store_test.go index 912dde3d..b31f6cb0 100644 --- a/pkg/node/raft_store_test.go +++ b/pkg/node/raft_store_test.go @@ -6,12 +6,12 @@ import ( "testing" "time" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" - cometcryptoed25519 "github.com/cometbft/cometbft/crypto/ed25519" + "github.com/cometbft/cometbft/libs/log" "github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/crypto/secp256k1" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" "github.com/stretchr/testify/require" ) @@ -26,17 +26,17 @@ func Test_StoreInMemOpenSingleNode(t *testing.T) { eciesKey, err := ecies.GenerateKey(rand.Reader, secp256k1.S256(), nil) require.NoError(t, err) - key := pcosigner.CosignerEd25519Key{ + key := cosigner.CosignEd25519Key{ PubKey: dummyPub, PrivateShard: []byte{}, ID: 1, } - cosigner := pcosigner.NewLocalCosigner( + cosigner := cosigner.NewLocalCosigner( log.NewNopLogger(), - &pcosigner.RuntimeConfig{}, - pcosigner.NewCosignerSecurityECIES( - pcosigner.CosignerECIESKey{ + &cosigner.RuntimeConfig{}, + cosigner.NewCosignerSecurityECIES( + cosigner.CosignEciesKey{ ID: key.ID, ECIESKey: eciesKey, ECIESPubs: []*ecies.PublicKey{&eciesKey.PublicKey}, @@ -46,6 +46,7 @@ func Test_StoreInMemOpenSingleNode(t *testing.T) { validator := &ThresholdValidator{ myCosigner: cosigner} + s := &RaftStore{ NodeID: "1", RaftDir: tmpDir, diff --git a/pkg/node/single_signer_validator.go b/pkg/node/single_signer_validator.go index 427b3037..e1eb40a2 100644 --- a/pkg/node/single_signer_validator.go +++ b/pkg/node/single_signer_validator.go @@ -5,7 +5,7 @@ import ( "os" "sync" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" cometcrypto "github.com/cometbft/cometbft/crypto" cometprivval "github.com/cometbft/cometbft/privval" @@ -17,7 +17,7 @@ var _ IPrivValidator = &SingleSignerValidator{} // SingleSignerValidator guards access to an underlying PrivValidator by using mutexes // for each of the PrivValidator interface functions type SingleSignerValidator struct { - config *pcosigner.RuntimeConfig + config *cosigner.RuntimeConfig chainState sync.Map } @@ -33,7 +33,7 @@ type SingleSignerChainState struct { // NewSingleSignerValidator constructs a validator for single-sign mode (not recommended). // NewThresholdValidator is recommended, but single-sign mode can be used for convenience. -func NewSingleSignerValidator(config *pcosigner.RuntimeConfig) *SingleSignerValidator { +func NewSingleSignerValidator(config *cosigner.RuntimeConfig) *SingleSignerValidator { return &SingleSignerValidator{ config: config, } diff --git a/pkg/node/single_signer_validator_test.go b/pkg/node/single_signer_validator_test.go index 4b20406e..126cd801 100644 --- a/pkg/node/single_signer_validator_test.go +++ b/pkg/node/single_signer_validator_test.go @@ -4,7 +4,7 @@ import ( "path/filepath" "time" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" "os" "testing" @@ -19,8 +19,6 @@ import ( "github.com/stretchr/testify/require" ) -// const testChainID = "test" - func TestSingleSignerValidator(t *testing.T) { tmpDir := t.TempDir() @@ -29,7 +27,7 @@ func TestSingleSignerValidator(t *testing.T) { err := os.MkdirAll(stateDir, 0700) require.NoError(t, err) - runtimeConfig := &pcosigner.RuntimeConfig{ + runtimeConfig := &cosigner.RuntimeConfig{ HomeDir: tmpDir, StateDir: filepath.Join(tmpDir, "state"), } diff --git a/pkg/node/threshold_validator.go b/pkg/node/threshold_validator.go index cb9054e2..26239a2c 100644 --- a/pkg/node/threshold_validator.go +++ b/pkg/node/threshold_validator.go @@ -8,7 +8,7 @@ import ( "sync" "time" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" "github.com/strangelove-ventures/horcrux/pkg/types" "github.com/cometbft/cometbft/crypto" @@ -31,7 +31,7 @@ type ValidatorSignBlockResponse struct { Signature []byte } type ThresholdValidator struct { - config *pcosigner.RuntimeConfig + config *cosigner.RuntimeConfig threshold int @@ -40,7 +40,7 @@ type ThresholdValidator struct { chainState sync.Map // our own cosigner - myCosigner *pcosigner.LocalCosigner + myCosigner *cosigner.LocalCosigner // peer cosigners peerCosigners []ICosigner @@ -68,11 +68,11 @@ type ChainSignState struct { // NewThresholdValidator creates and returns a new ThresholdValidator func NewThresholdValidator( logger log.Logger, - config *pcosigner.RuntimeConfig, + config *cosigner.RuntimeConfig, threshold int, grpcTimeout time.Duration, maxWaitForSameBlockAttempts int, - myCosigner *pcosigner.LocalCosigner, + myCosigner *cosigner.LocalCosigner, peerCosigners []ICosigner, leader ILeader, ) *ThresholdValidator { @@ -372,7 +372,7 @@ func (pv *ThresholdValidator) waitForPeerNonces( peer ICosigner, hrst types.HRSTKey, wg *sync.WaitGroup, - nonces map[ICosigner][]pcosigner.CosignerNonce, + nonces map[ICosigner][]cosigner.CosignNonce, thresholdPeersMutex *sync.Mutex, ) { peerStartTime := time.Now() @@ -400,7 +400,7 @@ func (pv *ThresholdValidator) waitForPeerSetNoncesAndSign( chainID string, peer ICosigner, hrst types.HRSTKey, - noncesMap map[ICosigner][]pcosigner.CosignerNonce, + noncesMap map[ICosigner][]cosigner.CosignNonce, signBytes []byte, shareSignatures *[][]byte, shareSignaturesMutex *sync.Mutex, @@ -408,7 +408,7 @@ func (pv *ThresholdValidator) waitForPeerSetNoncesAndSign( ) { peerStartTime := time.Now() defer wg.Done() - peerNonces := make([]pcosigner.CosignerNonce, 0, pv.threshold-1) + peerNonces := make([]cosigner.CosignNonce, 0, pv.threshold-1) peerID := peer.GetID() @@ -430,7 +430,7 @@ func (pv *ThresholdValidator) waitForPeerSetNoncesAndSign( } } - sigRes, err := peer.SetNoncesAndSign(pcosigner.CosignerSetNoncesAndSignRequest{ + sigRes, err := peer.SetNoncesAndSign(cosigner.SetNoncesAndSignRequest{ ChainID: chainID, Nonces: peerNonces, HRST: hrst, @@ -649,9 +649,9 @@ func (pv *ThresholdValidator) SignBlock(chainID string, block *Block) ([]byte, t // Used to track how close we are to threshold // Here the actual signing process starts from a cryptological perspective - // TODO: This process should be factored out. It is not the responsibility of the validator to know + // TODO: This process should be factored out. It is not the responsibility of the validator // how to arrange signature of a block. It should be a separate component that is injected into the validator. - nonces := make(map[ICosigner][]pcosigner.CosignerNonce) + nonces := make(map[ICosigner][]cosigner.CosignNonce) thresholdPeersMutex := sync.Mutex{} // From each cosigner peer we are requesting the nonce. @@ -724,7 +724,7 @@ func (pv *ThresholdValidator) SignBlock(chainID string, block *Block) ([]byte, t ) // collect all valid responses into array of partial signatures - shareSigs := make([]pcosigner.PartialSignature, 0, pv.threshold) + shareSigs := make([]cosigner.PartialSignature, 0, pv.threshold) for idx, shareSig := range shareSignatures { if len(shareSig) == 0 { continue @@ -732,7 +732,7 @@ func (pv *ThresholdValidator) SignBlock(chainID string, block *Block) ([]byte, t // we are ok to use the share signatures - complete boolean // prevents future concurrent access - shareSigs = append(shareSigs, pcosigner.PartialSignature{ + shareSigs = append(shareSigs, cosigner.PartialSignature{ ID: idx + 1, Signature: shareSig, }) diff --git a/pkg/node/threshold_validator_test.go b/pkg/node/threshold_validator_test.go index 8032b71b..f399a136 100644 --- a/pkg/node/threshold_validator_test.go +++ b/pkg/node/threshold_validator_test.go @@ -9,7 +9,7 @@ import ( "sync" "time" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" "os" "testing" @@ -51,15 +51,15 @@ func TestThresholdValidator3of5(t *testing.T) { } func loadKeyForLocalCosigner( - cosigner *pcosigner.LocalCosigner, + cosign *cosigner.LocalCosigner, pubKey cometcrypto.PubKey, chainID string, privateShard []byte, ) error { - key := pcosigner.CosignerEd25519Key{ + key := cosigner.CosignEd25519Key{ PubKey: pubKey, PrivateShard: privateShard, - ID: cosigner.GetID(), + ID: cosign.GetID(), } keyBz, err := key.MarshalJSON() @@ -67,7 +67,7 @@ func loadKeyForLocalCosigner( return err } - return os.WriteFile(cosigner.Config.KeyFilePathCosigner(chainID), keyBz, 0600) + return os.WriteFile(cosign.Config.KeyFilePathCosigner(chainID), keyBz, 0600) } func testThresholdValidator(t *testing.T, threshold, total uint8) { @@ -263,10 +263,10 @@ func testThresholdValidator(t *testing.T, threshold, total uint8) { } } -func getTestLocalCosigners(t *testing.T, threshold, total uint8) ([]*pcosigner.LocalCosigner, cometcrypto.PubKey) { +func getTestLocalCosigners(t *testing.T, threshold, total uint8) ([]*cosigner.LocalCosigner, cometcrypto.PubKey) { eciesKeys := make([]*ecies.PrivateKey, total) pubKeys := make([]*ecies.PublicKey, total) - cosigners := make([]*pcosigner.LocalCosigner, total) + cosigners := make([]*cosigner.LocalCosigner, total) for i := uint8(0); i < total; i++ { eciesKey, err := ecies.GenerateKey(rand.Reader, secp256k1.S256(), nil) @@ -284,10 +284,10 @@ func getTestLocalCosigners(t *testing.T, threshold, total uint8) ([]*pcosigner.L tmpDir := t.TempDir() - cosignersConfig := make(pcosigner.CosignersConfig, total) + cosignersConfig := make(cosigner.CosignersConfig, total) for i := range pubKeys { - cosignersConfig[i] = pcosigner.CosignerConfig{ + cosignersConfig[i] = cosigner.CosignConfig{ ShardID: i + 1, } } @@ -297,22 +297,22 @@ func getTestLocalCosigners(t *testing.T, threshold, total uint8) ([]*pcosigner.L err := os.MkdirAll(cosignerDir, 0777) require.NoError(t, err) - cosignerConfig := &pcosigner.RuntimeConfig{ + cosignerConfig := &cosigner.RuntimeConfig{ HomeDir: cosignerDir, StateDir: cosignerDir, - Config: pcosigner.Config{ - ThresholdModeConfig: &pcosigner.ThresholdModeConfig{ + Config: cosigner.Config{ + ThresholdModeConfig: &cosigner.ThresholdModeConfig{ Threshold: int(threshold), Cosigners: cosignersConfig, }, }, } - cosigner := pcosigner.NewLocalCosigner( + cosign := cosigner.NewLocalCosigner( cometlog.NewNopLogger(), cosignerConfig, - pcosigner.NewCosignerSecurityECIES( - pcosigner.CosignerECIESKey{ + cosigner.NewCosignerSecurityECIES( + cosigner.CosignEciesKey{ ID: i + 1, ECIESKey: eciesKeys[i], ECIESPubs: pubKeys, @@ -322,12 +322,12 @@ func getTestLocalCosigners(t *testing.T, threshold, total uint8) ([]*pcosigner.L ) require.NoError(t, err) - cosigners[i] = cosigner + cosigners[i] = cosign - err = loadKeyForLocalCosigner(cosigner, privateKey.PubKey(), testChainID, privShards[i]) + err = loadKeyForLocalCosigner(cosign, privateKey.PubKey(), testChainID, privShards[i]) require.NoError(t, err) - err = loadKeyForLocalCosigner(cosigner, privateKey.PubKey(), testChainID2, privShards[i]) + err = loadKeyForLocalCosigner(cosign, privateKey.PubKey(), testChainID2, privShards[i]) require.NoError(t, err) } diff --git a/pkg/types/sign_state.go b/pkg/types/sign_state.go index 66e4c88a..17019751 100644 --- a/pkg/types/sign_state.go +++ b/pkg/types/sign_state.go @@ -409,8 +409,8 @@ func LoadOrCreateSignState(filepath string) (*SignState, error) { return nil, fmt.Errorf("unexpected error checking file existence (%s): %w", filepath, err) } + // print that the file does not exist to the console fmt.Printf("\n\tNo filepath for Signstate exist for:\n\t\t%s.", filepath) - fmt.Printf("\n\tThis means NO(!) Signstate exists so we will create a new Signstate.\n") // the only scenario where we want to create a new sign state file is when the file does not exist. // Make an empty sign state and save it. diff --git a/test/horcrux_test.go b/test/horcrux_test.go index cb795a13..d8375f73 100644 --- a/test/horcrux_test.go +++ b/test/horcrux_test.go @@ -4,7 +4,7 @@ import ( "context" "fmt" - "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" "sync" "testing" @@ -44,8 +44,9 @@ func Test2Of3SignerThreeSentries(t *testing.T) { testChainSingleNodeAndHorcruxThreshold(t, 2, 3, 2, 3, 3) } -// Test2Of3SignerThreeSentriesUniqueConnection will spin up a chain with one single-node validator and one horcrux validator -// the horcrux validator will have three pcosigners nodes with a threshold of two, and three sentry nodes +// Test2Of3SignerThreeSentriesUniqueConnection will spin up a chain with one single-node validator +// and one horcrux validator. +// The horcrux validator will have three pcosigners nodes with a threshold of two, and three sentry nodes // where each pcosigners only connects to one sentry func Test2Of3SignerThreeSentriesUniqueConnection(t *testing.T) { testChainSingleNodeAndHorcruxThreshold(t, 2, 3, 2, 3, 1) @@ -66,21 +67,22 @@ func Test3Of5SignerTwoSentries(t *testing.T) { // Test3Of5SignerFiveSentries will spin up a chain with one single-node validator and one horcrux validator // the horcrux validator will have five pcosigners nodes with a threshold of three, and five sentry nodes -// where each pcosigners connects to all sentries. +// where each cosign connects to all sentries. func Test3Of5SignerFiveSentries(t *testing.T) { testChainSingleNodeAndHorcruxThreshold(t, 2, 5, 3, 5, 5) } -// Test3Of5SignerFiveSentriesUniqueConnection will spin up a chain with one single-node validator and one horcrux validator -// the horcrux validator will have three pcosigners nodes with a threshold of two, and three sentry nodes -// where each pcosigners only connects to one sentry. +// Test3Of5SignerFiveSentriesUniqueConnection will spin up a chain +// with one single-node validator and one horcrux validator. +// The horcrux validator will have three cosign nodes with a threshold of two, and three sentry nodes +// where each cosign only connects to one sentry. func Test3Of5SignerFiveSentriesUniqueConnection(t *testing.T) { testChainSingleNodeAndHorcruxThreshold(t, 2, 5, 3, 5, 1) } // Test4Of7SignerTwoSentries will spin up a chain with one single-node validator and one horcrux validator -// the horcrux validator will have seven pcosigners nodes with a threshold of four, and two sentry nodes -// where each pcosigners connects to all sentries. +// the horcrux validator will have seven cosign nodes with a threshold of four, and two sentry nodes +// where each cosign connects to all sentries. func Test4Of7SignerTwoSentries(t *testing.T) { testChainSingleNodeAndHorcruxThreshold(t, 2, 7, 4, 2, 2) } @@ -126,7 +128,8 @@ func TestUpgradeValidatorToHorcrux(t *testing.T) { err := v.StopContainer(ctx) require.NoError(t, err) - pubKey, err := convertValidatorToHorcrux(ctx, logger, client, network, v, totalSigners, threshold, cosmos.ChainNodes{v}, sentriesPerSigner) + pubKey, err := convertValidatorToHorcrux( + ctx, logger, client, network, v, totalSigners, threshold, cosmos.ChainNodes{v}, sentriesPerSigner) require.NoError(t, err) err = v.StartContainer(ctx) @@ -316,7 +319,8 @@ func TestChainPureHorcrux(t *testing.T) { totalValidators: totalValidators, totalSentries: 1 + totalValidators*(sentriesPerValidator-1), modifyGenesis: modifyGenesisStrictUptime, - preGenesis: preGenesisAllHorcruxThreshold(ctx, logger, client, network, signersPerValidator, threshold, sentriesPerValidator, sentriesPerSigner, pubKeys), + preGenesis: preGenesisAllHorcruxThreshold( + ctx, logger, client, network, signersPerValidator, threshold, sentriesPerValidator, sentriesPerSigner, pubKeys), } startChains( @@ -354,13 +358,13 @@ func TestMultipleChainHorcrux(t *testing.T) { for i := 0; i < totalChains; i++ { chainConfigs[i] = &cosignerChainConfig{ sentries: make([]cosmos.ChainNodes, sentriesPerSigner), - shards: make([]pcosigner.CosignerEd25519Key, totalSigners), + shards: make([]cosigner.CosignEd25519Key, totalSigners), } } cosignerSidecars := make(cosmos.SidecarProcesses, totalSigners) - eciesShards, err := pcosigner.CreateCosignerECIESShards(totalSigners) + eciesShards, err := cosigner.CreateCosignerECIESShards(totalSigners) require.NoError(t, err) var wg sync.WaitGroup @@ -437,7 +441,10 @@ func TestMultipleChainHorcrux(t *testing.T) { chains[i] = cw.chain } - testutil.WaitForBlocks(ctx, 20, chains...) + err = testutil.WaitForBlocks(ctx, 20, chains...) + if err != nil { + t.Logf("Error {WaitForBlocks} -> {%v}", err) + } for i, p := range pubKeys { requireHealthyValidator(t, chainWrappers[i].chain.Validators[0], p.Address()) @@ -446,14 +453,14 @@ func TestMultipleChainHorcrux(t *testing.T) { type cosignerChainConfig struct { chainID string - shards []pcosigner.CosignerEd25519Key + shards []cosigner.CosignEd25519Key sentries []cosmos.ChainNodes } func configureAndStartSidecars( ctx context.Context, t *testing.T, - eciesShards []pcosigner.CosignerECIESKey, + eciesShards []cosigner.CosignEciesKey, cosignerSidecars cosmos.SidecarProcesses, threshold int, wg *sync.WaitGroup, @@ -465,29 +472,29 @@ func configureAndStartSidecars( totalSigners := len(cosignerSidecars) - cosignersConfig := make(pcosigner.CosignersConfig, totalSigners) - for i, cosigner := range cosignerSidecars { - cosignersConfig[i] = pcosigner.CosignerConfig{ + cosignersConfig := make(cosigner.CosignersConfig, totalSigners) + for i, cosign := range cosignerSidecars { + cosignersConfig[i] = cosigner.CosignConfig{ ShardID: i + 1, - P2PAddr: fmt.Sprintf("tcp://%s:%s", cosigner.HostName(), signerPort), + P2PAddr: fmt.Sprintf("tcp://%s:%s", cosign.HostName(), signerPort), } } var eg errgroup.Group - for i, cosigner := range cosignerSidecars { + for i, cosign := range cosignerSidecars { numSentries := 0 for _, chainConfig := range chainConfigs { numSentries += len(chainConfig.sentries[i]) } - chainNodes := make(pcosigner.ChainNodes, 0, numSentries) + chainNodes := make(cosigner.ChainNodes, 0, numSentries) ed25519Shards := make([]chainEd25519Shard, len(chainConfigs)) for j, chainConfig := range chainConfigs { for _, sentry := range chainConfig.sentries[i] { - chainNodes = append(chainNodes, pcosigner.ChainNode{ + chainNodes = append(chainNodes, cosigner.ChainNode{ PrivValAddr: fmt.Sprintf("tcp://%s:1234", sentry.HostName()), }) } @@ -498,9 +505,9 @@ func configureAndStartSidecars( } } - config := pcosigner.Config{ - SignMode: pcosigner.SignModeThreshold, - ThresholdModeConfig: &pcosigner.ThresholdModeConfig{ + config := cosigner.Config{ + SignMode: cosigner.SignModeThreshold, + ThresholdModeConfig: &cosigner.ThresholdModeConfig{ Threshold: threshold, Cosigners: cosignersConfig, GRPCTimeout: "1500ms", @@ -509,20 +516,20 @@ func configureAndStartSidecars( ChainNodes: chainNodes, } - cosigner := cosigner + cosign := cosign i := i - // configure and start pcosigners in parallel + // configure and start cosigners in parallel eg.Go(func() error { - if err := writeConfigAndKeysThreshold(ctx, cosigner, config, eciesShards[i], ed25519Shards...); err != nil { + if err := writeConfigAndKeysThreshold(ctx, cosign, config, eciesShards[i], ed25519Shards...); err != nil { return err } - if err := cosigner.CreateContainer(ctx); err != nil { + if err := cosign.CreateContainer(ctx); err != nil { return err } - return cosigner.StartContainer(ctx) + return cosign.StartContainer(ctx) }) } diff --git a/test/validator_single.go b/test/validator_single.go index db71b0d9..1561afa5 100644 --- a/test/validator_single.go +++ b/test/validator_single.go @@ -10,7 +10,7 @@ import ( cometjson "github.com/cometbft/cometbft/libs/json" "github.com/cometbft/cometbft/privval" "github.com/docker/docker/client" - pcosigner "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + "github.com/strangelove-ventures/horcrux/pkg/cosigner" interchaintest "github.com/strangelove-ventures/interchaintest/v7" "github.com/strangelove-ventures/interchaintest/v7/chain/cosmos" "github.com/strangelove-ventures/interchaintest/v7/ibc" @@ -20,7 +20,8 @@ import ( "go.uber.org/zap/zaptest" ) -// testChainSingleNodeAndHorcruxSingle tests a single chain with a single horcrux (single-sign mode) validator and single node validators for the rest. +// testChainSingleNodeAndHorcruxSingle tests a single chain with a single horcrux (single-sign mode) validator and +// single node validators for the rest. func testChainSingleNodeAndHorcruxSingle( t *testing.T, totalValidators int, // total number of validators on chain (one horcrux + single node for the rest) @@ -84,15 +85,15 @@ func preGenesisSingleNodeAndHorcruxSingle( return err } - chainNodes := make(pcosigner.ChainNodes, len(sentries)) + chainNodes := make(cosigner.ChainNodes, len(sentries)) for i, sentry := range sentries { - chainNodes[i] = pcosigner.ChainNode{ + chainNodes[i] = cosigner.ChainNode{ PrivValAddr: fmt.Sprintf("tcp://%s:1234", sentry.HostName()), } } - config := pcosigner.Config{ - SignMode: pcosigner.SignModeSingle, + config := cosigner.Config{ + SignMode: cosigner.SignModeSingle, ChainNodes: chainNodes, } @@ -118,7 +119,7 @@ func writeConfigAndKeysSingle( ctx context.Context, chainID string, singleSigner *cosmos.SidecarProcess, - config pcosigner.Config, + config cosigner.Config, pvKey privval.FilePVKey, ) error { configBz, err := json.Marshal(config) diff --git a/test/validator_threshold.go b/test/validator_threshold.go index 07b8edbf..ff97e9ac 100644 --- a/test/validator_threshold.go +++ b/test/validator_threshold.go @@ -8,7 +8,7 @@ import ( "testing" "time" - pcosigner "github.com/strangelove-ventures/horcrux/pkg/pcosigner" + pcosigner "github.com/strangelove-ventures/horcrux/pkg/cosigner" "github.com/cometbft/cometbft/crypto" "github.com/docker/docker/client" @@ -195,7 +195,7 @@ func convertValidatorToHorcrux( return nil, err } - cosigners[i] = pcosigner.CosignerConfig{ + cosigners[i] = pcosigner.CosignConfig{ ShardID: i + 1, P2PAddr: fmt.Sprintf("tcp://%s:%s", validator.Sidecars[i].HostName(), signerPort), } @@ -251,7 +251,7 @@ func convertValidatorToHorcrux( } // getPrivvalKey gets the privval key from the validator and creates threshold shards from it. -func getShardedPrivvalKey(ctx context.Context, node *cosmos.ChainNode, threshold uint8, shards uint8) ([]pcosigner.CosignerEd25519Key, crypto.PubKey, error) { +func getShardedPrivvalKey(ctx context.Context, node *cosmos.ChainNode, threshold uint8, shards uint8) ([]pcosigner.CosignEd25519Key, crypto.PubKey, error) { pvKey, err := getPrivvalKey(ctx, node) if err != nil { return nil, nil, err @@ -265,7 +265,7 @@ func getShardedPrivvalKey(ctx context.Context, node *cosmos.ChainNode, threshold // chainEd25519Shard is a wrapper for a chain ID and a shard of an ed25519 consensus key. type chainEd25519Shard struct { chainID string - key pcosigner.CosignerEd25519Key + key pcosigner.CosignEd25519Key } // writeConfigAndKeysThreshold writes the config and keys for a horcrux cosigner to the sidecar's docker volume. @@ -273,7 +273,7 @@ func writeConfigAndKeysThreshold( ctx context.Context, cosigner *cosmos.SidecarProcess, config pcosigner.Config, - eciesKey pcosigner.CosignerECIESKey, + eciesKey pcosigner.CosignEciesKey, ed25519Shards ...chainEd25519Shard, ) error { configBz, err := json.Marshal(config)