Wallet cleanup

This commit is contained in:
David Kelly 2017-09-05 16:33:16 -07:00
parent ad5b8c438e
commit 350cee5f35
5 changed files with 15 additions and 32 deletions

View file

@ -325,7 +325,7 @@ Rectangle {
onClicked: {
if (passphraseSelection.validateAndSubmitPassphrase()) {
root.lastPage = "choosePassphrase";
commerce.balance(); // Do this here so that keys are generated. Order might change as backend changes?
commerce.generateKeyPair();
choosePassphraseContainer.visible = false;
privateKeysReadyContainer.visible = true;
}

View file

@ -86,7 +86,7 @@ void QmlCommerce::history() {
void QmlCommerce::setPassphrase(const QString& passphrase) {
auto wallet = DependencyManager::get<Wallet>();
if (wallet->getPassphrase() && !wallet->getPassphrase()->isEmpty()) {
if(wallet->getPassphrase() && !wallet->getPassphrase()->isEmpty() && !passphrase.isEmpty()) {
wallet->changePassphrase(passphrase);
} else {
wallet->setPassphrase(passphrase);
@ -94,6 +94,12 @@ void QmlCommerce::setPassphrase(const QString& passphrase) {
getWalletAuthenticatedStatus();
}
void QmlCommerce::generateKeyPair() {
auto wallet = DependencyManager::get<Wallet>();
wallet->generateKeyPair();
getWalletAuthenticatedStatus();
}
void QmlCommerce::reset() {
auto ledger = DependencyManager::get<Ledger>();
auto wallet = DependencyManager::get<Wallet>();

View file

@ -53,7 +53,7 @@ protected:
Q_INVOKABLE void balance();
Q_INVOKABLE void inventory();
Q_INVOKABLE void history();
Q_INVOKABLE void generateKeyPair();
Q_INVOKABLE void reset();
};

View file

@ -245,6 +245,8 @@ RSA* readPrivateKey(const char* filename) {
} else {
qCDebug(commerce) << "couldn't parse" << filename;
// if the passphrase is wrong, then let's not cache it
DependencyManager::get<Wallet>()->setPassphrase("");
}
fclose(fp);
} else {
@ -273,8 +275,6 @@ void Wallet::setPassphrase(const QString& passphrase) {
}
_passphrase = new QString(passphrase);
// no matter what, we now need to clear the keys as they
// need to be read using this passphrase
_publicKeys.clear();
}
@ -413,28 +413,10 @@ bool Wallet::walletIsAuthenticatedWithPassphrase() {
return false;
}
bool Wallet::createIfNeeded() {
if (_publicKeys.count() > 0) return false;
bool Wallet::generateKeyPair() {
// FIXME: initialize OpenSSL elsewhere soon
initialize();
// try to read existing keys if they exist...
auto publicKey = readPublicKey(keyFilePath().toStdString().c_str());
if (publicKey.size() > 0) {
if (auto key = readPrivateKey(keyFilePath().toStdString().c_str()) ) {
qCDebug(commerce) << "read private key";
RSA_free(key);
// K -- add the public key since we have a legit private key associated with it
_publicKeys.push_back(publicKey.toBase64());
return false;
}
}
qCInfo(commerce) << "Creating wallet.";
return generateKeyPair();
}
bool Wallet::generateKeyPair() {
qCInfo(commerce) << "Generating keypair.";
auto keyPair = generateRSAKeypair();
sendKeyFilePathIfExists();
@ -453,7 +435,6 @@ bool Wallet::generateKeyPair() {
QStringList Wallet::listPublicKeys() {
qCInfo(commerce) << "Enumerating public keys.";
createIfNeeded();
return _publicKeys;
}
@ -572,12 +553,8 @@ void Wallet::reset() {
// tell the provider we got nothing
updateImageProvider();
delete _passphrase;
_passphrase->clear();
// for now we need to maintain the hard-coded passphrase.
// FIXME: remove this line as part of wiring up the passphrase
// and probably set it to nullptr
_passphrase = new QString("pwd");
QFile keyFile(keyFilePath());
QFile imageFile(imageFilePath());
@ -608,6 +585,7 @@ bool Wallet::changePassphrase(const QString& newPassphrase) {
return false;
}
}
qCDebug(commerce) << "couldn't read keys";
qCDebug(commerce) << "couldn't decrypt keys with current passphrase, clearing";
setPassphrase(QString(""));
return false;
}

View file

@ -26,7 +26,6 @@ public:
~Wallet();
// These are currently blocking calls, although they might take a moment.
bool createIfNeeded();
bool generateKeyPair();
QStringList listPublicKeys();
QString signWithKey(const QByteArray& text, const QString& key);