createRole
The createRole
function from peaq SDK allows to create new roles in the network's Role-Based Access Control (RBAC) system, which manages roles and permissions within the blockchain ecosystem. The function returns a JSON object with roleId.
Parameters:
roleName (required)
: name of the role to be created.roleId (optional)
: ID of the role. If not provided, a new ID will be generated.address (optional)
: An address representing the association with the role.seed (optional)
: A string representing the mnemonic seed.
Code example
import { Sdk } from "@peaq-network/sdk";
import { mnemonicGenerate } from "@polkadot/util-crypto";
const generateMnemonicSeed = () => {
const mnemonicSeed = mnemonicGenerate();
return mnemonicSeed;
};
/**
* Create a new role within the RBAC system.
* @param {string} roleName - The name of the role to be created (required).
* @param {string} [roleId] - The ID of the role (not required).
* @param {Address} [address] - The address associated with the role (not required).
* @param {string} [seed] - The mnemonic seed (not required).
* @returns {string} The ID of the newly created role.
*/
const createRole = async (roleName, seed) => {
const sdkInstance = await Sdk.createInstance({
baseUrl: "wss://wsspc1-qa.agung.peaq.network",
seed,
});
try {
await sdkInstance.connect();
const roleId = await sdkInstance.rbac.createRole({
roleName: roleName,
});
return roleId;
} finally {
await sdkInstance.disconnect();
}
};
// Example usage
const roleName = 'myrole';
// You need to have balance with this seed, or use personal wallet.
const mnemonicSeed = generateMnemonicSeed();
// Before creating your new role, you should have some balance in your generated seed.
createRole(roleName, mnemonicSeed)
.then((roleId) => {
console.log(`Created role Id: ${roleId}`);
})
.catch((error) => {
console.error(`Error creating new role: ${error}`);
});
Response object
{
roleId: "78709009-cf06-4224-8632-b5ee8512"
}