1

What is the purpose of Interface<'info, T> and InterfaceAccount<'info, T> and in what scenario might I need to use them?

1
  • 1
    It 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 1

1

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.

3
  • I'm interested in this. If I have a set of account types (Dog, Cat), can I use InterfaceAccount 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 using UncheckedAccount, and in the ix handler I check the owner of that account is my program, is that dangerous?
    – Kise
    Commented Jan 17 at 8:07
  • 1
    You would need to be sure that your Dog and Cat accounts are deserializable as some more general Animal type and then downcast during your instruction processing. But you don't need InterfaceAccount 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 C
    Commented 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.
    – Kise
    Commented Jan 17 at 16:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.