From 2e701b2e4faf26bf87091afabb32006b5bfe5dd1 Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Wed, 8 Jan 2025 19:11:47 +0200 Subject: [PATCH 01/20] signer init --- go.mod | 5 +- go.sum | 2 + internal/tss/signerer.go | 203 +++++++++++++++++++++++++++++++++++++++ internal/tss/utils.go | 5 + 4 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 internal/tss/signerer.go diff --git a/go.mod b/go.mod index b546d05..d08112a 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ replace ( require ( github.com/bnb-chain/tss-lib/v2 v2.0.2 github.com/cosmos/cosmos-sdk v0.46.13 + github.com/ethereum/go-ethereum v1.10.26 github.com/hashicorp/vault/api v1.15.0 github.com/hyle-team/bridgeless-core v0.0.0-20241003084139-414cc4a6f73c github.com/pkg/errors v0.9.1 @@ -42,7 +43,10 @@ require ( github.com/armon/go-metrics v0.4.1 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect + github.com/btcsuite/btcd v0.23.4 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect + github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect + github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/cespare/xxhash v1.1.0 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect @@ -64,7 +68,6 @@ require ( github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/dvsekhvalnov/jose2go v1.5.0 // indirect - github.com/ethereum/go-ethereum v1.10.26 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/getsentry/sentry-go v0.27.0 // indirect github.com/go-gorp/gorp/v3 v3.1.0 // indirect diff --git a/go.sum b/go.sum index fec60cf..a22d0d4 100644 --- a/go.sum +++ b/go.sum @@ -1268,6 +1268,8 @@ github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtyd github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= github.com/btcsuite/btcutil v1.0.2/go.mod h1:j9HUFwoQRsZL3V4n+qG+CUnEGHOarIxfC3Le2Yhbcts= +github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce h1:YtWJF7RHm2pYCvA5t0RPmAaLUhREsKuKd+SLhxFbFeQ= +github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce/go.mod h1:0DVlHczLPewLcPGEIeUEzfOJhqGPQ0mJJRDBtD307+o= github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg= github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY= github.com/btcsuite/goleveldb v1.0.0/go.mod h1:QiK9vBlgftBg6rWQIj6wFzbPfRjiykIEhBH4obrXJ/I= diff --git a/internal/tss/signerer.go b/internal/tss/signerer.go new file mode 100644 index 0000000..ae0c1ad --- /dev/null +++ b/internal/tss/signerer.go @@ -0,0 +1,203 @@ +package tss + +import ( + "context" + "github.com/bnb-chain/tss-lib/v2/common" + "github.com/bnb-chain/tss-lib/v2/ecdsa/keygen" + "github.com/bnb-chain/tss-lib/v2/ecdsa/signing" + "github.com/bnb-chain/tss-lib/v2/tss" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/hyle-team/tss-svc/internal/core" + "github.com/hyle-team/tss-svc/internal/p2p" + "gitlab.com/distributed_lab/logan/v3" + "google.golang.org/protobuf/types/known/anypb" + "math/big" + "sync" + "sync/atomic" +) + +type LocalSignParty struct { + Address core.Address + data *keygen.LocalPartySaveData +} + +type SignParty struct { + wg *sync.WaitGroup + + parties map[core.Address]struct{} + sortedPartyIds tss.SortedPartyIDs + + self LocalSignParty + + logger *logan.Entry + party tss.Party + msgs chan partyMsg + broadcaster interface { + Send(msg *p2p.SubmitRequest, to core.Address) error + Broadcast(msg *p2p.SubmitRequest) error + } + + data string + threshold int + + ended atomic.Bool + result *common.SignatureData + sessionId string +} + +func NewSignParty(wg *sync.WaitGroup, self LocalSignParty, parties []p2p.Party, data, sessionId string) *SignParty { + partyMap := make(map[core.Address]struct{}, len(parties)) + partyIds := make([]*tss.PartyID, len(parties)+1) + partyIds[0] = p2p.AddrToPartyIdentifier(self.Address) + + for i, party := range parties { + if party.CoreAddress == self.Address { + continue + } + + partyMap[party.CoreAddress] = struct{}{} + partyIds[i+1] = party.Identifier() + } + return &SignParty{ + wg: wg, + self: self, + sortedPartyIds: tss.SortPartyIDs(partyIds), + parties: partyMap, + data: data, + threshold: GetThreshold(tss.SortPartyIDs(partyIds).Len()), + msgs: make(chan partyMsg, MsgsCapacity), + sessionId: sessionId, + } +} + +func (p *SignParty) Run(ctx context.Context) { + p.logger.Infof("Running TSS signing on set: %v", p.parties) + params := tss.NewParameters( + tss.S256(), tss.NewPeerContext(p.sortedPartyIds), + p2p.AddrToPartyIdentifier(p.self.Address), + len(p.sortedPartyIds), + len(p.sortedPartyIds), + ) + out := make(chan tss.Message, OutChannelSize) + end := make(chan *common.SignatureData, EndChannelSize) + + p.party = signing.NewLocalParty(new(big.Int).SetBytes(hexutil.MustDecode(p.data)), params, *p.self.data, out, end) + + p.wg.Add(3) + + go func() { + defer p.wg.Done() + if err := p.party.Start(); err != nil { + p.logger.WithError(err).Error("failed to run signer party") + close(end) + } + }() + go p.receiveMsgs(ctx) + go p.receiveUpdates(ctx, out, end) +} + +func (p *SignParty) WaitFor() *common.SignatureData { + p.wg.Wait() + p.ended.Store(true) + return p.result +} + +// Receive adds msg to msgs chan +func (p *SignParty) Receive(sender core.Address, data *p2p.TssData) { + if p.ended.Load() { + return + } + + p.msgs <- partyMsg{ + Sender: sender, + WireMsg: data.Data, + IsBroadcast: data.IsBroadcast, + } +} + +// receiveMsgs receives message from msg chan and updates party`s internal state +func (p *SignParty) receiveMsgs(ctx context.Context) { + defer p.wg.Done() + + for { + select { + case <-ctx.Done(): + p.logger.Warn("context is done; stopping receiving messages") + return + case msg, closed := <-p.msgs: + if closed { + p.logger.Debug("msg channel is closed") + return + } + + if _, exists := p.parties[msg.Sender]; !exists { + p.logger.Warn("got message from outside party") + continue + } + + _, err := p.party.UpdateFromBytes(msg.WireMsg, p2p.AddrToPartyIdentifier(msg.Sender), msg.IsBroadcast) + if err != nil { + p.logger.WithError(err).Error("failed to update party state") + } + } + } + +} + +func (p *SignParty) receiveUpdates(ctx context.Context, out <-chan tss.Message, end <-chan *common.SignatureData) { + defer p.wg.Done() + + for { + select { + case <-ctx.Done(): + p.logger.Warn("context is done; stopping listening to updates") + return + case result, ok := <-end: + close(p.msgs) + p.result = result + + if !ok { + p.logger.Error("tss party result channel is closed") + } + + return + case msg := <-out: + raw, routing, err := msg.WireBytes() + if err != nil { + p.logger.WithError(err).Error("failed to get message wire bytes") + continue + } + + tssData := &p2p.TssData{ + Data: raw, + IsBroadcast: routing.IsBroadcast, + } + + tssReq, _ := anypb.New(tssData) + submitReq := p2p.SubmitRequest{ + Sender: p.self.Address.String(), + SessionId: p.sessionId, + Type: p2p.RequestType_SIGN, + Data: tssReq, + } + + destination := routing.To + if destination == nil || len(destination) > 1 { + if err = p.broadcaster.Broadcast(&submitReq); err != nil { + p.logger.WithError(err).Error("failed to broadcast message") + } + continue + } + + dst, err := p2p.AddrFromPartyIdentifier(destination[0]) + if err != nil { + p.logger.WithError(err).Error("failed to get destination address") + continue + } + + if err = p.broadcaster.Send(&submitReq, dst); err != nil { + p.logger.WithError(err).Error("failed to send message") + } + } + } +} diff --git a/internal/tss/utils.go b/internal/tss/utils.go index 80f5d9a..cd6594a 100644 --- a/internal/tss/utils.go +++ b/internal/tss/utils.go @@ -15,3 +15,8 @@ type partyMsg struct { WireMsg []byte IsBroadcast bool } + +func GetThreshold(n int) int { + var res = float32(n) * 2 / 3 + return int(res) +} From 0bfb6a962c18d0901b3027b1b9abf49d941060f9 Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Wed, 8 Jan 2025 19:48:17 +0200 Subject: [PATCH 02/20] signer --- internal/tss/session/boundaries.go | 3 +- internal/tss/session/sign.go | 122 ++++++++++++++++++++++++ internal/tss/{signerer.go => signer.go} | 6 +- 3 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 internal/tss/session/sign.go rename internal/tss/{signerer.go => signer.go} (95%) diff --git a/internal/tss/session/boundaries.go b/internal/tss/session/boundaries.go index 578270a..08829ca 100644 --- a/internal/tss/session/boundaries.go +++ b/internal/tss/session/boundaries.go @@ -3,5 +3,6 @@ package session import "time" const ( - BoundaryKeygenSession = time.Minute + BoundaryKeygenSession = time.Minute + BoundarySigningSession = 10 * time.Second ) diff --git a/internal/tss/session/sign.go b/internal/tss/session/sign.go new file mode 100644 index 0000000..cb0027e --- /dev/null +++ b/internal/tss/session/sign.go @@ -0,0 +1,122 @@ +package session + +import ( + "context" + "fmt" + "github.com/bnb-chain/tss-lib/v2/common" + "github.com/hyle-team/tss-svc/internal/core" + "github.com/hyle-team/tss-svc/internal/p2p" + "github.com/hyle-team/tss-svc/internal/tss" + "github.com/pkg/errors" + "gitlab.com/distributed_lab/logan/v3" + "sync" + "time" +) + +type SigningSessionParams struct { + Id string + StartTime time.Time +} + +type SigningSession struct { + params SigningSessionParams + logger *logan.Entry + wg *sync.WaitGroup + + connectedPartiesCount func() int + partiesCount int + + signingParty interface { + Run(ctx context.Context) + WaitFor() *common.SignatureData + Receive(sender core.Address, data *p2p.TssData) + } + + data string + result *common.SignatureData + err error +} + +func NewSigningSession(self tss.LocalSignParty, params SigningSessionParams, logger *logan.Entry, parties []p2p.Party, data string, connectedPartiesCountFunc func() int) *SigningSession { + return &SigningSession{ + params: params, + wg: &sync.WaitGroup{}, + logger: logger, + connectedPartiesCount: connectedPartiesCountFunc, + partiesCount: len(parties), + data: data, + signingParty: tss.NewSignParty(self, parties, data, params.Id, logger), + } +} + +func (s *SigningSession) Run(ctx context.Context) error { + runDelay := time.Until(s.params.StartTime) + if runDelay <= 0 { + return errors.New("target time is in the past") + } + + s.logger.Info(fmt.Sprintf("signing session will start in %s", runDelay)) + + select { + case <-ctx.Done(): + s.logger.Info("signing session cancelled") + return nil + case <-time.After(runDelay): + } + + if s.connectedPartiesCount() != s.partiesCount { + return errors.New("cannot start signing session: not all parties connected") + } + + s.wg.Add(1) + go s.run(ctx) + return nil +} + +func (s *SigningSession) run(ctx context.Context) { + defer s.wg.Done() + + boundedCtx, cancel := context.WithTimeout(ctx, BoundarySigningSession) + defer cancel() + + s.signingParty.Run(boundedCtx) + s.result = s.signingParty.WaitFor() + s.logger.Info("signing session finished") + if s.result != nil { + return + } + + if err := boundedCtx.Err(); err != nil { + s.err = err + } else { + s.err = errors.New("signing session error occurred") + } +} + +func (s *SigningSession) WaitFor() (*common.SignatureData, error) { + s.wg.Wait() + return s.result, s.err +} + +func (s *SigningSession) Id() string { + return s.params.Id +} + +func (s *SigningSession) Receive(request *p2p.SubmitRequest) error { + if request.Type != p2p.RequestType_SIGN { + return errors.New("invalid request type") + } + + var data *p2p.TssData + + if err := request.Data.UnmarshalTo(data); err != nil { + return errors.Wrap(err, "failed to unmarshal TSS request data") + } + + sender, _ := core.AddressFromString(request.Sender) + s.signingParty.Receive(sender, data) + return nil +} + +// RegisterIdChangeListener is a no-op for SigningSession +func (s *SigningSession) RegisterIdChangeListener(func(oldId, newId string)) {} diff --git a/internal/tss/signerer.go b/internal/tss/signer.go similarity index 95% rename from internal/tss/signerer.go rename to internal/tss/signer.go index ae0c1ad..b04ed05 100644 --- a/internal/tss/signerer.go +++ b/internal/tss/signer.go @@ -45,7 +45,7 @@ type SignParty struct { sessionId string } -func NewSignParty(wg *sync.WaitGroup, self LocalSignParty, parties []p2p.Party, data, sessionId string) *SignParty { +func NewSignParty(self LocalSignParty, parties []p2p.Party, data, sessionId string, logger *logan.Entry) *SignParty { partyMap := make(map[core.Address]struct{}, len(parties)) partyIds := make([]*tss.PartyID, len(parties)+1) partyIds[0] = p2p.AddrToPartyIdentifier(self.Address) @@ -59,7 +59,7 @@ func NewSignParty(wg *sync.WaitGroup, self LocalSignParty, parties []p2p.Party, partyIds[i+1] = party.Identifier() } return &SignParty{ - wg: wg, + wg: &sync.WaitGroup{}, self: self, sortedPartyIds: tss.SortPartyIDs(partyIds), parties: partyMap, @@ -67,6 +67,8 @@ func NewSignParty(wg *sync.WaitGroup, self LocalSignParty, parties []p2p.Party, threshold: GetThreshold(tss.SortPartyIDs(partyIds).Len()), msgs: make(chan partyMsg, MsgsCapacity), sessionId: sessionId, + logger: logger, + broadcaster: p2p.NewBroadcaster(parties), } } From ae8c3f4a0e6469bcc8f393b58bdd7dceeab9d40b Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 11:38:15 +0200 Subject: [PATCH 03/20] signer fixes --- internal/tss/signer.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/tss/signer.go b/internal/tss/signer.go index b04ed05..08e6fbd 100644 --- a/internal/tss/signer.go +++ b/internal/tss/signer.go @@ -48,7 +48,7 @@ type SignParty struct { func NewSignParty(self LocalSignParty, parties []p2p.Party, data, sessionId string, logger *logan.Entry) *SignParty { partyMap := make(map[core.Address]struct{}, len(parties)) partyIds := make([]*tss.PartyID, len(parties)+1) - partyIds[0] = p2p.AddrToPartyIdentifier(self.Address) + partyIds[0] = self.Address.PartyIdentifier() for i, party := range parties { if party.CoreAddress == self.Address { @@ -76,7 +76,7 @@ func (p *SignParty) Run(ctx context.Context) { p.logger.Infof("Running TSS signing on set: %v", p.parties) params := tss.NewParameters( tss.S256(), tss.NewPeerContext(p.sortedPartyIds), - p2p.AddrToPartyIdentifier(p.self.Address), + p.sortedPartyIds.FindByKey(p.self.Address.PartyKey()), len(p.sortedPartyIds), len(p.sortedPartyIds), ) @@ -133,11 +133,11 @@ func (p *SignParty) receiveMsgs(ctx context.Context) { } if _, exists := p.parties[msg.Sender]; !exists { - p.logger.Warn("got message from outside party") + p.logger.WithField("party", msg.Sender).Warn("got message from outside party") continue } - _, err := p.party.UpdateFromBytes(msg.WireMsg, p2p.AddrToPartyIdentifier(msg.Sender), msg.IsBroadcast) + _, err := p.party.UpdateFromBytes(msg.WireMsg, p.sortedPartyIds.FindByKey(msg.Sender.PartyKey()), msg.IsBroadcast) if err != nil { p.logger.WithError(err).Error("failed to update party state") } @@ -191,8 +191,8 @@ func (p *SignParty) receiveUpdates(ctx context.Context, out <-chan tss.Message, continue } - dst, err := p2p.AddrFromPartyIdentifier(destination[0]) - if err != nil { + dst := core.AddrFromPartyId(destination[0]) + if len(dst.String()) == 0 { p.logger.WithError(err).Error("failed to get destination address") continue } From 17da66c67304f97c93eddecf0c21a14ba672ae4c Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 11:39:00 +0200 Subject: [PATCH 04/20] deleted mock session --- internal/tss/session/sign.go | 122 ----------------------------------- 1 file changed, 122 deletions(-) delete mode 100644 internal/tss/session/sign.go diff --git a/internal/tss/session/sign.go b/internal/tss/session/sign.go deleted file mode 100644 index cb0027e..0000000 --- a/internal/tss/session/sign.go +++ /dev/null @@ -1,122 +0,0 @@ -package session - -import ( - "context" - "fmt" - "github.com/bnb-chain/tss-lib/v2/common" - "github.com/hyle-team/tss-svc/internal/core" - "github.com/hyle-team/tss-svc/internal/p2p" - "github.com/hyle-team/tss-svc/internal/tss" - "github.com/pkg/errors" - "gitlab.com/distributed_lab/logan/v3" - "sync" - "time" -) - -type SigningSessionParams struct { - Id string - StartTime time.Time -} - -type SigningSession struct { - params SigningSessionParams - logger *logan.Entry - wg *sync.WaitGroup - - connectedPartiesCount func() int - partiesCount int - - signingParty interface { - Run(ctx context.Context) - WaitFor() *common.SignatureData - Receive(sender core.Address, data *p2p.TssData) - } - - data string - result *common.SignatureData - err error -} - -func NewSigningSession(self tss.LocalSignParty, params SigningSessionParams, logger *logan.Entry, parties []p2p.Party, data string, connectedPartiesCountFunc func() int) *SigningSession { - return &SigningSession{ - params: params, - wg: &sync.WaitGroup{}, - logger: logger, - connectedPartiesCount: connectedPartiesCountFunc, - partiesCount: len(parties), - data: data, - signingParty: tss.NewSignParty(self, parties, data, params.Id, logger), - } -} - -func (s *SigningSession) Run(ctx context.Context) error { - runDelay := time.Until(s.params.StartTime) - if runDelay <= 0 { - return errors.New("target time is in the past") - } - - s.logger.Info(fmt.Sprintf("signing session will start in %s", runDelay)) - - select { - case <-ctx.Done(): - s.logger.Info("signing session cancelled") - return nil - case <-time.After(runDelay): - } - - if s.connectedPartiesCount() != s.partiesCount { - return errors.New("cannot start signing session: not all parties connected") - } - - s.wg.Add(1) - go s.run(ctx) - return nil -} - -func (s *SigningSession) run(ctx context.Context) { - defer s.wg.Done() - - boundedCtx, cancel := context.WithTimeout(ctx, BoundarySigningSession) - defer cancel() - - s.signingParty.Run(boundedCtx) - s.result = s.signingParty.WaitFor() - s.logger.Info("signing session finished") - if s.result != nil { - return - } - - if err := boundedCtx.Err(); err != nil { - s.err = err - } else { - s.err = errors.New("signing session error occurred") - } -} - -func (s *SigningSession) WaitFor() (*common.SignatureData, error) { - s.wg.Wait() - return s.result, s.err -} - -func (s *SigningSession) Id() string { - return s.params.Id -} - -func (s *SigningSession) Receive(request *p2p.SubmitRequest) error { - if request.Type != p2p.RequestType_SIGN { - return errors.New("invalid request type") - } - - var data *p2p.TssData - - if err := request.Data.UnmarshalTo(data); err != nil { - return errors.Wrap(err, "failed to unmarshal TSS request data") - } - - sender, _ := core.AddressFromString(request.Sender) - s.signingParty.Receive(sender, data) - return nil -} - -// RegisterIdChangeListener is a no-op for SigningSession -func (s *SigningSession) RegisterIdChangeListener(func(oldId, newId string)) {} From cbc3aa6f9ad66cc0faadfffdf3db875c8e5b6f05 Mon Sep 17 00:00:00 2001 From: Eduard Mikhrin <48176280+EduardMikhrin@users.noreply.github.com> Date: Thu, 9 Jan 2025 11:40:11 +0200 Subject: [PATCH 05/20] Update boundaries.go --- internal/tss/session/boundaries.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/tss/session/boundaries.go b/internal/tss/session/boundaries.go index 08829ca..4bad5aa 100644 --- a/internal/tss/session/boundaries.go +++ b/internal/tss/session/boundaries.go @@ -4,5 +4,4 @@ import "time" const ( BoundaryKeygenSession = time.Minute - BoundarySigningSession = 10 * time.Second ) From a12e6ce4e8d7861953f39a74f68af68fe095eacb Mon Sep 17 00:00:00 2001 From: Eduard Mikhrin <48176280+EduardMikhrin@users.noreply.github.com> Date: Thu, 9 Jan 2025 11:40:34 +0200 Subject: [PATCH 06/20] Update boundaries.go --- internal/tss/session/boundaries.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/tss/session/boundaries.go b/internal/tss/session/boundaries.go index 4bad5aa..578270a 100644 --- a/internal/tss/session/boundaries.go +++ b/internal/tss/session/boundaries.go @@ -3,5 +3,5 @@ package session import "time" const ( - BoundaryKeygenSession = time.Minute + BoundaryKeygenSession = time.Minute ) From 3c2cfc301494acda7c7ba3b949982c072328868c Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 11:43:25 +0200 Subject: [PATCH 07/20] fix --- internal/tss/signer.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/internal/tss/signer.go b/internal/tss/signer.go index 08e6fbd..0212f20 100644 --- a/internal/tss/signer.go +++ b/internal/tss/signer.go @@ -32,10 +32,7 @@ type SignParty struct { logger *logan.Entry party tss.Party msgs chan partyMsg - broadcaster interface { - Send(msg *p2p.SubmitRequest, to core.Address) error - Broadcast(msg *p2p.SubmitRequest) error - } + broadcaster *p2p.Broadcaster data string threshold int From 8687a154e6c324d12974c4fe779dcb7814ded70a Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 11:45:04 +0200 Subject: [PATCH 08/20] Revert "fix" This reverts commit 405846e86db42f43e8660d93d80c582094e01960. --- internal/tss/signer.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/tss/signer.go b/internal/tss/signer.go index 0212f20..08e6fbd 100644 --- a/internal/tss/signer.go +++ b/internal/tss/signer.go @@ -32,7 +32,10 @@ type SignParty struct { logger *logan.Entry party tss.Party msgs chan partyMsg - broadcaster *p2p.Broadcaster + broadcaster interface { + Send(msg *p2p.SubmitRequest, to core.Address) error + Broadcast(msg *p2p.SubmitRequest) error + } data string threshold int From c297a5aba8de70717814079105ccea7f0284d63d Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 11:46:30 +0200 Subject: [PATCH 09/20] fix --- internal/tss/signer.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/internal/tss/signer.go b/internal/tss/signer.go index 08e6fbd..0212f20 100644 --- a/internal/tss/signer.go +++ b/internal/tss/signer.go @@ -32,10 +32,7 @@ type SignParty struct { logger *logan.Entry party tss.Party msgs chan partyMsg - broadcaster interface { - Send(msg *p2p.SubmitRequest, to core.Address) error - Broadcast(msg *p2p.SubmitRequest) error - } + broadcaster *p2p.Broadcaster data string threshold int From 96b0c83b6987e789d35f62ea9be38fe7c25b1178 Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 12:09:23 +0200 Subject: [PATCH 10/20] signer party fix --- internal/tss/signer.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/tss/signer.go b/internal/tss/signer.go index 0212f20..9684cd4 100644 --- a/internal/tss/signer.go +++ b/internal/tss/signer.go @@ -17,8 +17,9 @@ import ( ) type LocalSignParty struct { - Address core.Address - data *keygen.LocalPartySaveData + Address core.Address + data *keygen.LocalPartySaveData + countThreshold func(int) int } type SignParty struct { @@ -61,7 +62,7 @@ func NewSignParty(self LocalSignParty, parties []p2p.Party, data, sessionId stri sortedPartyIds: tss.SortPartyIDs(partyIds), parties: partyMap, data: data, - threshold: GetThreshold(tss.SortPartyIDs(partyIds).Len()), + threshold: self.countThreshold(len(parties)), msgs: make(chan partyMsg, MsgsCapacity), sessionId: sessionId, logger: logger, @@ -75,7 +76,7 @@ func (p *SignParty) Run(ctx context.Context) { tss.S256(), tss.NewPeerContext(p.sortedPartyIds), p.sortedPartyIds.FindByKey(p.self.Address.PartyKey()), len(p.sortedPartyIds), - len(p.sortedPartyIds), + p.threshold, ) out := make(chan tss.Message, OutChannelSize) end := make(chan *common.SignatureData, EndChannelSize) From 96abac08ad5018725223e975b0ca04143d84e4d8 Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 12:09:30 +0200 Subject: [PATCH 11/20] sign session --- internal/tss/session/sign.go | 122 +++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 internal/tss/session/sign.go diff --git a/internal/tss/session/sign.go b/internal/tss/session/sign.go new file mode 100644 index 0000000..cb0027e --- /dev/null +++ b/internal/tss/session/sign.go @@ -0,0 +1,122 @@ +package session + +import ( + "context" + "fmt" + "github.com/bnb-chain/tss-lib/v2/common" + "github.com/hyle-team/tss-svc/internal/core" + "github.com/hyle-team/tss-svc/internal/p2p" + "github.com/hyle-team/tss-svc/internal/tss" + "github.com/pkg/errors" + "gitlab.com/distributed_lab/logan/v3" + "sync" + "time" +) + +type SigningSessionParams struct { + Id string + StartTime time.Time +} + +type SigningSession struct { + params SigningSessionParams + logger *logan.Entry + wg *sync.WaitGroup + + connectedPartiesCount func() int + partiesCount int + + signingParty interface { + Run(ctx context.Context) + WaitFor() *common.SignatureData + Receive(sender core.Address, data *p2p.TssData) + } + + data string + result *common.SignatureData + err error +} + +func NewSigningSession(self tss.LocalSignParty, params SigningSessionParams, logger *logan.Entry, parties []p2p.Party, data string, connectedPartiesCountFunc func() int) *SigningSession { + return &SigningSession{ + params: params, + wg: &sync.WaitGroup{}, + logger: logger, + connectedPartiesCount: connectedPartiesCountFunc, + partiesCount: len(parties), + data: data, + signingParty: tss.NewSignParty(self, parties, data, params.Id, logger), + } +} + +func (s *SigningSession) Run(ctx context.Context) error { + runDelay := time.Until(s.params.StartTime) + if runDelay <= 0 { + return errors.New("target time is in the past") + } + + s.logger.Info(fmt.Sprintf("signing session will start in %s", runDelay)) + + select { + case <-ctx.Done(): + s.logger.Info("signing session cancelled") + return nil + case <-time.After(runDelay): + } + + if s.connectedPartiesCount() != s.partiesCount { + return errors.New("cannot start signing session: not all parties connected") + } + + s.wg.Add(1) + go s.run(ctx) + return nil +} + +func (s *SigningSession) run(ctx context.Context) { + defer s.wg.Done() + + boundedCtx, cancel := context.WithTimeout(ctx, BoundarySigningSession) + defer cancel() + + s.signingParty.Run(boundedCtx) + s.result = s.signingParty.WaitFor() + s.logger.Info("signing session finished") + if s.result != nil { + return + } + + if err := boundedCtx.Err(); err != nil { + s.err = err + } else { + s.err = errors.New("signing session error occurred") + } +} + +func (s *SigningSession) WaitFor() (*common.SignatureData, error) { + s.wg.Wait() + return s.result, s.err +} + +func (s *SigningSession) Id() string { + return s.params.Id +} + +func (s *SigningSession) Receive(request *p2p.SubmitRequest) error { + if request.Type != p2p.RequestType_SIGN { + return errors.New("invalid request type") + } + + var data *p2p.TssData + + if err := request.Data.UnmarshalTo(data); err != nil { + return errors.Wrap(err, "failed to unmarshal TSS request data") + } + + sender, _ := core.AddressFromString(request.Sender) + s.signingParty.Receive(sender, data) + return nil +} + +// RegisterIdChangeListener is a no-op for SigningSession +func (s *SigningSession) RegisterIdChangeListener(func(oldId, newId string)) {} From 27db3753b468a0da6f9dcb789da0377dfb9e71f5 Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 13:01:33 +0200 Subject: [PATCH 12/20] added signing service cmd --- cmd/service/run/run.go | 1 + cmd/service/run/sign.go | 100 +++++++++++++++++++++++++++++ internal/tss/config/config.go | 15 ++++- internal/tss/session/boundaries.go | 3 +- internal/tss/signer.go | 14 ++-- 5 files changed, 123 insertions(+), 10 deletions(-) create mode 100644 cmd/service/run/sign.go diff --git a/cmd/service/run/run.go b/cmd/service/run/run.go index 8715fcd..85e7c96 100644 --- a/cmd/service/run/run.go +++ b/cmd/service/run/run.go @@ -15,4 +15,5 @@ var Cmd = &cobra.Command{ func registerCommands(cmd *cobra.Command) { cmd.AddCommand(keygenCmd) + cmd.AddCommand(signCmd) } diff --git a/cmd/service/run/sign.go b/cmd/service/run/sign.go new file mode 100644 index 0000000..b2d8caf --- /dev/null +++ b/cmd/service/run/sign.go @@ -0,0 +1,100 @@ +package run + +import ( + "context" + "github.com/bnb-chain/tss-lib/v2/ecdsa/keygen" + tsslib "github.com/bnb-chain/tss-lib/v2/tss" + "github.com/hyle-team/tss-svc/cmd/utils" + "github.com/hyle-team/tss-svc/internal/p2p" + "github.com/hyle-team/tss-svc/internal/secrets/vault" + "github.com/hyle-team/tss-svc/internal/tss" + "github.com/hyle-team/tss-svc/internal/tss/session" + "github.com/pkg/errors" + "github.com/spf13/cobra" + "golang.org/x/sync/errgroup" + "os/signal" + "strconv" + "syscall" +) + +var signCmd = &cobra.Command{ + Use: "sign [data-string] [threshold]", + Args: cobra.ExactArgs(2), + PreRunE: func(cmd *cobra.Command, args []string) error { + if !utils.OutputValid() { + return errors.New("invalid output type") + } + return nil + }, + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := utils.ConfigFromFlags(cmd) + if err != nil { + return errors.Wrap(err, "failed to read config from flags") + } + + dataToSign := args[0] + arg2 := args[1] + threshoold, err := strconv.Atoi(arg2) + if err != nil { + return errors.Wrap(err, "invalid threshold") + } + + storage := vault.NewStorage(cfg.VaultClient()) + + // Configuring local data for LocalSignParty + localData := keygen.NewLocalPartySaveData(len(cfg.Parties())) + var partyIds []*tsslib.PartyID + for _, party := range cfg.Parties() { + partyIds = append(partyIds, party.Identifier()) + } + localSaveData := keygen.BuildLocalSaveDataSubset(localData, tsslib.SortPartyIDs(partyIds)) + account, err := storage.GetCoreAccount() + if err != nil { + return errors.Wrap(err, "failed to get core account") + } + + errGroup := new(errgroup.Group) + ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT) + defer cancel() + + connectionManager := p2p.NewConnectionManager(cfg.Parties(), p2p.PartyStatus_SIGNING, cfg.Log().WithField("component", "connection_manager")) + + session := session.NewSigningSession( + tss.LocalSignParty{ + Address: account.CosmosAddress(), + Data: &localSaveData, + Threshold: threshoold, + }, + cfg.TSSParams().SigningSessionParams(), + cfg.Log().WithField("component", "signing_session"), + cfg.Parties(), + dataToSign, + connectionManager.GetReadyCount, + ) + + sessionManager := p2p.NewSessionManager(session) + errGroup.Go(func() error { + server := p2p.NewServer(cfg.GRPCListener(), sessionManager) + server.SetStatus(p2p.PartyStatus_SIGNING) + return server.Run(ctx) + }) + + errGroup.Go(func() error { + defer cancel() + + if err := session.Run(ctx); err != nil { + return errors.Wrap(err, "failed to run signing session") + } + result, err := session.WaitFor() + if err != nil { + return errors.Wrap(err, "failed to obtain signing session result") + } + + cfg.Log().Info("signing session successfully completed. Signature: ", result.String()) + + return nil + }) + + return errGroup.Wait() + }, +} diff --git a/internal/tss/config/config.go b/internal/tss/config/config.go index e965f69..6c1d74a 100644 --- a/internal/tss/config/config.go +++ b/internal/tss/config/config.go @@ -18,7 +18,8 @@ type ParamsConfigurator interface { } type Params struct { - Keygen KeygenParams `fig:"keygen"` + Keygen KeygenParams `fig:"keygen"` + Signing SigningParams `fig:"signing"` } func (p Params) KeygenSessionParams() session.KeygenSessionParams { @@ -28,11 +29,23 @@ func (p Params) KeygenSessionParams() session.KeygenSessionParams { } } +func (p Params) SigningSessionParams() session.SigningSessionParams { + return session.SigningSessionParams{ + Id: p.Signing.Id, + StartTime: p.Signing.StartTime, + } +} + type KeygenParams struct { Id string `fig:"session_id,required"` StartTime time.Time `fig:"start_time,required"` } +type SigningParams struct { + Id string `fig:"session_id,required"` + StartTime time.Time `fig:"start_time,required"` +} + type tssParamsConfigurator struct { getter kv.Getter once comfig.Once diff --git a/internal/tss/session/boundaries.go b/internal/tss/session/boundaries.go index 578270a..08829ca 100644 --- a/internal/tss/session/boundaries.go +++ b/internal/tss/session/boundaries.go @@ -3,5 +3,6 @@ package session import "time" const ( - BoundaryKeygenSession = time.Minute + BoundaryKeygenSession = time.Minute + BoundarySigningSession = 10 * time.Second ) diff --git a/internal/tss/signer.go b/internal/tss/signer.go index 9684cd4..9260450 100644 --- a/internal/tss/signer.go +++ b/internal/tss/signer.go @@ -17,9 +17,9 @@ import ( ) type LocalSignParty struct { - Address core.Address - data *keygen.LocalPartySaveData - countThreshold func(int) int + Address core.Address + Data *keygen.LocalPartySaveData + Threshold int } type SignParty struct { @@ -35,8 +35,7 @@ type SignParty struct { msgs chan partyMsg broadcaster *p2p.Broadcaster - data string - threshold int + data string ended atomic.Bool result *common.SignatureData @@ -62,7 +61,6 @@ func NewSignParty(self LocalSignParty, parties []p2p.Party, data, sessionId stri sortedPartyIds: tss.SortPartyIDs(partyIds), parties: partyMap, data: data, - threshold: self.countThreshold(len(parties)), msgs: make(chan partyMsg, MsgsCapacity), sessionId: sessionId, logger: logger, @@ -76,12 +74,12 @@ func (p *SignParty) Run(ctx context.Context) { tss.S256(), tss.NewPeerContext(p.sortedPartyIds), p.sortedPartyIds.FindByKey(p.self.Address.PartyKey()), len(p.sortedPartyIds), - p.threshold, + p.self.Threshold, ) out := make(chan tss.Message, OutChannelSize) end := make(chan *common.SignatureData, EndChannelSize) - p.party = signing.NewLocalParty(new(big.Int).SetBytes(hexutil.MustDecode(p.data)), params, *p.self.data, out, end) + p.party = signing.NewLocalParty(new(big.Int).SetBytes(hexutil.MustDecode(p.data)), params, *p.self.Data, out, end) p.wg.Add(3) From bc9668e39bd0e4068e60eb7fc078a654ba7f6172 Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 13:03:33 +0200 Subject: [PATCH 13/20] add data validation --- cmd/service/run/sign.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cmd/service/run/sign.go b/cmd/service/run/sign.go index b2d8caf..96ea0e9 100644 --- a/cmd/service/run/sign.go +++ b/cmd/service/run/sign.go @@ -33,8 +33,11 @@ var signCmd = &cobra.Command{ } dataToSign := args[0] + if len(dataToSign) == 0 { + return errors.Wrap(errors.New("empty data to-sign"), "invalid data") + } arg2 := args[1] - threshoold, err := strconv.Atoi(arg2) + threshold, err := strconv.Atoi(arg2) if err != nil { return errors.Wrap(err, "invalid threshold") } @@ -63,7 +66,7 @@ var signCmd = &cobra.Command{ tss.LocalSignParty{ Address: account.CosmosAddress(), Data: &localSaveData, - Threshold: threshoold, + Threshold: threshold, }, cfg.TSSParams().SigningSessionParams(), cfg.Log().WithField("component", "signing_session"), From 152c90350364883bf7409962225095077b2a5fa7 Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 14:26:31 +0200 Subject: [PATCH 14/20] sign session config update --- internal/config/config.yaml | 4 ++++ internal/tss/config/config.go | 2 ++ internal/tss/session/sign.go | 1 + 3 files changed, 7 insertions(+) diff --git a/internal/config/config.yaml b/internal/config/config.yaml index 72c105b..7fd3fe5 100644 --- a/internal/config/config.yaml +++ b/internal/config/config.yaml @@ -24,4 +24,8 @@ tss: keygen: start_time: "2025-01-08 00:21:20" session_id: abcd + signing: + start_time: "2025-01-08 00:21:20" + session_id: abcd + threshold: 1 diff --git a/internal/tss/config/config.go b/internal/tss/config/config.go index 6c1d74a..45e1fd9 100644 --- a/internal/tss/config/config.go +++ b/internal/tss/config/config.go @@ -33,6 +33,7 @@ func (p Params) SigningSessionParams() session.SigningSessionParams { return session.SigningSessionParams{ Id: p.Signing.Id, StartTime: p.Signing.StartTime, + Threshold: p.Signing.Threshold, } } @@ -44,6 +45,7 @@ type KeygenParams struct { type SigningParams struct { Id string `fig:"session_id,required"` StartTime time.Time `fig:"start_time,required"` + Threshold int `fig:"threshold,required"` } type tssParamsConfigurator struct { diff --git a/internal/tss/session/sign.go b/internal/tss/session/sign.go index cb0027e..665ed9d 100644 --- a/internal/tss/session/sign.go +++ b/internal/tss/session/sign.go @@ -16,6 +16,7 @@ import ( type SigningSessionParams struct { Id string StartTime time.Time + Threshold int } type SigningSession struct { From c2d9f97ad1c2a37e27610bc04d4f9169862f17a3 Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 14:26:50 +0200 Subject: [PATCH 15/20] storage update --- internal/secrets/storage.go | 1 + internal/secrets/vault/vault.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/internal/secrets/storage.go b/internal/secrets/storage.go index 9f659d5..345eca1 100644 --- a/internal/secrets/storage.go +++ b/internal/secrets/storage.go @@ -13,4 +13,5 @@ type Storage interface { SaveCoreAccount(account *core.Account) error SaveTssShare(data *keygen.LocalPartySaveData) error + GetTssShare() (*keygen.LocalPartySaveData, error) } diff --git a/internal/secrets/vault/vault.go b/internal/secrets/vault/vault.go index 826795b..ce1016a 100644 --- a/internal/secrets/vault/vault.go +++ b/internal/secrets/vault/vault.go @@ -113,3 +113,20 @@ func (s *Storage) SaveCoreAccount(account *core.Account) error { "value": hexutil.Encode(account.PrivateKey().Bytes()), }) } + +func (s *Storage) GetTssShare() (*keygen.LocalPartySaveData, error) { + kvData, err := s.load(keyTssShare) + if err != nil { + return nil, errors.Wrap(err, "failed to load share") + } + val, ok := kvData["value"].(string) + if !ok { + return nil, errors.New("share value not found") + } + data := new(keygen.LocalPartySaveData) + err = json.Unmarshal([]byte(val), data) + if err != nil { + return nil, errors.Wrap(err, "failed to decode share data") + } + return data, nil +} From 6cce867575797f086c0ff6bc1af251638e52af93 Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 14:31:11 +0200 Subject: [PATCH 16/20] cmd update --- cmd/service/run/run.go | 1 - cmd/service/service.go | 1 + cmd/service/{run => }/sign.go | 31 ++++++++++--------------------- internal/tss/session/sign.go | 20 ++++++++++---------- 4 files changed, 21 insertions(+), 32 deletions(-) rename cmd/service/{run => }/sign.go (75%) diff --git a/cmd/service/run/run.go b/cmd/service/run/run.go index 85e7c96..8715fcd 100644 --- a/cmd/service/run/run.go +++ b/cmd/service/run/run.go @@ -15,5 +15,4 @@ var Cmd = &cobra.Command{ func registerCommands(cmd *cobra.Command) { cmd.AddCommand(keygenCmd) - cmd.AddCommand(signCmd) } diff --git a/cmd/service/service.go b/cmd/service/service.go index 29fce01..c1838ee 100644 --- a/cmd/service/service.go +++ b/cmd/service/service.go @@ -15,6 +15,7 @@ func init() { func registerServiceCommands(cmd *cobra.Command) { cmd.AddCommand(migrate.Cmd) cmd.AddCommand(run.Cmd) + cmd.AddCommand(signCmd) } var Cmd = &cobra.Command{ diff --git a/cmd/service/run/sign.go b/cmd/service/sign.go similarity index 75% rename from cmd/service/run/sign.go rename to cmd/service/sign.go index 96ea0e9..1f42f22 100644 --- a/cmd/service/run/sign.go +++ b/cmd/service/sign.go @@ -1,9 +1,7 @@ -package run +package service import ( "context" - "github.com/bnb-chain/tss-lib/v2/ecdsa/keygen" - tsslib "github.com/bnb-chain/tss-lib/v2/tss" "github.com/hyle-team/tss-svc/cmd/utils" "github.com/hyle-team/tss-svc/internal/p2p" "github.com/hyle-team/tss-svc/internal/secrets/vault" @@ -13,13 +11,12 @@ import ( "github.com/spf13/cobra" "golang.org/x/sync/errgroup" "os/signal" - "strconv" "syscall" ) var signCmd = &cobra.Command{ - Use: "sign [data-string] [threshold]", - Args: cobra.ExactArgs(2), + Use: "sign [data-string]", + Args: cobra.ExactArgs(1), PreRunE: func(cmd *cobra.Command, args []string) error { if !utils.OutputValid() { return errors.New("invalid output type") @@ -36,25 +33,17 @@ var signCmd = &cobra.Command{ if len(dataToSign) == 0 { return errors.Wrap(errors.New("empty data to-sign"), "invalid data") } - arg2 := args[1] - threshold, err := strconv.Atoi(arg2) - if err != nil { - return errors.Wrap(err, "invalid threshold") - } storage := vault.NewStorage(cfg.VaultClient()) - // Configuring local data for LocalSignParty - localData := keygen.NewLocalPartySaveData(len(cfg.Parties())) - var partyIds []*tsslib.PartyID - for _, party := range cfg.Parties() { - partyIds = append(partyIds, party.Identifier()) - } - localSaveData := keygen.BuildLocalSaveDataSubset(localData, tsslib.SortPartyIDs(partyIds)) account, err := storage.GetCoreAccount() if err != nil { return errors.Wrap(err, "failed to get core account") } + localSaveData, err := storage.GetTssShare() + if err != nil { + return errors.Wrap(err, "failed to get local share") + } errGroup := new(errgroup.Group) ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT) @@ -62,11 +51,11 @@ var signCmd = &cobra.Command{ connectionManager := p2p.NewConnectionManager(cfg.Parties(), p2p.PartyStatus_SIGNING, cfg.Log().WithField("component", "connection_manager")) - session := session.NewSigningSession( + session := session.NewDefaultSigningSession( tss.LocalSignParty{ Address: account.CosmosAddress(), - Data: &localSaveData, - Threshold: threshold, + Data: localSaveData, + Threshold: cfg.TSSParams().SigningSessionParams().Threshold, }, cfg.TSSParams().SigningSessionParams(), cfg.Log().WithField("component", "signing_session"), diff --git a/internal/tss/session/sign.go b/internal/tss/session/sign.go index 665ed9d..a85441c 100644 --- a/internal/tss/session/sign.go +++ b/internal/tss/session/sign.go @@ -19,7 +19,7 @@ type SigningSessionParams struct { Threshold int } -type SigningSession struct { +type DefaultSigningSession struct { params SigningSessionParams logger *logan.Entry wg *sync.WaitGroup @@ -38,8 +38,8 @@ type SigningSession struct { err error } -func NewSigningSession(self tss.LocalSignParty, params SigningSessionParams, logger *logan.Entry, parties []p2p.Party, data string, connectedPartiesCountFunc func() int) *SigningSession { - return &SigningSession{ +func NewDefaultSigningSession(self tss.LocalSignParty, params SigningSessionParams, logger *logan.Entry, parties []p2p.Party, data string, connectedPartiesCountFunc func() int) *DefaultSigningSession { + return &DefaultSigningSession{ params: params, wg: &sync.WaitGroup{}, logger: logger, @@ -50,7 +50,7 @@ func NewSigningSession(self tss.LocalSignParty, params SigningSessionParams, log } } -func (s *SigningSession) Run(ctx context.Context) error { +func (s *DefaultSigningSession) Run(ctx context.Context) error { runDelay := time.Until(s.params.StartTime) if runDelay <= 0 { return errors.New("target time is in the past") @@ -74,7 +74,7 @@ func (s *SigningSession) Run(ctx context.Context) error { return nil } -func (s *SigningSession) run(ctx context.Context) { +func (s *DefaultSigningSession) run(ctx context.Context) { defer s.wg.Done() boundedCtx, cancel := context.WithTimeout(ctx, BoundarySigningSession) @@ -94,16 +94,16 @@ func (s *SigningSession) run(ctx context.Context) { } } -func (s *SigningSession) WaitFor() (*common.SignatureData, error) { +func (s *DefaultSigningSession) WaitFor() (*common.SignatureData, error) { s.wg.Wait() return s.result, s.err } -func (s *SigningSession) Id() string { +func (s *DefaultSigningSession) Id() string { return s.params.Id } -func (s *SigningSession) Receive(request *p2p.SubmitRequest) error { +func (s *DefaultSigningSession) Receive(request *p2p.SubmitRequest) error { if request.Type != p2p.RequestType_SIGN { return errors.New("invalid request type") } @@ -119,5 +119,5 @@ func (s *SigningSession) Receive(request *p2p.SubmitRequest) error { return nil } -// RegisterIdChangeListener is a no-op for SigningSession -func (s *SigningSession) RegisterIdChangeListener(func(oldId, newId string)) {} +// RegisterIdChangeListener is a no-op for DefaultSigningSession +func (s *DefaultSigningSession) RegisterIdChangeListener(func(oldId, newId string)) {} From 7c31a38b8612ba73b5d1f5895e3db218426954c4 Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 14:38:41 +0200 Subject: [PATCH 17/20] cmd update --- cmd/service/sign.go | 29 +++++++++++++++++++++++++++++ internal/tss/utils.go | 5 ----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/cmd/service/sign.go b/cmd/service/sign.go index 1f42f22..6bd7717 100644 --- a/cmd/service/sign.go +++ b/cmd/service/sign.go @@ -2,6 +2,9 @@ package service import ( "context" + "encoding/json" + "fmt" + "github.com/bnb-chain/tss-lib/v2/common" "github.com/hyle-team/tss-svc/cmd/utils" "github.com/hyle-team/tss-svc/internal/p2p" "github.com/hyle-team/tss-svc/internal/secrets/vault" @@ -10,10 +13,15 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" "golang.org/x/sync/errgroup" + "os" "os/signal" "syscall" ) +func init() { + utils.RegisterOutputFlags(signCmd) +} + var signCmd = &cobra.Command{ Use: "sign [data-string]", Args: cobra.ExactArgs(1), @@ -84,9 +92,30 @@ var signCmd = &cobra.Command{ cfg.Log().Info("signing session successfully completed. Signature: ", result.String()) + err = saveSigningResult(result) + if err != nil { + return errors.Wrap(err, "failed to save signing result") + } return nil }) return errGroup.Wait() }, } + +func saveSigningResult(result *common.SignatureData) error { + raw, err := json.Marshal(result) + if err != nil { + return errors.Wrap(err, "failed to marshal signing result") + } + + switch utils.OutputType { + case "console": + fmt.Println(string(raw)) + case "file": + if err = os.WriteFile(utils.FilePath, raw, 0644); err != nil { + return errors.Wrap(err, "failed to write signing result to file") + } + } + return nil +} diff --git a/internal/tss/utils.go b/internal/tss/utils.go index cd6594a..80f5d9a 100644 --- a/internal/tss/utils.go +++ b/internal/tss/utils.go @@ -15,8 +15,3 @@ type partyMsg struct { WireMsg []byte IsBroadcast bool } - -func GetThreshold(n int) int { - var res = float32(n) * 2 / 3 - return int(res) -} From e7296a1241a9f61fb9086eac58cae73afe2fa699 Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 14:40:38 +0200 Subject: [PATCH 18/20] error fix --- internal/secrets/vault/vault.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/secrets/vault/vault.go b/internal/secrets/vault/vault.go index ce1016a..f87a673 100644 --- a/internal/secrets/vault/vault.go +++ b/internal/secrets/vault/vault.go @@ -121,7 +121,7 @@ func (s *Storage) GetTssShare() (*keygen.LocalPartySaveData, error) { } val, ok := kvData["value"].(string) if !ok { - return nil, errors.New("share value not found") + return nil, errors.Wrap(errors.New("no value"), "share value not found") } data := new(keygen.LocalPartySaveData) err = json.Unmarshal([]byte(val), data) From 405141750eeac3c2cbe95a03553cb667ca82fdb5 Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 14:42:08 +0200 Subject: [PATCH 19/20] config update --- build/configs/tss1.yaml | 5 +++++ build/configs/tss2.yaml | 4 ++++ build/configs/tss3.yaml | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/build/configs/tss1.yaml b/build/configs/tss1.yaml index 70dd60d..f1a6c33 100644 --- a/build/configs/tss1.yaml +++ b/build/configs/tss1.yaml @@ -22,4 +22,9 @@ tss: keygen: start_time: "2025-01-08 17:51:38" session_id: abcd + signing: + start_time: "2025-01-08 00:21:20" + session_id: abcd + threshold: 1 + diff --git a/build/configs/tss2.yaml b/build/configs/tss2.yaml index 0aefa48..1da9116 100644 --- a/build/configs/tss2.yaml +++ b/build/configs/tss2.yaml @@ -22,4 +22,8 @@ tss: keygen: start_time: "2025-01-08 17:51:38" session_id: abcd + signing: + start_time: "2025-01-08 00:21:20" + session_id: abcd + threshold: 1 diff --git a/build/configs/tss3.yaml b/build/configs/tss3.yaml index 5186c31..f6d0a28 100644 --- a/build/configs/tss3.yaml +++ b/build/configs/tss3.yaml @@ -22,4 +22,9 @@ tss: keygen: start_time: "2025-01-08 17:51:38" session_id: abcd + signing: + start_time: "2025-01-08 00:21:20" + session_id: abcd + threshold: 1 + From 13c4f9a3b6ba2467826b5fa72d688ce30e21e6d8 Mon Sep 17 00:00:00 2001 From: DrLivsey00 Date: Thu, 9 Jan 2025 19:09:47 +0200 Subject: [PATCH 20/20] signer update --- build/configs/tss1.yaml | 6 +++--- build/configs/tss2.yaml | 6 +++--- build/configs/tss3.yaml | 6 +++--- build/scripts/vault-init.sh | 19 ++++++++++++++++++- cmd/service/sign.go | 34 +++++++++++++++++++++++++++++----- cmd/utils/flags.go | 2 ++ internal/tss/session/sign.go | 16 +++++++++++----- internal/tss/signer.go | 12 +++++------- 8 files changed, 74 insertions(+), 27 deletions(-) diff --git a/build/configs/tss1.yaml b/build/configs/tss1.yaml index f1a6c33..fe37bd7 100644 --- a/build/configs/tss1.yaml +++ b/build/configs/tss1.yaml @@ -20,11 +20,11 @@ parties: tss: keygen: - start_time: "2025-01-08 17:51:38" + start_time: "2025-01-09 17:06:05" session_id: abcd signing: - start_time: "2025-01-08 00:21:20" + start_time: "2025-01-09 17:06:05" session_id: abcd - threshold: 1 + threshold: 2 diff --git a/build/configs/tss2.yaml b/build/configs/tss2.yaml index 1da9116..a8d19bf 100644 --- a/build/configs/tss2.yaml +++ b/build/configs/tss2.yaml @@ -20,10 +20,10 @@ parties: tss: keygen: - start_time: "2025-01-08 17:51:38" + start_time: "2025-01-09 17:06:05" session_id: abcd signing: - start_time: "2025-01-08 00:21:20" + start_time: "2025-01-09 17:06:05" session_id: abcd - threshold: 1 + threshold: 2 diff --git a/build/configs/tss3.yaml b/build/configs/tss3.yaml index f6d0a28..a1534a8 100644 --- a/build/configs/tss3.yaml +++ b/build/configs/tss3.yaml @@ -20,11 +20,11 @@ parties: tss: keygen: - start_time: "2025-01-08 17:51:38" + start_time: "2025-01-09 17:06:05" session_id: abcd signing: - start_time: "2025-01-08 00:21:20" + start_time: "2025-01-09 17:06:05" session_id: abcd - threshold: 1 + threshold: 2 diff --git a/build/scripts/vault-init.sh b/build/scripts/vault-init.sh index f981add..c202822 100755 --- a/build/scripts/vault-init.sh +++ b/build/scripts/vault-init.sh @@ -55,6 +55,12 @@ function init_tss { --request POST \ --data '{"data": {"value": "0xb5164b13d2a350f7c9263cb173211ada16c20883d70c449c541dc67ea9e94808"}}' \ $VAULT_ADDR/v1/tss1/data/core_account + curl \ + --header "X-Vault-Token: $VAULT_TOKEN" \ + --header "Content-Type: application/json" \ + --request POST \ + --data '{"data": {"value": "{\"PaillierSK\":{\"N\":25222539396214102132947645117922233593416310527922261576767907607209454161148886976687740431574376057717301376863337351866485660948587280403504333047565362634864942355235574974370280655547038889388126778626768745495789937343074376983433409811121185054635688712486168781113695875463482736079503428210169676308334530833939254903788354387280861868113597976139498558860029324728905034236387595729540429364624198142499381828274695128146897507978696565574138170172578682300653201107201844796079327428944058588394275048046535588206168347669132263451197928497591448865056897551369647572951678065293218262773535109517570063301,\"LambdaN\":12611269698107051066473822558961116796708155263961130788383953803604727080574443488343870215787188028858650688431668675933242830474293640201752166523782681317432471177617787487185140327773519444694063389313384372747894968671537188491716704905560592527317844356243084390556847937731741368039751714105084838154008241261454243447796086532479708781524180105490443551208392989750922769260216801420150334439469670438495082035278890685946845471301238500134216100126221853035754326896640128858144179658447569143559718996548863169793669510290518823845159656779366212898942225407056599595021864653058029691480332290256617987206,\"PhiN\":25222539396214102132947645117922233593416310527922261576767907607209454161148886976687740431574376057717301376863337351866485660948587280403504333047565362634864942355235574974370280655547038889388126778626768745495789937343074376983433409811121185054635688712486168781113695875463482736079503428210169676308016482522908486895592173064959417563048360210980887102416785979501845538520433602840300668878939340876990164070557781371893690942602477000268432200252443706071508653793280257716288359316895138287119437993097726339587339020581037647690319313558732425797884450814113199190043729306116059382960664580513235974412,\"P\":150891342892008580658638413715900709146437502755551199099303933791405935401292001882229020130305955511604989879569608191366019320577890618419676337008832733544995506129790473210447006147392030087682387887045119857095508770897363727579142004626039971697874459115593873160254895505626318195353306498612247624027,\"Q\":167156968138759427537542908605543595918800262403060257343939411435653560314661991007010740355378901753904227878147305564887187244798328946886029632911302242684149041184131113869343961964656890213592449167903689391523320556190730888181736610312819051369297987621662575222653053253550840684459564030392086464863},\"NTildei\":27522048145569981576385476070657020085925130726339017105732055245635131139617848779486234236089392739447177029230176776337417792840985410026305811898446122370628457991480751841368787992944674212855827136776106587708211623816096633385470508783942777769060242860553024607789618299480333621903785949269589743855899844781732294719464609693176227125635396506961291206642890240986520873279868204630398583955556159431730110625063162375117211307543187824440584752056647328233594782678897603405275933516376239252164516303232640795307757685973412817046088426802943281427373371681705483044019813362572029248015450975003456793397,\"H1i\":16517568479471580541042167147429887624339112445131695731362812965113271556999931499818506920295872359339618541473632721715590651347747155351693852424406909063273800991740936578905390940693930284712570022696272615200628747778340495713818589656470906148279108801537540281635961498936452198579197181393579833588271971375793237362898722119231386278352366524087073998056530952433549592269901646408479257883589277185145047553119070727427132500779070094700805261739909572847069577770555320557647528931722700347780859584941003633045209132957117021571486230238188597577097341756253345984078902464163897730250825619109443585525,\"H2i\":7009260890983307674042645873443598272360402291874184297628611268334644375502827051885602971774872550746932684397961239936661508751942327338374679890949524040617402859819754169960008995120441085537975312375011245160932483701011029053912892591633122502853519566790980511469316278839843627242914384033499665339269445033918946320801864810354356251335317386995536622835031324684861929125151890912785727964237447305235539333445504406193442566504156368203203389831716945872865325564528075805169478948659002126911172399429651055487198066475747305990261540384572530954665466045817671514939564054758032645426132833343419966435,\"Alpha\":24178785770298028035270144330521408366177012635516436515514285252609396045105742475132287920923959769687003596766445981073304522433616678002914754773901159001959974463126754423168234341647006144447075641205958141510051562440318832015614559651479520439016757062424577016523463556358066984455332402542345737206229521049493030266829368301746291976943609907055663691491379446563506102162819260353662751063316947194844335784011075059060057648958443191187527894586904520166853889241855199258872026464408485167407295789781647537311358449094940565206190187170103010146898769869153064820412597499183419498527611731140719760318,\"Beta\":1842236524392388004413363649589051135196344297525753833111438010897855410413093181110060727050440600134426211257380627083106678575071413056481657819043028456593757372179569411657666059089225339012202270194986807477435313527136786984376286348859309168239934410838983744773135963549032907967788659225286388093366714461120892086372702989229055826340780102622996079564894334810433574750995664565888225965673964231388730758770331294391500369940729992900609687626347573648112223246540777412969086148539364150238568423017889941252090386268997719310286650600055045520206618383817069645283380471540071618976006086973708783900,\"P\":89076859888329374428834448588875640295464889240139908584166848848489196343609305601818299469095943276512925872046409301814023225882204094799930979950034641671995237955900334121850010220364562220842812167600471148617631238103135231350978395374957884038881516324468027356030819827399246231780905231361459267861,\"Q\":77242417896389752863039920509045963779281716385916611266925798935629449831296516092513287832699841804792146069339631304248414077952739217103112238234257869899831607632499366369552994890774625972620602921438965937336758013127800902501035950942928735490400036839512660670795516140323670591064947868425817928719,\"Xi\":112867830883898034226972729932053902987415856443837369038061268210497562961743,\"ShareID\":625664809357144621199213330413524971235421323270,\"Ks\":[625664809357144621199213330413524971235421323270,766418774313531105623098104317968735865501209001,916059140686050998083978944447064389845990912875],\"NTildej\":[27522048145569981576385476070657020085925130726339017105732055245635131139617848779486234236089392739447177029230176776337417792840985410026305811898446122370628457991480751841368787992944674212855827136776106587708211623816096633385470508783942777769060242860553024607789618299480333621903785949269589743855899844781732294719464609693176227125635396506961291206642890240986520873279868204630398583955556159431730110625063162375117211307543187824440584752056647328233594782678897603405275933516376239252164516303232640795307757685973412817046088426802943281427373371681705483044019813362572029248015450975003456793397,26287095469882602259502960253155254360804319664057270189400555438054357337039614490548038307454671193615535895363160165395716478102599420425673849627176549512756025903516156113017789809445854157445538189888630423465309197773535791949509398585263047625717743386222018827777580573693980777100354534528660867076047014926437366102709599305753278778933593961568721564772996025784590013410301807035352087426157394490518039676044525664902377430194383257189430440590876636954585590235337308078794409472039895617243643395811418712000686126844715347501087676349248362549061632812724867306834763198717465818908662935792809313461,23480415588435419031447079673342707638584619157635925492892092700956060545054581211317334723150904861647190443363724915913333634731042690202334969095297558918226065644052149284231228609885061347987303336592232342077541836117562828680125795146950913810227400817925439088111347951718821237533712931454939571041465013966764098136630139098758185943869375547993588616403453940445655380460537844392425662492015903802136973858968487213723069854403978084196943365237107277679727406895299211293901166051965782637942434024652916212091234932440173565058930379352547491346302287905973361117574863852150429835457489028946773862121],\"H1j\":[16517568479471580541042167147429887624339112445131695731362812965113271556999931499818506920295872359339618541473632721715590651347747155351693852424406909063273800991740936578905390940693930284712570022696272615200628747778340495713818589656470906148279108801537540281635961498936452198579197181393579833588271971375793237362898722119231386278352366524087073998056530952433549592269901646408479257883589277185145047553119070727427132500779070094700805261739909572847069577770555320557647528931722700347780859584941003633045209132957117021571486230238188597577097341756253345984078902464163897730250825619109443585525,7237296850102268809994391546422806771347421159582956353244469392173556245485887457113471982438691859952586948054041931864327489311127741289168937238696227543427161114005626119761194204840161333289011028101857213468065152804524954246351365273946276689599882908377802458781536966578763817065593945744764572690228246297840801885643084377214499585041076814427271051646943709207624302771267224258220858975367390613934384286047461793500454991574228495571945567704822616857994598586850247654535280319590421886683662233275827643945568263492469168667896177789512153268054577266692088605275176858300251672907083732758224300605,5244625320890758926823236807577436724284838598713053894859482410751817243781879176481630696209928554813580388949933383265760121927355967924125199634578244258322548509081654208382874250887126067143103657480563891927357049788066150009272405448366743698886019402037370394519945482107092906983297562458511070353876474628171257852523538903349915748196522993465048339465501002966487352363881955493353463137731082045334297957366902605993954637756964170953811640782160016128220626751514742583329029928572309497541088770781393935677741525856476779288748073006775582646889485680084355364188574749761390046121904185116946045566],\"H2j\":[7009260890983307674042645873443598272360402291874184297628611268334644375502827051885602971774872550746932684397961239936661508751942327338374679890949524040617402859819754169960008995120441085537975312375011245160932483701011029053912892591633122502853519566790980511469316278839843627242914384033499665339269445033918946320801864810354356251335317386995536622835031324684861929125151890912785727964237447305235539333445504406193442566504156368203203389831716945872865325564528075805169478948659002126911172399429651055487198066475747305990261540384572530954665466045817671514939564054758032645426132833343419966435,18466749817946044150553924616420387687206097203280195091736108793324688078484955636193932153729736892592854146381201326144829913584998765092920657820383019994011038791187893733125656289003473873902534439014371112885776632378339360078278137714330073530807181523557193669920350571316763823451053180616837192996398055144521752057518902938931133591279480112693018857707713794810315766346454595609625905047554072392064316910915940533310037364560325306800203727643345507302326279358384562465006827568506035523943913412901074793452573117887015063684946946486775965102910635992776743190835788384535879618002669142951088681822,14594346189095736747225884320665122466781709130296311799936140821845005707361969481771237336943241045846198194729530707984812596096197073779339845516025127003079087887760214159407509566512887375945758017191971067305459350608195289978927459042719267798647279892469529011521861358583760223133316545691872222500422769804045468217338943573175507344490987948897073607894657847841365159409323996140440196447829619732543872240361901392265668680831787448636887349391368628269910852040117529969597030556767165164122605145506454531225988854626670635330962899644382358608473267918441558783891780673214920504774987530770981704584],\"BigXj\":[{\"Curve\":\"secp256k1\",\"Coords\":[79619008090826827417533415687559692527871152001603267383531655235634106782332,41355629595052273785150599597686768188079009448260639000634144582720181845349]},{\"Curve\":\"secp256k1\",\"Coords\":[21867636963708472172322265900235651323653488750672401605779021705975274068736,84738007586914296108324220152131620334372233882254099554454274458534137664767]},{\"Curve\":\"secp256k1\",\"Coords\":[21158565506482058018760924074984840299389628553494973790822294949512295764682,107113721497980556594054348456479615159111507110184031956614891017419549482879]}],\"PaillierPKs\":[{\"N\":25222539396214102132947645117922233593416310527922261576767907607209454161148886976687740431574376057717301376863337351866485660948587280403504333047565362634864942355235574974370280655547038889388126778626768745495789937343074376983433409811121185054635688712486168781113695875463482736079503428210169676308334530833939254903788354387280861868113597976139498558860029324728905034236387595729540429364624198142499381828274695128146897507978696565574138170172578682300653201107201844796079327428944058588394275048046535588206168347669132263451197928497591448865056897551369647572951678065293218262773535109517570063301},{\"N\":26795283137702680682928249296862530087842472246734793136745425103959797131946275785165380123246407347541301756608534399417315457621297828173799100415299038028000132400007033145208285975357270757349676475802290506334022575819711909048726984191229330686488645692766575512700815350477138871118264629456261245575818169248652012556458657614846527785238918007798503573524329415425634909433966472098690759475157567990295040713842313089421340490759563136309803284566724929689553851247027199218315480217785342029301299800845267212726702659481781642485092085828482087529504439014735616242894754686134401726498504177961361934173},{\"N\":23949789292911593449510090425353172637070236362229751109169870170660074497909172961293310462873907002932545468074300537494304277845207045851997549356787128175383049248182587416414995106526129859920221044303102714478973195782211467295196796329468913412347036581448101530025176782014110769308326049264044433721591396377444569703626747403360277212347996478898918531541102343860198874119847987688888682820636176591093459578593615102261280505750205171369244044064814235203973794638759615390042916863073533489259278661362761417186511175522276964580366578308875060521309515076941359221298299863412994866078466202574042667573}],\"ECDSAPub\":{\"Curve\":\"secp256k1\",\"Coords\":[502126147396950101462839227057312882525514004410561511775953670024016728846,17611364977305580060224387421197432146737413522728608426570173076481651013857]}}"}}' \ + $VAULT_ADDR/v1/tss1/data/tss_share echo "Initializing TSS 2" curl \ @@ -69,7 +75,12 @@ function init_tss { --request POST \ --data '{"data": {"value": "0x78e99c7e010ad161fb8aaace13528e272e17edbf50693ed146bf222727b57905"}}' \ $VAULT_ADDR/v1/tss2/data/core_account - + curl \ + --header "X-Vault-Token: $VAULT_TOKEN" \ + --header "Content-Type: application/json" \ + --request POST \ + --data '{"data": {"value": "{\"PaillierSK\":{\"N\":23949789292911593449510090425353172637070236362229751109169870170660074497909172961293310462873907002932545468074300537494304277845207045851997549356787128175383049248182587416414995106526129859920221044303102714478973195782211467295196796329468913412347036581448101530025176782014110769308326049264044433721591396377444569703626747403360277212347996478898918531541102343860198874119847987688888682820636176591093459578593615102261280505750205171369244044064814235203973794638759615390042916863073533489259278661362761417186511175522276964580366578308875060521309515076941359221298299863412994866078466202574042667573,\"LambdaN\":11974894646455796724755045212676586318535118181114875554584935085330037248954586480646655231436953501466272734037150268747152138922603522925998774678393564087691524624091293708207497553263064929960110522151551357239486597891105733647598398164734456706173518290724050765012588391007055384654163024632022216860640375798469880021536709557370577531772605109802353837479036021355748635699623455126267477588298425618567721274907913910908206434057292159869358240001078718226970146110646168083467233604175888466358753786198037231485227005500896725985164812282223808759546236941554880316003779704474822145334032125669227451494,\"PhiN\":23949789292911593449510090425353172637070236362229751109169870170660074497909172961293310462873907002932545468074300537494304277845207045851997549356787128175383049248182587416414995106526129859920221044303102714478973195782211467295196796329468913412347036581448101530025176782014110769308326049264044433721280751596939760043073419114741155063545210219604707674958072042711497271399246910252534955176596851237135442549815827821816412868114584319738716480002157436453940292221292336166934467208351776932717507572396074462970454011001793451970329624564447617519092473883109760632007559408949644290668064251338454902988,\"P\":168560804840592857953719124685092323950596591827625840277116650955044449708082732493489499923000606142729459918349646171582569792400403724617067865315330370214293803720727248222045544552572234240354310156346385692167371629919101722093488489006240398339626256337539770488433172870498142592029493065420761439619,\"Q\":142083975664216802599609163934029824852189667466585016305913650193657153012518344942864227721038719211228557110428141108862297845235217127013459698747326428535739698696740031001062905102149522316187460932620301262048685534601381790516548464738187044662590784856291828100857567583965207983380908885814826324967},\"NTildei\":23480415588435419031447079673342707638584619157635925492892092700956060545054581211317334723150904861647190443363724915913333634731042690202334969095297558918226065644052149284231228609885061347987303336592232342077541836117562828680125795146950913810227400817925439088111347951718821237533712931454939571041465013966764098136630139098758185943869375547993588616403453940445655380460537844392425662492015903802136973858968487213723069854403978084196943365237107277679727406895299211293901166051965782637942434024652916212091234932440173565058930379352547491346302287905973361117574863852150429835457489028946773862121,\"H1i\":5244625320890758926823236807577436724284838598713053894859482410751817243781879176481630696209928554813580388949933383265760121927355967924125199634578244258322548509081654208382874250887126067143103657480563891927357049788066150009272405448366743698886019402037370394519945482107092906983297562458511070353876474628171257852523538903349915748196522993465048339465501002966487352363881955493353463137731082045334297957366902605993954637756964170953811640782160016128220626751514742583329029928572309497541088770781393935677741525856476779288748073006775582646889485680084355364188574749761390046121904185116946045566,\"H2i\":14594346189095736747225884320665122466781709130296311799936140821845005707361969481771237336943241045846198194729530707984812596096197073779339845516025127003079087887760214159407509566512887375945758017191971067305459350608195289978927459042719267798647279892469529011521861358583760223133316545691872222500422769804045468217338943573175507344490987948897073607894657847841365159409323996140440196447829619732543872240361901392265668680831787448636887349391368628269910852040117529969597030556767165164122605145506454531225988854626670635330962899644382358608473267918441558783891780673214920504774987530770981704584,\"Alpha\":6849983899908839477446998981271744131325933529361571379418451316019981099335941018518872987098502545962210870285503466367236879379209835752622677240651847718148117782387557464941434343925340801690677827581243802202946029540882876148000789639310391232737175161153075193019087999995147840909112332932280345611684468595457782464460012011322167149066634383990122923438020021762489430848065292983874560611609056796134683497065218638164221506590753226536180358476214587409097579919853414736261669827341453686633517790118462010193065857873294119204591197936034415903929634546488536969308434620828170998975102411271908214450,\"Beta\":1335317112228187012591681633442315367076981402840725450256237355568443173067494686168575636615076475141387704560827215691780873780662116193990070171694314830127802717424694200486654488271460368390517751669243686049458502219836904297161887286444962050038730391886622568450873488545477563988132701170614994640401751652463407314316335905982645228286655962364559818918235324007659340176539997919714600985052059264364958857910249925345901254750798838349798938669280266482223976992744989435431891908101120310882759132822746224526244112340431949502423172601416858812968802104428574789717207603578958034192161020268395416556,\"P\":83205697366475707013272346458674560365964365948115580285063179406342662400228561778451021899715257517907197349419122670471046127419099529787196293112006144475901286703945889555737056213901995301964611616530315128697750145112153074320158894529162914984987799670774533069477396780252038616443269828729010160559,\"Q\":70549302306238109103155203324449977009957538047214854708176090947231122565831982440113126824918804906763755255072248524573563857706924566100524941234591720361019965188029453346046328692368145167725445181500697792323656923879765810345696113423994753776212138069069651771852988582222162236432391647424787205979,\"Xi\":15763412112260554229052878658640818374630961105887577266184852958862619239326,\"ShareID\":916059140686050998083978944447064389845990912875,\"Ks\":[625664809357144621199213330413524971235421323270,766418774313531105623098104317968735865501209001,916059140686050998083978944447064389845990912875],\"NTildej\":[27522048145569981576385476070657020085925130726339017105732055245635131139617848779486234236089392739447177029230176776337417792840985410026305811898446122370628457991480751841368787992944674212855827136776106587708211623816096633385470508783942777769060242860553024607789618299480333621903785949269589743855899844781732294719464609693176227125635396506961291206642890240986520873279868204630398583955556159431730110625063162375117211307543187824440584752056647328233594782678897603405275933516376239252164516303232640795307757685973412817046088426802943281427373371681705483044019813362572029248015450975003456793397,26287095469882602259502960253155254360804319664057270189400555438054357337039614490548038307454671193615535895363160165395716478102599420425673849627176549512756025903516156113017789809445854157445538189888630423465309197773535791949509398585263047625717743386222018827777580573693980777100354534528660867076047014926437366102709599305753278778933593961568721564772996025784590013410301807035352087426157394490518039676044525664902377430194383257189430440590876636954585590235337308078794409472039895617243643395811418712000686126844715347501087676349248362549061632812724867306834763198717465818908662935792809313461,23480415588435419031447079673342707638584619157635925492892092700956060545054581211317334723150904861647190443363724915913333634731042690202334969095297558918226065644052149284231228609885061347987303336592232342077541836117562828680125795146950913810227400817925439088111347951718821237533712931454939571041465013966764098136630139098758185943869375547993588616403453940445655380460537844392425662492015903802136973858968487213723069854403978084196943365237107277679727406895299211293901166051965782637942434024652916212091234932440173565058930379352547491346302287905973361117574863852150429835457489028946773862121],\"H1j\":[16517568479471580541042167147429887624339112445131695731362812965113271556999931499818506920295872359339618541473632721715590651347747155351693852424406909063273800991740936578905390940693930284712570022696272615200628747778340495713818589656470906148279108801537540281635961498936452198579197181393579833588271971375793237362898722119231386278352366524087073998056530952433549592269901646408479257883589277185145047553119070727427132500779070094700805261739909572847069577770555320557647528931722700347780859584941003633045209132957117021571486230238188597577097341756253345984078902464163897730250825619109443585525,7237296850102268809994391546422806771347421159582956353244469392173556245485887457113471982438691859952586948054041931864327489311127741289168937238696227543427161114005626119761194204840161333289011028101857213468065152804524954246351365273946276689599882908377802458781536966578763817065593945744764572690228246297840801885643084377214499585041076814427271051646943709207624302771267224258220858975367390613934384286047461793500454991574228495571945567704822616857994598586850247654535280319590421886683662233275827643945568263492469168667896177789512153268054577266692088605275176858300251672907083732758224300605,5244625320890758926823236807577436724284838598713053894859482410751817243781879176481630696209928554813580388949933383265760121927355967924125199634578244258322548509081654208382874250887126067143103657480563891927357049788066150009272405448366743698886019402037370394519945482107092906983297562458511070353876474628171257852523538903349915748196522993465048339465501002966487352363881955493353463137731082045334297957366902605993954637756964170953811640782160016128220626751514742583329029928572309497541088770781393935677741525856476779288748073006775582646889485680084355364188574749761390046121904185116946045566],\"H2j\":[7009260890983307674042645873443598272360402291874184297628611268334644375502827051885602971774872550746932684397961239936661508751942327338374679890949524040617402859819754169960008995120441085537975312375011245160932483701011029053912892591633122502853519566790980511469316278839843627242914384033499665339269445033918946320801864810354356251335317386995536622835031324684861929125151890912785727964237447305235539333445504406193442566504156368203203389831716945872865325564528075805169478948659002126911172399429651055487198066475747305990261540384572530954665466045817671514939564054758032645426132833343419966435,18466749817946044150553924616420387687206097203280195091736108793324688078484955636193932153729736892592854146381201326144829913584998765092920657820383019994011038791187893733125656289003473873902534439014371112885776632378339360078278137714330073530807181523557193669920350571316763823451053180616837192996398055144521752057518902938931133591279480112693018857707713794810315766346454595609625905047554072392064316910915940533310037364560325306800203727643345507302326279358384562465006827568506035523943913412901074793452573117887015063684946946486775965102910635992776743190835788384535879618002669142951088681822,14594346189095736747225884320665122466781709130296311799936140821845005707361969481771237336943241045846198194729530707984812596096197073779339845516025127003079087887760214159407509566512887375945758017191971067305459350608195289978927459042719267798647279892469529011521861358583760223133316545691872222500422769804045468217338943573175507344490987948897073607894657847841365159409323996140440196447829619732543872240361901392265668680831787448636887349391368628269910852040117529969597030556767165164122605145506454531225988854626670635330962899644382358608473267918441558783891780673214920504774987530770981704584],\"BigXj\":[{\"Curve\":\"secp256k1\",\"Coords\":[79619008090826827417533415687559692527871152001603267383531655235634106782332,41355629595052273785150599597686768188079009448260639000634144582720181845349]},{\"Curve\":\"secp256k1\",\"Coords\":[21867636963708472172322265900235651323653488750672401605779021705975274068736,84738007586914296108324220152131620334372233882254099554454274458534137664767]},{\"Curve\":\"secp256k1\",\"Coords\":[21158565506482058018760924074984840299389628553494973790822294949512295764682,107113721497980556594054348456479615159111507110184031956614891017419549482879]}],\"PaillierPKs\":[{\"N\":25222539396214102132947645117922233593416310527922261576767907607209454161148886976687740431574376057717301376863337351866485660948587280403504333047565362634864942355235574974370280655547038889388126778626768745495789937343074376983433409811121185054635688712486168781113695875463482736079503428210169676308334530833939254903788354387280861868113597976139498558860029324728905034236387595729540429364624198142499381828274695128146897507978696565574138170172578682300653201107201844796079327428944058588394275048046535588206168347669132263451197928497591448865056897551369647572951678065293218262773535109517570063301},{\"N\":26795283137702680682928249296862530087842472246734793136745425103959797131946275785165380123246407347541301756608534399417315457621297828173799100415299038028000132400007033145208285975357270757349676475802290506334022575819711909048726984191229330686488645692766575512700815350477138871118264629456261245575818169248652012556458657614846527785238918007798503573524329415425634909433966472098690759475157567990295040713842313089421340490759563136309803284566724929689553851247027199218315480217785342029301299800845267212726702659481781642485092085828482087529504439014735616242894754686134401726498504177961361934173},{\"N\":23949789292911593449510090425353172637070236362229751109169870170660074497909172961293310462873907002932545468074300537494304277845207045851997549356787128175383049248182587416414995106526129859920221044303102714478973195782211467295196796329468913412347036581448101530025176782014110769308326049264044433721591396377444569703626747403360277212347996478898918531541102343860198874119847987688888682820636176591093459578593615102261280505750205171369244044064814235203973794638759615390042916863073533489259278661362761417186511175522276964580366578308875060521309515076941359221298299863412994866078466202574042667573}],\"ECDSAPub\":{\"Curve\":\"secp256k1\",\"Coords\":[502126147396950101462839227057312882525514004410561511775953670024016728846,17611364977305580060224387421197432146737413522728608426570173076481651013857]}}"}}' \ + $VAULT_ADDR/v1/tss2/data/tss_share echo "Initializing TSS 3" curl \ --header "X-Vault-Token: $VAULT_TOKEN" \ @@ -83,6 +94,12 @@ function init_tss { --request POST \ --data '{"data": {"value": "0x24b399d4959b59d91c51877f695211f4766755eec1632524357e6ba111d27b94"}}' \ $VAULT_ADDR/v1/tss3/data/core_account + curl \ + --header "X-Vault-Token: $VAULT_TOKEN" \ + --header "Content-Type: application/json" \ + --request POST \ + --data '{"data": {"value": "{\"PaillierSK\":{\"N\":26795283137702680682928249296862530087842472246734793136745425103959797131946275785165380123246407347541301756608534399417315457621297828173799100415299038028000132400007033145208285975357270757349676475802290506334022575819711909048726984191229330686488645692766575512700815350477138871118264629456261245575818169248652012556458657614846527785238918007798503573524329415425634909433966472098690759475157567990295040713842313089421340490759563136309803284566724929689553851247027199218315480217785342029301299800845267212726702659481781642485092085828482087529504439014735616242894754686134401726498504177961361934173,\"LambdaN\":13397641568851340341464124648431265043921236123367396568372712551979898565973137892582690061623203673770650878304267199708657728810648914086899550207649519014000066200003516572604142987678635378674838237901145253167011287909855954524363492095614665343244322846383287756350407675238569435559132314728130622787745144005392029911347236343636798491944137624397554078492747961878651657896580917897840132068440220657417065768812273540144592041241733232904555384714876968365569545797913492391627142482822958858342086478228094807790438259988134089246680630567382818823048542511305501086220082767988639260236407896874346375534,\"PhiN\":26795283137702680682928249296862530087842472246734793136745425103959797131946275785165380123246407347541301756608534399417315457621297828173799100415299038028000132400007033145208285975357270757349676475802290506334022575819711909048726984191229330686488645692766575512700815350477138871118264629456261245575490288010784059822694472687273596983888275248795108156985495923757303315793161835795680264136880441314834131537624547080289184082483466465809110769429753936731139091595826984783254284965645917716684172956456189615580876519976268178493361261134765637646097085022611002172440165535977278520472815793748692751068,\"P\":172954130943874049238070010273458523122287507582033705181786044887978304227584807662572912031322578782673110936421182086222257268153211869437245544360307037235640788102071270597887490496507302608735336176521875010439781477289690493333654839695464655002460339403165136297658657744601617766551923741478471114247,\"Q\":154927106924078684526114917299472278228355251421361711357047446780353289413219828640437583306954547892787798239796583922909899140122884801063446970776663955722773971549128943837173704755632121703881790667867202586706044662215822970658075984998251794880947014588959477772795931405555505439473764642734198068859},\"NTildei\":26287095469882602259502960253155254360804319664057270189400555438054357337039614490548038307454671193615535895363160165395716478102599420425673849627176549512756025903516156113017789809445854157445538189888630423465309197773535791949509398585263047625717743386222018827777580573693980777100354534528660867076047014926437366102709599305753278778933593961568721564772996025784590013410301807035352087426157394490518039676044525664902377430194383257189430440590876636954585590235337308078794409472039895617243643395811418712000686126844715347501087676349248362549061632812724867306834763198717465818908662935792809313461,\"H1i\":7237296850102268809994391546422806771347421159582956353244469392173556245485887457113471982438691859952586948054041931864327489311127741289168937238696227543427161114005626119761194204840161333289011028101857213468065152804524954246351365273946276689599882908377802458781536966578763817065593945744764572690228246297840801885643084377214499585041076814427271051646943709207624302771267224258220858975367390613934384286047461793500454991574228495571945567704822616857994598586850247654535280319590421886683662233275827643945568263492469168667896177789512153268054577266692088605275176858300251672907083732758224300605,\"H2i\":18466749817946044150553924616420387687206097203280195091736108793324688078484955636193932153729736892592854146381201326144829913584998765092920657820383019994011038791187893733125656289003473873902534439014371112885776632378339360078278137714330073530807181523557193669920350571316763823451053180616837192996398055144521752057518902938931133591279480112693018857707713794810315766346454595609625905047554072392064316910915940533310037364560325306800203727643345507302326279358384562465006827568506035523943913412901074793452573117887015063684946946486775965102910635992776743190835788384535879618002669142951088681822,\"Alpha\":23117554482776287923815185169795695981757567945662710098031641486441947881649265365271273386263770953740935863059942122312285009258631179725741443391144436116994829519312826657526236949646552178668198337124392855535020775849792018939416731190074935154367192490277098235702951661208421136402298797262672005904967351957952899027250358843617695997858680454245269664217241000854107537505112465541350866057476386826310796414322718746088958954149354831361091532568088097047226482668772679699114702776371843617137947894639440817590959812516084816623024058680485779043753587623191994202920940324410560017853797724038877826436,\"Beta\":1132566243153808479225746000385719789311254050758712686435124578418360839993609856608293116344420260979097922883436305988388620065182888765471600251606105223647261918586935642407163880341206063399295560076090425016180744399947289166667837273090958510720514266044480485902818411738541707338242879440370481169868181737515371186384613322969647242830816536898331565454252327623797251806708798220322998263370701142369993846143132817216169932626880011898770602485129701390811136518669839758602342942766095694297920714214900275313962575607715826581610909427688531890587056661624392860000369165233848115036421452748660283773,\"P\":77714392845371388153195378034648821923180879986837047135308332090925837506214302625455715153652312734618196963640282412094711163539481513538494879036420970919370764889120725224428774180843818086913748113757413034049220109657819502159637084982369766722822976484623668308995298844271980494015822132189242146109,\"Q\":84563150104595593752399607605041212021595576333848847150402510468237250821413948617076592384933480271195419690701789471713502017927240972731916826791005138881925835684333581072319554466342678984195352390711933102727957625028717866189019028875684909382075856865158021465314101944235847951441541633598391916359,\"Xi\":18782339533783667926111136340382302780773345052893921131713323782574953738944,\"ShareID\":766418774313531105623098104317968735865501209001,\"Ks\":[625664809357144621199213330413524971235421323270,766418774313531105623098104317968735865501209001,916059140686050998083978944447064389845990912875],\"NTildej\":[27522048145569981576385476070657020085925130726339017105732055245635131139617848779486234236089392739447177029230176776337417792840985410026305811898446122370628457991480751841368787992944674212855827136776106587708211623816096633385470508783942777769060242860553024607789618299480333621903785949269589743855899844781732294719464609693176227125635396506961291206642890240986520873279868204630398583955556159431730110625063162375117211307543187824440584752056647328233594782678897603405275933516376239252164516303232640795307757685973412817046088426802943281427373371681705483044019813362572029248015450975003456793397,26287095469882602259502960253155254360804319664057270189400555438054357337039614490548038307454671193615535895363160165395716478102599420425673849627176549512756025903516156113017789809445854157445538189888630423465309197773535791949509398585263047625717743386222018827777580573693980777100354534528660867076047014926437366102709599305753278778933593961568721564772996025784590013410301807035352087426157394490518039676044525664902377430194383257189430440590876636954585590235337308078794409472039895617243643395811418712000686126844715347501087676349248362549061632812724867306834763198717465818908662935792809313461,23480415588435419031447079673342707638584619157635925492892092700956060545054581211317334723150904861647190443363724915913333634731042690202334969095297558918226065644052149284231228609885061347987303336592232342077541836117562828680125795146950913810227400817925439088111347951718821237533712931454939571041465013966764098136630139098758185943869375547993588616403453940445655380460537844392425662492015903802136973858968487213723069854403978084196943365237107277679727406895299211293901166051965782637942434024652916212091234932440173565058930379352547491346302287905973361117574863852150429835457489028946773862121],\"H1j\":[16517568479471580541042167147429887624339112445131695731362812965113271556999931499818506920295872359339618541473632721715590651347747155351693852424406909063273800991740936578905390940693930284712570022696272615200628747778340495713818589656470906148279108801537540281635961498936452198579197181393579833588271971375793237362898722119231386278352366524087073998056530952433549592269901646408479257883589277185145047553119070727427132500779070094700805261739909572847069577770555320557647528931722700347780859584941003633045209132957117021571486230238188597577097341756253345984078902464163897730250825619109443585525,7237296850102268809994391546422806771347421159582956353244469392173556245485887457113471982438691859952586948054041931864327489311127741289168937238696227543427161114005626119761194204840161333289011028101857213468065152804524954246351365273946276689599882908377802458781536966578763817065593945744764572690228246297840801885643084377214499585041076814427271051646943709207624302771267224258220858975367390613934384286047461793500454991574228495571945567704822616857994598586850247654535280319590421886683662233275827643945568263492469168667896177789512153268054577266692088605275176858300251672907083732758224300605,5244625320890758926823236807577436724284838598713053894859482410751817243781879176481630696209928554813580388949933383265760121927355967924125199634578244258322548509081654208382874250887126067143103657480563891927357049788066150009272405448366743698886019402037370394519945482107092906983297562458511070353876474628171257852523538903349915748196522993465048339465501002966487352363881955493353463137731082045334297957366902605993954637756964170953811640782160016128220626751514742583329029928572309497541088770781393935677741525856476779288748073006775582646889485680084355364188574749761390046121904185116946045566],\"H2j\":[7009260890983307674042645873443598272360402291874184297628611268334644375502827051885602971774872550746932684397961239936661508751942327338374679890949524040617402859819754169960008995120441085537975312375011245160932483701011029053912892591633122502853519566790980511469316278839843627242914384033499665339269445033918946320801864810354356251335317386995536622835031324684861929125151890912785727964237447305235539333445504406193442566504156368203203389831716945872865325564528075805169478948659002126911172399429651055487198066475747305990261540384572530954665466045817671514939564054758032645426132833343419966435,18466749817946044150553924616420387687206097203280195091736108793324688078484955636193932153729736892592854146381201326144829913584998765092920657820383019994011038791187893733125656289003473873902534439014371112885776632378339360078278137714330073530807181523557193669920350571316763823451053180616837192996398055144521752057518902938931133591279480112693018857707713794810315766346454595609625905047554072392064316910915940533310037364560325306800203727643345507302326279358384562465006827568506035523943913412901074793452573117887015063684946946486775965102910635992776743190835788384535879618002669142951088681822,14594346189095736747225884320665122466781709130296311799936140821845005707361969481771237336943241045846198194729530707984812596096197073779339845516025127003079087887760214159407509566512887375945758017191971067305459350608195289978927459042719267798647279892469529011521861358583760223133316545691872222500422769804045468217338943573175507344490987948897073607894657847841365159409323996140440196447829619732543872240361901392265668680831787448636887349391368628269910852040117529969597030556767165164122605145506454531225988854626670635330962899644382358608473267918441558783891780673214920504774987530770981704584],\"BigXj\":[{\"Curve\":\"secp256k1\",\"Coords\":[79619008090826827417533415687559692527871152001603267383531655235634106782332,41355629595052273785150599597686768188079009448260639000634144582720181845349]},{\"Curve\":\"secp256k1\",\"Coords\":[21867636963708472172322265900235651323653488750672401605779021705975274068736,84738007586914296108324220152131620334372233882254099554454274458534137664767]},{\"Curve\":\"secp256k1\",\"Coords\":[21158565506482058018760924074984840299389628553494973790822294949512295764682,107113721497980556594054348456479615159111507110184031956614891017419549482879]}],\"PaillierPKs\":[{\"N\":25222539396214102132947645117922233593416310527922261576767907607209454161148886976687740431574376057717301376863337351866485660948587280403504333047565362634864942355235574974370280655547038889388126778626768745495789937343074376983433409811121185054635688712486168781113695875463482736079503428210169676308334530833939254903788354387280861868113597976139498558860029324728905034236387595729540429364624198142499381828274695128146897507978696565574138170172578682300653201107201844796079327428944058588394275048046535588206168347669132263451197928497591448865056897551369647572951678065293218262773535109517570063301},{\"N\":26795283137702680682928249296862530087842472246734793136745425103959797131946275785165380123246407347541301756608534399417315457621297828173799100415299038028000132400007033145208285975357270757349676475802290506334022575819711909048726984191229330686488645692766575512700815350477138871118264629456261245575818169248652012556458657614846527785238918007798503573524329415425634909433966472098690759475157567990295040713842313089421340490759563136309803284566724929689553851247027199218315480217785342029301299800845267212726702659481781642485092085828482087529504439014735616242894754686134401726498504177961361934173},{\"N\":23949789292911593449510090425353172637070236362229751109169870170660074497909172961293310462873907002932545468074300537494304277845207045851997549356787128175383049248182587416414995106526129859920221044303102714478973195782211467295196796329468913412347036581448101530025176782014110769308326049264044433721591396377444569703626747403360277212347996478898918531541102343860198874119847987688888682820636176591093459578593615102261280505750205171369244044064814235203973794638759615390042916863073533489259278661362761417186511175522276964580366578308875060521309515076941359221298299863412994866078466202574042667573}],\"ECDSAPub\":{\"Curve\":\"secp256k1\",\"Coords\":[502126147396950101462839227057312882525514004410561511775953670024016728846,17611364977305580060224387421197432146737413522728608426570173076481651013857]}}"}}' \ + $VAULT_ADDR/v1/tss3/data/tss_share } echo "Initializing Vault..." diff --git a/cmd/service/sign.go b/cmd/service/sign.go index 6bd7717..b4b186e 100644 --- a/cmd/service/sign.go +++ b/cmd/service/sign.go @@ -2,10 +2,15 @@ package service import ( "context" + "crypto/ecdsa" "encoding/json" "fmt" "github.com/bnb-chain/tss-lib/v2/common" + "github.com/bnb-chain/tss-lib/v2/ecdsa/keygen" + tss_lib "github.com/bnb-chain/tss-lib/v2/tss" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/hyle-team/tss-svc/cmd/utils" + "github.com/hyle-team/tss-svc/internal/config" "github.com/hyle-team/tss-svc/internal/p2p" "github.com/hyle-team/tss-svc/internal/secrets/vault" "github.com/hyle-team/tss-svc/internal/tss" @@ -13,6 +18,7 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" "golang.org/x/sync/errgroup" + "math/big" "os" "os/signal" "syscall" @@ -68,7 +74,7 @@ var signCmd = &cobra.Command{ cfg.TSSParams().SigningSessionParams(), cfg.Log().WithField("component", "signing_session"), cfg.Parties(), - dataToSign, + []byte(dataToSign), connectionManager.GetReadyCount, ) @@ -90,21 +96,21 @@ var signCmd = &cobra.Command{ return errors.Wrap(err, "failed to obtain signing session result") } - cfg.Log().Info("signing session successfully completed. Signature: ", result.String()) - + cfg.Log().Info("signing session successfully completed") err = saveSigningResult(result) if err != nil { return errors.Wrap(err, "failed to save signing result") } + verifySignature(localSaveData, []byte(dataToSign), result, cfg) return nil }) - return errGroup.Wait() }, } func saveSigningResult(result *common.SignatureData) error { - raw, err := json.Marshal(result) + signature := hexutil.Encode(append(result.Signature, result.SignatureRecovery...)) + raw, err := json.Marshal(signature) if err != nil { return errors.Wrap(err, "failed to marshal signing result") } @@ -119,3 +125,21 @@ func saveSigningResult(result *common.SignatureData) error { } return nil } + +func verifySignature(localData *keygen.LocalPartySaveData, inputData []byte, signature *common.SignatureData, cfg config.Config) { + if utils.IsVerifyNeeded { + pk := ecdsa.PublicKey{ + Curve: tss_lib.EC(), + X: localData.ECDSAPub.X(), + Y: localData.ECDSAPub.Y(), + } + ok := ecdsa.Verify(&pk, big.NewInt(0).SetBytes(inputData).Bytes(), big.NewInt(0).SetBytes(signature.R), big.NewInt(0).SetBytes(signature.S)) + + if ok { + cfg.Log().Info("signature is valid") + } + if !ok { + cfg.Log().Warn("signature is invalid") + } + } +} diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 7f050a5..d0a43ee 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -14,6 +14,7 @@ const ( func RegisterOutputFlags(cmd *cobra.Command) { cmd.Flags().StringVarP(&OutputType, "output", "o", "console", "Output type: console, file, or vault") cmd.Flags().StringVar(&FilePath, "path", "preparams.json", "Path to save the pre-parameters file (used when output-type is 'file')") + cmd.Flags().BoolVar(&IsVerifyNeeded, "verify", false, "Verify that output signature is valid") RegisterConfigFlag(cmd) } @@ -28,6 +29,7 @@ func OutputValid() bool { var OutputType string var FilePath string var ConfigPath string +var IsVerifyNeeded bool func ConfigFromFlags(cmd *cobra.Command) (config.Config, error) { configPath, err := cmd.Flags().GetString(configFlag) diff --git a/internal/tss/session/sign.go b/internal/tss/session/sign.go index a85441c..d2d9644 100644 --- a/internal/tss/session/sign.go +++ b/internal/tss/session/sign.go @@ -33,12 +33,12 @@ type DefaultSigningSession struct { Receive(sender core.Address, data *p2p.TssData) } - data string + data []byte result *common.SignatureData err error } -func NewDefaultSigningSession(self tss.LocalSignParty, params SigningSessionParams, logger *logan.Entry, parties []p2p.Party, data string, connectedPartiesCountFunc func() int) *DefaultSigningSession { +func NewDefaultSigningSession(self tss.LocalSignParty, params SigningSessionParams, logger *logan.Entry, parties []p2p.Party, data []byte, connectedPartiesCountFunc func() int) *DefaultSigningSession { return &DefaultSigningSession{ params: params, wg: &sync.WaitGroup{}, @@ -104,17 +104,23 @@ func (s *DefaultSigningSession) Id() string { } func (s *DefaultSigningSession) Receive(request *p2p.SubmitRequest) error { + if request == nil || request.Data == nil { + return errors.New("nil request") + } if request.Type != p2p.RequestType_SIGN { return errors.New("invalid request type") } - var data *p2p.TssData - + data := &p2p.TssData{} if err := request.Data.UnmarshalTo(data); err != nil { return errors.Wrap(err, "failed to unmarshal TSS request data") } - sender, _ := core.AddressFromString(request.Sender) + sender, err := core.AddressFromString(request.Sender) + if err != nil { + return errors.Wrap(err, "failed to parse sender address") + } + s.signingParty.Receive(sender, data) return nil } diff --git a/internal/tss/signer.go b/internal/tss/signer.go index 9260450..1ec043f 100644 --- a/internal/tss/signer.go +++ b/internal/tss/signer.go @@ -6,7 +6,6 @@ import ( "github.com/bnb-chain/tss-lib/v2/ecdsa/keygen" "github.com/bnb-chain/tss-lib/v2/ecdsa/signing" "github.com/bnb-chain/tss-lib/v2/tss" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/hyle-team/tss-svc/internal/core" "github.com/hyle-team/tss-svc/internal/p2p" "gitlab.com/distributed_lab/logan/v3" @@ -35,14 +34,14 @@ type SignParty struct { msgs chan partyMsg broadcaster *p2p.Broadcaster - data string + data []byte ended atomic.Bool result *common.SignatureData sessionId string } -func NewSignParty(self LocalSignParty, parties []p2p.Party, data, sessionId string, logger *logan.Entry) *SignParty { +func NewSignParty(self LocalSignParty, parties []p2p.Party, data []byte, sessionId string, logger *logan.Entry) *SignParty { partyMap := make(map[core.Address]struct{}, len(parties)) partyIds := make([]*tss.PartyID, len(parties)+1) partyIds[0] = self.Address.PartyIdentifier() @@ -79,7 +78,7 @@ func (p *SignParty) Run(ctx context.Context) { out := make(chan tss.Message, OutChannelSize) end := make(chan *common.SignatureData, EndChannelSize) - p.party = signing.NewLocalParty(new(big.Int).SetBytes(hexutil.MustDecode(p.data)), params, *p.self.Data, out, end) + p.party = signing.NewLocalParty(new(big.Int).SetBytes(p.data), params, *p.self.Data, out, end) p.wg.Add(3) @@ -122,8 +121,8 @@ func (p *SignParty) receiveMsgs(ctx context.Context) { case <-ctx.Done(): p.logger.Warn("context is done; stopping receiving messages") return - case msg, closed := <-p.msgs: - if closed { + case msg, ok := <-p.msgs: + if !ok { p.logger.Debug("msg channel is closed") return } @@ -139,7 +138,6 @@ func (p *SignParty) receiveMsgs(ctx context.Context) { } } } - } func (p *SignParty) receiveUpdates(ctx context.Context, out <-chan tss.Message, end <-chan *common.SignatureData) {