-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionConfig.ts
More file actions
83 lines (71 loc) · 2.19 KB
/
ActionConfig.ts
File metadata and controls
83 lines (71 loc) · 2.19 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { AbstractActionComponent } from './Action/action-components/index.ts';
import { type Action } from './Action/index.ts';
import type { TransactionPayload } from './actions-spec.ts';
export interface ActionContext {
originalUrl: string;
action: Action;
actionType: 'trusted' | 'malicious' | 'unknown';
triggeredLinkedAction: AbstractActionComponent;
}
export interface IncomingActionConfig {
adapter: Pick<
ActionAdapter,
| 'connect'
| 'signTransaction'
| 'getConnectedAccount'
| 'interceptHandlePost'
| 'tsIframeRenderer'
> &
Partial<Pick<ActionAdapter, 'metadata'>>;
}
/**
* Metadata for an action adapter.
*
*
*/
export interface ActionAdapterMetadata {}
export interface ActionAdapter {
metadata: ActionAdapterMetadata;
connect: (context: ActionContext) => Promise<string | null>;
getConnectedAccount: () => Promise<string | null>;
signTransaction: (
payload: TransactionPayload,
context?: ActionContext,
) => Promise<
| { signature: string }
| { error: string | { code: number; message: string } }
>;
interceptHandlePost?: (href: AbstractActionComponent) => boolean|Promise<boolean>;
tsIframeRenderer?: (props: { websiteUrl: string }) => JSX.Element | null;
}
export class ActionConfig implements ActionAdapter {
private static readonly DEFAULT_METADATA: ActionAdapterMetadata = {};
constructor(private adapter: IncomingActionConfig['adapter']) {
this.adapter = adapter;
}
get metadata() {
return this.adapter.metadata ?? ActionConfig.DEFAULT_METADATA;
}
signTransaction(payload: TransactionPayload, context?: ActionContext) {
return this.adapter.signTransaction(payload, context);
}
async connect(context: ActionContext) {
try {
return await this.adapter.connect(context);
} catch {
return null;
}
}
async getConnectedAccount() {
try {
return await this.adapter.getConnectedAccount();
} catch {
return null;
}
}
async interceptHandlePost(component: AbstractActionComponent) {
return this.adapter.interceptHandlePost?.(component) || false;
}
tsIframeRenderer = (props: { websiteUrl: string }) =>
this.adapter.tsIframeRenderer?.(props) || null;
}