forked from attestantio/dirk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
343 lines (294 loc) · 11.8 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright © 2020 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package standard
import (
"context"
"fmt"
"sync"
"time"
"github.com/bliiitz/dirk/core"
"github.com/bliiitz/dirk/services/checker"
"github.com/bliiitz/dirk/services/fetcher"
"github.com/bliiitz/dirk/services/peers"
"github.com/bliiitz/dirk/services/sender"
"github.com/bliiitz/dirk/services/unlocker"
"github.com/herumi/bls-eth-go-binary/bls"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/rs/zerolog"
zerologger "github.com/rs/zerolog/log"
e2wallet "github.com/wealdtech/go-eth2-wallet"
distributed "github.com/wealdtech/go-eth2-wallet-distributed"
e2wtypes "github.com/wealdtech/go-eth2-wallet-types/v2"
)
// Service is used to manage the process of distributed key generation operations.
type Service struct {
checkerSvc checker.Service
fetcherSvc fetcher.Service
senderSvc sender.Service
peersSvc peers.Service
unlockerSvc unlocker.Service
encryptor e2wtypes.Encryptor
id uint64
stores []e2wtypes.Store
generationPassphrase []byte
generations map[string]*generation
generationsMu sync.RWMutex
}
// module-wide log.
var log zerolog.Logger
// New creates a new process service.
func New(ctx context.Context, params ...Parameter) (*Service, error) {
parameters, err := parseAndCheckParameters(params...)
if err != nil {
return nil, errors.Wrap(err, "problem with parameters")
}
// Set logging.
log = zerologger.With().Str("service", "process").Str("impl", "standard").Logger()
if parameters.logLevel != log.GetLevel() {
log = log.Level(parameters.logLevel)
}
s := &Service{
checkerSvc: parameters.checker,
fetcherSvc: parameters.fetcher,
unlockerSvc: parameters.unlocker,
senderSvc: parameters.sender,
peersSvc: parameters.peers,
id: parameters.id,
stores: parameters.stores,
encryptor: parameters.encryptor,
generationPassphrase: parameters.generationPassphrase,
generations: make(map[string]*generation),
}
return s, nil
}
// OnPrepare is called when we receive a request from the given participant to prepare for DKG.
func (s *Service) OnPrepare(ctx context.Context,
sender uint64,
account string,
passphrase []byte,
threshold uint32,
participants []*core.Endpoint) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "services.process.OnPrepare")
defer span.Finish()
log.Trace().Uint64("sender_id", sender).Str("account", account).Msg("Preparing for distributed key generation")
s.generationsMu.Lock()
defer s.generationsMu.Unlock()
if _, err := s.getGeneration(ctx, account); err == nil {
log.Debug().Uint64("sender_id", sender).Str("account", account).Msg("Already in progress")
return ErrInProgress
}
s.generations[account] = &generation{
processStarted: time.Now(),
id: s.id,
account: account,
passphrase: passphrase,
threshold: threshold,
participants: participants,
sharedSecrets: make(map[uint64]bls.SecretKey),
sharedVVecs: make(map[uint64][]bls.PublicKey),
}
if err := s.contribution(ctx, s.generations[account]); err != nil {
log.Debug().Uint64("sender_id", sender).Str("account", account).Msg("Failed to generate our own contribution")
return errors.Wrap(err, "failed to generate own contribution")
}
return nil
}
// OnExecute is called when we receive a request from the given participant to execute the given DKG.
func (s *Service) OnExecute(ctx context.Context, sender uint64, account string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "services.process.OnExecute")
defer span.Finish()
log.Trace().Uint64("sender", sender).Str("account", account).Msg("Executing")
s.generationsMu.Lock()
defer s.generationsMu.Unlock()
generation, err := s.getGeneration(ctx, account)
if err != nil {
if err == ErrNotFound {
return ErrNotInProgress
}
return err
}
// We need to swap secrets with all the other participants.
// Initiate calls to any participants with a higher ID than us.
verificationVector := generation.sharedVVecs[generation.id]
for id, distributionSecret := range generation.distributionSecrets {
if id > s.id {
peer, err := s.peersSvc.Peer(id)
if err != nil {
return errors.Wrap(err, "failed to obtain peer")
}
log.Trace().Uint64("id", s.id).Uint64("peer", id).Msg("Initiating contribution swap")
recipientSecret, recipientVVec, err := s.senderSvc.SendContribution(ctx, peer, account, distributionSecret, verificationVector)
if err != nil {
return errors.Wrap(err, "failed to send contribution")
}
if !verifyContribution(generation.id, recipientSecret, recipientVVec) {
log.Warn().Msg("Contribution invalid")
return fmt.Errorf("invalid contribution from %d", id)
}
if _, exists := generation.sharedSecrets[id]; exists {
return fmt.Errorf("duplicate contribution from %d", id)
}
generation.sharedSecrets[id] = recipientSecret
generation.sharedVVecs[id] = recipientVVec
}
}
return nil
}
// OnCommit is called when we receive a request from the given participant to commit the given DKG.
func (s *Service) OnCommit(ctx context.Context, sender uint64, account string, confirmationData []byte) ([]byte, []byte, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "services.process.OnCommit")
defer span.Finish()
s.generationsMu.Lock()
defer s.generationsMu.Unlock()
generation, err := s.getGeneration(ctx, account)
if err == ErrNotFound {
return nil, nil, ErrNotInProgress
}
if len(generation.sharedSecrets) != len(generation.participants) {
contributedParticipants := make([]uint64, 0)
for k := range generation.sharedSecrets {
contributedParticipants = append(contributedParticipants, k)
}
allParticipants := make([]uint64, len(generation.participants))
for k := range generation.participants {
allParticipants[k] = generation.participants[k].ID
}
return nil, nil, fmt.Errorf("have %d contributions (%v) , need %d (%v), aborting", len(generation.sharedSecrets), contributedParticipants, len(allParticipants), allParticipants)
}
if len(generation.sharedVVecs) != len(generation.participants) {
return nil, nil, fmt.Errorf("have %d contributions, need %d, aborting", len(generation.sharedVVecs), len(generation.participants))
}
privateKey := bls.SecretKey{}
for k := range generation.sharedSecrets {
sharedSecret := generation.sharedSecrets[k]
privateKey.Add(&sharedSecret)
}
aggregateVVec := make([]bls.PublicKey, generation.threshold)
for _, sharedVVec := range generation.sharedVVecs {
for i := range sharedVVec {
aggregateVVec[i].Add(&sharedVVec[i])
}
}
passphrase := generation.passphrase
if passphrase == nil {
passphrase = s.generationPassphrase
}
err = s.storeDistributedKey(ctx, generation.account, passphrase, privateKey, generation.threshold, aggregateVVec, generation.participants)
if err != nil {
log.Warn().Err(err).Msg("Failed to create key")
return nil, nil, errors.Wrap(err, ErrNotCreated.Error())
}
// Attempt to retrieve the key to ensure it has been stored properly.
walletName, accountName, err := e2wallet.WalletAndAccountNames(account)
if err != nil {
log.Warn().Err(err).Str("path", account).Msg("Failed to obtain wallet and accout names from path")
return nil, nil, ErrNotCreated
}
retrievedWallet, err := distributed.OpenWallet(ctx, walletName, s.stores[0], s.encryptor)
if err != nil {
log.Warn().Err(err).Msg("Failed to retrieve wallet for created account")
return nil, nil, ErrNotCreated
}
accountByNameProvider, isProvider := retrievedWallet.(e2wtypes.WalletAccountByNameProvider)
if !isProvider {
log.Info().Msg("Wallet does not support fetching accounts by name")
return nil, nil, errors.New("wallet does not support fetching accounts by name")
}
_, err = accountByNameProvider.AccountByName(ctx, accountName)
if err != nil {
log.Warn().Err(err).Msg("Failed to retrieve created account")
return nil, nil, ErrNotCreated
}
sig := privateKey.SignByte(confirmationData)
delete(s.generations, account)
return aggregateVVec[0].Serialize(), sig.Serialize(), nil
}
// OnAbort is called when we receive a request from the given participant to abort the given DKG.
func (s *Service) OnAbort(ctx context.Context, sender uint64, account string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "services.process.OnAbort")
defer span.Finish()
s.generationsMu.Lock()
defer s.generationsMu.Unlock()
_, err := s.getGeneration(ctx, account)
if err == ErrNotFound {
return ErrNotInProgress
}
delete(s.generations, account)
return nil
}
// OnContribute is is called when we need to swap contributions with another participant.
func (s *Service) OnContribute(ctx context.Context, sender uint64, account string, secret bls.SecretKey, vVec []bls.PublicKey) (bls.SecretKey, []bls.PublicKey, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "services.process.OnContribute")
defer span.Finish()
s.generationsMu.Lock()
defer s.generationsMu.Unlock()
generation, err := s.getGeneration(ctx, account)
if err != nil {
return bls.SecretKey{}, nil, err
}
if !verifyContribution(generation.id, secret, vVec) {
log.Warn().Uint64("sender", sender).Str("account", account).Msg("Received invalid contribution")
return bls.SecretKey{}, nil, fmt.Errorf("invalid contribution from %d", sender)
}
// Store the contributed information.
generation.sharedSecrets[sender] = secret
generation.sharedVVecs[sender] = vVec
// We return our unique generated secret for the sender, and our own verification vector.
return generation.distributionSecrets[sender], generation.sharedVVecs[generation.id], nil
}
func (s *Service) storeDistributedKey(ctx context.Context,
account string,
passphrase []byte,
privateKey bls.SecretKey,
threshold uint32,
verificationVector []bls.PublicKey,
participants []*core.Endpoint) error {
store := s.stores[0]
walletName, accountName, err := e2wallet.WalletAndAccountNames(account)
if err != nil {
return errors.Wrap(err, "failed to parse account")
}
vVec := make([][]byte, len(verificationVector))
for i := range verificationVector {
vVec[i] = verificationVector[i].Serialize()
}
wallet, err := distributed.OpenWallet(ctx, walletName, store, s.encryptor)
if err != nil {
return errors.Wrap(err, "failed to open wallet")
}
locker, isLocker := wallet.(e2wtypes.WalletLocker)
if isLocker {
if err := locker.Unlock(ctx, []byte{}); err != nil {
return errors.Wrap(err, "failed to unlock wallet")
}
defer func() {
if err := locker.Lock(ctx); err != nil {
log.Warn().Str("wallet", wallet.Name()).Msg("Failed to lock")
}
}()
}
walletParticipants := make(map[uint64]string, len(participants))
for i := range participants {
walletParticipants[participants[i].ID] = participants[i].ConnectAddress()
}
generatedAccount, err := wallet.(e2wtypes.WalletDistributedAccountImporter).ImportDistributedAccount(ctx, accountName, privateKey.Serialize(), threshold, vVec, walletParticipants, passphrase)
if err != nil {
return errors.Wrap(err, "failed to import account")
}
if err := s.fetcherSvc.AddAccount(ctx, wallet, generatedAccount); err != nil {
// Warn but do not propagate this error.
log.Warn().Err(err).Msg("Failed to add account to internal cache, will be unavailable until restart")
}
return nil
}