Skip to content

Commit

Permalink
Merge pull request #348 from meritlabs/feature/import-mnemonic
Browse files Browse the repository at this point in the history
Import Wallet Mnemonic
  • Loading branch information
tonypizzicato authored May 11, 2018
2 parents 8fa3596 + ab64116 commit a107b6e
Show file tree
Hide file tree
Showing 28 changed files with 900 additions and 63 deletions.
4 changes: 4 additions & 0 deletions src/Makefile.qt.include
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ QT_FORMS_UI = \
qt/forms/editaddressdialog.ui \
qt/forms/enterunlockcode.ui \
qt/forms/exportwalletdialog.ui \
qt/forms/importwalletdialog.ui \
qt/forms/helpmessagedialog.ui \
qt/forms/intro.ui \
qt/forms/modaloverlay.ui \
Expand Down Expand Up @@ -133,6 +134,7 @@ QT_MOC_CPP = \
qt/moc_csvmodelwriter.cpp \
qt/moc_editaddressdialog.cpp \
qt/moc_exportwalletdialog.cpp \
qt/moc_importwalletdialog.cpp \
qt/moc_enterunlockcode.cpp \
qt/moc_guiutil.cpp \
qt/moc_intro.cpp \
Expand Down Expand Up @@ -207,6 +209,7 @@ MERIT_QT_H = \
qt/csvmodelwriter.h \
qt/editaddressdialog.h \
qt/exportwalletdialog.h \
qt/importwalletdialog.h \
qt/enterunlockcode.h \
qt/guiconstants.h \
qt/guiutil.h \
Expand Down Expand Up @@ -327,6 +330,7 @@ MERIT_QT_BASE_CPP = \
qt/intro.cpp \
qt/enterunlockcode.cpp \
qt/exportwalletdialog.cpp \
qt/importwalletdialog.cpp \
qt/modaloverlay.cpp \
qt/networkstyle.cpp \
qt/notificator.cpp \
Expand Down
23 changes: 23 additions & 0 deletions src/crypto/mnemonic/mnemonic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ namespace mnemonic
}
}

WordList MnemonicStringToWords(const std::string& mnemonic)
{
std::stringstream s;
s << mnemonic;

WordList words;
while(s.good()) {
std::string word;
s >> word;

if(!word.empty()) {
words.push_back(word);
}
}
return words;
}

bool IsAValidMnemonic(const WordList& words) {
return words.size() == MNEMONIC_WORD_COUNT;
}

std::array<uint8_t, SEED_LENGTH> MnemonicToSeed(const WordList& mnemonic, const std::string& passphrase)
{
return MnemonicToSeed(Unwords(mnemonic), passphrase);
Expand Down Expand Up @@ -69,6 +90,8 @@ namespace mnemonic

WordList mnemonic(MNEMONIC_WORD_COUNT);
std::transform(inds.begin(), inds.end(), mnemonic.begin(), [&dict](const int& i) { return dict[i]; });

assert(IsAValidMnemonic(mnemonic));
return mnemonic;
}
}
2 changes: 2 additions & 0 deletions src/crypto/mnemonic/mnemonic.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace mnemonic
static constexpr size_t ENTROPY_BYTES = 16;
std::array<uint8_t, SEED_LENGTH> MnemonicToSeed(const WordList& mnemonic, const std::string& passphrase = "");
std::array<uint8_t, SEED_LENGTH> MnemonicToSeed(const std::string& mnemonic, const std::string& passphrase = "");
bool IsAValidMnemonic(const WordList& words);
WordList MnemonicStringToWords(const std::string& mnemonic);
std::string Unwords(const WordList& phrase);
WordList Entropy2Mnemonic(const std::vector<uint8_t>& entropy, const language::Dictionary& dict);
}
Expand Down
18 changes: 18 additions & 0 deletions src/qt/enterunlockcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "enterunlockcode.h"
#include "ui_enterunlockcode.h"
#include "importwalletdialog.h"

#include "guiutil.h"

Expand All @@ -25,11 +26,13 @@ userClosed(false)
connect(ui->aliasTextInput, SIGNAL(textChanged(QString)), this, SLOT(aliasChanged(QString)));
connect(this, SIGNAL(CanSubmitChanged(bool)), ui->submitButton, SLOT(setEnabled(bool)));
connect(ui->submitButton, SIGNAL(clicked()), this, SLOT(submit()));
connect(ui->importButton, SIGNAL(clicked()), this, SLOT(importWallet()));
if (parent) {
parent->installEventFilter(this);
raise();
}

ui->importButton->setEnabled(false);
setVisible(false);
}

Expand Down Expand Up @@ -90,6 +93,7 @@ void EnterUnlockCode::showHide(bool hide, bool userRequested)
void EnterUnlockCode::setModel(WalletModel *model)
{
this->walletModel = model;
ui->importButton->setEnabled(true);
}

