Skip to content

Commit 09cdcbd

Browse files
committed
Added MainWindow::makeKeychainBackup()
1 parent 7bfeea6 commit 09cdcbd

File tree

2 files changed

+74
-47
lines changed

2 files changed

+74
-47
lines changed

src/mainwindow.cpp

+72-45
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,68 @@ void MainWindow::setKeychainPassphrase()
885885
}
886886
}
887887

888+
int MainWindow::makeKeychainBackup(const QString& keychainName)
889+
{
890+
QString name;
891+
bool bLocked;
892+
if (keychainName.isEmpty())
893+
{
894+
QModelIndex index = keychainSelectionModel->currentIndex();
895+
int row = index.row();
896+
if (row < 0) {
897+
showError(tr("No keychain is selected."));
898+
return QDialog::Rejected;
899+
}
900+
901+
if (!keychainModel->hasSeed(row))
902+
{
903+
showError(tr("Keychain does not contain seed."));
904+
return QDialog::Rejected;
905+
}
906+
907+
int status = keychainModel->getStatus(row);
908+
bLocked = status == KeychainModel::LOCKED;
909+
910+
QStandardItem* nameItem = keychainModel->item(row, 0);
911+
name = nameItem->data(Qt::DisplayRole).toString();
912+
}
913+
else
914+
{
915+
if (!keychainModel->exists(keychainName))
916+
{
917+
showError(tr("Keychain not found."));
918+
return QDialog::Rejected;
919+
}
920+
921+
bLocked = keychainModel->isLocked(keychainName);
922+
923+
name = keychainName;
924+
}
925+
926+
try
927+
{
928+
if (!synchedVault.isVaultOpen()) throw std::runtime_error("No vault is open.");
929+
CoinDB::VaultLock lock(synchedVault);
930+
if (!synchedVault.isVaultOpen()) throw std::runtime_error("No vault is open.");
931+
932+
if (bLocked && !unlockKeychain(name)) return QDialog::Rejected;
933+
934+
secure_bytes_t seed = synchedVault.getVault()->exportBIP39(name.toStdString());
935+
936+
KeychainBackupWizard wizard(name, seed, this);
937+
if (bLocked) { lockKeychain(name); }
938+
return wizard.exec();
939+
}
940+
catch (const exception& e)
941+
{
942+
LOGGER(error) << "MainWindow::makeKeychainBackup - " << e.what() << std::endl;
943+
if (bLocked) { lockKeychain(name); }
944+
showError(e.what());
945+
}
946+
947+
return QDialog::Rejected;
948+
}
949+
888950
void MainWindow::importKeychain(QString fileName)
889951
{
890952
if (fileName.isEmpty()) {
@@ -1136,39 +1198,6 @@ void MainWindow::viewBIP39()
11361198
}
11371199
}
11381200

