Skip to content

Commit

Permalink
Add test for creating schema and credDef using did:ethr method
Browse files Browse the repository at this point in the history
Signed-off-by: aziz.karabashov <[email protected]>
  • Loading branch information
akarabashov committed Dec 6, 2023
1 parent 5162119 commit a15f6c7
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 14 deletions.
9 changes: 4 additions & 5 deletions indy-besu/smart_contracts/contracts/cl/CLRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import { DidNotFound } from "../did/DidErrors.sol";
import { DidNotFound, IncorrectDid } from "../did/DidErrors.sol";
import { DidMetadata } from "../did/DidTypes.sol";
import { UniversalDidResolverInterface } from "../did/UniversalDidResolverInterface.sol";
import { Errors } from "../utils/Errors.sol";
import { IssuerHasBeenDeactivated, IssuerNotFound, SenderIsNotIssuerDidOwner } from "./ClErrors.sol";
import { InvalidIssuerId, IssuerHasBeenDeactivated, IssuerNotFound, SenderIsNotIssuerDidOwner } from "./ClErrors.sol";

contract CLRegistry {
/**
Expand All @@ -24,9 +24,8 @@ contract CLRegistry {
}
if (metadata.deactivated) revert IssuerHasBeenDeactivated(id);
} catch (bytes memory reason) {
if (Errors.equals(reason, DidNotFound.selector)) {
revert IssuerNotFound(id);
}
if (Errors.equals(reason, DidNotFound.selector)) revert IssuerNotFound(id);
if (Errors.equals(reason, IncorrectDid.selector)) revert InvalidIssuerId(id);

Errors.rethrow(reason);
}
Expand Down
6 changes: 6 additions & 0 deletions indy-besu/smart_contracts/contracts/cl/ClErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ error FieldRequired(string name);
*/
error IssuerNotFound(string id);

/**
* @notice Error that occurs when the provided issuer ID is invalid.
* @param id Issuer ID.
*/
error InvalidIssuerId(string id);

/**
* @notice Error that occurs when attempting to perform an operation on a deactivated issuer.
* @param id Issuer ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ contract UniversalDidResolver is UniversalDidResolverInterface, ControlledUpgrad

bytes memory bytesArray = new bytes(20);
for (uint256 i = 0; i < 20; i++) {
(uint8 firstByte, bool firstByteValid) = hexCharToByte(hexString, 2 * i);
(uint8 firstByte, bool firstByteValid) = _hexCharToByte(hexString, 2 * i);
if (!firstByteValid) return address(0);

(uint8 secondByte, bool secondByteValid) = hexCharToByte(hexString, 2 * i + 1);
(uint8 secondByte, bool secondByteValid) = _hexCharToByte(hexString, 2 * i + 1);
if (!secondByteValid) return address(0);

bytesArray[i] = bytes1(firstByte * 16 + secondByte);
}
return address(bytes20(bytesArray));
}

function hexCharToByte(string memory s, uint256 index) internal pure returns (uint8, bool) {
function _hexCharToByte(string memory s, uint256 index) internal pure returns (uint8, bool) {
bytes1 hexChar = bytes(s)[index];
if (hexChar >= 0x30 && hexChar <= 0x39) {
// ascii 0-9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,33 @@ describe('CredentialDefinitionRegistry', function () {
// .withArgs(credDef.id)
// })
})

describe('Add/Resolve Credential Definition with did:ethr Issuer', function () {
it('Should create and resolve Credential Definition', async function () {
const ethrIssuerId = `did:ethr:${testAccounts.trustee.account.address.substring(2)}`
const credDef = createCredentialDefinitionObject({ issuerId: ethrIssuerId, schemaId })

await credentialDefinitionRegistry.createCredentialDefinition(credDef)
const result = await credentialDefinitionRegistry.resolveCredentialDefinition(credDef.id)

expect(result.credDef).to.be.deep.equal(credDef)
})

it('Should fail if Credential Definition is being created with not owned Issuer DID', async function () {
const ethrIssuerId = `did:ethr:${testAccounts.trustee2.account.address.substring(2)}`
const credDef = createCredentialDefinitionObject({ issuerId: ethrIssuerId, schemaId })

await expect(credentialDefinitionRegistry.createCredentialDefinition(credDef))
.to.be.revertedWithCustomError(credentialDefinitionRegistry.baseInstance, ClErrors.SenderIsNotIssuerDidOwner)
.withArgs(testAccounts.trustee.account.address, testAccounts.trustee2.account.address)
})

it('Should fail if Credential Definition is being created with invalid Issuer ID', async function () {
const credDef = createCredentialDefinitionObject({ issuerId: 'did:ethr:ab$ddfgh354345', schemaId })

await expect(credentialDefinitionRegistry.createCredentialDefinition(credDef))
.to.be.revertedWithCustomError(schemaRegistry.baseInstance, ClErrors.InvalidIssuerId)
.withArgs(credDef.issuerId)
})
})
})
31 changes: 31 additions & 0 deletions indy-besu/smart_contracts/test/cl/SchemaRegistry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,35 @@ describe('SchemaRegistry', function () {
.withArgs(testAccounts.trustee2.account.address, testAccounts.trustee.account.address)
})
})

describe('Add/Resolve Schema with did:ethr Issuer', function () {
it('Should create and resolve Schema', async function () {
const ethrIssuerId = `did:ethr:${testAccounts.trustee.account.address.substring(2)}`

const schema = createSchemaObject({ issuerId: ethrIssuerId })

await schemaRegistry.createSchema(schema)
const result = await schemaRegistry.resolveSchema(schema.id)

expect(result.schema).to.be.deep.equal(schema)
})

it('Should fail if Schema is being created with not owned Issuer DID', async function () {
const ethrIssuerId = `did:ethr:${testAccounts.trustee2.account.address.substring(2)}`

const schema = createSchemaObject({ issuerId: ethrIssuerId })

await expect(schemaRegistry.createSchema(schema))
.to.be.revertedWithCustomError(schemaRegistry.baseInstance, ClErrors.SenderIsNotIssuerDidOwner)
.withArgs(testAccounts.trustee.account.address, testAccounts.trustee2.account.address)
})

it('Should fail if Schema is being created with invalid Issuer ID', async function () {
const schema = createSchemaObject({ issuerId: 'did:ethr:ab$ddfgh354345' })

await expect(schemaRegistry.createSchema(schema))
.to.be.revertedWithCustomError(schemaRegistry.baseInstance, ClErrors.InvalidIssuerId)
.withArgs(schema.issuerId)
})
})
})
14 changes: 8 additions & 6 deletions indy-besu/smart_contracts/test/utils/contract-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,21 @@ export async function deployUniversalDidResolver() {
}

export async function deploySchemaRegistry() {
const { didRegistry, testAccounts } = await deployDidRegistry()
const schemaRegistry = await new TestableSchemaRegistry().deployProxy({ params: [didRegistry.address, ZERO_ADDRESS] })
const { universalDidReolver, didRegistry, testAccounts } = await deployUniversalDidResolver()
const schemaRegistry = await new TestableSchemaRegistry().deployProxy({
params: [universalDidReolver.address, ZERO_ADDRESS],
})

return { didRegistry, schemaRegistry, testAccounts }
return { universalDidReolver, didRegistry, schemaRegistry, testAccounts }
}

export async function deployCredentialDefinitionRegistry() {
const { didRegistry, schemaRegistry, testAccounts } = await deploySchemaRegistry()
const { universalDidReolver, didRegistry, schemaRegistry, testAccounts } = await deploySchemaRegistry()
const credentialDefinitionRegistry = await new TestableCredentialDefinitionRegistry().deployProxy({
params: [didRegistry.address, schemaRegistry.address, ZERO_ADDRESS],
params: [universalDidReolver.address, schemaRegistry.address, ZERO_ADDRESS],
})

return { credentialDefinitionRegistry, didRegistry, schemaRegistry, testAccounts }
return { credentialDefinitionRegistry, universalDidReolver, didRegistry, schemaRegistry, testAccounts }
}

export async function createDid(didRegistry: DidRegistry, did: string) {
Expand Down
1 change: 1 addition & 0 deletions indy-besu/smart_contracts/test/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export namespace ClErrors {
export const FieldRequired = 'FieldRequired'
export const IssuerNotFound = 'IssuerNotFound'
export const IssuerHasBeenDeactivated = 'IssuerHasBeenDeactivated'
export const InvalidIssuerId = 'InvalidIssuerId'
export const SenderIsNotIssuerDidOwner = 'SenderIsNotIssuerDidOwner'

// Schema errors
Expand Down

0 comments on commit a15f6c7

Please sign in to comment.