extern CTxDestination LookupDestination(const std::string& address);
Expand Down Expand Up @@ -164,3 +168,17 @@ void EnterUnlockCode::submit()
}
}
}

void EnterUnlockCode::importWallet()
{
if(!walletModel) {
return;
}

ImportWalletDialog importWalletDialog{this, walletModel};
importWalletDialog.exec();

if(importWalletDialog.result() == QDialog::Accepted) {
Q_EMIT WalletReferred();
}
}
3 changes: 3 additions & 0 deletions src/qt/enterunlockcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Ui {
class EnterUnlockCode;
}

class ImportWalletDialog;

/** Modal overlay to display information about the chain-sync state */
class EnterUnlockCode : public QWidget
{
Expand All @@ -31,6 +33,7 @@ public Q_SLOTS:
// will show or hide the modal layer
void showHide(bool hide = false, bool userRequested = false);
bool isLayerVisible() const { return layerIsVisible; }
void importWallet();

protected:
bool eventFilter(QObject * obj, QEvent * ev);
Expand Down
18 changes: 18 additions & 0 deletions src/qt/exportwalletdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "walletmodel.h"
#include "exportwalletdialog.h"
#include "ui_exportwalletdialog.h"
#include "util.h"

ExportWalletDialog::ExportWalletDialog(QWidget *parent, WalletModel *model) :
QDialog(parent),
Expand All @@ -34,11 +35,22 @@ ExportWalletDialog::~ExportWalletDialog()
delete ui;
}

void ExportWalletDialog::closeEvent(QCloseEvent *event) {
hideCode();
QDialog::closeEvent(event);
}

void ExportWalletDialog::onCancelClicked()
{
hideCode();
reject();
}

void ExportWalletDialog::hideCode() {
qrCodeIsVisible = false;
setQRCodeVisibility();
}

void ExportWalletDialog::onShowClicked()
{
qrCodeIsVisible = !qrCodeIsVisible;
Expand All @@ -49,6 +61,12 @@ void ExportWalletDialog::setQRCodeVisibility()
{
if(qrCodeIsVisible)
{
WalletModel::UnlockContext ctx(walletModel->requestUnlock());
if(!ctx.isValid()) {
// Unlock wallet was cancelled
return;
}

ui->lblQRCode->setText("");
#ifdef USE_QRCODE
bool livenet = Params().NetworkIDString() == CBaseChainParams::MAIN;
Expand Down
5 changes: 4 additions & 1 deletion src/qt/exportwalletdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ class ExportWalletDialog : public QDialog
explicit ExportWalletDialog(QWidget *parent, WalletModel *model);
~ExportWalletDialog();

protected:
void closeEvent(QCloseEvent *event);

private Q_SLOTS:
void onCancelClicked();
void onShowClicked();

private:
WalletModel *walletModel;
Ui::ExportWalletDialog *ui;
bool qrCodeIsVisible = false;

void hideCode();
void setQRCodeVisibility();
};

Expand Down
100 changes: 98 additions & 2 deletions src/qt/forms/enterunlockcode.ui
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

QLabel { color: rgb(40,40,40); }</string>
</property>
<layout class="QVBoxLayout" name="verticalLayoutSub" stretch="0,0">
<layout class="QVBoxLayout" name="verticalLayoutSub" stretch="0,0,0,0,0,0,0">
<property name="spacing">
<number>0</number>
</property>
Expand Down Expand Up @@ -260,7 +260,7 @@ QLabel { color: rgb(40,40,40); }</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Other users of &lt;span style=&quot; font-weight:600;&quot;&gt;Merit&lt;/span&gt; can use your &lt;span style=&quot; font-weight:600;&quot;&gt;Public Alias&lt;/span&gt; instead of an address to send you funds. This alias is &lt;span style=&quot; font-style:italic;&quot;&gt;optional&lt;/span&gt;. An alias cannot be changed for an address once it is chosen.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;The alias must be &lt;span style=&quot; font-weight:600;&quot;&gt;globally unique&lt;/span&gt;, nobody else can use your alias once it is commited to the Merit Blockchain.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;For more information, visit: &lt;a href=&quot;http://merit.me/aliases.html&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;merit.me/aliases.html&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Choose an optional Public Alias.</string>
<string>Choose a Public Alias.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
Expand Down Expand Up @@ -307,11 +307,33 @@ QLabel { color: rgb(40,40,40); }</string>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_7">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::MinimumExpanding</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>10</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="submitButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>40</height>
</size>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
Expand All @@ -322,6 +344,80 @@ QLabel { color: rgb(40,40,40); }</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_6">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Minimum</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>15</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="bottomMargin">
<number>10</number>
</property>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="importButton">
<property name="font">
<font>
<pointsize>9</pointsize>
</font>
</property>
<property name="text">
<string>Import Existing Wallet</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>80</height>
</size>
</property>
</spacer>
</item>
<item>
<spacer name="verticalSpacer_5">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
Expand Down
Loading

0 comments on commit a107b6e

Please sign in to comment.