What is the purpose of Interface<'info, T>
and InterfaceAccount<'info, T>
and in what scenario might I need to use them?
-
1It looks like you asked this question twice (and Jon answered it twice). Marking this one as a duplicate because the other answer is a little more in depth.– mikemaccana ♦Commented Aug 8 at 15:48
1 Answer
Interface
and InterfaceAccount
are useful when your program can handle any account or program that adheres to a particular interface.
By default, if you require an account type or program, Anchor enforces that the program must match the pubkey, and the owner of the account must match the pubkey of the program.
If you use Interface
, however, then the program / owner can be one of a set of addresses. It was implemented so that an Anchor program can work with both Token and Token-2022.
-
I'm interested in this. If I have a set of account types (
Dog
,Cat
), can I useInterfaceAccount
to check if an account in the ix is one of those types? I don't need to access the data from that account. Currently I'm usingUncheckedAccount
, and in the ix handler I check the owner of that account is my program, is that dangerous?– KiseCommented Jan 17 at 8:07 -
1You would need to be sure that your
Dog
andCat
accounts are deserializable as some more generalAnimal
type and then downcast during your instruction processing. But you don't needInterfaceAccount
for that! A normal account can be configured to do that. In general, it's best to use all of the checks provided by Anchor, but you can also mimic them in your code no problem.– Jon CCommented Jan 17 at 12:42 -
Thanks for the reply. In my case, I only care about the account's public key since I use it as one of the seeds for another account, so it's not necessary to deserialize the data. I just want some more assurance, so I put in a check that the given account is derived from my program. I've read some answers on here and people say to avoid the
UncheckedAccount
at all cost, so I worry there might be some exploits I'm not aware of.– KiseCommented Jan 17 at 16:59