Search in sources :

Example 36 with PrivateKey

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

the class PermissionsTest method shouldNotModifySeveralStateDataValues.

@Test
public void shouldNotModifySeveralStateDataValues() throws Exception {
    PrivateKey ownerKey2 = TestKeys.privateKey(1);
    Contract c = basicContractCreation(SUBSCRIPTION_WITH_DATA, PRIVATE_KEY, ownerKey2);
    Binder d = c.getStateData();
    Contract c1 = c.createRevision(ownerKey2);
    Binder d1 = c1.getStateData();
    String oldValue = "An example of smart contract.";
    String newValue = "UniversaSmartContract";
    String field = "description";
    setAndCheckOldNewValues(d, d1, oldValue, newValue, field);
    oldValue = "blockchain-partnership.";
    newValue = "blockchain-universa.";
    field = "partner_name";
    setAndCheckOldNewValues(d, d1, oldValue, newValue, field);
    d1.addToInt("transactional_units_left", -50);
    d1.addToInt("direction", 5);
    d1.addToInt("option", -1);
    sealCheckTrace(c1, false);
}
Also used : Binder(net.sergeych.tools.Binder) PrivateKey(com.icodici.crypto.PrivateKey) Test(org.junit.Test)

Example 37 with PrivateKey

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

the class PermissionsTest method revokeWithReference.

@Test
public void revokeWithReference() 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("revoke").getRole();
    assertThat(r, is(instanceOf(ListRole.class)));
    assertFalse(r.isAllowedFor(stepaPublicKeys, null));
    assertTrue(r.isAllowedFor(stepaPublicKeys, references));
    System.out.println("revoke permission :" + c.getPermissions().get("revoke"));
    c.seal();
    c.check();
    c.traceErrors();
    assertTrue(c.isOk());
    assertEquals(c, (c.getPermissions().getFirst("revoke").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 = ContractsService.createRevocation(c, stepaPrivateKeys.iterator().next());
    assertEquals(c2.getRevoking().get(0), c2.getRevoking().get(0).getPermissions().getFirst("revoke").getRole().getContract());
    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 = ContractsService.createRevocation(c, stepaPrivateKeys.iterator().next());
    Reference ref = new Reference();
    ref.name = "certification_contract";
    ref.type = Reference.TYPE_EXISTING;
    ref.addMatchingItem(new Contract());
    c3.getRevoking().get(0).getReferences().put(ref.name, ref);
    // assertEquals(c3.getRevoking().get(0), c3.getPermissions().getFirst("revoke").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) ErrorRecord(com.icodici.universa.ErrorRecord) Test(org.junit.Test)

Example 38 with PrivateKey

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

the class PermissionsTest method shouldNotModifyStateDataValues.

@Test
public void shouldNotModifyStateDataValues() throws Exception {
    PrivateKey ownerKey2 = TestKeys.privateKey(1);
    Contract c = basicContractCreation(SUBSCRIPTION_WITH_DATA, PRIVATE_KEY, ownerKey2);
    Binder d = c.getStateData();
    Contract c1 = c.createRevision(ownerKey2);
    Binder d1 = c1.getStateData();
    final String oldValue = "1";
    final String newValue = "2";
    final String field = "option";
    setAndCheckOldNewValues(d, d1, oldValue, newValue, field);
    sealCheckTrace(c1, false);
}
Also used : Binder(net.sergeych.tools.Binder) PrivateKey(com.icodici.crypto.PrivateKey) Test(org.junit.Test)

Example 39 with PrivateKey

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

the class PermissionsTest method shouldPopulateWithEmptyStateDataValues.

@Test
public void shouldPopulateWithEmptyStateDataValues() throws Exception {
    PrivateKey ownerKey2 = TestKeys.privateKey(1);
    Contract c = basicContractCreation(SUBSCRIPTION_WITH_DATA, PRIVATE_KEY, ownerKey2);
    Binder d = c.getStateData();
    Contract c1 = c.createRevision(ownerKey2);
    Binder d1 = c1.getStateData();
    final String oldValue = "An example of smart contract.";
    final String newValue = "";
    final String field = "description";
    setAndCheckOldNewValues(d, d1, oldValue, newValue, field);
    sealCheckTrace(c1, true);
}
Also used : Binder(net.sergeych.tools.Binder) PrivateKey(com.icodici.crypto.PrivateKey) Test(org.junit.Test)

Example 40 with PrivateKey

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

the class PermissionsTest method shouldNotModifyDescEmptyStateDataValues.

@Test
public void shouldNotModifyDescEmptyStateDataValues() throws Exception {
    PrivateKey ownerKey2 = TestKeys.privateKey(1);
    Contract c = basicContractCreation(SUBSCRIPTION_WITH_DATA, PRIVATE_KEY, ownerKey2);
    Binder d = c.getStateData();
    Contract c1 = c.createRevision(ownerKey2);
    Binder d1 = c1.getStateData();
    final String oldValue = "An example of smart contract.";
    final String newValue = "wrong value.";
    final String field = "description";
    setAndCheckOldNewValues(d, d1, oldValue, newValue, field);
    sealCheckTrace(c1, false);
}
Also used : Binder(net.sergeych.tools.Binder) PrivateKey(com.icodici.crypto.PrivateKey) Test(org.junit.Test)

Aggregations

PrivateKey (com.icodici.crypto.PrivateKey)168 Test (org.junit.Test)122 PublicKey (com.icodici.crypto.PublicKey)76 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)29 Contract (com.icodici.universa.contract.Contract)24 Binder (net.sergeych.tools.Binder)22 IOException (java.io.IOException)16 HashSet (java.util.HashSet)14 ListRole (com.icodici.universa.contract.roles.ListRole)12 SQLException (java.sql.SQLException)11 Ignore (org.junit.Ignore)9 File (java.io.File)8 KeyAddress (com.icodici.crypto.KeyAddress)7 KeyRecord (com.icodici.universa.contract.KeyRecord)7 Role (com.icodici.universa.contract.roles.Role)7 Semaphore (java.util.concurrent.Semaphore)7 RoleLink (com.icodici.universa.contract.roles.RoleLink)5 TimeoutException (java.util.concurrent.TimeoutException)5 Decimal (com.icodici.universa.Decimal)4 HashId (com.icodici.universa.HashId)4