// Command tinywallet-verify decides a swap dossier from the command line. // // It is the same code the page runs: a dossier in, a verdict out. It exists so a verdict can be // reproduced outside a browser, which is what makes it evidence. package main import ( "context" "encoding/hex" "encoding/json" "flag" "fmt" "io" "net/http" "os" "time" "git.smesh.lol/tinywallet/pkg/tx" "git.smesh.lol/tinywallet/pkg/verify" ) func main() { path := flag.String("dossier", "-", "dossier JSON file, - for standard input") txHex := flag.String("tx", "", "decode one raw transaction and print its facts as JSON") presetsFlag := flag.Bool("presets", false, "print the baked indexer presets as JSON") probe := flag.Bool("probe", false, "ask every preset for its tip and fee floor, and report agreement") fetch := flag.Bool("fetch", false, "fill the dossier's facts from the presets, then decide it") need := flag.Int("need", 2, "how many sources must agree") flag.Parse() if *presetsFlag { body, err := json.MarshalIndent(verify.Presets(), "", " ") if err != nil { fail(err) } fmt.Println(string(body)) return } if *probe { probePresets() return } if *txHex != "" { decode(*txHex) return } body, err := read(*path) if err != nil { fail(err) } if *fetch { body, err = fill(body, *need) if err != nil { fail(err) } } out, err := verify.AdjudicateJSON(body) if err != nil { fail(err) } var report verify.Report if err := json.Unmarshal(out, &report); err != nil { fail(err) } fmt.Println("verdict:", report.Verdict) fmt.Printf("verified by %d of %d independent sources\n", report.Agreement, report.Quorum) failed, unverifiable := report.Unverified() for _, check := range report.Checks { mark := "ok" if check.Status != verify.Pass { mark = string(check.Status) } fmt.Printf(" [%-6s] %-8s %-32s %s\n", check.Tier, mark, check.Name, check.Detail) } fmt.Printf("failed: %d, unverifiable: %d\n", len(failed), len(unverifiable)) // Exit non-zero when the gate would refuse, so this can be used in a script. if err := report.Gate(); err != nil { os.Exit(2) } } // decode prints what the verifier's own parser sees in a transaction, in the same vocabulary a // node's decoderawtransaction uses, so the two can be compared directly. func decode(text string) { raw, err := hex.DecodeString(text) if err != nil { fail(err) } parsed, err := tx.Decode(raw) if err != nil { fail(err) } id, err := parsed.TxIDHex() if err != nil { fail(err) } outputs := make([]map[string]any, 0, len(parsed.Outputs)) for _, out := range parsed.Outputs { outputs = append(outputs, map[string]any{ "value": out.Value, "script": hex.EncodeToString(out.Script), }) } sequences := make([]uint32, 0, len(parsed.Inputs)) for _, in := range parsed.Inputs { sequences = append(sequences, in.Sequence) } body, err := json.Marshal(map[string]any{ "txid": id, "locktime": parsed.LockTime, "sequences": sequences, "outputs": outputs, }) if err != nil { fail(err) } fmt.Println(string(body)) } // probePresets asks every baked indexer for the two facts that need no transaction, and shows // whether the Bitcoin sources agree on the head. func probePresets() { client := &http.Client{Timeout: 15 * time.Second} tips := map[string]verify.Tip{} for _, preset := range verify.Presets() { adapter, err := verify.AdapterFor(preset, client) if err != nil { fmt.Printf("%-16s %-4s error: %v\n", preset.Name, preset.Chain, err) continue } ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) tip, err := adapter.Tip(ctx) rate, feeErr := adapter.FeeEstimate(ctx) cancel() if err != nil { fmt.Printf("%-16s %-4s tip error: %v\n", preset.Name, preset.Chain, err) continue } tips[preset.Name] = tip note := "" if feeErr != nil { note = " (no fee floor: " + feeErr.Error() + ")" } fmt.Printf("%-16s %-4s tip %d %s… fee %d sat/vB%s\n", preset.Name, preset.Chain, tip.Height, verify.Hash(tip.Hash)[:16], rate, note) } // The Bitcoin sources are compared as one group, because that is the quorum a verdict uses. hashes := map[[32]byte]bool{} for name, tip := range tips { if len(name) > 0 && (name == "mempool.space" || name == "blockstream.info" || name == "btcscan.org" || name == "mempool.ninja") { hashes[tip.Hash] = true } } // One chain has one tip, so identical hashes are the expected state and not evidence of a // shared backend. What the probe reports is simply that the sources agree on the head. if len(hashes) == 1 { fmt.Println("bitcoin sources agree on the tip (expected on one chain; it is not an independence proof)") } else { fmt.Printf("bitcoin sources report %d different tip hashes: at least one is behind or on a fork\n", len(hashes)) } } // fill asks the presets about every transaction the dossier names, and replaces the dossier's // supplied facts with fetched ones. It is the native path to the same verdict the page renders. func fill(body []byte, need int) ([]byte, error) { var dossier verify.Dossier if err := json.Unmarshal(body, &dossier); err != nil { return nil, err } client := &http.Client{Timeout: 20 * time.Second} // One adapter per preset, built once. type source struct { preset verify.Preset adapter verify.Source } var sources []source for _, preset := range verify.Presets() { adapter, err := verify.AdapterFor(preset, client) if err != nil { return nil, err } sources = append(sources, source{preset: preset, adapter: adapter}) } facts := map[string]map[string]verify.FactJSON{} for _, leg := range dossier.Legs { for _, text := range []string{leg.Funding, leg.Refund, leg.Claim} { raw, err := hex.DecodeString(text) if err != nil { return nil, err } txid, err := verify.TxidOf(raw) if err != nil { return nil, err } for _, entry := range sources { if entry.preset.Chain != leg.Chain { continue } ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) fact, err := entry.adapter.Tx(ctx, txid) cancel() if facts[entry.preset.Name] == nil { facts[entry.preset.Name] = map[string]verify.FactJSON{} } if err != nil { // A source that could not answer does not appear for that txid. continue } facts[entry.preset.Name][verify.Hash(txid)] = verify.FactJSON{ Found: fact.Found, Hex: hex.EncodeToString(fact.Raw), Height: fact.Height, } } } } dossier.Need = need dossier.Sources = nil for name, entries := range facts { chain := "" for _, preset := range verify.Presets() { if preset.Name == name { chain = preset.Chain } } _ = chain dossier.Sources = append(dossier.Sources, verify.SourceJSON{Name: name, Facts: entries}) } return json.Marshal(dossier) } func read(path string) ([]byte, error) { if path == "-" { return io.ReadAll(os.Stdin) } return os.ReadFile(path) } func fail(err error) { fmt.Fprintln(os.Stderr, "tinywallet-verify:", err) os.Exit(1) }