-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathapplication.ts
More file actions
237 lines (199 loc) · 5.58 KB
/
Copy pathapplication.ts
File metadata and controls
237 lines (199 loc) · 5.58 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { getMissingPermissions, type PermissionType } from "./permissions";
import { type Allowance, type AllowanceBigNumber, defaultAllowance } from "./allowance";
import { PersistentStorage, useStorage } from "~utils/storage";
import type { Storage } from "@plasmohq/storage";
import { defaultGateway, type Gateway } from "~gateways/gateway";
import BigNumber from "bignumber.js";
export const PREFIX = "app_";
export const defaultBundler = "https://turbo.ardrive.io";
export const pricingEndpoint = "https://payment.ardrive.io";
export default class Application {
/** Root URL of the app */
public url: string;
#storage: Storage;
constructor(url: string) {
this.url = url;
this.#storage = PersistentStorage;
}
/**
* Private method used to retrieve all settings for an app
* @returns settings objects or an empty object
*/
async #getSettings() {
const settings = await this.#storage.get<Record<string, any>>(`${PREFIX}${this.url}`);
return settings || {};
}
/**
* Update settings for the app
*
* @param val Object of settings to update to
*/
async updateSettings(
val:
| Partial<InitAppParams>
| ((current: Record<string, any>) => Partial<InitAppParams> | Promise<Partial<InitAppParams>>),
) {
const settings = await this.#getSettings();
if (typeof val === "function") {
val = await val(settings);
}
// update keys
for (const key in val) {
settings[key] = val[key];
}
// save settings
const res = await this.#storage.set(`${PREFIX}${this.url}`, settings);
return res;
}
/**
* App name and logo
*/
async getAppData(): Promise<AppInfo> {
const settings = await this.#getSettings();
return {
name: settings.name,
logo: settings.logo,
};
}
/**
* Permissions granted to this app
*/
async getPermissions(): Promise<PermissionType[]> {
const settings = await this.#getSettings();
return settings.permissions || [];
}
/**
* Check if the app has the provided permissions
*
* @param permissions Permissions to check for
*/
async hasPermissions(permissions: PermissionType[]): Promise<{
/** App has permissions or not */
result: boolean;
/** Existing permissions */
has: PermissionType[];
/** Missing permissions */
missing: PermissionType[];
}> {
const existingPermissions = await this.getPermissions();
const missing = getMissingPermissions(existingPermissions, permissions);
return {
result: missing.length === 0,
has: existingPermissions,
missing,
};
}
/**
* Get if the app is connected to Wander
*/
async isConnected() {
const permissions = await this.getPermissions();
return permissions.length > 0;
}
/**
* Check if the app is present in the storage
*/
async isAppPresent() {
const settings = await this.#getSettings();
return Object.keys(settings).length > 0;
}
/**
* Gateway config for each individual app
*/
async getGatewayConfig(): Promise<Gateway> {
const settings = await this.#getSettings();
// TODO: wayfinder
return settings.gateway || defaultGateway;
}
/**
* Get the URL of the service for submitting data
* to Arweave instead of using a gateway
*/
async getBundler(): Promise<string> {
const settings = await this.#getSettings();
return settings.bundler || defaultBundler;
}
/**
* Allowance limit and spent qty
*/
async getAllowance(): Promise<AllowanceBigNumber> {
const settings = await this.#getSettings();
const allowance = settings.allowance || defaultAllowance;
return {
enabled: allowance.enabled,
limit: BigNumber(allowance.limit),
spent: BigNumber(allowance.spent),
};
}
/**
* Sign policy for the app
*/
async getSignPolicy(): Promise<"always_ask" | "ask_when_spending" | "auto_confirm"> {
const settings = await this.#getSettings();
return settings.signPolicy || "always_ask";
}
/**
* Blocked from interacting with Wander
*/
async isBlocked(): Promise<boolean> {
const settings = await this.#getSettings();
return !!settings.blocked;
}
hook() {
return useStorage<InitAppParams>(
{
key: `${PREFIX}${this.url}`,
instance: PersistentStorage,
},
(val) => {
if (typeof val === "undefined") return val;
// assign with default values
const values = {
allowance: defaultAllowance,
// TODO: wayfinder
gateway: defaultGateway,
bundler: defaultBundler,
...val,
};
return values;
},
);
}
}
/**
* App info submitted by the dApp
*/
export interface AppInfo {
name?: string;
logo?: string;
}
/**
* App Logo info to be used to derive the proper placeholder
* extends AppInfo's name and logo
* adding an optional type: "default" | "gateway" and placeholder
*/
export interface AppLogoInfo extends AppInfo {
type?: "default" | "gateway";
placeholder?: string;
}
/**
* Sign policy for the app
* - **always_ask**: always ask the user to sign
* - **ask_when_spending**: ask the user to sign when user assets are being spent or has network fees to be paid else auto sign
* - **auto_confirm**: automatically sign every transactions
*
* @default "always_ask"
*/
export type SignPolicy = "always_ask" | "ask_when_spending" | "auto_confirm";
/**
* Params to add an app with
*/
export interface InitAppParams extends AppInfo {
url: string;
permissions: PermissionType[];
gateway?: Gateway;
allowance?: Allowance;
blocked?: boolean;
bundler?: string;
signPolicy?: SignPolicy;
}