Search in sources :

Example 31 with PublicKey

use of com.icodici.crypto.PublicKey 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 32 with PublicKey

use of com.icodici.crypto.PublicKey in project universa by UniversaBlockchain.

the class BaseNetworkTest method getApprovedTUContract.

protected Contract getApprovedTUContract() throws Exception {
    synchronized (tuContractLock) {
        if (tuContract == null) {
            PrivateKey ownerKey = new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey"));
            Set<PublicKey> keys = new HashSet();
            keys.add(ownerKey.getPublicKey());
            Contract stepaTU = InnerContractsService.createFreshTU(100000000, keys);
            stepaTU.check();
            stepaTU.traceErrors();
            System.out.println("register new TU ");
            node.registerItem(stepaTU);
            tuContract = stepaTU;
        }
        int needRecreateTuContractNum = 0;
        for (Node n : nodes) {
            try {
                ItemResult itemResult = n.waitItem(tuContract.getId(), 15000);
                // assertEquals(ItemState.APPROVED, itemResult.state);
                if (itemResult.state != ItemState.APPROVED) {
                    System.out.println("TU: node " + n + " result: " + itemResult);
                    needRecreateTuContractNum++;
                }
            } catch (TimeoutException e) {
                System.out.println("ping ");
                // System.out.println(n.ping());
                // //                    System.out.println(n.traceTasksPool());
                // System.out.println(n.traceParcelProcessors());
                // System.out.println(n.traceItemProcessors());
                System.out.println("TU: node " + n + " timeout: ");
                needRecreateTuContractNum++;
            }
        }
        int recreateBorder = nodes.size() - config.getPositiveConsensus() - 1;
        if (recreateBorder < 0)
            recreateBorder = 0;
        if (needRecreateTuContractNum > recreateBorder) {
            tuContract = null;
            Thread.sleep(1000);
            return getApprovedTUContract();
        }
        return tuContract;
    }
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) TimeoutException(java.util.concurrent.TimeoutException)

Example 33 with PublicKey

use of com.icodici.crypto.PublicKey in project universa by UniversaBlockchain.

the class ResearchTest method registerSimpleContract.

@Test
public void registerSimpleContract() throws Exception {
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    Contract stepaCoins = Contract.fromDslFile(ROOT_PATH + "stepaCoins.yml");
    stepaCoins.addSignerKey(stepaPrivateKeys.iterator().next());
    stepaCoins.seal();
    System.out.println("nodeClient.register(stepaCoins)...");
    ItemResult itemResult = nodeClient.register(stepaCoins.getLastSealedBinary(), 5000);
    System.out.println("nodeClient.register(stepaCoins)... done! itemResult: " + itemResult.state);
    itemResult = nodeClient.getState(stepaCoins.getId());
    System.out.println("nodeClient.getState(stepaCoins): " + itemResult.state);
    assertEquals(ItemState.APPROVED, itemResult.state);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Contract(com.icodici.universa.contract.Contract) HashSet(java.util.HashSet)

Example 34 with PublicKey

use of com.icodici.crypto.PublicKey in project universa by UniversaBlockchain.

the class RoleReferencesTest method isAllowed.

@Test
public void isAllowed() throws Exception {
    PublicKey key = keys.get(0).getPublicKey();
    Set<PublicKey> keySet = new HashSet<>();
    keySet.add(key);
    SimpleRole sr = new SimpleRole("tr1");
    sr.addKeyRecord(new KeyRecord(key));
    assertTrue(!sr.isAllowedForKeys(new HashSet<>()));
    assertTrue(sr.isAllowedForKeys(keySet));
    assertTrue(sr.isAllowedFor(keySet, new HashSet<>()));
    sr.addRequiredReference("ref1", Role.RequiredMode.ALL_OF);
    sr.addRequiredReference("ref2", Role.RequiredMode.ALL_OF);
    Set<String> allRef = new HashSet<>();
    allRef.add("ref1");
    assertTrue(!sr.isAllowedFor(keySet, allRef));
    allRef.add("ref2");
    assertTrue(sr.isAllowedFor(keySet, allRef));
    sr.addRequiredReference("ref3", Role.RequiredMode.ANY_OF);
    assertTrue(!sr.isAllowedFor(keySet, allRef));
    sr.addRequiredReference("ref4", Role.RequiredMode.ANY_OF);
    sr.addRequiredReference("ref5", Role.RequiredMode.ANY_OF);
    allRef.add("ref4");
    assertTrue(sr.isAllowedFor(keySet, allRef));
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) PublicKey(com.icodici.crypto.PublicKey) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 35 with PublicKey

use of com.icodici.crypto.PublicKey in project universa by UniversaBlockchain.

the class SimpleRoleTest method testAddressRole.

@Test
public void testAddressRole() throws Exception {
    Set<KeyAddress> keyAddresses = new HashSet<>();
    keyAddresses.add(new KeyAddress(keys.get(0).getPublicKey(), 0, true));
    SimpleRole sr = new SimpleRole("tr1", keyAddresses);
    Binder serialized = DefaultBiMapper.serialize(sr);
    Role r1 = DefaultBiMapper.deserialize(serialized);
    Set<PublicKey> pubKeys = new HashSet<>();
    pubKeys.add(keys.get(0).getPublicKey());
    assertTrue(sr.isAllowedForKeys(pubKeys));
    assertTrue(r1.isAllowedForKeys(pubKeys));
    assertEquals(sr, r1);
}
Also used : Binder(net.sergeych.tools.Binder) PublicKey(com.icodici.crypto.PublicKey) KeyAddress(com.icodici.crypto.KeyAddress) Test(org.junit.Test)

Aggregations

PublicKey (com.icodici.crypto.PublicKey)84 PrivateKey (com.icodici.crypto.PrivateKey)75 Test (org.junit.Test)57 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)17 HashSet (java.util.HashSet)14 Binder (net.sergeych.tools.Binder)13 ListRole (com.icodici.universa.contract.roles.ListRole)11 Role (com.icodici.universa.contract.roles.Role)7 Contract (com.icodici.universa.contract.Contract)6 KeyAddress (com.icodici.crypto.KeyAddress)4 RoleLink (com.icodici.universa.contract.roles.RoleLink)4 KeyRecord (com.icodici.universa.contract.KeyRecord)3 ChangeOwnerPermission (com.icodici.universa.contract.permissions.ChangeOwnerPermission)3 Permission (com.icodici.universa.contract.permissions.Permission)3 TimeoutException (java.util.concurrent.TimeoutException)3 Decimal (com.icodici.universa.Decimal)2 ErrorRecord (com.icodici.universa.ErrorRecord)2 HashId (com.icodici.universa.HashId)2 Quantiser (com.icodici.universa.node2.Quantiser)2 List (java.util.List)2