Search in sources :

Example 96 with PrivateKey

use of com.icodici.crypto.PrivateKey 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 97 with PrivateKey

use of com.icodici.crypto.PrivateKey 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 98 with PrivateKey

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

the class TransactionPack method setContract.

/**
 * Add contract that already includes all its subItems, referenced items and keys. It will be added as a contract
 * per transaction, while its subItems will be added to subItems if not already included and refrenced items and keys too.
 * <p>
 * This is extremely important that the contract is properly sealed as well as its possibly new items, revoking
 * items and referenced items have binary image attached. <b>Do not ever seal the approved contract</b>:
 * it will break it's id and cancel the approval blockchain, so the new state will not be approved.
 * If it was done by mistake, reload the packed contract to continue.
 *
 * @param c is a contract to append to the list of transactions.
 */
public void setContract(Contract c) {
    if (contract != null)
        throw new IllegalArgumentException("the contract is already added");
    contract = c;
    packedBinary = null;
    extractAllSubItemsAndReferenced(c);
    c.setTransactionPack(this);
    for (PrivateKey key : c.getKeysToSignWith()) addKeys(key.getPublicKey());
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey)

Example 99 with PrivateKey

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

the class SimpleRole method initWithRecords.

private void initWithRecords(@NonNull Collection records) {
    records.forEach(x -> {
        KeyRecord kr = null;
        AnonymousId anonId = null;
        if (x instanceof KeyRecord)
            kr = (KeyRecord) x;
        else if (x instanceof PublicKey)
            kr = new KeyRecord((PublicKey) x);
        else if (x instanceof AnonymousId)
            anonId = (AnonymousId) x;
        else if (x instanceof PrivateKey)
            kr = new KeyRecord(((PrivateKey) x).getPublicKey());
        else if (x instanceof KeyAddress)
            keyAddresses.add((KeyAddress) x);
        else
            throw new IllegalArgumentException("Cant create KeyRecord from " + x);
        if (anonId != null)
            anonymousIds.add(anonId);
        else if (kr != null)
            keyRecords.put(kr.getPublicKey(), kr);
    });
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) KeyAddress(com.icodici.crypto.KeyAddress) AnonymousId(com.icodici.universa.contract.AnonymousId)

Example 100 with PrivateKey

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

the class ContractTest method checkAnonymizingRole.

@Test
public void checkAnonymizingRole() throws Exception {
    PrivateKey key = new PrivateKey(Do.read(PRIVATE_KEY_PATH));
    Contract contract = createCoin100apiv3();
    contract.addSignerKey(key);
    sealCheckTrace(contract, true);
    assertTrue(contract.getIssuer().getKeys().contains(key.getPublicKey()));
    contract.anonymizeRole("issuer");
    contract.anonymizeRole("owner");
    contract.seal();
    assertFalse(contract.getIssuer().getKeys().contains(key.getPublicKey()));
    Contract anonPublishedContract = new Contract(contract.getLastSealedBinary());
    assertFalse(anonPublishedContract.getIssuer().getKeys().contains(key.getPublicKey()));
    assertFalse(anonPublishedContract.getSealedByKeys().contains(key.getPublicKey()));
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) 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