Skip to content

Commit

Permalink
Use keychain backup wizard for account wizard.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeShark committed May 24, 2015
1 parent c2b6170 commit 2d3d346
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
39 changes: 28 additions & 11 deletions src/keychainbackupwizard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,35 @@
KeychainBackupWizard::KeychainBackupWizard(const QString& name, const secure_bytes_t& seed, QWidget* parent)
: QWizard(parent)
{
addPage(new WordlistViewPage(name, seed));
addPage(new WordlistVerifyPage(name, seed));
addPage(new WordlistCompletePage(name));
addPage(new WordlistViewPage(1, name, seed));
addPage(new WordlistVerifyPage(2, name, seed));
addPage(new WordlistCompletePage(3));
setWindowTitle(tr("Keychain Backup"));
}

KeychainBackupWizard::KeychainBackupWizard(const QList<QString>& names, const QList<secure_bytes_t>& seeds, QWidget* parent)
: QWizard(parent)
{
if (names.size() != seeds.size()) throw std::runtime_error("Number of names does not match number of seeds.");
if (names.isEmpty()) throw std::runtime_error("At least one keychain is required.");

int i = 0;
for (auto& name: names)
{
const secure_bytes_t& seed = seeds.at(i);
addPage(new WordlistViewPage(2*i + 1, name, seed));
addPage(new WordlistVerifyPage(2*i + 2, name, seed));
i++;
}
addPage(new WordlistCompletePage(2*i + 1));
setWindowTitle(tr("Keychain Backup"));
}


WordlistViewPage::WordlistViewPage(const QString& name, const secure_bytes_t& seed, QWidget* parent)
WordlistViewPage::WordlistViewPage(int i, const QString& name, const secure_bytes_t& seed, QWidget* parent)
: QWizardPage(parent)
{
setTitle(tr("Step 1: Copy"));
setTitle(tr("Step ") + QString::number(i) + tr(": Copy"));
setSubTitle(tr("Keychain: ") + name);

QLabel* promptLabel = new QLabel(tr("IMPORTANT: Please write down the following list of words. In the event you lose your data you can recover your keychain by entering this list."));
Expand All @@ -51,12 +69,12 @@ WordlistViewPage::WordlistViewPage(const QString& name, const secure_bytes_t& se
setLayout(layout);
}

WordlistVerifyPage::WordlistVerifyPage(const QString& name, const secure_bytes_t& seed, QWidget* parent)
WordlistVerifyPage::WordlistVerifyPage(int i, const QString& name, const secure_bytes_t& seed, QWidget* parent)
: QWizardPage(parent)
{
using namespace Coin;

setTitle(tr("Step 2: Verify"));
setTitle(tr("Step ") + QString::number(i) + tr(": Verify"));
setSubTitle(tr("Keychain: ") + name);

QLabel* promptLabel = new QLabel(tr("Please enter the words in the correct order below:"));
Expand Down Expand Up @@ -87,13 +105,12 @@ bool WordlistVerifyPage::isComplete() const
return (wordlist == wordlistEdit->text());
}

WordlistCompletePage::WordlistCompletePage(const QString& name, QWidget* parent)
WordlistCompletePage::WordlistCompletePage(int i, QWidget* parent)
: QWizardPage(parent)
{
setTitle(tr("Step 3: Put in safe, secure place"));
setSubTitle(tr("Keychain: ") + name);
setTitle(tr("Step ") + QString::number(i) + tr(": Put in safe, secure place"));

QLabel* promptLabel = new QLabel(tr("IMPORTANT: Keep the wordlist in a safe place."));
QLabel* promptLabel = new QLabel(tr("IMPORTANT: Keep all wordlists in a safe place."));
promptLabel->setWordWrap(true);

QVBoxLayout* layout = new QVBoxLayout();
Expand Down
8 changes: 5 additions & 3 deletions src/keychainbackupwizard.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <CoinQ/CoinQ_typedefs.h>

#include <QWizard>
#include <QList>

class QPlainTextEdit;
class QLineEdit;
Expand All @@ -23,6 +24,7 @@ class KeychainBackupWizard : public QWizard

public:
KeychainBackupWizard(const QString& name, const secure_bytes_t& seed, QWidget* parent = NULL);
KeychainBackupWizard(const QList<QString>& names, const QList<secure_bytes_t>& seeds, QWidget* parent = NULL);
};


Expand All @@ -31,7 +33,7 @@ class WordlistViewPage : public QWizardPage
Q_OBJECT

public:
WordlistViewPage(const QString& name, const secure_bytes_t& seed, QWidget* parent = NULL);
WordlistViewPage(int i, const QString& name, const secure_bytes_t& seed, QWidget* parent = NULL);

protected:
QPlainTextEdit* wordlistEdit;
Expand All @@ -42,7 +44,7 @@ class WordlistVerifyPage : public QWizardPage
Q_OBJECT

public:
WordlistVerifyPage(const QString& name, const secure_bytes_t& seed, QWidget* parent = NULL);
WordlistVerifyPage(int i, const QString& name, const secure_bytes_t& seed, QWidget* parent = NULL);

bool isComplete() const;

Expand All @@ -58,5 +60,5 @@ class WordlistCompletePage : public QWizardPage
Q_OBJECT

public:
WordlistCompletePage(const QString& name, QWidget* parent = NULL);
WordlistCompletePage(int i, QWidget* parent = NULL);
};
31 changes: 24 additions & 7 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,8 @@ void MainWindow::quickNewAccount()
if (!synchedVault.isVaultOpen()) throw std::runtime_error("No vault is open.");

