> ## Documentation Index
> Fetch the complete documentation index at: https://docs.peaq.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Group

> Create RBAC groups and fetch group information with the peaq JavaScript SDK. Covers group names, IDs, and EVM and Substrate examples.

## createGroup(groupName, groupId, seed)

Used to create a new group within the RBAC (Role-Based Access Control) system.

On EVM this call returns the transaction object together with the `groupId`, so you can record the ID that was generated for the group name.

| Parameter | Type | EVM | Substrate | Description |
| - | - | - | - | - |
| **groupName** | `string` | Required | Required | Name of the group to be created. |
| **groupId** | `string` | Optional | Optional | ID of the group. If not provided, a new ID will be generated. |
| **seed** | `string` | N/A | Optional | If not set at instance creation, allows user to define who sends the Substrate Transactions. |

### Create Group Code Examples

```javascript EVM Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const HTTPS_BASE_URL = "https://quicknode1.peaq.xyz";
const EVM_PRIVATE = process.env["EVM_PRIVATE"];

const sdk = await Sdk.createInstance({
    baseUrl: HTTPS_BASE_URL,
    chainType: Sdk.ChainType.EVM
});

const groupName = "peaq-group-1";
const group = await sdk.rbac.createGroup({
    groupName: groupName
});

// Send using Sdk function
const receipt = await Sdk.sendEvmTx({
  tx: group.tx,
  baseUrl: HTTPS_BASE_URL,
  seed: EVM_PRIVATE
});

// Log to see what the auto generated groupId is
console.log(group.groupId);
```

```javascript EVM Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.createGroup({}) return object
{
  tx: {
    to: '0x0000000000000000000000000000000000000802',
    data: '0x65c1e09c35386135323638342d336163652d346537322d613032352d38353038356231640000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000c706561712d67726f75702d310000000000000000000000000000000000000000'
  },
  groupId: '58a52684-3ace-4e72-a025-85085b1d'
}
```

```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const SUBSTRATE_SEED = process.env["SUBSTRATE_SEED"];

const sdk = await Sdk.createInstance({
    baseUrl: WSS_BASE_URL,
    chainType: Sdk.ChainType.SUBSTRATE,
    seed: SUBSTRATE_SEED
});

const groupName = "peaq-group-1";
const response = await sdk.rbac.createGroup({
    groupName: groupName
});

// disconnect when finished
await sdk.disconnect();
```

```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.createGroup({}) return object
{ groupId: '63d6417e-1a03-4fd8-9439-295ce979' }
```

## fetchGroup(owner, groupId, wssBaseUrl)

Fetches group information from the RBAC system based on the provided group ID and owner's address.

| Parameter | Type | EVM | Substrate | Description |
| - | - | - | - | - |
| **owner** | `string` | Required | Required | Address representing the owner of the group. |
| **groupId** | `string` | Required | Required | ID of the group to be fetched. |
| **wssBaseUrl** | `string` | Required | N/A | WSS URL used to query RBAC storage. |

### Fetch Group Code Examples

```javascript EVM Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const HTTPS_BASE_URL = "https://quicknode1.peaq.xyz";
const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const EVM_ADDRESS = process.env["EVM_ADDRESS"];

const sdk = await Sdk.createInstance({
    baseUrl: HTTPS_BASE_URL,
    chainType: Sdk.ChainType.EVM
});

// example of a previously created group id
const groupId = "58a52684-3ace-4e72-a025-85085b1d";

const response = await sdk.rbac.fetchGroup({
    owner: EVM_ADDRESS,
    groupId: groupId,
    wssBaseUrl: WSS_BASE_URL
});
```

```javascript EVM Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.fetchGroup({}) response object
{
  id: '58a52684-3ace-4e72-a025-85085b1d',
  name: 'peaq-group-1',
  enabled: true
}
```

