Skip to content

Commit

Permalink
Fix issues with cosigner and tss packages
Browse files Browse the repository at this point in the history
  • Loading branch information
nitronit committed Dec 23, 2023
1 parent 6011f98 commit 7f94824
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 31 deletions.
2 changes: 1 addition & 1 deletion cmd/horcrux/cmd/shards.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func createCosignerEd25519ShardsCmd() *cobra.Command {
cmd.SilenceUsage = true

for _, c := range csKeys {
dir, err := createCosignerDirectoryIfNecessary(out, c.ID())
dir, err := createCosignerDirectoryIfNecessary(out, c.ID)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/cosigner/local_cosigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,8 @@ func (cosigner *LocalCosigner) LoadSignStateIfNecessary(chainID string) error {
return err
}

// var signer IThresholdSigner

signer, err := tss.NewThresholdEd25519SignerSoft(cosigner.config, cosigner.GetIndex(), chainID)
var signer IThresholdSigner
signer, err = tss.NewThresholdEd25519SignerSoft(cosigner.config, cosigner.GetIndex(), chainID)
if err != nil {
return err
}
Expand Down
44 changes: 44 additions & 0 deletions pkg/tss/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package tss

/*
// Package tss provides functionality related to threshold signature schemes.
// It includes functions for key generation, signing, and verification.
//
// The tss package includes the following primary types:
//
// - Key: Represents a key used in threshold signature schemes.
//
// - Signature: Represents a signature produced by a threshold signature scheme.
//
// - Verifier: Provides functions for verifying signatures.
//
// - Signer: Provides functions for generating signatures.
//
// Example usage:
//
// // Generate a new key.
// key, err := tss.NewKey()
// if err != nil {
// log.Fatalf("failed to generate key: %v", err)
// }
//
// // Create a new signer.
// signer := tss.NewSigner(key)
//
// // Sign a message.
// msg := []byte("hello, world")
// sig, err := signer.Sign(msg)
// if err != nil {
// log.Fatalf("failed to sign message: %v", err)
// }
//
// // Create a new verifier.
// verifier := tss.NewVerifier(key)
//
// // Verify the signature.
// if !verifier.Verify(msg, sig) {
// log.Fatalf("failed to verify signature")
// }
//
// Note: This is a simplified example. The actual usage may be different depending on the specifics of the threshold signature scheme.

Check failure on line 43 in pkg/tss/doc.go

View workflow job for this annotation

GitHub Actions / lint

line is 134 characters (lll)
*/
5 changes: 0 additions & 5 deletions pkg/tss/godoc.go

This file was deleted.

10 changes: 5 additions & 5 deletions pkg/tss/threshold-ed25519_signer_soft.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ func NewThresholdEd25519SignerSoft(config *config.RuntimeConfig, id int, chainID

key, err := LoadVaultKeyFromFile(keyFile)
if err != nil {
return nil, fmt.Errorf("error reading cosigner key: %s", err)
return nil, fmt.Errorf("error reading Vault key: %s", err)
}

if key.id != id {
return nil, fmt.Errorf("key shard Index (%d) in (%s) does not match cosigner Index (%d)", key.id, keyFile, id)
if key.ID != id {
return nil, fmt.Errorf("key shard Index (%d) in (%s) does not match cosigner Index (%d)", key.ID, keyFile, id)
}

s := ThresholdSignerSoft{
privateKeyShard: key.privateShard,
privateKeyShard: key.PrivateShard,
pubKey: key.PubKey.Bytes(),
threshold: uint8(config.Config.ThresholdModeConfig.Threshold),
total: uint8(len(config.Config.ThresholdModeConfig.Cosigners)),
id: uint8(key.id),
id: uint8(key.ID),
}

return &s, nil
Expand Down
18 changes: 12 additions & 6 deletions pkg/tss/threshold_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ import (
"github.com/cometbft/cometbft/privval"
)

// LoadVaultKeyFromFile loads the persistent ThresholdSignerEd25519Key from file.
func LoadVaultKeyFromFile(file string) (VaultKey, error) {
pvKey := VaultKey{}
// LoadVaultKeyFromFile loads the persistent ThresholdSignerKey from file.

func LoadVaultKeyFromFile(file string) (Ed25519Key, error) {
//pvKey := VaultKey{}

Check failure on line 14 in pkg/tss/threshold_signer.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
var pvKey Ed25519Key
keyJSONBytes, err := os.ReadFile(file)
if err != nil {
if err != nil || len(keyJSONBytes) == 0 {
fmt.Printf("Could not read key from file %s", file)
return pvKey, err
}

fmt.Printf("keyJsonBytes is: %s", keyJSONBytes)

err = json.Unmarshal(keyJSONBytes, &pvKey)
if err != nil {
fmt.Printf("Could not unmarshal key from file %s", file)
return pvKey, err
}

Expand Down Expand Up @@ -54,8 +60,8 @@ func generatePersistentThresholdSignShards(filePVKey privval.FilePVKey, function
for i, key := range keys {
vaultKeys[i] = VaultKey{
PubKey: key.PubKey,
privateShard: key.PrivateShard,
id: key.ID,
PrivateShard: key.PrivateShard,
ID: key.ID,
}
}
return vaultKeys, nil
Expand Down
23 changes: 13 additions & 10 deletions pkg/tss/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,27 @@ type PersistentEd25519Key struct {
index int // Shamir index of this shard

Check failure on line 28 in pkg/tss/types.go

View workflow job for this annotation

GitHub Actions / lint

field `index` is unused (unused)
}

type VaultKey struct {
PubKey cometcrypto.PubKey `json:"pubKey"`
privateShard []byte //`// json:"privateShard"`
id int //`/Shamir index / json:"id"`
}
/*
type Ed25519Key struct {
PubKey cometcrypto.PubKey `json:"pubKey"`
PrivateShard []byte `json:"privateShard"`
Id int `json:"id"`
}
// TODO: Should not actually get this.
func (key *VaultKey) ID() int {
return key.id
}
func (key *Ed25519Key) ID() int {
return key.Id
}
*/
type Ed25519Key struct {
PubKey cometcrypto.PubKey `json:"pubKey"`
PrivateShard []byte `json:"privateShard"`
ID int `json:"id"`
}

type VaultKey Ed25519Key

// TODO: redo to a function.
func (key *Ed25519Key) MarshalJSON() ([]byte, error) {
type Alias Ed25519Key
Expand All @@ -68,7 +72,6 @@ func (key *Ed25519Key) MarshalJSON() ([]byte, error) {
})
}

// redo to a function
func (key *Ed25519Key) UnmarshalJSON(data []byte) error {
type Alias Ed25519Key

Expand Down Expand Up @@ -126,7 +129,7 @@ func ReadCometBFTPrivValidatorFile(filename string) (out privval.FilePVKey, err
}

type VaultPrivateKey interface {
VaultKey | Ed25519Key
Ed25519Key | VaultKey
}

// WriteToFile writes a key structure to a given file name.
Expand Down
2 changes: 1 addition & 1 deletion signer/cosigner_grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func (rpc *NodeGRPCServer) TransferLeadership(
shardIndex := fmt.Sprint(c.GetIndex())
if shardIndex == leaderID {
raftAddress := p2pURLToRaftAddress(c.GetAddress())

// TODO: Change to logging
fmt.Printf("Transferring leadership to Index: %s - Address: %s\n", shardIndex, raftAddress)
rpc.raftStore.raft.LeadershipTransferToServer(raft.ServerID(shardIndex), raft.ServerAddress(raftAddress))
return &proto.TransferLeadershipResponse{LeaderID: shardIndex, LeaderAddress: raftAddress}, nil
}
Expand Down

0 comments on commit 7f94824

Please sign in to comment.