Search in sources :

Example 11 with KeyAddress

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

the class SimpleRole method initWithDsl.

@Override
public void initWithDsl(Binder serializedRole) {
    boolean keysFound = true;
    boolean addressesFound = true;
    boolean anonIdsFound = true;
    if (serializedRole.containsKey("keys")) {
        List<Binder> list = serializedRole.getListOrThrow("keys");
        for (Object keyRecord : list) {
            addKeyRecord(new KeyRecord(Binder.of(keyRecord)));
        }
    } else if (serializedRole.containsKey("key")) {
        addKeyRecord(new KeyRecord(serializedRole));
    } else {
        keysFound = false;
    }
    if (serializedRole.containsKey("addresses")) {
        List<Binder> list = serializedRole.getListOrThrow("addresses");
        for (Object address : list) {
            keyAddresses.add(new KeyAddress(Binder.of(address)));
        }
    } else if (serializedRole.containsKey("uaddress")) {
        keyAddresses.add(new KeyAddress(serializedRole));
    } else {
        addressesFound = false;
    }
    if (serializedRole.containsKey("anonIds")) {
        List<Binder> list = serializedRole.getListOrThrow("anonIds");
        for (Object anonId : list) {
            anonymousIds.add(new AnonymousId(Binder.of(anonId)));
        }
    } else if (serializedRole.containsKey("anonymousId")) {
        anonymousIds.add(new AnonymousId(serializedRole));
    } else {
        anonIdsFound = false;
    }
    if (!addressesFound && !anonIdsFound && !keysFound) {
        // TODO: ?????? "binders" were in old code
        initWithRecords(serializedRole.getListOrThrow("binders"));
    }
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) Binder(net.sergeych.tools.Binder) KeyAddress(com.icodici.crypto.KeyAddress) AnonymousId(com.icodici.universa.contract.AnonymousId)

Example 12 with KeyAddress

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

the class SimpleRole method equals.

@Override
public boolean equals(Object obj) {
    if (obj instanceof SimpleRole) {
        boolean a = ((SimpleRole) obj).getName().equals(getName());
        boolean b = ((SimpleRole) obj).equalKeys(this);
        boolean c = ((SimpleRole) obj).anonymousIds.containsAll(this.anonymousIds);
        boolean d = this.anonymousIds.containsAll(((SimpleRole) obj).anonymousIds);
        if (!(a && b && c && d))
            return false;
        boolean e = true;
        for (KeyAddress ka1 : this.getKeyAddresses()) {
            e = false;
            for (KeyAddress ka2 : ((SimpleRole) obj).getKeyAddresses()) {
                if (ka1.equals(ka2)) {
                    e = true;
                    break;
                }
            }
            if (!e)
                break;
        }
        if (!e)
            return false;
        e = true;
        for (KeyAddress ka1 : ((SimpleRole) obj).getKeyAddresses()) {
            e = false;
            for (KeyAddress ka2 : this.getKeyAddresses()) {
                if (ka1.equals(ka2)) {
                    e = true;
                    break;
                }
            }
            if (!e)
                break;
        }
        return e;
    }
    return false;
}
Also used : KeyAddress(com.icodici.crypto.KeyAddress)

Example 13 with KeyAddress

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

the class Reference method compareOperands.

/**
 *The comparison method for finding reference contract
 *
 * @param refContract contract to check for matching
 * @param leftOperand field_selector
 * @param rightOperand right operand  (constant | field_selector), constant = ("null" | number | string | true | false)
 * @param typeOfRightOperand type of right operand (constant | field_selector), constant = ("null" | number | string | true | false)
 * @param indxOperator index operator in array of operators
 * @param contracts contract list to check for matching
 * @param iteration check inside references iteration number
 * @return true if match or false
 */
