Skip to content

Commit

Permalink
Restructure files
Browse files Browse the repository at this point in the history
  • Loading branch information
swift1337 committed Nov 19, 2024
1 parent 82778ed commit ae2479b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 64 deletions.
58 changes: 10 additions & 48 deletions zetaclient/tss/keysign.go → zetaclient/tss/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,22 @@ package tss
import (
"bytes"
"encoding/base64"
"strings"

"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/ethereum/go-ethereum/crypto"
"github.com/pkg/errors"
"github.com/rs/zerolog"
tsscommon "gitlab.com/thorchain/tss/go-tss/common"
"gitlab.com/thorchain/tss/go-tss/keysign"

"github.com/zeta-chain/node/pkg/cosmos"
"github.com/zeta-chain/node/zetaclient/logs"
)

var (
testKeySignData = []byte("hello meta")
base64Decode = base64.StdEncoding.Decode
base64DecodeString = base64.StdEncoding.DecodeString
base64EncodeString = base64.StdEncoding.EncodeToString
)

// TestKeySign performs a TSS key-sign test of sample data.
func TestKeySign(keySigner KeySigner, tssPubKey string, logger zerolog.Logger) error {
logger = logger.With().Str(logs.FieldModule, "tss_keysign").Logger()

hashedData := crypto.Keccak256Hash(testKeySignData)

logger.Info().
Str("keysign.test_data", string(testKeySignData)).
Str("keysign.test_data_hash", hashedData.String()).
Msg("Performing TSS key-sign test")

req := keysign.NewRequest(
tssPubKey,
[]string{base64.StdEncoding.EncodeToString(hashedData.Bytes())},
10,
nil,
Version,
)

res, err := keySigner.KeySign(req)
switch {
case err != nil:
return errors.Wrap(err, "key signing request error")
case res.Status != tsscommon.Success:
logger.Error().Interface("keysign.fail_blame", res.Blame).Msg("Keysign failed")
return errors.Wrapf(err, "key signing is not successful (status %d)", res.Status)
case len(res.Signatures) == 0:
return errors.New("signatures list is empty")
}

// 32B msg hash, 32B R, 32B S, 1B RC
signature := res.Signatures[0]

logger.Info().Interface("keysign.signature", signature).Msg("Received signature from TSS")

if _, err = VerifySignature(signature, tssPubKey, hashedData.Bytes()); err != nil {
return errors.Wrap(err, "signature verification failed")
}

logger.Info().Msg("TSS key-sign test passed")

return nil
}

// VerifySignature checks that keysign.Signature is valid and origins from expected TSS public key.
// Also returns signature as [65]byte (R, S, V)
func VerifySignature(sig keysign.Signature, tssPubKey string, expectedMsgHash []byte) ([65]byte, error) {
Expand Down Expand Up @@ -116,3 +71,10 @@ func SignatureToBytes(input keysign.Signature) (sig [65]byte, err error) {

return sig, nil
}

// combineDigests combines the digests
func combineDigests(digestList []string) []byte {

Check failure on line 76 in zetaclient/tss/crypto.go

View workflow job for this annotation

GitHub Actions / build-and-test

other declaration of combineDigests

Check failure on line 76 in zetaclient/tss/crypto.go

View workflow job for this annotation

GitHub Actions / build-and-test

other declaration of combineDigests

Check failure on line 76 in zetaclient/tss/crypto.go

View workflow job for this annotation

GitHub Actions / lint

other declaration of combineDigests (typecheck)

Check failure on line 76 in zetaclient/tss/crypto.go

View workflow job for this annotation

GitHub Actions / lint

other declaration of combineDigests) (typecheck)
digestConcat := strings.Join(digestList, "")
digestBytes := chainhash.DoubleHashH([]byte(digestConcat))
return digestBytes.CloneBytes()
}
49 changes: 49 additions & 0 deletions zetaclient/tss/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package tss

import (
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/pkg/errors"
"github.com/rs/zerolog"
tsscommon "gitlab.com/thorchain/tss/go-tss/common"
"gitlab.com/thorchain/tss/go-tss/keygen"
"gitlab.com/thorchain/tss/go-tss/keysign"
"gitlab.com/thorchain/tss/go-tss/tss"
"golang.org/x/crypto/sha3"

Expand Down Expand Up @@ -229,3 +232,49 @@ func digestReq(req keygen.Request) (string, error) {

return digest, nil
}

var testKeySignData = []byte("hello meta")

// TestKeySign performs a TSS key-sign test of sample data.
func TestKeySign(keySigner KeySigner, tssPubKey string, logger zerolog.Logger) error {
logger = logger.With().Str(logs.FieldModule, "tss_keysign").Logger()

hashedData := crypto.Keccak256Hash(testKeySignData)

logger.Info().
Str("keysign.test_data", string(testKeySignData)).
Str("keysign.test_data_hash", hashedData.String()).
Msg("Performing TSS key-sign test")

req := keysign.NewRequest(
tssPubKey,
[]string{base64.StdEncoding.EncodeToString(hashedData.Bytes())},
10,
nil,
Version,
)

res, err := keySigner.KeySign(req)
switch {
case err != nil:
return errors.Wrap(err, "key signing request error")
case res.Status != tsscommon.Success:
logger.Error().Interface("keysign.fail_blame", res.Blame).Msg("Keysign failed")
return errors.Wrapf(err, "key signing is not successful (status %d)", res.Status)
case len(res.Signatures) == 0:
return errors.New("signatures list is empty")
}

// 32B msg hash, 32B R, 32B S, 1B RC
signature := res.Signatures[0]

logger.Info().Interface("keysign.signature", signature).Msg("Received signature from TSS")

if _, err = VerifySignature(signature, tssPubKey, hashedData.Bytes()); err != nil {
return errors.Wrap(err, "signature verification failed")
}

logger.Info().Msg("TSS key-sign test passed")

return nil
}
12 changes: 1 addition & 11 deletions zetaclient/tss/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package tss

import (
"context"
"encoding/base64"
"encoding/hex"
"fmt"
"strings"
"time"

"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -158,7 +155,7 @@ func (s *Service) SignBatch(

digestsBase64 := make([]string, len(digests))
for i, digest := range digests {
digestsBase64[i] = base64.StdEncoding.EncodeToString(digest)
digestsBase64[i] = base64EncodeString(digest)
}

tssPubKeyBech32 := s.PubKey().Bech32String()
Expand Down Expand Up @@ -274,13 +271,6 @@ func (s *Service) blameFailure(
return errFailure
}

// combineDigests combines the digests
func combineDigests(digestList []string) []byte {
digestConcat := strings.Join(digestList, "")
digestBytes := chainhash.DoubleHashH([]byte(digestConcat))
return digestBytes.CloneBytes()
}

func keysignLogFields(req keysign.Request, height, nonce uint64, chainID int64) map[string]any {
return map[string]any{
"keysign.chain_id": chainID,
Expand Down
10 changes: 5 additions & 5 deletions zetaclient/tss/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/ecdsa"
"crypto/rand"
"encoding/base64"
"fmt"
"regexp"
"testing"
Expand Down Expand Up @@ -129,14 +128,15 @@ func (m *keySignerMock) PubKeyBech32() string {
return bech32
}

// AddCall mimics TSS signature process (when called with provided arguments)
func (m *keySignerMock) AddCall(pk string, digests [][]byte, height int64, success bool, err error) {
if success && err != nil {
m.t.Fatalf("success and error are mutually exclusive")
}

var (
msgs = lo.Map(digests, func(digest []byte, _ int) string {
return base64.StdEncoding.EncodeToString(digest)
return base64EncodeString(digest)
})

req = keysign.NewRequest(pk, msgs, height, nil, Version)
Expand Down Expand Up @@ -175,9 +175,9 @@ func (m *keySignerMock) sign(req keysign.Request) keysign.Response {

signatures = append(signatures, keysign.Signature{
Msg: msg,
R: base64.StdEncoding.EncodeToString(sig[:32]),
S: base64.StdEncoding.EncodeToString(sig[32:64]),
RecoveryID: base64.StdEncoding.EncodeToString(sig[64:65]),
R: base64EncodeString(sig[:32]),
S: base64EncodeString(sig[32:64]),
RecoveryID: base64EncodeString(sig[64:65]),
})
}

Expand Down

0 comments on commit ae2479b

Please sign in to comment.