Search in sources :

Example 1 with ListRole

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

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

the class MainTest method createNetConfigContract.

private Contract createNetConfigContract(Contract contract, List<NodeInfo> netConfig, Collection<PrivateKey> currentConfigKeys) throws IOException {
    contract = contract.createRevision();
    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);
    contract.getStateData().set("net_config", netConfig);
    List<KeyRecord> creatorKeys = new ArrayList<>();
    for (PrivateKey key : currentConfigKeys) {
        creatorKeys.add(new KeyRecord(key.getPublicKey()));
        contract.addSignerKey(key);
    }
    contract.setCreator(creatorKeys);
    contract.seal();
    return contract;
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) ListRole(com.icodici.universa.contract.roles.ListRole)

Example 3 with ListRole

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

the class BaseNetworkTest method referenceForChangeOwnerWithCreateContract.

@Test
public void referenceForChangeOwnerWithCreateContract() throws Exception {
    // manager -
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    // issuer
    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 = llcProperty.createRevision(stepaPrivateKeys);
    llcProperty2.setOwnerKeys(thirdPartyPublicKeys);
    llcProperty2.seal();
    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)

Example 4 with ListRole

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

the class BaseNetworkTest method referenceForSplitJoinWithCreateContract.

@Test
public void referenceForSplitJoinWithCreateContract() 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.createTokenContract(llcPrivateKeys, stepaPublicKeys, "100000000000");
    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.setMode(ListRole.Mode.ALL);
    listRole.addRole(ownerRole);
    listRole.addRequiredReference("certification_contract", Role.RequiredMode.ALL_OF);
    llcProperty.getPermissions().remove("split_join");
    llcProperty.getPermissions().remove("change_owner");
    llcProperty.getPermissions().remove("revoke");
    Binder params = new Binder();
    params.set("min_value", 0.01);
    params.set("min_unit", 0.001);
    params.set("field_name", "amount");
    List<String> listFields = new ArrayList<>();
    listFields.add("state.origin");
    params.set("join_match_fields", listFields);
    SplitJoinPermission splitJoinPerm = new SplitJoinPermission(listRole, params);
    llcProperty.addPermission(splitJoinPerm);
    llcProperty.addSignerKey(llcPrivateKeys.iterator().next());
    llcProperty.seal();
    registerAndCheckApproved(llcProperty);
    Contract llcProperty2 = ContractsService.createSplit(llcProperty, 100, "amount", stepaPrivateKeys, true);
    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)

Example 5 with ListRole

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

the class BaseNetworkTest method referenceForChangeNumberWithCreateContract.

@Test
public void referenceForChangeNumberWithCreateContract() 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);
    llcProperty.getDefinition().getData().remove("name");
    llcProperty.getDefinition().getData().remove("description");
    Binder binderdata = new Binder();
    binderdata.set("name", "Abonement");
    binderdata.set("description", "Abonement.");
    llcProperty.getDefinition().setData(binderdata);
    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);
    Binder params = new Binder();
    params.set("min_value", 1);
    params.set("max_step", -1);
    params.set("field_name", "units");
    ChangeNumberPermission ChangeNumberPerm = new ChangeNumberPermission(listRole, params);
    llcProperty.addPermission(ChangeNumberPerm);
    llcProperty.getStateData().set("units", 1000000);
    llcProperty.addSignerKey(llcPrivateKeys.iterator().next());
    llcProperty.seal();
    registerAndCheckApproved(llcProperty);
    Contract llcProperty2 = llcProperty.createRevision(stepaPrivateKeys);
    llcProperty2.getStateData().set("units", llcProperty.getStateData().getIntOrThrow("units") - 1);
    llcProperty2.seal();
    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