Skip to main content

Module delegate_host

Module delegate_host 

Source
Expand description

Host function API for delegates.

This module provides synchronous access to delegate context and secrets via host functions, eliminating the need for message round-trips.

§Example

use freenet_stdlib::prelude::*;

#[delegate]
impl DelegateInterface for MyDelegate {
    fn process(
        ctx: &mut DelegateCtx,
        _params: Parameters<'static>,
        _attested: Option<&'static [u8]>,
        message: InboundDelegateMsg,
    ) -> Result<Vec<OutboundDelegateMsg>, DelegateError> {
        // Read/write temporary context
        let data = ctx.read();
        ctx.write(b"new state");

        // Access persistent secrets
        if let Some(key) = ctx.get_secret(b"private_key") {
            // use key...
        }
        ctx.set_secret(b"new_secret", b"value");

        Ok(vec![])
    }
}

§Context vs Secrets

  • Context (read/write): Temporary state within a single message batch. Reset between separate runtime calls. Use for intermediate processing state.

  • Secrets (get_secret/set_secret): Persistent encrypted storage. Survives across all delegate invocations. Use for private keys, tokens, etc.

§Error Codes

Host functions return negative values to indicate errors:

CodeMeaning
0Success
-1Called outside process() context
-2Secret not found
-3Storage operation failed
-4Invalid parameter (e.g., negative length)
-5Context too large (exceeds i32::MAX)
-6Buffer too small

The wrapper methods in DelegateCtx handle these error codes and present a more ergonomic API.

Modules§

error_codes
Error codes returned by host functions.

Structs§

DelegateCtx
Opaque handle to the delegate’s execution environment.