zonerec

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 25 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 zonerec is bdns's record codec: the four records a zone's topic carries, the leaves a state commits to, their root and their proofs (docs/spec.md). Each record is a canonical CBOR map with unsigned-integer keys, key 0 its four-byte magic, read with bcommon's record rules: a record is refused for the first rule it breaks, in key order, and keys above the last one a version defines are preserved verbatim.

| Record | Magic | Carried as | | Record set state | "bdr" 0x01 | a chain token under the set's owner | | Origin state | "bdo" 0x01 | a chain token under the origin's owner | | Attached record | "bda" 0x01 | a PushDrop, signed by its writer | | Host record | "bdh" 0x01 | a PushDrop, signed by the host |

A leaf is not a record: it is a canonical CBOR array, one per name and type, and a state commits to its leaves by an RFC 6962 root.

Index

Constants

View Source
const (
	MaxTokenBEEF   = 196608 // a token with its parent, or with what its inputs spend
	MaxPieceBEEF   = 196608 // a leaves piece's carrier and its funding tree
	MaxLoosePieces = 256    // pieces a chain holds that no kept state names
	DefaultMaxBEEF = 262144
)

Bounds on what a host admits.

View Source
const (
	AKMagic  = 0
	AKZone   = 1 // the identifier of the record set or origin it attaches to
	AKKind   = 2 // what it is, 1 to 65,535
	AKWriter = 3 // who signs it, 33 bytes
	AKName   = 4 // the name it is about; no labels for the zone
	AKExpiry = 5 // Unix seconds after which it is not believed
	AKBody   = 6 // at most 8,192 bytes; its form is the kind's
	AKLast   = AKBody
)

The attached record's keys (docs/spec.md section 9). It is the payload of a PushDrop signed by its writer. Version 1 admits no kind.

View Source
const (
	KindMember     = 1
	KindNameserver = 2 // a nameserver advertisement
	KindReceipt    = 3 // a render receipt
	KindObjection  = 4
	KindFinding    = 5
	KindReport     = 6
	KindOutcome    = 7
)

The assigned kinds of an attached record. Only a member's body has a form in this version.

View Source
const (
	MKHost = 1 // the host's key, 33 bytes
	MKRule = 2 // RuleGrant or RuleAdvert

	RuleGrant  = 1
	RuleAdvert = 2
)

The member body's keys and rules.

View Source
const (
	TypeA          = 1
	TypeNS         = 2
	TypeCNAME      = 5
	TypeSOA        = 6
	TypePTR        = 12
	TypeMX         = 15
	TypeAAAA       = 28
	TypeSRV        = 33
	TypeDNAME      = 39
	TypeOPT        = 41
	TypeDS         = 43
	TypeRRSIG      = 46
	TypeNSEC       = 47
	TypeDNSKEY     = 48
	TypeNSEC3      = 50
	TypeNSEC3PARAM = 51
	TypeCDS        = 59
	TypeCDNSKEY    = 60
	TypeSVCB       = 64
	TypeHTTPS      = 65
	TypeTKEY       = 249
	TypeTSIG       = 250
)

The record types the checks name.

View Source
const (
	HKMagic     = 0
	HKHost      = 1 // the host's key, 33 bytes
	HKAddresses = 2 // 1 to 32 addresses of 4 or 16 bytes, distinct, ascending; none IPv4-mapped
	HKPort      = 3 // 1 to 65,535
	HKProtocols = 4 // 0 to 8 ALPN identifiers of 1 to 32 bytes, distinct, ascending
	HKCA        = 5 // the certificate authority's CAA domain, or ""
	HKAccount   = 6 // the account URI at that authority, at most 512 bytes; "" when there is no authority
	HKLocation  = 7 // an IATA metro code, three upper-case letters, or ""
	HKExpiry    = 8 // Unix seconds after which it is not believed
	HKLast      = HKExpiry
)

The host record's keys (docs/spec.md section 10). It is the payload of a PushDrop signed by the host's key. Version 1 admits none.

View Source
const (
	SecurityLevel     = 1
	ProtocolRecordSet = "bdns records"
	ProtocolOrigin    = "bdns origin"
	ProtocolMember    = "bdns member" // attached and host records

	KeyIDState  = "state"  // token lock and field signature
	KeyIDChain  = "chain"  // the outputs of the tree that pays for a chain's first token
	KeyIDAttach = "attach" // attached record lock and field signature
	KeyIDHost   = "host"   // host record lock and field signature
)

The BRC-43 protocols, all at security level 1, and their key ids. Derivations are BRC-42 with the anyone counterparty.

View Source
const (
	TopicPrefix   = "tm_bdns_"
	LookupService = "ls_bdns"
	TopicLetterN  = 28
)

The topic prefix and the lookup service.

View Source
const (
	OKMagic      = 0
	OKID         = 1  // the origin's identifier: 32 bytes, the same in every state
	OKSeq        = 2  // 0 for the first state, then one more each state
	OKGenesis    = 3  // the owner at seq 0, 33 bytes; the same in every state
	OKOwner      = 4  // who signs this state; the genesis key at seq 0
	OKAnchor     = 5  // the outpoint the first state spent: txid || uint32be(vout)
	OKBase       = 6  // the identifier of the record set this origin answers with
	OKSecret     = 7  // opens the record set's commitment to this origin
	OKSerial     = 8  // the SOA serial, greater than the previous state's
	OKRoot       = 9  // the RFC 6962 root over this origin's own leaves
	OKCount      = 10 // the number of its own leaves
	OKData       = 11 // SHA-256 of its leaves object
	OKSize       = 12 // bytes of its leaves object
	OKRecovery   = 13 // empty, or SHA-256 of a key that may replace a lost owner key
	OKSigning    = 14 // reserved for the zone's signing keys; empty
	OKExtensions = 15 // extensions, as a record set's
	OKLapse      = 16 // Unix seconds after which an unmined state is not believed
	OKLast       = OKLapse
)

The origin state's keys (docs/spec.md section 6).

View Source
const (
	MaxPiece       = 128 << 10      // bytes of a leaves object one piece carries
	MaxPieceRecord = MaxPiece + 512 // a piece, encoded
)

Bounds of a leaves piece (docs/spec.md section 12).

View Source
const (
	PKMagic   = 0
	PKZone    = 1 // the chain's identifier
	PKGenesis = 2 // the chain's genesis key
	PKAnchor  = 3 // the chain's anchor: txid || uint32be(vout)
	PKWriter  = 4 // who signs the piece
	PKData    = 5 // SHA-256 of the whole leaves object
	PKSize    = 6 // bytes of the whole leaves object
	PKIndex   = 7 // which piece, from 0
	PKBytes   = 8 // the object's bytes from index * MaxPiece
	PKLast    = PKBytes
)

The leaves piece's keys.

View Source
const (
	MaxProofLeaves = 64      // leaves at one name a proof carries
	MaxProof       = 2 << 20 // a proof, encoded

)

Bounds of a proof's byte form (docs/spec.md section 13).

View Source
const (
	RKMagic      = 0
	RKID         = 1  // the record set's identifier: 32 bytes, the same in every state
	RKSeq        = 2  // 0 for the first state, then one more each state
	RKGenesis    = 3  // the owner at seq 0, 33 bytes; the same in every state
	RKOwner      = 4  // who signs this state; the genesis key at seq 0
	RKAnchor     = 5  // the outpoint the first state spent: txid || uint32be(vout)
	RKRoot       = 6  // the RFC 6962 root over the leaves
	RKCount      = 7  // the number of leaves
	RKData       = 8  // SHA-256 of the leaves object
	RKSize       = 9  // bytes of the leaves object
	RKAccepts    = 10 // commitments to origins: distinct, ascending
	RKExtensions = 11 // extensions: distinct and ascending by kind
	RKLapse      = 12 // Unix seconds after which an unmined state is not believed
	RKLast       = RKLapse
)

The record set state's keys (docs/spec.md section 5).

View Source
const (
	MaxRecord      = 16 << 10 // any record, encoded
	MaxLeaf        = 16 << 10 // a leaf, encoded
	MaxLabel       = 63       // a label's bytes
	MaxNameWire    = 200      // a relative name's labels with their length bytes
	MaxDNSName     = 255      // an absolute name on the wire, its root byte included
	MaxParts       = 16       // parts of one record's data
	MaxPartBytes   = 4096     // a byte-string part
	MaxValues      = 64       // records of one leaf
	MaxType        = 65535
	MaxTTL         = 1<<31 - 1
	MaxSetCount    = 1 << 20  // leaves of a record set
	MaxSetSize     = 16 << 20 // a record set's leaves object, bytes
	MaxOriginCount = 4096     // an origin's own leaves
	MaxOriginSize  = 1 << 20  // an origin's leaves object, bytes
	MaxAccepts     = 64
	MaxExtensions  = 8
	MaxExtension   = 1024 // an extension's body
	MaxSerial      = 1<<32 - 1
	MaxBody        = 8192 // an attached record's body
	MaxAddresses   = 32
	MaxProtocols   = 8
	MaxProtocol    = 32
	MaxAccount     = 512
	MaxCADomain    = 253
	MaxSafe        = 1<<53 - 1
)

Bounds on what a record, a leaf and a leaves object carry.

View Source
const KeyIDLeaves = "leaves"

KeyIDLeaves locks a leaves piece's carrier and the trees that fund them (docs/spec.md sections 7 and 12).

Variables

View Source
var (
	ErrShape       = errors.New("zonerec: transaction has the wrong shape")
	ErrBEEF        = chaintoken.ErrBEEF
	ErrZone        = errors.New("zonerec: record is not for this topic's chain")
	ErrLock        = errors.New("zonerec: output script is not the canonical one for its key")
	ErrTokenInput  = errors.New("zonerec: token does not spend its predecessor, or its chain tree at seq 0")
	ErrAnchor      = errors.New("zonerec: anchor is not the outpoint the first token spends")
	ErrContinuity  = errors.New("zonerec: state does not follow its predecessor")
	ErrSerial      = errors.New("zonerec: serial is not greater than the previous state's")
	ErrRecovery    = errors.New("zonerec: recovery differs from the previous state's")
	ErrFieldSig    = errors.New("zonerec: field signature is not strict or does not verify under its signer")
	ErrUnmined     = errors.New("zonerec: proof does not verify against the host's headers")
	ErrScript      = errors.New("zonerec: an unmined token's input does not unlock the output it spends")
	ErrLapsed      = errors.New("zonerec: an unmined state's lapse time has passed")
	ErrOrphan      = errors.New("zonerec: the host does not hold the state this one follows")
	ErrFork        = errors.New("zonerec: the host holds another successor of the same state")
	ErrNoLeaves    = errors.New("zonerec: the host does not hold the state's whole leaves object")
	ErrPiece       = errors.New("zonerec: a leaves piece is not a piece of its object")
	ErrMineable    = errors.New("zonerec: carrier is mineable")
	ErrUnlock      = errors.New("zonerec: unlocking script is not one canonical signature push")
	ErrFunding     = errors.New("zonerec: carrier does not spend a funding output of its key, or does not carry its value")
	ErrWriter      = errors.New("zonerec: a leaves piece's writer may not sign the chain's next state")
	ErrHeld        = errors.New("zonerec: the host already holds a piece of this object at this index")
	ErrFull        = errors.New("zonerec: the host holds its bound of pieces that no kept state names")
	ErrUnsupported = errors.New("zonerec: attached and host records are not admitted yet")
	ErrNotBdns     = errors.New("zonerec: transaction is not for this topic")
	ErrHostConfig  = errors.New("zonerec: host configuration or previous coins out of range")
)

