Liking cljdoc? Tell your friends :D

skuld.ledger.authz

Who may write what: the transaction boundary.

The rule

A transaction is admitted when three things hold, in this order:

AUTHORSHIP an Ed25519 signature over the tx-data, by the key that claims to have sent it. SCOPE every datom it PRODUCES is in the sender's own attribute namespace, or is a system attribute the sender is allowed to write. COST it fits the budget, priced by skuld.ledger.cost.

A public key owns the attribute namespace that is its own hex:

:5f3a...c1/first-name          64 hex characters, then the attribute

which makes ownership self-evident from the attribute alone -- no registry, no lookup, no allocation step, and no way for two users to be assigned the same namespace.

Why the whole key and not a prefix

A shorter namespace is far nicer to read, and it is exactly wrong here. The namespace is not a display name, it is a CAPABILITY: whoever can produce a key whose namespace matches yours can write your data. Truncating to 10 hex characters leaves 40 bits, and finding a second key with a chosen 40-bit prefix is around 2^40 keygens -- hours of ordinary compute, targeted at a specific victim. At the full 32 bytes the same attack is 2^256 and the question does not arise.

Verified against datahike: a 64-character keyword namespace reads, prints, round-trips through the reader, installs, transacts and queries.

Why the check reads produced datoms, not submitted forms

This is the part that is easy to get wrong, because the obvious implementation -- walk the tx-data the sender submitted and look at the attributes it names -- is bypassable by any operation that does not name what it touches. Two are reachable in datahike today, and the second is the one that settles the design:

[:db/retractEntity 42] names NO attribute, and produces one retraction per attribute the entity has, including other users'. Measured: retracting an entity carrying two users' attributes produced a retraction datom in each.

:db/isComponent cascade WORSE. Retracting an entity cascades into its component children, so a caller can destroy datoms on an entity it never named, belonging to a user it never named. Measured: Alice retracting her OWN entity produced [:<bob>/secret "bobs-data" false].

Both are closed by the same move: run d/with and check what came OUT. That is also the call skuld.ledger.cost/price already makes, so the authorization decision and the price come from ONE speculative transaction rather than two.

The mutation matters here. Swapping this for the naive submitted-form check leaves every other test in authz-test passing -- including "a transaction cannot write another user's attribute" -- and fails only these two. That is why the naive version is tempting: it looks correct against the case anyone would think to write.

Stored transaction functions: not a hole today

[:my/fn args] resolving to a :db/fn on an entity would expand to arbitrary tx-data and is the surface I expected to matter. It is currently unreachable: datahike.db.transaction resolves it (transaction.cljc:1415) but :db/fn is defined in no datahike schema, cannot be installed under :schema-flexibility :write, and cannot be defined at all because the db namespace is reserved. An inline [:db.fn/call (fn ...)] is separately impossible over the wire, since the signature covers canonical CBOR of the tx-data and a function object has no CBOR encoding.

Recorded rather than dropped, because reading the call site alone suggests the opposite. If datahike completes the feature this becomes live immediately -- and this namespace already covers it, since an expansion is exactly a produced datom.

What entity ownership is not

Scope is per-ATTRIBUTE, never per-entity. Writing :<my-key>/note onto an entity someone else created is allowed, and an entity can carry attributes from many keys at once. That is deliberate -- it is what lets one user annotate another's data without permission -- but it means "who owns entity 42" has no answer, and code that assumes otherwise is assuming something this layer never promised.

System attributes

Anything whose namespace is not a 64-character hex string is a system attribute: :db/ident, :kontor.account/path, the ledger's own schema. These are READABLE by everyone -- reads are not gated here at all. The separation needs no registry: a hex namespace and an alphabetic one cannot collide, so a user key can never name a system attribute however it is constructed.

:system-writers below is part of THIS namespace's scope-violations, which the live write path does not use. skuld.boundary/admit routes through ownership/violations instead, and that refuses a protocol attribute before ownership or any writer set is consulted -- so under the live boundary no key writes :urd.account/* or :urd.stake/*, configured writer or not. Genesis installs schema by transacting directly, not through a boundary.

Who may write what: the transaction boundary.

## The rule

