Skip to main content

createPermission

createPermission function allows to create new permissions within the RBAC (Role-Based Access Control) system. This function returns JSON object with permission ID.

Parameters:

  • permissionName (required): name of the permission to be created.

  • permissionId (optional): ID of the permission to be created.

  • address (optional): address associated with the permission. This parameter is not required.

  • seed (optional): mnemonic seed. This parameter is not required.

Code example


import { Sdk } from "@peaq-network/sdk";
import { mnemonicGenerate } from "@polkadot/util-crypto";

const generateMnemonicSeed = () => {
const mnemonicSeed = mnemonicGenerate();
return mnemonicSeed;
};

/**
* Create a new permission within the RBAC system.
* @param {string} permissionName - The name of the permission to be created (required).
* @param {string} [permissionId] - The ID of the permission (not required).
* @param {Address} [address] - The address associated with the permission (not required).
* @param {string} [seed] - The mnemonic seed (not required).
* @returns {string} The ID of the newly created permission.
*/
const createPermission = async (permissionName, seed) => {
const sdkInstance = await Sdk.createInstance({
baseUrl: "wss://wsspc1-qa.agung.peaq.network",
seed,
});

try {
const { permissionId: createdPermissionId } = await sdkInstance.rbac.createPermission({
permissionName,
});

return createdPermissionId;
} finally {
await sdkInstance.disconnect();
}
};

// Example usage
const name = 'myPermission';
// You need to have balance with this seed.
const mnemonicSeed = generateMnemonicSeed();
// Before creating your new permission, you should have some balance in your generated seed.
createPermission(name, mnemonicSeed)
.then((permissionId) => {
console.log(`Created permission Id: ${permissionId}`);
})
.catch((error) => {
console.error(`Error creating new permission: ${error}`);
});



Response object


{
permissionId: "18709409-bc06-4444-2222-b4zz0000"
}