The transaction refusals (docs/spec.md section 12), beside the record refusals. A transaction is refused for the first rule it breaks.

View Source
var (
	TagRecordSet = []byte{0x62, 0x64, 0x01}
	TagFunding   = []byte{0x62, 0x64, 0x02}
	TagOrigin    = []byte{0x62, 0x64, 0x03}
	TagAttached  = []byte{0x62, 0x64, 0x04}
	TagHost      = []byte{0x62, 0x64, 0x05}
)

The tags a script carries before its record.

View Source
var (
	MagicRecordSet = []byte("bdr\x01")
	MagicOrigin    = []byte("bdo\x01")
	MagicAttached  = []byte("bda\x01")
	MagicHost      = []byte("bdh\x01")
)

The records' magics: four bytes, the last the version.

View Source
var (
	ErrLabel     = errors.New("zonerec: not a label")
	ErrName      = errors.New("zonerec: a name is over its bound, or has a wildcard label that is not first")
	ErrPart      = errors.New("zonerec: a data part is malformed, or two byte parts are adjacent")
	ErrData      = errors.New("zonerec: record data is missing, over the bound, or not distinct and ascending")
	ErrLeaf      = errors.New("zonerec: a leaf is not its five items")
	ErrScope     = errors.New("zonerec: a scope is not empty or 32 bytes that are not all zero, or an origin's own leaf has one")
	ErrOrder     = errors.New("zonerec: leaves are not in key order")
	ErrDuplicate = errors.New("zonerec: two leaves share a key")
	ErrKey       = errors.New("zonerec: not a canonical compressed public key, or the anyone key")
	ErrGenesis   = errors.New("zonerec: the owner at seq 0 is not the genesis key")
	ErrID        = errors.New("zonerec: the identifier is not the one its genesis key and outpoint give")
	ErrExtension = errors.New("zonerec: an extension is malformed, or kinds are not distinct and ascending")
	ErrMember    = errors.New("zonerec: not a member body")
	ErrAddress   = errors.New("zonerec: addresses are missing, not 4 or 16 bytes, IPv4-mapped, or not distinct and ascending")
	ErrProtocol  = errors.New("zonerec: protocols are over the bound, or not distinct and ascending")
	ErrCA        = errors.New("zonerec: not a certificate authority's domain")
	ErrAccount   = errors.New("zonerec: an account with no certificate authority")
	ErrLocation  = errors.New("zonerec: not an IATA metro code")
	ErrProof     = errors.New("zonerec: the proof does not hold")
	ErrPresent   = errors.New("zonerec: the key is present")
)

Refusals beyond bcommon's record ones. Reason gives each a fixed label.

View Source
var (
	ErrMismatch     = errors.New("zonerec: the leaves object does not match the state")
	ErrRendererType = errors.New("zonerec: a leaf of a type the renderer writes, or of a type that is not data")
	ErrCNAME        = errors.New("zonerec: a CNAME at the apex, beside another type, or of more than one record")
	ErrCut          = errors.New("zonerec: a leaf at or below a delegation")
	ErrNoName       = errors.New("zonerec: data of a type that names a host holds no name part")
)

The refusals of the checks of spec section 8: C1 ErrMismatch, C2 ErrScope, then C3 to C6 in order.

View Source
var AnyoneKey = []byte{
	0x02, 0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07,
	0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98,
}

AnyoneKey is BRC-43's anyone counterparty: the generator point, whose private key is 1. No record names it.

Chains lists them, for a host's metrics.

EmptyRoot is the root of no leaves: SHA-256 of the empty string.

Kinds lists them, for a host's metrics.

View Source
var MagicPiece = []byte("bdl\x01")

MagicPiece is a leaves piece's magic.

Functions

func CheckAnswer

func CheckAnswer(leaves []*Leaf) error

CheckAnswer applies C3 to C6 to what an origin answers: the leaves are one zone, so C4 and C5 hold whatever scope a leaf was written with.

func CheckKey

func CheckKey(b []byte) error

CheckKey holds b to the key rule of spec section 1: a canonical compressed point (bcommon guard.ParsePubKey) that is not the anyone key.

func CheckLeaves

func CheckLeaves(leaves []*Leaf) error

CheckLeaves applies checks C3 to C6 of spec section 8 to one state's leaves, in that order, each over all the leaves in the order given, and returns the first refusal:

C3 ErrRendererType  a leaf of a type the renderer writes or that is not data
C4 ErrCNAME         a CNAME at the apex, of more than one record, or at a
                    name with another type under the same scope
C5 ErrCut           a leaf at or below a name with an NS leaf, off the apex
C6 ErrNoName        a record of a type that names a host with no name part

C5 reads a delegation as cutting every leaf an origin could answer beside it: of the same scope, or with either scope empty. It lets through the NS leaves at the cut, DS at the cut, and A and AAAA leaves whose name an NS record of the cut gives as a single name part relative to the origin.

func CheckOrder

func CheckOrder(leaves []*Leaf) error

CheckOrder holds leaves to key order with no key twice: ErrDuplicate for two leaves of one key, ErrOrder for a leaf before its place.

func Commitment

func Commitment(originID, secret [32]byte) [32]byte

Commitment is what a record set lists for an origin it answers for: SHA-256 of the label, the origin's identifier and its secret.

func EncodeLeaves

func EncodeLeaves(leaves []*Leaf, maxCount, maxSize int) ([]byte, error)

EncodeLeaves writes the leaves object: the canonical CBOR array of the leaves in key order. It refuses a leaf DecodeLeaf refuses, leaves out of order, a key twice, more than maxCount leaves and more than maxSize bytes.

func EncodeName