```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const SUBSTRATE_ADDRESS = process.env["SUBSTRATE_ADDRESS"];

const sdk = await Sdk.createInstance({
    baseUrl: WSS_BASE_URL,
    chainType: Sdk.ChainType.SUBSTRATE
});

// example of a previously created group id
const groupId = "63d6417e-1a03-4fd8-9439-295ce979";

const response = await sdk.rbac.fetchGroup({
    owner: SUBSTRATE_ADDRESS,
    groupId: groupId
});

// disconnect when finished
await sdk.disconnect();
```

```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.fetchGroup({}) response object
{
  id: '63d6417e-1a03-4fd8-9439-295ce979',
  name: 'peaq-group-1',
  enabled: true
}
```

## fetchGroups(owner, wssBaseUrl)

Used to fetch all the groups associated with the passed owner address.

| Parameter | Type | EVM | Substrate | Description |
| - | - | - | - | - |
| **owner** | `string` | Required | Required | Address representing the owner of all the fetched groups. |
| **wssBaseUrl** | `string` | Required | N/A | WSS URL used to query RBAC storage. |

### Fetch Groups Code Examples

```javascript EVM Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const HTTPS_BASE_URL = "https://quicknode1.peaq.xyz";
const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const EVM_ADDRESS = process.env["EVM_ADDRESS"];

const sdk = await Sdk.createInstance({
    baseUrl: HTTPS_BASE_URL,
    chainType: Sdk.ChainType.EVM
});

const response = await sdk.rbac.fetchGroups({
    owner: EVM_ADDRESS,
    wssBaseUrl: WSS_BASE_URL
});
```

```javascript EVM Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.fetchGroups({}) response object
[
  {
    id: 'eda650fa-ec62-4d09-849b-b66c7771',
    name: 'group-name-123',
    enabled: true
  },
    ...
  {
    id: '58a52684-3ace-4e72-a025-85085b1d',
    name: 'peaq-group-1',
    enabled: true
  }
]
```

```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const SUBSTRATE_ADDRESS = process.env["SUBSTRATE_ADDRESS"];

const sdk = await Sdk.createInstance({
    baseUrl: WSS_BASE_URL,
    chainType: Sdk.ChainType.SUBSTRATE
});

const response = await sdk.rbac.fetchGroups({
    owner: SUBSTRATE_ADDRESS
});

// disconnect when finished
await sdk.disconnect();
```

```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.fetchGroups({}) response object
[
  {
    id: '9763c33e-2b1b-4834-a908-03f2a9e0',
    name: 'group-name-123',
    enabled: true
  },
    ...
  {
    id: '63d6417e-1a03-4fd8-9439-295ce979',
    name: 'peaq-group-1',
    enabled: true
  }
]
```

## updateGroup(groupName, groupId, seed)

Allows the owner to update the group name.

| Parameter | Type | EVM | Substrate | Description |
| - | - | - | - | - |
| **groupName** | string | Required | Required | Updated group name. |
| **groupId** | string | Required | Required | ID of the group (32 bytes) to be updated. |
| **seed** | string | N/A | Optional | If not set at instance creation, allows user to define who sends the Substrate Transactions. |

### Update Group Code Examples

```javascript EVM Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const HTTPS_BASE_URL = "https://quicknode1.peaq.xyz";
const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const EVM_ADDRESS = process.env["EVM_ADDRESS"];
const EVM_PRIVATE = process.env["EVM_PRIVATE"];

const sdk = await Sdk.createInstance({
    baseUrl: HTTPS_BASE_URL,
    chainType: Sdk.ChainType.EVM
});

// new group name that will be set
const groupName = "peaq-group-new";

// example of a previously created group id
const groupId = "58a52684-3ace-4e72-a025-85085b1d";

// build the evm tx
const tx = await sdk.rbac.updateGroup({
    groupName: groupName,
    groupId: groupId
});

// send using Sdk function
const receipt = await Sdk.sendEvmTx({
  tx: tx,
  baseUrl: HTTPS_BASE_URL,
  seed: EVM_PRIVATE
});

// fetch Group to confirm the name change
const response = await sdk.rbac.fetchGroup({
    owner: EVM_ADDRESS,
    groupId: groupId,
    wssBaseUrl: WSS_BASE_URL
});
```

