Skip to content

Commit

Permalink
Update indy-besu genesis and fix demo
Browse files Browse the repository at this point in the history
Signed-off-by: aziz.karabashov <[email protected]>
  • Loading branch information
akarabashov committed Dec 8, 2023
1 parent c0c69a3 commit a821a22
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 36 deletions.
38 changes: 16 additions & 22 deletions indy-besu/network/config/besu/genesis.json

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion indy-besu/smart_contracts/contracts/utils/DidUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,33 @@ library DidUtils {
return parsedDid;
}

/**
* @dev Converts a given Ethereum identifier to an Ethereum address.
* @param identifier The Ethereum identifier to be converted.
* @return The Ethereum address derived from the identifier, or the zero address if the identifier is incorrect.
*/
function convertEthereumIdentifierToAddress(string memory identifier) internal view returns (address) {
if (!StringUtils.hasHexPrefix(identifier)) return address(0);
if (!(StringUtils.length(identifier) != _ADDRESS_HEX_STRING_LENGTH)) return address(0);

bytes memory identifierBytes = StringUtils.hexToBytes(identifier);

return address(uint160(bytes20(identifierBytes)));
}

/**
* @dev Checks if a given method string corresponds to the Ethereum method identifier.
* @param method The method string to check.
* @return Returns `true` if the method string matches the Ethereum method identifier, `false` otherwise.
*/
function isEthereumMethod(string memory method) internal pure returns (bool) {
return method.toSlice().eq(DID_ETHR_METHOD.toSlice());
}

/**
* @dev Checks if a given method string corresponds to any of the Indy method identifiers.
* @param method The method string to check.
* @return Returns `true` if the method string matches any of the Indy method identifiers, `false` otherwise.
*/
function isIndyMethod(string memory method) internal pure returns (bool) {
StrSlice methodSlice = method.toSlice();
return
Expand Down
26 changes: 18 additions & 8 deletions indy-besu/smart_contracts/contracts/utils/StringUtils.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import { toSlice } from "@dk1a/solidity-stringutils/src/StrSlice.sol";
import { StrSlice, toSlice } from "@dk1a/solidity-stringutils/src/StrSlice.sol";

using { toSlice } for string;

Expand All @@ -18,7 +18,7 @@ library StringUtils {
* @dev Checks if two strings are equal.
* @param str First string to compare.
* @param other Second string to compare.
* @return bool True if strings are equal, false otherwise.
* @return True if strings are equal, false otherwise.
*/
function equals(string memory str, string memory other) internal pure returns (bool) {
return str.toSlice().eq(other.toSlice());
Expand All @@ -27,7 +27,7 @@ library StringUtils {
/**
* @dev Checks if a string is empty.
* @param str String to check.
* @return bool True if the string is empty, false otherwise.
* @return Returns `true` if the string is empty, `false` otherwise.
*/
function isEmpty(string memory str) internal pure returns (bool) {
return length(str) == 0;
Expand All @@ -36,32 +36,42 @@ library StringUtils {
/**
* @dev Returns the length of a string.
* @param str String to check.
* @return uint256 Length of the string.
* @return Length of the string.
*/
function length(string memory str) internal pure returns (uint256) {
return bytes(str).length;
}

/**
* @dev Check if a given string has a hex prefix
* @param str String to check.
* @return Returns `true` if strings has a hex prefix, `false` otherwise.
*/
function hasHexPrefix(string memory str) internal pure returns (bool) {
return str.toSlice().startsWith(_HEX_PREFIX.toSlice());
}

/**
* @dev Converts a hexadecimal string to bytes.
* @param hexString The hexadecimal string to be converted.
* @return The bytes represented by the hexadecimal string.
* @return The bytes represented by the hexadecimal string, or the zero bytes if the hex is incorrect.
*/
function hexToBytes(string memory hexString) internal view returns (bytes memory) {
hexString = hexString.toSlice().stripPrefix(_HEX_PREFIX.toSlice()).toString();
StrSlice hexStringSlice = hexString.toSlice();
StrSlice hexPrefixSlice = _HEX_PREFIX.toSlice();

// Check and remove hex prefix
if (!hexStringSlice.startsWith(_HEX_PREFIX.toSlice())) return "";
hexString = hexStringSlice.stripPrefix(hexPrefixSlice).toString();

bytes memory hexStringBytes = bytes(hexString);
bytes memory resultBytes = new bytes(hexStringBytes.length / 2);
for (uint256 i = 0; i < resultBytes.length; i++) {
(uint8 firstByte, bool firstByteValid) = _hexCharToByte(hexStringBytes[2 * i]);
if (!firstByteValid) return bytes(_HEX_PREFIX);
if (!firstByteValid) return "";

(uint8 secondByte, bool secondByteValid) = _hexCharToByte(hexStringBytes[2 * i + 1]);
if (!secondByteValid) return bytes(_HEX_PREFIX);
if (!secondByteValid) return "";

resultBytes[i] = bytes1(firstByte * 16 + secondByte);
}
Expand Down
6 changes: 3 additions & 3 deletions indy-besu/smart_contracts/demos/utils/actor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
RoleControl,
DidRegistry,
IndyDidRegistry,
SchemaRegistry,
CredentialDefinitionRegistry,
ValidatorControl,
Expand All @@ -13,7 +13,7 @@ export class Actor {
public account: Account
public roleControl!: RoleControl
public validatorControl!: ValidatorControl
public didRegistry!: DidRegistry
public didRegistry!: IndyDidRegistry
public ethereumDIDRegistry!: EthereumDIDRegistry
public schemaRegistry!: SchemaRegistry
public credentialDefinitionRegistry!: CredentialDefinitionRegistry
Expand All @@ -26,7 +26,7 @@ export class Actor {
public async init() {
this.roleControl = await new RoleControl(this.account).getInstance(RoleControl.defaultAddress)
this.validatorControl = await new ValidatorControl(this.account).getInstance(ValidatorControl.defaultAddress)
this.didRegistry = await new DidRegistry(this.account).getInstance(DidRegistry.defaultAddress)
this.didRegistry = await new IndyDidRegistry(this.account).getInstance(IndyDidRegistry.defaultAddress)
this.ethereumDIDRegistry = await new EthereumDIDRegistry(this.account).getInstance(
EthereumDIDRegistry.defaultAddress,
)
Expand Down
2 changes: 1 addition & 1 deletion indy-besu/smart_contracts/scripts/genesis/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const config: Config = {
name: 'IndyDidRegistry',
address: contractsAddresses.didRegistry,
description: 'Smart contract to manage DIDs',
libraries: { 'contracts/did/DidValidator.sol:DidValidator': contractsAddresses.didValidator },
libraries: { 'contracts/did/IndyDidValidator.sol:IndyDidValidator': contractsAddresses.didValidator },
data: {
dids: [],
upgradeControlAddress: contractsAddresses.upgradeControl,
Expand Down
2 changes: 1 addition & 1 deletion indy-besu/smart_contracts/utils/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Account {
}

public get didEthr() {
return `did:ethr:${this.address.substring(2)}`
return `did:ethr:${this.address}`
}

public get didDocument() {
Expand Down

0 comments on commit a821a22

Please sign in to comment.