Search in sources :

Example 76 with PublicKey

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

the class ContractsServiceTest method goodShare.

@Test
public void goodShare() throws Exception {
    Set<PrivateKey> martyPrivateKeys = new HashSet<>();
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PublicKey> martyPublicKeys = new HashSet<>();
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    martyPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/marty_mcfly.private.unikey")));
    stepaPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")));
    for (PrivateKey pk : stepaPrivateKeys) stepaPublicKeys.add(pk.getPublicKey());
    for (PrivateKey pk : martyPrivateKeys) martyPublicKeys.add(pk.getPublicKey());
    Contract notaryContract = ContractsService.createShareContract(martyPrivateKeys, stepaPublicKeys, "100");
    notaryContract.check();
    notaryContract.traceErrors();
    assertTrue(notaryContract.isOk());
    assertTrue(notaryContract.getOwner().isAllowedForKeys(stepaPublicKeys));
    assertTrue(notaryContract.getIssuer().isAllowedForKeys(martyPrivateKeys));
    assertTrue(notaryContract.getCreator().isAllowedForKeys(martyPrivateKeys));
    assertFalse(notaryContract.getOwner().isAllowedForKeys(martyPrivateKeys));
    assertFalse(notaryContract.getIssuer().isAllowedForKeys(stepaPublicKeys));
    assertFalse(notaryContract.getCreator().isAllowedForKeys(stepaPublicKeys));
    assertTrue(notaryContract.getExpiresAt().isAfter(ZonedDateTime.now().plusMonths(3)));
    assertTrue(notaryContract.getCreatedAt().isBefore(ZonedDateTime.now()));
    assertEquals(InnerContractsService.getDecimalField(notaryContract, "amount"), new Decimal(100));
    assertEquals(notaryContract.getPermissions().get("split_join").size(), 1);
    Binder splitJoinParams = notaryContract.getPermissions().get("split_join").iterator().next().getParams();
    assertEquals(splitJoinParams.get("min_value"), 1);
    assertEquals(splitJoinParams.get("min_unit"), 1);
    assertEquals(splitJoinParams.get("field_name"), "amount");
    assertTrue(splitJoinParams.get("join_match_fields") instanceof List);
    assertEquals(((List) splitJoinParams.get("join_match_fields")).get(0), "state.origin");
    assertTrue(notaryContract.isPermitted("revoke", stepaPublicKeys));
    assertTrue(notaryContract.isPermitted("revoke", martyPublicKeys));
    assertTrue(notaryContract.isPermitted("change_owner", stepaPublicKeys));
    assertFalse(notaryContract.isPermitted("change_owner", martyPublicKeys));
    assertTrue(notaryContract.isPermitted("split_join", stepaPublicKeys));
    assertFalse(notaryContract.isPermitted("split_join", martyPublicKeys));
}
Also used : Binder(net.sergeych.tools.Binder) PrivateKey(com.icodici.crypto.PrivateKey) Decimal(com.icodici.universa.Decimal) PublicKey(com.icodici.crypto.PublicKey) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 77 with PublicKey

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

the class ContractsServiceTest method createTU.

