Skip to content

Commit

Permalink
Merge branch 'merge-dev' into 'dev'
Browse files Browse the repository at this point in the history
Merge dev

See merge request ergo/rosen-bridge/sign-protocols!31
  • Loading branch information
vorujack committed May 27, 2024
2 parents e2261b6 + c6f2820 commit d45371d
Show file tree
Hide file tree
Showing 14 changed files with 683 additions and 268 deletions.
50 changes: 38 additions & 12 deletions services/tss-api/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"fmt"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
"net/http"
Expand All @@ -16,10 +17,12 @@ type TssController interface {
Sign() echo.HandlerFunc
Keygen() echo.HandlerFunc
Message() echo.HandlerFunc
Validate(interface{}) error
}

type tssController struct {
rosenTss _interface.RosenTss
rosenTss _interface.RosenTss
validator *validator.Validate
}

type response struct {
Expand All @@ -32,7 +35,8 @@ var logging *zap.SugaredLogger
func NewTssController(rosenTss _interface.RosenTss) TssController {
logging = logger.NewSugar("controller")
return &tssController{
rosenTss: rosenTss,
rosenTss: rosenTss,
validator: validator.New(),
}
}

Expand All @@ -50,6 +54,13 @@ func (tssController *tssController) checkKeygenOperation(crypto string) error {
return nil
}

func (tssController *tssController) Validate(i interface{}) error {
if err := tssController.validator.Struct(i); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
return nil
}

// check if there is any common operation between forbidden and running ones.
func (tssController *tssController) checkSignOperation(crypto string) error {
forbiddenOperations := []string{crypto + "Keygen", crypto + "Regroup"}
Expand Down Expand Up @@ -78,14 +89,17 @@ func (tssController *tssController) checkOperation(operationName string, crypto

// Keygen returns echo handler, starting new keygen process
func (tssController *tssController) Keygen() echo.HandlerFunc {
return func(c echo.Context) error {
return func(c echo.Context) (err error) {
data := models.KeygenMessage{}

if err := c.Bind(&data); err != nil {
if err = c.Bind(&data); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
if err = c.Validate(&data); err != nil {
return err
}
logging.Debugf("keygen controller called with data: {%v}", data)
err := tssController.checkOperation("keygen", data.Crypto)
err = tssController.checkOperation("keygen", data.Crypto)
if err != nil {
return echo.NewHTTPError(http.StatusConflict, err.Error())
}
Expand All @@ -110,14 +124,17 @@ func (tssController *tssController) Keygen() echo.HandlerFunc {

// returns echo handler, starting new sign process.
func (tssController *tssController) Sign() echo.HandlerFunc {
return func(c echo.Context) error {
return func(c echo.Context) (err error) {
data := models.SignMessage{}

if err := c.Bind(&data); err != nil {
if err = c.Bind(&data); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
if err = c.Validate(&data); err != nil {
return err
}
logging.Debugf("sign controller called with data: {%v}", data)
err := tssController.checkOperation("sign", data.Crypto)
err = tssController.checkOperation("sign", data.Crypto)
if err != nil {
return echo.NewHTTPError(http.StatusConflict, err.Error())
}
Expand All @@ -126,7 +143,11 @@ func (tssController *tssController) Sign() echo.HandlerFunc {
switch err.Error() {
case models.DuplicatedMessageIdError:
return echo.NewHTTPError(http.StatusConflict, err.Error())
case models.NoKeygenDataFoundError, models.WrongCryptoProtocolError:
case
models.ECDSANoKeygenDataFoundError,
models.WrongDerivationPathError,
models.EDDSANoKeygenDataFoundError,
models.WrongCryptoProtocolError:
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
default:
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
Expand Down Expand Up @@ -164,9 +185,14 @@ func (tssController *tssController) Message() echo.HandlerFunc {
// returns echo handler, get threshold of meta data
func (tssController *tssController) Threshold() echo.HandlerFunc {
return func(c echo.Context) error {
meta := tssController.rosenTss.GetMetaData()
if meta == (models.MetaData{}) {
return echo.NewHTTPError(http.StatusBadRequest, models.NoMetaDataFoundError)
crypto := c.QueryParam("crypto")
if crypto == "" {
return echo.NewHTTPError(http.StatusBadRequest, models.InvalidCryptoFoundError)
}

meta, err := tssController.rosenTss.GetMetaData(crypto)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
res := map[string]int{"threshold": meta.Threshold}
return c.JSON(http.StatusOK, res)
Expand Down
1 change: 1 addition & 0 deletions services/tss-api/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func InitRouting(e *echo.Echo, tssController TssController) {
// Middleware
zapLogger := logger.NewLogger().Named("tss/http")

e.Validator = tssController
e.Use(echozap.ZapLogger(zapLogger))
e.Use(middleware.Recover())

Expand Down
8 changes: 4 additions & 4 deletions services/tss-api/app/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"rosen-bridge/tss-api/storage"
)

// (keygen eddsa protocol)
// (keygen protocol)
type KeygenOperation interface {
Init(RosenTss, []string) error
StartAction(RosenTss, chan models.GossipMessage, chan error) error
GetClassName() string
}

// (sign eddsa protocol)
// (sign protocol)
type SignOperation interface {
Init(RosenTss, []models.Peer) error
StartAction(RosenTss, chan models.GossipMessage, chan error) error
Expand All @@ -29,8 +29,8 @@ type RosenTss interface {
GetStorage() storage.Storage
GetConnection() network.Connection

SetMetaData(data models.MetaData) error
GetMetaData() models.MetaData
SetMetaData(data models.MetaData, crypto string) error
GetMetaData(crypto string) (models.MetaData, error)

SetPeerHome(string) error
GetPeerHome() string
Expand Down
20 changes: 15 additions & 5 deletions services/tss-api/app/keygen/ecdsa/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func (s *operationECDSAKeygen) Init(rosenTss _interface.RosenTss, peers []string
PeersCount: s.KeygenMessage.PeersCount,
Threshold: s.KeygenMessage.Threshold,
}
err := rosenTss.SetMetaData(meta)

err := rosenTss.SetMetaData(meta, models.ECDSA)
if err != nil {
return err
}
Expand Down Expand Up @@ -65,9 +66,14 @@ func (s *operationECDSAKeygen) CreateParty(rosenTss _interface.RosenTss, statusC
outCh := make(chan tss.Message, len(s.LocalTssData.PartyIds))
endCh := make(chan *ecdsaKeygen.LocalPartySaveData, len(s.LocalTssData.PartyIds))

threshold := rosenTss.GetMetaData().Threshold
ecdsaMetaData, err := rosenTss.GetMetaData(models.ECDSA)
if err != nil {
s.Logger.Errorf("there was an error in getting metadata: %+v", err)
errorCh <- err
return
}

err := s.StartParty(&s.LocalTssData, threshold, outCh, endCh)
err = s.StartParty(&s.LocalTssData, ecdsaMetaData.Threshold, outCh, endCh)
if err != nil {
s.Logger.Errorf("there was an error in starting party: %+v", err)
errorCh <- err
Expand Down Expand Up @@ -222,15 +228,19 @@ func (s *operationECDSAKeygen) HandleEndMessage(rosenTss _interface.RosenTss, ke
PubKey: encodedPK,
Status: "success",
}
ecdsaMetaData, err := rosenTss.GetMetaData(models.ECDSA)
if err != nil {
return err
}
tssConfigECDSA := models.TssConfigECDSA{
MetaData: rosenTss.GetMetaData(),
MetaData: ecdsaMetaData,
KeygenData: *keygenData,
}

s.Logger.Infof("hex pubKey: %v", encodedPK)
s.Logger.Infof("keygen process for ShareID: {%s} and Crypto: {%s} finished.", shareIDStr, s.KeygenMessage.Crypto)

err := rosenTss.GetStorage().WriteData(tssConfigECDSA, rosenTss.GetPeerHome(), keygen.KeygenFileName, "ecdsa")
err = rosenTss.GetStorage().WriteData(tssConfigECDSA, rosenTss.GetPeerHome(), keygen.KeygenFileName, models.ECDSA)
if err != nil {
return err
}
Expand Down
20 changes: 15 additions & 5 deletions services/tss-api/app/keygen/eddsa/eddsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (s *operationEDDSAKeygen) Init(rosenTss _interface.RosenTss, peers []string
PeersCount: s.KeygenMessage.PeersCount,
Threshold: s.KeygenMessage.Threshold,
}
err := rosenTss.SetMetaData(meta)
err := rosenTss.SetMetaData(meta, models.EDDSA)
if err != nil {
return err
}
Expand Down Expand Up @@ -64,9 +64,14 @@ func (s *operationEDDSAKeygen) CreateParty(rosenTss _interface.RosenTss, statusC
outCh := make(chan tss.Message, len(s.LocalTssData.PartyIds))
endCh := make(chan *eddsaKeygen.LocalPartySaveData, len(s.LocalTssData.PartyIds))

threshold := rosenTss.GetMetaData().Threshold
metaData, err := rosenTss.GetMetaData(models.EDDSA)
if err != nil {
s.Logger.Errorf("there was an error in getting metadata: %+v", err)
errorCh <- err
return
}

err := s.StartParty(&s.LocalTssData, threshold, outCh, endCh)
err = s.StartParty(&s.LocalTssData, metaData.Threshold, outCh, endCh)
if err != nil {
s.Logger.Errorf("there was an error in starting party: %+v", err)
errorCh <- err
Expand Down Expand Up @@ -221,15 +226,20 @@ func (s *operationEDDSAKeygen) HandleEndMessage(rosenTss _interface.RosenTss, ke
PubKey: encodedPK,
Status: "success",
}
eddsaMetaData, err := rosenTss.GetMetaData(models.EDDSA)
if err != nil {
return err
}

tssConfigEDDSA := models.TssConfigEDDSA{
MetaData: rosenTss.GetMetaData(),
MetaData: eddsaMetaData,
KeygenData: *keygenData,
}

s.Logger.Infof("hex pubKey: %v", encodedPK)
s.Logger.Infof("keygen process for ShareID: {%s} and Crypto: {%s} finished.", shareIDStr, s.KeygenMessage.Crypto)

err := rosenTss.GetStorage().WriteData(tssConfigEDDSA, rosenTss.GetPeerHome(), keygen.KeygenFileName, "eddsa")
err = rosenTss.GetStorage().WriteData(tssConfigEDDSA, rosenTss.GetPeerHome(), keygen.KeygenFileName, models.EDDSA)
if err != nil {
return err
}
Expand Down
57 changes: 44 additions & 13 deletions services/tss-api/app/rosenTss.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.uber.org/zap"
"golang.org/x/crypto/blake2b"
"rosen-bridge/tss-api/app/interface"
ecdsaSign "rosen-bridge/tss-api/app/sign/ecdsa"
eddsaSign "rosen-bridge/tss-api/app/sign/eddsa"
"rosen-bridge/tss-api/logger"
"rosen-bridge/tss-api/models"
Expand All @@ -23,7 +24,8 @@ type rosenTss struct {
ChannelMap map[string]chan models.GossipMessage
KeygenOperationMap map[string]_interface.KeygenOperation
SignOperationMap map[string]_interface.SignOperation
metaData models.MetaData
eddsaMetaData models.MetaData
ecdsaMetaData models.MetaData
storage storage.Storage
connection network.Connection
Config models.Config
Expand All @@ -41,7 +43,8 @@ func NewRosenTss(connection network.Connection, storage storage.Storage, config
ChannelMap: make(map[string]chan models.GossipMessage),
KeygenOperationMap: make(map[string]_interface.KeygenOperation),
SignOperationMap: make(map[string]_interface.SignOperation),
metaData: models.MetaData{},
eddsaMetaData: models.MetaData{},
ecdsaMetaData: models.MetaData{},
storage: storage,
connection: connection,
trustKey: trustKey,
Expand Down Expand Up @@ -95,9 +98,9 @@ func (r *rosenTss) StartNewKeygen(keygenMessage models.KeygenMessage) error {

var operation _interface.KeygenOperation
switch keygenMessage.Crypto {
case "eddsa":
case models.EDDSA:
operation = eddsaKeygen.NewKeygenEDDSAOperation(keygenMessage)
case "ecdsa":
case models.ECDSA:
operation = ecdsaKeygen.NewKeygenECDSAOperation(keygenMessage)
default:
return fmt.Errorf(models.WrongCryptoProtocolError)
Expand Down Expand Up @@ -150,14 +153,19 @@ func (r *rosenTss) StartNewSign(signMessage models.SignMessage) error {
}

var operation _interface.SignOperation
println(signMessage.Crypto)
switch signMessage.Crypto {
case "eddsa":
case models.EDDSA:
operation = eddsaSign.NewSignEDDSAOperation(signMessage)
case models.ECDSA:
if len(signMessage.DerivationPath) == 0 {
return fmt.Errorf(models.WrongDerivationPathError)
}
operation = ecdsaSign.NewSignECDSAOperation(signMessage)
default:
return fmt.Errorf(models.WrongCryptoProtocolError)
}
channelId := fmt.Sprintf("%s%s", operation.GetClassName(), messageId)

channelId := fmt.Sprintf("%s%s%s", operation.GetClassName(), signMessage.ChainCode, messageId)
r.SignOperationMap[channelId] = operation

errorCh := make(chan error)
Expand Down Expand Up @@ -256,14 +264,37 @@ func (r *rosenTss) GetPeerHome() string {
}

// setting ups metadata from given file in the home directory
func (r *rosenTss) SetMetaData(meta models.MetaData) error {
r.metaData = meta
return nil
func (r *rosenTss) SetMetaData(meta models.MetaData, crypto string) error {
switch crypto {
case models.EDDSA:
r.eddsaMetaData = meta
return nil
case models.ECDSA:
r.ecdsaMetaData = meta
return nil
default:
return fmt.Errorf(models.WrongCryptoProtocolError)
}
}

// returns peer's meta data
func (r *rosenTss) GetMetaData() models.MetaData {
return r.metaData
func (r *rosenTss) GetMetaData(crypto string) (models.MetaData, error) {
switch crypto {
case models.EDDSA:
if (r.eddsaMetaData != models.MetaData{}) {
return r.eddsaMetaData, nil
} else {
return r.eddsaMetaData, fmt.Errorf(models.EDDSANoMetaDataFoundError)
}
case models.ECDSA:
if (r.ecdsaMetaData != models.MetaData{}) {
return r.ecdsaMetaData, nil
} else {
return r.ecdsaMetaData, fmt.Errorf(models.ECDSANoMetaDataFoundError)
}
default:
return models.MetaData{}, fmt.Errorf(models.WrongCryptoProtocolError)
}
}

// returns list of operations
Expand All @@ -290,7 +321,7 @@ func (r *rosenTss) deleteInstance(operationType string, messageId string, channe
func (r *rosenTss) deleteKeygenInstance(messageId string, channelId string, errorCh chan error) {
operationName := r.KeygenOperationMap[channelId].GetClassName()
logging.Debugf("deleting %s for channelId %s and messageId %s for keygen operation", operationName, channelId, messageId)
delete(r.SignOperationMap, channelId)
delete(r.KeygenOperationMap, channelId)
delete(r.ChannelMap, messageId)
close(errorCh)
logging.Infof("operation %s removed for channelId %s and messageId %s for keygen operation", operationName, channelId, messageId)
Expand Down
Loading

0 comments on commit d45371d

Please sign in to comment.