diff --git a/modules/client/src/main/kotlin/jp/co/soramitsu/iroha2/transaction/Instructions.kt b/modules/client/src/main/kotlin/jp/co/soramitsu/iroha2/transaction/Instructions.kt index d52723bf0..d10cbd53c 100644 --- a/modules/client/src/main/kotlin/jp/co/soramitsu/iroha2/transaction/Instructions.kt +++ b/modules/client/src/main/kotlin/jp/co/soramitsu/iroha2/transaction/Instructions.kt @@ -240,6 +240,11 @@ object Instructions { */ fun unregisterDomain(id: DomainId) = unregisterSome { IdBox.DomainId(id) } + /** + * Unregister a role + */ + fun unregisterRole(id: RoleId) = unregisterSome { IdBox.RoleId(id) } + /** * Register an asset */ @@ -434,6 +439,15 @@ object Instructions { target: AccountId, ) = grantSome(target, PermissionToken(permission.type, payload.asStringWithJson()).asValue()) + /** + * Grant an account the custom permission + */ + fun grantPermissionToken( + permission: String, + payload: String = "", + target: AccountId, + ) = grantSome(target, PermissionToken(permission.asName(), payload.asStringWithJson()).asValue()) + /** * Grant an account a given role. */ diff --git a/modules/client/src/main/kotlin/jp/co/soramitsu/iroha2/transaction/TransactionBuilder.kt b/modules/client/src/main/kotlin/jp/co/soramitsu/iroha2/transaction/TransactionBuilder.kt index 5f5d68128..3ecfd3154 100644 --- a/modules/client/src/main/kotlin/jp/co/soramitsu/iroha2/transaction/TransactionBuilder.kt +++ b/modules/client/src/main/kotlin/jp/co/soramitsu/iroha2/transaction/TransactionBuilder.kt @@ -249,6 +249,10 @@ class TransactionBuilder(builder: TransactionBuilder.() -> Unit = {}) { vararg tokens: PermissionToken, ) = this.apply { instructions.value.add(Instructions.registerRole(id, *tokens)) } + fun unregisterRole( + id: RoleId, + ) = this.apply { instructions.value.add(Instructions.unregisterRole(id)) } + @JvmOverloads fun registerAccount( id: AccountId, @@ -378,6 +382,10 @@ class TransactionBuilder(builder: TransactionBuilder.() -> Unit = {}) { instructions.value.add(Instructions.grantPermissionToken(permission, payload, target)) } + fun grantPermissionToken(permission: String, payload: String = "", target: AccountId) = this.apply { + instructions.value.add(Instructions.grantPermissionToken(permission, payload, target)) + } + fun burnAsset(assetId: AssetId, value: Int) = this.apply { instructions.value.add(Instructions.burnAsset(assetId, value)) } diff --git a/modules/client/src/test/kotlin/jp/co/soramitsu/iroha2/InstructionsTest.kt b/modules/client/src/test/kotlin/jp/co/soramitsu/iroha2/InstructionsTest.kt index 7c542ccce..6db07f904 100644 --- a/modules/client/src/test/kotlin/jp/co/soramitsu/iroha2/InstructionsTest.kt +++ b/modules/client/src/test/kotlin/jp/co/soramitsu/iroha2/InstructionsTest.kt @@ -37,6 +37,7 @@ import jp.co.soramitsu.iroha2.testengine.AliceHasRoleWithAccessToBobsMetadata import jp.co.soramitsu.iroha2.testengine.AliceWithTestAssets import jp.co.soramitsu.iroha2.testengine.BOB_ACCOUNT_ID import jp.co.soramitsu.iroha2.testengine.BOB_KEYPAIR +import jp.co.soramitsu.iroha2.testengine.BobCanUnregisterAnyRole import jp.co.soramitsu.iroha2.testengine.DEFAULT_ASSET_DEFINITION_ID import jp.co.soramitsu.iroha2.testengine.DEFAULT_ASSET_ID import jp.co.soramitsu.iroha2.testengine.DEFAULT_DOMAIN_ID @@ -76,6 +77,8 @@ import kotlin.test.assertEquals import kotlin.test.assertFails import kotlin.test.assertFailsWith import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertNull import kotlin.test.assertTrue @Owner("akostyuchenko") @@ -426,7 +429,7 @@ class InstructionsTest : IrohaTest() { client.tx(BOB_ACCOUNT_ID, BOB_KEYPAIR) { registerDomain(domainId) grantPermissionToken( - Permissions.CanSetKeyValueInDomain, + Permissions.CanSetKeyValueInDomain.type.string, domainId.asJsonString(), ALICE_ACCOUNT_ID, ) @@ -1002,7 +1005,7 @@ class InstructionsTest : IrohaTest() { } @Test - @WithIroha([DefaultGenesis::class]) + @WithIroha([BobCanUnregisterAnyRole::class]) @Feature("Assets") @Story("Account registers an asset definition") @SdkTestId("register_asset_definition_with_store_value_type") @@ -1062,6 +1065,22 @@ class InstructionsTest : IrohaTest() { roles.isEmpty(), ) } + QueryBuilder.findAllRoles() + .account(BOB_ACCOUNT_ID) + .buildSigned(BOB_KEYPAIR) + .let { query -> client.sendQuery(query) } + .firstOrNull { it.id == roleId } + .also { assertNotNull(it) } + + client.tx(BOB_ACCOUNT_ID, BOB_KEYPAIR) { + unregisterRole(roleId) + } + QueryBuilder.findAllRoles() + .account(BOB_ACCOUNT_ID) + .buildSigned(BOB_KEYPAIR) + .let { query -> client.sendQuery(query) } + .firstOrNull { it.id == roleId } + .also { assertNull(it) } } @Test diff --git a/modules/client/src/test/kotlin/jp/co/soramitsu/iroha2/testengine/Genesis.kt b/modules/client/src/test/kotlin/jp/co/soramitsu/iroha2/testengine/Genesis.kt index bc42aebd2..373e63e6d 100644 --- a/modules/client/src/test/kotlin/jp/co/soramitsu/iroha2/testengine/Genesis.kt +++ b/modules/client/src/test/kotlin/jp/co/soramitsu/iroha2/testengine/Genesis.kt @@ -409,6 +409,18 @@ open class FatGenesis : Genesis( } } +/** + * Grant permission token to unregister any role + */ +open class BobCanUnregisterAnyRole : Genesis( + rawGenesisBlock( + Instructions.grantPermissionToken( + permission = Permissions.CanUnregisterAnyRole.type.string, + target = BOB_ACCOUNT_ID, + ), + ), +) + /** * Return [RawGenesisBlock] with instructions to init genesis block */