Search in sources :

Example 6 with SimpleRole

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

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

Example 8 with SimpleRole

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

the class InnerContractsService method createFreshTU.

/**
 * Creates fresh contract in the first revision with transaction units.
 *<br><br>
 * This contract should be registered and then should be used as payment for other contract's processing.
 * TU contracts signs with special Universa keys and set as owner public keys from params.
 *<br><br>
 * @param amount is initial number of TU that will be have an owner
 * @param ownerKeys is public keys that will became an owner of TU
 * @param withTestTU if true TU will be created with test transaction units
 * @return sealed TU contract; should be registered in the Universa by simplified procedure.
 * @throws IOException with exceptions while contract preparing
 */
public static synchronized Contract createFreshTU(int amount, Set<PublicKey> ownerKeys, boolean withTestTU) throws IOException {
    PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(Config.tuKeyPath));
    Contract tu;
    if (withTestTU) {
        tu = Contract.fromDslFile(Config.testTUTemplatePath);
    } else {
        tu = Contract.fromDslFile(Config.tuTemplatePath);
    }
    SimpleRole ownerRole = new SimpleRole("owner");
    for (PublicKey k : ownerKeys) {
        KeyRecord kr = new KeyRecord(k);
        ownerRole.addKeyRecord(kr);
    }
    tu.registerRole(ownerRole);
    tu.createRole("owner", ownerRole);
    tu.getStateData().set("transaction_units", amount);
    if (withTestTU) {
        tu.getStateData().set("test_transaction_units", amount * 100);
    }
    tu.addSignerKey(manufacturePrivateKey);
    tu.seal();
    return tu;
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) PublicKey(com.icodici.crypto.PublicKey)

Example 9 with SimpleRole

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

the class CLIMainTest method revokeContractVirtual.

@Test
public void revokeContractVirtual() throws Exception {
    Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
    c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
    c.addSignerKeyFromFile(rootPath + "keys/tu_key.private.unikey");
    PrivateKey goodKey = c.getKeysToSignWith().iterator().next();
    // let's make this key among owners
    ((SimpleRole) c.getRole("owner")).addKeyRecord(new KeyRecord(goodKey.getPublicKey()));
    c.seal();
    System.out.println("---");
    System.out.println("register contract");
    System.out.println("---");
    CLIMain.registerContract(c);
    Thread.sleep(1500);
    System.out.println("---");
    System.out.println("check contract");
    System.out.println("---");
    callMain("--probe", c.getId().toBase64String());
    System.out.println(output);
    assertTrue(output.indexOf(ItemState.APPROVED.name()) >= 0);
    PrivateKey issuer1 = TestKeys.privateKey(1);
    Contract tc = ContractsService.createRevocation(c, issuer1, goodKey);
    assertTrue(tc.check());
    System.out.println("---");
    System.out.println("register revoking contract");
    System.out.println("---");
    CLIMain.registerContract(tc);
    Thread.sleep(2500);
    System.out.println("---");
    System.out.println("check revoking contract");
    System.out.println("---");
    callMain("--probe", tc.getId().toBase64String());
    System.out.println(output);
    assertTrue(output.indexOf(ItemState.APPROVED.name()) >= 1);
    Thread.sleep(1500);
    System.out.println("---");
    System.out.println("check contract");
    System.out.println("---");
    callMain("--probe", c.getId().toBase64String());
    System.out.println(output);
    assertTrue(output.indexOf(ItemState.REVOKED.name()) >= 0);
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 10 with SimpleRole

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

the class CLIMainTest method testExportImportWithAddresses.

@Test
public void testExportImportWithAddresses() throws Exception {
    callMain2("-create", rootPath + "simple_root_contract_v2.yml", "-name", basePath + "contractWithAddresses.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey");
    Contract contract = CLIMain.loadContract(basePath + "contractWithAddresses.unicon", true);
    Set<KeyAddress> keyAddresses = new HashSet<>();
    keyAddresses.add(new KeyAddress(TestKeys.publicKey(0), 0, true));
    SimpleRole sr1 = new SimpleRole("owner", keyAddresses);
    contract.registerRole(sr1);
    contract.addSignerKey(TestKeys.privateKey(0));
    contract.seal();
    CLIMain.saveContract(contract, basePath + "contractWithAddresses.unicon");
    callMain("-e", basePath + "contractWithAddresses.unicon", "-name", basePath + "contractWithAddresses.json");
    System.out.println(output);
    assertTrue(output.indexOf("export as json ok") >= 0);
    assertEquals(0, errors.size());
    callMain("-i", basePath + "contractWithAddresses.json", "-name", basePath + "contractWithAddressesImported.unicon");
    System.out.println(output);
    assertTrue(output.indexOf("import from json ok") >= 0);
    assertEquals(1, errors.size());
    if (errors.size() > 0) {
        assertEquals(Errors.NOT_SIGNED.name(), errors.get(0).get("code"));
    }
    Contract contractImported = CLIMain.loadContract(basePath + "contractWithAddressesImported.unicon", true);
    assertTrue(contractImported.getOwner().getKeyAddresses().iterator().next().isMatchingKey(TestKeys.privateKey(0).getPublicKey()));
    PrivateKey creatorPrivateKey = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey"));
    contractImported.addSignatureToSeal(creatorPrivateKey);
    contractImported.addSignatureToSeal(TestKeys.privateKey(0));
    assertTrue(contractImported.check());
    Set<PrivateKey> signKeys = new HashSet<>();
    signKeys.add(creatorPrivateKey);
    signKeys.add(TestKeys.privateKey(0));
    contractImported.setKeysToSignWith(signKeys);
    byte[] sealedContract = contractImported.sealAsV2();
    TransactionPack tp = new TransactionPack();
    tp.addKeys(creatorPrivateKey.getPublicKey());
    tp.addKeys(TestKeys.privateKey(0).getPublicKey());
    Contract restoredContract = new Contract(sealedContract, tp);
    assertTrue(restoredContract.check());
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) KeyAddress(com.icodici.crypto.KeyAddress) Contract(com.icodici.universa.contract.Contract) TransactionPack(com.icodici.universa.contract.TransactionPack) Test(org.junit.Test)

Aggregations

SimpleRole (com.icodici.universa.contract.roles.SimpleRole)23 PrivateKey (com.icodici.crypto.PrivateKey)22 Test (org.junit.Test)12 PublicKey (com.icodici.crypto.PublicKey)11 Binder (net.sergeych.tools.Binder)9 Contract (com.icodici.universa.contract.Contract)7 KeyRecord (com.icodici.universa.contract.KeyRecord)6 ListRole (com.icodici.universa.contract.roles.ListRole)6 ItemResult (com.icodici.universa.node.ItemResult)2 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 KeyAddress (com.icodici.crypto.KeyAddress)1 TransactionPack (com.icodici.universa.contract.TransactionPack)1 ChangeOwnerPermission (com.icodici.universa.contract.permissions.ChangeOwnerPermission)1 ModifyDataPermission (com.icodici.universa.contract.permissions.ModifyDataPermission)1 RoleLink (com.icodici.universa.contract.roles.RoleLink)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Path (java.nio.file.Path)1