Search in sources :

Example 21 with PublicKey

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

Example 22 with PublicKey

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

the class BaseNetworkTest method testLedgerLocks.

@Ignore("Stress test")
@Test(timeout = 900000)
public void testLedgerLocks() throws Exception {
    ExtendedSignatureTest.parallelize(Executors.newCachedThreadPool(), 4, () -> {
        try {
            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());
            }
            PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(ROOT_PATH + "keys/tu_key.private.unikey"));
            int N = 100;
            for (int i = 0; i < N; i++) {
                Contract stepaCoins = Contract.fromDslFile(ROOT_PATH + "stepaCoins.yml");
                stepaCoins.addSignerKey(stepaPrivateKeys.iterator().next());
                stepaCoins.seal();
                Parcel parcel = createParcelWithClassTU(stepaCoins, stepaPrivateKeys);
                synchronized (tuContractLock) {
                    tuContract = parcel.getPaymentContract();
                }
                System.out.println("-------------- register parcel " + parcel.getId() + " (iteration " + i + ") ------------");
                node.registerParcel(parcel);
                for (Node n : nodes) {
                    n.waitParcel(parcel.getId(), 15000);
                    ItemResult itemResult = n.waitItem(stepaCoins.getId(), 15000);
                }
                ItemState itemState1 = node.waitItem(parcel.getPaymentContract().getRevoking().get(0).getId(), 15000).state;
                ItemState itemState2 = node.getLedger().getRecord(parcel.getPaymentContract().getRevoking().get(0).getId()).getState();
                System.out.println("--- check item " + parcel.getPaymentContract().getRevoking().get(0).getId() + " --- iteration " + i);
                System.out.println("state from node: " + itemState1);
                System.out.println("state from ledger: " + itemState2);
                assertEquals(itemState1, itemState2);
                assertEquals(ItemState.REVOKED, itemState1);
                assertEquals(ItemState.REVOKED, itemState2);
            }
        } catch (Exception e) {
            System.out.println("exception: " + e.toString());
        }
    });
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) TimeoutException(java.util.concurrent.TimeoutException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 23 with PublicKey

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

the class BaseNetworkTest method swapManyContractsViaTransactionAllGood.

@Test(timeout = 90000)
public void swapManyContractsViaTransactionAllGood() 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<>();
    Contract delorean1 = Contract.fromDslFile(ROOT_PATH + "DeLoreanOwnership.yml");
    Contract delorean2 = Contract.fromDslFile(ROOT_PATH + "DeLoreanOwnership.yml");
    Contract delorean3 = Contract.fromDslFile(ROOT_PATH + "DeLoreanOwnership.yml");
    List<Contract> deloreans = new ArrayList<>();
    deloreans.add(delorean1);
    deloreans.add(delorean2);
    deloreans.add(delorean3);
    Contract lamborghini1 = Contract.fromDslFile(ROOT_PATH + "LamborghiniOwnership.yml");
    Contract lamborghini2 = Contract.fromDslFile(ROOT_PATH + "LamborghiniOwnership.yml");
    List<Contract> lamborghinis = new ArrayList<>();
    lamborghinis.add(lamborghini1);
    lamborghinis.add(lamborghini2);
    // ----- prepare contracts -----------
    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());
    }
    PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey"));
    for (Contract d : deloreans) {
        d.addSignerKey(manufacturePrivateKey);
        d.seal();
        registerAndCheckApproved(d);
    }
    for (Contract l : lamborghinis) {
        l.addSignerKey(manufacturePrivateKey);
        l.seal();
        registerAndCheckApproved(l);
    }
    // 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);
    // then Marty send new revisions to Stepa
    // and Stepa sign own new contract, Marty's new contract
    swapContract = imitateSendingTransactionToPartner(swapContract);
    ContractsService.signPresentedSwap(swapContract, stepaPrivateKeys);
    // then Stepa send draft transaction back to Marty
    // and Marty sign Stepa's new contract and send to approving
    swapContract = imitateSendingTransactionToPartner(swapContract);
    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());
    registerAndCheckApproved(swapContract);
    for (Contract d : deloreans) {
        assertEquals(ItemState.REVOKED, node.waitItem(d.getId(), 5000).state);
        System.out.println("delorean is " + node.waitItem(d.getId(), 5000).state);
    }
    for (Contract l : lamborghinis) {
        assertEquals(ItemState.REVOKED, node.waitItem(l.getId(), 5000).state);
        System.out.println("lamborghini is " + node.waitItem(l.getId(), 5000).state);
    }
    for (Approvable a : swapContract.getNewItems()) {
        assertEquals(ItemState.APPROVED, node.waitItem(a.getId(), 5000).state);
        System.out.println("new is " + node.waitItem(a.getId(), 5000).state);
    }
