forked from drand/drand
-
Notifications
You must be signed in to change notification settings - Fork 1
/
public.go
103 lines (94 loc) · 2.49 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
package main
import (
"github.com/drand/drand/core"
"github.com/drand/drand/net"
"github.com/drand/drand/protobuf/drand"
"github.com/nikkolasg/slog"
"github.com/urfave/cli"
)
func getPrivateCmd(c *cli.Context) error {
if !c.Args().Present() {
slog.Fatal("Get private takes a group file as argument.")
}
defaultManager := net.NewCertManager()
if c.IsSet("tls-cert") {
defaultManager.Add(c.String("tls-cert"))
}
ids := getNodes(c)
client := core.NewGrpcClientFromCert(defaultManager)
var resp []byte
var err error
for _, public := range ids {
resp, err = client.Private(public)
if err == nil {
slog.Infof("drand: successfully retrieved private randomness "+
"from %s", public.Addr)
break
}
slog.Infof("drand: error contacting node %s: %s", public.Addr, err)
}
if resp == nil {
slog.Fatalf("drand: zero successful contacts with nodes")
}
type private struct {
Randomness []byte
}
printJSON(&private{resp})
return nil
}
func getPublicRandomness(c *cli.Context) error {
if !c.Args().Present() {
slog.Fatal("Get public command takes a group file as argument.")
}
defaultManager := net.NewCertManager()
if c.IsSet("tls-cert") {
defaultManager.Add(c.String("tls-cert"))
}
ids := getNodes(c)
group := getGroup(c)
if group.PublicKey == nil {
slog.Fatalf("drand: group file must contain the distributed public key!")
}
public := group.PublicKey
client := core.NewGrpcClientFromCert(defaultManager)
isTLS := !c.Bool("tls-disable")
var resp *drand.PublicRandResponse
var err error
for _, id := range ids {
if c.IsSet("round") {
resp, err = client.Public(id.Addr, public, isTLS, c.Int("round"))
} else {
resp, err = client.LastPublic(id.Addr, public, isTLS)
}
if err == nil {
slog.Infof("drand: public randomness retrieved from %s", id.Addr)
break
}
slog.Printf("drand: could not get public randomness from %s: %s", id.Addr, err)
}
printJSON(resp)
return nil
}
func getCokeyCmd(c *cli.Context) error {
defaultManager := net.NewCertManager()
if c.IsSet("tls-cert") {
defaultManager.Add(c.String("tls-cert"))
}
ids := getNodes(c)
client := core.NewGrpcClientFromCert(defaultManager)
var dkey *drand.DistKeyResponse
var err error
for _, id := range ids {
dkey, err = client.DistKey(id.Addr, !c.Bool("tls-disable"))
if err == nil {
break
}
slog.Printf("drand: error fetching distributed key from %s : %s",
id.Addr, err)
}
if dkey == nil {
slog.Fatalf("drand: can't retrieve dist. key from all nodes")
}
printJSON(dkey)
return nil
}