forked from keys-pub/keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sigchains.go
203 lines (183 loc) · 4.24 KB
/
sigchains.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
package keys
import (
"context"
"encoding/json"
"strings"
"github.com/keys-pub/keys/dstore"
"github.com/keys-pub/keys/tsutil"
"github.com/pkg/errors"
)
// Sigchains stores sigchains.
type Sigchains struct {
ds dstore.Documents
clock tsutil.Clock
}
// NewSigchains creates a Sigchains from Documents.
func NewSigchains(ds dstore.Documents) *Sigchains {
return &Sigchains{
ds: ds,
clock: tsutil.NewClock(),
}
}
// SetClock to use a custom time.Now.
func (s *Sigchains) SetClock(clock tsutil.Clock) {
s.clock = clock
}
// KIDs returns all key ids.
func (s *Sigchains) KIDs() ([]ID, error) {
iter, err := s.ds.DocumentIterator(context.TODO(), "sigchain", dstore.NoData())
if err != nil {
return nil, err
}
ids := NewIDSet()
for {
doc, err := iter.Next()
if err != nil {
return nil, err
}
if doc == nil {
break
}
pc := dstore.PathLast(doc.Path)
str := strings.Split(pc, "-")[0]
id, err := ParseID(str)
if err != nil {
return nil, errors.Wrapf(err, "invalid path %q", doc.Path)
}
ids.Add(id)
}
iter.Release()
return ids.IDs(), nil
}
// Save sigchain.
func (s *Sigchains) Save(sc *Sigchain) error {
if len(sc.Statements()) == 0 {
return errors.Errorf("failed to save sigchain: no statements")
}
for _, st := range sc.Statements() {
b, err := st.Bytes()
if err != nil {
return err
}
if st.Seq <= 0 {
return errors.Errorf("statement sequence missing")
}
if err := s.ds.Set(context.TODO(), dstore.Path("sigchain", StatementID(st.KID, st.Seq)), dstore.Data(b)); err != nil {
return err
}
}
if err := s.Index(sc.KID()); err != nil {
return err
}
return nil
}
func statementFromDocument(doc *dstore.Document) (*Statement, error) {
var st Statement
if err := json.Unmarshal(doc.Data(), &st); err != nil {
return nil, err
}
return &st, nil
}
// Sigchain returns sigchain for key.
func (s *Sigchains) Sigchain(kid ID) (*Sigchain, error) {
// logger.Debugf("Loading sigchain %s", kid)
iter, err := s.ds.DocumentIterator(context.TODO(), "sigchain", dstore.Prefix(kid.String()))
if err != nil {
return nil, err
}
sc := NewSigchain(kid)
for {
doc, err := iter.Next()
if err != nil {
return nil, err
}
if doc == nil {
break
}
st, err := statementFromDocument(doc)
if err != nil {
return nil, err
}
if err := sc.Add(st); err != nil {
return nil, err
}
}
iter.Release()
return sc, nil
}
func (s *Sigchains) sigchainPaths(kid ID) ([]string, error) {
iter, err := s.ds.DocumentIterator(context.TODO(), "sigchain", dstore.Prefix(kid.String()), dstore.NoData())
if err != nil {
return nil, err
}
defer iter.Release()
paths := make([]string, 0, 100)
for {
doc, err := iter.Next()
if err != nil {
return nil, err
}
if doc == nil {
break
}
paths = append(paths, doc.Path)
}
return paths, nil
}
// Delete sigchain.
func (s *Sigchains) Delete(kid ID) (bool, error) {
paths, err := s.sigchainPaths(kid)
if err != nil {
return false, err
}
if len(paths) == 0 {
return false, nil
}
for _, path := range paths {
if _, err := s.ds.Delete(context.TODO(), path); err != nil {
return false, err
}
}
// TODO: Delete reverse key lookup?
return true, nil
}
// Exists returns true if sigchain exists.
func (s *Sigchains) Exists(kid ID) (bool, error) {
return s.ds.Exists(context.TODO(), dstore.Path("sigchain", StatementID(kid, 1)))
}
// indexRKL is collection for reverse key lookups.
const indexRKL = "rkl"
// Lookup key.
// Returns key associated with the specified key.
func (s *Sigchains) Lookup(kid ID) (ID, error) {
path := dstore.Path(indexRKL, kid.String())
doc, err := s.ds.Get(context.TODO(), path)
if err != nil {
return "", err
}
if doc == nil {
return "", nil
}
rkid, err := ParseID(string(doc.Data()))
if err != nil {
return "", err
}
return rkid, nil
}
// Index key.
// Adds reverse key lookup for EdX25519 to X25519 public key.
func (s *Sigchains) Index(key Key) error {
if key.Type() == EdX25519 {
pk, err := NewEdX25519PublicKeyFromID(key.ID())
if err != nil {
return err
}
rk := pk.X25519PublicKey()
rklPath := dstore.Path(indexRKL, rk.ID())
// TODO: Store this as a string not as data.
if err := s.ds.Set(context.TODO(), rklPath, dstore.Data([]byte(key.ID().String()))); err != nil {
return err
}
}
return nil
}