// checkSwapResultSuccess(swapContract, delorean, lamborghini, martyPublicKeys, stepaPublicKeys);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Test(org.junit.Test)

Example 24 with PublicKey

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

the class BaseNetworkTest method changeOwnerWithAddress.

@Test(timeout = 90000)
public void changeOwnerWithAddress() throws Exception {
    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")));
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    for (PrivateKey pk : stepaPrivateKeys) stepaPublicKeys.add(pk.getPublicKey());
    for (PrivateKey pk : martyPrivateKeys) martyPublicKeys.add(pk.getPublicKey());
    PrivateKey key = new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey"));
    Contract c1 = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    c1.addSignerKey(key);
    c1.seal();
    c1.check();
    c1.traceErrors();
    registerAndCheckApproved(c1);
    // 
    KeyAddress stepaAddress = stepaPublicKeys.iterator().next().getShortAddress();
    Contract anonOwnerContract = c1.createRevisionWithAddress(Arrays.asList(key));
    anonOwnerContract.addSignerKey(key);
    anonOwnerContract.setOwnerKey(stepaAddress);
    anonOwnerContract.seal();
    anonOwnerContract.check();
    anonOwnerContract.traceErrors();
    Contract anonAfterSend = imitateSendingTransactionToPartner(anonOwnerContract);
    registerAndCheckApproved(anonAfterSend);
    assertTrue(anonAfterSend.getOwner().getKeyAddresses().iterator().next().equals(stepaAddress));
    assertEquals(0, anonAfterSend.getOwner().getKeys().size());
    // 
    Contract anonSignedContract = anonAfterSend.createRevisionWithAddress(stepaPrivateKeys);
    anonSignedContract.addSignerKeys(stepaPrivateKeys);
    anonSignedContract.setOwnerKeys(martyPublicKeys);
    anonSignedContract.seal();
    anonSignedContract.check();
    anonSignedContract.traceErrors();
    Contract afterSend = imitateSendingTransactionToPartner(anonSignedContract);
    registerAndCheckApproved(afterSend);
    assertEquals(0, afterSend.getOwner().getKeyAddresses().size());
    assertTrue(afterSend.getOwner().isAllowedForKeys(martyPublicKeys));
    Contract anonPublishedContract = new Contract(anonSignedContract.getLastSealedBinary());
    ItemResult itemResult = node.waitItem(anonPublishedContract.getId(), 8000);
    assertEquals(ItemState.APPROVED, itemResult.state);
    assertFalse(anonPublishedContract.getSealedByKeys().contains(stepaPublicKeys.iterator().next()));
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) KeyAddress(com.icodici.crypto.KeyAddress) Test(org.junit.Test)

Example 25 with PublicKey

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

the class BaseNetworkTest method declineReferenceForRevoke.