QuickNewAccountDialog dlg(this);
while (dlg.exec()) {
while (dlg.exec())
{
try {
QString accountName = dlg.getName();
if (accountName.isEmpty())
Expand All @@ -1397,23 +1398,39 @@ void MainWindow::quickNewAccount()
const int MAX_KEYCHAIN_INDEX = 1000;
int i = 0;
QList<QString> keychainNames;
while (keychainNames.size() < dlg.getMaxSigs() && ++i <= MAX_KEYCHAIN_INDEX) {
QList<secure_bytes_t> keychainSeeds;
while (keychainNames.size() < dlg.getMaxSigs() && ++i <= MAX_KEYCHAIN_INDEX)
{
QString keychainName = accountName + " " + QString::number(i);
if (!keychainModel->exists(keychainName))
{
// TODO: Randomize using user input for seed entropy
keychainNames << keychainName;
keychainSeeds << getRandomBytes(32);
}
}

if (i > MAX_KEYCHAIN_INDEX)
throw std::runtime_error(tr("Ran out of keychain indices.").toStdString());

for (auto& keychainName: keychainNames) {
// TODO: Randomize using user input for entropy
// Require user to copy down the wordlists
KeychainBackupWizard backupDlg(keychainNames, keychainSeeds, this);
if (!backupDlg.exec()) return;

{
CoinDB::VaultLock lock(synchedVault);
if (!synchedVault.isVaultOpen()) throw std::runtime_error("No vault is open.");
secure_bytes_t entropy = getRandomBytes(32);
synchedVault.getVault()->newKeychain(keychainName.toStdString(), entropy);

int i = 0;
for (auto& keychainName: keychainNames)
{
const secure_bytes_t& keychainSeed = keychainSeeds.at(i++);
synchedVault.getVault()->newKeychain(keychainName.toStdString(), keychainSeed);
}

accountModel->newAccount(accountName, dlg.getMinSigs(), keychainNames, dlg.getCreationTime());
}

accountModel->newAccount(accountName, dlg.getMinSigs(), keychainNames, dlg.getCreationTime());
accountModel->update();
accountView->updateColumns();
keychainModel->update();
Expand Down

0 comments on commit 2d3d346

Please sign in to comment.