Search in sources :

Example 1 with ModifyDataPermission

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;
}
Also used : RoleLink(com.icodici.universa.contract.roles.RoleLink) ModifyDataPermission(com.icodici.universa.contract.permissions.ModifyDataPermission) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Binder(net.sergeych.tools.Binder) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) ChangeOwnerPermission(com.icodici.universa.contract.permissions.ChangeOwnerPermission) ListRole(com.icodici.universa.contract.roles.ListRole)

Example 2 with ModifyDataPermission

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;
}
Also used : RoleLink(com.icodici.universa.contract.roles.RoleLink) ModifyDataPermission(com.icodici.universa.contract.permissions.ModifyDataPermission) PublicKey(com.icodici.crypto.PublicKey) ModifyDataPermission(com.icodici.universa.contract.permissions.ModifyDataPermission) ChangeOwnerPermission(com.icodici.universa.contract.permissions.ChangeOwnerPermission) Permission(com.icodici.universa.contract.permissions.Permission) ChangeOwnerPermission(com.icodici.universa.contract.permissions.ChangeOwnerPermission) ListRole(com.icodici.universa.contract.roles.ListRole)

Aggregations

ChangeOwnerPermission (com.icodici.universa.contract.permissions.ChangeOwnerPermission)2 ModifyDataPermission (com.icodici.universa.contract.permissions.ModifyDataPermission)2 ListRole (com.icodici.universa.contract.roles.ListRole)2 RoleLink (com.icodici.universa.contract.roles.RoleLink)2 PublicKey (com.icodici.crypto.PublicKey)1 Permission (com.icodici.universa.contract.permissions.Permission)1 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Binder (net.sergeych.tools.Binder)1