Search in sources :

Example 71 with PrivateKey

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

the class BaseNetworkTest method shouldApproveSplit.

// split and join section
@Test(timeout = 90000)
public void shouldApproveSplit() throws Exception {
    if (node == null) {
        System.out.println("network not inited");
        return;
    }
    PrivateKey key = new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey"));
    // 100
    Contract c = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    c.addSignerKey(key);
    assertTrue(c.check());
    c.seal();
    registerAndCheckApproved(c);
    // 100 - 30 = 70
    Contract c1 = ContractsService.createSplit(c, 30, "amount", new HashSet<PrivateKey>(Arrays.asList(key)));
    Contract c2 = c1.getNew().get(0);
    assertEquals("70", c1.getStateData().get("amount").toString());
    assertEquals("30", c2.getStateData().get("amount").toString());
    registerAndCheckApproved(c1);
    assertEquals("70", c1.getStateData().get("amount").toString());
    assertEquals("30", c2.getStateData().get("amount").toString());
    assertEquals(ItemState.REVOKED, node.waitItem(c.getId(), 5000).state);
    assertEquals(ItemState.APPROVED, node.waitItem(c1.getId(), 5000).state);
    assertEquals(ItemState.APPROVED, node.waitItem(c2.getId(), 5000).state);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) Test(org.junit.Test)

Example 72 with PrivateKey

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

the class BaseNetworkTest method splitSnatch2.

@Test
@Ignore("it is snatch test")
public void splitSnatch2() throws Exception {
    PrivateKey key = new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey"));
    Contract c1 = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    c1.addSignerKey(key);
    assertTrue(c1.check());
    c1.seal();
    registerAndCheckApproved(c1);
    // Contract c1copy = new Contract(c1.getLastSealedBinary());
    // Contract c1copy = new Contract(c1.getLastSealedBinary());
    System.out.println("true money origin: " + c1.getOrigin().toBase64String());
    Contract c1copy = new Contract(c1.getLastSealedBinary());
    System.out.println("money before split (c1): " + c1.getStateData().getIntOrThrow("amount"));
    c1 = c1.createRevision();
    Contract c2 = c1.splitValue("amount", new Decimal(60));
    System.out.println("money after split (c1): " + c1.getStateData().getIntOrThrow("amount"));
    System.out.println("money after split (c2): " + c2.getStateData().getIntOrThrow("amount"));
    System.out.println("check after split (c1.origin): " + c1.getOrigin().toBase64String());
    System.out.println("check after split (c2.origin): " + c2.getOrigin().toBase64String());
    ((Contract) c1.getRevokingItems().iterator().next()).getStateData().set("amount", 2000);
    c1.addSignerKey(key);
    c1.seal();
    c2.getStateData().set("amount", 1960);
    c2.addSignerKey(key);
    c2.seal();
    System.out.println("money after snatch (c2): " + c2.getStateData().getIntOrThrow("amount"));
    System.out.println("check after snatch (c2): " + c2.check());
    // registerAndCheckApproved(c1);
    // registerAndCheckApproved(c2);
    System.out.println("check after snatch (c1.origin): " + c1.getOrigin().toBase64String());
    System.out.println("check after snatch (c2.origin): " + c2.getOrigin().toBase64String());
    System.out.println("check after snatch (c1copy.origin): " + c1copy.getOrigin().toBase64String());
    registerAndCheckDeclined(c2);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 73 with PrivateKey

use of com.icodici.crypto.PrivateKey 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 74 with PrivateKey

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

the class BasicHttpServerTest method handshakeAndSecureCommand.

// @Test
// public void addEndpoint() throws Exception {
// BasicHttpServer s = new BasicHttpServer(null, 15600, 32, log);
// BasicHttpClient c = new BasicHttpClient("http://localhost:15600");
// BasicHttpClient.Answer a = c.request("ping", Binder.of("hello", "world"));
// System.out.println(":: "+a);
// s.shutdown();
// }
@Test
public void handshakeAndSecureCommand() throws Exception {
    PrivateKey nodeKey = TestKeys.privateKey(1);
    PrivateKey clientKey = TestKeys.privateKey(2);
    BasicHttpServer s = new BasicHttpServer(nodeKey, 15600, 32, log);
    BasicHttpClient c = new BasicHttpClient("http://localhost:15600");
    c.start(clientKey, nodeKey.getPublicKey(), null);
    Binder res = c.command("sping");
    assertEquals("spong", res.getStringOrThrow("sping"));
    s.addSecureEndpoint("getSessionInfo", (params, session) -> {
        // System.out.println("\t "+session.getPublicKey());
        return Binder.of("publicKey", session.getPublicKey().info().toString());
    });
    res = c.command("getSessionInfo");
    s.shutdown();
}
Also used : Binder(net.sergeych.tools.Binder) PrivateKey(com.icodici.crypto.PrivateKey) Test(org.junit.Test)

Example 75 with PrivateKey

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

the class UnikeyFactoryTest method testFromToUnikeyPublicMethods.

@Test
public void testFromToUnikeyPublicMethods() throws Exception {
    final PrivateKey pk1 = new PrivateKey(pk1Bytes);
    assertArrayEquals(pk1Bytes, pk1.pack());
    final PrivateKey pk2 = UnikeyFactory.fromUnikey(pk1Bytes);
    assertEquals(pk1, pk2);
    assertArrayEquals(pk1Bytes, UnikeyFactory.toUnikey(pk2));
}
Also used : 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