Browse Source

new account plugin finished

cl-refactor
debris 10 years ago
parent
commit
cb2a5ec268
  1. 3
      alethzero/Main.ui
  2. 77
      alethzero/MainWin.cpp
  3. 1
      alethzero/MainWin.h
  4. 125
      alethzero/NewAccount.cpp
  5. 14
      alethzero/NewAccount.h
  6. 99
      alethzero/NewAccount.ui

3
alethzero/Main.ui

@ -132,7 +132,7 @@
<x>0</x>
<y>0</y>
<width>1617</width>
<height>24</height>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
@ -162,7 +162,6 @@
<addaction name="prepNextDAG"/>
<addaction name="separator"/>
<addaction name="newTransaction"/>
<addaction name="newAccount"/>
<addaction name="importKey"/>
<addaction name="importKeyFile"/>
<addaction name="claimPresale"/>

77
alethzero/MainWin.cpp

@ -2104,83 +2104,6 @@ void Main::keysChanged()
onBalancesChange();
}
bool beginsWith(Address _a, bytes const& _b)
{
for (unsigned i = 0; i < min<unsigned>(20, _b.size()); ++i)
if (_a[i] != _b[i])
return false;
return true;
}
void Main::on_newAccount_triggered()
{
bool ok = true;
enum { NoVanity = 0, DirectICAP, FirstTwo, FirstTwoNextTwo, FirstThree, FirstFour, StringMatch };
QStringList items = {"No vanity (instant)", "Direct ICAP address", "Two pairs first (a few seconds)", "Two pairs first and second (a few minutes)", "Three pairs first (a few minutes)", "Four pairs first (several hours)", "Specific hex string"};
unsigned v = items.QList<QString>::indexOf(QInputDialog::getItem(this, "Vanity Key?", "Would you a vanity key? This could take several hours.", items, 1, false, &ok));
if (!ok)
return;
bytes bs;
if (v == StringMatch)
{
QString s = QInputDialog::getText(this, "Vanity Beginning?", "Enter some hex digits that it should begin with.\nNOTE: The more you enter, the longer generation will take.", QLineEdit::Normal, QString(), &ok);
if (!ok)
return;
bs = fromHex(s.toStdString());
}
KeyPair p;
bool keepGoing = true;
unsigned done = 0;
function<void()> f = [&]() {
KeyPair lp;
while (keepGoing)
{
done++;
if (done % 1000 == 0)
cnote << "Tried" << done << "keys";
lp = KeyPair::create();
auto a = lp.address();
if (v == NoVanity ||
(v == DirectICAP && !a[0]) ||
(v == FirstTwo && a[0] == a[1]) ||
(v == FirstTwoNextTwo && a[0] == a[1] && a[2] == a[3]) ||
(v == FirstThree && a[0] == a[1] && a[1] == a[2]) ||
(v == FirstFour && a[0] == a[1] && a[1] == a[2] && a[2] == a[3]) ||
(v == StringMatch && beginsWith(lp.address(), bs))
)
break;
}
if (keepGoing)
p = lp;
keepGoing = false;
};
vector<std::thread*> ts;
for (unsigned t = 0; t < std::thread::hardware_concurrency() - 1; ++t)
ts.push_back(new std::thread(f));
f();
for (std::thread* t: ts)
{
t->join();
delete t;
}
QString s = QInputDialog::getText(this, "Create Account", "Enter this account's name");
if (QMessageBox::question(this, "Create Account", "Would you like to use additional security for this key? This lets you protect it with a different password to other keys, but also means you must re-enter the key's password every time you wish to use the account.", QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
{
bool ok = false;
std::string hint;
std::string password = getPassword("Create Account", "Enter the password you would like to use for this key. Don't forget it!", &hint, &ok);
if (!ok)
return;
m_keyManager.import(p.secret(), s.toStdString(), password, hint);
}
else
m_keyManager.import(p.secret(), s.toStdString());
keysChanged();
}
void Main::on_killAccount_triggered()
{
if (ui->ourAccounts->currentRow() >= 0)

1
alethzero/MainWin.h

@ -152,7 +152,6 @@ private slots:
void on_preview_triggered();
// Account management
void on_newAccount_triggered();
void on_killAccount_triggered();
void on_importKey_triggered();
void on_reencryptKey_triggered();

125
alethzero/NewAccount.cpp

@ -15,17 +15,14 @@
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file NewAccount.h
* @author Gav Wood <i@gavwood.com>
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2015
*/
#include "NewAccount.h"
#include <QMenu>
#include <QDialog>
#include <boost/random/random_device.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <libdevcore/Log.h>
#include <libdevcrypto/WordList.h>
#include <libethcore/KeyManager.h>
#include <libethereum/Client.h>
#include "ui_NewAccount.h"
@ -34,6 +31,14 @@ using namespace dev;
using namespace az;
using namespace eth;
bool beginsWith(Address _a, bytes const& _b)
{
for (unsigned i = 0; i < min<unsigned>(20, _b.size()); ++i)
if (_a[i] != _b[i])
return false;
return true;
}
DEV_AZ_NOTE_PLUGIN(NewAccount);
NewAccount::NewAccount(MainFace* _m):
@ -49,30 +54,116 @@ NewAccount::~NewAccount()
void NewAccount::create()
{
QDialog d;
Ui_NewAccount u;
Ui::NewAccount u;
u.setupUi(&d);
d.setWindowTitle("New Account Wallet");
u.enterHexText->setEnabled(false);
u.enterPasswordText->setEnabled(false);
u.enterPasswordAgainText->setEnabled(false);
enum { NoVanity = 0, DirectICAP, FirstTwo, FirstTwoNextTwo, FirstThree, FirstFour, StringMatch };
u.hexText->setEnabled(false);
u.passwordText->setEnabled(false);
u.passwordAgainText->setEnabled(false);
QStringList items = {"No vanity (instant)", "Direct ICAP address", "Two pairs first (a few seconds)", "Two pairs first and second (a few minutes)", "Three pairs first (a few minutes)", "Four pairs first (several hours)", "Specific hex string"};
u.selectTypeComboBox->addItems(items);
QStringList items =
{
"No vanity (instant)",
"Direct ICAP address",
"Two pairs first (a few seconds)",
"Two pairs first and second (a few minutes)",
"Three pairs first (a few minutes)",
"Four pairs first (several hours)",
"Specific hex string"
};
u.typeComboBox->addItems(items);
void (QComboBox:: *indexChangedSignal)(int) = &QComboBox::currentIndexChanged;
connect(u.selectTypeComboBox, indexChangedSignal, [&](int index) {
u.enterHexText->setEnabled(index == StringMatch);
connect(u.typeComboBox, indexChangedSignal, [&](int index)
{
u.hexText->setEnabled(index == StringMatch);
});
connect(u.additionalCheckBox, &QCheckBox::clicked, [&]() {
connect(u.additionalCheckBox, &QCheckBox::clicked, [&]()
{
bool checked = u.additionalCheckBox->checkState() == Qt::CheckState::Checked;
u.enterPasswordText->setEnabled(checked);
u.enterPasswordAgainText->setEnabled(checked);
u.passwordText->setEnabled(checked);
u.passwordAgainText->setEnabled(checked);
});
connect(u.create, &QPushButton::clicked, [&]()
{
if (u.additionalCheckBox->checkState() == Qt::CheckState::Checked && !validatePassword(u))
{
u.passwordAgainLabel->setStyleSheet("QLabel { color : red }");
u.passwordAgainLabel->setText("Invalid! Please re-enter password correctly:");
return;
}
d.accept();
});
if (d.exec() == QDialog::Accepted)
onDialogAccepted(u);
}
bool NewAccount::validatePassword(Ui::NewAccount const& _u)
{
return QString::compare(_u.passwordText->toPlainText(), _u.passwordAgainText->toPlainText()) == 0;
}
void NewAccount::onDialogAccepted(Ui::NewAccount const& _u)
{
Type v = (Type)_u.typeComboBox->currentIndex();
bytes bs = fromHex(_u.hexText->toPlainText().toStdString());
KeyPair p = newKeyPair(v, bs);
QString s = _u.nameText->toPlainText();
if (_u.additionalCheckBox->checkState() == Qt::CheckState::Checked)
{
std::string hint = _u.hintText->toPlainText().toStdString();
std::string password = _u.passwordText->toPlainText().toStdString();
main()->keyManager().import(p.secret(), s.toStdString(), password, hint);
}
else
main()->keyManager().import(p.secret(), s.toStdString());
main()->noteKeysChanged();
}
KeyPair NewAccount::newKeyPair(Type _type, bytes const& _prefix)
{
KeyPair p;
bool keepGoing = true;
unsigned done = 0;
function<void()> f = [&]() {
KeyPair lp;
while (keepGoing)
{
done++;
if (done % 1000 == 0)
cnote << "Tried" << done << "keys";
lp = KeyPair::create();
auto a = lp.address();
if (_type == NoVanity ||
(_type == DirectICAP && !a[0]) ||
(_type == FirstTwo && a[0] == a[1]) ||
(_type == FirstTwoNextTwo && a[0] == a[1] && a[2] == a[3]) ||
(_type == FirstThree && a[0] == a[1] && a[1] == a[2]) ||
(_type == FirstFour && a[0] == a[1] && a[1] == a[2] && a[2] == a[3]) ||
(_type == StringMatch && beginsWith(lp.address(), _prefix))
)
break;
}
if (keepGoing)
p = lp;
keepGoing = false;
};
vector<std::thread*> ts;
for (unsigned t = 0; t < std::thread::hardware_concurrency() - 1; ++t)
ts.push_back(new std::thread(f));
f();
for (std::thread* t: ts)
{
//main()->noteKeysChanged();
t->join();
delete t;
}
return p;
}

14
alethzero/NewAccount.h

@ -15,7 +15,7 @@
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file NewAccount.h
* @author Gav Wood <i@gavwood.com>
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2015
*/
@ -23,6 +23,12 @@
#include "MainFace.h"
namespace Ui
{
class NewAccount;
}
namespace dev
{
namespace az
@ -38,6 +44,12 @@ public:
private slots:
void create();
private:
enum Type { NoVanity = 0, DirectICAP, FirstTwo, FirstTwoNextTwo, FirstThree, FirstFour, StringMatch };
bool validatePassword(Ui::NewAccount const& _u);
void onDialogAccepted(Ui::NewAccount const& _u);
KeyPair newKeyPair(Type _type, bytes const& _prefix);
};
}

99
alethzero/NewAccount.ui

@ -13,9 +13,12 @@
<property name="windowTitle">
<string>Dialog</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="selectTypeLabel">
<widget class="QLabel" name="typeLabel">
<property name="maximumSize">
<size>
<width>16777215</width>
@ -31,7 +34,7 @@
</widget>
</item>
<item>
<widget class="QComboBox" name="selectTypeComboBox">
<widget class="QComboBox" name="typeComboBox">
<property name="maximumSize">
<size>
<width>16777215</width>
@ -41,7 +44,7 @@
</widget>
</item>
<item>
<widget class="QLabel" name="enterHexLabel">
<widget class="QLabel" name="hexLabel">
<property name="maximumSize">
<size>
<width>16777215</width>
@ -54,17 +57,17 @@
</widget>
</item>
<item>
<widget class="QTextEdit" name="enterHexText">
<widget class="QTextEdit" name="hexText">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>20</height>
<height>21</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="enterNameLabel">
<widget class="QLabel" name="nameLabel">
<property name="maximumSize">
<size>
<width>16777215</width>
@ -77,20 +80,13 @@
</widget>
</item>
<item>
<widget class="QTextEdit" name="enterNameText">
<widget class="QTextEdit" name="nameText">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>20</height>
<height>21</height>
</size>
</property>
<property name="html">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'.Helvetica Neue DeskInterface'; font-size:13pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
@ -120,7 +116,7 @@ p, li { white-space: pre-wrap; }
</widget>
</item>
<item>
<widget class="QLabel" name="enterPasswordLabel">
<widget class="QLabel" name="passwordLabel">
<property name="maximumSize">
<size>
<width>16777215</width>
@ -133,20 +129,17 @@ p, li { white-space: pre-wrap; }
</widget>
</item>
<item>
<widget class="QTextEdit" name="enterPasswordText">
<property name="enabled">
<bool>true</bool>
</property>
<widget class="QTextEdit" name="passwordText">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>20</height>
<height>21</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="enterPasswordAgainLabel">
<widget class="QLabel" name="passwordAgainLabel">
<property name="maximumSize">
<size>
<width>16777215</width>
@ -159,14 +152,28 @@ p, li { white-space: pre-wrap; }
</widget>
</item>
<item>
<widget class="QTextEdit" name="enterPasswordAgainText">
<property name="enabled">
<bool>true</bool>
<widget class="QTextEdit" name="passwordAgainText">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>21</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="hintLabel">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;Enter hint:&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
<widget class="QTextEdit" name="hintText">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>20</height>
<height>50</height>
</size>
</property>
</widget>
@ -188,6 +195,15 @@ p, li { white-space: pre-wrap; }
</item>
<item>
<widget class="QPushButton" name="cancel">
<property name="maximumSize">
<size>
<width>83</width>
<height>16777215</height>
</size>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="text">
<string>Cancel</string>
</property>
@ -195,6 +211,21 @@ p, li { white-space: pre-wrap; }
</item>
<item>
<widget class="QPushButton" name="create">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>83</width>
<height>16777215</height>
</size>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="text">
<string>&amp;Create</string>
</property>
@ -209,22 +240,6 @@ p, li { white-space: pre-wrap; }
</widget>
<resources/>
<connections>
<connection>
<sender>create</sender>
<signal>clicked()</signal>
<receiver>NewAccount</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>462</x>
<y>484</y>
</hint>
<hint type="destinationlabel">
<x>449</x>
<y>504</y>
</hint>
</hints>
</connection>
<connection>
<sender>cancel</sender>
<signal>clicked()</signal>

Loading…
Cancel
Save