Search in sources :

Example 61 with Contract

use of com.icodici.universa.contract.Contract 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)

Example 62 with Contract

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

the class CLIMainTest method anonymizeAllRolesForTwoContracts.

@Test
public void anonymizeAllRolesForTwoContracts() throws Exception {
    callMain2("-create", rootPath + "TokenDSLTemplate.yml", "-name", basePath + "forRoleAnonymizing1.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey");
    callMain2("-create", rootPath + "TokenDSLTemplate.yml", "-name", basePath + "forRoleAnonymizing2.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey");
    assertTrue(new File(basePath + "forRoleAnonymizing1.unicon").exists());
    assertTrue(new File(basePath + "forRoleAnonymizing2.unicon").exists());
    callMain("-anonymize", basePath + "forRoleAnonymizing1.unicon", basePath + "forRoleAnonymizing2.unicon");
    assertTrue(new File(basePath + "forRoleAnonymizing1_anonymized.unicon").exists());
    assertTrue(new File(basePath + "forRoleAnonymizing2_anonymized.unicon").exists());
    System.out.println(output);
    PrivateKey key = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey"));
    PrivateKey ownerKey = new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey"));
    Contract contract1 = CLIMain.loadContract(basePath + "forRoleAnonymizing1_anonymized.unicon", true);
    assertFalse(contract1.getOwner().getKeys().contains(ownerKey.getPublicKey()));
    assertFalse(contract1.getIssuer().getKeys().contains(key.getPublicKey()));
    assertFalse(contract1.getCreator().getKeys().contains(key.getPublicKey()));
    Contract anonPublishedContract1 = new Contract(contract1.getLastSealedBinary());
    assertFalse(anonPublishedContract1.getOwner().getKeys().contains(ownerKey.getPublicKey()));
    assertFalse(anonPublishedContract1.getIssuer().getKeys().contains(key.getPublicKey()));
    assertFalse(anonPublishedContract1.getCreator().getKeys().contains(key.getPublicKey()));
    assertFalse(anonPublishedContract1.getSealedByKeys().contains(key.getPublicKey()));
    assertFalse(anonPublishedContract1.getSealedByKeys().contains(ownerKey.getPublicKey()));
    Contract contract2 = CLIMain.loadContract(basePath + "forRoleAnonymizing1_anonymized.unicon", true);
    assertFalse(contract2.getOwner().getKeys().contains(ownerKey.getPublicKey()));
    assertFalse(contract2.getIssuer().getKeys().contains(key.getPublicKey()));
    assertFalse(contract2.getCreator().getKeys().contains(key.getPublicKey()));
    Contract anonPublishedContract2 = new Contract(contract2.getLastSealedBinary());
    assertFalse(anonPublishedContract2.getOwner().getKeys().contains(ownerKey.getPublicKey()));
    assertFalse(anonPublishedContract2.getIssuer().getKeys().contains(key.getPublicKey()));
    assertFalse(anonPublishedContract2.getCreator().getKeys().contains(key.getPublicKey()));
    assertFalse(anonPublishedContract2.getSealedByKeys().contains(key.getPublicKey()));
    assertFalse(anonPublishedContract2.getSealedByKeys().contains(ownerKey.getPublicKey()));
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) File(java.io.File) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 63 with Contract

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

the class CLIMainTest method packContractWithCounterParts.

@Test
public void packContractWithCounterParts() throws Exception {
    String contractFileName = basePath + "coin1000.unicon";
    Contract contract = createCoin();
    contract.getStateData().set(FIELD_NAME, new Decimal(1000));
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    contract.seal();
    CLIMain.saveContract(contract, contractFileName);
    callMain2("--check", contractFileName, "-v");
    callMain2("-pack-with", contractFileName, "-add-sibling", basePath + "packedContract_new_item.unicon", "-add-revoke", basePath + "packedContract_revoke.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey", "-v");
    callMain("--check", contractFileName, "-v");
    System.out.println(output);
// assertEquals(0, errors.size());
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 64 with Contract

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

the class CLIMainTest method createRegisterTwoContractsCheckRevoke4.

@Test
public void createRegisterTwoContractsCheckRevoke4() throws Exception {
    String keyFileName = rootPath + "_xer0yfe2nn1xthc.private.unikey";
    callMain("-c", rootPath + "simple_root_contract_v2.yml", "-k", keyFileName, "-name", basePath + "simple_root_contract_v2.unicon");
    String contractFileName = basePath + "simple_root_contract_v2.unicon";
    assertTrue(new File(contractFileName).exists());
    assertEquals(0, errors.size());
    callMain("-c", rootPath + "another_root_contract_v2.yml", "-k", keyFileName, "-name", basePath + "another_root_contract_v2.unicon");
    String contractFileName2 = basePath + "another_root_contract_v2.unicon";
    assertTrue(new File(contractFileName2).exists());
    assertEquals(0, errors.size());
    Contract c = Contract.fromSealedFile(contractFileName);
    System.out.println("first contract: " + c.getId());
    Contract c2 = Contract.fromSealedFile(contractFileName2);
    System.out.println("second contract: " + c.getId());
    callMain2("--register", "--verbose", contractFileName + "," + contractFileName2);
}
Also used : File(java.io.File) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 65 with Contract

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

the class CLIMainTest method registerContractWithPayment.

@Test
public void registerContractWithPayment() throws Exception {
    // Should register contracts and use -cost as key to print cost of processing it.
    Contract contract = createCoin();
    contract.getStateData().set(FIELD_NAME, new Decimal(100));
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    contract.seal();
    CLIMain.saveContract(contract, basePath + "contract_for_register_and_cost.unicon");
    System.out.println("--- get tu ---");
    String tuContract = getApprovedTUContract();
    System.out.println("--- registering contract (with processing cost print) ---");
    LogPrinter.showDebug(true);
    callMain("--register", basePath + "contract_for_register_and_cost.unicon", "--tu", tuContract, "-k", rootPath + "keys/stepan_mamontov.private.unikey", "-amount", "2", "-wait", "5000");
    System.out.println(output);
    assertTrue(output.indexOf("registering the paid contract " + contract.getId() + " from " + basePath + "contract_for_register_and_cost.unicon" + " for 2 TU") >= 0);
    assertTrue(output.indexOf("paid contract " + contract.getId() + " submitted with result: ItemResult<APPROVED") >= 0);
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Aggregations

Contract (com.icodici.universa.contract.Contract)131 Test (org.junit.Test)67 Decimal (com.icodici.universa.Decimal)31 PrivateKey (com.icodici.crypto.PrivateKey)24 File (java.io.File)16 AsyncEvent (net.sergeych.tools.AsyncEvent)16 TimeoutException (java.util.concurrent.TimeoutException)14 Binder (net.sergeych.tools.Binder)14 KeyRecord (com.icodici.universa.contract.KeyRecord)13 HashSet (java.util.HashSet)9 Parcel (com.icodici.universa.contract.Parcel)8 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)7 Quantiser (com.icodici.universa.node2.Quantiser)7 PublicKey (com.icodici.crypto.PublicKey)6 TransactionPack (com.icodici.universa.contract.TransactionPack)6 IOException (java.io.IOException)6 BackingStoreException (java.util.prefs.BackingStoreException)6 OptionException (joptsimple.OptionException)6 HashId (com.icodici.universa.HashId)5 Arrays.asList (java.util.Arrays.asList)5