cadence
cadence copied to clipboard
Fetch all PublicAccount Keys
Issue To Be Solved
Allow fetching all keys from an account by doing something like PublicAccount.keys, currently this returns {}. The functionality is already present in flow-view-source and flow CLI (using the command flow accounts get {addres}.
Currently cadence only allows fetching individual keys with PublicAccount.keys.get(index).
Suggested Solution
Add a method to the PublicAccount.keys type which returns an array of all keyIds or key objects assigned to a PublicAccount.
Adding support for this should be possible. It would need some dedicated export code for SimpleCompositeValue where the type ID is PublicAccount.Keys in runtime.exportSimpleCompositeValue
In addition, it might make sense to add a let count: Int field to the *Account.Keys types
do we want to make visible the total number of keys in an AuthAccount?
also, what should the signature of this look like? we could return an entire array or do something similar to https://github.com/onflow/cadence/issues/1938, with short-circuiting iteration.
so the first option would either be a function that collects all keys, or a field member that provides a view into an underlying keys array:
fun {Auth,Public}Account.keys.getAll(): [AccountKey]
and the second would be similar to our storage path iteration API's, e.g. PublicAccount.forEachPublic,
fun {Auth,Public}Account.keys.iter(_ f: (AccountKey: Bool)): Void
@dreamsmasher
do we want to make visible the total number of keys in an AuthAccount? Good idea. Maybe
let length: Int, like on dictionaries and arrays?
also, what should the signature of this look like? we could return an entire array or do something similar to #1938, with short-circuiting iteration.
so the first option would either be a function that collects all keys, or a field member that provides a view into an underlying keys array:
fun {Auth,Public}Account.keys.getAll(): [AccountKey]and the second would be similar to our storage path iteration API's, e.g.
PublicAccount.forEachPublic,fun {Auth,Public}Account.keys.iter(_ f: (AccountKey: Bool)): Void
We might want have the API similar to the recently added storage path iteration functionality (see https://developers.flow.com/cadence/language/accounts#publicaccount):
Similar to how the *Account types have a *Paths computed field that returns all paths in the account storage, e.g. PublicAccount has let publicPaths: [PublicPath], it would be nice to have a similar computed field let all: [AccountKey] that returns all keys. I see this rather as optional, as it can be build using the more important function, a way to iterate over all keys:
Similar to how the *Account types have a forEach* function that iterates over all paths of a domain, e.g. PublicAccount has fun forEachPublic(_ function: ((PublicPath, Type): Bool)), it would be nice to have a similar function fun forEach(_ function: ((AccountKey): Bool)).
@turbolent off-topic a bit, but seeing recently a lot of forEach and iterators around issues. Can't we implement something more generic on arrays ? Something like: generic iterators on lazy arrays. I feel like we can detect move or assignment vs method call.
Yes, eventually it would be nice to have iterators, "lazy" data structures, etc. but they all require significantly more effort to add. We can at least already ship a solution to the problem that's low effort.
@dreamsmasher flow-go/FVM will need to implement the new AccountKeysCount function which was added to the interface. This is blocking a CLI release of Stable Cadence. Do you have an ETA for this? Or is the FVM team going to work on this?
working on it rn