/
jrpc_handlers.go
656 lines (568 loc) · 25.5 KB
/
jrpc_handlers.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
package dkgnode
import (
"context"
"encoding/hex"
"fmt"
"math/big"
"sort"
"strconv"
"strings"
"time"
pcmn "github.com/torusresearch/torus-node/common"
"github.com/torusresearch/torus-node/dealer"
"github.com/torusresearch/torus-node/telemetry"
tronCrypto "github.com/TRON-US/go-eccrypto"
logging "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"github.com/torusresearch/bijson"
"github.com/torusresearch/jsonrpc"
tmquery "github.com/torusresearch/tendermint/libs/pubsub/query"
tmtypes "github.com/torusresearch/tendermint/types"
"github.com/torusresearch/torus-common/common"
"github.com/torusresearch/torus-common/crypto"
"github.com/torusresearch/torus-common/secp256k1"
"github.com/torusresearch/torus-node/config"
"github.com/torusresearch/torus-node/eventbus"
)
const requestTimer = 10
func (h CommitmentRequestHandler) ServeJSONRPC(c context.Context, params *bijson.RawMessage) (interface{}, *jsonrpc.Error) {
telemetry.IncrementCounter(pcmn.TelemetryConstants.JRPC.CommitmentRequestCounter, pcmn.TelemetryConstants.JRPC.Prefix)
serviceLibrary := NewServiceLibrary(h.eventBus, "commitment_request_handler")
logging.WithField("params", stringify(params)).Debug("CommitmentRequestHandler")
var p CommitmentRequestParams
if err := jsonrpc.Unmarshal(params, &p); err != nil {
return nil, err
}
tokenCommitment := p.TokenCommitment
verifierIdentifier := p.VerifierIdentifier
// check if message prefix is correct
if p.MessagePrefix != "mug00" {
logging.WithField("params", stringify(params)).Debug("incorrect message prefix")
return nil, &jsonrpc.Error{Code: -32603, Message: "Internal error", Data: "Incorrect message prefix"}
}
found := serviceLibrary.CacheMethods().TokenCommitExists(verifierIdentifier, tokenCommitment)
if found {
// allow for loadtesting in debug mode
if !config.GlobalConfig.IsDebug {
logging.WithFields(logging.Fields{
"verifierIdentifier": verifierIdentifier,
"tokenCommitment": stringify(tokenCommitment),
}).Debug("duplicate token found")
return nil, &jsonrpc.Error{Code: -32603, Message: "Internal error", Data: "Duplicate token found"}
}
}
tempPubKey := common.Point{
X: *common.HexToBigInt(p.TempPubX),
Y: *common.HexToBigInt(p.TempPubY),
}
serviceLibrary.CacheMethods().RecordTokenCommit(verifierIdentifier, tokenCommitment, tempPubKey)
// sign data
commitmentRequestResultData := CommitmentRequestResultData{
p.MessagePrefix,
p.TokenCommitment,
p.TempPubX,
p.TempPubY,
p.VerifierIdentifier,
strconv.FormatInt(time.Now().Unix(), 10),
}
logging.WithField("CURRENTTIME", strconv.FormatInt(time.Now().Unix(), 10)).Debug()
k := serviceLibrary.EthereumMethods().GetSelfPrivateKey()
pk := serviceLibrary.EthereumMethods().GetSelfPublicKey()
sig := crypto.SignData([]byte(commitmentRequestResultData.ToString()), crypto.BigIntToECDSAPrivateKey(k))
res := CommitmentRequestResult{
Signature: crypto.SigToHex(sig),
Data: commitmentRequestResultData.ToString(),
NodePubX: pk.X.Text(16),
NodePubY: pk.Y.Text(16),
}
logging.WithField("CommitmentRequestResult", stringify(res)).Debug()
return res, nil
}
func (h PingHandler) ServeJSONRPC(c context.Context, params *bijson.RawMessage) (interface{}, *jsonrpc.Error) {
telemetry.IncrementCounter(pcmn.TelemetryConstants.JRPC.PingRequestCounter, pcmn.TelemetryConstants.JRPC.Prefix)
serviceLibrary := NewServiceLibrary(h.eventBus, "ping_handler")
var p PingParams
if err := jsonrpc.Unmarshal(params, &p); err != nil {
return nil, err
}
return PingResult{
Message: serviceLibrary.EthereumMethods().GetSelfAddress().Hex(),
}, nil
}
func (h ConnectionDetailsHandler) ServeJSONRPC(c context.Context, params *bijson.RawMessage) (interface{}, *jsonrpc.Error) {
telemetry.IncrementCounter(pcmn.TelemetryConstants.JRPC.ConnectionDetailsCounter, pcmn.TelemetryConstants.JRPC.Prefix)
logging.WithField("connectiondetailsparams", string(*params)).Debug("connection details handler handling request")
serviceLibrary := NewServiceLibrary(h.eventBus, "connection_details_handler")
var p ConnectionDetailsParams
if err := jsonrpc.Unmarshal(params, &p); err != nil {
return nil, err
}
if !serviceLibrary.EthereumMethods().ValidateEpochPubKey(p.ConnectionDetailsMessage.NodeAddress, common.Point{X: p.PubKeyX, Y: p.PubKeyY}) {
return nil, &jsonrpc.Error{Code: -32602, Message: "Internal error", Data: "Invalid pub key for provided epoch"}
}
valid, err := p.ConnectionDetailsMessage.Validate(p.PubKeyX, p.PubKeyY, p.Signature)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: "Internal error", Data: fmt.Sprintf("Could not validate connection details message, err: %v", err)}
}
if !valid {
return nil, &jsonrpc.Error{Code: -32602, Message: "Internal error", Data: "invalid connection details message"}
}
return ConnectionDetailsResult{
TMP2PConnection: serviceLibrary.EthereumMethods().GetTMP2PConnection(),
P2PConnection: serviceLibrary.EthereumMethods().GetP2PConnection(),
}, nil
}
// checks id for assignment and then the auth token for verification
// returns the node's share of the user's key
func (h ShareRequestHandler) ServeJSONRPC(c context.Context, params *bijson.RawMessage) (interface{}, *jsonrpc.Error) {
telemetry.IncrementCounter(pcmn.TelemetryConstants.JRPC.ShareRequestCounter, pcmn.TelemetryConstants.JRPC.Prefix)
serviceLibrary := NewServiceLibrary(h.eventBus, "share_request_handler")
currEpoch := abciServiceLibrary.EthereumMethods().GetCurrentEpoch()
currEpochInfo, err := abciServiceLibrary.EthereumMethods().GetEpochInfo(currEpoch, false)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: "Internal error", Data: "Error occurred while current epoch"}
}
logging.WithField("params", stringify(params)).Debug("ShareRequestHandler")
var p ShareRequestParams
if err := jsonrpc.Unmarshal(params, &p); err != nil {
return nil, err
}
logging.WithField("params", stringify(p)).Debug("ShareRequesthandler after parsing params")
numberOfThresholdNodes := int(currEpochInfo.K.Int64())
allKeyIndexes := make(map[string]big.Int) // String keyindex => keyindex
allValidVerifierIDs := make(map[string]bool) // verifier + pcmn.Delimiter1 + verifierIDs => bool
var pubKey common.Point
// For Each VerifierItem we check its validity
for _, rawItem := range p.Item {
var parsedVerifierParams ShareRequestItem
err := bijson.Unmarshal(rawItem, &parsedVerifierParams)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: "Internal error", Data: "Error occurred while parsing sharerequestitem"}
}
logging.WithField("PARSEDVERIFIERPARAMS", stringify(parsedVerifierParams)).Debug()
// verify token validity against verifier, do not pass along nodesignatures
jsonMap := make(map[string]interface{})
err = bijson.Unmarshal(rawItem, &jsonMap)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: "Internal error", Data: "Error occurred while parsing jsonmap"}
}
delete(jsonMap, "nodesignatures")
redactedRawItem, err := bijson.Marshal(jsonMap)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: "Internal error", Data: "Error occurred while marshalling" + err.Error()}
}
verified, verifierID, err := serviceLibrary.VerifierMethods().Verify((*bijson.RawMessage)(&redactedRawItem))
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: "Internal error", Data: "Error occurred while verifying params" + err.Error()}
}
if !verified {
return nil, &jsonrpc.Error{Code: -32602, Message: "Internal error", Data: "Could not verify params"}
}
// Validate signatures
var validSignatures []ValidatedNodeSignature
for i := 0; i < len(parsedVerifierParams.NodeSignatures); i++ {
logging.WithField("NODESIGNATURE", stringify(parsedVerifierParams.NodeSignatures[i])).Debug()
nodeRef, err := parsedVerifierParams.NodeSignatures[i].NodeValidation(h.eventBus)
if err == nil {
validSignatures = append(validSignatures, ValidatedNodeSignature{
parsedVerifierParams.NodeSignatures[i],
*nodeRef.Index,
})
} else {
logging.WithError(err).Error("could not validate signatures")
}
}
// Check if we have threshold number of signatures
if len(validSignatures) < numberOfThresholdNodes {
return nil, &jsonrpc.Error{Code: -32602, Message: "Internal error", Data: "Not enough valid signatures. Only " + strconv.Itoa(len(validSignatures)) + "valid signatures found."}
}
// Find common data string, and filter valid signatures on the wrong data
// this is to prevent nodes from submitting valid signatures on wrong data
commonDataMap := make(map[string]int)
for i := 0; i < len(validSignatures); i++ {
var commitmentRequestResultData CommitmentRequestResultData
ok, err := commitmentRequestResultData.FromString(validSignatures[i].Data)
if !ok || err != nil {
logging.WithField("ok", ok).WithError(err).Error("could not get commitmentRequestResultData from string")
}
stringData := strings.Join([]string{
commitmentRequestResultData.MessagePrefix,
commitmentRequestResultData.TokenCommitment,
commitmentRequestResultData.VerifierIdentifier,
}, pcmn.Delimiter1)
commonDataMap[stringData]++
}
var commonDataString string
var commonDataCount int
for k, v := range commonDataMap {
if v > commonDataCount {
commonDataString = k
}
}
var validCommonSignatures []ValidatedNodeSignature
for i := 0; i < len(validSignatures); i++ {
var commitmentRequestResultData CommitmentRequestResultData
ok, err := commitmentRequestResultData.FromString(validSignatures[i].Data)
if !ok || err != nil {
logging.WithField("ok", ok).WithError(err).Error("could not get commitmentRequestResultData from string")
}
stringData := strings.Join([]string{
commitmentRequestResultData.MessagePrefix,
commitmentRequestResultData.TokenCommitment,
commitmentRequestResultData.VerifierIdentifier,
}, pcmn.Delimiter1)
if stringData == commonDataString {
validCommonSignatures = append(validCommonSignatures, validSignatures[i])
}
}
if len(validCommonSignatures) < numberOfThresholdNodes {
return nil, &jsonrpc.Error{Code: -32602, Message: "Internal error", Data: "Not enough valid signatures on the same data, " + strconv.Itoa(len(validCommonSignatures)) + " valid signatures."}
}
commonData := strings.Split(commonDataString, pcmn.Delimiter1)
if len(commonData) != 3 {
return nil, &jsonrpc.Error{Code: -32602, Message: "Internal error", Data: "Could not parse common data"}
}
commonTokenCommitment := commonData[1]
commonVerifierIdentifier := commonData[2]
// Lookup verifier and
// verify that hash of token = tokenCommitment
cleanedToken, err := serviceLibrary.VerifierMethods().CleanToken(commonVerifierIdentifier, parsedVerifierParams.IDToken)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: "Internal error", Data: "Error when cleaning token " + err.Error()}
}
if hex.EncodeToString(secp256k1.Keccak256([]byte(cleanedToken))) != commonTokenCommitment {
return nil, &jsonrpc.Error{Code: -32602, Message: "Internal error", Data: "Token commitment and token are not compatible"}
}
keyIndexes, err := serviceLibrary.ABCIMethods().GetIndexesFromVerifierID(commonVerifierIdentifier, verifierID)
if err != nil {
return nil, &jsonrpc.Error{Code: -32603, Message: "Internal error", Data: fmt.Sprintf("share request could not retrieve keyIndexes: %v", err)}
}
// Add to overall list and valid verifierIDs
for _, index := range keyIndexes {
allKeyIndexes[index.Text(16)] = index
}
pubKey = serviceLibrary.CacheMethods().GetTokenCommitKey(commonVerifierIdentifier, commonTokenCommitment)
allValidVerifierIDs[strings.Join([]string{parsedVerifierParams.VerifierIdentifier, verifierID}, pcmn.Delimiter1)] = true
}
response := ShareRequestResult{}
var allKeyIndexesSorted []big.Int
for _, index := range allKeyIndexes {
allKeyIndexesSorted = append(allKeyIndexesSorted, index)
}
sort.Slice(allKeyIndexesSorted, func(a, b int) bool {
return allKeyIndexesSorted[a].Cmp(&allKeyIndexesSorted[b]) == -1
})
for _, index := range allKeyIndexesSorted {
// check if we have enough validTokens according to Access Structure
pubKeyAccessStructure, err := serviceLibrary.ABCIMethods().RetrieveKeyMapping(index)
if err != nil {
return nil, &jsonrpc.Error{Code: -32603, Message: "Internal error", Data: fmt.Sprintf("could not retrieve access structure: %v", err)}
}
validCount := 0
for verifieridentifier, verifierIDs := range pubKeyAccessStructure.Verifiers {
for _, verifierID := range verifierIDs {
// check against all validVerifierIDs
for verifierStrings := range allValidVerifierIDs {
if strings.Join([]string{verifieridentifier, verifierID}, pcmn.Delimiter1) == verifierStrings {
validCount++
}
}
}
}
si, _, err := serviceLibrary.DatabaseMethods().RetrieveCompletedShare(index)
if err != nil {
return nil, &jsonrpc.Error{Code: -32603, Message: "Internal error", Data: "could not retrieve completed share"}
}
if validCount >= pubKeyAccessStructure.Threshold { // if we have enough authenticators we return Si
keyAssignment := KeyAssignment{
KeyAssignmentPublic: pubKeyAccessStructure,
Share: si.Bytes(),
}
if config.GlobalMutableConfig.GetB("EncryptShares") {
pubKeyHex := "04" + fmt.Sprintf("%064s", pubKey.X.Text(16)) + fmt.Sprintf("%064s", pubKey.Y.Text(16))
encrypted, metadata, err := tronCrypto.Encrypt(pubKeyHex, keyAssignment.Share)
if err != nil {
return nil, &jsonrpc.Error{Code: -32603, Message: "Internal error", Data: fmt.Sprintf("could not encrypt shares with err: %v", err)}
}
if metadata == nil {
return nil, &jsonrpc.Error{Code: -32603, Message: "Internal error", Data: "could not encrypt shares, metadata nil"}
}
keyAssignment.Share = []byte(encrypted)
keyAssignment.Metadata = *metadata
}
response.Keys = append(response.Keys, keyAssignment)
}
}
return response, nil
}
func assignKey(c context.Context, eventBus eventbus.Bus, verifier string, verifierID string) *jsonrpc.Error {
serviceLibrary := NewServiceLibrary(eventBus, "key_assign_handler")
requestContext, requestContextCancel := context.WithTimeout(c, time.Duration(requestTimer)*time.Second)
defer requestContextCancel()
logging.Debug("checking if verifierID is provided")
if verifierID == "" {
return &jsonrpc.Error{Code: -32602, Message: "Input error", Data: "VerifierID is empty"}
}
logging.Debug("checking if verifier is supported")
// check if verifier is valid
verifiers := serviceLibrary.VerifierMethods().ListVerifiers()
found := false
for _, v := range verifiers {
if v == verifier {
found = true
break
}
}
if !found {
return &jsonrpc.Error{Code: -32602, Message: "Input error", Data: "Verifier not supported"}
}
logging.Debug("broadcasting assignment transaction")
// new assignment
// broadcast assignment transaction
assMsg := AssignmentBFTTx{VerifierID: verifierID, Verifier: verifier}
hash, err := serviceLibrary.TendermintMethods().Broadcast(assMsg)
if err != nil {
return &jsonrpc.Error{Code: -32603, Message: "Internal error", Data: "Unable to broadcast: " + err.Error()}
}
// subscribe to updates
logging.Debug("subscribing to updates")
query := tmquery.MustParse("tx.hash='" + hash.String() + "'")
logging.WithField("queryString", query.String()).Debug("BFTWS")
responseCh, err := serviceLibrary.TendermintMethods().RegisterQuery(query.String(), 1)
if err != nil {
logging.WithField("queryString", query.String()).Debug("BFTWS could not register query")
}
var tmpJrpcErr jsonrpc.Error
for {
responseReceived := false
select {
case e := <-responseCh:
if err != nil {
tmpJrpcErr = jsonrpc.Error{Code: -32603, Message: "Internal error", Data: fmt.Sprintf("unable to parse websocket tm response")}
}
logging.WithField("gjson", gjson.GetBytes(e, "query").String()).Debug("BFTWS")
if gjson.GetBytes(e, "query").String() != query.String() {
break
}
parsed := gjson.GetBytes(e, "data")
parsedTwo := gjson.GetBytes([]byte(parsed.Raw), "value")
parsedThree := gjson.GetBytes([]byte(parsedTwo.Raw), "TxResult")
var txResult tmtypes.TxResult
err = bijson.Unmarshal([]byte(parsedThree.Raw), &txResult)
if txResult.Result.Code != 0 {
logging.WithFields(logging.Fields{
"txQuery": query.String(),
"code": txResult.Result.GetCode(),
}).Debug("BFTWS Got response")
break
}
responseReceived = true
case <-requestContext.Done():
tmpJrpcErr = jsonrpc.Error{Code: -32603, Message: "Internal error", Data: fmt.Sprintf("key assignment error: timed out")}
responseReceived = true
}
if responseReceived {
break
}
}
if tmpJrpcErr != (jsonrpc.Error{}) {
return &tmpJrpcErr
}
return nil
}
func retrieveKeysFromVerifierID(c context.Context, eventBus eventbus.Bus, verifier string, verifierID string) ([]KeyAssignItem, *jsonrpc.Error) {
serviceLibrary := NewServiceLibrary(eventBus, "key_assign_handler")
keyIndexes, err := serviceLibrary.ABCIMethods().GetIndexesFromVerifierID(verifier, verifierID)
if err != nil {
return nil, &jsonrpc.Error{Code: -32603, Message: "Internal error", Data: fmt.Sprintf("Unable to retieve keyIndexes error: %v", err)}
}
var keys []KeyAssignItem
for _, index := range keyIndexes {
pk, err := serviceLibrary.DatabaseMethods().RetrieveIndexToPublicKey(index)
if err != nil {
return nil, &jsonrpc.Error{Code: -32603, Message: fmt.Sprintf("Could not find address to key index error: %v", err)}
}
//form address eth
addr := crypto.PointToEthAddress(pk)
keys = append(keys, KeyAssignItem{
KeyIndex: index.Text(16),
PubKeyX: pk.X,
PubKeyY: pk.Y,
Address: addr.String(),
})
}
return keys, nil
}
// assigns a user a secret, returns the same index if the user has been previously assigned
func (h KeyAssignHandler) ServeJSONRPC(c context.Context, params *bijson.RawMessage) (interface{}, *jsonrpc.Error) {
telemetry.IncrementCounter(pcmn.TelemetryConstants.JRPC.KeyAssignCounter, pcmn.TelemetryConstants.JRPC.Prefix)
var p KeyAssignParams
if err := jsonrpc.Unmarshal(params, &p); err != nil {
return nil, err
}
tmpJrpcErr := assignKey(c, h.eventBus, p.Verifier, p.VerifierID)
if tmpJrpcErr != nil {
return nil, tmpJrpcErr
}
keys, tmpJrpcErr := retrieveKeysFromVerifierID(c, h.eventBus, p.Verifier, p.VerifierID)
if tmpJrpcErr != nil {
return nil, tmpJrpcErr
}
return KeyAssignResult{Keys: keys}, nil
}
// Looksup Keys assigned to a Verifier + VerifierID
func (h VerifierLookupHandler) ServeJSONRPC(c context.Context, params *bijson.RawMessage) (interface{}, *jsonrpc.Error) {
telemetry.IncrementCounter(pcmn.TelemetryConstants.JRPC.VerifierLookupCounter, pcmn.TelemetryConstants.JRPC.Prefix)
serviceLibrary := NewServiceLibrary(h.eventBus, "verifier_lookup_handler")
var p VerifierLookupParams
if err := jsonrpc.Unmarshal(params, &p); err != nil {
return nil, err
}
if p.VerifierID == "" {
return nil, &jsonrpc.Error{Code: -32602, Message: "Input error", Data: "VerifierID is empty"}
}
// check if verifier is valid
verifiers := serviceLibrary.VerifierMethods().ListVerifiers()
found := false
for _, verifier := range verifiers {
if verifier == p.Verifier {
found = true
}
}
if !found {
return nil, &jsonrpc.Error{Code: -32602, Message: "Input error", Data: "Verifier not supported"}
}
// retrieve index
keyIndexes, err := serviceLibrary.ABCIMethods().GetIndexesFromVerifierID(p.Verifier, p.VerifierID)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: "Input Error", Data: fmt.Sprintf("Verifier + VerifierID has not yet been assigned %v", err)}
}
// prepare and send response
result := VerifierLookupResult{}
for _, index := range keyIndexes {
publicKeyAss, err := serviceLibrary.ABCIMethods().RetrieveKeyMapping(index)
if err != nil {
return nil, &jsonrpc.Error{Code: -32603, Message: fmt.Sprintf("Could not find address to key index error: %v", err)}
}
pk := publicKeyAss.PublicKey
//form address eth
addr := crypto.PointToEthAddress(pk)
result.Keys = append(result.Keys, VerifierLookupItem{
KeyIndex: index.Text(16),
PubKeyX: pk.X,
PubKeyY: pk.Y,
Address: addr.String(),
})
}
return result, nil
}
// Looksup Verifier + VerifierIDs assigned to a key (access structure)
func (h KeyLookupHandler) ServeJSONRPC(c context.Context, params *bijson.RawMessage) (interface{}, *jsonrpc.Error) {
telemetry.IncrementCounter(pcmn.TelemetryConstants.JRPC.KeyLookupCounter, pcmn.TelemetryConstants.JRPC.Prefix)
serviceLibrary := NewServiceLibrary(h.eventBus, "key_lookup_handler")
var p KeyLookupParams
if err := jsonrpc.Unmarshal(params, &p); err != nil {
return nil, err
}
if p.PubKeyX.Text(16) == "0" || p.PubKeyY.Text(16) == "0" {
return nil, &jsonrpc.Error{Code: -32602, Message: "Input error", Data: "pubkey is empty"}
}
pubKey := common.BigIntToPoint(&p.PubKeyX, &p.PubKeyY)
// prepare and send response
keyIndex, err := serviceLibrary.DatabaseMethods().RetrievePublicKeyToIndex(pubKey)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: "Input error", Data: "no log of public key"}
}
keyAssignmentPublic, err := serviceLibrary.ABCIMethods().RetrieveKeyMapping(keyIndex)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: "Input error", Data: "no key assignment found for public key"}
}
return KeyLookupResult{keyAssignmentPublic}, nil
}
func (h UpdatePublicKeyHandler) ServeJSONRPC(c context.Context, params *bijson.RawMessage) (interface{}, *jsonrpc.Error) {
telemetry.IncrementCounter(pcmn.TelemetryConstants.JRPC.UpdatePublicKeyCounter, pcmn.TelemetryConstants.JRPC.Prefix)
serviceLibrary := NewServiceLibrary(h.eventBus, "update_public_key_handler")
var dealerMessage dealer.Message
if err := jsonrpc.Unmarshal(params, &dealerMessage); err != nil {
return nil, err
}
existingPubKey, err := serviceLibrary.DatabaseMethods().RetrieveIndexToPublicKey(dealerMessage.KeyIndex)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: err.Error(), Data: "error while getting the existing public key"}
}
if !dealerMessage.Validate(existingPubKey) {
return nil, &jsonrpc.Error{Code: -32602, Message: "validation error", Data: "invalid dealer message"}
}
_, err = serviceLibrary.TendermintMethods().Broadcast(dealerMessage)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: err.Error(), Data: "error while doing the tendermint broadcast"}
}
return DealerResult{
Code: 200,
Message: "public key updated",
}, nil
}
func (h UpdateShareHandler) ServeJSONRPC(c context.Context, params *bijson.RawMessage) (interface{}, *jsonrpc.Error) {
telemetry.IncrementCounter(pcmn.TelemetryConstants.JRPC.UpdateShareCounter, pcmn.TelemetryConstants.JRPC.Prefix)
serviceLibrary := NewServiceLibrary(h.eventBus, "update_public_key_handler")
var dealerMessage dealer.Message
if err := jsonrpc.Unmarshal(params, &dealerMessage); err != nil {
return nil, err
}
existingPubKey, err := serviceLibrary.DatabaseMethods().RetrieveIndexToPublicKey(dealerMessage.KeyIndex)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: err.Error(), Data: "error while getting the existing public key"}
}
if !dealerMessage.Validate(existingPubKey) {
return nil, &jsonrpc.Error{Code: -32602, Message: "validation error", Data: "Invalid dealer message"}
}
var updateShareMessage dealer.MsgUpdateShare
err = bijson.Unmarshal(dealerMessage.Data, &updateShareMessage)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: err.Error(), Data: "error while unmarshalling dealerMessage.Data into dealer.MsgUpdateShare"}
}
if !updateShareMessage.Validate() {
return nil, &jsonrpc.Error{Code: -32602, Message: "validation error", Data: "invalid dealer.MsgUpdateShare"}
}
err = serviceLibrary.DatabaseMethods().StoreCompletedPSSShare(updateShareMessage.KeyIndex, updateShareMessage.Si, updateShareMessage.Siprime)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: err.Error(), Data: "error while storing completed PSS share"}
}
telemetry.IncGauge("dealer_update_share")
return DealerResult{
Code: 200,
Message: "share updated",
}, nil
}
func (h UpdateCommitmentHandler) ServeJSONRPC(c context.Context, params *bijson.RawMessage) (interface{}, *jsonrpc.Error) {
telemetry.IncrementCounter(pcmn.TelemetryConstants.JRPC.UpdateCommitmentCounter, pcmn.TelemetryConstants.JRPC.Prefix)
serviceLibrary := NewServiceLibrary(h.eventBus, "update_public_key_handler")
var dealerMessage dealer.Message
if err := jsonrpc.Unmarshal(params, &dealerMessage); err != nil {
return nil, err
}
existingPubKey, err := serviceLibrary.DatabaseMethods().RetrieveIndexToPublicKey(dealerMessage.KeyIndex)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: err.Error(), Data: "error while getting the existing public key"}
}
if !dealerMessage.Validate(existingPubKey) {
return nil, &jsonrpc.Error{Code: -32602, Message: "public Key Validation error", Data: "invalid public key"}
}
var updateCommitmentMessage dealer.MsgUpdateCommitment
err = bijson.Unmarshal(dealerMessage.Data, &updateCommitmentMessage)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: err.Error(), Data: "error while unmarshalling the dealerMessage.Data into MsgUpdateCommitment"}
}
if !updateCommitmentMessage.Validate() {
return nil, &jsonrpc.Error{Code: -32602, Message: "validation error", Data: "invalid dealer.MsgUpdateCommitment"}
}
err = serviceLibrary.DatabaseMethods().StorePSSCommitmentMatrix(updateCommitmentMessage.KeyIndex, updateCommitmentMessage.Commitment)
if err != nil {
return nil, &jsonrpc.Error{Code: -32602, Message: err.Error(), Data: "error while storing the commitment matrix"}
}
telemetry.IncGauge("dealer_update_commitment")
return DealerResult{
Code: 200,
Message: "commitment updated!",
}, nil
}