func EncodeName(n Name) ([]byte, error)

EncodeName writes a relative name as its canonical CBOR array of text labels, refusing what DecodeName refuses.

func FundingScript

func FundingScript(key *ec.PublicKey) []byte

FundingScript is a funding output locked to key: <key> OP_CHECKSIG <62 64 02> OP_DROP. A chain tree's outputs and a leaves tree's are each this, under their own key.

func LeafKey

func LeafKey(name Name, typ uint16, scope []byte) []byte

LeafKey is the key of the leaf of a name, type and scope.

func LockingKey

func LockingKey(d pushdrop.Derivation, identity []byte) (*ec.PublicKey, error)

LockingKey is the key an identity's output is locked to under d.

func OriginID

func OriginID(genesis []byte, txid [32]byte, vout uint32) [32]byte

OriginID is an origin's identifier, as RecordSetID under its own label.

func Pieces

func Pieces(size uint64) uint64

Pieces is how many pieces an object of size bytes is cut into.

func Reason

func Reason(err error) string

Reason is the fixed label of a refusal: bcommon's record labels, then this package's, else "other".

func Reasons

func Reasons() []string

Reasons are every label Reason gives but "other": bcommon's record labels, then this package's. A host presets a counter for each.

func RecordSetID

func RecordSetID(genesis []byte, txid [32]byte, vout uint32) [32]byte

RecordSetID is a record set's identifier: SHA-256 of the label, the genesis key, and the anchor: the outpoint its first state spent, the txid in hash byte order.

func Root

func Root(leaves []*Leaf) ([32]byte, error)

Root is the RFC 6962 root over leaves, which are in key order: each leaf's hash is commit.HashLeaf of its encoding.

func Signed

func Signed(tag, rec []byte, spent transaction.Outpoint) []byte

Signed is what a token's field signature covers: the tag, the record and the outpoint the token spends (its predecessor's, or the anchor at seq 0), the txid in hash byte order then the index as four big-endian bytes. The outpoint binds the signature to the token's place in the chain: the output copied into a transaction that spends anything else does not verify.

func SignedBy

func SignedBy(tx *transaction.Transaction, vout int, c Chain, identity []byte, spent transaction.Outpoint) bool

SignedBy reports whether the token at output vout of tx carries a strict field signature of identity's "state" key over its tag, its record and the outpoint spent.

func SortLeaves

func SortLeaves(leaves []*Leaf)

SortLeaves puts leaves in key order.

func TokenLock

func TokenLock(ctx context.Context, signer wallet.Interface, originator string, s *State, spent transaction.Outpoint) (*script.Script, error)

TokenLock builds a state token's locking script: locked to derive(owner, "state") and signed, over Signed's bytes for the outpoint the token will spend, by signer's wallet under its own "state" key. signer is the owner's wallet, or at a change of owner the previous owner's.

func TokenScript

func TokenScript(key *ec.PublicKey, tag, rec, sig []byte) []byte

TokenScript is a state token's output: <key> OP_CHECKSIG <tag> <record> <signature> OP_2DROP OP_DROP. The signature is over tag || record.

func Topic

func Topic(id [32]byte) string

Topic is the topic of a record set or an origin.

func TopicLetters

func TopicLetters(id [32]byte) string

TopicLetters is the identifier's first 16 bytes read as a big-endian number and written in base 26 with the digits a to z, padded on the left with a to 28 letters.

func ValidDomain

func ValidDomain(d string) bool

ValidDomain reports a lower-case DNS name in dotted form: at least two letter-digit-hyphen labels, 253 bytes at most, no trailing dot, and a last label that is not all digits.

func ValidLabel

func ValidLabel(l string) bool

ValidLabel reports a label: 1 to 63 bytes of lower-case letters, digits, hyphen and underscore, or the single byte "*".

Types

type Absence

type Absence struct {
	Before *Presence
	After  *Presence
}

Absence proves that a state holds no leaf in a range of keys: the leaf before the range and the leaf after it, adjacent in the state. Before is nil when the range sorts before every leaf, After when it sorts after every leaf, and both when the state has no leaves.

func (*Absence) Verify

func (a *Absence) Verify(root [32]byte, count uint64, key []byte) error

Verify holds the proof that no leaf has key in a state of count leaves under root. It trusts nothing of the prover: the neighbors are adjacent by index, or the first or the last leaf; each is under the root at its index; and key sorts strictly between their keys.

func (*Absence) VerifyName

func (a *Absence) VerifyName(root [32]byte, count uint64, name Name) error

VerifyName holds the proof that no leaf of any type or scope is at name.

type Admission

type Admission struct {
	Kind    Kind
	Txid    [32]byte
	Outputs []int
	Retain  []int
	// For a token: the state, whether the token is proven, the outpoint it
	// spends (its predecessor, or its anchor at seq 0), the unmined rival it
	// replaces, an unknown critical extension's kind, and a failed check.
	State    *State
	Mined    bool
	Spends   transaction.Outpoint
	Replaces *transaction.Outpoint
	Critical *uint64
	// Check is the label of the check of C2 to C6 the state fails, or "":
	// such a state is admitted and is not rendered (spec section 12.2).
	Check string
	// For a leaves piece: the piece and its funding outpoint.
	Piece   *Piece
	Funding transaction.Outpoint
}

Admission is what a host admits from one transaction.

func Admit

func Admit(ctx context.Context, beef []byte, held []int, h Admitter) (*Admission, error)

Admit applies the rules to one submission: the BEEF as it arrived, and held, the indices of its inputs that spend an output the topic holds. held decides only what is retained (every one of them), never a verdict. SPV of the BEEF is the engine's and is assumed to have run.

type Admitter

type Admitter struct {
	Zone    [32]byte
	Headers chaintracker.ChainTracker
	MaxBEEF int
	Now     func() time.Time
	View    View
}

