// Package verify decides whether an atomic swap is safe to proceed with, from bytes the caller // already has plus facts that must come from outside. // // The split is the point. Local checks never touch a network: they parse the counterparty's // transactions, rebuild digests, check signatures and scripts, and compare every commitment with // the signed terms. External facts are the handful of things only a chain can answer, and they // come from several independent sources with agreement, never from one. // // The threat this is built for is a swap counterparty who also runs, or colludes with, the // gateway that supplies chain data: it can show a real transaction on a chain it controls, or // claim a transaction exists that does not. A verdict therefore fails closed, and the caller must // pass every check before an irreversible step. package verify import ( "encoding/hex" "errors" "fmt" "sort" "strings" ) // Tier says where a check's truth comes from. type Tier string const ( // TierLocal is a check that needs no network: bytes in, answer out. TierLocal Tier = "local" // TierExternal is a check that needs chain facts, which only a source can supply. TierExternal Tier = "external" ) // Status is one check's outcome. type Status string const ( // Pass means the check was performed and the claim holds. Pass Status = "pass" // Warn means the claim holds but something about it deserves the user's attention. Warn Status = "warn" // Fail means the claim is false, or the check could not be run on the data supplied. Fail Status = "fail" // Unverifiable means the fact could not be established, which is not the same as a failure: // the UI lists these separately and the gate still refuses. Unverifiable Status = "unverifiable" ) // Verdict is the whole result. It is never "pass" while any required check is not a Pass. type Verdict string const ( VerdictPass Verdict = "pass" VerdictWarn Verdict = "warn" VerdictFail Verdict = "fail" VerdictUnverifiable Verdict = "unverifiable" ) // Check is one named result. type Check struct { // Name is a stable identifier, not a sentence. Name string `json:"name"` // Tier is local or external. Tier Tier `json:"tier"` // Status is the outcome. Status Status `json:"status"` // Detail says what was compared, in the operator's words. Detail string `json:"detail"` // Sources names the external sources that agreed, when the check is external. Sources []string `json:"sources,omitempty"` } // Report is everything a caller and a UI need. type Report struct { Verdict Verdict `json:"verdict"` Checks []Check `json:"checks"` // SourcesUsed is every external source consulted. SourcesUsed []string `json:"sources_used"` // Agreement is how many sources agreed on the facts that mattered. Agreement int `json:"agreement"` // Quorum is how many agreement was required. Quorum int `json:"quorum"` // Absence reports what "not found" meant, including the sample and whether the sources // looked independent. Absence []Absence `json:"absence,omitempty"` // Independence is the cross-source check. Independence Independence `json:"independence"` // Notes carry the residual leaks and any advice. Notes []string `json:"notes,omitempty"` } // Unverified lists the failed and unverifiable checks separately, which is how the UI must show // them: a failed claim is evidence against the trade, an unverifiable one is missing evidence. func (r *Report) Unverified() (failed, unverifiable []Check) { for _, check := range r.Checks { switch check.Status { case Fail: failed = append(failed, check) case Unverifiable: unverifiable = append(unverifiable, check) } } return failed, unverifiable } // settle recomputes the verdict from the checks, so a caller cannot set a verdict that contradicts // its checks. func (r *Report) settle() { verdict := VerdictPass for _, check := range r.Checks { switch check.Status { case Fail: verdict = VerdictFail case Unverifiable: if verdict != VerdictFail { verdict = VerdictUnverifiable } case Warn: if verdict == VerdictPass { verdict = VerdictWarn } } } r.Verdict = verdict } // Local runs a local check and records it. func (r *Report) Local(name string, detail string, err error) { status := Pass if err != nil { status = Fail detail = strings.TrimSpace(detail + ": " + err.Error()) } r.Checks = append(r.Checks, Check{Name: name, Tier: TierLocal, Status: status, Detail: detail}) r.settle() } // LocalFail records a local check that failed. func (r *Report) LocalFail(name, detail string) { r.Checks = append(r.Checks, Check{Name: name, Tier: TierLocal, Status: Fail, Detail: detail}) r.settle() } // LocalWarn records a local check that passed with a caveat. func (r *Report) LocalWarn(name, detail string) { r.Checks = append(r.Checks, Check{Name: name, Tier: TierLocal, Status: Warn, Detail: detail}) r.settle() } // External runs an external check and records the sources that agreed. func (r *Report) External(name, detail string, sources []string, err error) { status := Pass if err != nil { if errors.Is(err, ErrUnverifiable) { status = Unverifiable } else { status = Fail } detail = strings.TrimSpace(detail + ": " + err.Error()) } r.Checks = append(r.Checks, Check{ Name: name, Tier: TierExternal, Status: status, Detail: detail, Sources: sources, }) r.settle() } // Gate is the fail-closed barrier. The irreversible step calls this and nothing else. // // A warning does not block: it says something about the evidence the user must see, such as a // single source where agreement was impossible. Only a failed claim or an unverified fact blocks, // because those are the two ways an irreversible step gets taken on something untrue. func (r *Report) Gate() error { if r.Verdict == VerdictFail || r.Verdict == VerdictUnverifiable { failed, unverifiable := r.Unverified() var reasons []string for _, check := range failed { reasons = append(reasons, "failed: "+check.Name) } for _, check := range unverifiable { reasons = append(reasons, "unverified: "+check.Name) } sort.Strings(reasons) return fmt.Errorf("verify: refusing the irreversible step (%s): %s", r.Verdict, strings.Join(reasons, "; ")) } return nil } // Hash is a display-order helper used by callers that print txids. func Hash(h [32]byte) string { reversed := make([]byte, 32) for i := 0; i < 32; i++ { reversed[31-i] = h[i] } return hex.EncodeToString(reversed) }