Search in sources :

Example 86 with PrivateKey

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

the class CLIMainTest method testExportImportWithAddresses.

@Test
public void testExportImportWithAddresses() throws Exception {
    callMain2("-create", rootPath + "simple_root_contract_v2.yml", "-name", basePath + "contractWithAddresses.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey");
    Contract contract = CLIMain.loadContract(basePath + "contractWithAddresses.unicon", true);
    Set<KeyAddress> keyAddresses = new HashSet<>();
    keyAddresses.add(new KeyAddress(TestKeys.publicKey(0), 0, true));
    SimpleRole sr1 = new SimpleRole("owner", keyAddresses);
    contract.registerRole(sr1);
    contract.addSignerKey(TestKeys.privateKey(0));
    contract.seal();
    CLIMain.saveContract(contract, basePath + "contractWithAddresses.unicon");
    callMain("-e", basePath + "contractWithAddresses.unicon", "-name", basePath + "contractWithAddresses.json");
    System.out.println(output);
    assertTrue(output.indexOf("export as json ok") >= 0);
    assertEquals(0, errors.size());
    callMain("-i", basePath + "contractWithAddresses.json", "-name", basePath + "contractWithAddressesImported.unicon");
    System.out.println(output);
    assertTrue(output.indexOf("import from json ok") >= 0);
    assertEquals(1, errors.size());
    if (errors.size() > 0) {
        assertEquals(Errors.NOT_SIGNED.name(), errors.get(0).get("code"));
    }
    Contract contractImported = CLIMain.loadContract(basePath + "contractWithAddressesImported.unicon", true);
    assertTrue(contractImported.getOwner().getKeyAddresses().iterator().next().isMatchingKey(TestKeys.privateKey(0).getPublicKey()));
    PrivateKey creatorPrivateKey = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey"));
    contractImported.addSignatureToSeal(creatorPrivateKey);
    contractImported.addSignatureToSeal(TestKeys.privateKey(0));
    assertTrue(contractImported.check());
    Set<PrivateKey> signKeys = new HashSet<>();
    signKeys.add(creatorPrivateKey);
    signKeys.add(TestKeys.privateKey(0));
    contractImported.setKeysToSignWith(signKeys);
    byte[] sealedContract = contractImported.sealAsV2();
    TransactionPack tp = new TransactionPack();
    tp.addKeys(creatorPrivateKey.getPublicKey());
    tp.addKeys(TestKeys.privateKey(0).getPublicKey());
    Contract restoredContract = new Contract(sealedContract, tp);
    assertTrue(restoredContract.check());
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) KeyAddress(com.icodici.crypto.KeyAddress) Contract(com.icodici.universa.contract.Contract) TransactionPack(com.icodici.universa.contract.TransactionPack) Test(org.junit.Test)

Example 87 with PrivateKey

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

the class CLIMainTest method anonymizeRole.

@Test
public void anonymizeRole() throws Exception {
    callMain2("-create", rootPath + "TokenDSLTemplate.yml", "-name", basePath + "forRoleAnonymizing.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey");
    assertTrue(new File(basePath + "forRoleAnonymizing.unicon").exists());
    callMain("-anonymize", basePath + "forRoleAnonymizing.unicon", "-role", "issuer");
    assertTrue(new File(basePath + "forRoleAnonymizing_anonymized.unicon").exists());
    System.out.println(output);
    PrivateKey key = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey"));
    Contract contract = CLIMain.loadContract(basePath + "forRoleAnonymizing_anonymized.unicon", true);
    assertFalse(contract.getIssuer().getKeys().contains(key.getPublicKey()));
    Contract anonPublishedContract = new Contract(contract.getLastSealedBinary());
    assertFalse(anonPublishedContract.getIssuer().getKeys().contains(key.getPublicKey()));
    assertFalse(anonPublishedContract.getSealedByKeys().contains(key.getPublicKey()));
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) File(java.io.File) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 88 with PrivateKey

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

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

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

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