Skip to content

Commit

Permalink
use broker to connect messages via params
Browse files Browse the repository at this point in the history
  • Loading branch information
MagicalTux committed Jun 4, 2024
1 parent 5e39ded commit 34f34fc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
5 changes: 3 additions & 2 deletions ecdsatss/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Keygen struct {
data *Key // key data currently being generated
round int // current round

Receiver tss.JsonExpect
Receiver tss.MessageReceiver
}

func NewKeygen(params *tss.Parameters) (*Keygen, error) {
Expand Down Expand Up @@ -155,10 +155,11 @@ func (kg *Keygen) round1() error {
}
otherIds = append(otherIds, p)
m := tss.JsonWrap("ecdsa:keygen:round1", msg, Pi, p)
_ = m
kg.params.Broker().Receive(m)
}

kg.Receiver = tss.NewJsonExpect[keygenRound2msg1]("ecdsa:keygen:round2-1", otherIds, kg.round2)
kg.params.Broker().Connect("ecdsa:keygen:round2-1", kg.Receiver)

return nil
}
Expand Down
32 changes: 32 additions & 0 deletions tss/broker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package tss

import "fmt"

type MessageReceiver interface {
Receive(msg *JsonMessage) error
}

type MessageBroker interface {
MessageReceiver
Connect(typ string, dest MessageReceiver)
}

type testBroker struct {
rcv map[string]MessageReceiver
}

func NewTestBroker() *testBroker {
return &testBroker{rcv: make(map[string]MessageReceiver)}
}

func (b *testBroker) Receive(msg *JsonMessage) error {
tgt, ok := b.rcv[msg.Type]
if !ok {
return fmt.Errorf("no handler for message type %s", msg.Type)
}
return tgt.Receive(msg)
}

func (b *testBroker) Connect(typ string, dest MessageReceiver) {
b.rcv[typ] = dest
}
10 changes: 3 additions & 7 deletions tss/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import (
sync "sync"
)

type JsonExpect interface {
Receive(msg *JsonMessage) error
}

// JsonExpect is an object used to collect messages from peers and trigger a callback once
// jsonExpect is an object used to collect messages from peers and trigger a callback once
// enough messages have been collected
type jsonExpect[T any] struct {
Type string
Expand Down Expand Up @@ -72,9 +68,9 @@ func JsonWrap(typ string, o any, from *PartyID, to *PartyID) *JsonMessage {
return &JsonMessage{Type: typ, From: from, To: to, Data: o}
}

// NewJsonExpect returns a new JsonExpect of the given type that can be used to collect
// NewJsonExpect returns a new MessageReceiver of the given type that can be used to collect
// packets from multiple parties, and trigger a callback once everything has been collected
func NewJsonExpect[T any](typ string, parties []*PartyID, cb func([]*PartyID, []*T)) JsonExpect {
func NewJsonExpect[T any](typ string, parties []*PartyID, cb func([]*PartyID, []*T)) MessageReceiver {
res := &jsonExpect[T]{
Type: typ,
From: parties,
Expand Down
10 changes: 10 additions & 0 deletions tss/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type (
noProofFac bool
// random sources
partialKeyRand, rand io.Reader
broker MessageBroker
}

ReSharingParameters struct {
Expand All @@ -56,6 +57,7 @@ func NewParameters(ec elliptic.Curve, ctx *PeerContext, partyID *PartyID, partyC
safePrimeGenTimeout: defaultSafePrimeGenTimeout,
partialKeyRand: rand.Reader,
rand: rand.Reader,
broker: NewTestBroker(),
}
}

Expand Down Expand Up @@ -112,6 +114,14 @@ func (params *Parameters) SetNoProofFac() {
params.noProofFac = true
}

func (params *Parameters) SetBroker(b MessageBroker) {
params.broker = b
}

func (params *Parameters) Broker() MessageBroker {
return params.broker
}

func (params *Parameters) PartialKeyRand() io.Reader {
return params.partialKeyRand
}
Expand Down

0 comments on commit 34f34fc

Please sign in to comment.