From 1482599e521d1406611969eceeb6e8c593a84544 Mon Sep 17 00:00:00 2001 From: yann300 Date: Fri, 29 May 2015 16:28:30 +0200 Subject: [PATCH] - Check uint, int input from c++. - Check address also by contract name. --- mix/QBigInt.cpp | 35 +++++++++++++++++++++++++++++++++++ mix/QBigInt.h | 3 +++ mix/qml/TransactionDialog.qml | 2 +- mix/qml/js/InputValidator.js | 29 +++++++++++++++++++++-------- 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/mix/QBigInt.cpp b/mix/QBigInt.cpp index 21d32a9c3..ee29cea43 100644 --- a/mix/QBigInt.cpp +++ b/mix/QBigInt.cpp @@ -57,3 +57,38 @@ QBigInt* QBigInt::divide(QBigInt* const& _value) const BigIntVariant toDivide = _value->internalValue(); return new QBigInt(boost::apply_visitor(mix::divide(), m_internalValue, toDivide)); } + +QVariantMap QBigInt::checkAgainst(QString const& _type) const +{ + QVariantMap ret; + QString type = _type; + QString capacity = type.replace("uint", "").replace("int", ""); + if (capacity.isEmpty()) + capacity = "256"; + bigint range = 256^(capacity.toInt() / 8); + bigint value = boost::get(this->internalValue()); + ret.insert("valid", true); + if (_type.startsWith("uint") && value > range - 1) + { + ret.insert("minValue", "0"); + std::ostringstream s; + s << range - 1; + ret.insert("maxValue", QString::fromStdString(s.str())); + if (value > range) + ret["valid"] = false; + } + else if (_type.startsWith("int")) + { + range = range / 2; + std::ostringstream s; + s << -range; + ret.insert("minValue", QString::fromStdString(s.str())); + s.str(""); + s.clear(); + s << range - 1; + ret.insert("maxValue", QString::fromStdString(s.str())); + if (-range > value || value > range - 1) + ret["valid"] = false; + } + return ret; +} diff --git a/mix/QBigInt.h b/mix/QBigInt.h index b549a16db..ccf487d2a 100644 --- a/mix/QBigInt.h +++ b/mix/QBigInt.h @@ -84,6 +84,7 @@ public: Q_INVOKABLE QString value() const; /// Set the value of the BigInteger used. Will use u256 type. Invokable from QML. Q_INVOKABLE void setValue(QString const& _value) { m_internalValue = dev::jsToU256(_value.toStdString()); } + Q_INVOKABLE void setBigInt(QString const& _value) { m_internalValue = bigint(_value.toStdString()); } /// Subtract by @a _value. Invokable from QML. Q_INVOKABLE QBigInt* subtract(QBigInt* const& _value) const; /// Add @a _value to the current big integer. Invokable from QML. @@ -92,6 +93,8 @@ public: Q_INVOKABLE QBigInt* multiply(QBigInt* const& _value) const; /// divide by @a _value. Invokable from QML. Q_INVOKABLE QBigInt* divide(QBigInt* const& _value) const; + /// check if the current value satisfy the given type + Q_INVOKABLE QVariantMap checkAgainst(QString const& _type) const; protected: BigIntVariant m_internalValue; diff --git a/mix/qml/TransactionDialog.qml b/mix/qml/TransactionDialog.qml index a4c896d3f..df5ad781b 100644 --- a/mix/qml/TransactionDialog.qml +++ b/mix/qml/TransactionDialog.qml @@ -515,7 +515,7 @@ Dialog { } else { - errorDialog.text = qsTr("some parameters are invalid:\n"); + errorDialog.text = qsTr("Some parameters are invalid:\n"); for (var k in invalid) errorDialog.text += invalid[k].message + "\n"; errorDialog.open(); diff --git a/mix/qml/js/InputValidator.js b/mix/qml/js/InputValidator.js index 6e94dec6e..37185b898 100644 --- a/mix/qml/js/InputValidator.js +++ b/mix/qml/js/InputValidator.js @@ -1,3 +1,5 @@ +Qt.include("QEtherHelper.js") + var nbRegEx = new RegExp('^[0-9]+$'); function validate(model, values) { @@ -8,7 +10,9 @@ function validate(model, values) { var type = model[k].type.name; var res; - if (type.indexOf("int") !== -1) + if (isContractType(type)) + res = validateAddress(type, values[model[k].name]); + else if (type.indexOf("int") !== -1) res = validateInt(type, values[model[k].name]); else if (type.indexOf("bytes") !== -1) res = validateBytes(type, values[model[k].name]); @@ -17,7 +21,7 @@ function validate(model, values) else if (type.indexOf("address") !== -1) res = validateAddress(type, values[model[k].name]); else - res = validateAddress(type, values[model[k].name]); //we suppose that this is a ctr type. + res.valid = true; if (!res.valid) inError.push({ type: type, value: values, message: res.message }); } @@ -25,6 +29,16 @@ function validate(model, values) return inError; } +function isContractType(_type) +{ + for (var k in Object.keys(codeModel.contracts)) + { + if ("contract " + Object.keys(codeModel.contracts)[k] === _type) + return true; + } + return false; +} + function validateInt(_type, _value) { var ret = { valid: true, message: "" } @@ -35,7 +49,6 @@ function validateInt(_type, _value) { ret.valid = false; ret.message = "uint type cannot represent negative number"; - return false; } } ret.valid = nbRegEx.test(_value); @@ -43,12 +56,13 @@ function validateInt(_type, _value) ret.message = _value + " does not represent " + _type + " type."; else { - var t = _type.replace("uint", "").replace("int", ""); - var max = parseInt(t) / 4; - if (_value.length > max) + var bigInt = createBigInt(_value); + bigInt.setBigInt(_value); + var result = bigInt.checkAgainst(_type); + if (!result.valid) { ret.valid = false; - ret.message = _type + " should not contains more than " + max + " digits"; + ret.message = _type + " should be between " + result.minValue + " and " + result.maxValue; } } return ret; @@ -60,7 +74,6 @@ function validateAddress(_type, _value) if (_value.indexOf("<") === 0 && _value.indexOf(">") === _value.length - 1) { var v = _value.split(' - '); - console.log(JSON.stringify(v)); if (v.length !== 2 || !nbRegEx.test(v[1].replace(">", ""))) // { ret.valid = false;