forked from gtfierro/wave
-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.go
92 lines (85 loc) · 2.29 KB
/
log.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
package main
import (
"context"
"fmt"
"time"
"github.com/gogo/protobuf/proto"
"github.com/golang/glog"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"github.com/google/trillian"
ktestonly "github.com/google/trillian/crypto/keys/testonly"
"github.com/google/trillian/crypto/keyspb"
spb "github.com/google/trillian/crypto/sigpb"
_ "github.com/google/trillian/merkle/coniks"
"github.com/google/trillian/testonly"
"google.golang.org/grpc"
)
const logServer = "localhost:8092"
func mustMarshalAny(pb proto.Message) *any.Any {
value, err := ptypes.MarshalAny(pb)
if err != nil {
panic(err)
}
return value
}
var logconn *grpc.ClientConn
var LogTree *trillian.Tree
var vlog trillian.TrillianLogClient
var logId int64
func initlog() {
LogTree = &trillian.Tree{
TreeState: trillian.TreeState_ACTIVE,
TreeType: trillian.TreeType_LOG,
HashStrategy: trillian.HashStrategy_RFC6962_SHA256,
HashAlgorithm: spb.DigitallySigned_SHA256,
SignatureAlgorithm: spb.DigitallySigned_ECDSA,
DisplayName: "Llamas Log",
Description: "Registry of publicly-owned llamas",
PrivateKey: mustMarshalAny(&keyspb.PrivateKey{
Der: ktestonly.MustMarshalPrivatePEMToDER(testonly.DemoPrivateKey, testonly.DemoPrivateKeyPass),
}),
PublicKey: &keyspb.PublicKey{
Der: ktestonly.MustMarshalPublicPEMToDER(testonly.DemoPublicKey),
},
MaxRootDuration: ptypes.DurationProto(0 * time.Millisecond),
}
var err error
ctx := context.Background()
logconn, err = grpc.Dial(logServer, grpc.WithInsecure())
if err != nil {
glog.Fatal(err)
}
adm := trillian.NewTrillianAdminClient(logconn)
respct, err := adm.CreateTree(ctx, &trillian.CreateTreeRequest{
Tree: LogTree,
})
if err != nil {
panic(err)
}
treeId := respct.TreeId
LogTree.TreeId = treeId
fmt.Printf("tree ID is %d\n", treeId)
vlog = trillian.NewTrillianLogClient(logconn)
_, err = vlog.InitLog(ctx, &trillian.InitLogRequest{
LogId: treeId,
})
if err != nil {
panic(err)
}
logId = treeId
}
func addToLog(value []byte) int64 {
ctx := context.Background()
llf := &trillian.LogLeaf{
LeafValue: value,
}
resp, err := vlog.QueueLeaf(ctx, &trillian.QueueLeafRequest{
LogId: logId,
Leaf: llf,
})
if err != nil {
panic(err)
}
return resp.QueuedLeaf.Leaf.LeafIndex
}