forked from joe-p/arc58
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstracted_account.algo.ts
213 lines (180 loc) · 6.75 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
import { Contract } from '@algorandfoundation/tealscript';
type PluginsKey = { application: AppID; allowedCaller: Address };
export class AbstractedAccount extends Contract {
/** Target AVM 10 */
programVersion = 10;
/** The admin of the abstracted account */
admin = GlobalStateKey<Address>({ key: 'a' });
/** The address this app controls */
controlledAddress = GlobalStateKey<Address>({ key: 'c' });
/**
* The apps and addresses that are authorized to send itxns from the abstracted account,
* The key is the appID + address, the value (referred to as `end`)
* is the timestamp when the permission expires for the address to call the app for your account.
*/
plugins = BoxMap<PluginsKey, uint64>({ 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 its address
*/
private verifyRekeyToAbstractedAccount(): void {
let rekeyedBack = false;
for (let i = this.txn.groupIndex; i < this.txnGroup.length; i += 1) {
const txn = this.txnGroup[i];
// The transaction is an explicit rekey back
if (txn.sender === this.controlledAddress.value && txn.rekeyTo === this.getAuthAddr()) {
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.numAppArgs === 1 &&
txn.applicationArgs[0] === method('arc58_verifyAuthAddr()void')
) {
rekeyedBack = true;
break;
}
}
assert(rekeyedBack);
}
/**
* What the value of this.address.value.authAddr should be when this.controlledAddress
* is able to be controlled by this app. It will either be this.app.address or zeroAddress
*/
private getAuthAddr(): Address {
return this.controlledAddress.value === this.app.address ? Address.zeroAddress : this.app.address;
}
/**
* Create an abstracted account application.
* This is not part of ARC58 and implementation specific.
*
* @param controlledAddress The address of the abstracted account. If zeroAddress, then the address of the contract account will be used
* @param admin The admin for this app
*/
createApplication(controlledAddress: Address, admin: Address): void {
verifyAppCallTxn(this.txn, {
sender: { includedIn: [controlledAddress, admin] },
});
assert(admin !== controlledAddress);
this.admin.value = admin;
this.controlledAddress.value = controlledAddress === Address.zeroAddress ? this.app.address : controlledAddress;
}
/**
* 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;
}
/**
* 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.
*/
arc58_getAdmin(): Address {
return this.admin.value;
}
/**
* Verify the abstracted account is rekeyed to this app
*/
arc58_verifyAuthAddr(): void {
assert(this.controlledAddress.value.authAddr === this.getAuthAddr());
}
/**
* 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({
sender: this.controlledAddress.value,
receiver: addr,
rekeyTo: addr,
note: 'rekeying abstracted account',
});
if (flash) this.verifyRekeyToAbstractedAccount();
}
/**
* Temporarily rekey to an approved plugin app address
*
* @param plugin The app to rekey to
*/
arc58_rekeyToPlugin(plugin: AppID): void {
const globalKey: PluginsKey = { application: plugin, allowedCaller: globals.zeroAddress };
// If this plugin is not approved globally, then it must be approved for this address
if (!this.plugins(globalKey).exists || this.plugins(globalKey).value < globals.latestTimestamp) {
const key: PluginsKey = { application: plugin, allowedCaller: this.txn.sender };
assert(this.plugins(key).exists && this.plugins(key).value > globals.latestTimestamp);
}
sendPayment({
sender: this.controlledAddress.value,
receiver: this.controlledAddress.value,
rekeyTo: plugin.address,
note: 'rekeying to plugin app',
});
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 end The timestamp when the permission expires
*/
arc58_addPlugin(app: AppID, allowedCaller: Address, end: uint64): void {
verifyTxn(this.txn, { sender: this.admin.value });
const key: PluginsKey = { application: app, allowedCaller: allowedCaller };
this.plugins(key).value = end;
}
/**
* 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
*/
arc58_addNamedPlugin(name: string, app: AppID, allowedCaller: Address, end: uint64): 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;
this.plugins(key).value = end;
}
/**
* 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();
}
}