@Test
public void createTU() throws Exception {
    PrivateKey privateKey = TestKeys.privateKey(3);
    Set<PublicKey> keys = new HashSet();
    keys.add(privateKey.getPublicKey());
    Contract tu = InnerContractsService.createFreshTU(100, keys);
    tu.check();
    tu.traceErrors();
    assertEquals(true, tu.getRole("owner").isAllowedForKeys(keys));
    assertEquals(100, tu.getStateData().getIntOrThrow("transaction_units"));
    PrivateKey manufacturePrivateKey = new PrivateKey(Do.read("./src/test_contracts/keys/tu_key.private.unikey"));
    Set<PublicKey> badKeys = new HashSet();
    badKeys.add(manufacturePrivateKey.getPublicKey());
    assertEquals(false, tu.getRole("owner").isAllowedForKeys(badKeys));
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 78 with PublicKey

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

the class ExtendedSignatureTest method parallelExecutionVerify.

@Test
public void parallelExecutionVerify() throws Exception {
    ExecutorService single = Executors.newSingleThreadExecutor();
    ExecutorService multiple = Executors.newCachedThreadPool();
    byte[] data = "Hello world".getBytes();
    PrivateKey k = new PrivateKey(2048);
    PublicKey key = k.getPublicKey();
    byte[] signature = ExtendedSignature.sign(k, data);
    // warm up
    for (int i = 0; i < 200; i++) ExtendedSignature.sign(k, data);
    double t1 = parallelize(single, 1, () -> {
        for (int i = 0; i < 1000; i++) ExtendedSignature.verify(key, signature, data);
    });
    System.out.println(t1);
    double t2 = parallelize(multiple, 4, () -> {
        for (int i = 0; i < 1000; i++) ExtendedSignature.verify(key, signature, data);
    });
    System.out.println(t2);
    assertThat(Math.abs(t1 - t2), is(lessThan(0.15)));
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 79 with PublicKey

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

the class PermissionsTest method changeOwnerWithReference.

@Test
public void changeOwnerWithReference() throws Exception {
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    stepaPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")));
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    Set<String> references = new HashSet<>();
    references.add("certification_contract");
    Contract c = Contract.fromDslFile(rootPath + "NotaryWithReferenceDSLTemplate.yml");
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    Role r = c.getPermissions().getFirst("change_owner").getRole();
    assertThat(r, is(instanceOf(ListRole.class)));
    assertFalse(r.isAllowedFor(stepaPublicKeys, null));
    assertTrue(r.isAllowedFor(stepaPublicKeys, references));
    System.out.println("Owner now :" + c.getOwner());
    System.out.println("change owner permission :" + c.getPermissions().get("change_owner"));
    c.seal();
    c.check();
    c.traceErrors();
    assertTrue(c.isOk());
    assertEquals(c, (c.getPermissions().getFirst("change_owner").getRole()).getContract());
    // Bad contract change: owner has no right to change owner ;)
    Contract c1 = c.createRevision(TestKeys.privateKey(0));
    c1.setOwnerKey(ownerKey2);
    assertNotEquals(c.getOwner(), c1.getOwner());
    c1.seal();
    c1.check();
    c1.traceErrors();
    assertEquals(1, c1.getErrors().size());
    ErrorRecord error = c1.getErrors().get(0);
    assertEquals(Errors.FORBIDDEN, error.getError());
    // bad contract change: good key but bad reference
    Contract c2 = c.createRevision(stepaPrivateKeys);
    c2.setOwnerKey(ownerKey3);
    assertEquals(c2, c2.getPermissions().getFirst("change_owner").getRole().getContract());
    assertNotEquals(c.getOwner(), c2.getOwner());
    c2.seal();
    c2.check();
    c2.traceErrors();
    assertEquals(1, c2.getErrors().size());
    error = c2.getErrors().get(0);
    assertEquals(Errors.FORBIDDEN, error.getError());
    // good contract change: creator is an owner
    Contract c3 = c.createRevision(stepaPrivateKeys);
    c3.setOwnerKey(ownerKey3);
    Reference ref = new Reference();
    ref.name = "certification_contract";
    ref.type = Reference.TYPE_EXISTING;
    ref.addMatchingItem(new Contract());
    c3.getReferences().put(ref.name, ref);
    assertEquals(c3, c3.getPermissions().getFirst("change_owner").getRole().getContract());
    assertNotEquals(c.getOwner(), c3.getOwner());
    sealCheckTrace(c3, true);
}
Also used : ListRole(com.icodici.universa.contract.roles.ListRole) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Role(com.icodici.universa.contract.roles.Role) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) HashSet(java.util.HashSet) ErrorRecord(com.icodici.universa.ErrorRecord) Test(org.junit.Test)

Example 80 with PublicKey

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

the class PermissionsTest method splitJoinWithReference.

@Test
public void splitJoinWithReference() throws Exception {
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    stepaPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")));
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    Set<String> references = new HashSet<>();
    references.add("certification_contract");
    Contract c = Contract.fromDslFile(rootPath + "TokenWithReferenceDSLTemplate.yml");
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    Role r = c.getPermissions().getFirst("split_join").getRole();
    assertThat(r, is(instanceOf(ListRole.class)));
    assertFalse(r.isAllowedFor(stepaPublicKeys, null));
    assertTrue(r.isAllowedFor(stepaPublicKeys, references));
    System.out.println("split join permission :" + c.getPermissions().get("split_join"));
    c.seal();
    c.check();
    c.traceErrors();
    assertTrue(c.isOk());
    assertEquals(c, (c.getPermissions().getFirst("split_join").getRole()).getContract());
    // Bad contract change: owner has no right to change owner ;)
    Set<PrivateKey> badPrivateKeys = new HashSet<>();
    badPrivateKeys.add(TestKeys.privateKey(0));
    Contract c1 = ContractsService.createSplit(c, 1, "amount", badPrivateKeys);
    c1.seal();
    c1.check();
    c1.traceErrors();
    // assertEquals(1, c1.getErrors().size());
    // ErrorRecord error = c1.getErrors().get(0);
    // assertEquals(Errors.FORBIDDEN, error.getError());
    assertFalse(c1.isOk());
    // bad contract change: good key but no reference
    Contract c2 = ContractsService.createSplit(c, 1, "amount", stepaPrivateKeys);
    c2.createRole("creator", c2.getRole("owner"));
    c2.getNew().get(0).createRole("creator", c2.getNew().get(0).getRole("owner"));
    assertEquals(c2, c2.getPermissions().getFirst("split_join").getRole().getContract());
    System.out.println("-------------");
    c1.seal();
    c1.check();
    c1.traceErrors();
    // assertEquals(1, c1.getErrors().size());
    // ErrorRecord error = c1.getErrors().get(0);
    // assertEquals(Errors.FORBIDDEN, error.getError());
    assertFalse(c1.isOk());
    // good contract change: creator is an owner
    Contract c3 = ContractsService.createSplit(c, 1, "amount", stepaPrivateKeys);
    c3.createRole("creator", c3.getRole("owner"));
    c3.getNew().get(0).createRole("creator", c3.getNew().get(0).getRole("owner"));
    Reference ref = new Reference();
    ref.name = "certification_contract";
    ref.type = Reference.TYPE_EXISTING;
    ref.addMatchingItem(new Contract());
    c3.getReferences().put(ref.name, ref);
    c3.getNew().get(0).getReferences().put(ref.name, ref);
    assertEquals(c3, c3.getPermissions().getFirst("split_join").getRole().getContract());
    System.out.println("-------------");
    sealCheckTrace(c3, true);
}
Also used : ListRole(com.icodici.universa.contract.roles.ListRole) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Role(com.icodici.universa.contract.roles.Role) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) HashSet(java.util.HashSet) 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