Search in sources :

Example 6 with ListRole

use of com.icodici.universa.contract.roles.ListRole 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)

Example 7 with ListRole

use of com.icodici.universa.contract.roles.ListRole in project universa by UniversaBlockchain.

the class BaseNetworkTest method referenceForRevokeWithCreateContract.

@Test
public void referenceForRevokeWithCreateContract() throws Exception {
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PrivateKey> llcPrivateKeys = new HashSet<>();
    Set<PrivateKey> thirdPartyPrivateKeys = new HashSet<>();
    llcPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey")));
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    thirdPartyPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/marty_mcfly.private.unikey")));
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    Set<PublicKey> thirdPartyPublicKeys = new HashSet<>();
    for (PrivateKey pk : thirdPartyPrivateKeys) {
        thirdPartyPublicKeys.add(pk.getPublicKey());
    }
    Contract jobCertificate = new Contract(llcPrivateKeys.iterator().next());
    jobCertificate.setOwnerKeys(stepaPublicKeys);
    jobCertificate.getDefinition().getData().set("issuer", "Roga & Kopita");
    jobCertificate.getDefinition().getData().set("type", "chief accountant assignment");
    jobCertificate.seal();
    registerAndCheckApproved(jobCertificate);
    Contract llcProperty = ContractsService.createNotaryContract(llcPrivateKeys, stepaPublicKeys);
    List<String> listConditions = new ArrayList<>();
    listConditions.add("ref.definition.issuer == \"26RzRJDLqze3P5Z1AzpnucF75RLi1oa6jqBaDh8MJ3XmTaUoF8R\"");
    listConditions.add("ref.definition.data.issuer == \"Roga & Kopita\"");
    listConditions.add("ref.definition.data.type == \"chief accountant assignment\"");
    Reference reference = new Reference(llcProperty);
    reference.name = "certification_contract";
    reference.type = Reference.TYPE_EXISTING;
    Binder conditions = new Binder();
    conditions.set("all_of", listConditions);
    reference.setConditions(conditions);
    reference.addMatchingItem(jobCertificate);
    // llcProperty.getDefinition().getReferences().add(reference);
    llcProperty.addReference(reference);
    ListRole listRole = new ListRole("list_role");
    SimpleRole ownerRole = new SimpleRole("owner", stepaPrivateKeys);
    listRole.addRole(ownerRole);
    listRole.addRequiredReference("certification_contract", Role.RequiredMode.ALL_OF);
    llcProperty.getPermissions().remove("change_owner");
    llcProperty.getPermissions().remove("revoke");
    ChangeOwnerPermission changeOwnerPerm = new ChangeOwnerPermission(listRole);
    llcProperty.addPermission(changeOwnerPerm);
    RevokePermission revokePerm = new RevokePermission(listRole);
    llcProperty.addPermission(revokePerm);
    llcProperty.addSignerKey(llcPrivateKeys.iterator().next());
    llcProperty.seal();
    registerAndCheckApproved(llcProperty);
    Contract llcProperty2 = ContractsService.createRevocation(llcProperty, stepaPrivateKeys.iterator().next());
    llcProperty2.check();
    llcProperty2.traceErrors();
    // assertFalse(llcProperty2.isOk());
    TransactionPack tp_before = llcProperty2.getTransactionPack();
    // tp_before.addReferencedItem(jobCertificate);
    byte[] data = tp_before.pack();
    TransactionPack tp_after = TransactionPack.unpack(data);
    registerAndCheckApproved(tp_after);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Binder(net.sergeych.tools.Binder) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) ListRole(com.icodici.universa.contract.roles.ListRole) Test(org.junit.Test)

Aggregations

ListRole (com.icodici.universa.contract.roles.ListRole)7 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)6 PrivateKey (com.icodici.crypto.PrivateKey)5 PublicKey (com.icodici.crypto.PublicKey)5 Binder (net.sergeych.tools.Binder)5 Test (org.junit.Test)4 ChangeOwnerPermission (com.icodici.universa.contract.permissions.ChangeOwnerPermission)2 ModifyDataPermission (com.icodici.universa.contract.permissions.ModifyDataPermission)2 RoleLink (com.icodici.universa.contract.roles.RoleLink)2 Permission (com.icodici.universa.contract.permissions.Permission)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1