Documentation
¶
Overview ¶
Package siterec is bweb's record codec: the four records a site's topic carries (docs/spec.md). Each 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 | | Site | "bws" 0x01 | a mined chain token under the bucket owner | | Terms | "bwt" 0x01 | a mined record beside a hosting output | | Advertisement | "bwa" 0x01 | a mined PushDrop, one per host and site | | Outcome | "bwo" 0x01 | an unmined carrier, by whoever audited |
Index ¶
- Constants
- Variables
- func FundingScript(key *ec.PublicKey) []byte
- func HostingScript(key *ec.PublicKey) []byte
- func LockingKey(d pushdrop.Derivation, identity []byte) (*ec.PublicKey, error)
- func Reason(err error) string
- func Reasons() []string
- func SameChain(prev, next *Site) bool
- func Settable(name string) bool
- func SiteID(owner []byte, txid [32]byte, vout uint32) [32]byte
- func TaggedScript(key *ec.PublicKey, tag, rec, sig []byte) []byte
- func Topic(site [32]byte) string
- func TopicSite(topic string) ([32]byte, bool)
- func ValidHost(h string) bool
- func ValidKey(k string) bool
- func ValidServeURL(s string) bool
- type Admission
- type Advertisement
- type Claim
- type Header
- type Host
- type Kind
- type Outcome
- type Rule
- type Site
- type Terms
Constants ¶
const ( MaxTokenBEEF = 196608 // a site token and its parent MaxMinedBEEF = 65536 // a mined terms or advertisement transaction alone MaxCarrierBEEF = 81920 // an outcome carrier and its funding tree MaxAdsPerTx = 64 DefaultMaxBEEF = 262144 )
Bounds on what a host admits.
const ( AKMagic = 0 AKHost = 1 // the host's identity key, 33 bytes AKSite = 2 // the site, 32 bytes AKURL = 3 // where it serves: the scheme, the address and the port; no path AKLocation = 4 // an IATA metro code, three upper-case letters AKCountry = 5 // ISO 3166-1 alpha-2 AKSubdivision = 6 // ISO 3166-2 (e.g. "US-CA"), or "" when not given AKExpiry = 7 // Unix seconds after which it is not a candidate AKCertificate = 8 // reserved for an address certificate; empty AKLast = AKCertificate )
The serving advertisement's keys (docs/spec.md section 4). It is the payload of a mined PushDrop signed by the host's key: one per host and site, canceled by spending, renewed by a successor that spends it.
const ( OKMagic = 0 OKSite = 1 // the site, 32 bytes OKAdTxid = 2 // the advertisement challenged OKAdVout = 3 OKToken = 4 // the site's bucket token the samples were proven against, its txid OKSamples = 5 // samples taken OKPassed = 6 // samples that passed OKPass = 7 // the verdict: every sample passed OKAt = 8 // Unix seconds, when the challenge ended OKAuditor = 9 // the key of whoever ran it, 33 bytes OKLast = OKAuditor )
The outcome record's keys (docs/spec.md section 5): what one serving challenge found, published by whoever ran it.
const ( SKMagic = 0 SKSite = 1 // the site's identifier: 32 bytes, fixed by its first token SKBucket = 2 // the bstore bucket it serves: its name SKOwner = 3 // the bucket's owner key, 33 bytes: the only key that signs a site record SKBucketID = 4 // the bucket's identifier, 32 bytes SKSeq = 5 // 0 for the first token, then one more each token SKPrev = 6 // the previous token's txid, hash byte order; zero at seq 0 SKHosts = 7 // the host names it answers to: 1 to 16, lower case, distinct, ascending SKIndex = 8 // the index document a path ending in "/" serves SKError = 9 // the error document's key, or "" for none SKPublic = 10 // whether the site is served SKHeaders = 11 // header rules, in order SKCreated = 12 // Unix seconds, the owner's claim; the token's block is the proven bound SKOrigin = 13 // the outpoint the chain's seq 0 spent: txid (hash byte order) || uint32be(vout) SKLast = SKOrigin )
The site record's keys (docs/spec.md section 2).
const ( MaxRecord = 16 << 10 // any record, encoded MaxHosts = 16 // host names a site answers to MaxRules = 32 // header rules a site sets MaxRuleSets = 8 // headers one rule sets MaxURL = 512 MaxHeader = 1024 // a header rule's value MaxSafe = 1<<53 - 1 )
Bounds on what a record carries.
const ( TKMagic = 0 TKSite = 1 // the site, 32 bytes TKVout = 2 // the hosting output's index in the terms' own transaction TKSatoshis = 3 // its value: what the race pays out, all of it TKT = 4 // servers that must pass before any is paid TKK = 5 // servers paid, by rank TKTerm = 6 // seconds a paid server must stay advertised and serving TKDeadline = 7 // blocks after the hosting output mines when its owner may reclaim it unpaid TKCheckC = 8 // samples in a serving challenge TKFirstByte = 9 // the most milliseconds a sample's first byte may take TKCreated = 10 // Unix seconds, the publisher's claim TKOwner = 11 // the publisher's key, 33 bytes: it signs the record and locks the hosting output TKLast = TKOwner )
The hosting terms record's keys (docs/spec.md section 3): bstore's eight hold elements, for a site and a term.
const ( MaxK = 64 MinCheckC = 1 MaxCheckC = 1024 MaxTerm = 366 * 24 * 3600 MaxDeadline = 52_560 // about a year of blocks MaxFirstByte = 60_000 MaxSatoshis = 2_100_000_000_000_000 )
Bounds on the race a terms record names.
const ( ProtocolName = "bweb origin" // BRC-43 names are at least 5 characters KeySite = "site" KeySiteChain = "site-chain" KeyTerms = "terms" KeyHosting = "hosting" KeyServe = "serve" KeyOutcome = "outcome" // TopicPrefix and the site id in lower-case hex name a site's topic. TopicPrefix = "tm_bweb_" LookupService = "ls_bweb" // DomainSiteID is hashed into a site id. DomainSiteID = "bweb site id v1" )
Names (docs/spec.md section 6).
Variables ¶
var ( ErrShape = errors.New("siterec: transaction has the wrong shape") ErrBEEF = chaintoken.ErrBEEF ErrMineable = errors.New("siterec: carrier is mineable") ErrUnlock = errors.New("siterec: unlocking script is not one canonical signature push") ErrLock = errors.New("siterec: output script is not the canonical one for its key") ErrFieldSig = errors.New("siterec: field signature is not strict or does not verify") ErrFunding = errors.New("siterec: carrier does not spend a funding output of its key, or does not carry its value") ErrUnmined = errors.New("siterec: proof does not verify against the host's headers") ErrTokenInput = errors.New("siterec: token does not spend its predecessor, or its site chain tree at seq 0") ErrSiteID = errors.New("siterec: site is not the site id of the outpoint the first token spends") ErrSite = errors.New("siterec: record is not for this topic's site") ErrHosting = errors.New("siterec: terms do not name a hosting output of their owner in their own transaction") ErrTooMany = errors.New("siterec: too many advertisements in one transaction") ErrNotBweb = errors.New("siterec: transaction is not for this topic") ErrHostConfig = errors.New("siterec: host configuration or previous coins out of range") )
The transaction refusals (docs/spec.md section 8), beside the record refusals. A transaction is refused for the first rule it breaks.
var ( MagicSite = []byte("bws\x01") MagicTerms = []byte("bwt\x01") MagicAdvertisement = []byte("bwa\x01") MagicOutcome = []byte("bwo\x01") )
The records' magics: four bytes, the last the version.
var ( ErrHost = errors.New("siterec: a host name is not a lower-case DNS name") ErrHosts = errors.New("siterec: host names are missing, over the bound, or not distinct and ascending") ErrIndex = errors.New("siterec: the index document is not a file name") ErrKey = errors.New("siterec: not an object key") ErrRule = errors.New("siterec: a header rule is malformed or sets a header that describes the bytes") ErrBucket = errors.New("siterec: not a bstore bucket name") ErrOwner = errors.New("siterec: not a canonical compressed public key, or the anyone key") ErrRace = errors.New("siterec: the race's t, K and checkC are out of order") ErrURL = errors.New("siterec: not an http or https URL") ErrLocation = errors.New("siterec: not an IATA Location, ISO 3166 country or subdivision") ErrCount = errors.New("siterec: more samples passed than were taken") )
Refusals beyond bcommon's record ones. Reason gives each a fixed label.
var ( TagSite = []byte{'b', 'w', 0x01} TagFunding = []byte{'b', 'w', 0x02} TagHosting = []byte{'b', 'w', 0x03} TagServe = []byte{'b', 'w', 0x04} TagTerms = []byte{'b', 'w', 0x05} )
Tags under the prefix "bw".
var ( Protocol = wallet.Protocol{SecurityLevel: wallet.SecurityLevelEveryApp, Protocol: ProtocolName} SiteDerivation = pushdrop.Derivation{Protocol: Protocol, KeyID: KeySite} SiteChainDerivation = pushdrop.Derivation{Protocol: Protocol, KeyID: KeySiteChain} TermsDerivation = pushdrop.Derivation{Protocol: Protocol, KeyID: KeyTerms} HostingDerivation = pushdrop.Derivation{Protocol: Protocol, KeyID: KeyHosting} ServeDerivation = pushdrop.Derivation{Protocol: Protocol, KeyID: KeyServe} OutcomeDerivation = pushdrop.Derivation{Protocol: Protocol, KeyID: KeyOutcome} )
Derivations under [1, "bweb"], each with counterparty anyone, so anyone derives a key from the identity key alone.
var Kinds = []Kind{KindSite, KindTerms, KindAds, KindOutcome}
Kinds lists them, for a host's metrics.
Functions ¶
func FundingScript ¶ added in v0.2.0
FundingScript is a funding output locked to key: <key> OP_CHECKSIG <62 77 02> OP_DROP. A site chain tree's outputs and an auditor's outcome tree's are each this, under their own key.
func HostingScript ¶ added in v0.2.0
HostingScript is a hosting output locked to key: <key> OP_CHECKSIG <62 77 03> OP_DROP.
func LockingKey ¶ added in v0.2.0
LockingKey is the key an identity's output is locked to under d (bstore's derivation, with bweb's protocol).
func Reason ¶
Reason is the fixed label of a refusal: bcommon's record labels, then this package's, else "other".
func Reasons ¶ added in v0.2.0
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 SameChain ¶
SameChain reports whether next may follow prev in one site's chain: the same site, bucket, owner and origin, one more seq. The token transactions' own link (next spends prev) is the module's to check.
func Settable ¶
Settable reports whether a header rule may set name: never the headers that describe the bytes, which are the store's and proven.
func SiteID ¶ added in v0.2.0
SiteID is a site's identifier: SHA-256 of the domain, the owner's key (33 bytes), then the outpoint its first token spends (the txid in hash byte order, the index as four big-endian bytes). The owner is hashed in so that a token past seq 0, read alone, still proves its owner made the site: another key's chain cannot carry this site id.
func TaggedScript ¶ added in v0.2.0
TaggedScript is a tagged record output: <key> OP_CHECKSIG <tag> <record> <signature> OP_2DROP OP_DROP. The signature is over tag || record.
func TopicSite ¶ added in v0.2.0
TopicSite reads the site id back from a topic name: the prefix and 64 lower-case hex digits.
func ValidHost ¶
ValidHost reports a lower-case DNS host name: at least two labels, each a letter-digit-hyphen label, 253 bytes at most, and a last label that is not all digits (so an IPv4 address is never a host name).
func ValidKey ¶
ValidKey reports an object key a site may name: a bstore object key (1 to 1,024 bytes, one line: no control character, U+2028 or U+2029) with no leading slash and no empty, "." or ".." segment.
func ValidServeURL ¶
ValidServeURL reports an origin a host may serve a site at, in its one canonical spelling: "http://" or "https://", then a host, then optionally ":" and a port from 1 to 65535 without leading zeros, and nothing else (no user information, path, trailing "/", query or fragment). The host is a lower-case DNS name (ValidHost), a dotted IPv4 address, or an IPv6 address in brackets, in its shortest lower-case form, with no zone.
Types ¶
type Admission ¶ added in v0.2.0
type Admission struct {
Kind Kind
Txid [32]byte
Outputs []int
Retain []int
// Site, Spends (the outpoint the token spends) and, past seq 0, the
// predecessor's output index in Spends' transaction, for a site token.
Site *Site
Spends transaction.Outpoint
// Terms and its output index, for terms; the hosting output is
// Terms.Vout.
Terms *Terms
TermsVout int
// Ads by output index, for advertisements.
Ads map[int]*Advertisement
// Outcome and the funding outpoint, for an outcome.
Outcome *Outcome
Funding transaction.Outpoint
}
Admission is what a host admits from one transaction.
func Admit ¶ added in v0.2.0
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 Advertisement ¶
type Advertisement struct {
Host []byte
Site [32]byte
URL string
Location string
Country string
Subdivision string
Expiry uint64
Certificate []byte
Extra cbor.Map
}
Advertisement is a host's claim that it serves a site at an address.
func DecodeAdvertisement ¶
func DecodeAdvertisement(raw []byte) (*Advertisement, error)
DecodeAdvertisement reads an advertisement's payload, key by key. A subdivision, when given, is in the advertised country.
func (*Advertisement) Encode ¶
func (a *Advertisement) Encode() ([]byte, error)
Encode writes the payload canonically, refusing what DecodeAdvertisement refuses.
type Claim ¶ added in v0.2.0
type Claim string
Claim is what an output claims to be.
const ( ClaimNone Claim = "" ClaimSite Claim = "site" ClaimTerms Claim = "terms" ClaimAd Claim = "advertisement" ClaimOutcome Claim = "outcome" ClaimFunding Claim = "funding" ClaimHosting Claim = "hosting" )
The claims.
func Classify ¶ added in v0.2.0
Classify says what a locking script claims, reading only its first push after <33-byte key> OP_CHECKSIG: the tag of a site token, a terms record or an advertisement; the start of an outcome record; or, for the whole 40-byte script <key> OP_CHECKSIG <tag> OP_DROP, the funding or hosting tag.
type Host ¶ added in v0.2.0
type Host struct {
Site [32]byte
Headers chaintracker.ChainTracker
MaxBEEF int
}
Host is what admission needs beyond the submission: the topic's site, a chain tracker over the host's headers and its BEEF bound (zero is the floor, DefaultMaxBEEF). Nothing the topic holds is here: no rule reads another record, so a refusal never depends on what arrived first.
type Outcome ¶
type Outcome struct {
Site [32]byte
AdTxid [32]byte
AdVout uint32
Token [32]byte
Samples uint64
Passed uint64
Pass bool
At uint64
Auditor []byte
Extra cbor.Map
}
Outcome is one serving challenge's result.
func DecodeOutcome ¶
DecodeOutcome reads an outcome record, key by key, then: passed is at most samples, and the verdict is pass exactly when every sample passed.
type Rule ¶
type Rule struct {
Prefix string
Suffix string
Set []Header // distinct names, ascending by lower-cased name
}
Rule sets headers on keys with a prefix and a suffix (either may be ""). A rule matches the key served with a leading "/".
type Site ¶
type Site struct {
Site [32]byte
Bucket string
Owner []byte
BucketID [32]byte
Seq uint64
Prev [32]byte
Hosts []string
Index string
Error string
Public bool
Headers []Rule
Created uint64
// Origin is the outpoint the chain's seq 0 token spent, carried by
// every token: with Owner it is hashed into Site, so any token shows on
// its own that its owner made the site.
OriginTxid [32]byte
OriginVout uint32
// Extra holds keys above SKLast, verbatim.
Extra cbor.Map
}
Site is a site's settings: the S3 website configuration of one bucket, with the names it answers to. A site's key is its bucket owner's key.
func DecodeSite ¶
DecodeSite reads a site record, 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 seq 0 with a non-zero prev.
func ReadSite ¶ added in v0.2.0
func ReadSite(tx *transaction.Transaction) (*Site, int, error)
ReadSite reads the one site token a stored transaction holds, well formed, without its parent: how an index reads a token it admitted.
type Terms ¶
type Terms struct {
Site [32]byte
Vout uint32
Satoshis uint64
T, K uint64
Term uint64
Deadline uint64
CheckC uint64
FirstByte uint64
Created uint64
Owner []byte
Extra cbor.Map
}
Terms is a publisher's offer to pay the first K servers that pass a serving challenge, from one hosting output, for one term of a site. The hosting output is output Vout of the transaction that carries the record: a record cannot name its own transaction's id.
func DecodeTerms ¶
DecodeTerms reads a terms record: key by key, then 1 <= t <= K.