forked from fixes-world/elizaOnFlow
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAccountsPool.cdc
290 lines (241 loc) · 9.83 KB
/
AccountsPool.cdc
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
/**
> Author: Fixes Lab <https://github.com/fixes-world/>
# Accounts Pool
The Hybrid Custody Account Pool.
This contract is responsible for managing all the child accounts.
*/
import "EVM"
import "ViewResolver"
import "HybridCustody"
access(all) contract AccountsPool {
access(all) entitlement Admin
access(all) entitlement Child
/* --- Events --- */
/// Event emitted when a new child account is added, if tick is nil, it means the child account is not a shared account
access(all) event NewChildAccountAdded(type: String, address: Address, key: String?)
/* --- Variable, Enums and Structs --- */
access(all)
let StoragePath: StoragePath
access(all)
let PublicPath: PublicPath
/// The public interface can be accessed by anyone
///
access(all) resource interface PoolPublic {
/// ---- Getters ----
/// Returns the addresses of the FRC20 with the given type
access(all)
view fun getAddresses(type: String): {String: Address}
/// Get Address
access(all)
view fun getAddress(type: String, _ key: String): Address?
/// Get Children Amount
access(all)
view fun getChildrenAmount(type: String): UInt64
/// ----- Access account methods -----
/// Borrow child's AuthAccount
access(Child)
view fun borrowChildAccount(type: String, _ key: String?): auth(Storage, Contracts, Keys, Inbox, Capabilities) &Account?
}
/// The admin interface can only be accessed by the the account manager's owner
///
access(all) resource interface PoolAdmin {
/// Sets up a new child account
access(Admin)
fun setupNewChildByKey(
type: String,
key: String,
_ acctCap: Capability<auth(Storage, Contracts, Keys, Inbox, Capabilities) &Account>,
)
}
access(all) resource Pool: PoolPublic, PoolAdmin {
access(self)
let hcManagerCap: Capability<auth(HybridCustody.Manage) &HybridCustody.Manager>
// AccountType -> Key -> Address
access(self)
let addressMapping: {String: {String: Address}}
init(
_ hcManagerCap: Capability<auth(HybridCustody.Manage) &HybridCustody.Manager>
) {
self.hcManagerCap = hcManagerCap
self.addressMapping = {}
}
/** ---- Public Methods ---- */
/// Returns the addresses of the FRC20 with the given type
access(all)
view fun getAddresses(type: String): {String: Address} {
if let tickDict = self.addressMapping[type] {
return tickDict
}
return {}
}
/// Get Address
///
access(all)
view fun getAddress(type: String, _ key: String): Address? {
if let dict = self.borrowDict(type) {
return dict[key]
}
return nil
}
/// Get Children Amount
access(all)
view fun getChildrenAmount(type: String): UInt64 {
if let dict = self.borrowDict(type) {
return UInt64(dict.keys.length)
}
return 0
}
/// ----- Access account methods -----
/// Borrow child's AuthAccount
///
access(Child)
view fun borrowChildAccount(type: String, _ key: String?): auth(Storage, Contracts, Keys, Inbox, Capabilities) &Account? {
let hcManagerRef = self.hcManagerCap.borrow()
?? panic("Failed to borrow hcManager")
let specifiedKey = key ?? ""
if let dict = self.borrowDict(type) {
if let childAddr = dict[specifiedKey] {
if let ownedChild = hcManagerRef.borrowOwnedAccount(addr: childAddr) {
return ownedChild.borrowAccount()
}
}
}
return nil
}
/** ---- Admin Methods ---- */
/// Sets up a new child account
///
access(Admin)
fun setupNewChildByKey(
type: String,
key: String,
_ childAcctCap: Capability<auth(Storage, Contracts, Keys, Inbox, Capabilities) &Account>,
) {
pre {
childAcctCap.check(): "Child account capability is invalid"
}
self._ensureDictExists(type)
let dict = self.borrowDict(type) ?? panic("Failed to borrow tick ")
// no need to setup if already exists
if dict[key] != nil {
return
}
// record new child account address
dict[key] = childAcctCap.address
// setup new child account
self._setupChildAccount(childAcctCap)
// emit event
emit NewChildAccountAdded(
type: type,
address: childAcctCap.address,
key: key,
)
}
/** ---- Internal Methods ---- */
/// Sets up a new child account
///
access(self)
fun _setupChildAccount(
_ childAcctCap: Capability<auth(Storage, Contracts, Keys, Inbox, Capabilities) &Account>,
) {
let hcManager = self.hcManagerCap.borrow() ?? panic("Failed to borrow hcManager")
let hcManagerAddr = self.hcManagerCap.address
// >>> [0] Get child AuthAccount
var child = childAcctCap.borrow()
?? panic("Failed to borrow child account")
// >>> [1] Child: createOwnedAccount
if child.storage.borrow<&AnyResource>(from: HybridCustody.OwnedAccountStoragePath) == nil {
let ownedAccount <- HybridCustody.createOwnedAccount(acct: childAcctCap)
child.storage.save(<-ownedAccount, to: HybridCustody.OwnedAccountStoragePath)
}
// ensure owned account exists
let childRef = child.storage
.borrow<auth(HybridCustody.Owner) &HybridCustody.OwnedAccount>(from: HybridCustody.OwnedAccountStoragePath)
?? panic("owned account not found")
// check that paths are all configured properly
// public path
let _unpub1 = child.capabilities.unpublish(HybridCustody.OwnedAccountPublicPath)
child.capabilities.publish(
child.capabilities.storage.issue<&HybridCustody.OwnedAccount>(HybridCustody.OwnedAccountStoragePath),
at: HybridCustody.OwnedAccountPublicPath
)
let publishIdentifier = HybridCustody.getOwnerIdentifier(hcManagerAddr)
// give ownership to manager
childRef.giveOwnership(to: hcManagerAddr)
// only childRef will be available after 'giveaway', so we need to re-borrow it
child = childRef.borrowAccount()
// unpublish the priv capability
let _unpub2 = child.inbox.unpublish<
auth(HybridCustody.Owner) &{HybridCustody.OwnedAccountPrivate, HybridCustody.OwnedAccountPublic, ViewResolver.Resolver}
>(publishIdentifier)
// >> [2] manager: add owned child account
// Link a Capability for the new owner, retrieve & publish
let ownedPrivCap = child.capabilities.storage
.issue<auth(HybridCustody.Owner) &{HybridCustody.OwnedAccountPrivate, HybridCustody.OwnedAccountPublic, ViewResolver.Resolver}>(HybridCustody.OwnedAccountStoragePath)
assert(ownedPrivCap.check(), message: "Failed to get owned account capability")
// add owned account to manager
hcManager.addOwnedAccount(cap: ownedPrivCap)
// >> [3] Child: Ensure the child account is initialized with COA
let storagePath = StoragePath(identifier: "evm")!
let publicPath = PublicPath(identifier: "evm")!
if child.storage.borrow<&AnyResource>(from: storagePath) == nil {
let coa <- EVM.createCadenceOwnedAccount()
// Save the COA to the new account
child.storage.save<@EVM.CadenceOwnedAccount>(<-coa, to: storagePath)
let addressableCap = child.capabilities.storage.issue<&EVM.CadenceOwnedAccount>(storagePath)
let _unpub3 = child.capabilities.unpublish(publicPath)
child.capabilities.publish(addressableCap, at: publicPath)
}
}
/// Borrow dictioinary
///
access(self)
view fun borrowDict(_ type: String): auth(Mutate) &{String: Address}? {
return &self.addressMapping[type]
}
/// ensure type dict exists
///
access(self)
fun _ensureDictExists(_ type: String) {
if self.addressMapping[type] == nil {
self.addressMapping[type] = {}
}
}
}
/* --- Public Methods --- */
/// Returns the public account manager interface
///
access(all)
view fun borrowAccountsPool(
_ from: Address
): &Pool? {
return getAccount(from)
.capabilities.get<&Pool>(self.PublicPath)
.borrow()
}
/// Creates a new Pool resource
///
access(all)
fun createAccountsPool(
_ cap: Capability<auth(HybridCustody.Manage) &HybridCustody.Manager>
): @Pool {
return <- create Pool(cap)
}
access(all)
fun isAddressOwnedBy(
_ mainAddr: Address,
checkAddress: Address
): Bool {
if let childOwnedAcct = getAccount(checkAddress)
.capabilities.get<&HybridCustody.OwnedAccount>(HybridCustody.OwnedAccountPublicPath)
.borrow() {
return childOwnedAcct.isChildOf(mainAddr)
}
return false
}
init() {
let identifier = "AccountsPool_".concat(self.account.address.toString())
self.StoragePath = StoragePath(identifier: identifier)!
self.PublicPath = PublicPath(identifier: identifier)!
}
}