Skip to main content

DelegateInterface

Trait DelegateInterface 

Source
pub trait DelegateInterface {
    // Required method
    fn process(
        ctx: &mut DelegateCtx,
        parameters: Parameters<'static>,
        origin: Option<MessageOrigin>,
        message: InboundDelegateMsg<'_>,
    ) -> Result<Vec<OutboundDelegateMsg>, DelegateError>;
}
Expand description

A Delegate is a webassembly code designed to act as an agent for the user on Freenet. Delegates can:

  • Store private data on behalf of the user
  • Create, read, and modify contracts
  • Create other delegates
  • Send and receive messages from other delegates and user interfaces
  • Ask the user questions and receive answers

Example use cases:

  • A delegate stores a private key for the user, other components can ask the delegate to sign messages, it will ask the user for permission
  • A delegate monitors an inbox contract and downloads new messages when they arrive

§Example

use freenet_stdlib::prelude::*;

struct MyDelegate;

#[delegate]
impl DelegateInterface for MyDelegate {
    fn process(
        ctx: &mut DelegateCtx,
        _params: Parameters<'static>,
        _origin: Option<MessageOrigin>,
        message: InboundDelegateMsg,
    ) -> Result<Vec<OutboundDelegateMsg>, DelegateError> {
        // Access secrets synchronously - no round-trip needed!
        if let Some(key) = ctx.get_secret(b"private_key") {
            // use key...
        }
        ctx.set_secret(b"new_key", b"value");

        // Read/write context for temporary state within a batch
        ctx.write(b"some state");

        Ok(vec![])
    }
}

Required Methods§

Source

fn process( ctx: &mut DelegateCtx, parameters: Parameters<'static>, origin: Option<MessageOrigin>, message: InboundDelegateMsg<'_>, ) -> Result<Vec<OutboundDelegateMsg>, DelegateError>

Process inbound message, producing zero or more outbound messages in response.

§Arguments
  • ctx: Mutable handle to the delegate’s execution environment. Provides:
    • Context (temporary): read(), write(), len(), clear() - state within a batch
    • Secrets (persistent): get_secret(), set_secret(), has_secret(), remove_secret()
  • parameters: The delegate’s initialization parameters.
  • origin: An optional MessageOrigin identifying where the message came from. For messages sent by web applications, this is MessageOrigin::WebApp(contract_id).
  • message: The inbound message to process.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§