A transaction is admitted when three things hold, in this order:

  AUTHORSHIP   an Ed25519 signature over the tx-data, by the key that claims
               to have sent it.
  SCOPE        every datom it PRODUCES is in the sender's own attribute
               namespace, or is a system attribute the sender is allowed to
               write.
  COST         it fits the budget, priced by `skuld.ledger.cost`.

A public key owns the attribute namespace that is its own hex:

    :5f3a...c1/first-name          64 hex characters, then the attribute

which makes ownership self-evident from the attribute alone -- no registry,
no lookup, no allocation step, and no way for two users to be assigned the
same namespace.

## Why the whole key and not a prefix

A shorter namespace is far nicer to read, and it is exactly wrong here. The
namespace is not a display name, it is a CAPABILITY: whoever can produce a
key whose namespace matches yours can write your data. Truncating to 10 hex
characters leaves 40 bits, and finding a second key with a chosen 40-bit
prefix is around 2^40 keygens -- hours of ordinary compute, targeted at a
specific victim. At the full 32 bytes the same attack is 2^256 and the
question does not arise.

Verified against datahike: a 64-character keyword namespace reads, prints,
round-trips through the reader, installs, transacts and queries.

## Why the check reads produced datoms, not submitted forms

This is the part that is easy to get wrong, because the obvious
implementation -- walk the tx-data the sender submitted and look at the
attributes it names -- is bypassable by any operation that does not name
what it touches. Two are reachable in datahike today, and the second is the
one that settles the design:

  [:db/retractEntity 42]      names NO attribute, and produces one retraction
                              per attribute the entity has, including other
                              users'. Measured: retracting an entity carrying
                              two users' attributes produced a retraction
                              datom in each.

  :db/isComponent cascade     WORSE. Retracting an entity cascades into its
                              component children, so a caller can destroy
                              datoms on an entity it never named, belonging
                              to a user it never named. Measured: Alice
                              retracting her OWN entity produced
                              `[:<bob>/secret "bobs-data" false]`.

Both are closed by the same move: run `d/with` and check what came OUT. That
is also the call `skuld.ledger.cost/price` already makes, so the authorization
decision and the price come from ONE speculative transaction rather than two.

The mutation matters here. Swapping this for the naive submitted-form check
leaves every other test in `authz-test` passing -- including "a transaction
cannot write another user's attribute" -- and fails only these two. That is
why the naive version is tempting: it looks correct against the case anyone
would think to write.

## Stored transaction functions: not a hole today

`[:my/fn args]` resolving to a `:db/fn` on an entity would expand to
arbitrary tx-data and is the surface I expected to matter. It is currently
unreachable: `datahike.db.transaction` resolves it (transaction.cljc:1415)
but `:db/fn` is defined in no datahike schema, cannot be installed under
`:schema-flexibility :write`, and cannot be defined at all because the `db`
namespace is reserved. An inline `[:db.fn/call (fn ...)]` is separately
impossible over the wire, since the signature covers canonical CBOR of the
tx-data and a function object has no CBOR encoding.

Recorded rather than dropped, because reading the call site alone suggests
the opposite. If datahike completes the feature this becomes live
immediately -- and this namespace already covers it, since an expansion is
exactly a produced datom.

## What entity ownership is not

Scope is per-ATTRIBUTE, never per-entity. Writing `:<my-key>/note` onto an
entity someone else created is allowed, and an entity can carry attributes
from many keys at once. That is deliberate -- it is what lets one user
annotate another's data without permission -- but it means "who owns entity
42" has no answer, and code that assumes otherwise is assuming something
this layer never promised.

## System attributes

Anything whose namespace is not a 64-character hex string is a system
attribute: `:db/ident`, `:kontor.account/path`, the ledger's own schema.
These are READABLE by everyone -- reads are not gated here at all. The
separation needs no registry: a hex namespace and an alphabetic one cannot
collide, so a user key can never name a system attribute however it is
constructed.

`:system-writers` below is part of THIS namespace's `scope-violations`, which
the live write path does not use. `skuld.boundary/admit` routes through
`ownership/violations` instead, and that refuses a protocol attribute before
ownership or any writer set is consulted -- so under the live boundary no
key writes `:urd.account/*` or `:urd.stake/*`, configured writer or not.
Genesis installs schema by transacting directly, not through a boundary.
raw docstring

