Search in sources :

Example 46 with PrivateKey

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

the class BaseNetworkTest method swapSplitJoinAllGood.

@Test(timeout = 60000)
public void swapSplitJoinAllGood() throws Exception {
    if (node == null) {
        System.out.println("network not inited");
        return;
    }
    Set<PrivateKey> martyPrivateKeys = new HashSet<>();
    Set<PublicKey> martyPublicKeys = new HashSet<>();
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    martyPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/marty_mcfly.private.unikey")));
    for (PrivateKey pk : martyPrivateKeys) {
        martyPublicKeys.add(pk.getPublicKey());
    }
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    Contract martyCoins = Contract.fromDslFile(ROOT_PATH + "martyCoins.yml");
    martyCoins.addSignerKey(martyPrivateKeys.iterator().next());
    martyCoins.seal();
    martyCoins.check();
    martyCoins.traceErrors();
    registerAndCheckApproved(martyCoins);
    Contract stepaCoins = Contract.fromDslFile(ROOT_PATH + "stepaCoins.yml");
    stepaCoins.addSignerKey(stepaPrivateKeys.iterator().next());
    stepaCoins.seal();
    stepaCoins.check();
    stepaCoins.traceErrors();
    registerAndCheckApproved(stepaCoins);
    System.out.println("--- coins created ---");
    // 100 - 30 = 70
    Contract martyCoinsSplit = ContractsService.createSplit(martyCoins, 30, "amount", martyPrivateKeys);
    Contract martyCoinsSplitToStepa = martyCoinsSplit.getNew().get(0);
    Contract stepaCoinsSplit = ContractsService.createSplit(stepaCoins, 30, "amount", stepaPrivateKeys);
    Contract stepaCoinsSplitToMarty = stepaCoinsSplit.getNew().get(0);
    martyCoinsSplitToStepa.check();
    martyCoinsSplitToStepa.traceErrors();
    stepaCoinsSplitToMarty.check();
    stepaCoinsSplitToMarty.traceErrors();
    // register swapped contracts using ContractsService
    System.out.println("--- register swapped contracts using ContractsService ---");
    Contract swapContract;
    swapContract = ContractsService.startSwap(martyCoinsSplitToStepa, stepaCoinsSplitToMarty, martyPrivateKeys, stepaPublicKeys, false);
    ContractsService.signPresentedSwap(swapContract, stepaPrivateKeys);
    ContractsService.finishSwap(swapContract, martyPrivateKeys);
    swapContract.getNewItems().clear();
    swapContract.addNewItems(martyCoinsSplit, stepaCoinsSplit);
    swapContract.seal();
    swapContract.addSignatureToSeal(martyPrivateKeys);
    swapContract.check();
    swapContract.traceErrors();
    System.out.println("Transaction contract for swapping is valid: " + swapContract.isOk());
    registerAndCheckApproved(swapContract);
    assertEquals(ItemState.APPROVED, node.waitItem(martyCoinsSplit.getId(), 5000).state);
    assertEquals(ItemState.APPROVED, node.waitItem(stepaCoinsSplit.getId(), 5000).state);
    assertEquals(ItemState.APPROVED, node.waitItem(martyCoinsSplitToStepa.getId(), 5000).state);
    assertEquals(ItemState.APPROVED, node.waitItem(stepaCoinsSplitToMarty.getId(), 5000).state);
    assertEquals(ItemState.REVOKED, node.waitItem(martyCoins.getId(), 5000).state);
    assertEquals(ItemState.REVOKED, node.waitItem(stepaCoins.getId(), 5000).state);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Test(org.junit.Test)

Example 47 with PrivateKey

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

the class BaseNetworkTest method checkCreateTwoSignedContractAllGood.

private void checkCreateTwoSignedContractAllGood(boolean newRev) throws Exception {
    if (node == null) {
        System.out.println("network not inited");
        return;
    }
    Set<PrivateKey> martyPrivateKeys = new HashSet<>();
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    martyPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/marty_mcfly.private.unikey")));
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    for (PrivateKey pk : stepaPrivateKeys) stepaPublicKeys.add(pk.getPublicKey());
    Contract baseContract = Contract.fromDslFile(ROOT_PATH + "DeLoreanOwnership.yml");
    PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey"));
    baseContract.addSignerKey(manufacturePrivateKey);
    baseContract.seal();
    System.out.println("Base contract contract is valid: " + baseContract.isOk());
    if (newRev)
        registerAndCheckApproved(baseContract);
    Contract twoSignContract = ContractsService.createTwoSignedContract(baseContract, martyPrivateKeys, stepaPublicKeys, newRev);
    twoSignContract = imitateSendingTransactionToPartner(twoSignContract);
    twoSignContract.addSignatureToSeal(stepaPrivateKeys);
    twoSignContract.check();
    twoSignContract.traceErrors();
    registerAndCheckDeclined(twoSignContract);
    twoSignContract = imitateSendingTransactionToPartner(twoSignContract);
    twoSignContract.addSignatureToSeal(martyPrivateKeys);
    twoSignContract.check();
    twoSignContract.traceErrors();
    System.out.println("Contract with two signature is valid: " + twoSignContract.isOk());
    registerAndCheckApproved(twoSignContract);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey)

Example 48 with PrivateKey

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

the class BaseNetworkTest method splitSnatch.

@Test
@Ignore("it is snatch test")
public void splitSnatch() 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());
    System.out.println("money before split (c1): " + c1.getStateData().getIntOrThrow("amount"));
    Contract c2 = c1.splitValue("amount", new Decimal(50));
    System.out.println("money after split (c1): " + c1.getStateData().getIntOrThrow("amount"));
    System.out.println("money after split (c2): " + c2.getStateData().getIntOrThrow("amount"));
    // c2.addRevokingItems(c1copy);
    // 150);
    c2.getStateData().set("amount", 9000);
    c2.seal();
    System.out.println("money after snatch (c2): " + c2.getStateData().getIntOrThrow("amount"));
    System.out.println("check after snatch (c2): " + c2.check());
    registerAndCheckDeclined(c2);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 49 with PrivateKey

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

the class BaseNetworkTest method declineParcelWithTestPaymentButTooFatherExpiration.

@Test(timeout = 90000)
public void declineParcelWithTestPaymentButTooFatherExpiration() 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.setExpiresAt(ZonedDateTime.now().plusYears(2));
    stepaCoins.seal();
    stepaCoins.check();
    stepaCoins.traceErrors();
    assertTrue(stepaCoins.getExpiresAt().isAfter(ZonedDateTime.now().plusYears(1)));
    PrivateKey ownerKey = new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey"));
    Set<PublicKey> keys = new HashSet();
    keys.add(ownerKey.getPublicKey());
    Contract stepaTU = InnerContractsService.createFreshTU(100, keys, true);
    stepaTU.check();
    // stepaTU.setIsTU(true);
    stepaTU.traceErrors();
    node.registerItem(stepaTU);
    ItemResult itemResult = node.waitItem(stepaTU.getId(), 18000);
    assertEquals(ItemState.APPROVED, itemResult.state);
    Parcel parcel = ContractsService.createParcel(stepaCoins, stepaTU, 1, stepaPrivateKeys, true);
    parcel.getPayment().getContract().paymentCheck(config.getTransactionUnitsIssuerKey());
    parcel.getPayment().getContract().traceErrors();
    parcel.getPayload().getContract().check();
    parcel.getPayload().getContract().traceErrors();
    assertEquals(100, parcel.getPaymentContract().getStateData().getIntOrThrow("transaction_units"));
    assertEquals(10000 - 1, parcel.getPaymentContract().getStateData().getIntOrThrow("test_transaction_units"));
    assertTrue(parcel.getPaymentContract().isOk());
    assertTrue(parcel.getPaymentContract().isLimitedForTestnet());
    assertFalse(parcel.getPayloadContract().isOk());
    assertTrue(parcel.getPayloadContract().isLimitedForTestnet());
    System.out.println("Parcel: " + parcel.getId());
    System.out.println("Payment contract: " + parcel.getPaymentContract().getId() + " is TU: " + parcel.getPaymentContract().isTU(config.getTransactionUnitsIssuerKey(), config.getTUIssuerName()));
    System.out.println("Payload contract: " + parcel.getPayloadContract().getId() + " is TU: " + parcel.getPayloadContract().isTU(config.getTransactionUnitsIssuerKey(), config.getTUIssuerName()));
    // LogPrinter.showDebug(true);
    node.registerParcel(parcel);
    // wait parcel
    node.waitParcel(parcel.getId(), 8000);
    // check payment and payload contracts
    assertEquals(ItemState.APPROVED, node.waitItem(parcel.getPayment().getContract().getId(), 8000).state);
    assertEquals(ItemState.DECLINED, node.waitItem(parcel.getPayload().getContract().getId(), 8000).state);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Test(org.junit.Test)

Example 50 with PrivateKey

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

the class BaseNetworkTest method referenceForSplitJoin.

@Test
public void referenceForSplitJoin() throws Exception {
    // You have a notary dsl with llc's property
    // and only owner of trusted manager's contract can chamge the owner of property
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PrivateKey> llcPrivateKeys = new HashSet<>();
    Set<PrivateKey> thirdPartyPrivateKeys = new HashSet<>();
    llcPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey")));
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    thirdPartyPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/marty_mcfly.private.unikey")));
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    Set<PublicKey> thirdPartyPublicKeys = new HashSet<>();
    for (PrivateKey pk : thirdPartyPrivateKeys) {
        thirdPartyPublicKeys.add(pk.getPublicKey());
    }
    Contract jobCertificate = new Contract(llcPrivateKeys.iterator().next());
    jobCertificate.setOwnerKeys(stepaPublicKeys);
    jobCertificate.getDefinition().getData().set("issuer", "Roga & Kopita");
    jobCertificate.getDefinition().getData().set("type", "chief accountant assignment");
    jobCertificate.seal();
    registerAndCheckApproved(jobCertificate);
    Contract llcProperty = Contract.fromDslFile(ROOT_PATH + "TokenWithReferenceDSLTemplate.yml");
    llcProperty.addSignerKey(llcPrivateKeys.iterator().next());
    llcProperty.seal();
    registerAndCheckApproved(llcProperty);
    Contract llcProperty2 = ContractsService.createSplit(llcProperty, 100, "amount", stepaPrivateKeys, true);
    // llcProperty2.createRole("creator", llcProperty2.getRole("owner"));
    // llcProperty2.getNew().get(0).createRole("creator", llcProperty2.getNew().get(0).getRole("owner"));
    llcProperty2.check();
    llcProperty2.traceErrors();
    assertFalse(llcProperty2.isOk());
    TransactionPack tp_before = llcProperty2.getTransactionPack();
    tp_before.addReferencedItem(jobCertificate);
    byte[] data = tp_before.pack();
    // here we "send" data and "got" it
    TransactionPack tp_after = TransactionPack.unpack(data);
    registerAndCheckApproved(tp_after);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) 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