spv

package
v0.16.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 21 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package spv verifies a BEEF object by SPV before an overlay engine admits it: every merkle path the object carries against a chain tracker, and the scripts, amounts and structure of every transaction in it that carries no path.

It is the bridge's half of moving verification out of the TypeScript engine, and it is written for a second caller: a Go overlay engine. Nothing here knows about lanes, topics, HTTP or the bridge's header store; a caller hands it bytes and a chaintracker.ChainTracker and gets a Verdict back.

What is verified, and why every transaction

An engine verifies the transaction it picks as the object's subject (the Atomic BEEF subject, or the last transaction of a plain BEEF) together with that transaction's unproven ancestors. This package verifies every transaction in the BEEF that no other transaction in it spends (the tips), and with them every unproven ancestor, which is every unproven transaction in the object. It therefore covers whichever transaction an engine picks as the subject, so an engine told "this object passed" can skip script verification for any transaction in it without depending on the two parsers agreeing on which one the subject is.

The checks are go-sdk v1.7.2 spv.Verify's, run here from go-sdk's primitives (merkle path, script interpreter, activation heights) so that finality is a policy (FinalityPolicy, see check.go): merkle paths first, then for each unproven transaction bitcoin-sv's own rules (inputs and outputs present, no duplicate input, amounts in range, outputs not exceeding inputs, finality, coinbase maturity, scripts under the network's activation heights). That is STRICTER than the TypeScript SDK's verify, which checks paths, amounts and scripts only; a disagreement between the two is what shadow mode counts.

Three results, and only one of them is a refusal

  • ResultOK: every path proved and every unproven transaction passed.
  • ResultRefused: the object is invalid on its merits: it does not parse, a merkle path proves a root the chain does not commit to, or a transaction fails a script or structural check.
  • ResultError: verification could not reach a verdict: the chain tracker failed, a path names a height above the tracker's tip (the header has not arrived yet), or the deadline ran out. A caller must not read this as invalid. The bridge submits such an object WITHOUT its trust marker, so the engine verifies it in full, exactly as before.

Index

Constants

View Source
const MarkerHeader = "X-Overlay-Bridge-Verified"

MarkerHeader is the request header that carries the marker.

View Source
const MinKeyBytes = 32

MinKeyBytes is the shortest key accepted.

Variables

This section is empty.

Functions

func CheckMarker

func CheckMarker(key, body []byte, value string) bool

CheckMarker reports whether value is the marker for body under key. Constant time in the MAC comparison.

func LoadKey

func LoadKey(path string) ([]byte, error)

LoadKey reads a marker key file: its bytes, surrounding whitespace removed, are the key (generate one with `openssl rand -hex 32`). The file must not be readable by group or others.

func Marker

func Marker(key, body []byte) string

Marker returns the header value for body under key.

Types

type FinalityPolicy added in v0.4.1

type FinalityPolicy string

The policies.

FinalityIgnore is the overlay plane's policy and the default. Committed- record carriers are non-final BY DESIGN: nLockTime 4102444800 (2100-01-01) with a non-final input, so that they can never be mined (bbox docs/spec.md, the committed-record ruling of 2026-09-24). An overlay admits them when their scripts, proofs and amounts are valid, which is what the TypeScript engine does. go-sdk v1.7.2's spv.Verify applies bitcoin-sv's finality rule with no way to switch it off alone, so this package runs its checks itself from go-sdk primitives (owner ruling 2026-10-21, "own policy on v1.7.2").

FinalityRequire applies bitcoin-sv's rule as v1.7.2 does: a transaction with a non-zero nLockTime and a non-final input must have a height-based lock at or below tip+1; a time-based lock is refused, since the tracker reports no median time past.

const (
	FinalityIgnore  FinalityPolicy = "ignore"
	FinalityRequire FinalityPolicy = "require"
)

func ParseFinality added in v0.4.1

func ParseFinality(s string) (FinalityPolicy, error)

ParseFinality reads a policy name; empty is FinalityIgnore.

type Network

type Network string

Network selects the activation heights scripts are judged under.

const (
	NetworkMain Network = "main"
	NetworkTest Network = "test"
	NetworkAll  Network = "all"
)