```javascript EVM Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.updateGroup({}) return object
{
  to: '0x0000000000000000000000000000000000000802',
  data: '0xadb32ee135386135323638342d336163652d346537322d613032352d38353038356231640000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000e706561712d67726f75702d6e6577000000000000000000000000000000000000'
}
```

```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const SUBSTRATE_ADDRESS = process.env["SUBSTRATE_ADDRESS"];
const SUBSTRATE_SEED = process.env["SUBSTRATE_SEED"];

const sdk = await Sdk.createInstance({
    baseUrl: WSS_BASE_URL,
    chainType: Sdk.ChainType.SUBSTRATE,
    seed: SUBSTRATE_SEED
});

// new group name that will be set
const groupName = "peaq-group-new";

// example of a previously created group id
const groupId = "63d6417e-1a03-4fd8-9439-295ce979";

// build the evm tx
const response = await sdk.rbac.updateGroup({
    groupName: groupName,
    groupId: groupId
});

// fetch Group to confirm the name change
const fetchedGroup = await sdk.rbac.fetchGroup({
    owner: SUBSTRATE_ADDRESS,
    groupId: groupId
});

// disconnect when finished
await sdk.disconnect();
```

```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.updateGroup({}) return object
{
  message: 'Successfully update group 63d6417e-1a03-4fd8-9439-295ce979 with new name: peaq-group-new'
}
```

## disableGroup(groupId, seed)

Disables a group.

| Parameter | Type | EVM | Substrate | Description |
| - | - | - | - | - |
| **groupId** | `string` | Required | Required | The unique identifier (ID) of the group to be disabled. |
| **seed** | `string` | N/A | Optional | If not set at instance creation, allows user to define who sends the Substrate Transactions. |

### Disable Group Code Examples

```javascript EVM Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const HTTPS_BASE_URL = "https://quicknode1.peaq.xyz";
const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const EVM_ADDRESS = process.env["EVM_ADDRESS"];
const EVM_PRIVATE = process.env["EVM_PRIVATE"];

const sdk = await Sdk.createInstance({
    baseUrl: HTTPS_BASE_URL,
    chainType: Sdk.ChainType.EVM
});

// group id to disable
const groupId = "58a52684-3ace-4e72-a025-85085b1d";

// build the evm tx
const tx = await sdk.rbac.disableGroup({
    groupId: groupId
});

// send using Sdk function
const receipt = await Sdk.sendEvmTx({
  tx: tx,
  baseUrl: HTTPS_BASE_URL,
  seed: EVM_PRIVATE
});

// fetch group to confirm it has been changed to disabled
const response = await sdk.rbac.fetchGroup({
    owner: EVM_ADDRESS,
    groupId: groupId,
    wssBaseUrl: WSS_BASE_URL
});
```

```javascript EVM Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.disableGroup({}) return object
{
  to: '0x0000000000000000000000000000000000000802',
  data: '0x47538f1235386135323638342d336163652d346537322d613032352d3835303835623164'
}
```

```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const SUBSTRATE_ADDRESS = process.env["SUBSTRATE_ADDRESS"];
const SUBSTRATE_SEED = process.env["SUBSTRATE_SEED"];

const sdk = await Sdk.createInstance({
    baseUrl: WSS_BASE_URL,
    chainType: Sdk.ChainType.SUBSTRATE,
    seed: SUBSTRATE_SEED
});

// group id to disable
const groupId = "63d6417e-1a03-4fd8-9439-295ce979";

// execute the extrinsic
const response = await sdk.rbac.disableGroup({
    groupId: groupId
});

// fetch group to confirm it has been changed to disabled
const fetchedGroup = await sdk.rbac.fetchGroup({
    owner: SUBSTRATE_ADDRESS,
    groupId: groupId,
});

// disconnect when finished
await sdk.disconnect();
```

