This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify_image.go
207 lines (173 loc) · 5.01 KB
/
verify_image.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
204
205
206
207
/*
* proofable-image
* Copyright (C) 2020 Southbank Software Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* @Author: guiguan
* @Date: 2020-07-30T00:25:21+10:00
* @Last modified by: guiguan
* @Last modified time: 2020-08-31T16:32:46+10:00
*/
package main
import (
"context"
"encoding/json"
"errors"
"image"
"io"
"os"
"time"
"github.com/SouthbankSoftware/proofable/pkg/api"
"github.com/SouthbankSoftware/proofable/pkg/colorcli"
anchorPB "github.com/SouthbankSoftware/proofable/pkg/protos/anchor"
apiPB "github.com/SouthbankSoftware/proofable/pkg/protos/api"
"google.golang.org/grpc/status"
)
func unpackGRPCErr(err error) error {
if s, ok := status.FromError(err); ok {
return errors.New(s.Message())
}
return err
}
func (s *appState) verifyImage(ctx context.Context, prfFile io.Reader) {
colorcli.Printf("Verifying %s against image certificate %s...\n",
colorcli.Green(s.imagePath), colorcli.Green(s.imgcertPath))
var (
verifiable = false
triePf *apiPB.TrieProof
count = 0
)
err := api.WithImportedTrie(ctx, s.apiClient, "", s.imgcertPath, func(id, root string) error {
tp, err := api.GetTrieProof(ctx, s.apiClient, id, "", root)
if err != nil {
return err
}
triePf = tp
var dotOutputPath string
if s.outputDotGraph {
dotOutputPath = s.imgcertPath + ".dot"
}
kvCH, rpCH, errCH := api.VerifyTrieProof(ctx, s.apiClient, id, tp.GetId(),
true, dotOutputPath)
kvCH = api.InterceptKeyValueStream(ctx, kvCH,
api.StripCompoundKeyAnchorTriePart)
metadata := &imageTrieRootMetadata{}
// read metadata
err = api.UnmarshalFromKeyValues(
api.MetadataPrefix,
func() (kv *apiPB.KeyValue, er error) {
keyValue, ok := <-kvCH
if !ok {
er = errors.New("stream is closed")
return
}
kv = keyValue
return
},
metadata,
)
if err != nil {
return err
}
s.imgcertSize = metadata.ImageSize
for kv := range kvCH {
count++
p := image.Point{}
err := json.Unmarshal(kv.Key, &p)
if err != nil {
return err
}
hash, err := s.getBoxImageHashAt(p)
if err != nil {
return err
}
// return nil
expectedHash, err := bytesToImageHash(kv.Value)
if err != nil {
return err
}
dist, err := hash.Distance(expectedHash)
if err != nil {
return err
}
if uint64(dist) > s.distanceTolerance {
colorcli.Faillnf("pixel box mismatched at %s with distance %s",
colorcli.Red(p), colorcli.Red(dist))
s.mismatches = append(s.mismatches, p)
}
}
err = <-errCH
if err != nil {
return err
}
verifiable = true
rp := <-rpCH
if !rp.GetVerified() {
return errors.New(rp.GetError())
}
return nil
})
if err != nil {
if verifiable {
colorcli.Faillnf("the image certificate at %s with a root hash of %s is falsified: %s",
colorcli.Red(s.imgcertPath),
colorcli.Red(triePf.GetProofRoot()),
unpackGRPCErr(err))
return
}
colorcli.Faillnf("the image certificate at %s is unverifiable: %s",
colorcli.Red(s.imgcertPath),
unpackGRPCErr(err))
os.Exit(1)
}
colorcli.Oklnf("the image certificate at %s with %v pixel boxes and a root hash of %s is anchored to %s in block %v with transaction %s at %s, which can be viewed at %s",
colorcli.Green(s.imgcertPath),
colorcli.Green(count),
colorcli.Green(triePf.GetProofRoot()),
colorcli.Green(triePf.GetAnchorType()),
colorcli.Green(anchorPB.GetBlockNumberString(
triePf.GetAnchorType().String(),
triePf.GetBlockTime(),
triePf.GetBlockTimeNano(),
triePf.GetBlockNumber())),
colorcli.Green(triePf.GetTxnId()),
colorcli.Green(time.Unix(
int64(triePf.GetBlockTime()),
int64(triePf.GetBlockTimeNano())).Format(time.UnixDate)),
colorcli.Green(triePf.GetTxnUri()))
// also add area not covered by certificate as mismatches
imageSize := s.imageSize()
numX, numY := getBoxNumX(imageSize.X), getBoxNumY(imageSize.Y)
numPfX, numPfY :=
getBoxNumX(s.imgcertSize.X), getBoxNumY(s.imgcertSize.Y)
for i := numPfX; i < numX; i++ {
for j := 0; j < numY; j++ {
s.mismatches = append(s.mismatches, image.Pt(i, j))
}
}
l := min(numPfX, numX)
for j := numPfY; j < numY; j++ {
for i := 0; i < l; i++ {
s.mismatches = append(s.mismatches, image.Pt(i, j))
}
}
if l := len(s.mismatches); l > 0 {
colorcli.Faillnf("%s is fasified: found %s image mismatches",
colorcli.Red(s.imagePath), colorcli.Red(l))
return
}
colorcli.Passlnf("%s is verified", colorcli.Green(s.imagePath))
}