Admitter is what admission needs beyond the submission: the topic's chain, a chain tracker over the host's headers, its BEEF bound (zero is the floor, DefaultMaxBEEF), its clock and its view of the topic.

type Attached

type Attached struct {
	Zone   [32]byte
	Kind   uint64
	Writer []byte
	Name   Name
	Expiry uint64
	Body   []byte
	Extra  cbor.Map
}

Attached is a record attached to a record set or an origin.

func DecodeAttached

func DecodeAttached(raw []byte) (*Attached, error)

DecodeAttached reads an attached record's payload, key by key. A member's body is held to the member form; every other kind's body is opaque to this version and only bounded.

func (*Attached) Encode

func (a *Attached) Encode() ([]byte, error)

Encode writes the payload canonically, refusing what DecodeAttached refuses.

type Chain

type Chain string

Chain is which of the two chains a topic carries.

const (
	ChainRecordSet Chain = "record-set"
	ChainOrigin    Chain = "origin"
)

The chains.

func ChainOf

func ChainOf(id [32]byte, genesis []byte, txid [32]byte, vout uint32) (Chain, bool)

ChainOf is the chain whose identifier id is, for a genesis key and an anchor: a record set's and an origin's are hashed under different labels.

func (Chain) Derivation

func (c Chain) Derivation(keyID string) pushdrop.Derivation

Derivation is the chain's derivation of keyID, with counterparty anyone, so anyone derives a key from the identity key alone.

func (Chain) Protocol

func (c Chain) Protocol() wallet.Protocol

Protocol is the chain's BRC-43 protocol.

func (Chain) Tag

func (c Chain) Tag() []byte

Tag is the chain's token tag.

type CheckFailure added in v0.1.1

type CheckFailure struct {
	// Number is the check, "C2" to "C6".
	Number string
	Err    error
}

CheckFailure is a refusal by one of checks C2 to C6 (spec section 8): the leaves object is the state's (C1 holds), and what it holds may not be rendered. A host admits such a state and keeps it for the chain's continuity (spec section 12.2).

func Failed added in v0.1.1

func Failed(err error) (*CheckFailure, bool)

Failed reports whether err is a refusal by one of checks C2 to C6, and which.

func (*CheckFailure) Error added in v0.1.1

func (e *CheckFailure) Error() string

func (*CheckFailure) Label added in v0.1.1

func (e *CheckFailure) Label() string

Label is the check's label (Reason).

func (*CheckFailure) Unwrap added in v0.1.1

func (e *CheckFailure) Unwrap() error

type Claim

type Claim string

Claim is what an output claims to be.

const (
	ClaimNone      Claim = ""
	ClaimRecordSet Claim = "record-set"
	ClaimOrigin    Claim = "origin"
	ClaimAttached  Claim = "attached"
	ClaimHost      Claim = "host"
	ClaimPiece     Claim = "leaves"
	ClaimFunding   Claim = "funding"
)

The claims.

func Classify

func Classify(s []byte) Claim

Classify says what a locking script claims, reading only its first push after <33-byte key> OP_CHECKSIG: a token's, an attached record's or a host record's tag; the start of a leaves piece; or, for the whole 40-byte script <key> OP_CHECKSIG <tag> OP_DROP, the funding tag.

type Data

type Data []Part

Data is one resource record's data: 1 to 16 parts, no two byte parts adjacent. The codec does not know record types.

func DecodeData

func DecodeData(raw []byte) (Data, error)

DecodeData reads one record's data from its canonical CBOR array.

func (Data) Check

func (d Data) Check() error

Check holds data to section 3.

func (Data) Encode

func (d Data) Encode() ([]byte, error)

Encode writes the data as its canonical CBOR array, refusing what DecodeData refuses.

func (Data) HasName

func (d Data) HasName() bool

HasName reports whether the data holds a name part.

func (Data) Join

func (d Data) Join() Data

Join returns the data with adjacent byte parts joined, as an encoder writes it. A joined part over 4,096 bytes is still refused by Check.

func (Data) Wire

func (d Data) Wire(origin []string) []byte

Wire is the data in DNS wire form under origin, the origin's labels leftmost first: the parts in order, each name written uncompressed with its root byte, and the origin's labels appended to a relative one. It is nil when the data does not hold to section 3, when origin has a label that is not 1 to 63 bytes, or when a name would pass 255 bytes on the wire.

type Extension

type Extension struct {
	Kind     uint64
	Critical bool
	Body     []byte
}

Extension is one extension of a state: a kind, whether a reader that does not know the kind must stop, and a body of at most 1,024 bytes.

type Held

type Held struct {
	Outpoint transaction.Outpoint
	State    *State
	Mined    bool
}

Held is a state the host keeps: its token's outpoint, the state, and whether the host knows the token mined.

type Host

type Host struct {
	Host      []byte
	Addresses [][]byte
	Port      uint16
	Protocols []string
	CA        string
	Account   string
	Location  string
	Expiry    uint64
	Extra     cbor.Map
}

Host is a host's claim of where it answers.

func DecodeHost

func DecodeHost(raw []byte) (*Host, error)

DecodeHost reads a host record's payload, key by key. Addresses and protocols ascend by their own bytes.

func (*Host) Encode

func (h *Host) Encode() ([]byte, error)

Encode writes the payload canonically, refusing what DecodeHost refuses.

type Kind

type Kind string

Kind is what an admitted transaction is to the topic.

const (
	KindRecordSet Kind = "record-set"
	KindOrigin    Kind = "origin"
	KindLeaves    Kind = "leaves"
)

The kinds.

type Leaf

type Leaf struct {
	Name Name
	Type uint16 // 1 to 65,535
	TTL  uint32 // 0 to 2,147,483,647
	// Data is 1 to 64 records' data, distinct, ascending by encoded bytes.
	Data []Data
	// Scope is empty, or the 32-byte identifier of the one origin this leaf
	// stands under.
	Scope []byte
}