1139-
void MainWindow::backupKeychain()
1140-
{
1141-
QModelIndex index = keychainSelectionModel->currentIndex();
1142-
int row = index.row();
1143-
if (row < 0) {
1144-
showError(tr("No keychain is selected."));
1145-
return;
1146-
}
1147-
1148-
QStandardItem* nameItem = keychainModel->item(row, 0);
1149-
QString name = nameItem->data(Qt::DisplayRole).toString();
1150-
1151-
try {
1152-
bytes_t extendedKey;
1153-
1154-
if (keychainModel->isPrivate(name)) {
1155-
// TODO: prompt user for decryption key
1156-
extendedKey = keychainModel->getExtendedKeyBytes(name, true);
1157-
}
1158-
else {
1159-
extendedKey = keychainModel->getExtendedKeyBytes(name);
1160-
}
1161-
1162-
KeychainBackupDialog dlg(tr("Keychain information"));
1163-
dlg.setExtendedKey(extendedKey);
1164-
dlg.exec();
1165-
}
1166-
catch (const exception& e) {
1167-
LOGGER(debug) << "MainWindow::backupKeychain - " << e.what() << std::endl;
1168-
showError(e.what());
1169-
}
1170-
}
1171-
11721201
void MainWindow::updateCurrentKeychain(const QModelIndex& current, const QModelIndex& /*previous*/)
11731202
{
11741203
int row = current.row();
@@ -1178,11 +1207,10 @@ void MainWindow::updateCurrentKeychain(const QModelIndex& current, const QModelI
11781207
viewPrivateBIP32Action->setEnabled(false);
11791208
viewPublicBIP32Action->setEnabled(false);
11801209
viewBIP39Action->setEnabled(false);
1181-
backupKeychainAction->setEnabled(false);
11821210
}
11831211
else {
11841212
int status = keychainModel->getStatus(row);
1185-
bool hasSeed = keychainModel->hasSeed(row);
1213+
bool hasSeed = keychainModel->hasSeed(row);
11861214

11871215
unlockKeychainAction->setEnabled(status == KeychainModel::LOCKED);
11881216
lockKeychainAction->setEnabled(status == KeychainModel::UNLOCKED);
@@ -1192,7 +1220,7 @@ void MainWindow::updateCurrentKeychain(const QModelIndex& current, const QModelI
11921220
viewPrivateBIP32Action->setEnabled(status != KeychainModel::PUBLIC);
11931221
viewPublicBIP32Action->setEnabled(true);
11941222
viewBIP39Action->setEnabled(status != KeychainModel::PUBLIC && hasSeed);
1195-
backupKeychainAction->setEnabled(true);
1223+
makeKeychainBackupAction->setEnabled(status != KeychainModel::PUBLIC && hasSeed);
11961224
}
11971225
}
11981226

@@ -2084,6 +2112,11 @@ void MainWindow::createActions()
20842112
setKeychainPassphraseAction->setEnabled(false);
20852113
connect(setKeychainPassphraseAction, SIGNAL(triggered()), this, SLOT(setKeychainPassphrase()));
20862114

2115+
makeKeychainBackupAction = new QAction(tr("Make Keychain Backup..."), this);
2116+
makeKeychainBackupAction->setStatusTip(tr("Open keychain backup wizard"));
2117+
makeKeychainBackupAction->setEnabled(false);
2118+
connect(makeKeychainBackupAction, SIGNAL(triggered()), this, SLOT(makeKeychainBackup()));
2119+
20872120
importPrivateAction = new QAction(tr("Private Imports"), this);
20882121
importPrivateAction->setCheckable(true);
20892122
importPrivateAction->setStatusTip(tr("Import private keys if available"));
@@ -2140,11 +2173,6 @@ void MainWindow::createActions()
21402173
viewBIP39Action->setEnabled(false);
21412174
connect(viewBIP39Action, SIGNAL(triggered()), this, SLOT(viewBIP39()));
21422175

2143-
backupKeychainAction = new QAction(tr("Backup Keychain..."), this);
2144-
backupKeychainAction->setStatusTip(tr("Make paper backup"));
2145-
backupKeychainAction->setEnabled(false);
2146-
connect(backupKeychainAction, SIGNAL(triggered()), this, SLOT(backupKeychain()));
2147-
21482176
// account actions
21492177
quickNewAccountAction = new QAction(QIcon(":/icons/magicwand.png"), tr("Account &Wizard..."), this);
21502178
quickNewAccountAction->setStatusTip(tr("Create a new account, automatically create new keychains for it"));
@@ -2354,6 +2382,9 @@ void MainWindow::createMenus()
23542382
keychainMenu->addAction(lockKeychainAction);
23552383
keychainMenu->addAction(lockAllKeychainsAction);
23562384
keychainMenu->addAction(setKeychainPassphraseAction);
2385+
keychainMenu->addSeparator();
2386+
keychainMenu->addAction(makeKeychainBackupAction);
2387+
23572388
/*
23582389
keychainMenu->addSeparator()->setText(tr("Import Mode"));
23592390
keychainMenu->addAction(importPrivateAction);
@@ -2377,10 +2408,6 @@ void MainWindow::createMenus()
23772408

23782409
keychainMenu->addSeparator();
23792410
keychainMenu->addAction(quickNewAccountAction);
2380-
/*
2381-
keychainMenu->addSeparator();
2382-
keychainMenu->addAction(backupKeychainAction);
2383-
*/
23842411

23852412
networkMenu = menuBar()->addMenu(tr("&Network"));
23862413
networkMenu->addAction(connectAction);

src/mainwindow.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ private slots:
142142
void lockKeychain(QString name = QString());
143143
void lockAllKeychains();
144144
void setKeychainPassphrase();
145+
int makeKeychainBackup(const QString& keychainName = QString());
145146
void importKeychain(QString fileName = QString());
146147
void exportKeychain(bool exportPrivate);
147148
void importBIP32();
148149
void viewBIP32(bool viewPrivate);
149150
void importBIP39();
150151
void viewBIP39();
151-
void backupKeychain();
152152
// void mergeKeychains();
153153
// void removeKeychain();
154154
// void renameKeychain();
@@ -294,6 +294,7 @@ private slots:
294294
QAction* lockKeychainAction;
295295
QAction* lockAllKeychainsAction;
296296
QAction* setKeychainPassphraseAction;
297+
QAction* makeKeychainBackupAction;
297298
bool importPrivate;
298299
QAction* importPrivateAction;
299300
QAction* importPublicAction;
@@ -306,7 +307,6 @@ private slots:
306307
QAction* viewPublicBIP32Action;
307308
QAction* importBIP39Action;
308309
QAction* viewBIP39Action;
309-
QAction* backupKeychainAction;
310310

311311
// account actions
312312
QAction* quickNewAccountAction;

0 commit comments

Comments
 (0)