Skip to content

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

@2chanhaeng

Description

@2chanhaeng

Summary

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

federation.setActorDispatcher(
  "/users/{identifier}",
  async (ctx, id) => {
    const { publicKey, assertionMethod } = await ctx.getActorFirstKeyByUsage(id)`.
    return new Person({ /* ... */, publicKey, assertionMethod });
  },
)

and

federation.setActorDispatcher(
  "/users/{identifier}",
  async (ctx, id) => {
    const { publicKeys, assertionMethods } = await ctx.getActorKeysByUsage(id)`.
    return new Person({ /* ... */, publicKeys, assertionMethods });
  },
)

Problem

Because this is too confused:

federation.setActorDispatcher(
  "/users/{identifier}",
  async (ctx, id) => {
    const keys = await ctx.getActorKeyPairs(id);
    return new Person({
      // ...
      publicKey: keys.map((key) => key.cryptographicKey)[0],
      assertionMethod: keys.map((key) => key.multikey)[0],
    });
  },
)

Proposed Solution

Add followings to packages/fedify/src/federation/middleware.ts:

export class ContextImpl<TContextData> implements Context<TContextData> {
  // ...
  async getActorKeysByUsage(identifier: string): Promise<{
    publicKeys: CryptographicKey[];
    assertionMethods: Multikey[];
  }> {
    return this.getActorKeyPairs(identifier)
      .then((keys) => ({
        publicKeys: keys.map((k) => k.cryptographicKey),
        assertionMethods: keys.map((k) => k.multikey),
      }));
  }

  async getActorFirstKeyByUsage(identifier: string): Promise<{
    publicKey: CryptographicKey;
    assertionMethod: Multikey;
  }> {
    return this.getActorKeysByUsage(identifier)
      .then(({ publicKeys, assertionMethods }) => ({
        publicKey: publicKeys[0],
        assertionMethod: assertionMethods[0],
      }));
  }
}

Alternatives Considered

No response

Scope / Dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions