src/agents/envelope.ts is the protocol edge of the agents package. It builds the signed envelope every request carries, resolves the three endpoint URLs of an agents server, and validates what the user types as a server address. It has no network code and no state.
The envelope
encodeAgentEnvelope(signer, action, now) returns DAG-CBOR bytes of this map:
Field | Value |
|---|---|
|
|
| The signing key as a principal: 34 bytes, the Ed25519 multicodec prefix |
| 64 byte Ed25519 signature |
| The principal bytes decoded from the account id string |
|
|
| The action object with |
The action's discriminator is its _ field, for example {_: 'ListAgents'}.
Zero-sig signing
The signature covers the whole envelope, including a placeholder for itself:
Build the envelope with sig set to 64 zero bytes.
Encode it with the canonical codec and sign those bytes.
Encode again with the real signature in place.
A verifier does the reverse: replace sig with zeros, re-encode, verify. The first unit test checks both that this verifies and that verifying over the envelope with sig removed does not. The codec is encode from src/crypto/cbor, crypto's vendored cborg with DAG-CBOR options. The header comment explains: "The server verifies by re-encoding with the shared client codec ... which is exactly crypto's vendored encode; no separately patched cborg is needed here". See Crypto: vendored codecs.
omitUndefined strips undefined object fields recursively, maps undefined array items to null, and leaves Uint8Array values intact so a binary field such as SetSecret.value stays bytes.
Signers
AgentSigner is {accountId, publicKey, sign(bytes)}. publicKey may be raw (32 bytes) or already a principal (34 bytes); the encoder normalises it. If the principal string does not equal signer.accountId, encoding throws "The local key holds no delegation for this account": delegated signing with a capability is not implemented, so the signer and the account are always the same key. In production the signer comes from signerFor in identity. agentSignerFromSeed builds one from an explicit seed and is "used only with throwaway keys in tests".
Endpoint layouts
agentEndpoint(base, leaf) supports two server layouts:
Leaf | Base path ends with | Any other base |
|---|---|---|
|
|
|
|
|
|
|
|
|
For ws the scheme becomes wss: for https: and ws: otherwise. The default server https://agentic.seed.hyper.media uses the second column.
Server URL normalization
normalizeAgentServerUrl(input) runs once, when the user saves the agent server field (through the agentsNormalizeServer source). It trims, requires http: or https:, refuses a username or password, drops query and fragment, turns a path of exactly /agents into the root, and strips trailing slashes. Errors are user-facing sentences: "Agent server URL is required.", "Use an http:// or https:// server URL.", "Use an http:// or https:// server URL without credentials.". The same function validates a model provider's base URL before it is saved.
Secret-target safety
safeAgentSecretTarget(base) is true for https: and for http: only when the host is localhost, 127.0.0.1 or [::1]. It guards provider API keys twice: the overview reply carries it as safeSecrets, which disables the Save button and shows "This server uses remote HTTP. Use HTTPS before sending an API key.", and agentsSaveProvider checks again before sending.
Tests
In agents.test.ts, the agents protocol group has five tests: the byte-for-byte vector, raw or principal public keys and the delegation refusal, undefined stripping with binary preserved, URL normalization with both layouts, and the secret target. The vector, envelope-vector.json, was produced by tools/mock-agents/generate-vector.ts, which extracts and executes signAgentActionWith from frontend/packages/ui/src/agents/client.ts and sign from frontend/packages/client/src/blobs.ts with a fixed clock, and records a SHA-256 of that source. review.test.ts asserts that no file in src/agents imports @ipld/dag-cbor or cborg directly.
Related
Agents: reads, commands and the clock: where the envelope is sent
Crypto: signed envelopes: the notify and vault envelopes, which differ from this one
Crypto: primitives and key derivation: principalFromEd25519, sign
Mock agents server: verifies this envelope
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime