Search in sources :

Example 16 with SimpleRole

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

the class ContractsService method createTwoSignedContract.

/**
 * Creates a contract with two signatures.
 *<br><br>
 * The service creates a contract which asks two signatures.
 * It can not be registered without both parts of deal, so it is make sure both parts that they agreed with contract.
 * Service creates a contract that should be send to partner,
 * then partner should sign it and return back for final sign from calling part.
 *<br><br>
 * @param BaseContract is base contract
 * @param fromKeys is own private keys
 * @param toKeys is foreign public keys
 * @param createNewRevision create new revision if true
 * @return contract with two signatures that should be send from first part to partner.
 */
public static synchronized Contract createTwoSignedContract(Contract BaseContract, Set<PrivateKey> fromKeys, Set<PublicKey> toKeys, boolean createNewRevision) {
    Contract twoSignContract = BaseContract;
    if (createNewRevision) {
        twoSignContract = BaseContract.createRevision(fromKeys);
        twoSignContract.getKeysToSignWith().clear();
    }
    SimpleRole creatorFrom = new SimpleRole("creator");
    for (PrivateKey k : fromKeys) {
        KeyRecord kr = new KeyRecord(k.getPublicKey());
        creatorFrom.addKeyRecord(kr);
    }
    SimpleRole ownerTo = new SimpleRole("owner");
    for (PublicKey k : toKeys) {
        KeyRecord kr = new KeyRecord(k);
        ownerTo.addKeyRecord(kr);
    }
    twoSignContract.createTransactionalSection();
    twoSignContract.getTransactional().setId(HashId.createRandom().toBase64String());
    Reference reference = new Reference(twoSignContract);
    reference.transactional_id = twoSignContract.getTransactional().getId();
    reference.type = Reference.TYPE_TRANSACTIONAL;
    reference.required = true;
    reference.signed_by = new ArrayList<>();
    reference.signed_by.add(creatorFrom);
    reference.signed_by.add(ownerTo);
    twoSignContract.getTransactional().addReference(reference);
    twoSignContract.setOwnerKeys(toKeys);
    twoSignContract.seal();
    return twoSignContract;
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) PublicKey(com.icodici.crypto.PublicKey)

Example 17 with SimpleRole

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

the class ContractsService method createRevocation.

/**
 * Implementing revoking procedure.
 * <br><br>
 * Service create temp contract with given contract in revoking items and return it.
 * That temp contract should be send to Universa and given contract will be revoked.
 * <br><br>
 * @param c is contract should revoked be
 * @param keys is keys from owner of c
 * @return working contract that should be register in the Universa to finish procedure.
 */
public static synchronized Contract createRevocation(Contract c, PrivateKey... keys) {
    Contract tc = new Contract();
    Contract.Definition cd = tc.getDefinition();
    // by default, transactions expire in 30 days
    cd.setExpiresAt(tc.getCreatedAt().plusDays(30));
    SimpleRole issuerRole = new SimpleRole("issuer");
    for (PrivateKey k : keys) {
        KeyRecord kr = new KeyRecord(k.getPublicKey());
        issuerRole.addKeyRecord(kr);
        tc.addSignerKey(k);
    }
    tc.registerRole(issuerRole);
    tc.createRole("owner", issuerRole);
    tc.createRole("creator", issuerRole);
    if (!tc.getRevokingItems().contains(c)) {
        Binder data = tc.getDefinition().getData();
        List<Binder> actions = data.getOrCreateList("actions");
        tc.getRevokingItems().add(c);
        actions.add(Binder.fromKeysValues("action", "remove", "id", c.getId()));
    }
    tc.seal();
    return tc;
}
Also used : Binder(net.sergeych.tools.Binder) PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole)

Example 18 with SimpleRole

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

the class ContractsService method createShareContract.

/**
 * Creates a share contract for given keys.
 *<br><br>
 * The service creates a simple share 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: 1 for min_value, 1 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.
 *<br><br>
 * @param issuerKeys is issuer private keys.
 * @param ownerKeys is owner public keys.
 * @param amount is maximum shares number.
 * @return signed and sealed contract, ready for register.
 */
