Skip to content

Commit

Permalink
feature: unregister role and grant permission token by custom name (#404
Browse files Browse the repository at this point in the history
)

* feature: unregister role and grant permission token by custom name

Signed-off-by: Timur Guskov <[email protected]>

* feature: make payload optional

Signed-off-by: Timur Guskov <[email protected]>

* feature: test optional payload

Signed-off-by: Timur Guskov <[email protected]>

---------

Signed-off-by: Timur Guskov <[email protected]>
Co-authored-by: Timur Guskov <[email protected]>
  • Loading branch information
gv-timur and gv-timur authored Feb 1, 2024
1 parent b3c7745 commit 410986c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -426,7 +429,7 @@ class InstructionsTest : IrohaTest<Iroha2Client>() {
client.tx(BOB_ACCOUNT_ID, BOB_KEYPAIR) {
registerDomain(domainId)
grantPermissionToken(
Permissions.CanSetKeyValueInDomain,
Permissions.CanSetKeyValueInDomain.type.string,
domainId.asJsonString(),
ALICE_ACCOUNT_ID,
)
Expand Down Expand Up @@ -1002,7 +1005,7 @@ class InstructionsTest : IrohaTest<Iroha2Client>() {
}

@Test
@WithIroha([DefaultGenesis::class])
@WithIroha([BobCanUnregisterAnyRole::class])
@Feature("Assets")
@Story("Account registers an asset definition")
@SdkTestId("register_asset_definition_with_store_value_type")
Expand Down Expand Up @@ -1062,6 +1065,22 @@ class InstructionsTest : IrohaTest<Iroha2Client>() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 410986c

Please sign in to comment.