public boolean compareOperands(Contract refContract, String leftOperand, String rightOperand, compareOperandType typeOfRightOperand, int indxOperator, Collection<Contract> contracts, int iteration) {
    boolean ret = false;
    Contract leftOperandContract = null;
    Contract rightOperandContract = null;
    Object left = null;
    Object right = null;
    double leftValD = 0;
    double rightValD = 0;
    long leftValL = 0;
    long rightValL = 0;
    boolean isLeftDouble = false;
    boolean isRightDouble = false;
    int firstPointPos;
    if (leftOperand.startsWith("ref.")) {
        leftOperand = leftOperand.substring(4);
        leftOperandContract = refContract;
    } else if (leftOperand.startsWith("this.")) {
        if (baseContract == null)
            throw new IllegalArgumentException("Use left operand in condition: " + leftOperand + ". But this contract not initialized.");
        leftOperand = leftOperand.substring(5);
        leftOperandContract = baseContract;
    } else if ((firstPointPos = leftOperand.indexOf(".")) > 0) {
        if (baseContract == null)
            throw new IllegalArgumentException("Use left operand in condition: " + leftOperand + ". But this contract not initialized.");
        Reference ref = baseContract.findReferenceByName(leftOperand.substring(0, firstPointPos));
        if (ref == null)
            throw new IllegalArgumentException("Not found reference: " + leftOperand.substring(0, firstPointPos));
        for (Contract checkedContract : contracts) if (ref.isMatchingWith(checkedContract, contracts, iteration + 1))
            leftOperandContract = checkedContract;
        if (leftOperandContract == null)
            return false;
        leftOperand = leftOperand.substring(firstPointPos + 1);
    } else
        throw new IllegalArgumentException("Invalid format of left operand in condition: " + leftOperand + ". Missing contract field.");
    if (rightOperand != null) {
        // if != null, rightOperand then FIELD or CONSTANT
        if (typeOfRightOperand == compareOperandType.FIELD) {
            // if typeOfRightOperand - FIELD
            if (rightOperand.startsWith("ref.")) {
                rightOperand = rightOperand.substring(4);
                rightOperandContract = refContract;
            } else if (rightOperand.startsWith("this.")) {
                if (baseContract == null)
                    throw new IllegalArgumentException("Use right operand in condition: " + rightOperand + ". But this contract not initialized.");
                rightOperand = rightOperand.substring(5);
                rightOperandContract = baseContract;
            } else if ((firstPointPos = rightOperand.indexOf(".")) > 0) {
                if (baseContract == null)
                    throw new IllegalArgumentException("Use right operand in condition: " + rightOperand + ". But this contract not initialized.");
                Reference ref = baseContract.findReferenceByName(rightOperand.substring(0, firstPointPos));
                if (ref == null)
                    throw new IllegalArgumentException("Not found reference: " + rightOperand.substring(0, firstPointPos));
                for (Contract checkedContract : contracts) if (ref.isMatchingWith(checkedContract, contracts, iteration + 1))
                    rightOperandContract = checkedContract;
                if (rightOperandContract == null)
                    return false;
                rightOperand = rightOperand.substring(firstPointPos + 1);
            } else
                throw new IllegalArgumentException("Invalid format of right operand in condition: " + rightOperand + ". Missing contract field.");
        }
        left = leftOperandContract.get(leftOperand);
        if (rightOperandContract != null)
            right = rightOperandContract.get(rightOperand);
        try {
            switch(indxOperator) {
                case LESS:
                case MORE:
                case LESS_OR_EQUAL:
                case MORE_OR_EQUAL:
                    if (left == null)
                        break;
                    if (typeOfRightOperand == compareOperandType.FIELD) {
                        // rightOperand is FIELD
                        if (right != null) {
                            if (isLeftDouble = isObjectMayCastToDouble(left))
                                leftValD = objectCastToDouble(left);
                            else
                                leftValL = objectCastToLong(left);
                            if (isRightDouble = isObjectMayCastToDouble(right))
                                rightValD = objectCastToDouble(right);
                            else
                                rightValL = objectCastToLong(right);
                            if (((indxOperator == LESS) && ((isLeftDouble ? leftValD : leftValL) < (isRightDouble ? rightValD : rightValL))) || ((indxOperator == MORE) && ((isLeftDouble ? leftValD : leftValL) > (isRightDouble ? rightValD : rightValL))) || ((indxOperator == LESS_OR_EQUAL) && ((isLeftDouble ? leftValD : leftValL) <= (isRightDouble ? rightValD : rightValL))) || ((indxOperator == MORE_OR_EQUAL) && ((isLeftDouble ? leftValD : leftValL) >= (isRightDouble ? rightValD : rightValL))))
                                ret = true;
                        }
                    } else if (typeOfRightOperand == compareOperandType.CONSTOTHER) {
                        // rightOperand is CONSTANT (null | number | true | false)
                        if (isLeftDouble = isObjectMayCastToDouble(left))
                            leftValD = objectCastToDouble(left);
                        else
                            leftValL = objectCastToLong(left);
                        if ((rightOperand != "null") && (rightOperand != "false") && (rightOperand != "true"))
                            if ((rightOperand.contains(".") && (((indxOperator == LESS) && ((isLeftDouble ? leftValD : leftValL) < Double.parseDouble(rightOperand))) || ((indxOperator == MORE) && ((isLeftDouble ? leftValD : leftValL) > Double.parseDouble(rightOperand))) || ((indxOperator == LESS_OR_EQUAL) && ((isLeftDouble ? leftValD : leftValL) <= Double.parseDouble(rightOperand))) || ((indxOperator == MORE_OR_EQUAL) && ((isLeftDouble ? leftValD : leftValL) >= Double.parseDouble(rightOperand))))) || (!rightOperand.contains(".") && (((indxOperator == LESS) && ((isLeftDouble ? leftValD : leftValL) < Long.parseLong(rightOperand))) || ((indxOperator == MORE) && ((isLeftDouble ? leftValD : leftValL) > Long.parseLong(rightOperand))) || ((indxOperator == LESS_OR_EQUAL) && ((isLeftDouble ? leftValD : leftValL) <= Long.parseLong(rightOperand))) || ((indxOperator == MORE_OR_EQUAL) && ((isLeftDouble ? leftValD : leftValL) >= Long.parseLong(rightOperand))))))
                                ret = true;
                    } else
                        throw new IllegalArgumentException("Invalid operator for string in condition: " + operators[indxOperator]);
                    break;
                case NOT_EQUAL:
                case EQUAL:
                    if (typeOfRightOperand == compareOperandType.FIELD) {
                        // rightOperand is FIELD
                        if ((left != null) && (right != null)) {
                            boolean isNumbers = true;
                            if (isLeftDouble = isObjectMayCastToDouble(left))
                                leftValD = objectCastToDouble(left);
                            else if (isObjectMayCastToLong(left))
                                leftValL = objectCastToLong(left);
                            else
                                isNumbers = false;
                            if (isNumbers) {
                                if (isRightDouble = isObjectMayCastToDouble(right))
                                    rightValD = objectCastToDouble(right);
                                else if (isObjectMayCastToLong(right))
                                    rightValL = objectCastToLong(right);
                                else
                                    isNumbers = false;
                            }
                            if (isNumbers && ((isLeftDouble && !isRightDouble) || (!isLeftDouble && isRightDouble))) {
                                if (((indxOperator == NOT_EQUAL) && ((isLeftDouble ? leftValD : leftValL) != (isRightDouble ? rightValD : rightValL))) || ((indxOperator == EQUAL) && ((isLeftDouble ? leftValD : leftValL) == (isRightDouble ? rightValD : rightValL))))
                                    ret = true;
                            } else if (((indxOperator == NOT_EQUAL) && !left.equals(right)) || ((indxOperator == EQUAL) && left.equals(right)))
                                ret = true;
                        }
                    } else if ((left != null) && (left.getClass().getName().endsWith("Role") || left.getClass().getName().endsWith("RoleLink"))) {
                        // if role - compare with address
                        try {
                            KeyAddress ka = new KeyAddress(rightOperand);
                            ret = ((Role) left).isMatchingKeyAddress(ka);
                        } catch (Exception e) {
                            throw new IllegalArgumentException("Address compare error in condition: " + e.getMessage());
                        }
                        if (indxOperator == NOT_EQUAL)
                            ret = !ret;
                    } else if (typeOfRightOperand == compareOperandType.CONSTOTHER) {
                        // rightOperand is CONSTANT (null|number|true|false)
                        if (!rightOperand.equals("null") && !rightOperand.equals("false") && !rightOperand.equals("true")) {
                            if (left != null) {
                                if (isObjectMayCastToDouble(left)) {
                                    leftValD = objectCastToDouble(left);
                                    Double leftDouble = new Double(leftValD);
                                    if ((rightOperand.contains(".") && (((indxOperator == NOT_EQUAL) && !leftDouble.equals(Double.parseDouble(rightOperand))) || ((indxOperator == EQUAL) && leftDouble.equals(Double.parseDouble(rightOperand))))) || (!rightOperand.contains(".") && (((indxOperator == NOT_EQUAL) && (leftValD != Long.parseLong(rightOperand))) || ((indxOperator == EQUAL) && (leftValD == Long.parseLong(rightOperand))))))
                                        ret = true;
                                } else {
                                    leftValL = objectCastToLong(left);
                                    Long leftLong = new Long(leftValL);
                                    if ((!rightOperand.contains(".") && (((indxOperator == NOT_EQUAL) && !leftLong.equals(Long.parseLong(rightOperand))) || ((indxOperator == EQUAL) && leftLong.equals(Long.parseLong(rightOperand))))) || (rightOperand.contains(".") && (((indxOperator == NOT_EQUAL) && (leftValL != Double.parseDouble(rightOperand))) || ((indxOperator == EQUAL) && (leftValL == Double.parseDouble(rightOperand))))))
                                        ret = true;
                                }
                            }
                        } else {
                            // if rightOperand : null|false|true
                            if (((indxOperator == NOT_EQUAL) && ((rightOperand.equals("null") && (left != null)) || (rightOperand.equals("true") && ((left != null) && !(boolean) left)) || (rightOperand.equals("false") && ((left != null) && (boolean) left)))) || ((indxOperator == EQUAL) && ((rightOperand.equals("null") && (left == null)) || (rightOperand.equals("true") && ((left != null) && (boolean) left)) || (rightOperand.equals("false") && ((left != null) && !(boolean) left)))))
                                ret = true;
                        }
                    } else if (typeOfRightOperand == compareOperandType.CONSTSTR) {
                        // rightOperand is CONSTANT (string)
                        if ((left != null) && (((indxOperator == NOT_EQUAL) && !left.equals(rightOperand)) || ((indxOperator == EQUAL) && left.equals(rightOperand))))
                            ret = true;
                    }
                    break;
                case MATCHES:
                    break;
                default:
                    throw new IllegalArgumentException("Invalid operator in condition");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Error compare operands in condition: " + e.getMessage());
        }
    } else {
        // if rightOperand == null, then operation: defined / undefined
        if (indxOperator == DEFINED) {
            try {
                if (leftOperandContract.get(leftOperand) != null)
                    ret = true;
            } catch (Exception e) {
            }
        } else if (indxOperator == UNDEFINED) {
            try {
                ret = (leftOperandContract.get(leftOperand) == null);
            } catch (Exception e) {
                ret = true;
            }
        } else
            throw new IllegalArgumentException("Invalid operator in condition");
    }
    return ret;
}
Also used : Role(com.icodici.universa.contract.roles.Role) KeyAddress(com.icodici.crypto.KeyAddress)

Aggregations

KeyAddress (com.icodici.crypto.KeyAddress)13 PrivateKey (com.icodici.crypto.PrivateKey)7 Test (org.junit.Test)5 PublicKey (com.icodici.crypto.PublicKey)4 AnonymousId (com.icodici.universa.contract.AnonymousId)3 KeyRecord (com.icodici.universa.contract.KeyRecord)2 BackingStoreException (java.util.prefs.BackingStoreException)2 OptionException (joptsimple.OptionException)2 Binder (net.sergeych.tools.Binder)2 Contract (com.icodici.universa.contract.Contract)1 TransactionPack (com.icodici.universa.contract.TransactionPack)1 Role (com.icodici.universa.contract.roles.Role)1 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)1