use of com.icodici.universa.contract.permissions.ModifyDataPermission in project universa by UniversaBlockchain.
the class MainTest method createNetConfigContract.
private Contract createNetConfigContract(List<NodeInfo> netConfig, PrivateKey issuerKey) throws IOException {
Contract contract = new Contract();
contract.setIssuerKeys(issuerKey.getPublicKey());
contract.registerRole(new RoleLink("creator", "issuer"));
ListRole listRole = new ListRole("owner");
for (NodeInfo ni : netConfig) {
SimpleRole role = new SimpleRole(ni.getName());
contract.registerRole(role);
role.addKeyRecord(new KeyRecord(ni.getPublicKey()));
listRole.addRole(role);
}
listRole.setQuorum(netConfig.size() - 1);
contract.registerRole(listRole);
RoleLink ownerLink = new RoleLink("ownerlink", "owner");
ChangeOwnerPermission changeOwnerPermission = new ChangeOwnerPermission(ownerLink);
HashMap<String, Object> fieldsMap = new HashMap<>();
fieldsMap.put("net_config", null);
Binder modifyDataParams = Binder.of("fields", fieldsMap);
ModifyDataPermission modifyDataPermission = new ModifyDataPermission(ownerLink, modifyDataParams);
contract.addPermission(changeOwnerPermission);
contract.addPermission(modifyDataPermission);
contract.setExpiresAt(ZonedDateTime.now().plusYears(40));
contract.getStateData().set("net_config", netConfig);
contract.addSignerKey(issuerKey);
contract.seal();
return contract;
}
use of com.icodici.universa.contract.permissions.ModifyDataPermission in project universa by UniversaBlockchain.
the class Node method checkIfContractContainsNetConfig.
private boolean checkIfContractContainsNetConfig(Contract contract) {
if (!contract.getStateData().containsKey("net_config")) {
return false;
}
// check if owner is list role
if (!(contract.getOwner() instanceof ListRole)) {
return false;
}
// TODO: network council criteria here
// check if quorum matches network concil criteria
ListRole owner = (ListRole) contract.getOwner();
if (owner.getQuorum() == 0 || owner.getQuorum() < owner.getRoles().size() - 1) {
return false;
}
// check if owner keys set equals to nodes key set
Object obj = DefaultBiMapper.getInstance().deserializeObject(contract.getStateData().get("net_config"));
if (!(obj instanceof List)) {
return false;
}
List contractNodes = (List) obj;
Set<PublicKey> ownerKeys = contract.getOwner().getKeys();
if (contractNodes.size() != ownerKeys.size() || !contractNodes.stream().allMatch(nodeInfo -> nodeInfo instanceof NodeInfo && ownerKeys.contains(((NodeInfo) nodeInfo).getPublicKey()))) {
return false;
}
for (Permission permission : contract.getPermissions().values()) {
if (permission instanceof ChangeOwnerPermission) {
if (!(permission.getRole() instanceof RoleLink) || ((RoleLink) permission.getRole()).getRole() != contract.getOwner())
return false;
}
if (permission instanceof ModifyDataPermission) {
if (((ModifyDataPermission) permission).getFields().containsKey("net_config")) {
if (!(permission.getRole() instanceof RoleLink) || ((RoleLink) permission.getRole()).getRole() != contract.getOwner())
return false;
}
}
}
return true;
}
Aggregations