Leaf is one record set in the DNS sense: every record of one name and type (spec section 4). It is the canonical CBOR array [name, type, ttl, data, scope].

func Answer

func Answer(recordSet, own []*Leaf, originID [32]byte) ([]*Leaf, error)

Answer is what an origin answers (spec section 6): the record set's leaves with an empty scope, then its leaves with the origin's identifier as scope, then the origin's own leaves; of leaves of one name and type the later wins. The result is ordered by name and type as leaf keys are, and its leaves keep the scope they were written with.

Both inputs are in key order with no key twice. An own leaf with a scope is refused (ErrScope), and so is a result that does not hold to checks C3 to C6 read as one zone (CheckAnswer). The origin's domain is not known here: a name that would pass 255 bytes under it is the renderer's to leave out and report.

func DecodeLeaf

func DecodeLeaf(raw []byte) (*Leaf, error)

DecodeLeaf reads a leaf, refusing it for the first rule it breaks: the bound, canonical CBOR, an array of five items, then item by item.

func DecodeLeaves

func DecodeLeaves(raw []byte, maxCount, maxSize int) ([]*Leaf, error)

DecodeLeaves reads a leaves object of at most maxSize bytes and maxCount leaves: the bound, canonical CBOR, an array, then leaf by leaf, each within MaxLeaf and after the one before it in key order.

func (*Leaf) Encode

func (l *Leaf) Encode() ([]byte, error)

Encode writes the leaf canonically. It refuses what DecodeLeaf refuses, so a leaf this package writes is one it reads.

func (*Leaf) Key

func (l *Leaf) Key() []byte

Key is the leaf's key: for each label from the last to the first, the label then one zero byte; then one more zero byte; then the type as two big-endian bytes; then the scope, or 32 zero bytes when it is empty. Leaves are ordered by their keys as byte strings, which orders their names as RFC 4034 section 6.1 does.

func (*Leaf) SortData

func (l *Leaf) SortData() error

SortData puts a leaf's records in the order a leaf carries them and drops repeats. It returns the first refusal of a record's data.

type Member

type Member struct {
	Host []byte
	Rule uint64
}

Member is the body of an attached record of kind member: the canonical CBOR map {1: host key, 2: rule}, and no other key. A different body is a different kind.

func DecodeMember

func DecodeMember(raw []byte) (*Member, error)

DecodeMember reads a member body.

func (*Member) Encode

func (m *Member) Encode() ([]byte, error)

Encode writes the body canonically, refusing what DecodeMember refuses.

type Name

type Name []string

Name is a name of spec section 2: labels, leftmost first, each a CBOR text string. A relative name stands under an origin a record does not know; no labels is the origin itself. An absolute name is the same form read as a full domain name; no labels is the root.

func DecodeName

func DecodeName(raw []byte) (Name, error)

DecodeName reads a relative name from its canonical CBOR array.

func (Name) Check

func (n Name) Check() error

Check holds a relative name to section 2: every label a label, "*" only first, and at most 200 bytes with the labels' length bytes.

func (Name) CheckAbsolute

func (n Name) CheckAbsolute() error

CheckAbsolute holds an absolute name to section 2: as Check, and at most 255 bytes with the labels' length bytes and the root byte.

func (Name) Equal

func (n Name) Equal(o Name) bool

Equal reports the same labels in the same order.

func (Name) KeyPrefix

func (n Name) KeyPrefix() []byte

KeyPrefix is what every leaf key of exactly this name starts with: the labels last to first, each followed by one zero byte, then one more zero byte.

func (Name) String

func (n Name) String() string

String is the name in dotted form, "@" for no labels.

func (Name) WireLen

func (n Name) WireLen() int

WireLen is the name's length on the wire without a root byte: each label and its length byte.

type Origin

type Origin struct {
	ID      [32]byte
	Seq     uint64
	Genesis []byte
	Owner   []byte
	// AnchorTxid and AnchorVout are the outpoint the chain's seq 0 token
	// spent (key 5), as a record set's.
	AnchorTxid [32]byte
	AnchorVout uint32
	Base       [32]byte
	Secret     [32]byte
	Serial     uint32
	Root       [32]byte
	Count      uint64
	Data       [32]byte
	Size       uint64
	Recovery   []byte
	Signing    [][]byte
	Extensions []Extension
	Lapse      uint64
	// Extra holds keys above OKLast, verbatim.
	Extra cbor.Map
}

Origin is one state of an origin: a zone apex that answers with a record set's leaves and its own. It does not carry its domain.

func DecodeOrigin

func DecodeOrigin(raw []byte) (*Origin, error)

DecodeOrigin reads an origin state, key by key, then the rules that join keys, as DecodeRecordSet.

func (*Origin) AnswersWith

func (o *Origin) AnswersWith(s *RecordSet) bool

AnswersWith reports whether the origin answers with the record set's state s: s is a state of the origin's base, and it lists the origin's commitment. That s is the record set's latest state is the caller's.

func (*Origin) Check

func (o *Origin) Check(object []byte) ([]*Leaf, error)

Check applies checks C1 to C6 to an origin state and its own leaves object.

func (*Origin) Commit

func (o *Origin) Commit(leaves []*Leaf) ([]byte, error)

Commit is RecordSet.Commit for an origin state and its own leaves, none of which has a scope.

func (*Origin) Commitment

func (o *Origin) Commitment() [32]byte

Commitment is the value a record set lists to answer for this origin.

func (*Origin) Encode

func (o *Origin) Encode() ([]byte, error)

Encode writes the record canonically, refusing what DecodeOrigin refuses.

func (*Origin) Precedes

func (o *Origin) Precedes(next *Origin) bool

