Skip to content

Commit

Permalink
[add]: grant-permissions, grant-roles
Browse files Browse the repository at this point in the history
[edit]: register-roles, use-instructions, work-with-numeric-assets

Signed-off-by: Nurzhan Sakén <[email protected]>
  • Loading branch information
nxsaken committed Apr 11, 2024
1 parent 1551872 commit 9ebde90
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 38 deletions.
37 changes: 36 additions & 1 deletion src/cookbook/grant-permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,39 @@ head:

# How to Grant Permissions

TODO
Granting a permission to an account:

```rust
fn grant_permission_to_account(
iroha: &Client,
) {
// Alice will be given permission to unregister the kingdom domain
let grant_permission_to_unregister_kingdom = Grant::permission(
PermissionToken::new(
"CanUnregisterDomain".parse().unwrap(),
&json!({ "domain_id": "kingdom" }),
),
AccountId::from_str("alice@wonderland").unwrap()
);
iroha.submit(grant_permission_to_unregister_kingdom).unwrap();
}
```

Granting a permission to a role:

```rust
fn grant_permission_to_role(
iroha: &Client,
) {
// all accounts with the DOMAIN_DESTROYER role
// will be able to unregister the kingdom domain
let grant_permission_to_unregister_kingdom = Grant::role_permission(
PermissionToken::new(
"CanUnregisterDomain".parse().unwrap(),
&json!({ "domain_id": "kingdom" }),
),
RoleId::from_str("DOMAIN_DESTROYER").unwrap(),
);
iroha.submit(grant_permission_to_unregister_kingdom).unwrap();
}
```
14 changes: 13 additions & 1 deletion src/cookbook/grant-roles.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,16 @@ head:

# How to Grant a Role

TODO
Roles are granted to accounts:

```rust
fn grant_role(
iroha: &Client,
) {
let grant_role = Grant::role(
RoleId::from_str("DOMAIN_DESTROYER"),
AccountId::from_str("alice@wonderland").unwrap()
);
iroha.submit(grant_role).unwrap();
}
```
33 changes: 18 additions & 15 deletions src/cookbook/register-roles.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ The minimal case is an empty role (without any permission tokens):

```rust
fn register_new_role(
role_name: &str,
iroha_client: &Client
iroha: &Client
) {
let role_id = RoleId::from_str(role_name).unwrap();
let role_id = RoleId::from_str("MY_EMPTY_ROLE").unwrap();
let role = iroha_data_model::role::Role::new(role_id);
let register_role = Register::role(role);
iroha_client.submit(register_role).unwrap();
iroha.submit(register_role).unwrap();
}
```

Expand All @@ -33,18 +32,22 @@ see [Define Custom Permission Tokens](define-custom-permission-tokens.md).

```rust
fn register_new_role_with_permission(
role_name: &str,
domain_id: DomainId,
iroha_client: &Client
iroha: &Client,
) {
let role_id = RoleId::from_str(role_name).unwrap();
let can_unregister_domain = PermissionToken::new(
"CanUnregisterDomain".parse().unwrap(),
&json!({ "domain_id": domain_id }),
let roses_of_alice = AssetId::from_str("rose##alice@wonderland").unwrap();
let roses_of_mouse = AssetId::from_str("rose##mouse@wonderland").unwrap();
let can_burn_roses_of_alice = PermissionToken::new(
"CanBurnUserAsset".parse().unwrap(),
&json!({ "asset_id": roses_of_alice }),
);
let role = iroha_data_model::role::Role::new(role_id)
.add_permission(can_unregister_domain);
let register_role = Register::role(role);
iroha_client.submit(register_role).unwrap();
let can_burn_roses_of_mouse = PermissionToken::new(
"CanBurnUserAsset".parse().unwrap(),
&json!({ "asset_id": roses_of_mouse }),
);
let rose_burner = Role::new("ROSE_BURNER".parse().unwrap())
.add_permission(can_burn_roses_of_alice)
.add_permission(can_burn_roses_of_mouse);
let register_rose_burner = Register::role(rose_burner);
iroha.submit(register_rose_burner).unwrap();
}
```
8 changes: 4 additions & 4 deletions src/cookbook/use-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ Building and submitting an instruction:

```rust
fn use_instruction(
client: &Client,
roses: AssetDefinitionId,
alice: AccountId,
iroha: &Client,
) {
let roses = AssetDefinitionId::from_str("rose#wonderland").unwrap();
let alice = AccountId::from_str("alice@wonderland").unwrap();
// build an instruction
let mint_roses_for_alice = Mint::asset_numeric(
42_u32,
AssetId::new(roses, alice)
);
// submit the instruction
client.submit(mint_roses_for_alice).unwrap();
iroha.submit(mint_roses_for_alice).unwrap();
}
```
30 changes: 13 additions & 17 deletions src/cookbook/work-with-numeric-assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,44 @@ Minting roses for Alice:

```rust
fn mint_numeric_asset(
client: &Client,
roses: AssetDefinitionId,
alice: AccountId,
iroha: &Client,
) {
let mint_roses_for_alice = Mint::asset_numeric(
42_u32,
AssetId::new(roses, alice)
"rose##alice@wonderland".parse().unwrap()
);
client.submit(mint_roses_for_alice).unwrap();
iroha.submit(mint_roses_for_alice).unwrap();
}
```

Burning Alice's roses:

```rust
fn burn_numeric_asset(
client: &Client,
roses: AssetDefinitionId,
alice: AccountId,
iroha: &Client,
) {
let burn_roses_of_alice = Burn::asset_numeric(
8_u32,
AssetId::new(roses, alice)
8_u32,
AssetId::from_str("rose##alice@wonderland").unwrap()
);
client.submit(burn_roses_of_alice).unwrap();
iroha.submit(burn_roses_of_alice).unwrap();
}
```

Transferring Alice's roses to Mouse:

```rust
fn transfer_numeric_asset(
client: &Client,
roses: AssetDefinitionId,
alice: AccountId,
mouse: AccountId,
iroha: &Client,
) {
let roses = AssetDefinitionId::from_str("rose#wonderland").unwrap();
let alice = AccountId::from_str("alice@wonderland").unwrap();
let mouse = AccountId::from_str("mouse@wonderland").unwrap();
let transfer_roses_from_alice_to_mouse = Transfer::asset_numeric(
AssetId::new(roses, alice),
13_u32,
mouse
mouse,
);
client.submit(transfer_roses_from_alice_to_mouse).unwrap();
iroha.submit(transfer_roses_from_alice_to_mouse).unwrap();
}
```

0 comments on commit 9ebde90

Please sign in to comment.