resolve

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 21 Imported by: 1 module, 1 package

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 resolve turns a user@domain into an identity key and the overlay host that serves it, using only the two documents a domain publishes about itself: its BRC-180 manifest and its BRC-169 handle-resolution endpoint.

The domain is the trust anchor and nothing else is. A manifest says what a domain CLAIMS to host and a resolution answer says what it CLAIMS a handle's key is; both are secured by control of the domain and by nothing stronger. Every later step a reader takes (signature, sequence, proof, pin) exists because this step proves so little, so this package is deliberately literal: it fetches exactly the documents the specifications name, refuses anything that does not match what was asked, and never probes.

The one step that proves more is the handle certificate: where the domain declares a certifier key (metanet.trust), CheckHandleCertificate checks the BRC-52 certificate a resolution answer carries against it and against a revocation source the caller names, so the binding of handle to key is signed by the certifier and not only served by the domain.

HTTP here is the one place a reader reaches beyond the host and header service it was configured with, so the client policy is part of the security posture rather than a convenience: system roots only, HTTPS only, no cross-origin redirects, bounded bodies. NewHTTPClient is that policy in one place.

Index

Examples

Constants

View Source
const HandleCertificateType = "XgCFdUfxEcI+3xtDjsIuSAjMl5EwzCUjsQc45ds1lC8="

HandleCertificateType is the BRC-52 type of a BRC-169 handle certificate: the standard base64 of SHA-256("metanet-handles handle certificate v1") (BRC-169 section 4.5).

View Source
const RevocationFreshness = 60 * time.Second

RevocationFreshness is how old a revocation check may be before a value-moving action, by default (BRC-169 section 4.2 rule 4). See Handle.CertificateFresh.

Variables

View Source
var (
	// ErrCertificate is what every refusal of a handle certificate's own
	// content wraps: checks 1 to 4 of BRC-169 section 4.1 (the signature,
	// the certifier, the handle and domain, the subject) and the shape of
	// the certificate. The error says which rule failed.
	ErrCertificate = errors.New("handle certificate refused")
	// ErrCertificateRevoked is a handle certificate whose revocation
	// outpoint a revocation source shows spent (BRC-169 section 4.2): the
	// handle was released, reassigned or revoked. The error wraps
	// ErrRevoked too.
	ErrCertificateRevoked = errors.New("handle certificate revoked: its revocation outpoint is spent")
)
View Source
var (
	// ErrNotHTTPS refuses a URL that is not https. RFC 7033 and BRC-169 both
	// require HTTPS for discovery, and a manifest that points resolution at
	// http:// has asked the client to accept a key that anyone on the path
	// could have written.
	ErrNotHTTPS = errors.New("not an https URL")
	// ErrCrossOriginRedirect refuses a redirect that leaves the origin the
	// request was made to. Following one would let a domain hand its
	// authority to a host the user never named.
	ErrCrossOriginRedirect = errors.New("redirect to another origin refused")
	// ErrBodyTooLarge refuses a response above the bound.
	ErrBodyTooLarge = errors.New("response body exceeds the bound")
	// ErrNoHandles means the manifest has no metanet.handles object, so the
	// domain does not offer handle resolution. BRC-169 section 5.1 forbids
	// probing the well-known path in that case.
	ErrNoHandles = errors.New("domain does not offer handle resolution (no metanet.handles in its manifest)")
	// ErrNotFound is a 404: no such handle is registered at the domain.
	ErrNotFound = errors.New("handle is not registered at this domain")
	// ErrRevoked means the handle was registered and has been released or
	// revoked. The error may carry a forwarding record; see RevokedError.
	ErrRevoked = errors.New("handle revoked")
	// ErrCertificateUnverified is a handle certificate whose content passed
	// checks 1 to 4 of BRC-169 section 4.1 and whose revocation status was
	// not established: no revocation source was given, or none could
	// answer (CheckHandleCertificate). Such a certificate is not valid for
	// a value-moving action (section 4.2 rule 6).
	ErrCertificateUnverified = errors.New("handle certificate not verified: its revocation status was not checked")
)

Functions

func CheckHandleCertificate added in v0.25.0

func CheckHandleCertificate(ctx context.Context, h *Handle, m *Manifest, spends ...SpendSource) error

CheckHandleCertificate verifies h.Certificate, the BRC-52 handle certificate a resolution answer carried, under BRC-169 section 4.1, against m, the manifest of h's domain:

  1. the certificate's signature verifies under BRC-52 (the certifier's key derived under [2, "certificate signature"], key id "<type> <serialNumber>", counterparty anyone), and is strict low-S DER;
  2. the certifier is m's metanet.trust.publicKey;
  3. fields.handle and fields.domain, normalized, are h's handle and domain;
  4. the subject is h.IdentityKey;
  5. the revocation outpoint is unspent, by spends (section 4.2).

The type must be HandleCertificateType, and BRC-52's disabled revocation outpoint is refused, since a handle certificate is retired by spending its outpoint (section 4.3). A failure of any of 1 to 4, or of the certificate's shape, is an error wrapping ErrCertificate, checked before any source is asked.

