Search in sources :

Example 11 with KeyRecord

use of com.icodici.universa.contract.KeyRecord in project universa by UniversaBlockchain.

the class SimpleRoleTest method serializeMany.

@Test
public void serializeMany() throws Exception {
    SimpleRole sr = new SimpleRole("tr1");
    keys.forEach(k -> sr.addKeyRecord(new KeyRecord(k.getPublicKey())));
    Binder serialized = DefaultBiMapper.serialize(sr);
    Role r1 = DefaultBiMapper.deserialize(serialized);
    assertEquals(sr, r1);
    Set<PublicKey> kk = r1.getKeys();
    keys.forEach(k -> assertTrue(kk.contains(k.getPublicKey())));
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) Binder(net.sergeych.tools.Binder) PublicKey(com.icodici.crypto.PublicKey) Test(org.junit.Test)

Example 12 with KeyRecord

use of com.icodici.universa.contract.KeyRecord in project universa by UniversaBlockchain.

the class CLIMainTest method revokeContractVirtual.

@Test
public void revokeContractVirtual() throws Exception {
    Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
    c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
    c.addSignerKeyFromFile(rootPath + "keys/tu_key.private.unikey");
    PrivateKey goodKey = c.getKeysToSignWith().iterator().next();
    // let's make this key among owners
    ((SimpleRole) c.getRole("owner")).addKeyRecord(new KeyRecord(goodKey.getPublicKey()));
    c.seal();
    System.out.println("---");
    System.out.println("register contract");
    System.out.println("---");
    CLIMain.registerContract(c);
    Thread.sleep(1500);
    System.out.println("---");
    System.out.println("check contract");
    System.out.println("---");
    callMain("--probe", c.getId().toBase64String());
    System.out.println(output);
    assertTrue(output.indexOf(ItemState.APPROVED.name()) >= 0);
    PrivateKey issuer1 = TestKeys.privateKey(1);
    Contract tc = ContractsService.createRevocation(c, issuer1, goodKey);
    assertTrue(tc.check());
    System.out.println("---");
    System.out.println("register revoking contract");
    System.out.println("---");
    CLIMain.registerContract(tc);
    Thread.sleep(2500);
    System.out.println("---");
    System.out.println("check revoking contract");
    System.out.println("---");
    callMain("--probe", tc.getId().toBase64String());
    System.out.println(output);
    assertTrue(output.indexOf(ItemState.APPROVED.name()) >= 1);
    Thread.sleep(1500);
    System.out.println("---");
    System.out.println("check contract");
    System.out.println("---");
    callMain("--probe", c.getId().toBase64String());
    System.out.println(output);
    assertTrue(output.indexOf(ItemState.REVOKED.name()) >= 0);
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 13 with KeyRecord

use of com.icodici.universa.contract.KeyRecord in project universa by UniversaBlockchain.

the class CLIMainTest method registerManyContractsFromVariousNodes.

// 
// @Test
// public void registerManyContracts() throws Exception {
// 
// int numContracts = 100;
// List<Contract> contracts = new ArrayList<>();
// 
// for (int i = 0; i < numContracts; i++) {
// Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
// c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
// PrivateKey goodKey = c.getKeysToSignWith().iterator().next();
// // let's make this key among owners
// ((SimpleRole) c.getRole("owner")).addKeyRecord(new KeyRecord(goodKey.getPublicKey()));
// c.seal();
// 
// contracts.add(c);
// }
// 
// Thread.sleep(500);
// 
// for (int i = 0; i < numContracts; i++) {
// 
// System.out.println("---");
// System.out.println("register contract " + i);
// System.out.println("---");
// final Contract contract = contracts.get(i);
// Thread thread = new Thread(() -> {
// try {
// System.out.println("register contract -> run thread");
// CLIMain.registerContract(contract);
// } catch (IOException e) {
// e.printStackTrace();
// }
// });
// 
// thread.start();
// }
// 
// Thread.sleep(30000);
// 
// for (int i = 0; i < numContracts; i++) {
// System.out.println("---");
// System.out.println("check contract " + i);
// System.out.println("---");
// 
// final Contract contract = contracts.get(i);
// Thread thread = new Thread(() -> {
// System.out.println("check contract -> run thread");
// try {
// callMain2("--probe", contract.getId().toBase64String());
// } catch (IOException e) {
// e.printStackTrace();
// } catch (Exception e) {
// e.printStackTrace();
// }
// });
// 
// thread.start();
// }
// 
// Thread.sleep(30000);
// 
// System.out.println("---");
// System.out.println("check contracts in order");
// System.out.println("---");
// for (int i = 0; i < numContracts; i++) {
// 
// final Contract contract = contracts.get(i);
// try {
// callMain2("--probe", contract.getId().toBase64String());
// } catch (IOException e) {
// e.printStackTrace();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// 
// assertEquals(0, CLIMain.getReporter().getErrors().size());
// }
@Test
public void registerManyContractsFromVariousNodes() throws Exception {
    ClientNetwork clientNetwork1 = new ClientNetwork("http://localhost:8080", CLIMain.getPrivateKey(), null);
    ClientNetwork clientNetwork2 = new ClientNetwork("http://localhost:6002", CLIMain.getPrivateKey(), null);
    ClientNetwork clientNetwork3 = new ClientNetwork("http://localhost:6004", CLIMain.getPrivateKey(), null);
    int numContracts = 10;
    List<Contract> contracts = new ArrayList<>();
    for (int i = 0; i < numContracts; i++) {
        Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
        c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
        PrivateKey goodKey = c.getKeysToSignWith().iterator().next();
        // let's make this key among owners
        ((SimpleRole) c.getRole("owner")).addKeyRecord(new KeyRecord(goodKey.getPublicKey()));
        c.seal();
        contracts.add(c);
    }
    Thread.sleep(500);
    for (int i = 0; i < numContracts; i++) {
        // System.out.println("---");
        // System.out.println("register contract " + i);
        // System.out.println("---");
        final Contract contract = contracts.get(i);
        Thread thread1 = new Thread(() -> {
            try {
                // System.out.println("register contract on the client 1 -> run thread");
                CLIMain.registerContract(contract);
                ItemResult r1 = clientNetwork1.register(contract.getPackedTransaction(), 50);
            // System.out.println("register contract on the client 1 -> result: " + r1.toString());
            } catch (IOException e) {
                if (e.getCause() instanceof SocketTimeoutException) {
                    System.err.println(">>>> ERROR 1: " + e.getMessage());
                } else if (e.getCause() instanceof ConnectException) {
                    System.err.println(">>>> ERROR 1: " + e.getMessage());
                } else if (e.getCause() instanceof IllegalStateException) {
                    System.err.println(">>>> ERROR 1: " + e.getMessage());
                } else {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();
        Thread thread2 = new Thread(() -> {
            try {
                // System.out.println("register contracz on the client 2 -> run thread");
                CLIMain.registerContract(contract);
                ItemResult r2 = clientNetwork2.register(contract.getPackedTransaction(), 50);
            // System.out.println("register contract on the client 2 -> result: " + r2.toString());
            } catch (IOException e) {
                if (e.getCause() instanceof SocketTimeoutException) {
                    System.err.println(">>>> ERROR 2: " + e.getMessage());
                } else if (e.getCause() instanceof ConnectException) {
                    System.err.println(">>>> ERROR 2: " + e.getMessage());
                } else if (e.getCause() instanceof IllegalStateException) {
                    System.err.println(">>>> ERROR 2: " + e.getMessage());
                } else {
                    e.printStackTrace();
                }
            }
        });
        thread2.start();
        Thread thread3 = new Thread(() -> {
            try {
                // System.out.println("register contract on the client 3 -> run thread");
                CLIMain.registerContract(contract);
                ItemResult r3 = clientNetwork3.register(contract.getPackedTransaction(), 50);
            // System.out.println("register contract on the client 3 -> result: " + r3.toString());
            } catch (IOException e) {
                if (e.getCause() instanceof SocketTimeoutException) {
                    System.err.println(">>>> ERROR 3: " + e.getMessage());
                } else if (e.getCause() instanceof ConnectException) {
                    System.err.println(">>>> ERROR 3: " + e.getMessage());
                } else if (e.getCause() instanceof IllegalStateException) {
                    System.err.println(">>>> ERROR 3: " + e.getMessage());
                } else {
                    e.printStackTrace();
                }
            }
        });
        thread3.start();
    }
    Thread.sleep(1000);
    System.out.println("---");
    System.out.println("check contracts in order");
    System.out.println("---");
    for (int i = 0; i < numContracts; i++) {
        final Contract contract = contracts.get(i);
        callMain2("--probe", contract.getId().toBase64String());
    }
    assertEquals(0, CLIMain.getReporter().getErrors().size());
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) IOException(java.io.IOException) KeyRecord(com.icodici.universa.contract.KeyRecord) SocketTimeoutException(java.net.SocketTimeoutException) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) ItemResult(com.icodici.universa.node.ItemResult) Contract(com.icodici.universa.contract.Contract) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 14 with KeyRecord

use of com.icodici.universa.contract.KeyRecord in project universa by UniversaBlockchain.

the class CLIMainTest method registerContractFromVariousNetworks.

@Test
public void registerContractFromVariousNetworks() throws Exception {
    final Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
    c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
    PrivateKey goodKey = c.getKeysToSignWith().iterator().next();
    // let's make this key among owners
    ((SimpleRole) c.getRole("owner")).addKeyRecord(new KeyRecord(goodKey.getPublicKey()));
    c.seal();
    // CLIMain.registerContract(c);
    List<ClientNetwork> clientNetworks = new ArrayList<>();
    int numConnections = 10;
    for (int i = 0; i < numConnections; i++) {
        clientNetworks.add(new ClientNetwork("http://localhost:8080", new PrivateKey(2048), null));
    }
    for (int i = 0; i < numConnections; i++) {
        final int index = i;
        try {
            clientNetworks.get(index).ping();
        // System.out.println("result (" + index + "): " + r1.toString());
        } catch (IOException e) {
            if (e.getCause() instanceof SocketTimeoutException) {
                System.err.println(">>>> ERROR 1: " + e.getMessage());
            } else if (e.getCause() instanceof ConnectException) {
                System.err.println(">>>> ERROR 1: " + e.getMessage());
            } else if (e.getCause() instanceof IllegalStateException) {
                System.err.println(">>>> ERROR 1: " + e.getMessage());
            } else {
                e.printStackTrace();
            }
        }
    }
    for (int i = 0; i < numConnections; i++) {
        final int index = i;
        Thread thread1 = new Thread(() -> {
            try {
                ItemResult r1 = clientNetworks.get(index).register(c.getPackedTransaction());
                System.out.println("result from thread (" + index + "): " + r1.toString());
            } catch (IOException e) {
                if (e.getCause() instanceof SocketTimeoutException) {
                    System.err.println(">>>> ERROR 1: " + e.getMessage());
                } else if (e.getCause() instanceof ConnectException) {
                    System.err.println(">>>> ERROR 1: " + e.getMessage());
                } else if (e.getCause() instanceof IllegalStateException) {
                    System.err.println(">>>> ERROR 1: " + e.getMessage());
                } else {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();
    }
// Thread.sleep(10000);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) IOException(java.io.IOException) KeyRecord(com.icodici.universa.contract.KeyRecord) SocketTimeoutException(java.net.SocketTimeoutException) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) ItemResult(com.icodici.universa.node.ItemResult) Contract(com.icodici.universa.contract.Contract) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 15 with KeyRecord

use of com.icodici.universa.contract.KeyRecord in project universa by UniversaBlockchain.

the class CLIMainTest method prepareRoot.

@BeforeClass
public static void prepareRoot() throws Exception {
    createLocalNetwork();
    ownerKey1 = TestKeys.privateKey(3);
    ownerKey2 = TestKeys.privateKey(1);
    ownerKey3 = TestKeys.privateKey(2);
    // new File(rootPath + "/simple_root_contract.unicon").delete();
    assertTrue(new File(rootPath + "/simple_root_contract.yml").exists());
    assertTrue(new File(rootPath + "/simple_root_contract_v2.yml").exists());
    CLIMain.setTestMode();
    CLIMain.setTestRootPath(rootPath);
    CLIMain.setNodeUrl("http://localhost:8080");
    File file = new File(basePath);
    if (!file.exists()) {
        file.mkdir();
    }
    ZonedDateTime zdt = ZonedDateTime.now().plusHours(1);
    String field = "definition.expires_at";
    String value = "definition.expires_at:\n" + "    seconds: " + zdt.toEpochSecond() + "\n" + "    __type: unixtime";
    callMain("-c", "-v", rootPath + "simple_root_contract_v2.yml", "-name", basePath + "contract1.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey", "-set", field, "-value", value);
    callMain("-c", rootPath + "simple_root_contract_v2.yml", "-name", basePath + "contract2.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey", "-set", field, "-value", value);
    callMain("-c", rootPath + "simple_root_contract_v2.yml", "-name", basePath + "contract3.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey", "-set", field, "-value", value);
    callMain("-c", rootPath + "simple_root_contract_v2.yml", "-name", basePath + "contract_to_export.unicon", "-set", field, "-value", value);
    Contract c1 = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
    c1.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
    PrivateKey goodKey1 = c1.getKeysToSignWith().iterator().next();
    // let's make this key among owners
    ((SimpleRole) c1.getRole("owner")).addKeyRecord(new KeyRecord(goodKey1.getPublicKey()));
    c1.seal();
    CLIMain.saveContract(c1, basePath + "contract_for_revoke1.unicon");
    Contract c2 = Contract.fromDslFile(rootPath + "another_root_contract_v2.yml");
    c2.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
    PrivateKey goodKey2 = c2.getKeysToSignWith().iterator().next();
    // let's make this key among owners
    ((SimpleRole) c2.getRole("owner")).addKeyRecord(new KeyRecord(goodKey2.getPublicKey()));
    c2.seal();
    CLIMain.saveContract(c2, basePath + "contract_for_revoke2.unicon");
    Contract c3 = Contract.fromDslFile(rootPath + "simple_root_contract_v2.yml");
    c3.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
    PrivateKey goodKey3 = c3.getKeysToSignWith().iterator().next();
    // let's make this key among owners
    ((SimpleRole) c3.getRole("owner")).addKeyRecord(new KeyRecord(goodKey3.getPublicKey()));
    c3.seal();
    CLIMain.saveContract(c3, basePath + "contract_for_revoke3.unicon");
    callMain("-e", basePath + "contract1.unicon", "-name", basePath + "contract_to_import.json");
    callMain("-e", basePath + "contract1.unicon", "-name", basePath + "contract_to_import.xml");
    callMain("-e", basePath + "contract1.unicon", "-name", basePath + "contract_to_import.XML");
    callMain("-e", basePath + "contract1.unicon", "-name", basePath + "contract_to_import.yaml");
    callMain("-i", basePath + "contract_to_import.json", "-name", basePath + "not_signed_contract.unicon");
    Path path = Paths.get(rootPath + "packedContract.unicon");
    byte[] data = Files.readAllBytes(path);
    Set<PrivateKey> keys = new HashSet<>();
    keys.add(new PrivateKey(Do.read(PRIVATE_KEY_PATH)));
    Contract contract = createCoin100apiv3();
    contract.addSignerKey(keys.iterator().next());
    contract.seal();
    CLIMain.saveContract(contract, basePath + "packedContract.unicon");
    callMain("--register", basePath + "packedContract.unicon", "--wait", "5000");
    Contract packedContract = ContractsService.createSplit(contract, 1, FIELD_NAME, keys);
    packedContract.addSignerKey(keys.iterator().next());
    packedContract.seal();
    CLIMain.saveContract(packedContract, basePath + "packedContract.unicon", true, true);
    // try (FileOutputStream fs = new FileOutputStream(basePath + "packedContract.unicon")) {
    // fs.write(data);
    // fs.close();
    // }
    path = Paths.get(rootPath + "packedContract.unicon");
    data = Files.readAllBytes(path);
    try (FileOutputStream fs = new FileOutputStream(basePath + "packedContract2.unicon")) {
        fs.write(data);
        fs.close();
    }
    path = Paths.get(rootPath + "packedContract_new_item.unicon");
    data = Files.readAllBytes(path);
    try (FileOutputStream fs = new FileOutputStream(basePath + "packedContract_new_item.unicon")) {
        fs.write(data);
        fs.close();
    }
    path = Paths.get(rootPath + "packedContract_revoke.unicon");
    data = Files.readAllBytes(path);
    try (FileOutputStream fs = new FileOutputStream(basePath + "packedContract_revoke.unicon")) {
        fs.write(data);
        fs.close();
    }
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) Path(java.nio.file.Path) PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) ZonedDateTime(java.time.ZonedDateTime) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Contract(com.icodici.universa.contract.Contract) BeforeClass(org.junit.BeforeClass)

Aggregations

KeyRecord (com.icodici.universa.contract.KeyRecord)24 Test (org.junit.Test)21 Contract (com.icodici.universa.contract.Contract)13 Binder (net.sergeych.tools.Binder)10 PrivateKey (com.icodici.crypto.PrivateKey)7 HashSet (java.util.HashSet)7 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)6 AbstractKey (com.icodici.crypto.AbstractKey)4 PublicKey (com.icodici.crypto.PublicKey)3 KeyAddress (com.icodici.crypto.KeyAddress)2 AnonymousId (com.icodici.universa.contract.AnonymousId)2 ItemResult (com.icodici.universa.node.ItemResult)2 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Path (java.nio.file.Path)1 ZonedDateTime (java.time.ZonedDateTime)1 BeforeClass (org.junit.BeforeClass)1