Precedes reports whether next may follow o in one origin's chain: the same identifier, genesis key, outpoint and recovery, one more seq, and a greater serial.

func (*Origin) UnknownCritical

func (o *Origin) UnknownCritical(known func(kind uint64) bool) (uint64, bool)

UnknownCritical is RecordSet.UnknownCritical for an origin state.

type Part

type Part struct {
	// Bytes is a byte part: 1 to 4,096 bytes. Unused by a name part.
	Bytes []byte
	// IsName marks a name part. Absolute is its form: false for a name
	// relative to the origin, true for an absolute name.
	IsName   bool
	Absolute bool
	Name     Name
}

Part is one part of a record's data (spec section 3): wire-format bytes copied as they are, or a name the renderer writes out.

func Abs

func Abs(labels ...string) Part

Abs is an absolute name part.

func Raw

func Raw(b []byte) Part

Raw is a byte part.

func Rel

func Rel(labels ...string) Part

Rel is a name part relative to the origin.

type Piece

type Piece struct {
	Zone       [32]byte
	Genesis    []byte
	AnchorTxid [32]byte
	AnchorVout uint32
	Writer     []byte
	Data       [32]byte
	Size       uint64
	Index      uint64
	Bytes      []byte
	// Chain is the chain Zone names, set by DecodePiece.
	Chain Chain
	// Extra holds keys above PKLast, verbatim.
	Extra cbor.Map
}

Piece is one piece of a leaves object: the object cut every MaxPiece bytes, so one object has one cutting.

func Cut

func Cut(s *State, writer, object []byte) []*Piece

Cut cuts a leaves object into its pieces for a chain and a writer.

func DecodePiece

func DecodePiece(raw []byte) (*Piece, error)

DecodePiece reads a leaves piece: the record rules with its own bound, key by key, then the rules that join keys, in this order: the zone is the identifier the genesis key and the anchor give, as a record set or as an origin (id); then the object is within its chain's bound, the index is one of its pieces and the bytes are that piece's length (piece).

func (*Piece) Encode

func (p *Piece) Encode() ([]byte, error)

Encode writes the piece canonically, refusing what DecodePiece refuses.

type Presence

type Presence struct {
	Leaf  *Leaf
	Index uint64
	Path  [][32]byte
}

Presence proves one leaf of a state: the leaf, its index, and its RFC 6962 audit path, leaf to root. The state's count and root are the verifier's own, from the state.

func (*Presence) Verify

func (p *Presence) Verify(root [32]byte, count uint64) error

Verify recomputes the root from the leaf and its path and holds it to root, in a state of count leaves.

type Proof

type Proof struct {
	// Whole is every leaf at Name, whatever its type and scope; else the
	// one leaf of Name, Type and Scope.
	Whole bool
	Name  Name
	Type  uint16
	Scope []byte
	// Present are the leaves there, in key order.
	Present []*Presence
	// Before and After are the leaves next to the key or the name's leaves:
	// nil at either end of the state, and both nil for one leaf proven
	// present, which needs no neighbor.
	Before, After *Presence
}

Proof is the byte form of a proof against one state (spec section 13): for one key (a name, a type and a scope) or for a whole name, the leaves there, each with its path, and the neighbors that show there is no other. It answers both ways: Present holds what is there, and when it is empty the neighbors prove absence (section 4).

func DecodeProof

func DecodeProof(raw []byte) (*Proof, error)

DecodeProof reads a proof's byte form: the bound, canonical CBOR, the seven items, each of its type and within its rule. It decides nothing about whether the proof holds: Verify does, against a state.

func (*Proof) Encode

func (p *Proof) Encode() ([]byte, error)

Encode writes the proof as its canonical CBOR array [form, name, type, scope, present, before, after], refusing what DecodeProof refuses.

func (*Proof) Verify

func (p *Proof) Verify(root [32]byte, count uint64) error

Verify holds the proof against a state of count leaves under root. It trusts nothing of the prover. With no leaf present it is section 4's proof of absence. One key present is the leaf under the root, with that key and no neighbor. A whole name present is its leaves, adjacent by index, each under the root with the name's prefix, and on either side a neighbor outside the name, or the end of the state.

type RecordSet

type RecordSet struct {
	ID      [32]byte
	Seq     uint64
	Genesis []byte
	Owner   []byte
	// AnchorTxid and AnchorVout are the outpoint the chain's seq 0 token
	// spent (key 5): the txid in hash byte order. With Genesis it is hashed
	// into ID.
	AnchorTxid [32]byte
	AnchorVout uint32
	Root       [32]byte
	Count      uint64
	Data       [32]byte
	Size       uint64
	Accepts    [][32]byte
	Extensions []Extension
	Lapse      uint64
	// Extra holds keys above RKLast, verbatim.
	Extra cbor.Map
}

RecordSet is one state of a record set: the leaves it commits to and the origins it answers for.

func DecodeRecordSet

func DecodeRecordSet(raw []byte) (*RecordSet, error)

