Skip to content

Commit

Permalink
Added demonstration for event based cl approach
Browse files Browse the repository at this point in the history
Signed-off-by: artem.ivanov <[email protected]>
  • Loading branch information
Artemkaaas committed Dec 4, 2023
1 parent dca3cdc commit dbdcf18
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 53 deletions.
43 changes: 22 additions & 21 deletions indy-besu/network/config/besu/genesis.json

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion indy-besu/smart_contracts/contracts-ts/SchemaRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ export class SchemaRegistry extends Contract {

public async createSchema(data: Schema) {
const tx = await this.instance.createSchema(data)
return tx.wait()
const result = await tx.wait()
console.log(result)
return result
}

public async createSchemaAsJson(data: Schema) {
const tx = await this.instance.createSchemaAsJson(data.id, JSON.stringify(data))
const result = await tx.wait()
console.log(result)
return result
}

public async resolveSchema(id: string): Promise<SchemaWithMetadataStruct> {
Expand Down
6 changes: 6 additions & 0 deletions indy-besu/smart_contracts/contracts/cl/SchemaRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ contract SchemaRegistry is SchemaRegistryInterface, ControlledUpgradeable, CLReg
* Mapping Schema ID to its Schema Details and Metadata.
*/
mapping(string id => SchemaWithMetadata schemaWithMetadata) private _schemas;
mapping(bytes32 id => bool exists) private _schemasAsJson;

/**
* Checks the uniqueness of the Schema ID
Expand Down Expand Up @@ -59,6 +60,11 @@ contract SchemaRegistry is SchemaRegistryInterface, ControlledUpgradeable, CLReg
emit SchemaCreated(schema.id, msg.sender);
}

function createSchemaAsJson(string calldata id, string calldata schema) public virtual {
_schemasAsJson[keccak256(abi.encodePacked(id))] = true;
emit SchemaStringCreated(keccak256(abi.encodePacked(id)), schema);
}

/// @inheritdoc SchemaRegistryInterface
function resolveSchema(
string calldata id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface SchemaRegistryInterface {
*/
event SchemaCreated(string schemaId, address indexed sender);

event SchemaStringCreated(bytes32 indexed schemaId, string schema);

/**
* @dev Creates a new Schema.
*
Expand All @@ -30,6 +32,8 @@ interface SchemaRegistryInterface {
*/
function createSchema(Schema calldata schema) external;

function createSchemaAsJson(string calldata id, string calldata schema) external;

/**
* @dev Resolve the Schema associated with the given ID.
*
Expand Down
51 changes: 51 additions & 0 deletions indy-besu/smart_contracts/demos/events-approach.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Web3 from 'web3'
import environment, { host } from '../environment'
import { Actor } from './utils/actor'
import { ROLES } from '../contracts-ts'
import { createSchemaObject } from '../utils'

async function demo() {
const web3 = new Web3(new Web3.providers.HttpProvider(host))

let receipt: any

const trustee = await new Actor(environment.accounts.account1).init()
const faber = await new Actor().init()

console.log('1. Trustee assign ENDORSER role to Faber')
receipt = await trustee.roleControl.assignRole(ROLES.ENDORSER, faber.address)
console.log(`Role ${ROLES.ENDORSER} assigned to account ${faber.address}. Receipt: ${JSON.stringify(receipt)}`)

console.log('2. Faber creates DID Document')
receipt = await trustee.didRegistry.createDid(faber.didDocument)
console.log(`Did Document created for DID ${faber.did}. Receipt: ${JSON.stringify(receipt)}`)

console.log('3. Faber creates Test Schema')
const schema = createSchemaObject({ issuerId: faber.did })
receipt = await faber.schemaRegistry.createSchemaAsJson(schema)
console.log(`Schema created for id ${schema.id}. Receipt: ${JSON.stringify(receipt)}`)

const eventsByType = await faber.schemaRegistry.instance.queryFilter('SchemaStringCreated')
console.log(`Events by type using Ethers ${JSON.stringify(eventsByType, null, 2)}`)
console.log(JSON.stringify(eventsByType, null, 2))

const filter = await faber.schemaRegistry.instance.filters.SchemaStringCreated(web3.utils.keccak256(schema.id))
const eventsUsingEthers = await faber.schemaRegistry.instance.queryFilter(filter)
console.log(`Events using Ethers ${JSON.stringify(eventsUsingEthers, null, 2)}`)

let eventsUsingWeb3 = await web3.eth.getPastLogs({
address: '0x0000000000000000000000000000000000005555',
topics: [
null, // same as: web3.utils.sha3("SchemaStringCreated(uint,uint)"),
web3.utils.keccak256(schema.id),
],
})
console.log(`Events using Web3: ${JSON.stringify(eventsUsingWeb3, null, 2)}`)
console.log(eventsUsingWeb3)
}

if (require.main === module) {
demo()
}

module.exports = exports = demo
1 change: 1 addition & 0 deletions indy-besu/smart_contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"demo/flow": "yarn run-on-besu demos/flow.ts",
"demo/account": "yarn run-on-besu demos/account-control.ts",
"demo/upgrade": "yarn run-on-besu demos/upgrade-control.ts",
"demo/events-approach": "yarn run-on-besu demos/events-approach.ts",
"genesis/generate": "ts-node scripts/genesis/generate.ts",
"postinstall": "cd ../../ && husky install indy-besu/smart_contracts/.husky",
"solc-compile": "solc -o compiled-contracts --optimize --bin-runtime --evm-version=constantinople @dk1a=$(pwd)/node_modules/@dk1a @openzeppelin=$(pwd)/node_modules/@openzeppelin contracts/**/*.sol node_modules/@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"
Expand Down
37 changes: 9 additions & 28 deletions indy-besu/smart_contracts/test/utils/contract-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { HardhatEthersSigner } from '@nomicfoundation/hardhat-ethers/signers'
import { ethers } from 'hardhat'
import {
CredentialDefinitionRegistry,
DidRegistry,
Expand All @@ -6,8 +8,8 @@ import {
UpgradeControl,
ValidatorControl,
} from '../../contracts-ts'
import { Contract, createBaseDidDocument, createSchemaObject } from '../../utils'
import { getTestAccounts, ZERO_ADDRESS } from './test-entities'
import { Contract } from '../../utils'
import { ZERO_ADDRESS } from './test-entities'

export class DidRegex extends testableContractMixin(Contract) {
constructor() {
Expand All @@ -34,49 +36,28 @@ export const TestableRoleControl = testableContractMixin(RoleControl)
export const TestableValidatorControl = testableContractMixin(ValidatorControl)
export const TestableUpgradeControl = testableContractMixin(UpgradeControl)

export async function deployRoleControl() {
const roleControl = await new RoleControl().deployProxy({ params: [ZERO_ADDRESS] })
const testAccounts = await getTestAccounts(roleControl)

return { roleControl, testAccounts }
}

export async function deployDidRegistry() {
const { testAccounts } = await deployRoleControl()

const didRegex = await new DidRegex().deploy()
const didValidator = await new DidValidator().deploy({ libraries: [didRegex] })
const didRegistry = await new TestableDidRegistry().deployProxy({ params: [ZERO_ADDRESS], libraries: [didValidator] })

return { didRegistry, didValidator, didRegex, testAccounts }
return { didRegistry, didValidator, didRegex }
}

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

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

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

return { credentialDefinitionRegistry, didRegistry, schemaRegistry, testAccounts }
}

export async function createDid(didRegistry: DidRegistry, did: string) {
const didDocument = createBaseDidDocument(did)
await didRegistry.createDid(didDocument)
return didDocument
}

export async function createSchema(schemaRegistry: SchemaRegistry, issuerId: string) {
const schema = createSchemaObject({ issuerId })
await schemaRegistry.createSchema(schema)
return schema
return { credentialDefinitionRegistry, didRegistry, schemaRegistry }
}

function testableContractMixin<T extends new (...args: any[]) => Contract>(Base: T) {
Expand Down
3 changes: 1 addition & 2 deletions indy-besu/smart_contracts/test/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export namespace ClErrors {
export const FieldRequired = 'FieldRequired'
export const IssuerNotFound = 'IssuerNotFound'
export const IssuerHasBeenDeactivated = 'IssuerHasBeenDeactivated'
export const SenderIsNotIssuerDidOwner = 'SenderIsNotIssuerDidOwner'

// Schema errors
export const InvalidSchemaId = 'InvalidSchemaId'
Expand All @@ -15,7 +14,7 @@ export namespace ClErrors {

// CredDef errors
export const InvalidCredentialDefinitionId = 'InvalidCredentialDefinitionId'
export const UnsupportedCredentialDefinitionType = 'UnsupportedCredentialDefinitionType'
export const UnsupportedCredentialDefintionType = 'UnsupportedCredentialDefintionType'
export const CredentialDefinitionAlreadyExist = 'CredentialDefinitionAlreadyExist'
export const CredentialDefinitionNotFound = 'CredentialDefinitionNotFound'
}
Expand Down
2 changes: 1 addition & 1 deletion indy-besu/smart_contracts/utils/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class Contract {

protected readonly name: string
protected readonly signer?: Signer
protected instance: any
public instance: any

constructor(name: string, sender?: any) {
this.name = name
Expand Down

0 comments on commit dbdcf18

Please sign in to comment.