8

I want the wallet that deployed the smart contract to be the owner of that smart contract and I need to set up access control such that only the owner of that program will be able to call any function. Need help as to how do I implement this. Need to implement this in anchor

2 Answers 2

3

All executable programs (smart contracts) are owned by the BPF loader.

What you refer to as the "owner" in this case, will be the upgrade authority.

You can implement access control checks by passing in an account as a signer and checking that account against public key(s) that should have access to call the function.

e.g.

  • Public key mYCustomPublickey12345 should be able to call any function
  • From the client: public key mYCustomPublickey12345 has to sign the transaction
  • Program: validates that mYCustomPublickey12345 signed the transaction

If mYCustomPublickey12345 hasn't signed the transaction, return an error.


edit:

This seems to be exactly what you're looking for: https://docs.rs/anchor-lang/latest/anchor_lang/accounts/program/struct.Program.html

3
  • do I type out the public key in the access control function? is there no variable in anchor that can store the upgrade authority?
    – fahad
    Commented Aug 12, 2022 at 12:05
  • This seems to be what you're looking for: docs.rs/anchor-lang/latest/anchor_lang/accounts/program/…
    – C.OG
    Commented Aug 12, 2022 at 12:19
  • thanks this has been really helpful. Any Idea how I would be able to do this if the Smart Contract was not upgrade able and did not have an upgrading authority?
    – fahad
    Commented Aug 15, 2022 at 11:58
1

anchor example

This Initialize instruction can be called only by Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS

#[derive(Accounts)]
#[instruction(random: String)]
pub struct Initialize<'info> {

    #[account(mut,address=pubkey!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"))]
    pub payer: Signer<'info>,

    ...

}

This will make sure that only the Signer with address Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS can call this instruction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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