Skip to content

Commit

Permalink
solidity contracts changed so they use bytes instead of bytes32
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Rojo <miguelangel.rojofernandez@mastercard.com>
  • Loading branch information
freemanzMrojo committed Aug 19, 2022
1 parent 46e2e41 commit e154a47
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 53 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.privacy;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;
import java.util.List;

import org.apache.tuweni.bytes.Bytes;
Expand Down Expand Up @@ -40,4 +55,16 @@ public void testGetParticipantsFromParameter() {
assertThat(actualParticipants.get(0)).isEqualTo(expectedECParticipant1);
assertThat(actualParticipants.get(1)).isEqualTo(expectedECParticipant2);
}

@Test
public void testDecodeList() {
// FIXME review this test when the encoded list is correct for EC enclave pub keys
Bytes bytes =
Bytes.fromBase64String(
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAATBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABEvJwuKk");

List<String> salida = FlexiblePrivacyGroupContract.decodeList(bytes);

assertThat(salida).isEqualTo(Arrays.asList(""));
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/

package org.hyperledger.besu.privacy.contracts.generated;

import java.math.BigInteger;
Expand All @@ -23,6 +24,7 @@
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Bool;
import org.web3j.abi.datatypes.DynamicArray;
import org.web3j.abi.datatypes.DynamicBytes;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Bytes32;
Expand All @@ -44,7 +46,7 @@
* or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the <a
* href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.
*
* <p>Generated with web3j version 4.5.16.
* <p>Generated with web3j version 1.4.1.
*/
@SuppressWarnings("rawtypes")
public class FlexiblePrivacyGroupManagementInterface extends Contract {
Expand Down Expand Up @@ -107,10 +109,10 @@ public RemoteFunctionCall<TransactionReceipt> addParticipants(List<byte[]> publi
new Function(
FUNC_ADDPARTICIPANTS,
Arrays.<Type>asList(
new org.web3j.abi.datatypes.DynamicArray<org.web3j.abi.datatypes.generated.Bytes32>(
org.web3j.abi.datatypes.generated.Bytes32.class,
new org.web3j.abi.datatypes.DynamicArray<org.web3j.abi.datatypes.DynamicBytes>(
org.web3j.abi.datatypes.DynamicBytes.class,
org.web3j.abi.Utils.typeMap(
publicEnclaveKeys, org.web3j.abi.datatypes.generated.Bytes32.class))),
publicEnclaveKeys, org.web3j.abi.datatypes.DynamicBytes.class))),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}
Expand All @@ -136,7 +138,7 @@ public RemoteFunctionCall<List> getParticipants() {
new Function(
FUNC_GETPARTICIPANTS,
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<Bytes32>>() {}));
Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<DynamicBytes>>() {}));
return new RemoteFunctionCall<List>(
function,
new Callable<List>() {
Expand Down Expand Up @@ -168,7 +170,7 @@ public RemoteFunctionCall<TransactionReceipt> removeParticipant(byte[] participa
final Function function =
new Function(
FUNC_REMOVEPARTICIPANT,
Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(participant)),
Arrays.<Type>asList(new org.web3j.abi.datatypes.DynamicBytes(participant)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
pragma solidity ^0.6.0;
pragma solidity >=0.7.0 <0.9.0;
pragma experimental ABIEncoderV2;
import "./FlexiblePrivacyGroupManagementInterface.sol";

contract DefaultFlexiblePrivacyGroupManagementContract is FlexiblePrivacyGroupManagementInterface {

address private _owner;
bool private _canExecute;
bytes32 private _version;
bytes32[] private distributionList;
mapping(bytes32 => uint256) private distributionIndexOf;
bytes[] private distributionList;
mapping(bytes => uint256) private distributionIndexOf;

function getVersion() external view override returns (bytes32) {
return _version;
Expand All @@ -43,10 +44,10 @@ contract DefaultFlexiblePrivacyGroupManagementContract is FlexiblePrivacyGroupMa
_canExecute = true;
}

function addParticipants(bytes32[] memory _publicEnclaveKeys) public override returns (bool) {
function addParticipants(bytes[] memory _publicEnclaveKeys) public override returns (bool) {
require(!_canExecute);
if (_owner == address(0x0)) {
// The account creating this group is set to be the owner
// The account creating this group is set to be the owner
_owner = tx.origin;
}
require(tx.origin == _owner, "Origin not the owner.");
Expand All @@ -56,15 +57,15 @@ contract DefaultFlexiblePrivacyGroupManagementContract is FlexiblePrivacyGroupMa
return result;
}

function removeParticipant(bytes32 _participant) public override returns (bool) {
function removeParticipant(bytes memory _participant) public override returns (bool) {
require(_canExecute);
require(tx.origin == _owner, "Origin not the owner.");
bool result = removeInternal(_participant);
updateVersion();
return result;
}

function getParticipants() public view override returns (bytes32[] memory) {
function getParticipants() public view override returns (bytes[] memory) {
return distributionList;
}

Expand All @@ -74,7 +75,7 @@ contract DefaultFlexiblePrivacyGroupManagementContract is FlexiblePrivacyGroupMa


//internal functions
function addAll(bytes32[] memory _publicEnclaveKeys) internal returns (bool) {
function addAll(bytes[] memory _publicEnclaveKeys) internal returns (bool) {
bool allAdded = true;
for (uint i = 0; i < _publicEnclaveKeys.length; i++) {
if (isMember(_publicEnclaveKeys[i])) {
Expand All @@ -90,11 +91,11 @@ contract DefaultFlexiblePrivacyGroupManagementContract is FlexiblePrivacyGroupMa
return allAdded;
}

function isMember(bytes32 _publicEnclaveKey) internal view returns (bool) {
function isMember(bytes memory _publicEnclaveKey) internal view returns (bool) {
return distributionIndexOf[_publicEnclaveKey] != 0;
}

function addParticipant(bytes32 _publicEnclaveKey) internal returns (bool) {
function addParticipant(bytes memory _publicEnclaveKey) internal returns (bool) {
if (distributionIndexOf[_publicEnclaveKey] == 0) {
distributionList.push(_publicEnclaveKey);
distributionIndexOf[_publicEnclaveKey] = distributionList.length;
Expand All @@ -103,12 +104,12 @@ contract DefaultFlexiblePrivacyGroupManagementContract is FlexiblePrivacyGroupMa
return false;
}

function removeInternal(bytes32 _participant) internal returns (bool) {
function removeInternal(bytes memory _participant) internal returns (bool) {
uint256 index = distributionIndexOf[_participant];
if (index > 0 && index <= distributionList.length) {
//move last address into index being vacated (unless we are dealing with last index)
if (index != distributionList.length) {
bytes32 lastPublicKey = distributionList[distributionList.length - 1];
bytes memory lastPublicKey = distributionList[distributionList.length - 1];
distributionList[index - 1] = lastPublicKey;
distributionIndexOf[lastPublicKey] = index;
}
Expand All @@ -120,12 +121,12 @@ contract DefaultFlexiblePrivacyGroupManagementContract is FlexiblePrivacyGroupMa
}

function updateVersion() internal returns (int) {
_version = keccak256(abi.encodePacked(blockhash(block.number-1), block.coinbase, distributionList));
_version = keccak256(abi.encode(blockhash(block.number-1), block.coinbase, distributionList));
}

event ParticipantAdded(
bool success,
bytes32 publicEnclaveKey,
bytes publicEnclaveKey,
string message
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
pragma solidity ^0.6.0;
pragma solidity >=0.7.0 <0.9.0;
pragma experimental ABIEncoderV2;

interface FlexiblePrivacyGroupManagementInterface {

function addParticipants(bytes32[] calldata publicEnclaveKeys) external returns (bool);
function addParticipants(bytes[] calldata publicEnclaveKeys) external returns (bool);

function removeParticipant(bytes32 participant) external returns (bool);
function removeParticipant(bytes calldata participant) external returns (bool);

function getParticipants() external view returns (bytes32[] memory);
function getParticipants() external view returns (bytes[] memory);

function lock() external;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
pragma solidity ^0.6.0;
pragma solidity >=0.7.0 <0.9.0;
pragma experimental ABIEncoderV2;
import "./FlexiblePrivacyGroupManagementInterface.sol";

contract FlexiblePrivacyGroupManagementProxy is FlexiblePrivacyGroupManagementInterface {
Expand All @@ -27,23 +28,23 @@ contract FlexiblePrivacyGroupManagementProxy is FlexiblePrivacyGroupManagementIn
implementation = _newImp;
}

function addParticipants(bytes32[] memory _publicEnclaveKeys) public override returns (bool) {
function addParticipants(bytes[] memory _publicEnclaveKeys) public override returns (bool) {
FlexiblePrivacyGroupManagementInterface privacyInterface = FlexiblePrivacyGroupManagementInterface(implementation);
return privacyInterface.addParticipants(_publicEnclaveKeys);
}

function getParticipants() view public override returns (bytes32[] memory) {
function getParticipants() view public override returns (bytes[] memory) {
FlexiblePrivacyGroupManagementInterface privacyInterface = FlexiblePrivacyGroupManagementInterface(implementation);
return privacyInterface.getParticipants();
}

function removeParticipant(bytes32 _participant) public override returns (bool) {
function removeParticipant(bytes memory _participant) public override returns (bool) {
FlexiblePrivacyGroupManagementInterface privacyInterface = FlexiblePrivacyGroupManagementInterface(implementation);
bool result = privacyInterface.removeParticipant(_participant);
if (result) {
emit ParticipantRemoved(_participant);
}
return result;
return result;
}

function lock() public override {
Expand Down Expand Up @@ -75,15 +76,14 @@ contract FlexiblePrivacyGroupManagementProxy is FlexiblePrivacyGroupManagementIn
require(this.canExecute(), "The contract is locked.");
require(implementation != _newImplementation, "The contract to upgrade to has to be different from the current management contract.");
require(this.canUpgrade(), "Not allowed to upgrade the management contract.");
bytes32[] memory participants = this.getParticipants();
bytes[] memory participants = this.getParticipants();
_setImplementation(_newImplementation);
FlexiblePrivacyGroupManagementInterface privacyInterface = FlexiblePrivacyGroupManagementInterface(implementation);
privacyInterface.addParticipants(participants);
}

event ParticipantRemoved(
bytes32 publicEnclaveKey
bytes publicEnclaveKey
);


}
2 changes: 1 addition & 1 deletion privacy-contracts/src/main/solidity/generateWrappers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ done

for target in ${targets}; do

web3j solidity generate \
web3j generate solidity \
-b build/${target}.bin \
-a build/${target}.abi \
-o ../java \
Expand Down

0 comments on commit e154a47

Please sign in to comment.