Merge pull request #11311 from davidkelly/dk/walletCleanup

wallet cleanup
This commit is contained in:
Howard Stearns 2017-09-06 14:29:24 -07:00 committed by GitHub
commit a0ae9ee747
8 changed files with 21 additions and 33 deletions

View file

@ -55,6 +55,7 @@ Item {
text: "DEBUG: Clear Cached Passphrase";
onClicked: {
commerce.setPassphrase("");
sendSignalToWallet({method: 'passphraseReset'});
}
}
HifiControlsUit.Button {

View file

@ -318,7 +318,7 @@ Rectangle {
Connections {
onSendSignalToWallet: {
if (msg.method === 'walletReset') {
if (msg.method === 'walletReset' || msg.method === 'passphraseReset') {
sendToScript(msg);
}
}

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);

View file

@ -71,6 +71,10 @@
case 'maybeEnableHmdPreview':
Menu.setIsOptionChecked("Disable Preview", isHmdPreviewDisabled);
break;
case 'passphraseReset':
onButtonClicked();
onButtonClicked();
break;
case 'walletReset':
onButtonClicked();
onButtonClicked();