admitclj

(admit db
       {:keys [chain sender genesis public-key signature nonce tx-data
               system-writers schedule]
        :or {system-writers #{} schedule cost/default-schedule}})

Decide whether to accept a signed transaction, and at what price.

Returns a CHANNEL of

{:ok? bool
 :reason kw            ; when refused
 :cost n               ; charged whether or not it is applied
 :datoms k
 :violations [...]     ; when refused for scope
 :report r}            ; the speculative tx-report, when admitted

The order is load-bearing

Signature FIRST, then d/with, then scope. Verification is one Ed25519 operation against a key we already hold; d/with walks the database and allocates. Doing the expensive half for an unsigned transaction is a way to make a node do unbounded work on a message anyone can send.

A refused transaction is still charged

The base fee applies to refusals, for the reason skuld.ledger.cost gives: a transaction that can be retried for free after being refused is a free retry loop, which is the thing a budget exists to prevent. A BAD SIGNATURE is the exception -- it is charged nothing, because there is nobody to charge. We have no authenticated sender, so any fee would be taken from an account named by an unverified claim.

Decide whether to accept a signed transaction, and at what price.

Returns a CHANNEL of

    {:ok? bool
     :reason kw            ; when refused
     :cost n               ; charged whether or not it is applied
     :datoms k
     :violations [...]     ; when refused for scope
     :report r}            ; the speculative tx-report, when admitted

## The order is load-bearing

Signature FIRST, then `d/with`, then scope. Verification is one Ed25519
operation against a key we already hold; `d/with` walks the database and
allocates. Doing the expensive half for an unsigned transaction is a way to
make a node do unbounded work on a message anyone can send.

## A refused transaction is still charged

The base fee applies to refusals, for the reason `skuld.ledger.cost` gives:
a transaction that can be retried for free after being refused is a free
retry loop, which is the thing a budget exists to prevent. A BAD SIGNATURE
is the exception -- it is charged nothing, because there is nobody to
charge. We have no authenticated sender, so any fee would be taken from an
account named by an unverified claim.
raw docstring

authorised?clj

(authorised? genesis public-key sender)

May public-key sign for sender, given genesis?

Both halves, and neither alone is enough: the genesis must hash to the claimed namespace, AND the key must be one of its operational keys. Without the first, anyone with a valid genesis signs under any name; without the second, a rotation key -- which is meant to live offline and sign nothing but rotations -- could sign transactions.

May `public-key` sign for `sender`, given `genesis`?

Both halves, and neither alone is enough: the genesis must hash to the
claimed namespace, AND the key must be one of its operational keys. Without
the first, anyone with a valid genesis signs under any name; without the
second, a rotation key -- which is meant to live offline and sign nothing
but rotations -- could sign transactions.
raw docstring

intent-attribute?clj

(intent-attribute? attr)

May any authenticated sender write attr as a request to the protocol?

May any authenticated sender write `attr` as a request to the protocol?
raw docstring

intent-namespaceclj

The one namespace an ordinary sender may write that the PROTOCOL reads.

Everything else divides in two: a hex namespace belongs to the key that owns it, and every other namespace is protocol state no sender may touch. That is a sound rule and it left no way to ASK the protocol for anything. Staking is the case that made it concrete -- a delegation is not a row in your own namespace, and urd.stake is refused to senders by construction.

So a third class: a REQUEST. Writing :urd.stake.intent/op moves nothing and proves nothing. It records that a sender asked for something, and the sender pays for the bytes like any other write. The protocol reads those datoms at the end of the block, re-validates every one against state the sender does not control -- balances, share counts, whether the validator exists -- and only then moves value. A malformed or unaffordable intent is refused there, having paid its fee.

The property that makes this safe is that an intent is INERT: a message, not an effect. The moment an attribute in this namespace means something to anything other than the end-of-block interpreter -- an invariant that reads it, a balance keyed by it -- that stops being true, and this paragraph stops being the whole argument.

Note it does not collide with ownership/protocol-attribute?, which tests the EXACT namespace string urd.stake.

The one namespace an ordinary sender may write that the PROTOCOL reads.

Everything else divides in two: a hex namespace belongs to the key that owns
it, and every other namespace is protocol state no sender may touch. That is
a sound rule and it left no way to ASK the protocol for anything. Staking is
the case that made it concrete -- a delegation is not a row in your own
namespace, and `urd.stake` is refused to senders by construction.

So a third class: a REQUEST. Writing `:urd.stake.intent/op` moves nothing and
proves nothing. It records that a sender asked for something, and the sender
pays for the bytes like any other write. The protocol reads those datoms at
the end of the block, re-validates every one against state the sender does
not control -- balances, share counts, whether the validator exists -- and
only then moves value. A malformed or unaffordable intent is refused there,
having paid its fee.

The property that makes this safe is that an intent is INERT: a message, not
an effect. The moment an attribute in this namespace means something to
anything other than the end-of-block interpreter -- an invariant that reads
it, a balance keyed by it -- that stops being true, and this paragraph stops
being the whole argument.

Note it does not collide with `ownership/protocol-attribute?`, which tests
the EXACT namespace string `urd.stake`.
raw docstring

key-namespace-lengthclj

Characters in the namespace a public key owns: 32 bytes as hex.

Characters in the namespace a public key owns: 32 bytes as hex.
raw docstring

key-namespace?clj

(key-namespace? s)

Is s shaped like a key-owned namespace?

Length AND alphabet, because a 64-character namespace that is not hex is not a key's -- and treating it as one would let an attacker claim a namespace no key can produce, which nobody could ever write to but which would read as user-owned.

Is `s` shaped like a key-owned namespace?

Length AND alphabet, because a 64-character namespace that is not hex is not
a key's -- and treating it as one would let an attacker claim a namespace no
key can produce, which nobody could ever write to but which would read as
user-owned.
raw docstring

namespace-forclj

(namespace-for genesis)

The attribute namespace owned by a genesis record.

The hex of netz.identity/peer-id-bytes -- a hash of the genesis, not of a key. 64 characters, so everything downstream that reasons about a "64-hex-char namespace" is unchanged.

Why not the public key

It was (bytes->hex public-key), and that is the mistake SSB, did:key and UCAN all made and none of them can undo: if the identity IS the key, the key can never change, so a compromise is terminal. kabel's design record names it outright -- peer-id = SHA-256(tag || pubkey) is the mistake -- and says why a genesis record is the answer.

It was worse here than in SSB. There, losing a key strands a social graph. Here the key is baked into the ATTRIBUTE NAME of every datom the user ever wrote, so rotation is not a re-signature, it is a rewrite of the database plus a break of every external reference to :<oldkey>/foo.

A genesis names operational keys (hot, sign everything, expected to be stolen), rotation keys with a threshold (cold, offline), and a revocation commitment published in advance -- so an attacker holding the hot key cannot revoke, because revocation needs a secret whose commitment predates them. Replace the operational keys and the namespace does not move, because the namespace was never the key.

The attribute namespace owned by a `genesis` record.

The hex of `netz.identity/peer-id-bytes` -- a hash of the genesis, not of a
key. 64 characters, so everything downstream that reasons about a
"64-hex-char namespace" is unchanged.

## Why not the public key

It was `(bytes->hex public-key)`, and that is the mistake SSB, did:key and
UCAN all made and none of them can undo: if the identity IS the key, the key
can never change, so a compromise is terminal. kabel's design record names it
outright -- `peer-id = SHA-256(tag || pubkey)` is the mistake -- and says why
a genesis record is the answer.

It was worse here than in SSB. There, losing a key strands a social graph.
Here the key is baked into the ATTRIBUTE NAME of every datom the user ever
wrote, so rotation is not a re-signature, it is a rewrite of the database
plus a break of every external reference to `:<oldkey>/foo`.

A genesis names operational keys (hot, sign everything, expected to be
stolen), rotation keys with a threshold (cold, offline), and a revocation
commitment published in advance -- so an attacker holding the hot key cannot
revoke, because revocation needs a secret whose commitment predates them.
Replace the operational keys and the namespace does not move, because the
namespace was never the key.
raw docstring

scope-violationsclj

(scope-violations {:keys [sender system-writers] :or {system-writers #{}}}
                  datoms)

The produced datoms sender is not allowed to write.

sender is the owning namespace (hex), system-writers a set of hex namespaces permitted to write system attributes. Returns a vector of {:attr a :value v :added? bool :reason kw} -- data rather than a boolean, so a refusal can say which attribute it tripped on. A caller that only needs yes/no can ask whether it is empty.

Ordered by the datoms themselves so the answer is deterministic, which matters because every validator must refuse for the same stated reason.

The produced datoms `sender` is not allowed to write.

`sender` is the owning namespace (hex), `system-writers` a set of hex
namespaces permitted to write system attributes. Returns a vector of
`{:attr a :value v :added? bool :reason kw}` -- data rather than a boolean,
so a refusal can say which attribute it tripped on. A caller that only needs
yes/no can ask whether it is empty.

Ordered by the datoms themselves so the answer is deterministic, which
matters because every validator must refuse for the same stated reason.
raw docstring

system-attribute?clj

(system-attribute? attr)

Is attr part of the system/ledger schema rather than a user's data?

Everything that is not key-owned. Reads are never gated, so this only ever decides who may WRITE.

Is `attr` part of the system/ledger schema rather than a user's data?

Everything that is not key-owned. Reads are never gated, so this only ever
decides who may WRITE.
raw docstring

tx-digestclj

(tx-digest chain sender nonce tx-data)

The bytes an author's signature covers.

:chain is domain separation and is not optional, for the reason urd.consensus.auth gives at length: without it a transaction signed on a testnet is a valid transaction on mainnet. :nonce is what stops the same signed transaction being replayed forever; it is the caller's job to reject a nonce it has seen, which this namespace cannot do because it holds no state.

Canonical CBOR (urd.wire), which is what makes a non-Clojure client possible: signing is cbor2.dumps(payload, canonical=True) followed by an ordinary Ed25519 sign, rather than a reimplementation of hasch/edn-hash. The tx-data keywords ride as CBOR tag 39, the same way kabel's wire already carries them.

The bytes an author's signature covers.

`:chain` is domain separation and is not optional, for the reason
`urd.consensus.auth` gives at length: without it a transaction signed on a
testnet is a valid transaction on mainnet. `:nonce` is what stops the same
signed transaction being replayed forever; it is the caller's job to reject
a nonce it has seen, which this namespace cannot do because it holds no
state.

Canonical CBOR (`urd.wire`), which is what makes a non-Clojure client
possible: signing is `cbor2.dumps(payload, canonical=True)` followed by an
ordinary Ed25519 sign, rather than a reimplementation of `hasch/edn-hash`.
The tx-data keywords ride as CBOR tag 39, the same way kabel's wire already
carries them.
raw docstring

verified-author?clj

(verified-author? {:keys [chain sender public-key signature nonce tx-data]})

Does signature prove public-key authored this transaction?

Returns a CHANNEL of a boolean, because Ed25519 verification is channel-returning on both platforms -- ClojureScript's Web Crypto is promise-based, and geheimnis gives the JVM the same contract so one implementation serves both. This is the same reason urd.consensus.auth lives at the network edge and not inside a step.

true? and not truthiness: geheimnis puts an ExceptionInfo ON the channel for a malformed key or signature rather than throwing, and an exception object is truthy. Taking the obvious (when (<! ...)) here would admit every malformed signature -- the exact bypass urd.consensus.auth/try-verify documents having found.

Does `signature` prove `public-key` authored this transaction?

Returns a CHANNEL of a boolean, because Ed25519 verification is
channel-returning on both platforms -- ClojureScript's Web Crypto is
promise-based, and geheimnis gives the JVM the same contract so one
implementation serves both. This is the same reason `urd.consensus.auth`
lives at the network edge and not inside a `step`.

`true?` and not truthiness: geheimnis puts an ExceptionInfo ON the channel
for a malformed key or signature rather than throwing, and an exception
object is truthy. Taking the obvious `(when (<! ...))` here would admit
every malformed signature -- the exact bypass `urd.consensus.auth/try-verify`
documents having found.
raw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close