1 // Package verify decides whether an atomic swap is safe to proceed with, from bytes the caller
2 // already has plus facts that must come from outside.
3 //
4 // The split is the point. Local checks never touch a network: they parse the counterparty's
5 // transactions, rebuild digests, check signatures and scripts, and compare every commitment with
6 // the signed terms. External facts are the handful of things only a chain can answer, and they
7 // come from several independent sources with agreement, never from one.
8 //
9 // The threat this is built for is a swap counterparty who also runs, or colludes with, the
10 // gateway that supplies chain data: it can show a real transaction on a chain it controls, or
11 // claim a transaction exists that does not. A verdict therefore fails closed, and the caller must
12 // pass every check before an irreversible step.
13 package verify
14 15 import (
16 "encoding/hex"
17 "errors"
18 "fmt"
19 "sort"
20 "strings"
21 )
22 23 // Tier says where a check's truth comes from.
24 type Tier string
25 26 const (
27 // TierLocal is a check that needs no network: bytes in, answer out.
28 TierLocal Tier = "local"
29 // TierExternal is a check that needs chain facts, which only a source can supply.
30 TierExternal Tier = "external"
31 )
32 33 // Status is one check's outcome.
34 type Status string
35 36 const (
37 // Pass means the check was performed and the claim holds.
38 Pass Status = "pass"
39 // Warn means the claim holds but something about it deserves the user's attention.
40 Warn Status = "warn"
41 // Fail means the claim is false, or the check could not be run on the data supplied.
42 Fail Status = "fail"
43 // Unverifiable means the fact could not be established, which is not the same as a failure:
44 // the UI lists these separately and the gate still refuses.
45 Unverifiable Status = "unverifiable"
46 )
47 48 // Verdict is the whole result. It is never "pass" while any required check is not a Pass.
49 type Verdict string
50 51 const (
52 VerdictPass Verdict = "pass"
53 VerdictWarn Verdict = "warn"
54 VerdictFail Verdict = "fail"
55 VerdictUnverifiable Verdict = "unverifiable"
56 )
57 58 // Check is one named result.
59 type Check struct {
60 // Name is a stable identifier, not a sentence.
61 Name string `json:"name"`
62 // Tier is local or external.
63 Tier Tier `json:"tier"`
64 // Status is the outcome.
65 Status Status `json:"status"`
66 // Detail says what was compared, in the operator's words.
67 Detail string `json:"detail"`
68 // Sources names the external sources that agreed, when the check is external.
69 Sources []string `json:"sources,omitempty"`
70 }
71 72 // Report is everything a caller and a UI need.
73 type Report struct {
74 Verdict Verdict `json:"verdict"`
75 Checks []Check `json:"checks"`
76 // SourcesUsed is every external source consulted.
77 SourcesUsed []string `json:"sources_used"`
78 // Agreement is how many sources agreed on the facts that mattered.
79 Agreement int `json:"agreement"`
80 // Quorum is how many agreement was required.
81 Quorum int `json:"quorum"`
82 // Absence reports what "not found" meant, including the sample and whether the sources
83 // looked independent.
84 Absence []Absence `json:"absence,omitempty"`
85 // Independence is the cross-source check.
86 Independence Independence `json:"independence"`
87 // Notes carry the residual leaks and any advice.
88 Notes []string `json:"notes,omitempty"`
89 }
90 91 // Unverified lists the failed and unverifiable checks separately, which is how the UI must show
92 // them: a failed claim is evidence against the trade, an unverifiable one is missing evidence.
93 func (r *Report) Unverified() (failed, unverifiable []Check) {
94 for _, check := range r.Checks {
95 switch check.Status {
96 case Fail:
97 failed = append(failed, check)
98 case Unverifiable:
99 unverifiable = append(unverifiable, check)
100 }
101 }
102 103 return failed, unverifiable
104 }
105 106 // settle recomputes the verdict from the checks, so a caller cannot set a verdict that contradicts
107 // its checks.
108 func (r *Report) settle() {
109 verdict := VerdictPass
110 111 for _, check := range r.Checks {
112 switch check.Status {
113 case Fail:
114 verdict = VerdictFail
115 case Unverifiable:
116 if verdict != VerdictFail {
117 verdict = VerdictUnverifiable
118 }
119 case Warn:
120 if verdict == VerdictPass {
121 verdict = VerdictWarn
122 }
123 }
124 }
125 126 r.Verdict = verdict
127 }
128 129 // Local runs a local check and records it.
130 func (r *Report) Local(name string, detail string, err error) {
131 status := Pass
132 133 if err != nil {
134 status = Fail
135 detail = strings.TrimSpace(detail + ": " + err.Error())
136 }
137 138 r.Checks = append(r.Checks, Check{Name: name, Tier: TierLocal, Status: status, Detail: detail})
139 r.settle()
140 }
141 142 // LocalFail records a local check that failed.
143 func (r *Report) LocalFail(name, detail string) {
144 r.Checks = append(r.Checks, Check{Name: name, Tier: TierLocal, Status: Fail, Detail: detail})
145 r.settle()
146 }
147 148 // LocalWarn records a local check that passed with a caveat.
149 func (r *Report) LocalWarn(name, detail string) {
150 r.Checks = append(r.Checks, Check{Name: name, Tier: TierLocal, Status: Warn, Detail: detail})
151 r.settle()
152 }
153 154 // External runs an external check and records the sources that agreed.
155 func (r *Report) External(name, detail string, sources []string, err error) {
156 status := Pass
157 158 if err != nil {
159 if errors.Is(err, ErrUnverifiable) {
160 status = Unverifiable
161 } else {
162 status = Fail
163 }
164 165 detail = strings.TrimSpace(detail + ": " + err.Error())
166 }
167 168 r.Checks = append(r.Checks, Check{
169 Name: name, Tier: TierExternal, Status: status, Detail: detail, Sources: sources,
170 })
171 r.settle()
172 }
173 174 // Gate is the fail-closed barrier. The irreversible step calls this and nothing else.
175 //
176 // A warning does not block: it says something about the evidence the user must see, such as a
177 // single source where agreement was impossible. Only a failed claim or an unverified fact blocks,
178 // because those are the two ways an irreversible step gets taken on something untrue.
179 func (r *Report) Gate() error {
180 if r.Verdict == VerdictFail || r.Verdict == VerdictUnverifiable {
181 failed, unverifiable := r.Unverified()
182 183 var reasons []string
184 185 for _, check := range failed {
186 reasons = append(reasons, "failed: "+check.Name)
187 }
188 189 for _, check := range unverifiable {
190 reasons = append(reasons, "unverified: "+check.Name)
191 }
192 193 sort.Strings(reasons)
194 return fmt.Errorf("verify: refusing the irreversible step (%s): %s", r.Verdict,
195 strings.Join(reasons, "; "))
196 }
197 198 return nil
199 }
200 201 // Hash is a display-order helper used by callers that print txids.
202 func Hash(h [32]byte) string {
203 reversed := make([]byte, 32)
204 205 for i := 0; i < 32; i++ {
206 reversed[31-i] = h[i]
207 }
208 209 return hex.EncodeToString(reversed)
210 }
211