DecodeRecordSet reads a record set state, refusing it for the first rule it breaks: the record rules (too-large, cbor, key-type, magic), then key by key in key order (missing, type, the key's own rule), then the rules that join keys, in this order: at seq 0 the owner is the genesis key (genesis), and the identifier is the one the genesis key and the anchor give (id).

func (*RecordSet) Check

func (s *RecordSet) Check(object []byte) ([]*Leaf, error)

Check applies checks C1 and C3 to C6 of spec section 8 to the state and its leaves object, and returns the leaves or the first refusal. A refusal by C2 to C6 is a CheckFailure, returned with the leaves.

func (*RecordSet) Commit

func (s *RecordSet) Commit(leaves []*Leaf) ([]byte, error)

Commit sets the state's root, count, data and size from leaves and returns the leaves object it then commits to.

func (*RecordSet) Encode

func (s *RecordSet) Encode() ([]byte, error)

Encode writes the record canonically. It refuses what DecodeRecordSet refuses, so a record this package writes is one it reads.

func (*RecordSet) Lists

func (s *RecordSet) Lists(c [32]byte) bool

Lists reports whether the state lists commitment c.

func (*RecordSet) Precedes

func (s *RecordSet) Precedes(next *RecordSet) bool

Precedes reports whether next may follow s in one record set's chain: the same identifier, genesis key and outpoint, one more seq. The token transactions' own link, and who signs a change of owner, are the module's to check.

func (*RecordSet) UnknownCritical

func (s *RecordSet) UnknownCritical(known func(kind uint64) bool) (uint64, bool)

UnknownCritical reports the first critical extension of a kind the reader does not know. When it reports one, the reader stops rendering the chain at this state and says so. Version 1 defines no kind.

type State

type State struct {
	Chain  Chain
	Set    *RecordSet // a record set's state
	Origin *Origin    // an origin's
	// Record is the record's bytes, as the token carries them.
	Record []byte
}

State is one state of either chain, read through what the two share.

func DecodeState

func DecodeState(c Chain, rec []byte) (*State, error)

DecodeState reads rec as a state of chain c.

func NewState

func NewState(set *RecordSet, origin *Origin) (*State, error)

NewState wraps a record set state or an origin state, encoded.

func ReadState

func ReadState(tx *transaction.Transaction) (*State, int, error)

ReadState reads the one state token a stored transaction holds, without its parent: how an index reads a token it admitted. The field signature is not checked here, since it covers the outpoint spent and at a change of owner is the previous owner's; SignedBy checks it once both are known.

func (*State) Anchor

func (s *State) Anchor() transaction.Outpoint

Anchor is the outpoint the chain's first token spent.

func (*State) Check

func (s *State) Check(object []byte) ([]*Leaf, error)

Check applies checks C1 to C6 to the state and its leaves object. A refusal by C2 to C6 is a CheckFailure, returned with the leaves.

func (*State) Count

func (s *State) Count() uint64

Count is the number of leaves.

func (*State) Data

func (s *State) Data() [32]byte

Data and Size commit to the leaves object's bytes.

func (*State) Follows

func (s *State) Follows(prev *State) error

Follows holds s to the continuity rules after prev (spec section 12), in their order: the same chain, identifier, genesis key and anchor and one more seq (ErrContinuity); for an origin a greater serial (ErrSerial) and the same recovery (ErrRecovery).

func (*State) Genesis

func (s *State) Genesis() []byte

Genesis is the owner at seq 0.

func (*State) ID

func (s *State) ID() [32]byte

ID is the chain's identifier.

func (*State) Lapse

func (s *State) Lapse() uint64

Lapse is when an unmined token of this state stops being believed.

func (*State) Owner

func (s *State) Owner() []byte

Owner is who owns the chain from this state.

func (*State) Root

func (s *State) Root() [32]byte

Root and Count commit to the state's leaves.

func (*State) Seq

func (s *State) Seq() uint64

Seq is the state's place in the chain.

func (*State) Signer

func (s *State) Signer(prev *State) []byte

Signer is the key that signs a token of this state: its owner, or at a change of owner the previous state's. prev is nil at seq 0.

func (*State) Size

func (s *State) Size() uint64

Size is the leaves object's length.

func (*State) UnknownCritical

func (s *State) UnknownCritical(known func(kind uint64) bool) (uint64, bool)

UnknownCritical reports the first critical extension of a kind known does not report.

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

Tree is a state's leaves in key order with their keys and hashes: what a holder of the leaves object proves from.

func NewTree

func NewTree(leaves []*Leaf) (*Tree, error)

NewTree hashes leaves, which are in key order with no key twice. It refuses a leaf DecodeLeaf refuses.

func (*Tree) Count

func (t *Tree) Count() uint64

Count is the number of leaves.

func (*Tree) Find

func (t *Tree) Find(key []byte) (int, bool)

Find is the index of the leaf with key, and whether there is one.

func (*Tree) Prove

func (t *Tree) Prove(index int) (*Presence, error)

Prove proves the leaf at index.

func (*Tree) ProveAbsent

func (t *Tree) ProveAbsent(key []byte) (*Absence, error)

ProveAbsent proves that no leaf has key, or returns ErrPresent.

func (*Tree) ProveAbsentName

func (t *Tree) ProveAbsentName(name Name) (*Absence, error)

ProveAbsentName proves that no leaf of any type or scope is at name, or returns ErrPresent.

func (*Tree) ProveKey

func (t *Tree) ProveKey(name Name, typ uint16, scope []byte) (*Proof, error)

ProveKey proves the leaf of a name, a type and a scope, or that there is none.

func (*Tree) ProveName

func (t *Tree) ProveName(name Name) (*Proof, error)

ProveName proves every leaf at a name, or that there is none. A name of more than MaxProofLeaves leaves is refused (too-large): its leaves are asked for one key at a time.

func (*Tree) Root

func (t *Tree) Root() [32]byte

Root is the RFC 6962 root over the leaves.

type View

type View interface {
	// At is the state kept at seq, or nil.
	At(seq uint64) *Held
	// Tip is the kept state of the highest seq, or nil.
	Tip() *Held
	// HasPiece reports whether a piece of the object of data and size is
	// held at index.
	HasPiece(data [32]byte, size, index uint64) bool
	// Loose is how many pieces are held that no kept state names.
	Loose() int
	// Object is the leaves object of data and size joined from the pieces
	// held, or nil while a piece is missing. The bytes are not checked.
	Object(ctx context.Context, data [32]byte, size uint64) ([]byte, error)
}

View is what admission reads of what the topic holds, and nothing more: the states kept and the leaves pieces held.

Jump to

Keyboard shortcuts

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