Search in sources :

Example 6 with PublicKey

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

the class ContractsServiceTest method createTestTU.

@Test
public void createTestTU() throws Exception {
    PrivateKey privateKey = TestKeys.privateKey(3);
    Set<PublicKey> keys = new HashSet();
    keys.add(privateKey.getPublicKey());
    Contract tu = InnerContractsService.createFreshTU(100, keys, true);
    tu.check();
    tu.traceErrors();
    assertEquals(true, tu.getRole("owner").isAllowedForKeys(keys));
    assertEquals(100, tu.getStateData().getIntOrThrow("transaction_units"));
    assertEquals(10000, tu.getStateData().getIntOrThrow("test_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 7 with PublicKey

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

the class ContractsServiceTest method goodToken.

@Test
public void goodToken() 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.createTokenContract(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"), 0.01);
    assertEquals(splitJoinParams.get("min_unit"), 0.01);
    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 8 with PublicKey

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

the class ContractsServiceTest method goodNotary.

@Test
public void goodNotary() 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.createNotaryContract(martyPrivateKeys, stepaPublicKeys);
    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()));
    assertTrue(notaryContract.isPermitted("revoke", stepaPublicKeys));
    assertTrue(notaryContract.isPermitted("revoke", martyPublicKeys));
    assertTrue(notaryContract.isPermitted("change_owner", stepaPublicKeys));
    assertFalse(notaryContract.isPermitted("change_owner", martyPublicKeys));
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with PublicKey

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

the class ExtendedSignatureTest method extractPublicKey.

@Test
public void extractPublicKey() throws Exception {
    byte[] data = "Hello world".getBytes();
    PrivateKey k = TestKeys.privateKey(3);
    byte[] signature = ExtendedSignature.sign(k, data, true);
    PublicKey pubKey = k.getPublicKey();
    ExtendedSignature es = ExtendedSignature.verify(pubKey, signature, data);
    assertNotNull(es);
    assertAlmostSame(es.getCreatedAt(), ZonedDateTime.now());
    assertEquals(ExtendedSignature.keyId(k), ExtendedSignature.keyId(pubKey));
    assertEquals(ExtendedSignature.keyId(k), ExtendedSignature.extractKeyId(signature));
    assertNotNull(es.getPublicKey());
    assertEquals(pubKey, es.getPublicKey());
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Test(org.junit.Test)

Example 10 with PublicKey

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

the class ExtendedSignatureTest method sign.

@Test
public void sign() throws Exception {
    byte[] data = "Hello world".getBytes();
    PrivateKey k = TestKeys.privateKey(3);
    byte[] signature = ExtendedSignature.sign(k, data);
    PublicKey pubKey = k.getPublicKey();
    ExtendedSignature es = ExtendedSignature.verify(pubKey, signature, data);
    assertNotNull(es);
    assertAlmostSame(es.getCreatedAt(), ZonedDateTime.now());
    assertEquals(ExtendedSignature.keyId(k), ExtendedSignature.keyId(pubKey));
    assertEquals(ExtendedSignature.keyId(k), ExtendedSignature.extractKeyId(signature));
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) 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