Networks. NetworkAll makes every rule active from the first block, which is what a regtest-style chain (the lab's) needs, and is the closest match to the TypeScript SDK, which has no notion of eras at all.

func ParseNetwork

func ParseNetwork(s string) (Network, error)

ParseNetwork reads a network name; empty is NetworkMain.

type Reason

type Reason string

Reason says why an object was refused or errored. Its values are bounded so they can be logged and labelled; the full error is in Verdict.Err.

const (
	ReasonNone      Reason = ""
	ReasonParse     Reason = "parse"     // not a BEEF this package can read
	ReasonMerkle    Reason = "merkle"    // a path proves a root the chain does not commit to
	ReasonScript    Reason = "script"    // a script failed
	ReasonStructure Reason = "structure" // amounts, inputs, finality, maturity, a missing source
	ReasonTracker   Reason = "tracker"   // the chain tracker failed
	ReasonNotYet    Reason = "not_yet"   // a path names a height above the tracker's tip
	ReasonDeadline  Reason = "deadline"  // the context ended first
)

Reasons. ReasonNone accompanies ResultOK.

type Result

type Result string

Result is the outcome of one verification. Its values are metric labels.

const (
	ResultOK      Result = "ok"
	ResultRefused Result = "refused"
	ResultError   Result = "error"
)

The three results. See the package comment.

type Verdict

type Verdict struct {
	Result Result
	Reason Reason
	// TxID is the transaction the verdict is about: the first that failed,
	// or, on success, the last tip verified. Zero when the BEEF did not parse.
	TxID chainhash.Hash
	// Transactions is the number of transactions in the object; Scripts is
	// the number of input scripts run (inputs of unproven transactions).
	Transactions, Scripts int
	// Work counters (R2), set whenever the object parsed, whatever the
	// result. Inputs is every input of every transaction in the object.
	// Proofs is the number of transactions carrying a merkle path. Depth is
	// the object's ancestry depth: the longest chain of in-object parents
	// below any transaction, so a child of a proven parent is 1, and a
	// lone transaction is 0.
	Inputs, Proofs, Depth int
	Duration              time.Duration
	Err                   error
	// Marker is the trust marker for the object under Verifier.MarkerKey,
	// taken in the same traversal as the txids (inplace.go). It is set only
	// with ResultOK, and only when the object was read in place; a caller
	// that finds it empty computes Marker(key, object) itself.
	Marker string
	// ObjectSHA256 is SHA-256 of the object, taken in that same traversal
	// when Verifier.ObjectDigest asks for it; HasObjectSHA256 says it is
	// set, which it is whatever the result once the object was read in
	// place. Its SHA-256 is the object's content identifier.
	ObjectSHA256    [32]byte
	HasObjectSHA256 bool
}

Verdict is what Verify returns. It never carries a nil Result.

type Verifier

type Verifier struct {
	// Tracker answers merkle roots and the chain tip. Required.
	Tracker chaintracker.ChainTracker
	// Network selects activation heights. Empty means NetworkMain.
	Network Network
	// Finality is the finality policy. Empty means FinalityIgnore, the
	// overlay plane's policy (see FinalityPolicy).
	Finality FinalityPolicy
	// Timeout bounds one verification. Zero means 5 s. A script that runs
	// past it ends the verification with ResultError, never ResultOK.
	Timeout time.Duration
	// SkipScripts runs every check but the scripts: merkle paths, structure,
	// amounts, finality under the policy and maturity. It is for a caller
	// that already holds a passing verdict on these exact bytes from a
	// verifier it trusts: an engine that accepted the bridge's marker
	// (marker.go). Verdict.Scripts is then 0.
	SkipScripts bool
	// MarkerKey, when at least MinKeyBytes long, makes a passing verdict
	// carry the trust marker (Verdict.Marker) without a second pass over
	// the object.
	MarkerKey []byte
	// ObjectDigest makes the verdict carry SHA-256 of the object
	// (Verdict.ObjectSHA256), for a caller that would otherwise hash the
	// object once more for its identity.
	ObjectDigest bool
	// contains filtered or unexported fields
}

Verifier verifies BEEF objects. It is safe for concurrent use; it holds no state between calls.

func (*Verifier) Verify

func (v *Verifier) Verify(ctx context.Context, beef []byte) Verdict

Verify verifies one BEEF object (BRC-62 V1, BRC-96 V2 or BRC-95 Atomic).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL