Skip to content

Commit

Permalink
Add new files and update existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
nitronit committed Dec 26, 2023
1 parent abeebc6 commit 31f7684
Show file tree
Hide file tree
Showing 26 changed files with 321 additions and 141 deletions.
12 changes: 6 additions & 6 deletions cmd/horcrux/cmd/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func addressCmd() *cobra.Command {
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {

var pubKey crypto.PubKey
var pubKey tss.PubKey

chainID := args[0]

Expand All @@ -51,7 +51,7 @@ func addressCmd() *cobra.Command {
return fmt.Errorf("error reading threshold key: %w, check that key is present for chain id: %s", err, chainID)
}

pubKey = key.PubKey
pubKey = key.PubKey.(crypto.PubKey)
case cconfig.SignModeSingle:
err := config.Config.ValidateSingleSignerConfig()
if err != nil {
Expand All @@ -68,10 +68,10 @@ func addressCmd() *cobra.Command {
default:
panic(fmt.Errorf("unexpected sign mode: %s", config.Config.SignMode))
}
pubKeyComet := pubKey.(crypto.PubKey)
pubKeyAddress := pubKeyComet.Address()

pubKeyAddress := pubKey.Address()

pubKeyJSON, err := cconfig.PubKey("", pubKey)
pubKeyJSON, err := cconfig.PubKey("", pubKeyComet)
if err != nil {
return err
}
Expand All @@ -87,7 +87,7 @@ func addressCmd() *cobra.Command {
return err
}
output.ValConsAddress = bech32ValConsAddress
pubKeyBech32, err := cconfig.PubKey(args[1], pubKey)
pubKeyBech32, err := cconfig.PubKey(args[1], pubKeyComet)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/horcrux/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.

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

"github.com/strangelove-ventures/horcrux/pkg/cosigner/nodesecurity"
tss "github.com/strangelove-ventures/horcrux/pkg/tss"
"github.com/strangelove-ventures/horcrux/pkg/tss"

"github.com/spf13/cobra"
)
Expand Down
21 changes: 10 additions & 11 deletions pkg/cosigner/remote.go → pkg/cosigner/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package cosigner

import (
"context"
"fmt"
"net/url"
"time"

cometcrypto "github.com/cometbft/cometbft/crypto"
"github.com/google/uuid"
"github.com/strangelove-ventures/horcrux/signer/proto"
"google.golang.org/grpc"
Expand Down Expand Up @@ -69,15 +67,15 @@ func (cosigner *RemoteCosigner) GetAddress() string {

// GetPubKey returns public key of the validator.
// Implements Cosigner interface
func (cosigner *RemoteCosigner) GetPubKey(_ string) (cometcrypto.PubKey, error) {
return nil, fmt.Errorf("unexpected call to RemoteCosigner.GetPubKey")
}
// func (cosigner *RemoteCosigner) GetPubKey(_ string) (cometcrypto.PubKey, error) {
// return nil, fmt.Errorf("unexpected call to RemoteCosigner.GetPubKey")
// }

// VerifySignature validates a signed payload against the public key.
// Implements Cosigner interface
func (cosigner *RemoteCosigner) VerifySignature(_ string, _, _ []byte) bool {
return false
}
// func (cosigner *RemoteCosigner) VerifySignature(_ string, _, _ []byte) bool {
// return false
// }

func getGRPCClient(address string) (proto.CosignerClient, error) {
var grpcAddress string
Expand Down Expand Up @@ -114,7 +112,7 @@ func (cosigner *RemoteCosigner) GetNonces(
for i, nonces := range res.Nonces {
out[i] = &CosignerUUIDNonces{
UUID: uuid.UUID(nonces.Uuid),
Nonces: CosignerNoncesFromProto(nonces.Nonces),
Nonces: FromProtoToNonces(nonces.Nonces),
}
}
return out, nil
Expand All @@ -123,7 +121,7 @@ func (cosigner *RemoteCosigner) GetNonces(
// Implements the cosigner interface
func (cosigner *RemoteCosigner) SetNoncesAndSign(
ctx context.Context,
req CosignerSetNoncesAndSignRequest) (*CosignerSignResponse, error) {
req CosignerSetNoncesAndSignRequest) (*SignatureResponse, error) {
res, err := cosigner.Client.SetNoncesAndSign(ctx, &proto.SetNoncesAndSignRequest{
Uuid: req.Nonces.UUID[:],
ChainID: req.ChainID,
Expand All @@ -134,13 +132,14 @@ func (cosigner *RemoteCosigner) SetNoncesAndSign(
if err != nil {
return nil, err
}
return &CosignerSignResponse{
return &SignatureResponse{
NoncePublic: res.GetNoncePublic(),
Timestamp: time.Unix(0, res.GetTimestamp()),
Signature: res.GetSignature(),
}, nil
}

// TODO: This should move to ThresholdValidator. Its is not the responsibility of the cosigner
func (cosigner *RemoteCosigner) Sign(
ctx context.Context,
req CosignerSignBlockRequest,
Expand Down
53 changes: 32 additions & 21 deletions pkg/cosigner/cosigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,52 @@ import (
/*
// threshold-ed25519
type MPC struct {
// our own cosigner
// our own cosigner (i.e server)
MyCosigner *LocalCosigner // TODO Should be an interface as well.
server
// peer cosigners
clients // peers to call
// peer cosigners (i.e clients to call)
peerCosigners []*RemoteCosigner // "i.e clients to call"
cosignerHealth *CosignerHealth
// FIX: This f-up a lot. Now its like 3-4 places that
// spaggettio leaders, cosigners etc etc
nonceCache *CosignerNonceCache
}
type Localcosigner interface {
type ServerCosigner interface {
// TODO - add methods
}
type Remotecosigner interface {
type ClientCosigner interface {
// TODO - add methods
}
*/
// CosignerSignRequest is sent to a co-signer to obtain their signature for the SignBytes
// SignatureRequest is sent to a co-signer to obtain their signature for the SignBytes
// The SignBytes should be a serialized block
type CosignerSignRequest struct {
type SignatureRequest struct {
ChainID string
SignBytes []byte
UUID uuid.UUID
}

type CosignerSignResponse struct {
type SignatureResponse struct {
NoncePublic []byte
Timestamp time.Time
Signature []byte
}

type CosignerNonce struct {
SourceID int
DestinationID int
type Nonce struct {
SourceID int // Client ID
DestinationID int // Server ID
PubKey []byte
Share []byte
Signature []byte
}

func (secretPart *CosignerNonce) toProto() *proto.Nonce {
func (secretPart *Nonce) toProto() *proto.Nonce {
return &proto.Nonce{
SourceID: int32(secretPart.SourceID),
DestinationID: int32(secretPart.DestinationID),
Expand All @@ -67,18 +75,19 @@ func (secretPart *CosignerNonce) toProto() *proto.Nonce {
}
}

// CosignerNonces are a list of CosignerNonce
type CosignerNonces []CosignerNonce
// Nonces is a list of CosignerNonce
type Nonces []Nonce

func (secretParts CosignerNonces) toProto() (out []*proto.Nonce) {
func (secretParts Nonces) toProto() (out []*proto.Nonce) {
for _, secretPart := range secretParts {
out = append(out, secretPart.toProto())
}
return
}

func CosignerNonceFromProto(secretPart *proto.Nonce) CosignerNonce {
return CosignerNonce{
// FromProtoToNonce converts a proto.Nonce to a Nonce
func FromProtoToNonce(secretPart *proto.Nonce) Nonce {
return Nonce{
SourceID: int(secretPart.SourceID),
DestinationID: int(secretPart.DestinationID),
PubKey: secretPart.PubKey,
Expand All @@ -87,10 +96,10 @@ func CosignerNonceFromProto(secretPart *proto.Nonce) CosignerNonce {
}
}

func CosignerNoncesFromProto(secretParts []*proto.Nonce) []CosignerNonce {
out := make([]CosignerNonce, len(secretParts))
func FromProtoToNonces(secretParts []*proto.Nonce) []Nonce {
out := make([]Nonce, len(secretParts))
for i, secretPart := range secretParts {
out[i] = CosignerNonceFromProto(secretPart)
out[i] = FromProtoToNonce(secretPart)
}
return out
}
Expand All @@ -103,9 +112,11 @@ type CosignerSignBlockRequest struct {
type CosignerSignBlockResponse struct {

Check warning on line 112 in pkg/cosigner/cosigner.go

View workflow job for this annotation

GitHub Actions / lint

exported: type name will be used as cosigner.CosignerSignBlockResponse by other packages, and that stutters; consider calling this SignBlockResponse (revive)
Signature []byte
}

// CosignerUUIDNonces
type CosignerUUIDNonces struct {

Check warning on line 117 in pkg/cosigner/cosigner.go

View workflow job for this annotation

GitHub Actions / lint

exported: type name will be used as cosigner.CosignerUUIDNonces by other packages, and that stutters; consider calling this UUIDNonces (revive)
UUID uuid.UUID
Nonces CosignerNonces
UUID uuid.UUID // UUID is the unique identifier of the nonce
Nonces Nonces
}

func (n *CosignerUUIDNonces) For(id int) *CosignerUUIDNonces {
Expand Down
5 changes: 5 additions & 0 deletions pkg/cosigner/icosigner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cosigner

type iCosigner interface {

Check failure on line 3 in pkg/cosigner/icosigner.go

View workflow job for this annotation

GitHub Actions / lint

type `iCosigner` is unused (unused)
Health()
}
2 changes: 1 addition & 1 deletion pkg/cosigner/icosigner_security.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type ICosignerSecurity interface {
id int,
noncePub []byte,
nonceShare []byte,
) (CosignerNonce, error)
) (Nonce, error)

// DecryptAndVerify decrypts the nonce and verifies the signature to authenticate the source cosigner.
DecryptAndVerify(
Expand Down
7 changes: 4 additions & 3 deletions pkg/cosigner/local_cosigner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ func testLocalCosignerSign(t *testing.T, threshold, total uint8, security []cosi
privKeyBytes := [64]byte{}
copy(privKeyBytes[:], privateKey[:])
privShards := tsed25519.DealShares(tsed25519.ExpandSecret(privKeyBytes[:32]), threshold, total)
pubKey := privateKey.PubKey()
// Returns the public key from the private key and type asserts it to an tss key.
pubKey := privateKey.PubKey().(tss.PubKey)

cfg := config.Config{
ThresholdModeConfig: &config.ThresholdModeConfig{
Expand All @@ -125,7 +126,7 @@ func testLocalCosignerSign(t *testing.T, threshold, total uint8, security []cosi
tmpDir := t.TempDir()

thresholdCosigners := make([]*cosigner.LocalCosigner, threshold)
nonces := make([][]cosigner.CosignerNonce, threshold)
nonces := make([][]cosigner.Nonce, threshold)

now := time.Now()

Expand Down Expand Up @@ -201,7 +202,7 @@ func testLocalCosignerSign(t *testing.T, threshold, total uint8, security []cosi
sigs := make([]types.PartialSignature, threshold)

for i, local_cosigner := range thresholdCosigners {

Check warning on line 204 in pkg/cosigner/local_cosigner_test.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; range var local_cosigner should be localCosigner (revive)
cosignerNonces := make([]cosigner.CosignerNonce, 0, threshold-1)
cosignerNonces := make([]cosigner.Nonce, 0, threshold-1)

for j, nonce := range nonces {
if i == j {
Expand Down
6 changes: 3 additions & 3 deletions pkg/cosigner/nodesecurity/cosigner_security_ecies.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func (c *CosignerSecurityECIES) GetID() int {

// EncryptAndSign encrypts the nonce and signs it for authentication.
func (c *CosignerSecurityECIES) EncryptAndSign(
id int, noncePub []byte, nonceShare []byte) (cosigner.CosignerNonce, error) {
nonce := cosigner.CosignerNonce{
id int, noncePub []byte, nonceShare []byte) (cosigner.Nonce, error) {
nonce := cosigner.Nonce{
SourceID: c.key.ID,
}

Expand Down Expand Up @@ -208,7 +208,7 @@ func (c *CosignerSecurityECIES) DecryptAndVerify(
return nil, nil, fmt.Errorf("unknown cosigner: %d", id)
}

digestMsg := cosigner.CosignerNonce{
digestMsg := cosigner.Nonce{
SourceID: id,
PubKey: encryptedNoncePub,
Share: encryptedNonceShare,
Expand Down
6 changes: 3 additions & 3 deletions pkg/cosigner/nodesecurity/cosigner_security_rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func (c *CosignerSecurityRSA) GetID() int {
}

// EncryptAndSign encrypts the nonce and signs it for authentication.
func (c *CosignerSecurityRSA) EncryptAndSign(id int, noncePub []byte, nonceShare []byte) (cosigner.CosignerNonce, error) {
nonce := cosigner.CosignerNonce{
func (c *CosignerSecurityRSA) EncryptAndSign(id int, noncePub []byte, nonceShare []byte) (cosigner.Nonce, error) {
nonce := cosigner.Nonce{
SourceID: c.key.ID,
}

Expand Down Expand Up @@ -195,7 +195,7 @@ func (c *CosignerSecurityRSA) DecryptAndVerify(
return nil, nil, fmt.Errorf("unknown cosigner: %d", id)
}

digestMsg := cosigner.CosignerNonce{
digestMsg := cosigner.Nonce{
SourceID: id,
PubKey: encryptedNoncePub,
Share: encryptedNonceShare,
Expand Down
Loading

0 comments on commit 31f7684

Please sign in to comment.