@@ -17,8 +17,6 @@ import (
1717
1818 "encoding/json"
1919
20- "sync"
21-
2220 "github.com/nats-io/nats.go"
2321)
2422
@@ -30,24 +28,21 @@ const (
3028type ECDHSession interface {
3129 ListenKeyExchange () error
3230 BroadcastPublicKey () error
33- WaitForExchangeComplete () error
34- ResetLocalKeys ()
31+ RemovePeer ( peerID string )
32+ GetReadyPeersCount () int
3533 ErrChan () <- chan error
3634 Close () error
3735}
3836
3937type ecdhSession struct {
40- nodeID string
41- peerIDs []string
42- pubSub messaging.PubSub
43- ecdhSub messaging.Subscription
44- identityStore identity.Store
45- privateKey * ecdh.PrivateKey
46- publicKey * ecdh.PublicKey
47- exchangeComplete chan struct {}
48- errCh chan error
49- exchangeDone bool
50- mu sync.RWMutex
38+ nodeID string
39+ peerIDs []string
40+ pubSub messaging.PubSub
41+ ecdhSub messaging.Subscription
42+ identityStore identity.Store
43+ privateKey * ecdh.PrivateKey
44+ publicKey * ecdh.PublicKey
45+ errCh chan error
5146}
5247
5348func NewECDHSession (
@@ -57,20 +52,24 @@ func NewECDHSession(
5752 identityStore identity.Store ,
5853) * ecdhSession {
5954 return & ecdhSession {
60- nodeID : nodeID ,
61- peerIDs : peerIDs ,
62- pubSub : pubSub ,
63- identityStore : identityStore ,
64- exchangeComplete : make (chan struct {}, 1 ),
65- errCh : make (chan error , 1 ),
55+ nodeID : nodeID ,
56+ peerIDs : peerIDs ,
57+ pubSub : pubSub ,
58+ identityStore : identityStore ,
59+ errCh : make (chan error , 1 ),
6660 }
6761}
6862
69- func (e * ecdhSession ) ResetLocalKeys () {
70- // Set a specific key to an empty []byte
71- for _ , peerID := range e .peerIDs {
72- e .identityStore .SetSymmetricKey (peerID , []byte {})
73- }
63+ func (e * ecdhSession ) RemovePeer (peerID string ) {
64+ e .identityStore .RemoveSymmetricKey (peerID )
65+ }
66+
67+ func (e * ecdhSession ) GetReadyPeersCount () int {
68+ return e .identityStore .GetSymetricKeyCount ()
69+ }
70+
71+ func (e * ecdhSession ) ErrChan () <- chan error {
72+ return e .errCh
7473}
7574
7675func (e * ecdhSession ) ListenKeyExchange () error {
@@ -114,21 +113,7 @@ func (e *ecdhSession) ListenKeyExchange() error {
114113 // Derive symmetric key using HKDF
115114 symmetricKey := e .deriveSymmetricKey (sharedSecret , ecdhMsg .From )
116115 e .identityStore .SetSymmetricKey (ecdhMsg .From , symmetricKey )
117-
118- requiredKeyCount := len (e .peerIDs ) - 1
119- logger .Info ("ECDH progress" , "peer" , ecdhMsg .From , "required" , requiredKeyCount )
120-
121- if e .identityStore .CheckSymmetricKeyComplete (requiredKeyCount ) {
122- logger .Info ("Completed ECDH!" , "symmetric key counts of peers" , requiredKeyCount )
123- logger .Info ("ALL PEERS ARE READY! Starting to accept MPC requests" )
124-
125- e .mu .Lock ()
126- if ! e .exchangeDone {
127- e .exchangeDone = true
128- e .exchangeComplete <- struct {}{}
129- }
130- e .mu .Unlock ()
131- }
116+ logger .Debug ("ECDH progress" , "peer" , ecdhMsg .From , "current" , e .identityStore .GetSymetricKeyCount ())
132117 })
133118
134119 e .ecdhSub = sub
@@ -138,10 +123,6 @@ func (e *ecdhSession) ListenKeyExchange() error {
138123 return nil
139124}
140125
141- func (s * ecdhSession ) ErrChan () <- chan error {
142- return s .errCh
143- }
144-
145126func (s * ecdhSession ) Close () error {
146127 err := s .ecdhSub .Unsubscribe ()
147128 if err != nil {
@@ -173,25 +154,6 @@ func (e *ecdhSession) BroadcastPublicKey() error {
173154 return nil
174155}
175156
176- func (e * ecdhSession ) WaitForExchangeComplete () error {
177- e .mu .RLock ()
178- if e .exchangeDone {
179- e .mu .RUnlock ()
180- return nil
181- }
182- e .mu .RUnlock ()
183- timeout := time .After (ECDHExchangeTimeout ) // 2 minutes timeout
184-
185- select {
186- case <- e .exchangeComplete :
187- return nil
188- case err := <- e .errCh :
189- return err
190- case <- timeout :
191- return fmt .Errorf ("ECDH exchange timeout!" )
192- }
193- }
194-
195157func deriveConsistentInfo (a , b string ) []byte {
196158 if a < b {
197159 return []byte (a + b )
0 commit comments