Documentation
¶
Overview ¶
Package engine is a bsecret publisher: the owner's tooling (docs/spec.md sections 7, 8, 12 and 13), run from one identity's home. It creates vaults on a borg group, seals and wraps each version of a secret, mints the vault's chain tokens, acknowledges a new epoch of the group and re-keys what the vault held, retracts by sweep, and publishes every object on the plane or to each host.
It is built on bcommon's producer package. A Payer pays for funding trees and vault tokens from the home's coin pool, and Trees mints the two kinds of funding tree the contract keeps apart (spec section 12.4): secret trees, which fund carriers and are minted ahead, so the next is on its way before the current runs out, and chain trees, which pay for a vault's first token. A tree is recorded with its fee coin before it reaches the settlement leg (Trees.Prepare), and a command that finds the record of a tree never adopted settles it before it spends (RecoverTrees). A funding tree is never published to a crypt on its own: every carrier carries its own, and a vault's first token carries its chain tree.
Every object is persisted in the home's state before it is sent: a carrier with the funding output it spends marked used and its commitment recorded, a transition with the record it signs. A run that stops part way sends the same bytes again (Resume), and never a second carrier on one funding output. A transition takes effect only when a transaction spending its predecessor is mined in the publisher's own headers and carries byte for byte the record built (spec section 7.1).
One goroutine runs a publisher. Every Payer the engine makes (NewPayer) and both Trees are over the home's one pool, and the library collects a tree minted ahead on whichever goroutine takes a coin while the pool has none (producer.Payer.Take), so all of them belong to the goroutine that runs the command. The engine starts none of its own: the only goroutines under it are the library's settlement of a tree minted ahead, which touches no Payer, pool or state, and a unicast publisher's requests to its hosts (unicast.Set), which touch none either.
The tooling folds before it signs. One token whose counts its change lists do not give ends a vault for good (spec section 11.2), so every transition is applied to a copy of the fold of the vault's own chain and held to the host's transition rules before it is built, and one that either would flag is refused here, unbuilt.
Index ¶
- Variables
- func CollectChange(ctx context.Context, pool *bwallet.Pool, asset nodeapi.ProofSource) int
- func Covered(owes []Stale, plan map[string]Choice, known func(key string) bool) (ignored []string, err error)
- func FeeError(err error) error
- func Fold(v *state.Vault) (*secrec.VaultFold, []*secrec.Vault, error)
- func Journal(st *state.State, pool *bwallet.Pool, carried []bwallet.Output)
- func Known(v *state.Vault, key string) bool
- func Mark(st *state.State, v *state.Vault, rec *secrec.Vault)
- func Minimal(mp *transaction.MerklePath, txid *chainhash.Hash) (*transaction.MerklePath, error)
- func Params(kind string) carrier.Params
- func Reconcile(ctx context.Context, st *state.State, pool *bwallet.Pool, ...) []bwallet.Output
- func RecordOf(x *state.SecretVersion) (*secrec.Secret, error)
- func Records(v *state.Vault) ([]*secrec.Vault, error)
- func Retractable(v *state.Vault, epoch uint64, interval, ended bool, retired map[string]bool) map[string][]uint32
- func Short(txid string) string
- func TokenBEEF(ctx context.Context, crypt string, token, parent *transaction.Transaction, ...) ([]byte, error)
- func Unjournal(st *state.State)
- type Change
- type Choice
- type Engine
- func (e *Engine) Anchor(ctx context.Context, w *Writer) (int, error)
- func (e *Engine) Close(ctx context.Context) error
- func (e *Engine) CreateVault(ctx context.Context, crypt, name, hall string, group [secrec.GroupIDLen]byte, ...) (*state.Vault, error)
- func (e *Engine) EpochKey(ctx context.Context, g *vread.Group, epoch uint64) ([32]byte, error)
- func (e *Engine) FinishSweep(ctx context.Context) error
- func (e *Engine) FinishTransitions(ctx context.Context) error
- func (e *Engine) NewPayer() *producer.Payer
- func (e *Engine) OrgKey() []byte
- func (e *Engine) Owed(ctx context.Context) error
- func (e *Engine) Put(ctx context.Context, w *Writer, items []Item, hold bool) ([]PutResult, error)
- func (e *Engine) Readable(ctx context.Context, w *Writer, owes []Stale, plan map[string]Choice) error
- func (e *Engine) Ready(ctx context.Context, v *state.Vault) (*Writer, error)
- func (e *Engine) RecoverTrees(ctx context.Context) error
- func (e *Engine) Resume(ctx context.Context) error
- func (e *Engine) Retire(ctx context.Context, w *Writer, names []string) error
- func (e *Engine) RetireTrees() error
- func (e *Engine) Retract(ctx context.Context, crypt, txid string, vouts []uint32) (*state.Sweep, error)
- func (e *Engine) Rotate(ctx context.Context, w *Writer, plan map[string]Choice, allow bool) (*RotateResult, error)
- func (e *Engine) Start(ctx context.Context) error
- func (e *Engine) SweepEnded(ctx context.Context, v *state.Vault, ended bool, retired []string) (int, error)
- func (e *Engine) SweptBy(txid string, vout uint32) *state.Sweep
- func (e *Engine) Sync(ctx context.Context, crypt string) (*SyncReport, error)
- func (e *Engine) Tx(txid string) (*transaction.Transaction, error)
- type Item
- type Legs
- type LostError
- type Minted
- type Options
- type PutResult
- type RotateResult
- type Stale
- type SweepRefusedError
- type SyncReport
- type Writer
Constants ¶
This section is empty.
Variables ¶
var ErrStale = errors.New("no positive evidence that the vault's and the group's latest tokens are the latest")
ErrStale is evidence a writer could not gather: without it nothing is written (spec section 7.1: fail closed).
var ErrUnwrapped = errors.New("borg's wraps of the new epoch's key are not published for every member")
ErrUnwrapped is a new epoch whose key borg has not yet released to every member (spec section 8.3).
var MaxChanges = secrec.MaxDelta
MaxChanges is the most secrets one token of this publisher creates, updates or retires: the record bound. A test lowers it so that a small vault takes several tokens.
var Profile = bwallet.Profile{ FundProtocol: secrec.Protocol, FundKeyID: secrec.KeyFund, FundBasket: "bsecret fund", Version: "bsecret-1", LegacyPoolFile: "bsecret-pool.json", }
Profile is the embedded wallet's profile: the fund key under [1, "bsecret"] key id fund, and the registered basket.
var Retries = limits.Retries
Retries are the waits between the tries of a submission to the facade.
Functions ¶
func CollectChange ¶
CollectChange gives every unproven coin in the pool its proof, once its transaction has mined.
func Covered ¶
func Covered(owes []Stale, plan map[string]Choice, known func(key string) bool) (ignored []string, err error)
Covered holds a plan to the rule that a revoke is never begun that cannot be finished: every stale secret has exactly one choice. A choice for a secret the rotation owes nothing (already under the new epoch, or retired) is returned as ignored, so that a rotation that stopped part way is finished by the same command; a choice for a name the vault never held is an error, since it is more likely a misspelling of one it does.
func Fold ¶
Fold folds a vault's own chain from seq 0. A chain that does not fold consistently is an error: the home never builds on one.
func Journal ¶
Journal makes every save of st record the pool, with the coins carried over from a reconciliation that could not settle them.
func Mark ¶
Mark records in the home what a mined token of the vault did: the versions it listed are listed and their commitments recorded, and the versions it replaced or retired are ended.
func Minimal ¶
func Minimal(mp *transaction.MerklePath, txid *chainhash.Hash) (*transaction.MerklePath, error)
Minimal is mp cut to the proof of txid alone (secrec.MinimalPath).
func Params ¶
Params are the carrier parameters of a tree kind: its derivation and the funding tag. A payload is checked by the host's rules before anything is persisted, so it is not checked again here.
func Reconcile ¶
func Reconcile(ctx context.Context, st *state.State, pool *bwallet.Pool, asset nodeapi.SpendSource, note func(string, ...any)) []bwallet.Output
Reconcile settles what a run that stopped left in the journal. A coin the journal holds that the pool no longer does was taken; when no transaction the home records spends it, the run stopped before it recorded one. The node is asked: a coin it shows unspent goes back in the pool; one it shows spent is spent by a transaction this home did not record, and is reported; one it cannot answer for is carried over to the next command.
func RecordOf ¶
func RecordOf(x *state.SecretVersion) (*secrec.Secret, error)
RecordOf is the secret record a version's kept carrier carries.
func Retractable ¶
func Retractable(v *state.Vault, epoch uint64, interval, ended bool, retired map[string]bool) map[string][]uint32
Retractable are the versions of a vault the owner retracts (spec sections 8.1, 8.2 and 8.4), by funding tree:
- always, in a per-version vault, every version published and never listed (what a transition that lost its predecessor left behind); in an interval vault an unlisted version only below the listed one, since a version an unlisted successor still links through is kept (spec section 4.4);
- every version of the secrets in retired that the set no longer holds;
- with ended, once the vault is settled, every version a later token replaced or retired that is keyed under an epoch before the vault's (which a removed member could open), and every version of any retired secret.
func TokenBEEF ¶
func TokenBEEF(ctx context.Context, crypt string, token, parent *transaction.Transaction, headers chaintracker.ChainTracker) ([]byte, error)
TokenBEEF assembles the BEEF a host admits a token in: the token and its parent, each with its minimal proof, held to the host's rules for crypt against headers.
Types ¶
type Change ¶
type Change struct {
// Create and Update are the versions entering the set, each with the
// record a carrier already published carries; Retire the identifiers
// leaving it.
Create []*Minted
Update []*Minted
Retire [][32]byte
// Epoch, when set, is the later epoch of the group the transition
// acknowledges; nil keeps the vault's epoch.
Epoch *secrec.EpochEvent
What string
}
Change is a vault transition asked for.
type Choice ¶
type Choice struct {
// Value is the new value, when Set.
Set bool
Value []byte
// Same re-keys the value the secret holds.
Same bool
// Retire retires the secret.
Retire bool
}
Choice is what a revoke does with one stale secret (docs/spec.md section 8.2): a new value, an explicit re-keying of the same value, or a retirement. There is no default: re-keying a credential without changing it leaves the removed reader holding a value that still works.
type Engine ¶
type Engine struct {
St *state.State
Signer *bwallet.Signer
// Key is the organization's identity private key. It derives the
// signer key, the member of the vault's group through which the
// organization opens an epoch's key (vread.SignerWallet).
Key *ec.PrivateKey
// Release, when Key is nil, is the wallet that holds the
// organization's identity key: an epoch's key is opened through it, as
// the releaser of borg's wrap to the signer key (vread.ReleaserWallet).
Release wallet.Interface
Pool *bwallet.Pool
Legs Legs
Opts Options
// Now is the clock records are stamped with; Sleep waits.
Now func() time.Time
Sleep func(ctx context.Context, d time.Duration) error
// Sample draws a content key and its salt; nil draws them at random. A
// test sets it to show that a repeated key is refused.
Sample func() ([32]byte, [secrec.SaltLen]byte, error)
// Note receives progress lines.
Note func(format string, args ...any)
Verbose bool
// contains filtered or unexported fields
}
Engine is one publisher run over one home.
func New ¶
func New(st *state.State, signer *bwallet.Signer, pool *bwallet.Pool, legs Legs, o Options) (*Engine, error)
New builds an engine over a home's state.
func (*Engine) Anchor ¶
Anchor lists, in an interval vault, the latest unlisted version of every secret that has one (spec section 4.4): one token, at an interval of the owner's choosing.
func (*Engine) Close ¶
Close collects a tree still being minted ahead, so its change reaches the pool, and records every tree the library holds and never adopted in its kind's Ahead list, so a sweep finds it and a later command, which holds nothing in memory, switches to them in the same order.
func (*Engine) CreateVault ¶
func (e *Engine) CreateVault(ctx context.Context, crypt, name, hall string, group [secrec.GroupIDLen]byte, labels []string, anchor uint64) (*state.Vault, error)
CreateVault creates a vault keyed to a group of the organization (spec section 8.1): a chain tree, mined; vault token seq 0 spending one of its outputs, with the vault identifier that outpoint gives, the group and its latest epoch, the label set and the anchor mode; once it is mined, the token is published with the chain tree in its BEEF. The vault is created empty: its first secrets are put afterwards.
func (*Engine) EpochKey ¶
EpochKey opens the symmetric key of one epoch of the group as the organization: through the wrap borg made to the organization's signer key, by borg's member checks.
func (*Engine) FinishSweep ¶
FinishSweep takes the sweep in flight, if any, through settlement, its proof and its publication. A sweep the network definitively refuses is marked failed, its fee coin given back when the node shows it unspent, and a *SweepRefusedError returned. Any other failure leaves it in flight.
func (*Engine) FinishTransitions ¶
FinishTransitions takes every persisted transition to the chain. Every vault is tried: a transition that is lost, or cannot finish yet, does not keep another vault's from the chain. The first failure is returned, a *LostError only when nothing else failed.
func (*Engine) Owed ¶
Owed publishes what the home owes its crypts: a token of its own vaults mined and not yet published.
func (*Engine) Put ¶
Put writes a new version of each item and lists them in the vault's next token (spec section 8.1): in create for a secret the set does not hold, in update for one it does. With hold, in an interval vault, the versions are published and not listed: readers take them by their links, marked unanchored, until Anchor lists the latest of each (spec section 4.4).
It refuses while the vault is behind its group or in debt: then the only transitions are the acknowledgement and the re-keying (Rotate).
func (*Engine) Readable ¶
func (e *Engine) Readable(ctx context.Context, w *Writer, owes []Stale, plan map[string]Choice) error
Readable holds a plan to what its same-value re-keyings need: the value of each secret re-keyed as it is, read from the version the new one follows, through this home's own wrap and, for a secret kept by its hash, a content source. It returns the first that cannot be read, so that a rotation that cannot be finished is not begun: a carrier published for one secret while another's value cannot be read is a funding output spent on a version no token will list.
func (*Engine) Ready ¶
Ready gathers a writer's evidence for one vault. Without it the tooling does not write.
func (*Engine) RecoverTrees ¶
RecoverTrees settles every tree a run signed for a coin and did not adopt, before anything is spent:
- a tree the chain holds is adopted, or held while the current tree of its kind has outputs left, and its change is taken. Any number are held, in the order of their records, and the switch takes them in that order. A held tree is recorded among the trees minted ahead, and its record stays until the switch adopts it;
- a tree the node does not know whose coin another transaction spent is gone, and its record is dropped;
- a tree the node does not know whose coin is unspent never reached the chain as far as the node can say now. The coin goes back to the pool, and the record is kept for one more command: a tree handed to the leg an instant before the run stopped can reach the node later. The second such answer drops the record;
- a node that cannot answer decides nothing: the record stays, the next command asks again, and this one goes on.
A coin the pool holds although the chain shows it spent (it was put back before the tree landed) is taken out of the pool by the library, which says so. Only a failure to save the state is returned.
func (*Engine) Resume ¶
Resume publishes every object a previous run persisted: the same bytes, so a host that already holds one answers a duplicate. Every object is tried; one that cannot be published stays in the outbox, and the first failure is returned.
func (*Engine) Retire ¶
Retire retires secrets by name (spec section 8.1): one token that lists their identifiers in retire. While the vault is in debt only stale secrets may be retired, which the transition rules hold it to.
func (*Engine) RetireTrees ¶
RetireTrees stops the home spending every secret tree it holds: after the kill switch no output of them funds anything again.
func (*Engine) Retract ¶
func (e *Engine) Retract(ctx context.Context, crypt, txid string, vouts []uint32) (*state.Sweep, error)
Retract builds and persists one sweep of vouts of the funding tree txid, with its fee from the pool, then settles and publishes it in crypt.
func (*Engine) Rotate ¶
func (e *Engine) Rotate(ctx context.Context, w *Writer, plan map[string]Choice, allow bool) (*RotateResult, error)
Rotate acknowledges the group's latest epoch and re-keys or retires everything the vault held under an earlier one (spec section 8.2), in as many tokens as it takes at 64 changes a token, one a block: the acknowledgement carries the first re-keyings. plan gives, by name, what becomes of each stale secret; it is held to Covered before anything is built, so a rotation is never begun that cannot be finished. ready gathers the writer's evidence again before each token.
A version keyed under a new epoch is published only after the group token that began the epoch is mined in the publisher's own headers and borg's wraps of the epoch's key are published (spec section 8.3): unwrapped names the members still without a wrap, and the rotation refuses while there is one unless allow is set.
func (*Engine) Start ¶
Start reads the chain tip for coinbase maturity, collects the proofs of change the pool holds back, and finishes whatever a previous run persisted and did not finish: a sweep, every object not confirmed published, every vault's transition, and every token mined and not yet published. Each piece of work is tried whatever became of the one before it: one that cannot finish (a host that is down, a transition another beat to the chain) does not keep the rest from the chain or the hosts. The first failure is returned, a *LostError only when nothing else failed; the others are noted.
func (*Engine) SweepEnded ¶
func (e *Engine) SweepEnded(ctx context.Context, v *state.Vault, ended bool, retired []string) (int, error)
SweepEnded retracts what Retractable names, one sweep a funding tree, and returns how many versions it retracted. It does nothing while the vault is in debt or a transition is in flight.
func (*Engine) SweptBy ¶
SweptBy is the sweep that spends output vout of the funding tree txid, or nil. A failed sweep spends nothing.
func (*Engine) Sync ¶
Sync catches this home up with the hosts (spec section 8.5): every vault of the organization in crypt, its chain from seq 0 read as any reader reads it, against the home's own headers, and folded; every current secret's record, and each one's name and claims through its self wrap, which needs only the organization's identity key; and every commitment any token listed, so that none is ever published again. The funding tree of every carrier the hosts answer is recorded, spent, so that a sweep or the kill switch reaches versions another instance published; this home never funds a carrier from one (two instances must not share a tree).
What it cannot bring back: a version published and not yet listed whose carrier no host kept, and another instance's unpublished objects.
func (*Engine) Tx ¶
func (e *Engine) Tx(txid string) (*transaction.Transaction, error)
Tx is a funding tree the home keeps, rebuilt.
type Item ¶
type Item struct {
Name string
Value []byte
// Label is the classification; empty keeps the secret's last label, or
// for a new secret takes the vault's default.
Label string
// Type, NotAfter and RotateEvery are the payload's optional claims.
Type string
NotAfter uint64
RotateEvery uint64
}
Item is one secret value to write.
type Legs ¶
type Legs struct {
Settler publish.Settler
// Chain is the chain view: transactions, proofs and spends
// (nodeapi.ParseChain over a node or WhatsOnChain).
Chain nodeapi.Chain
Arcade *publish.Arcade
Facade *publish.Facade
Hosts *unicast.Set
Direct *unicast.Set
Reader *vread.Client
Groups *reader.Client
Headers chaintracker.ChainTracker
Content *vread.Store
}
Legs are the endpoints a publisher talks to. Objects go to exactly one of Facade (mode plane) and Hosts (mode unicast). In mode plane, Direct are the other hosts every mined object is also submitted to. Reader confirms, by lookup, a host that admitted nothing. Groups reads the vault's group from its hall's hosts, through borg's reader. Headers are the publisher's own block headers: a mined object is published only once its block is in them. Content is where a ciphertext too large to carry is written.
type LostError ¶
type LostError struct {
What string
Txid string
By string
// Fee is set when it was the fee coin, not the predecessor, that
// another transaction spent: no transition beat this one.
Fee bool
}
LostError is a transition another transaction spending the same predecessor beat to the chain (spec section 8.4): nothing it listed is listed, and the versions it published stay unlisted until they are swept.
type Minted ¶
type Minted struct {
Name string
Record *secrec.Secret
Entry secrec.Entry
// Txid is the carrier's txid; Stored where a ciphertext that is not
// carried was written.
Txid string
Stored string
}
Minted is one version written: its record, as its carrier published it, and the entry a vault token lists it by.
type Options ¶
type Options struct {
// TreeCount sizes secret trees, ChainTreeCount chain trees; TreeSats is
// each output's value; Ahead is how few outputs a secret tree has left
// when the next is minted ahead.
TreeCount int
ChainTreeCount int
TreeSats uint64
Ahead uint32
// Fees is the fee policy of trees, tokens, sweeps and payments.
Fees mint.Fees
// ObjectBound is the plane's object bound, in bytes.
ObjectBound int
// Poll paces proof waits and Wait bounds one.
Poll time.Duration
Wait time.Duration
// Delay is the propagation delay before a mined object is published.
Delay time.Duration
// Freshness is how old a writer's evidence may be when it publishes a
// version (spec section 7.1); every transition retakes it as it is
// built.
Freshness time.Duration
// Segment is the plaintext bytes of one segment of a new version.
Segment uint64
// PlaneNeed is, in mode plane, how many of the hosts named must answer
// a carrier by lookup for it to count as published; zero is all of
// them. A mined object always needs every host.
PlaneNeed int
}
Options are a publisher's choices. None of them is frozen.
type PutResult ¶
type PutResult struct {
Minted *Minted
// Created is set when the secret entered the set; Listed when a token
// lists the version (false for a deferred version of an interval
// vault).
Created bool
Listed bool
Seq uint64
}
PutResult is what a put did.
type RotateResult ¶
type RotateResult struct {
// Acknowledged is the epoch the vault acknowledged in this run, when it
// did.
Acknowledged *uint64
Tokens int
Rekeyed []string
Retired []string
Swept int
}
RotateResult is what a rotation did.
type Stale ¶
Stale is one secret of the vault's set that a rotation owes a re-keying or a retirement: keyed under an epoch before the one the vault is to write under.
type SweepRefusedError ¶
type SweepRefusedError struct {
Txid string
// Why is the refusal, as the evidence said it.
Why string
// Coin is what became of the fee coin.
Coin string
}
SweepRefusedError is a sweep the network refused. The sweep is marked failed in the home, its fee coin is back in the pool when the node shows it unspent, and the outputs it named may be swept again.
func (*SweepRefusedError) Error ¶
func (e *SweepRefusedError) Error() string
type SyncReport ¶
type SyncReport struct {
Vaults, NewVaults int
Tokens int
Versions, Named int
// Unlisted are the versions of an interval vault another instance
// published and no token lists yet.
Unlisted int
Trees int
Unreadable, Broken []string
// Ahead are the vaults whose chain this home holds further than any
// host answered: the home's own tokens are kept and published again.
Ahead []string
}
SyncReport is what a catch-up found.
type Writer ¶
type Writer struct {
Vault *state.Vault
// Recs are the vault's chain; Pred its latest record and Last that
// token.
Recs []*secrec.Vault
Pred *secrec.Vault
Last *state.Token
// Fold is the fold of the chain; a transition is tried on a clone.
Fold *secrec.VaultFold
// Group is the vault's group and Stand the vault's standing against it.
Group *vread.Group
Stand vread.Standing
// At is when the evidence was gathered.
At time.Time
}
Writer is a writer's view of one vault at the moment it holds the evidence the contract asks for before any transition (spec section 7.1): the vault's own chain folded, its latest token mined in the writer's headers and unspent per the node, and the group's chain read through borg with its latest token unspent too.
func (*Writer) Build ¶
Build makes the next vault record for a change and proves, before anything is signed, that no host and no index would flag it: the record is applied to a copy of the fold of the vault's own chain, which must answer consistent, held to the transition rules a host checks against its predecessor, and every version it lists is held to the record the carrier published. It returns the record, encoded.