Search in sources :

Example 21 with SimpleRole

use of com.icodici.universa.contract.roles.SimpleRole in project universa by UniversaBlockchain.

the class BaseNetworkTest method referenceForRevokeWithCreateContract.

@Test
public void referenceForRevokeWithCreateContract() throws Exception {
    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 = ContractsService.createNotaryContract(llcPrivateKeys, stepaPublicKeys);
    List<String> listConditions = new ArrayList<>();
    listConditions.add("ref.definition.issuer == \"26RzRJDLqze3P5Z1AzpnucF75RLi1oa6jqBaDh8MJ3XmTaUoF8R\"");
    listConditions.add("ref.definition.data.issuer == \"Roga & Kopita\"");
    listConditions.add("ref.definition.data.type == \"chief accountant assignment\"");
    Reference reference = new Reference(llcProperty);
    reference.name = "certification_contract";
    reference.type = Reference.TYPE_EXISTING;
    Binder conditions = new Binder();
    conditions.set("all_of", listConditions);
    reference.setConditions(conditions);
    reference.addMatchingItem(jobCertificate);
    // llcProperty.getDefinition().getReferences().add(reference);
    llcProperty.addReference(reference);
    ListRole listRole = new ListRole("list_role");
    SimpleRole ownerRole = new SimpleRole("owner", stepaPrivateKeys);
    listRole.addRole(ownerRole);
    listRole.addRequiredReference("certification_contract", Role.RequiredMode.ALL_OF);
    llcProperty.getPermissions().remove("change_owner");
    llcProperty.getPermissions().remove("revoke");
    ChangeOwnerPermission changeOwnerPerm = new ChangeOwnerPermission(listRole);
    llcProperty.addPermission(changeOwnerPerm);
    RevokePermission revokePerm = new RevokePermission(listRole);
    llcProperty.addPermission(revokePerm);
    llcProperty.addSignerKey(llcPrivateKeys.iterator().next());
    llcProperty.seal();
    registerAndCheckApproved(llcProperty);
    Contract llcProperty2 = ContractsService.createRevocation(llcProperty, stepaPrivateKeys.iterator().next());
    llcProperty2.check();
    llcProperty2.traceErrors();
    // assertFalse(llcProperty2.isOk());
    TransactionPack tp_before = llcProperty2.getTransactionPack();
    // tp_before.addReferencedItem(jobCertificate);
    byte[] data = tp_before.pack();
    TransactionPack tp_after = TransactionPack.unpack(data);
    registerAndCheckApproved(tp_after);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Binder(net.sergeych.tools.Binder) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) ListRole(com.icodici.universa.contract.roles.ListRole) Test(org.junit.Test)

Example 22 with SimpleRole

use of com.icodici.universa.contract.roles.SimpleRole 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 23 with SimpleRole

use of com.icodici.universa.contract.roles.SimpleRole in project universa by UniversaBlockchain.

the class ContractsService method createTokenContract.

/**
 * Creates a token contract for given keys.
 *<br><br>
 * The service creates a simple token contract with issuer, creator and owner roles;
 * with change_owner permission for owner, revoke permissions for owner and issuer and split_join permission for owner.
 * Split_join permission has by default following params: 0.01 for min_value, 0.01 for min_unit, "amount" for field_name,
 * "state.origin" for join_match_fields.
 * By default expires at time is set to 60 months from now.
 * @param ownerKeys is owner public keys.
 * @param amount is maximum token number.
 * @return signed and sealed contract, ready for register.
 */
public static synchronized Contract createTokenContract(Set<PrivateKey> issuerKeys, Set<PublicKey> ownerKeys, String amount) {
    Contract tokenContract = new Contract();
    tokenContract.setApiLevel(3);
    Contract.Definition cd = tokenContract.getDefinition();
    cd.setExpiresAt(ZonedDateTime.now().plusMonths(60));
    Binder data = new Binder();
    data.set("name", "Default token name");
    data.set("currency_code", "DT");
    data.set("currency_name", "Default token name");
    data.set("description", "Default token description.");
    cd.setData(data);
    SimpleRole issuerRole = new SimpleRole("issuer");
    for (PrivateKey k : issuerKeys) {
        KeyRecord kr = new KeyRecord(k.getPublicKey());
        issuerRole.addKeyRecord(kr);
    }
    SimpleRole ownerRole = new SimpleRole("owner");
    for (PublicKey k : ownerKeys) {
        KeyRecord kr = new KeyRecord(k);
        ownerRole.addKeyRecord(kr);
    }
    tokenContract.registerRole(issuerRole);
    tokenContract.createRole("issuer", issuerRole);
    tokenContract.createRole("creator", issuerRole);
    tokenContract.registerRole(ownerRole);
    tokenContract.createRole("owner", ownerRole);
    tokenContract.getStateData().set("amount", amount);
    ChangeOwnerPermission changeOwnerPerm = new ChangeOwnerPermission(ownerRole);
    tokenContract.addPermission(changeOwnerPerm);
    Binder params = new Binder();
    params.set("min_value", 0.01);
    params.set("min_unit", 0.01);
    params.set("field_name", "amount");
    List<String> listFields = new ArrayList<>();
    listFields.add("state.origin");
    params.set("join_match_fields", listFields);
    SplitJoinPermission splitJoinPerm = new SplitJoinPermission(ownerRole, params);
    tokenContract.addPermission(splitJoinPerm);
    RevokePermission revokePerm1 = new RevokePermission(ownerRole);
    tokenContract.addPermission(revokePerm1);
    RevokePermission revokePerm2 = new RevokePermission(issuerRole);
    tokenContract.addPermission(revokePerm2);
    tokenContract.seal();
    tokenContract.addSignatureToSeal(issuerKeys);
    return tokenContract;
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Binder(net.sergeych.tools.Binder) SimpleRole(com.icodici.universa.contract.roles.SimpleRole)

Aggregations

SimpleRole (com.icodici.universa.contract.roles.SimpleRole)23 PrivateKey (com.icodici.crypto.PrivateKey)22 Test (org.junit.Test)12 PublicKey (com.icodici.crypto.PublicKey)11 Binder (net.sergeych.tools.Binder)9 Contract (com.icodici.universa.contract.Contract)7 KeyRecord (com.icodici.universa.contract.KeyRecord)6 ListRole (com.icodici.universa.contract.roles.ListRole)6 ItemResult (com.icodici.universa.node.ItemResult)2 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 KeyAddress (com.icodici.crypto.KeyAddress)1 TransactionPack (com.icodici.universa.contract.TransactionPack)1 ChangeOwnerPermission (com.icodici.universa.contract.permissions.ChangeOwnerPermission)1 ModifyDataPermission (com.icodici.universa.contract.permissions.ModifyDataPermission)1 RoleLink (com.icodici.universa.contract.roles.RoleLink)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Path (java.nio.file.Path)1