feat: add host function API for delegate context and secrets access#49
Merged
iduartgomez merged 8 commits intomainfrom Feb 4, 2026
Merged
feat: add host function API for delegate context and secrets access#49iduartgomez merged 8 commits intomainfrom
iduartgomez merged 8 commits intomainfrom
Conversation
This adds a new, cleaner API for delegates to access context and secrets via opaque handles (`DelegateCtx` and `SecretsStore`) passed to the `process()` function. This eliminates the need for message round-trips (GetSecretRequest/GetSecretResponse) and allows synchronous access. Breaking change: `DelegateInterface::process` signature now takes `ctx: &mut DelegateCtx` and `secrets: &mut SecretsStore` as the first two parameters. Changes: - Add `delegate_host.rs` with `DelegateCtx` and `SecretsStore` types - Update `DelegateInterface` trait signature - Update `#[delegate]` macro to create and pass the handles - Update example delegate to demonstrate the new API
- Add error_codes module with named constants for all error values - Update __frnt__delegate__ctx_write to return i32 (error code) - Add __frnt__delegate__get_secret_len host function for querying secret size - Update SecretsStore::get to use get_secret_len for proper buffer allocation - Add SecretsStore::get_len method for querying secret size - Export error_codes module from prelude
Simplify the delegate host function API by merging SecretsStore functionality into DelegateCtx. Delegates now use a single context parameter that provides access to both: - Temporary context (read/write/clear) - Persistent secrets (get_secret/set_secret/has_secret/remove_secret) The SecretsStore type is retained as a deprecated type alias for backward compatibility. Changes: - Move secret methods (get/set/has/remove) to DelegateCtx - Update DelegateInterface trait to single ctx parameter - Update #[delegate] macro to remove secrets parameter - Deprecate SecretsStore as type alias to DelegateCtx
- Remove legacy DelegateInterface trait (without ctx parameter) - Remove #[cfg(feature = "contract")] guards from delegate_host - DelegateCtx is now always available, not feature-gated
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a cleaner API for delegates to access context and secrets via an opaque
DelegateCtxhandle passed to theprocess()function. This eliminates the need for message round-trips (GetSecretRequest/GetSecretResponse) and allows synchronous access during a singleprocess()call.Breaking Change
DelegateInterface::processsignature now takes aDelegateCtxas the first argument:API
The
DelegateCtxprovides unified access to both temporary context and persistent secrets:Context Methods (temporary state within a batch)
ctx.read()- Read current context bytesctx.write(data)- Write new context bytesctx.len()- Get context lengthctx.clear()- Clear the contextSecret Methods (persistent encrypted storage)
ctx.get_secret(key)- Get a secret by keyctx.set_secret(key, value)- Store a secretctx.has_secret(key)- Check if a secret existsctx.remove_secret(key)- Remove a secretctx.get_secret_len(key)- Get secret length without fetching valueChanges
delegate_host.rswith unifiedDelegateCtxtypeerror_codesmodule with standardized host function error codesDelegateInterfacetrait signature (breaking change)#[delegate]macro to create handle and pass toprocess()DelegateCtxis always availableExample Usage
Error Codes
Host functions return negative values to indicate errors:
Related
Requires corresponding runtime changes in freenet-core PR #2844 to provide the host functions.
[AI-assisted - Claude]