From 26c024784d05930dfbccdd0e8c27d6860d774643 Mon Sep 17 00:00:00 2001 From: Daniil Polyakov Date: Wed, 1 Mar 2023 17:29:17 +0300 Subject: [PATCH] [feature] #3271: Add `asset_definition/transfer` validator Signed-off-by: Daniil Polyakov --- permission_validators/runtime/Cargo.toml | 1 + .../asset_definition/transfer/Cargo.toml | 13 +++++++ .../asset_definition/transfer/src/lib.rs | 39 +++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 permission_validators/runtime/asset_definition/transfer/Cargo.toml create mode 100644 permission_validators/runtime/asset_definition/transfer/src/lib.rs diff --git a/permission_validators/runtime/Cargo.toml b/permission_validators/runtime/Cargo.toml index e760e9c3335..562ff4c4b8a 100644 --- a/permission_validators/runtime/Cargo.toml +++ b/permission_validators/runtime/Cargo.toml @@ -10,6 +10,7 @@ members = [ "asset_definition/set_key_value", "asset_definition/remove_key_value", "asset_definition/unregister", + "asset_definition/transfer", "account/set_key_value", "account/remove_key_value", ] diff --git a/permission_validators/runtime/asset_definition/transfer/Cargo.toml b/permission_validators/runtime/asset_definition/transfer/Cargo.toml new file mode 100644 index 00000000000..6cf2b47b572 --- /dev/null +++ b/permission_validators/runtime/asset_definition/transfer/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "iroha_asset_definition_transfer_validator" +version.workspace = true +authors.workspace = true +edition.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +crate-type = ['rlib'] + +[dependencies] +iroha_wasm.workspace = true diff --git a/permission_validators/runtime/asset_definition/transfer/src/lib.rs b/permission_validators/runtime/asset_definition/transfer/src/lib.rs new file mode 100644 index 00000000000..d204c4721e3 --- /dev/null +++ b/permission_validators/runtime/asset_definition/transfer/src/lib.rs @@ -0,0 +1,39 @@ +//! Validator that checks [`Transfer`] instruction related to asset definitions + +#![no_std] +#![no_main] + +extern crate alloc; + +use iroha_wasm::validator::{prelude::*, utils}; + +/// Validate [`Transfer`] instruction +/// +/// # [`Transfer`] +/// +/// ## Pass +/// +/// - [`Transfer`] `source_id` is not an [`AssetDefinitionId`]; +/// - `authority` is an asset definition owner; +/// +/// ## Deny +/// +/// If none of the `Pass` conditions are met. +pub fn validate(authority: ::Id, instruction: Instruction) -> Verdict { + let Instruction::Transfer(transfer) = instruction else { + pass!(); + }; + + let IdBox::AssetDefinitionId(asset_definition_id) = transfer.source_id + .evaluate_on_host() + .dbg_expect("Failed to evaluate `Transfer` source id") else { + pass!(); + }; + + pass_if!(utils::is_asset_definition_owner( + &asset_definition_id, + &authority + )); + + deny!("Can't transfer asset definition of another account") +}