BRC-169 does not say how a resolution answer served without authentication reveals the two fields to the reader. Two forms are read, and nothing else: the certificate object carries a keyring for the verifier "anyone" (BRC-52's verifier keyring, so any reader decrypts it), and the fields are decrypted through it; or it carries no keyring, and each field value is read as the base64 of its plaintext, the form of BRC-169's worked example (Appendix A.3). The signature covers the field text as served either way, so a production certificate whose fields are encrypted, served without such a keyring, fails rule 3.

BRC-52's JSON form writes revocationOutpoint as "txid.vout"; BRC-169's example writes it as an object {"txid", "vout"}. Both are read. A duplicate member name anywhere in the certificate is refused, as BRC-52 requires; members this package does not know are ignored, as BRC-169 section 5.1 requires.

Revocation is detectable only through a source that indexes spends (section 4.2), so spends are those sources, each a SpendSource (nodeapi.WoC, nodeapi.Asset, or an index of the caller's). They are asked in order, and all of them are asked: the certificate is revoked when any names a spender (ErrCertificateRevoked, wrapping ErrRevoked), and verified when none does and at least one shows the outpoint unspent. When no source is given, or none could answer, the error wraps ErrCertificateUnverified: checks 1 to 4 passed, and the certificate MUST NOT be treated as valid for a value-moving action (section 4.2 rule 6), though a caller may render existing content marked unverified.

On success it sets h.CertificateChecked, h.CertificateCheckedAt (the time of the revocation check, which section 4.2 rule 3 requires be kept with a cached result) and h.RevocationOutpoint, and returns nil. On any error it clears h.CertificateChecked.

func NewHTTPClient

func NewHTTPClient(timeout time.Duration) *http.Client

NewHTTPClient returns a client that implements the discovery policy: system TLS roots only, no way to skip verification, redirects followed only within the origin of the first request, and no proxy.

No proxy is a choice, not an omission. HTTP_PROXY in the environment would route discovery through a host that appears nowhere in the caller's configuration or its trace, and a run should reach exactly what it was configured to reach.

func VerifyHandleCertificate

func VerifyHandleCertificate(h *Handle, m *Manifest) error

VerifyHandleCertificate is CheckHandleCertificate with no revocation source: checks 1 to 4 of BRC-169 section 4.1. It never sets CertificateChecked, since revocation is not checked: a certificate that passes returns an error wrapping ErrCertificateUnverified, and one that fails returns an error wrapping ErrCertificate.

Types

type Acct

type Acct struct {
	// Handle is lowercase, with any +tag stripped.
	Handle string
	// Domain is the lowercase FQDN. Never an alias; see ParseAcct.
	Domain string
	// Tag is the "+tag" subhandle if one was given, without the "+". It is
	// kept because it is the recipient's routing hint (section 3) and
	// stripped from Handle because resolution is on the bare handle (5.2).
	Tag string
}

Acct is a parsed recipient: the fully qualified form of BRC-169 section 2.1 with the ecosystem already known to be a domain.

func ParseAcct

func ParseAcct(s string) (Acct, error)

ParseAcct accepts user@domain, @user@domain and acct:user@domain, lowercases per section 2.1 rule 2, strips and keeps a +tag, and validates the grammar.

The ecosystem MUST be a domain. Section 2.1 rule 5 makes the dot the discriminator, so a dotless ecosystem is an alias, and aliases are not supported here: resolving one means trusting a directory to say which domain "example" is, which is a trust step this package has no way to check. The error says so rather than guessing at a hostname the user did not type.

Example

ParseAcct accepts the three written forms of a BRC-169 address, lowercases it, and keeps a +tag apart from the handle it routes to.

package main

import (
	"fmt"

	"github.com/lightwebinc/bcommon/resolve"
)

func main() {
	for _, s := range []string{"Alice@Example.COM", "@alice+news@example.com", "acct:alice@example.com", "alice@example"} {
		a, err := resolve.ParseAcct(s)
		if err != nil {
			fmt.Println(s, "=>", err)
			continue
		}
		fmt.Printf("%s => %s (tag %q)\n", s, a, a.Tag)
	}
}
Output:
Alice@Example.COM => alice@example.com (tag "")
@alice+news@example.com => alice@example.com (tag "news")
acct:alice@example.com => alice@example.com (tag "")
alice@example => resolve: "alice@example": ecosystem "example" has no dot, so it is an alias (BRC-169 section 2.1 rule 5); aliases are not supported, name the domain in full

func (Acct) String

func (a Acct) String() string

String returns handle@domain, the paymail-style form, which is how an address is written on a command line and keyed in a pin file.

type Handle

type Handle struct {
	Acct
	IdentityKey [33]byte
	// Certificate is the BRC-52 handle certificate exactly as served,
	// unverified until CheckHandleCertificate checks it.
	Certificate json.RawMessage
	Messagebox  string
	// TTL is the caching bound in seconds (section 5.4): binding upward,
	// advisory downward.
	TTL     int
	Revoked bool
	// CertificateChecked reports whether CheckHandleCertificate verified
	// Certificate against the domain's certifier key, its revocation
	// outpoint included, at CertificateCheckedAt (BRC-169 section 4.2 rule
	// 3). RevocationOutpoint is the certificate's revocation outpoint,
	// txid.vout, once its content has been checked.
	CertificateChecked   bool
	CertificateCheckedAt time.Time
	RevocationOutpoint   string
}

Handle is a resolved handle: what the domain says the identity key is.

CertificateChecked is false in every Handle ResolveHandle returns, so that a caller cannot mistake "the domain said so" for "the certifier signed it"; CheckHandleCertificate sets it.

func ResolveHandle

func ResolveHandle(ctx context.Context, c *http.Client, m *Manifest, a Acct) (*Handle, error)

ResolveHandle queries the domain's handle-resolution endpoint (section 5.2) and returns what it claims about a.

The echo check is the one that matters. The endpoint must echo the handle and domain it resolved, and a static file that returns the same document for every handle passes every other check in this function: valid JSON, valid key, valid version. Requiring the echo to equal what was asked is how that file is caught here rather than trusted.

func (*Handle) CertificateFresh added in v0.25.0

func (h *Handle) CertificateFresh(now time.Time, bound time.Duration) bool

CertificateFresh reports whether h's certificate was verified, revocation included, no longer than bound before now: what section 4.2 rule 4 asks before a value-moving action, with bound RevocationFreshness by default. The bound is capped at the resolution's TTL (rule 4: it must not exceed it), and a TTL of 0 allows no cached result at all.

type Handles

type Handles struct {
	Version    string   `json:"version"`
	Resolve    string   `json:"resolve"`
	Search     string   `json:"search"`
	Reverse    string   `json:"reverse"`
	Messagebox string   `json:"messagebox"`
	Aliases    []string `json:"aliases"`
}

Handles is metanet.handles.

type Manifest

type Manifest struct {
	// Overlays maps a topic manager or lookup service name to its base URL
	// (BRC-180). Nil when the domain declares no overlays, which is a valid
	// manifest and not an error.
	Overlays map[string]string
	// Handles is the metanet.handles object (BRC-169 section 5.1). Nil when
	// the domain does not offer handle resolution.
	Handles *Handles
	// Trust is the metanet.trust anchor (BRC-68). Its PublicKey is the
	// certifier key handle certificates are checked against.
	Trust *Trust
	// Raw is the document as served.
	Raw json.RawMessage
}

Manifest is the part of a domain's /manifest.json this package reads: the BRC-180 overlay map, the BRC-169 handles object and the BRC-68 trust anchor. Unknown keys are ignored, as both specifications require, and Raw keeps the whole document for a caller that needs one this struct does not carry.

func FetchManifest

func FetchManifest(ctx context.Context, c *http.Client, domain string) (*Manifest, error)

FetchManifest reads https://<domain>/manifest.json.

The path is fixed by BRC-68 and the scheme by BRC-180; there is no fallback to http and no alternative location, because a manifest reached any other way would not be secured by control of the domain, which is the only thing securing it.

func ParseManifest

func ParseManifest(body []byte) (*Manifest, error)

ParseManifest decodes a manifest document.

func (*Manifest) Overlay

func (m *Manifest) Overlay(name string) (string, bool)

Overlay returns the base URL the domain declares for a service. ok is false when the entry is absent, and BRC-180 is explicit about what that means: the domain does not offer the service, and the client must not go looking.

Example

A manifest names one base URL per service. An absent entry means the domain does not offer the service, and a reader must not go looking.

package main

import (
	"fmt"

	"github.com/lightwebinc/bcommon/resolve"
)

func main() {
	m, err := resolve.ParseManifest([]byte(`{"metanet":{"overlays":{"ls_example":"https://overlay.example.com"}}}`))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(m.Overlay("ls_example"))
	fmt.Println(m.Overlay("ls_other"))
}
Output:
https://overlay.example.com true
 false

type RevokedError

type RevokedError struct {
	Acct       Acct
	Status     int
	Forwarding json.RawMessage
}

RevokedError is ErrRevoked with the forwarding record, if the domain served one (section 4.3). errors.Is(err, ErrRevoked) matches it.

func (*RevokedError) Error

func (e *RevokedError) Error() string

func (*RevokedError) Is

func (e *RevokedError) Is(target error) bool

Is makes errors.Is(err, ErrRevoked) true for a RevokedError.

type SpendSource added in v0.25.0

type SpendSource interface {
	Spender(ctx context.Context, txid string, vout uint32) (string, error)
}

SpendSource names the transaction that spent an output, "" with a nil error only when the source shows it unspent, and an error for every other answer: nodeapi.SpendSource's contract, which nodeapi.WoC and nodeapi.Asset meet, declared here so that this package imports no other.

type Trust

type Trust struct {
	Name      string `json:"name"`
	PublicKey string `json:"publicKey"`
}

Trust is metanet.trust.

Jump to

Keyboard shortcuts

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