Skip to content

Commit

Permalink
feature: added new instructions to transfer domain ownerhsip and revo…
Browse files Browse the repository at this point in the history
…ke permissions (#382)

* feature: added new instructions to transfer domain ownerhsip and revoke permissions

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

* feature: fix instruction name and added test for domain transfer

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 Oct 24, 2023
1 parent d412f74 commit 9777064
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,18 @@ object Instructions {
),
)

/**
* Transfer domain ownership.
*/
fun transferDomainOwnership(sourceId: AccountId, value: IdBox.DomainId, destinationId: AccountId) =
InstructionBox.Transfer(
TransferBox(
sourceId = IdBox.AccountId(sourceId).evaluatesTo(),
`object` = Value.Id(value).evaluatesTo(),
destinationId = IdBox.AccountId(destinationId).evaluatesTo(),
),
)

/**
* Evaluate one instruction if a [condition] is met and another one otherwise.
*/
Expand Down Expand Up @@ -480,6 +492,30 @@ object Instructions {
}
}

/**
* Revoke an account the [Permissions.CanSetKeyValueInUserAccount] permission
*/
fun revokeSetKeyValueAccount(accountId: AccountId, target: AccountId): InstructionBox {
return revokeSome(IdBox.AccountId(target)) {
PermissionToken(
definitionId = Permissions.CanSetKeyValueInUserAccount.type,
payload = accountId.asJsonString().asStringWithJson(),
)
}
}

/**
* Revoke an account the [Permissions.CanSetKeyValueInDomain] permission
*/
fun revokeSetKeyValueDomain(domainId: DomainId, target: AccountId): InstructionBox {
return revokeSome(IdBox.AccountId(target)) {
PermissionToken(
definitionId = Permissions.CanSetKeyValueInDomain.type,
payload = domainId.asJsonString().asStringWithJson(),
)
}
}

/**
* Revoke an account a given role.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import jp.co.soramitsu.iroha2.generated.AssetValue
import jp.co.soramitsu.iroha2.generated.AssetValueType
import jp.co.soramitsu.iroha2.generated.DomainId
import jp.co.soramitsu.iroha2.generated.Executable
import jp.co.soramitsu.iroha2.generated.IdBox
import jp.co.soramitsu.iroha2.generated.InstructionBox
import jp.co.soramitsu.iroha2.generated.IpfsPath
import jp.co.soramitsu.iroha2.generated.Metadata
Expand Down Expand Up @@ -393,6 +394,10 @@ class TransactionBuilder(builder: TransactionBuilder.() -> Unit = {}) {
instructions.value.add(Instructions.transferAsset(sourceId, value, destinationId))
}

fun transferDomainOwnership(sourceId: AccountId, value: IdBox.DomainId, destinationId: AccountId) = this.apply {
instructions.value.add(Instructions.transferDomainOwnership(sourceId, value, destinationId))
}

fun `if`(condition: Boolean, then: InstructionBox, otherwise: InstructionBox) = this.apply {
instructions.value.add(Instructions.`if`(condition, then, otherwise))
}
Expand All @@ -412,6 +417,12 @@ class TransactionBuilder(builder: TransactionBuilder.() -> Unit = {}) {
fun revokeSetKeyValueAsset(assetId: AssetId, target: AccountId) =
this.apply { instructions.value.add(Instructions.revokeSetKeyValueAsset(assetId, target)) }

fun revokeSetKeyValueAccount(accountId: AccountId, target: AccountId) =
this.apply { instructions.value.add(Instructions.revokeSetKeyValueAccount(accountId, target)) }

fun revokeSetKeyValueDomain(domainId: DomainId, target: AccountId) =
this.apply { instructions.value.add(Instructions.revokeSetKeyValueDomain(domainId, target)) }

fun revokeRole(
roleId: RoleId,
accountId: AccountId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import jp.co.soramitsu.iroha2.generated.AssetDefinitionId
import jp.co.soramitsu.iroha2.generated.AssetId
import jp.co.soramitsu.iroha2.generated.AssetValue
import jp.co.soramitsu.iroha2.generated.AssetValueType
import jp.co.soramitsu.iroha2.generated.IdBox
import jp.co.soramitsu.iroha2.generated.Metadata
import jp.co.soramitsu.iroha2.generated.Name
import jp.co.soramitsu.iroha2.generated.PermissionToken
Expand All @@ -35,6 +36,7 @@ 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
import jp.co.soramitsu.iroha2.testengine.DefaultGenesis
import jp.co.soramitsu.iroha2.testengine.GENESIS
import jp.co.soramitsu.iroha2.testengine.IROHA_CONFIG_DELIMITER
import jp.co.soramitsu.iroha2.testengine.IrohaTest
import jp.co.soramitsu.iroha2.testengine.NewAccountWithMetadata
Expand Down Expand Up @@ -1020,6 +1022,37 @@ class InstructionsTest : IrohaTest<Iroha2Client>() {
assertEquals(27, domains.size)
}

@Test
@WithIroha([DefaultGenesis::class])
@Feature("Domains")
@Story("Account transfers domain ownership")
@SdkTestId("transfer_domain_ownership")
fun `transfer domain ownership`(): Unit = runBlocking {
val genesisAccountId = AccountId(GENESIS.asName(), GENESIS.asDomainId())
var domain = QueryBuilder.findDomainById(DEFAULT_DOMAIN_ID)
.account(super.account)
.buildSigned(super.keyPair)
.let { query ->
client.sendQuery(query)
}
assertEquals(genesisAccountId, domain.ownedBy)

client.tx {
transferDomainOwnership(
genesisAccountId,
IdBox.DomainId(DEFAULT_DOMAIN_ID),
BOB_ACCOUNT_ID,
)
}
domain = QueryBuilder.findDomainById(DEFAULT_DOMAIN_ID)
.account(super.account)
.buildSigned(super.keyPair)
.let { query ->
client.sendQuery(query)
}
assertEquals(BOB_ACCOUNT_ID, domain.ownedBy)
}

private suspend fun registerAccount(id: AccountId, publicKey: PublicKey) {
client.sendTransaction {
account(super.account)
Expand Down

0 comments on commit 9777064

Please sign in to comment.