forked from hyperledger/indy-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added demonstration for CL registry based on event based approach
Signed-off-by: artem.ivanov <[email protected]>
- Loading branch information
1 parent
dca3cdc
commit b4e2f2c
Showing
14 changed files
with
196 additions
and
40 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
indy-besu/smart_contracts/contracts-ts/EthereumCLRegistry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Contract } from '../utils/contract' | ||
|
||
export class EthereumCLRegistry extends Contract { | ||
public static readonly defaultAddress = '0x0000000000000000000000000000000000111111' | ||
|
||
constructor(sender?: any) { | ||
super(EthereumCLRegistry.name, sender) | ||
} | ||
|
||
public async createResource(id: string, resource: string) { | ||
const tx = await this.instance.createResource(id, resource) | ||
return tx.wait() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
indy-besu/smart_contracts/contracts/cl/EthereumCLRegistry.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.20; | ||
|
||
import { ControlledUpgradeable } from "../upgrade/ControlledUpgradeable.sol"; | ||
|
||
import { EthereumCLRegistryInterface } from "./EthereumCLRegistryInterface.sol"; | ||
import { ResourceAlreadyExist } from "./ClErrors.sol"; | ||
|
||
contract EthereumCLRegistry is EthereumCLRegistryInterface, ControlledUpgradeable { | ||
/** | ||
* Mapping to track created created resources by their id. | ||
*/ | ||
mapping(string id => bool exists) private _resources; | ||
|
||
/** | ||
* Checks the uniqueness of the resource ID | ||
*/ | ||
modifier _uniqueResourceId(string memory id) { | ||
if (_resources[id] == true) revert ResourceAlreadyExist(id); | ||
_; | ||
} | ||
|
||
function initialize(address upgradeControlAddress) public reinitializer(1) { | ||
_initializeUpgradeControl(upgradeControlAddress); | ||
} | ||
|
||
/// @inheritdoc EthereumCLRegistryInterface | ||
function createResource(string calldata id, string calldata resource) public virtual _uniqueResourceId(id) { | ||
bytes32 resourceIdHash = keccak256(abi.encodePacked(id)); | ||
_resources[id] = true; | ||
emit EthereumCLResourceCreated(resourceIdHash, resource); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
indy-besu/smart_contracts/contracts/cl/EthereumCLRegistryInterface.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.20; | ||
|
||
interface EthereumCLRegistryInterface { | ||
/** | ||
* @dev Event that is sent when a new CL resource created | ||
* | ||
* @param id Hash of resource identifier | ||
* @param resource Created resource as JSON string | ||
*/ | ||
event EthereumCLResourceCreated(bytes32 indexed id, string resource); | ||
|
||
/** | ||
* @dev Creates a new CL resource using event based approach. | ||
* | ||
* @param id Identifier of the resource. | ||
* @param resource The new AnonCreds resource as JSON string. | ||
*/ | ||
function createResource(string calldata id, string calldata resource) external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import Web3 from 'web3' | ||
import environment, { host } from '../environment' | ||
import { Actor } from './utils/actor' | ||
import { ROLES } from '../contracts-ts' | ||
import { createSchemaObject } from '../utils' | ||
import { EthereumCLRegistry } from '../contracts-ts/EthereumCLRegistry' | ||
|
||
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 Test Schema') | ||
const schema = createSchemaObject({ issuerId: faber.did }) | ||
receipt = await faber.ethereumCLRegistry.createResource(schema.id, JSON.stringify(schema)) | ||
console.log(`Schema created for id ${schema.id}. Receipt: ${JSON.stringify(receipt)}`) | ||
|
||
console.log('3. Faber resolves Test Schema') | ||
const eventsByType = await faber.ethereumCLRegistry.instance.queryFilter('EthereumCLResourceCreated') | ||
console.log(`Resolve schema using events by type and Ethers ${JSON.stringify(eventsByType, null, 2)}`) | ||
|
||
const filter = await faber.ethereumCLRegistry.instance.filters.EthereumCLResourceCreated( | ||
web3.utils.keccak256(schema.id), | ||
) | ||
const eventsUsingEthers = await faber.ethereumCLRegistry.instance.queryFilter(filter) | ||
console.log(`Resolve schema using events and Ethers: ${JSON.stringify(eventsUsingEthers, null, 2)}`) | ||
const resolvedSchema = web3.utils.hexToAscii(eventsUsingEthers[0].data) | ||
console.log(`Schema JSON: ${resolvedSchema}`) | ||
|
||
let eventsUsingWeb3 = await web3.eth.getPastLogs({ | ||
address: EthereumCLRegistry.defaultAddress, | ||
topics: [ | ||
null, // same as: web3.utils.sha3("SchemaStringCreated(uint,uint)"), | ||
web3.utils.keccak256(schema.id), | ||
], | ||
}) | ||
console.log(`Resolve schema using events and Web3: ${JSON.stringify(eventsUsingWeb3, null, 2)}`) | ||
console.log(eventsUsingWeb3) | ||
} | ||
|
||
if (require.main === module) { | ||
demo() | ||
} | ||
|
||
module.exports = exports = demo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
indy-besu/smart_contracts/scripts/genesis/contracts/ethereumCLRegistry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { padLeft } from 'web3-utils' | ||
import { config } from '../config' | ||
import { ContractConfig } from '../contractConfig' | ||
import { buildProxySection, slots } from '../helpers' | ||
|
||
export interface EthereumCLRegistryConfig extends ContractConfig { | ||
data: { | ||
upgradeControlAddress: string | ||
} | ||
} | ||
|
||
export function ethereumCLRegistry() { | ||
const { name, address, description, data } = config.ethereumCLRegistry | ||
const storage: any = {} | ||
|
||
// address of upgrade control contact stored in slot 1 | ||
storage[slots['0']] = padLeft(data.upgradeControlAddress, 64) | ||
return buildProxySection(name, address, description, storage) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters