Browse Source

- Check uint, int input from c++.

- Check address also by contract name.
cl-refactor
yann300 10 years ago
parent
commit
1482599e52
  1. 35
      mix/QBigInt.cpp
  2. 3
      mix/QBigInt.h
  3. 2
      mix/qml/TransactionDialog.qml
  4. 29
      mix/qml/js/InputValidator.js

35
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<bigint>(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;
}

3
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;

2
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();

29
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(">", ""))) // <Contract - 2>
{
ret.valid = false;

Loading…
Cancel
Save