Skip to content

Commit

Permalink
Merge pull request #12 from nitronit/entangle-threshold-signer
Browse files Browse the repository at this point in the history
factorizing out localsigner config and update test
  • Loading branch information
nitronit authored Jul 30, 2022
2 parents c6989b9 + e414aeb commit 3b0bd1c
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 29 deletions.
8 changes: 4 additions & 4 deletions cmd/horcrux/cmd/cosigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func StartCosignerCmd() *cobra.Command {
ListenAddress: config.CosignerConfig.P2PListen,
Nodes: config.Nodes(),
Cosigners: config.CosignerPeers(),
// FIXME Might need to change the code and add Threshold signer here when HSM signer is implemented.
}

if err = cfg.KeyFileExists(); err != nil {
Expand Down Expand Up @@ -194,8 +195,6 @@ func StartCosignerCmd() *cobra.Command {
})
}

// TODO Add switch statement o build a var which is a ThresholdEd25519Signature and then it would be added to the config on your

total := len(cfg.Cosigners) + 1
localCosignerConfig := signer.LocalCosignerConfig{
CosignerKey: key,
Expand All @@ -205,10 +204,11 @@ func StartCosignerCmd() *cobra.Command {
Peers: peers,
Total: uint8(total),
Threshold: uint8(cfg.CosignerThreshold),
// Localsigner: // TODO LocalSoftSignThresholdEd25519Signature instance should be passed into the config here as the desired ThresholdEd25519Signature
}

localCosigner := signer.NewLocalCosigner(localCosignerConfig)
// Instanciace the localsigner of choice via a switch statement which is a ThresholdEd25519Signature, so it can be passed to signer.NewLocalCosigner
localsigner := signer.NewLocalSigner(cfg.ThresholdSigner, localCosignerConfig)
localCosigner := signer.NewLocalCosigner(localCosignerConfig, localsigner)

timeout, _ := time.ParseDuration(config.CosignerConfig.Timeout)

Expand Down
1 change: 1 addition & 0 deletions signer/Config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Config struct {
ListenAddress string
Nodes []NodeConfig
Cosigners []CosignerConfig
ThresholdSigner string `default:"SoftSign"`
}

func (cfg *Config) KeyFileExists() error {
Expand Down
14 changes: 1 addition & 13 deletions signer/local_cosigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ type LocalCosignerConfig struct {
RsaKey rsa.PrivateKey
Peers []CosignerPeer
Address string
RaftAddress string // FIXME this attribute looks really redundant - dont understand what it does.
Total uint8
Threshold uint8
Localsigner ThresholdEd25519Signature
}

// DELETE refactorise temporary aliasing ThresholdEd25519Signature
Expand All @@ -57,17 +55,7 @@ type LocalCosigner struct {
localsigner ThresholdEd25519Signature
}

func NewLocalCosigner(cfg LocalCosignerConfig) *LocalCosigner {

// TODO: factorise out localsigner, should be passed as a parameter in the cfg rather than constructed here. And the same for the init of the local signer config.
localsignerconfig := LocalSoftSignThresholdEd25519SignatureConfig{
CosignerKey: cfg.CosignerKey,
RsaKey: cfg.RsaKey,
Total: cfg.Total,
Threshold: cfg.Threshold,
}

localsigner := localsignerconfig.NewThresholdEd25519Signature()
func NewLocalCosigner(cfg LocalCosignerConfig, localsigner ThresholdEd25519Signature) *LocalCosigner {

LastSignStateStruct := LastSignStateStruct{
LastSignStateMutex: sync.Mutex{},
Expand Down
10 changes: 7 additions & 3 deletions signer/local_cosigner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func TestLocalCosignerGetID(t *testing.T) {
}},
}

cosigner := NewLocalCosigner(config)
localsigner := NewLocalSigner("SoftSign", config)
cosigner := NewLocalCosigner(config, localsigner)
require.Equal(t, cosigner.GetID(), 1)
}

Expand Down Expand Up @@ -120,8 +121,11 @@ func TestLocalCosignerSign2of2(t *testing.T) {
var cosigner1 Cosigner
var cosigner2 Cosigner

cosigner1 = NewLocalCosigner(config1)
cosigner2 = NewLocalCosigner(config2)
localsigner1 := NewLocalSigner("SoftSign", config1)
cosigner1 = NewLocalCosigner(config1, localsigner1)

localsigner2 := NewLocalSigner("SoftSign", config2)
cosigner2 = NewLocalCosigner(config2, localsigner2)

require.Equal(t, cosigner1.GetID(), 1)
require.Equal(t, cosigner2.GetID(), 2)
Expand Down
3 changes: 2 additions & 1 deletion signer/raft_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func Test_StoreInMemOpenSingleNode(t *testing.T) {
}},
}

cosigner := NewLocalCosigner(config)
localsigner := NewLocalSigner("SoftSign", config)
cosigner := NewLocalCosigner(config, localsigner)

s := &RaftStore{
NodeID: "1",
Expand Down
11 changes: 11 additions & 0 deletions signer/threshold_ed25519_localsoftsign_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ type LocalSoftSignThresholdEd25519Signature struct {
HrsMeta map[HRSTKey]HrsMetadata
}

func NewLocalSoftSignThresholdEd25519SignatureConfig(cfg LocalCosignerConfig) LocalSoftSignThresholdEd25519SignatureConfig {
// TODO: factorise out localsigner, should be passed as a parameter in the cfg rather than constructed here. And the same for the init of the local signer config.
localsignerconfig := LocalSoftSignThresholdEd25519SignatureConfig{
CosignerKey: cfg.CosignerKey,
RsaKey: cfg.RsaKey,
Total: cfg.Total,
Threshold: cfg.Threshold,
}
return localsignerconfig
}

type LocalSoftSignThresholdEd25519SignatureConfig struct {
CosignerKey CosignerKey
RsaKey rsa.PrivateKey
Expand Down
15 changes: 15 additions & 0 deletions signer/threshold_ed25519_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@ type HrsMetadata struct {
type ThresholdEd25519SignatureConfig interface {
NewThresholdEd25519Signature() ThresholdEd25519Signature
}

// TODO: Fix so that also HSM can be called
func NewLocalSigner(signertype string, cfg LocalCosignerConfig) ThresholdEd25519Signature {
switch signertype {
case "SoftSign":
localsigner := NewLocalSoftSignThresholdEd25519SignatureConfig(cfg)
return localsigner.NewThresholdEd25519Signature()
case "HSMsign":
panic("Not Implemented")
// localsigner := NewLocalHSMSignThresholdEd25519SignatureConfig(cfg)
// return localsigner.NewThresholdEd25519Signature()
default:
panic("Need to be Softsign as its the only one implemented")
}
}
29 changes: 21 additions & 8 deletions signer/threshold_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@ func TestThresholdValidator2of2(t *testing.T) {
var cosigner1 Cosigner
var cosigner2 Cosigner

cosigner1 = NewLocalCosigner(config1)
cosigner2 = NewLocalCosigner(config2)
localsigner1 := NewLocalSigner("SoftSign", config1)
cosigner1 = NewLocalCosigner(config1, localsigner1)

localsigner2 := NewLocalSigner("SoftSign", config2)
cosigner2 = NewLocalCosigner(config2, localsigner2)

require.Equal(t, cosigner1.GetID(), 1)
require.Equal(t, cosigner2.GetID(), 2)
Expand Down Expand Up @@ -257,9 +260,14 @@ func TestThresholdValidator3of3(t *testing.T) {
var cosigner2 Cosigner
var cosigner3 Cosigner

cosigner1 = NewLocalCosigner(config1)
cosigner2 = NewLocalCosigner(config2)
cosigner3 = NewLocalCosigner(config3)
localsigner1 := NewLocalSigner("SoftSign", config1)
cosigner1 = NewLocalCosigner(config1, localsigner1)

localsigner2 := NewLocalSigner("SoftSign", config2)
cosigner2 = NewLocalCosigner(config2, localsigner2)

localsigner3 := NewLocalSigner("SoftSign", config3)
cosigner3 = NewLocalCosigner(config3, localsigner3)

require.Equal(t, cosigner1.GetID(), 1)
require.Equal(t, cosigner2.GetID(), 2)
Expand Down Expand Up @@ -409,9 +417,14 @@ func TestThresholdValidator2of3(t *testing.T) {
var cosigner2 Cosigner
var cosigner3 Cosigner

cosigner1 = NewLocalCosigner(config1)
cosigner2 = NewLocalCosigner(config2)
cosigner3 = NewLocalCosigner(config3)
localsigner1 := NewLocalSigner("SoftSign", config1)
cosigner1 = NewLocalCosigner(config1, localsigner1)

localsigner2 := NewLocalSigner("SoftSign", config2)
cosigner2 = NewLocalCosigner(config2, localsigner2)

localsigner3 := NewLocalSigner("SoftSign", config3)
cosigner3 = NewLocalCosigner(config3, localsigner3)

require.Equal(t, cosigner1.GetID(), 1)
require.Equal(t, cosigner2.GetID(), 2)
Expand Down

0 comments on commit 3b0bd1c

Please sign in to comment.