Skip to content

Add context helpers for actor keys by usage#604

Closed
SergioChan wants to merge 1 commit intofedify-dev:mainfrom
SergioChan:add-actor-key-usage
Closed

Add context helpers for actor keys by usage#604
SergioChan wants to merge 1 commit intofedify-dev:mainfrom
SergioChan:add-actor-key-usage

Conversation

@SergioChan
Copy link

@SergioChan SergioChan commented Mar 6, 2026

Summary

Add Context helpers to retrieve actor public keys and assertion methods by usage, plus a convenience method for the first key.

Related issue

Changes

  • extend Context interface with getActorKeysByUsage/getActorFirstKeyByUsage
  • implement the new helpers in ContextImpl
  • add tests covering the new helpers

Benefits

Simplifies actor dispatcher code by avoiding manual key pair mapping for public keys and assertion methods.

Checklist

  • Did you add a changelog entry to the CHANGES.md?
  • Did you write some relevant docs about this change (if it's a new feature)?
  • Did you write a regression test to reproduce the bug (if it's a bug fix)?
  • Did you write some tests for this change (if it's a new feature)?
  • Did you run mise test on your machine?

Additional notes

Tests not run (not requested).

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces new utility methods within the federation context to streamline the retrieval of actor cryptographic keys. By providing helpers to access public keys and assertion methods based on their usage, it simplifies the logic for components like the actor dispatcher, reducing the need for manual key pair processing and enhancing code clarity.

Highlights

  • New Context Helpers: Two new methods, getActorKeysByUsage and getActorFirstKeyByUsage, have been added to the Context interface to retrieve actor public keys and assertion methods, grouped by usage or as the first available key, respectively.
  • Implementation: The ContextImpl class now includes the concrete implementations for the newly introduced getActorKeysByUsage and getActorFirstKeyByUsage methods, leveraging existing getActorKeyPairs functionality.
  • Test Coverage: Comprehensive tests have been added to middleware.test.ts to ensure the correct functionality and behavior of the new context helper methods.
Changelog
  • packages/fedify/src/federation/context.ts
    • Added getActorKeysByUsage method to the Context interface.
    • Added getActorFirstKeyByUsage method to the Context interface.
  • packages/fedify/src/federation/middleware.test.ts
    • Updated existing test to store getActorKeyPairs result for reuse.
    • Added tests for the new getActorKeysByUsage method.
    • Added tests for the new getActorFirstKeyByUsage method.
  • packages/fedify/src/federation/middleware.ts
    • Implemented getActorKeysByUsage method in ContextImpl.
    • Implemented getActorFirstKeyByUsage method in ContextImpl.
Activity
  • No human activity has occurred on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@issues-auto-labeler issues-auto-labeler bot added component/actor Actor dispatcher related component/federation Federation object related component/testing Testing utilities (@fedify/testing) labels Mar 6, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces two new helper methods, getActorKeysByUsage and getActorFirstKeyByUsage, to the Context for easier access to actor keys. The implementations and tests are well-structured. I've suggested one optimization for getActorFirstKeyByUsage to improve its efficiency by avoiding the creation of unnecessary intermediate arrays.

Comment on lines +1983 to +1993
async getActorFirstKeyByUsage(identifier: string): Promise<{
publicKey: CryptographicKey | undefined;
assertionMethod: Multikey | undefined;
}> {
const { publicKeys, assertionMethods } =
await this.getActorKeysByUsage(identifier);
return {
publicKey: publicKeys[0],
assertionMethod: assertionMethods[0],
};
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This implementation is correct, but it can be made more efficient. It currently calls getActorKeysByUsage, which creates two new arrays by mapping over all key pairs, only to then use the first element of each. You can optimize this by calling getActorKeyPairs directly and accessing the first element, avoiding the creation of intermediate arrays.

  async getActorFirstKeyByUsage(identifier: string): Promise<{
    publicKey: CryptographicKey | undefined;
    assertionMethod: Multikey | undefined;
  }> {
    const keys = await this.getActorKeyPairs(identifier);
    const firstKey = keys[0];
    if (firstKey == null) {
      return { publicKey: undefined, assertionMethod: undefined };
    }
    return {
      publicKey: firstKey.cryptographicKey,
      assertionMethod: firstKey.multikey,
    };
  }

@dahlia
Copy link
Member

dahlia commented Mar 6, 2026

Thank you so much for taking the time to implement this—I'm sorry you put in the effort before we had a chance to weigh in on the issue.

Unfortunately, we've just closed the related issue #488 as “not planned.” The main reasons are:

  • The ergonomic improvement over keys[0]?.cryptographicKey is minimal.
  • More importantly, "pick the first key" is too opinionated a choice to bake into the framework—applications may need to select keys by algorithm, key ID, or other criteria, and that logic belongs in application code rather than in Fedify itself.

I'm closing this PR accordingly. Again, I really appreciate your contribution, and I hope this doesn't discourage you from contributing in the future—please feel free to open a discussion first for larger changes so we can align early.

@dahlia dahlia closed this Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component/actor Actor dispatcher related component/federation Federation object related component/testing Testing utilities (@fedify/testing)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Context.getActorFirstKeyByUsage(id) and Context.getActorKeysByUsage(id).

2 participants