-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathabstracted_account.algo.ts
292 lines (251 loc) · 8.72 KB
/
abstracted_account.algo.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { Contract } from '@algorandfoundation/tealscript';
type PluginsKey = {
/** The application containing plugin logic */
application: AppID;
/** The address that is allowed to initiate a rekey to the plugin */
allowedCaller: Address;
};
type PluginInfo = {
/** The last round at which this plugin can be called */
lastValidRound: uint64;
/** The number of rounds that must pass before the plugin can be called again */
cooldown: uint64;
/** The last round the plugin was called */
lastCalled: uint64;
/** Whether the plugin has permissions to change the admin account */
adminPrivileges: boolean;
};
export class AbstractedAccount extends Contract {
/** Target AVM 10 */
programVersion = 10;
/** The admin of the abstracted account. This address can add plugins and initiate rekeys */
admin = GlobalStateKey<Address>({ key: 'a' });
/**
* Plugins that add functionality to the contract wallet and the account that has permission to use it.
*/
plugins = BoxMap<PluginsKey, PluginInfo>({ prefix: 'p' });
/**
* Plugins that have been given a name for discoverability
*/
namedPlugins = BoxMap<bytes, PluginsKey>({ prefix: 'n' });
/**
* Ensure that by the end of the group the abstracted account has control of itself
*/
private verifyRekeyToAbstractedAccount(): void {
let rekeyedBack = false;
for (let i = this.txn.groupIndex + 1; i < this.txnGroup.length; i += 1) {
const txn = this.txnGroup[i];
// The transaction is an explicit rekey back
if (txn.sender === this.app.address && txn.rekeyTo === this.app.address) {
rekeyedBack = true;
break;
}
// The transaction is an application call to this app's arc58_verifyAuthAddr method
if (
txn.typeEnum === TransactionType.ApplicationCall &&
txn.applicationID === this.app &&
txn.onCompletion === 0 && // OnCompletion.NoOp
txn.numAppArgs === 1 &&
txn.applicationArgs[0] === method('arc58_verifyAuthAddr()void')
) {
rekeyedBack = true;
break;
}
}
assert(rekeyedBack);
}
/**
* Create an abstracted account application.
* This is not part of ARC58 and implementation specific.
*
* @param admin The admin for this app
*/
createApplication(admin: Address): void {
verifyAppCallTxn(this.txn, {
sender: admin,
});
this.admin.value = admin;
}
/**
* Attempt to change the admin for this app. Some implementations MAY not support this.
*
* @param newAdmin The new admin
*/
arc58_changeAdmin(newAdmin: Address): void {
verifyTxn(this.txn, { sender: this.admin.value });
this.admin.value = newAdmin;
}
/**
* Attempt to change the admin via plugin.
*
* @param plugin The app calling the plugin
* @param allowedCaller The address that triggered the plugin
* @param newAdmin The new admin
*
*/
arc58_pluginChangeAdmin(plugin: AppID, allowedCaller: Address, newAdmin: Address): void {
verifyTxn(this.txn, { sender: plugin.address });
assert(this.app.address.authAddr === plugin.address, 'This plugin is not in control of the account');
const key: PluginsKey = { application: plugin, allowedCaller: allowedCaller };
assert(
this.plugins(key).exists && this.plugins(key).value.adminPrivileges,
'This plugin does not have admin privileges'
);
this.admin.value = newAdmin;
}
/**
* Get the admin of this app. This method SHOULD always be used rather than reading directly from state
* because different implementations may have different ways of determining the admin.
*/
@abi.readonly
arc58_getAdmin(): Address {
return this.admin.value;
}
/**
* Verify the abstracted account is rekeyed to this app
*/
arc58_verifyAuthAddr(): void {
assert(this.app.address.authAddr === globals.zeroAddress);
}
/**
* Rekey the abstracted account to another address. Primarily useful for rekeying to an EOA.
*
* @param addr The address to rekey to
* @param flash Whether or not this should be a flash rekey. If true, the rekey back to the app address must done in the same txn group as this call
*/
arc58_rekeyTo(addr: Address, flash: boolean): void {
verifyAppCallTxn(this.txn, { sender: this.admin.value });
sendPayment({
receiver: addr,
rekeyTo: addr,
note: 'rekeying abstracted account',
});
if (flash) this.verifyRekeyToAbstractedAccount();
}
private pluginCallAllowed(app: AppID, caller: Address): boolean {
const key: PluginsKey = { application: app, allowedCaller: caller };
return (
this.plugins(key).exists &&
this.plugins(key).value.lastValidRound >= globals.round &&
globals.round - this.plugins(key).value.lastCalled >= this.plugins(key).value.cooldown
);
}
/**
* check whether the plugin can be used
*
* @param plugin the plugin to be rekeyed to
* @returns whether the plugin can be called via txn sender or globally
*/
@abi.readonly
arc58_canCall(plugin: AppID, address: Address): boolean {
const globalAllowed = this.pluginCallAllowed(plugin, Address.zeroAddress);
if (globalAllowed) return true;
return this.pluginCallAllowed(plugin, address);
}
/**
* Temporarily rekey to an approved plugin app address
*
* @param plugin The app to rekey to
*/
arc58_rekeyToPlugin(plugin: AppID): void {
const globalAllowed = this.pluginCallAllowed(plugin, Address.zeroAddress);
if (!globalAllowed)
assert(this.pluginCallAllowed(plugin, this.txn.sender), 'This sender is not allowed to trigger this plugin');
sendPayment({
receiver: this.app.address,
rekeyTo: plugin.address,
note: 'rekeying to plugin app',
});
this.plugins({
application: plugin,
allowedCaller: globalAllowed ? Address.zeroAddress : this.txn.sender,
}).value.lastCalled = globals.round;
this.verifyRekeyToAbstractedAccount();
}
/**
* Temporarily rekey to a named plugin app address
*
* @param name The name of the plugin to rekey to
*/
arc58_rekeyToNamedPlugin(name: string): void {
this.arc58_rekeyToPlugin(this.namedPlugins(name).value.application);
}
/**
* Add an app to the list of approved plugins
*
* @param app The app to add
* @param allowedCaller The address of that's allowed to call the app
* or the global zero address for all addresses
* @param lastValidRound The round when the permission expires
* @param cooldown The number of rounds that must pass before the plugin can be called again
* @param adminPrivileges Whether the plugin has permissions to change the admin account
*/
arc58_addPlugin(
app: AppID,
allowedCaller: Address,
lastValidRound: uint64,
cooldown: uint64,
adminPrivileges: boolean
): void {
verifyTxn(this.txn, { sender: this.admin.value });
const key: PluginsKey = { application: app, allowedCaller: allowedCaller };
this.plugins(key).value = {
lastValidRound: lastValidRound,
cooldown: cooldown,
lastCalled: 0,
adminPrivileges: adminPrivileges,
};
}
/**
* Remove an app from the list of approved plugins
*
* @param app The app to remove
*/
arc58_removePlugin(app: AppID, allowedCaller: Address): void {
verifyTxn(this.txn, { sender: this.admin.value });
const key: PluginsKey = { application: app, allowedCaller: allowedCaller };
this.plugins(key).delete();
}
/**
* Add a named plugin
*
* @param app The plugin app
* @param name The plugin name
* @param allowedCaller The address of that's allowed to call the app
* or the global zero address for all addresses
* @param lastValidRound The round when the permission expires
* @param cooldown The number of rounds that must pass before the plugin can be called again
* @param adminPrivileges Whether the plugin has permissions to change the admin account
*/
arc58_addNamedPlugin(
name: string,
app: AppID,
allowedCaller: Address,
lastValidRound: uint64,
cooldown: uint64,
adminPrivileges: boolean
): void {
verifyTxn(this.txn, { sender: this.admin.value });
assert(!this.namedPlugins(name).exists);
const key: PluginsKey = { application: app, allowedCaller: allowedCaller };
this.namedPlugins(name).value = key;
const pluginInfo: PluginInfo = {
lastValidRound: lastValidRound,
cooldown: cooldown,
lastCalled: 0,
adminPrivileges: adminPrivileges,
};
this.plugins(key).value = pluginInfo;
}
/**
* Remove a named plugin
*
* @param name The plugin name
*/
arc58_removeNamedPlugin(name: string): void {
verifyTxn(this.txn, { sender: this.admin.value });
const app = this.namedPlugins(name).value;
this.namedPlugins(name).delete();
this.plugins(app).delete();
}
}