Search in sources :

Example 56 with PublicKey

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

the class BaseNetworkTest method changeOwnerWithAddress2.

@Test(timeout = 90000)
public void changeOwnerWithAddress2() 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().getLongAddress();
    Contract anonOwnerContract = c1.createRevision(key);
    anonOwnerContract.setOwnerKey(stepaAddress);
    anonOwnerContract.seal();
    anonOwnerContract.check();
    anonOwnerContract.traceErrors();
    registerAndCheckApproved(anonOwnerContract);
    assertTrue(anonOwnerContract.getOwner().getKeyAddresses().iterator().next().equals(stepaAddress));
    assertEquals(0, anonOwnerContract.getOwner().getKeys().size());
    // 
    Contract anonSignedContract = anonOwnerContract.createRevision();
    anonSignedContract.setOwnerKeys(martyPublicKeys);
    anonSignedContract.setCreatorKeys(stepaAddress);
    anonSignedContract.addSignerKey(stepaPrivateKeys.iterator().next());
    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 57 with PublicKey

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

the class BaseNetworkTest method startSwap_wrongKey.

public synchronized Contract startSwap_wrongKey(Contract contract1, Contract contract2, Set<PrivateKey> fromKeys, Set<PublicKey> toKeys, PrivateKey wrongKey) {
    Set<PublicKey> fromPublicKeys = new HashSet<>();
    for (PrivateKey pk : fromKeys) {
        fromPublicKeys.add(pk.getPublicKey());
    }
    // first of all we creating main swap contract which will include new revisions of contract for swap
    // you can think about this contract as about transaction
    Contract swapContract = new Contract();
    Contract.Definition cd = swapContract.getDefinition();
    // by default, transactions expire in 30 days
    cd.setExpiresAt(swapContract.getCreatedAt().plusDays(30));
    SimpleRole issuerRole = new SimpleRole("issuer");
    for (PrivateKey k : fromKeys) {
        KeyRecord kr = new KeyRecord(k.getPublicKey());
        issuerRole.addKeyRecord(kr);
    }
    swapContract.registerRole(issuerRole);
    swapContract.registerRole((issuerRole).linkAs("owner"));
    swapContract.registerRole((issuerRole).linkAs("creator"));
    // now we will prepare new revisions of contracts
    // create new revisions of contracts and create transactional sections in it
    Contract newContract1 = contract1.createRevision(wrongKey);
    Contract.Transactional transactional1 = newContract1.createTransactionalSection();
    transactional1.setId(HashId.createRandom().toBase64String());
    Contract newContract2 = contract2.createRevision();
    Contract.Transactional transactional2 = newContract2.createTransactionalSection();
    transactional2.setId(HashId.createRandom().toBase64String());
    // prepare roles for references
    // it should new owners and old creators in new revisions of contracts
    SimpleRole ownerFrom = new SimpleRole("owner");
    SimpleRole creatorFrom = new SimpleRole("creator");
    for (PrivateKey k : fromKeys) {
        KeyRecord kr = new KeyRecord(k.getPublicKey());
        ownerFrom.addKeyRecord(kr);
        creatorFrom.addKeyRecord(kr);
    }
    SimpleRole ownerTo = new SimpleRole("owner");
    SimpleRole creatorTo = new SimpleRole("creator");
    for (PublicKey k : toKeys) {
        KeyRecord kr = new KeyRecord(k);
        ownerTo.addKeyRecord(kr);
        creatorTo.addKeyRecord(kr);
    }
    // create references for contracts that point to each other and asks correct signs
    Reference reference1 = new Reference();
    reference1.transactional_id = transactional2.getId();
    reference1.type = Reference.TYPE_TRANSACTIONAL;
    reference1.required = true;
    reference1.signed_by = new ArrayList<>();
    reference1.signed_by.add(ownerFrom);
    reference1.signed_by.add(creatorTo);
    Reference reference2 = new Reference();
    reference2.transactional_id = transactional1.getId();
    reference2.type = Reference.TYPE_TRANSACTIONAL;
    reference2.required = true;
    reference2.signed_by = new ArrayList<>();
    reference2.signed_by.add(ownerTo);
    reference2.signed_by.add(creatorFrom);
    // and add this references to existing transactional section
    transactional1.addReference(reference1);
    transactional2.addReference(reference2);
    // swap owners in this contracts
    newContract1.setOwnerKeys(toKeys);
    newContract2.setOwnerKeys(fromPublicKeys);
    newContract1.seal();
    newContract2.seal();
    // finally on this step add created new revisions to main swap contract
    swapContract.addNewItems(newContract1);
    swapContract.addNewItems(newContract2);
    swapContract.seal();
    return swapContract;
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) PublicKey(com.icodici.crypto.PublicKey)

Example 58 with PublicKey

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

the class BaseNetworkTest method declineReferenceForSplitJoin.

@Test
public void declineReferenceForSplitJoin() 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 + "TokenWithReferenceDSLTemplate.yml");
    llcProperty.addSignerKey(llcPrivateKeys.iterator().next());
    llcProperty.seal();
    registerAndCheckApproved(llcProperty);
    Contract llcProperty2 = ContractsService.createSplit(llcProperty, 100, "amount", stepaPrivateKeys, true);
    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)

Example 59 with PublicKey

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

the class BaseNetworkTest method referenceForChangeOwner.

@Test
public void referenceForChangeOwner() 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 + "NotaryWithReferenceDSLTemplate.yml");
    llcProperty.addSignerKey(llcPrivateKeys.iterator().next());
    llcProperty.seal();
    registerAndCheckApproved(llcProperty);
    Contract llcProperty2 = llcProperty.createRevision(stepaPrivateKeys);
    llcProperty2.setOwnerKeys(thirdPartyPublicKeys);
    llcProperty2.seal();
    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 60 with PublicKey

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

the class BaseNetworkTest method signPresentedSwap_wrongKey.

public synchronized Contract signPresentedSwap_wrongKey(Contract swapContract, Set<PrivateKey> keys, PrivateKey wrongKey) {
    Set<PublicKey> publicKeys = new HashSet<>();
    for (PrivateKey pk : keys) {
        publicKeys.add(pk.getPublicKey());
    }
    List<Contract> swappingContracts = (List<Contract>) swapContract.getNew();
    // looking for contract that will be own and sign it
    HashId contractHashId = null;
    for (Contract c : swappingContracts) {
        boolean willBeMine = c.getOwner().isAllowedForKeys(publicKeys);
        if (willBeMine) {
            c.addSignatureToSeal(wrongKey);
            contractHashId = c.getId();
        }
    }
    // looking for contract that was own, add to reference hash of above contract and sign it
    for (Contract c : swappingContracts) {
        boolean willBeNotMine = (!c.getOwner().isAllowedForKeys(publicKeys));
        if (willBeNotMine) {
            Set<KeyRecord> krs = new HashSet<>();
            for (PublicKey k : publicKeys) {
                krs.add(new KeyRecord(k));
            }
            c.setCreator(krs);
            if (c.getTransactional() != null && c.getTransactional().getReferences() != null) {
                for (Reference rm : c.getTransactional().getReferences()) {
                    rm.contract_id = contractHashId;
                }
            } else {
                return swapContract;
            }
            c.seal();
            c.addSignatureToSeal(wrongKey);
        }
    }
    swapContract.seal();
    return swapContract;
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey)

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