Search in sources :

Example 81 with PublicKey

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

Example 82 with PublicKey

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

the class ContractsService method signPresentedSwap.

/**
 * Second step of swap procedure. Calls from swapper2 part.
 *<br><br>
 * Swapper2 got swap contract from swapper1 and give it to service.
 * Service sign new contract where calling part was owner, store hashId of this contract.
 * Then add to reference of new contract, that will be own for calling part,
 * contract_id and point it to contract that was own for calling part.
 * Then sign second contract too.
 *<br><br>
 * Swap procedure consist from three steps:<br>
 * (1) prepare contracts with creating transactional section on the first swapper site, sign only one contract;<br>
 * (2) sign contracts on the second swapper site;<br>
 * (3) sign lost contracts on the first swapper site and finishing swap.
 *<br><br>
 * @param swapContract is being processing swap contract, got from swapper1
 * @param keys is own (belongs to swapper2) private keys
 * @return modified swapContract;
 * should be send back to partner (swapper1) and he should go to step (3) of the swap procedure.
 */
public static synchronized Contract signPresentedSwap(Contract swapContract, Set<PrivateKey> keys) {
    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
    HashMap<String, HashId> contractHashId = new HashMap<>();
    for (Contract c : swappingContracts) {
        boolean willBeMine = c.getOwner().isAllowedForKeys(publicKeys);
        if (willBeMine) {
            c.addSignatureToSeal(keys);
            contractHashId.put(c.getTransactional().getId(), 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.get(rm.transactional_id);
                }
            } else {
                return swapContract;
            }
            c.seal();
            c.addSignatureToSeal(keys);
        }
    }
    swapContract.seal();
    return swapContract;
}
Also used : HashId(com.icodici.universa.HashId) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey)

Example 83 with PublicKey

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

the class KeyRecord method setupKey.

private void setupKey() {
    try {
        Object x = getOrThrow("key");
        remove("key");
        if (x instanceof PublicKey) {
            publicKey = (PublicKey) x;
        } else if (x instanceof PrivateKey) {
            publicKey = ((PrivateKey) x).getPublicKey();
        } else if (x instanceof String) {
            publicKey = new PublicKey(Base64u.decodeCompactString((String) x));
        } else {
            if (x instanceof Bytes)
                x = ((Bytes) x).toArray();
            if (x instanceof byte[]) {
                publicKey = new PublicKey((byte[]) x);
            } else {
                throw new IllegalArgumentException("unsupported key object: " + x.getClass().getName());
            }
        }
        put("key", publicKey);
    } catch (EncryptionError e) {
        throw new IllegalArgumentException("unsupported key, failed to construct", e);
    }
}
Also used : Bytes(net.sergeych.utils.Bytes) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) EncryptionError(com.icodici.crypto.EncryptionError)

Example 84 with PublicKey

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

the class ContractDelta method excludePermittedChanges.

private void excludePermittedChanges() throws Quantiser.QuantiserException {
    Set<PublicKey> checkingKeys = changed.getSealedByKeys();
    Set<String> checkingReferences = changed.getReferences().keySet();
    for (String key : existing.getPermissions().keySet()) {
        Collection<Permission> permissions = existing.getPermissions().get(key);
        boolean permissionQuantized = false;
        for (Permission permission : permissions) {
            if (permission.isAllowedFor(checkingKeys, checkingReferences)) {
                if (!permissionQuantized) {
                    changed.checkApplicablePermissionQuantized(permission);
                    permissionQuantized = true;
                }
                permission.checkChanges(existing, changed, stateChanges);
            }
        }
    }
}
Also used : PublicKey(com.icodici.crypto.PublicKey) Permission(com.icodici.universa.contract.permissions.Permission)

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