@Test
public void declineReferenceForRevoke() 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 llcProperty = Contract.fromDslFile(ROOT_PATH + "NotaryWithReferenceDSLTemplate.yml");
    llcProperty.addSignerKey(llcPrivateKeys.iterator().next());
    llcProperty.seal();
    registerAndCheckApproved(llcProperty);
    Contract llcProperty2 = ContractsService.createRevocation(llcProperty, stepaPrivateKeys.iterator().next());
    llcProperty2.check();
    llcProperty2.traceErrors();
    assertFalse(llcProperty2.isOk());
    // bad situations
    TransactionPack tp_before;
    TransactionPack tp_after;
    Contract jobCertificate;
    // missing data.issuer
    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);
    tp_before = llcProperty2.getTransactionPack();
    tp_before.getReferencedItems().clear();
    tp_before.addReferencedItem(jobCertificate);
    // here we "send" data and "got" it
    tp_after = TransactionPack.unpack(tp_before.pack());
    registerAndCheckDeclined(tp_after);
    // missing data.type
    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);
    tp_before = llcProperty2.getTransactionPack();
    tp_before.getReferencedItems().clear();
    tp_before.addReferencedItem(jobCertificate);
    // here we "send" data and "got" it
    tp_after = TransactionPack.unpack(tp_before.pack());
    registerAndCheckDeclined(tp_after);
    // not registered
    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);
    tp_before = llcProperty2.getTransactionPack();
    tp_before.getReferencedItems().clear();
    tp_before.addReferencedItem(jobCertificate);
    // here we "send" data and "got" it
    tp_after = TransactionPack.unpack(tp_before.pack());
    registerAndCheckDeclined(tp_after);
    // missing reference
    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);
    tp_before = llcProperty2.getTransactionPack();
    tp_before.getReferencedItems().clear();
    // tp_before.addForeignReference(jobCertificate);
    // here we "send" data and "got" it
    tp_after = TransactionPack.unpack(tp_before.pack());
    registerAndCheckDeclined(tp_after);
    // wrong issuer
    jobCertificate = new Contract(stepaPrivateKeys.iterator().next());
    jobCertificate.setOwnerKeys(stepaPublicKeys);
    jobCertificate.getDefinition().getData().set("issuer", "Roga & Kopita");
    jobCertificate.getDefinition().getData().set("type", "chief accountant assignment");
    jobCertificate.seal();
    registerAndCheckApproved(jobCertificate);
    tp_before = llcProperty2.getTransactionPack();
    tp_before.getReferencedItems().clear();
    tp_before.addReferencedItem(jobCertificate);
    // here we "send" data and "got" it
    tp_after = TransactionPack.unpack(tp_before.pack());
    registerAndCheckDeclined(tp_after);
    // wrong data.issuer
    jobCertificate = new Contract(llcPrivateKeys.iterator().next());
    jobCertificate.setOwnerKeys(stepaPublicKeys);
    jobCertificate.getDefinition().getData().set("issuer", "Not Roga & Kopita");
    jobCertificate.getDefinition().getData().set("type", "chief accountant assignment");
    jobCertificate.seal();
    registerAndCheckApproved(jobCertificate);
    tp_before = llcProperty2.getTransactionPack();
    tp_before.getReferencedItems().clear();
    tp_before.addReferencedItem(jobCertificate);
    // here we "send" data and "got" it
    tp_after = TransactionPack.unpack(tp_before.pack());
    registerAndCheckDeclined(tp_after);
    // wrong data.type
    jobCertificate = new Contract(llcPrivateKeys.iterator().next());
    jobCertificate.setOwnerKeys(stepaPublicKeys);
    jobCertificate.getDefinition().getData().set("issuer", "Roga & Kopita");
    jobCertificate.getDefinition().getData().set("type", "Not chief accountant assignment");
    jobCertificate.seal();
    registerAndCheckApproved(jobCertificate);
    tp_before = llcProperty2.getTransactionPack();
    tp_before.getReferencedItems().clear();
    tp_before.addReferencedItem(jobCertificate);
    // here we "send" data and "got" it
    tp_after = TransactionPack.unpack(tp_before.pack());
    registerAndCheckDeclined(tp_after);
    // revoked reference
    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.addPermission(new RevokePermission(jobCertificate.getOwner()));
    jobCertificate.seal();
    registerAndCheckApproved(jobCertificate);
    Contract revokingJobCertificate = ContractsService.createRevocation(jobCertificate, stepaPrivateKeys.iterator().next());
    revokingJobCertificate.check();
    revokingJobCertificate.traceErrors();
    registerAndCheckApproved(revokingJobCertificate);
    tp_before = llcProperty2.getTransactionPack();
    tp_before.getReferencedItems().clear();
    tp_before.addReferencedItem(jobCertificate);
    // here we "send" data and "got" it
    tp_after = TransactionPack.unpack(tp_before.pack());
    registerAndCheckDeclined(tp_after);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Test(org.junit.Test)

Aggregations

PublicKey (com.icodici.crypto.PublicKey)84 PrivateKey (com.icodici.crypto.PrivateKey)75 Test (org.junit.Test)57 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)17 HashSet (java.util.HashSet)14 Binder (net.sergeych.tools.Binder)13 ListRole (com.icodici.universa.contract.roles.ListRole)11 Role (com.icodici.universa.contract.roles.Role)7 Contract (com.icodici.universa.contract.Contract)6 KeyAddress (com.icodici.crypto.KeyAddress)4 RoleLink (com.icodici.universa.contract.roles.RoleLink)4 KeyRecord (com.icodici.universa.contract.KeyRecord)3 ChangeOwnerPermission (com.icodici.universa.contract.permissions.ChangeOwnerPermission)3 Permission (com.icodici.universa.contract.permissions.Permission)3 TimeoutException (java.util.concurrent.TimeoutException)3 Decimal (com.icodici.universa.Decimal)2 ErrorRecord (com.icodici.universa.ErrorRecord)2 HashId (com.icodici.universa.HashId)2 Quantiser (com.icodici.universa.node2.Quantiser)2 List (java.util.List)2