forked from immesys/wave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
151 lines (140 loc) · 3.45 KB
/
db.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
package main
import (
"bytes"
"context"
"encoding/gob"
"encoding/pem"
"fmt"
"time"
"github.com/dgraph-io/badger"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"github.com/google/trillian"
"github.com/google/trillian/crypto/keyspb"
spb "github.com/google/trillian/crypto/sigpb"
_ "github.com/google/trillian/merkle/rfc6962"
)
const OurPublicKey = `-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEmTz0jNtdPnob3U8uylM5PORUJPw2
9VEU8V68V8FtlxFxuuU6MFHzN5/3XnWCeJ0xJ1Uabk1r/eS0H7aWOksMNA==
-----END PUBLIC KEY-----`
const OurPrivateKey = `-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgaK0u/I9YXTE7Yxb6
uGK3vX/KzWQVqCpqctv4hhAWEcuhRANCAASZPPSM210+ehvdTy7KUzk85FQk/Db1
URTxXrxXwW2XEXG65TowUfM3n/dedYJ4nTEnVRpuTWv95LQftpY6Sww0
-----END PRIVATE KEY-----`
func mustMarshalAny(pb proto.Message) *any.Any {
value, err := ptypes.MarshalAny(pb)
if err != nil {
panic(err)
}
return value
}
func (a *adt) initmap() int64 {
ctx := context.Background()
pubk, _ := pem.Decode([]byte(OurPublicKey))
privk, _ := pem.Decode([]byte(OurPrivateKey))
var err error
MapTree := &trillian.Tree{
TreeState: trillian.TreeState_ACTIVE,
TreeType: trillian.TreeType_MAP,
HashStrategy: trillian.HashStrategy_TEST_MAP_HASHER,
HashAlgorithm: spb.DigitallySigned_SHA256,
SignatureAlgorithm: spb.DigitallySigned_ECDSA,
DisplayName: "WAVE Storage map",
Description: "Storage of attestations and entities for WAVE",
PrivateKey: mustMarshalAny(&keyspb.PrivateKey{
Der: privk.Bytes,
}),
PublicKey: &keyspb.PublicKey{
Der: pubk.Bytes,
},
MaxRootDuration: ptypes.DurationProto(0 * time.Millisecond),
}
respct, err := a.adminClient.CreateTree(ctx, &trillian.CreateTreeRequest{
Tree: MapTree,
})
if err != nil {
panic(err)
}
_, err = a.mapClient.InitMap(ctx, &trillian.InitMapRequest{
MapId: respct.TreeId,
})
if err != nil {
panic(err)
}
return respct.TreeId
}
func (a *adt) LoadStateFromDB() {
err := a.db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte("state"))
var st *state
if err == badger.ErrKeyNotFound {
st = a.defaultstate()
} else {
val, err := item.Value()
if err != nil {
return err
}
st = &state{}
buf := bytes.NewBuffer(val)
err = gob.NewDecoder(buf).Decode(st)
if err != nil {
return err
}
}
a.state = st
return nil
})
if err != nil {
panic(err)
}
}
func (a *adt) haveWeVerified(roothash []byte) bool {
found := false
err := a.db.View(func(txn *badger.Txn) error {
hash := []byte(fmt.Sprintf("verifiedroots/%x", roothash))
_, err := txn.Get(hash)
if err == nil {
found = true
} else if err != badger.ErrKeyNotFound {
return err
}
return nil
})
if err != nil {
panic(err)
}
return found
}
func (a *adt) storeVerified(roothash []byte) {
err := a.db.Update(func(txn *badger.Txn) error {
hash := []byte(fmt.Sprintf("verifiedroots/%x", roothash))
return txn.Set(hash, []byte("1"))
})
if err != nil {
panic(err)
}
}
func (a *adt) defaultstate() *state {
treeid := a.initmap()
return &state{
MapTreeId: treeid,
OpLogIndex: 0,
RootLogIndex: -1,
}
}
func (a *adt) SaveStateToDB() {
buf := bytes.Buffer{}
err := gob.NewEncoder(&buf).Encode(a.state)
if err != nil {
panic(err)
}
err = a.db.Update(func(txn *badger.Txn) error {
return txn.Set([]byte("state"), buf.Bytes())
})
if err != nil {
panic(err)
}
}