addGroup
The addGroup
function is used to create a new group within the RBAC (Role-Based Access Control) system. This function returns a JSON object with groupID.
Parameters:
name (required)
: name of the group to be created.groupId (not required)
: ID of the group. If not provided, a new ID will be generated.address (not required)
: address associated with the operation.seed (not required)
: mnemonic seed used for transaction signing.
Code example
import { Sdk } from "@peaq-network/sdk";
import { mnemonicGenerate } from "@polkadot/util-crypto";
const generateMnemonicSeed = () => {
const mnemonicSeed = mnemonicGenerate();
return mnemonicSeed;
};
/**
* Create a new group within the RBAC system.
* @param {string} name - The name of the group to be created (required).
* @param {string} [groupId] - The ID of the group (not required).
* @param {Address} [address] - The address associated with the operation (not required).
* @param {string} [seed] - The mnemonic seed (not required).
* @returns {Promise<Object>} A promise that resolves to an object containing the groupId field.
*/
const addGroup = async (name, seed) => {
const sdkInstance = await Sdk.createInstance({
baseUrl: "wss://wsspc1-qa.agung.peaq.network",
seed,
});
try {
const { groupId: createdGroupId } = await sdkInstance.rbac.addGroup({
name
});
return createdGroupId;
} finally {
await sdkInstance.disconnect();
}
};
// Example usage
const groupName = 'myGroup';
const mnemonicSeed = generateMnemonicSeed();
addGroup(groupName, mnemonicSeed)
.then((groupId) => {
console.log(`Created group Id: ${groupId}`);
})
.catch((error) => {
console.error(`Error creating new group: ${error}`);
});
Response object
{
groupId: "02709409-kd01-4444-8222-b5zz8512"
}