-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathverifyincluded.go
More file actions
213 lines (168 loc) · 6.06 KB
/
verifyincluded.go
File metadata and controls
213 lines (168 loc) · 6.06 KB
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
204
205
206
207
208
209
210
211
212
213
package veracity
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"strings"
"github.com/datatrails/go-datatrails-logverification/logverification/app"
"github.com/datatrails/go-datatrails-merklelog/massifs"
"github.com/datatrails/go-datatrails-merklelog/mmr"
"github.com/urfave/cli/v2"
veracityapp "github.com/datatrails/veracity/app"
)
var (
ErrVerifyInclusionFailed = errors.New("the entry is not in the log")
ErrUncommittedEvents = errors.New("one or more events did not have record of their inclusion in the log")
)
const (
skipUncommittedFlagName = "skip-uncommitted"
)
func proofPath(proof [][]byte) string {
var hexProof []string
for _, node := range proof {
hexProof = append(hexProof, fmt.Sprintf("%x", node))
}
return fmt.Sprintf("[%s]", strings.Join(hexProof, ", "))
}
// verifyEvent is an example function of how to verify the inclusion of a datatrails event using the mmr and massifs modules
func verifyEvent(
event *app.AppEntry, logTenant string, mmrEntry []byte, massifHeight uint8, massifGetter MassifGetter,
) ([][]byte, error) {
// Get the mmrIndex from the request and then compute the massif
// it implies based on the massifHeight command line option.
mmrIndex := event.MMRIndex()
massifIndex := massifs.MassifIndexFromMMRIndex(massifHeight, mmrIndex)
// read the massif blob
massif, err := massifGetter.GetMassif(context.Background(), logTenant, massifIndex)
if err != nil {
return nil, err
}
mmrSize := massif.RangeCount()
proof, err := mmr.InclusionProof(&massif, mmrSize, mmrIndex)
if err != nil {
return nil, err
}
// Note: we verify against the mmrSize of the massif which
// includes the event. Future work can deepen this to include
// discovery of the log head, and or verification against a
// sealed MMRSize.
verified, err := mmr.VerifyInclusion(&massif, sha256.New(), mmrSize, mmrEntry, mmrIndex, proof)
if verified {
return proof, nil
}
return nil, fmt.Errorf("%w: %v", ErrVerifyInclusionFailed, err)
}
// NewVerifyIncludedCmd verifies inclusion of a DataTrails event in the tenants Merkle Log
//
//nolint:gocognit
func NewVerifyIncludedCmd() *cli.Command {
return &cli.Command{
Name: "verify-included",
Aliases: []string{"included"},
Usage: `verify the inclusion of an event, or list of events, in the tenant's merkle log.
The event response data from the DataTrails get event or list event queries can be provided directly.
See the README for example use.
Note: for publicly attested events, or shared protected events, you must use --tenant. Otherwise, the tenant is inferred from the event data.
`,
Flags: []cli.Flag{
&cli.BoolFlag{Name: skipUncommittedFlagName, Value: false},
},
Action: func(cCtx *cli.Context) error {
cmd := &CmdCtx{}
var err error
// This command uses the structured logger for all optional output.
// Output not explicitly printed is silenced by default.
if err = cfgLogging(cmd, cCtx); err != nil {
return err
}
log := func(m string, args ...any) {
cmd.log.Infof(m, args...)
}
tenantIdentity := cCtx.String("tenant")
if tenantIdentity != "" {
log("verifying for tenant: %s", tenantIdentity)
} else {
log("verifying protected events for the event creator")
}
// If we are reading the massif log locally, the log path is the
// data-local path. The reader does the right thing regardless of
// whether the option is a directory or a file.
// verifyEvent defaults it to tenantIdentity for the benefit of the remote reader implementation
tenantLogPath := cCtx.String("data-local")
if tenantLogPath == "" {
tenantLogPath = tenantIdentity
}
appData, err := veracityapp.ReadAppData(cCtx.Args().Len() == 0, cCtx.Args().Get(0))
if err != nil {
return err
}
verifiableLogEntries, err := veracityapp.AppDataToVerifiableLogEntries(appData, tenantIdentity)
if err != nil {
return err
}
if err = cfgMassifReader(cmd, cCtx); err != nil {
return err
}
var countNotCommitted int
var countVerifyFailed int
previousMassifIndex := uint64(0)
var massifContext *massifs.MassifContext = nil
for _, event := range verifiableLogEntries {
leafIndex := mmr.LeafIndex(event.MMRIndex())
// get the massif index for the event event
massifIndex := massifs.MassifIndexFromMMRIndex(cmd.massifHeight, event.MMRIndex())
// find the log tenant path if not provided
if tenantLogPath == "" {
var err error
tenantLogPath, err = event.LogTenant()
if err != nil {
return err
}
}
// check if we need this event is part of a different massif than the previous event
//
// if it is, we get the new massif
if massifContext == nil || massifIndex != previousMassifIndex {
massif, err := cmd.massifReader.GetMassif(cCtx.Context, tenantLogPath, massifIndex)
if err != nil {
return err
}
massifContext = &massif
}
verified, err := event.VerifyInclusion(massifContext)
// We keep going if the error is a verification failure, as
// this supports reporting "gaps". All other errors are
// immediately terminal
if errors.Is(err, mmr.ErrVerifyInclusionFailed) || !verified {
countVerifyFailed += 1
log("XX|%d %d\n", event.MMRIndex(), leafIndex)
continue
}
// all other errors immediately terminal
if err != nil {
return err
}
proof, err := event.Proof(massifContext)
if err != nil {
return err
}
log("OK|%d %d|%s", event.MMRIndex(), leafIndex, proofPath(proof))
previousMassifIndex = massifIndex
}
if countVerifyFailed != 0 {
if len(verifiableLogEntries) == 1 {
return fmt.Errorf("%w. for tenant %s", ErrVerifyInclusionFailed, tenantIdentity)
}
return fmt.Errorf("%w. for tenant %s", ErrVerifyInclusionFailed, tenantIdentity)
}
if countNotCommitted > 0 {
if len(verifiableLogEntries) == 1 {
return fmt.Errorf("%w. not committed: %d", ErrUncommittedEvents, countNotCommitted)
}
return fmt.Errorf("%w. %d events of %d were not committed", ErrUncommittedEvents, countNotCommitted, len(verifiableLogEntries))
}
return nil
},
}
}