Skip to main content Link Menu Expand (external link) Document Search Copy Copied

SC Test demo example


Table of contents

  1. Create a Sol Cerberus instance
  2. Create Sol Cerberus app
  3. Assign role to a wallet
  4. Grant some permission to the role
  5. Fetch the updated permissions
  6. Login using the allowed wallet
  7. Verify access

The easiest to understand testing is by looking at the tests examples of the Sol Cerberus Demo program.

Create a Sol Cerberus instance

You can use either a random generated SC app ID or a fixed public key.

solCerberus = new SolCerberus(PROVIDER.connection, PROVIDER.wallet, {
    appId: new PublicKey("REPLACE_BY_YOUR_SC_APP_ID"),
  });

Demo code example

Create Sol Cerberus app

Creates a SC app in the block-chain for your Anchor program, using the app ID defined on the previous step.

await solCerberus.initializeApp("SolCerberusRBAC", null, {
    cached: false,
});

Demo code example

Assign role to a wallet

  await solCerberus.assignRole(
      "MyRole",
      addressTypes.Wallet,
      new PublicKey("HERE_THE_ADDRESS_OF_THE_ALLOWED_WALLET")
    );

Demo code example

Grant some permission to the role

await solCerberus.addRule("MyRole", "Square", "Add");

Demo code example

Fetch the updated permissions

await solCerberus.fetchPerms();

Demo code example

Login using the allowed wallet

await solCerberus.login({ wallet: new PublicKey("HERE_THE_ADDRESS_OF_THE_ALLOWED_WALLET") });

Demo code example

Verify access

Check an instruction of your program using the allowed wallet to verify access

    await DEMO_PROGRAM.methods
    .addSquare("ff0000", 50)
    .accounts({
      demo: demoPda,
      signer: new PublicKey("HERE_THE_ADDRESS_OF_THE_ALLOWED_WALLET"),
      ...(await solCerberus.accounts("Square", "Add")),
    })
    .signers([ALLOWED_WALLET_KEY_PAIR])
    .rpc();

Demo code example