```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.disableGroup({}) return object
{
  message: 'Successfully disable group 63d6417e-1a03-4fd8-9439-295ce979'
}
```

## assignRoleToGroup(groupId, roleId, seed)

Assigns a role to a group.

| Parameter | Type | EVM | Substrate | Description |
| - | - | - | - | - |
| **groupId** | `string` | Required | Required | The unique identifier of the group to which the role will be assigned. |
| **roleId** | `string` | Required | Required | ID of the role that will be assigned to the group. |
| **seed** | `string` | N/A | Optional | If not set at instance creation, allows user to define who sends the Substrate Transactions. |

### Assign Role to Group Code Examples

```javascript EVM Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const HTTPS_BASE_URL = "https://quicknode1.peaq.xyz";
const EVM_PRIVATE = process.env["EVM_PRIVATE"];

const sdk = await Sdk.createInstance({
    baseUrl: HTTPS_BASE_URL,
    chainType: Sdk.ChainType.EVM
});

// group id (generated with createGroup)
const groupId = "58a52684-3ace-4e72-a025-85085b1d";

// role id to assign to group
const roleId = "b68a5589-1284-49e9-8276-0359a429";

// build the evm tx
const tx = await sdk.rbac.assignRoleToGroup({
    groupId: groupId,
    roleId: roleId
});

// send using Sdk function
const receipt = await Sdk.sendEvmTx({
  tx: tx,
  baseUrl: HTTPS_BASE_URL,
  seed: EVM_PRIVATE
});
```

```javascript EVM Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.assignRoleToGroup({}) return object
{
  to: '0x0000000000000000000000000000000000000802',
  data: '0x03c4d7fb62363861353538392d313238342d343965392d383237362d303335396134323935386135323638342d336163652d346537322d613032352d3835303835623164'
}
```

```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const SUBSTRATE_SEED = process.env["SUBSTRATE_SEED"];

const sdk = await Sdk.createInstance({
    baseUrl: WSS_BASE_URL,
    chainType: Sdk.ChainType.SUBSTRATE,
    seed: SUBSTRATE_SEED
});

// group id (generated with createGroup)
const groupId = "63d6417e-1a03-4fd8-9439-295ce979";

// role id to assign to group
const roleId = "bc3f20d5-c519-4048-8db1-4bbf48dc";

// execute extrinsic
const response = await sdk.rbac.assignRoleToGroup({
    groupId: groupId,
    roleId: roleId
});

// disconnect when finished
await sdk.disconnect();
```

```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.assignRoleToGroup({}) return object
{
  message: 'Successfully assign role bc3f20d5-c519-4048-8db1-4bbf48dc to group 63d6417e-1a03-4fd8-9439-295ce979'
}
```

## fetchGroupRoles(owner, groupId, wssBaseUrl)

Fetches the roles assigned to a group.

| Parameter | Type | EVM | Substrate | Description |
| - | - | - | - | - |
| **owner** | `string` | Required | Required | Address of the owner of the roles. This is typically the account that manages the roles and permissions. |
| **groupId** | `string` | Required | Required | Unique identifier of the group for whom roles are to be fetched. |
| **wssBaseUrl** | `string` | Required | N/A | WSS URL used to query RBAC storage. |

### Fetch Group Roles Code Examples

```javascript EVM Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const HTTPS_BASE_URL = "https://quicknode1.peaq.xyz";
const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const EVM_ADDRESS = process.env["EVM_ADDRESS"];

const sdk = await Sdk.createInstance({
    baseUrl: HTTPS_BASE_URL,
    chainType: Sdk.ChainType.EVM
});

const groupId = "58a52684-3ace-4e72-a025-85085b1d";

const response = await sdk.rbac.fetchGroupRoles({
    owner: EVM_ADDRESS,
    groupId: groupId,
    wssBaseUrl: WSS_BASE_URL
});
```

```javascript EVM Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.fetchGroupRoles({}) return object
[
  {
    role: 'b68a5589-1284-49e9-8276-0359a429',
    group: '58a52684-3ace-4e72-a025-85085b1d'
  }
]
```

```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const SUBSTRATE_ADDRESS = process.env["SUBSTRATE_ADDRESS"];

const sdk = await Sdk.createInstance({
    baseUrl: WSS_BASE_URL,
    chainType: Sdk.ChainType.SUBSTRATE
});

const groupId = "63d6417e-1a03-4fd8-9439-295ce979";

const response = await sdk.rbac.fetchGroupRoles({
    owner: SUBSTRATE_ADDRESS,
    groupId: groupId
});

// disconnect when finished
await sdk.disconnect();
```

```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.fetchGroupRoles({}) return object
[
  {
    role: 'bc3f20d5-c519-4048-8db1-4bbf48dc',
    group: '63d6417e-1a03-4fd8-9439-295ce979'
  }
]
```

## unassignRoleToGroup(groupId, roleId, seed)

Unassigns a role from a group.

| Parameter | Type | EVM | Substrate | Description |
| - | - | - | - | - |
| **groupId** | `string` | Required | Required | ID of the group from which the role should be unassigned. |
| **roleId** | `string` | Required | Required | ID of the role that needs to be unassigned. |
| **seed** | `string` | N/A | Optional | If not set at instance creation, allows user to define who sends the Substrate Transactions. |

### Unassign Role to Group Code Examples

```javascript EVM Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const HTTPS_BASE_URL = "https://quicknode1.peaq.xyz";
const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const EVM_ADDRESS = process.env["EVM_ADDRESS"];
const EVM_PRIVATE = process.env["EVM_PRIVATE"];

const sdk = await Sdk.createInstance({
    baseUrl: HTTPS_BASE_URL,
    chainType: Sdk.ChainType.EVM
});

// previously created groupId
const groupId = "58a52684-3ace-4e72-a025-85085b1d";

// role id to unassign to group
const roleId = "b68a5589-1284-49e9-8276-0359a429";

// build the evm tx
const tx = await sdk.rbac.unassignRoleToGroup({
    groupId: groupId,
    roleId: roleId
});

// send using Sdk function
const receipt = await Sdk.sendEvmTx({
  tx: tx,
  baseUrl: HTTPS_BASE_URL,
  seed: EVM_PRIVATE
});

// fetch to see it has been unassigned
const response = await sdk.rbac.fetchGroupRoles({
    owner: EVM_ADDRESS,
    groupId: groupId,
    wssBaseUrl: WSS_BASE_URL
});
```

```javascript EVM Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.unassignRoleToGroup({}) return object
{
  to: '0x0000000000000000000000000000000000000802',
  data: '0xad58943762363861353538392d313238342d343965392d383237362d303335396134323935386135323638342d336163652d346537322d613032352d3835303835623164'
}
```

```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const SUBSTRATE_ADDRESS = process.env["SUBSTRATE_ADDRESS"];
const SUBSTRATE_SEED = process.env["SUBSTRATE_SEED"];

const sdk = await Sdk.createInstance({
    baseUrl: WSS_BASE_URL,
    chainType: Sdk.ChainType.SUBSTRATE,
    seed: SUBSTRATE_SEED
});

// previously created groupId
const groupId = "63d6417e-1a03-4fd8-9439-295ce979";

// role id to unassign to group
const roleId = "bc3f20d5-c519-4048-8db1-4bbf48dc";

// send extrinsic
const response = await sdk.rbac.unassignRoleToGroup({
    groupId: groupId,
    roleId: roleId
});

// fetch to see it has been unassigned (throws an error)
const fetchedUserGroups = await sdk.rbac.fetchGroupRoles({
    owner: SUBSTRATE_ADDRESS,
    groupId: groupId
});

// disconnect when finished
await sdk.disconnect();
```

```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.unassignRoleToGroup({}) return object
{
  message: 'Successfully unassign role: bc3f20d5-c519-4048-8db1-4bbf48dc from group: 63d6417e-1a03-4fd8-9439-295ce979'
}
```

## assignUserToGroup(userId, groupId, seed)

Assigns a user to a group.

| Parameter | Type | EVM | Substrate | Description |
| - | - | - | - | - |
| **userId** | `string` | Required | Required | The unique identifier of the user to whom the group will be assigned. ID created by user and must be 32 bytes. |
| **groupId** | `string` | Required | Required | ID of the group that will be assigned to the user. |
| **seed** | `string` | N/A | Optional | If not set at instance creation, allows user to define who sends the Substrate Transactions. |

### Assign User to Group Code Examples

```javascript EVM Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const HTTPS_BASE_URL = "https://quicknode1.peaq.xyz";
const EVM_PRIVATE = process.env["EVM_PRIVATE"];

const sdk = await Sdk.createInstance({
    baseUrl: HTTPS_BASE_URL,
    chainType: Sdk.ChainType.EVM
});

// user id to assign to group
const userId = "9e8c7866-8435-4b76-8683-709a03c9";

// group id (generated with createGroup)
const groupId = "58a52684-3ace-4e72-a025-85085b1d";

// build the evm tx
const tx = await sdk.rbac.assignUserToGroup({
    userId: userId,
    groupId: groupId,
});

// send using Sdk function
const receipt = await Sdk.sendEvmTx({
  tx: tx,
  baseUrl: HTTPS_BASE_URL,
  seed: EVM_PRIVATE
});
```

```javascript EVM Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.assignUserToGroup({}) return object
{
  to: '0x0000000000000000000000000000000000000802',
  data: '0xa76110c439653863373836362d383433352d346237362d383638332d373039613033633935386135323638342d336163652d346537322d613032352d3835303835623164'
}
```

```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const SUBSTRATE_SEED = process.env["SUBSTRATE_SEED"];

const sdk = await Sdk.createInstance({
    baseUrl: WSS_BASE_URL,
    chainType: Sdk.ChainType.SUBSTRATE,
    seed: SUBSTRATE_SEED
});

// user id to assign to group
const userId = "9e8c7866-8435-4b76-8683-709a03c9";

// group id (generated with createGroup)
const groupId = "63d6417e-1a03-4fd8-9439-295ce979";

// send the extrinsic
const response = await sdk.rbac.assignUserToGroup({
    userId: userId,
    groupId: groupId,
});

// disconnect when finished
await sdk.disconnect();
```

```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.assignUserToGroup({}) return object
{
  message: 'Successfully assign user 9e8c7866-8435-4b76-8683-709a03c9 to group 63d6417e-1a03-4fd8-9439-295ce979'
}
```

## fetchUserGroups(owner, userId, wssBaseUrl)

Fetches the groups a user belongs to.

| Parameter | Type | EVM | Substrate | Description |
| - | - | - | - | - |
| **owner** | `string` | Required | Required | Address of the owner of the roles. This is typically the account that manages the roles and permissions. |
| **userId** | `string` | Required | Required | Unique identifier of the user for whom groups are to be fetched. |
| **wssBaseUrl** | `string` | Required | N/A | WSS URL used to query RBAC storage. |

### Fetch User Groups Code Examples

```javascript EVM Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const HTTPS_BASE_URL = "https://quicknode1.peaq.xyz";
const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const EVM_ADDRESS = process.env["EVM_ADDRESS"];

const sdk = await Sdk.createInstance({
    baseUrl: HTTPS_BASE_URL,
    chainType: Sdk.ChainType.EVM
});

const userId = "9e8c7866-8435-4b76-8683-709a03c9";

const response = await sdk.rbac.fetchUserGroups({
    owner: EVM_ADDRESS,
    userId: userId,
    wssBaseUrl: WSS_BASE_URL
});
```

```javascript EVM Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.fetchUserGroups({}) return object
[
  {
    user: '9e8c7866-8435-4b76-8683-709a03c9',
    group: '58a52684-3ace-4e72-a025-85085b1d'
  }
]
```

```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const SUBSTRATE_ADDRESS = process.env["SUBSTRATE_ADDRESS"];

const sdk = await Sdk.createInstance({
    baseUrl: WSS_BASE_URL,
    chainType: Sdk.ChainType.SUBSTRATE
});

const userId = "9e8c7866-8435-4b76-8683-709a03c9";

const response = await sdk.rbac.fetchUserGroups({
    owner: SUBSTRATE_ADDRESS,
    userId: userId
});

// disconnect when finished
await sdk.disconnect();
```

```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.fetchUserGroups({}) return object
[
  {
    user: '9e8c7866-8435-4b76-8683-709a03c9',
    group: '63d6417e-1a03-4fd8-9439-295ce979'
  }
]
```

## unassignUserToGroup(userId, groupId, seed)

Unassigns a user from a group.

| Parameter | Type | EVM | Substrate | Description |
| - | - | - | - | - |
| **userId** | `string` | Required | Required | ID of the user from which the group should be unassigned. |
| **groupId** | `string` | Required | Required | ID of the group that needs to be unassigned. |
| **seed** | `string` | N/A | Optional | If not set at instance creation, allows user to define who sends the Substrate Transactions. |

### Unassign User to Group Code Examples

```javascript EVM Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const HTTPS_BASE_URL = "https://quicknode1.peaq.xyz";
const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const EVM_ADDRESS = process.env["EVM_ADDRESS"];
const EVM_PRIVATE = process.env["EVM_PRIVATE"];

const sdk = await Sdk.createInstance({
    baseUrl: HTTPS_BASE_URL,
    chainType: Sdk.ChainType.EVM
});

// user id to unassign from group
const userId = "9e8c7866-8435-4b76-8683-709a03c9";

// previously created groupId
const groupId = "58a52684-3ace-4e72-a025-85085b1d";

// build the evm tx
const tx = await sdk.rbac.unassignUserToGroup({
    userId: userId,
    groupId: groupId,
});

// send using Sdk function
const receipt = await Sdk.sendEvmTx({
  tx: tx,
  baseUrl: HTTPS_BASE_URL,
  seed: EVM_PRIVATE
});

// fetch to see it has been unassigned
const response = await sdk.rbac.fetchUserGroups({
    owner: EVM_ADDRESS,
    userId: userId,
    wssBaseUrl: WSS_BASE_URL
});
```

```javascript EVM Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.unassignUserToGroup({}) return object
{
  to: '0x0000000000000000000000000000000000000802',
  data: '0x0b9cc42839653863373836362d383433352d346237362d383638332d373039613033633935386135323638342d336163652d346537322d613032352d3835303835623164'
}
```

```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
import { Sdk } from "@peaq-network/sdk";

const WSS_BASE_URL = "wss://quicknode1.peaq.xyz";
const SUBSTRATE_ADDRESS = process.env["SUBSTRATE_ADDRESS"];
const SUBSTRATE_SEED = process.env["SUBSTRATE_SEED"];

const sdk = await Sdk.createInstance({
    baseUrl: WSS_BASE_URL,
    chainType: Sdk.ChainType.SUBSTRATE,
    seed: SUBSTRATE_SEED
});

// user id to unassign from group
const userId = "9e8c7866-8435-4b76-8683-709a03c9";

// previously created groupId
const groupId = "63d6417e-1a03-4fd8-9439-295ce979";

const response = await sdk.rbac.unassignUserToGroup({
    userId: userId,
    groupId: groupId,
});

// fetch to see it has been unassigned
const fetchedUserGroups = await sdk.rbac.fetchUserGroups({
    owner: SUBSTRATE_ADDRESS,
    userId: userId
});

// disconnect when finished
await sdk.disconnect();
```

```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
// sdk.rbac.unassignUserToGroup({}) return object
{
  message: 'Successfully unassign user: 9e8c7866-8435-4b76-8683-709a03c9 from group: 63d6417e-1a03-4fd8-9439-295ce979'
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.