public static synchronized Contract createShareContract(Set<PrivateKey> issuerKeys, Set<PublicKey> ownerKeys, String amount) {
    Contract shareContract = new Contract();
    shareContract.setApiLevel(3);
    Contract.Definition cd = shareContract.getDefinition();
    cd.setExpiresAt(ZonedDateTime.now().plusMonths(60));
    Binder data = new Binder();
    data.set("name", "Default share name");
    data.set("currency_code", "DSH");
    data.set("currency_name", "Default share name");
    data.set("description", "Default share 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);
    }
    shareContract.registerRole(issuerRole);
    shareContract.createRole("issuer", issuerRole);
    shareContract.createRole("creator", issuerRole);
    shareContract.registerRole(ownerRole);
    shareContract.createRole("owner", ownerRole);
    shareContract.getStateData().set("amount", amount);
    ChangeOwnerPermission changeOwnerPerm = new ChangeOwnerPermission(ownerRole);
    shareContract.addPermission(changeOwnerPerm);
    Binder params = new Binder();
    params.set("min_value", 1);
    params.set("min_unit", 1);
    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);
    shareContract.addPermission(splitJoinPerm);
    RevokePermission revokePerm1 = new RevokePermission(ownerRole);
    shareContract.addPermission(revokePerm1);
    RevokePermission revokePerm2 = new RevokePermission(issuerRole);
    shareContract.addPermission(revokePerm2);
    shareContract.seal();
    shareContract.addSignatureToSeal(issuerKeys);
    return shareContract;
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Binder(net.sergeych.tools.Binder) SimpleRole(com.icodici.universa.contract.roles.SimpleRole)

Example 19 with SimpleRole

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

the class CLIMainTest method checkSessionReusing.

@Test
public void checkSessionReusing() throws Exception {
    Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
    c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
    PrivateKey goodKey = c.getKeysToSignWith().iterator().next();
    // let's make this key among owners
    ((SimpleRole) c.getRole("owner")).addKeyRecord(new KeyRecord(goodKey.getPublicKey()));
    c.seal();
    CLIMain.setVerboseMode(true);
    Thread.sleep(1000);
    CLIMain.clearSession();
    System.out.println("---session cleared---");
    CLIMain.registerContract(c);
    Thread.sleep(1000);
    CLIMain.setNodeUrl("http://localhost:8080");
    System.out.println("---session should be reused from variable---");
    CLIMain.registerContract(c);
    CLIMain.saveSession();
    Thread.sleep(1000);
    CLIMain.clearSession(false);
    CLIMain.setNodeUrl("http://localhost:8080");
    System.out.println("---session should be reused from file---");
    CLIMain.registerContract(c);
    CLIMain.saveSession();
    Thread.sleep(1000);
    // CLIMain.clearSession(false);
    // 
    // CLIMain.setNodeUrl(null);
    // 
    // System.out.println("---session should be created for remote network---");
    // 
    // CLIMain.registerContract(c);
    // 
    // CLIMain.saveSession();
    CLIMain.breakSession(-1);
    Thread.sleep(2000);
    CLIMain.clearSession(false);
    CLIMain.setNodeUrl("http://localhost:8080");
    System.out.println("---broken session should be recreated---");
    CLIMain.registerContract(c);
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 20 with SimpleRole

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

the class CLIMainTest method revokeCreatedContractWithRole.

@Test
public void revokeCreatedContractWithRole() throws Exception {
    Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    PrivateKey goodKey = c.getKeysToSignWith().iterator().next();
    // let's make this key among owners
    ((SimpleRole) c.getRole("owner")).addKeyRecord(new KeyRecord(goodKey.getPublicKey()));
    c.seal();
    String contractFileName = basePath + "with_role_for_revoke.unicon";
    CLIMain.saveContract(c, contractFileName);
    System.out.println("---");
    System.out.println("register contract");
    System.out.println("---");
    String tuContract = getApprovedTUContract();
    // CLIMain.registerContract(c);
    callMain2("--register", contractFileName, "--verbose", "--tu", tuContract, "--k", rootPath + "keys/stepan_mamontov.private.unikey");
    Thread.sleep(1500);
    System.out.println("---");
    System.out.println("check contract");
    System.out.println("---");
    callMain("--probe", c.getId().toBase64String());
    System.out.println(output);
    assertTrue(output.indexOf(ItemState.APPROVED.name()) >= 0);
    tuContract = getApprovedTUContract();
    callMain2("-revoke", contractFileName, "-k", PRIVATE_KEY_PATH, "-v", "--tu", tuContract, "--k", rootPath + "keys/stepan_mamontov.private.unikey");
    Thread.sleep(1500);
    System.out.println("---");
    System.out.println("check contract after revoke");
    System.out.println("---");
    callMain("--probe", c.getId().toBase64String());
    System.out.println(output);
    assertTrue(output.indexOf(ItemState.REVOKED.name()) >= 0);
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

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