-
Notifications
You must be signed in to change notification settings - Fork 107
/
public.go
137 lines (124 loc) 路 3.28 KB
/
public.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
package drand
import (
"encoding/hex"
"errors"
"fmt"
gonet "net"
"os"
"github.com/drand/drand/chain"
"github.com/drand/drand/client"
"github.com/drand/drand/client/grpc"
"github.com/drand/drand/core"
"github.com/drand/drand/net"
"github.com/urfave/cli/v2"
)
func getPrivateCmd(c *cli.Context) error {
if !c.Args().Present() {
return errors.New("get private takes a group file as argument")
}
defaultManager := net.NewCertManager()
if c.IsSet("tls-cert") {
if err := defaultManager.Add(c.String("tls-cert")); err != nil {
return err
}
}
ids, err := getNodes(c)
if err != nil {
return err
}
grpcClient := core.NewGrpcClientFromCert(defaultManager)
var resp []byte
for _, public := range ids {
resp, err = grpcClient.Private(public.Identity)
if err == nil {
fmt.Fprintf(output, "drand: successfully retrieved private randomness "+
"from %s", public.Addr)
break
}
fmt.Fprintf(output, "drand: error contacting node %s: %s", public.Addr, err)
}
if resp == nil {
return errors.New("zero successful contacts with nodes")
}
type private struct {
Randomness []byte
}
return printJSON(&private{resp})
}
func getPublicRandomness(c *cli.Context) error {
if !c.Args().Present() {
return errors.New("get public command takes a group file as argument")
}
certPath := ""
if c.IsSet(tlsCertFlag.Name) {
certPath = c.String(tlsCertFlag.Name)
}
ids, err := getNodes(c)
if err != nil {
return err
}
group, err := getGroup(c)
if err != nil {
return err
}
if group.PublicKey == nil {
return errors.New("drand: group file must contain the distributed public key")
}
var resp client.Result
var foundCorrect bool
for _, id := range ids {
grpcClient, err := grpc.New(id.Addr, certPath, !id.TLS)
if err != nil {
fmt.Fprintf(os.Stderr, "drand: could not connect to %s: %s", id.Addr, err)
break
}
resp, err = grpcClient.Get(c.Context, uint64(c.Int(roundFlag.Name)))
if err == nil {
foundCorrect = true
if c.Bool(verboseFlag.Name) {
fmt.Fprintf(output, "drand: public randomness retrieved from %s", id.Addr)
}
break
}
fmt.Fprintf(os.Stderr, "drand: could not get public randomness from %s: %s", id.Addr, err)
}
if !foundCorrect {
return errors.New("drand: could not verify randomness")
}
return printJSON(resp)
}
func getChainInfo(c *cli.Context) error {
var grpcClient = core.NewGrpcClient()
if c.IsSet(tlsCertFlag.Name) {
defaultManager := net.NewCertManager()
certPath := c.String(tlsCertFlag.Name)
if err := defaultManager.Add(certPath); err != nil {
return err
}
grpcClient = core.NewGrpcClientFromCert(defaultManager)
}
var ci *chain.Info
for _, addr := range c.Args().Slice() {
_, _, err := gonet.SplitHostPort(addr)
if err != nil {
return fmt.Errorf("invalid address given: %s", err)
}
ci, err = grpcClient.ChainInfo(net.CreatePeer(addr, !c.Bool("tls-disable")))
if err == nil {
break
}
fmt.Fprintf(os.Stderr, "drand: error fetching distributed key from %s : %s",
addr, err)
}
if ci == nil {
return errors.New("drand: can't retrieve dist. key from all nodes")
}
return printChainInfo(c, ci)
}
func printChainInfo(c *cli.Context, ci *chain.Info) error {
if c.Bool(hashOnly.Name) {
fmt.Fprintf(output, "%s\n", hex.EncodeToString(ci.Hash()))
return nil
}
return printJSON(ci.ToProto())
}