Search in sources :

Example 51 with Contract

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

the class CLIMainTest method createRegisterTwoContractsCheckRevoke1.

@Test
public void createRegisterTwoContractsCheckRevoke1() 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", contractFileName, contractFileName2, "--verbose");
}
Also used : File(java.io.File) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 52 with Contract

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

the class CLIMainTest method revokeTwoContracts.

@Test
public void revokeTwoContracts() throws Exception {
    String contractFileName1 = basePath + "contract_for_revoke1.unicon";
    String contractFileName2 = basePath + "contract_for_revoke2.unicon";
    System.out.println("---");
    System.out.println("register contracts");
    System.out.println("---");
    String tuContract = getApprovedTUContract();
    callMain2("--register", contractFileName1, contractFileName2, "--verbose", "--tu", tuContract, "--k", rootPath + "keys/stepan_mamontov.private.unikey");
    Thread.sleep(1500);
    System.out.println("---");
    System.out.println("check tu");
    System.out.println("---");
    tuContract = getApprovedTUContract();
    Contract tu = CLIMain.loadContract(tuContract);
    System.out.println("check tu " + tu.getId().toBase64String());
    callMain2("--probe", tu.getId().toBase64String(), "--verbose");
    System.out.println("---");
    System.out.println("revoke contracts");
    System.out.println("---");
    callMain2("-revoke", contractFileName1, contractFileName2, "-v", "--tu", tuContract, "-k", rootPath + "keys/stepan_mamontov.private.unikey", "-k", PRIVATE_KEY_PATH);
    Thread.sleep(1500);
    System.out.println("---");
    System.out.println("check contracts after revoke");
    System.out.println("---");
    Contract c1 = CLIMain.loadContract(contractFileName1);
    callMain2("--probe", c1.getId().toBase64String(), "--verbose");
    Contract c2 = CLIMain.loadContract(contractFileName2);
    callMain("--probe", c2.getId().toBase64String(), "--verbose");
    Contract tu2 = CLIMain.loadContract(tuContract);
    System.out.println("check tu " + tu2.getId().toBase64String());
    callMain2("--probe", tu2.getId().toBase64String(), "--verbose");
    System.out.println(output);
    assertEquals(0, errors.size());
    assertTrue(output.indexOf(ItemState.REVOKED.name()) >= 1);
}
Also used : Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 53 with Contract

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

the class CLIMainTest method swapManyContractsViaTransactionAllGood.

// @Test
public void swapManyContractsViaTransactionAllGood() throws Exception {
    Set<PrivateKey> martyPrivateKeys = new HashSet<>();
    Set<PublicKey> martyPublicKeys = new HashSet<>();
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    Contract delorean1 = Contract.fromDslFile(rootPath + "DeLoreanOwnership.yml");
    Contract delorean2 = Contract.fromDslFile(rootPath + "DeLoreanOwnership.yml");
    Contract delorean3 = Contract.fromDslFile(rootPath + "DeLoreanOwnership.yml");
    List<Contract> deloreans = new ArrayList<>();
    deloreans.add(delorean1);
    deloreans.add(delorean2);
    deloreans.add(delorean3);
    Contract lamborghini1 = Contract.fromDslFile(rootPath + "LamborghiniOwnership.yml");
    Contract lamborghini2 = Contract.fromDslFile(rootPath + "LamborghiniOwnership.yml");
    List<Contract> lamborghinis = new ArrayList<>();
    lamborghinis.add(lamborghini1);
    lamborghinis.add(lamborghini2);
    // ----- prepare contracts -----------
    martyPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/marty_mcfly.private.unikey")));
    for (PrivateKey pk : martyPrivateKeys) {
        martyPublicKeys.add(pk.getPublicKey());
    }
    stepaPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")));
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey"));
    int i = 0;
    for (Contract d : deloreans) {
        i++;
        d.addSignerKey(manufacturePrivateKey);
        d.seal();
        CLIMain.saveContract(d, rootPath + "delorean" + i + ".unicon");
        callMain("--register", rootPath + "delorean" + i + ".unicon", "-wait", "5000");
    }
    i = 0;
    for (Contract l : lamborghinis) {
        i++;
        l.addSignerKey(manufacturePrivateKey);
        l.seal();
        CLIMain.saveContract(l, rootPath + "lamborghini" + i + ".unicon");
        callMain("--register", rootPath + "lamborghini" + i + ".unicon", "-wait", "5000");
    }
    // register swapped contracts using ContractsService
    System.out.println("--- register swapped contracts using ContractsService ---");
    Contract swapContract;
    // first Marty create transaction, add both contracts and swap owners, sign own new contract
    swapContract = ContractsService.startSwap(deloreans, lamborghinis, martyPrivateKeys, stepaPublicKeys);
    ContractsService.signPresentedSwap(swapContract, stepaPrivateKeys);
    ContractsService.finishSwap(swapContract, martyPrivateKeys);
    swapContract.check();
    swapContract.traceErrors();
    System.out.println("Transaction contract for swapping is valid: " + swapContract.isOk() + " num new contracts: " + swapContract.getNewItems().size());
    CLIMain.saveContract(swapContract, rootPath + "swapContract.unicon", true, true);
    callMain("--register", rootPath + "swapContract.unicon", "-wait", "5000");
    i = 0;
    for (Contract c : swapContract.getNew()) {
        i++;
        CLIMain.saveContract(c, rootPath + "new" + i + ".unicon");
        callMain("-e", rootPath + "new" + i + ".unicon", "-pretty");
    }
// checkSwapResultSuccess(swapContract, delorean, lamborghini, martyPublicKeys, stepaPublicKeys);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Contract(com.icodici.universa.contract.Contract)

Example 54 with Contract

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

the class CLIMainTest method anonymizeAllRoles.

@Test
public void anonymizeAllRoles() 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");
    assertTrue(new File(basePath + "forRoleAnonymizing_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 contract = CLIMain.loadContract(basePath + "forRoleAnonymizing_anonymized.unicon", true);
    assertFalse(contract.getOwner().getKeys().contains(ownerKey.getPublicKey()));
    assertFalse(contract.getIssuer().getKeys().contains(key.getPublicKey()));
    assertFalse(contract.getCreator().getKeys().contains(key.getPublicKey()));
    Contract anonPublishedContract = new Contract(contract.getLastSealedBinary());
    assertFalse(anonPublishedContract.getOwner().getKeys().contains(ownerKey.getPublicKey()));
    assertFalse(anonPublishedContract.getIssuer().getKeys().contains(key.getPublicKey()));
    assertFalse(anonPublishedContract.getCreator().getKeys().contains(key.getPublicKey()));
    assertFalse(anonPublishedContract.getSealedByKeys().contains(key.getPublicKey()));
    assertFalse(anonPublishedContract.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 55 with Contract

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

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