-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmy-plugin.ts
43 lines (37 loc) · 1.55 KB
/
my-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { IAgentPlugin } from '@veramo/core-types'
import {
IMyAgentPlugin,
IMyAgentPluginFooArgs,
IRequiredContext,
IMyAgentPluginFooResult
} from '../types/IMyAgentPlugin.js'
import schema from "../plugin.schema.json" assert { type: 'json' }
/**
* {@inheritDoc IMyAgentPlugin}
* @beta
*/
export class MyAgentPlugin implements IAgentPlugin {
readonly schema = schema.IMyAgentPlugin
// map the methods your plugin is declaring to their implementation
readonly methods: IMyAgentPlugin = {
myPluginFoo: this.myPluginFoo.bind(this),
}
// list the event types that this plugin cares about.
// When the agent emits an event of these types, `MyAgentPlugin.onEvent()` will get called.
readonly eventTypes = ['validatedMessage']
// the event handler for the types listed in `eventTypes`
public async onEvent(event: { type: string; data: any }, context: IRequiredContext) {
// you can emit other events
await context.agent.emit('my-event', { foo: event.data.id })
// or call other agent methods that are declared in the context
const allDIDs = await context.agent.didManagerFind()
}
/** {@inheritDoc IMyAgentPlugin.myPluginFoo} */
private async myPluginFoo(args: IMyAgentPluginFooArgs, context: IRequiredContext): Promise<IMyAgentPluginFooResult> {
// you can call other agent methods (that are declared in the `IRequiredContext`)
const didDoc = await context.agent.resolveDid({ didUrl: args.did })
// or emit some events
await context.agent.emit('my-other-event', { foo: 'hello' })
return { foobar: args.bar }
}
}