From 9ebde90bebcebbabf0b09a73c0aab849254527e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nurzhan=20Sak=C3=A9n?= Date: Thu, 11 Apr 2024 10:58:19 +0400 Subject: [PATCH] [add]: grant-permissions, grant-roles [edit]: register-roles, use-instructions, work-with-numeric-assets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nurzhan Sakén --- src/cookbook/grant-permissions.md | 37 +++++++++++++++++++++++- src/cookbook/grant-roles.md | 14 ++++++++- src/cookbook/register-roles.md | 33 +++++++++++---------- src/cookbook/use-instructions.md | 8 ++--- src/cookbook/work-with-numeric-assets.md | 30 +++++++++---------- 5 files changed, 84 insertions(+), 38 deletions(-) diff --git a/src/cookbook/grant-permissions.md b/src/cookbook/grant-permissions.md index 7876a68f0..13d9e1a38 100644 --- a/src/cookbook/grant-permissions.md +++ b/src/cookbook/grant-permissions.md @@ -11,4 +11,39 @@ head: # How to Grant Permissions -TODO \ No newline at end of file +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(); +} +``` diff --git a/src/cookbook/grant-roles.md b/src/cookbook/grant-roles.md index 4c32c1201..0d1bc4ab1 100644 --- a/src/cookbook/grant-roles.md +++ b/src/cookbook/grant-roles.md @@ -11,4 +11,16 @@ head: # How to Grant a Role -TODO \ No newline at end of file +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(); +} +``` diff --git a/src/cookbook/register-roles.md b/src/cookbook/register-roles.md index b473bf292..f25cf627b 100644 --- a/src/cookbook/register-roles.md +++ b/src/cookbook/register-roles.md @@ -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(); } ``` @@ -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(); } ``` diff --git a/src/cookbook/use-instructions.md b/src/cookbook/use-instructions.md index ed0832ffd..1518868fc 100644 --- a/src/cookbook/use-instructions.md +++ b/src/cookbook/use-instructions.md @@ -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(); } ``` \ No newline at end of file diff --git a/src/cookbook/work-with-numeric-assets.md b/src/cookbook/work-with-numeric-assets.md index 37a264aae..a208f7aa4 100644 --- a/src/cookbook/work-with-numeric-assets.md +++ b/src/cookbook/work-with-numeric-assets.md @@ -15,15 +15,13 @@ 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(); } ``` @@ -31,15 +29,13 @@ 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(); } ``` @@ -47,16 +43,16 @@ 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(); } ``` \ No newline at end of file