handle password changes

This commit is contained in:
Howard Stearns 2018-08-14 13:14:48 -07:00
parent ef9cc782b9
commit ef1aacd226
3 changed files with 29 additions and 11 deletions
interface/src/commerce

View file

@ -117,7 +117,7 @@ void Ledger::buy(const QString& hfc_key, int cost, const QString& asset_id, cons
signedSend("transaction", transactionString, hfc_key, "buy", "buySuccess", "buyFailure", controlled_failure);
}
bool Ledger::receiveAt(const QString& hfc_key, const QString& signing_key) {
bool Ledger::receiveAt(const QString& hfc_key, const QString& signing_key, const QByteArray& locker) {
auto accountManager = DependencyManager::get<AccountManager>();
if (!accountManager->isLoggedIn()) {
qCWarning(commerce) << "Cannot set receiveAt when not logged in.";
@ -125,8 +125,6 @@ bool Ledger::receiveAt(const QString& hfc_key, const QString& signing_key) {
emit receiveAtResult(result);
return false; // We know right away that we will fail, so tell the caller.
}
auto wallet = DependencyManager::get<Wallet>();
QByteArray locker = wallet->getWallet();
QJsonObject transaction;
transaction["public_key"] = hfc_key;
transaction["locker"] = QString::fromUtf8(locker);
@ -137,6 +135,16 @@ bool Ledger::receiveAt(const QString& hfc_key, const QString& signing_key) {
return true; // Note that there may still be an asynchronous signal of failure that callers might be interested in.
}
bool Ledger::receiveAt() {
auto wallet = DependencyManager::get<Wallet>();
auto keys = wallet->listPublicKeys();
if (keys.isEmpty()) {
return false;
}
auto key = keys.first();
return receiveAt(key, key, wallet->getWallet());
}
void Ledger::balance(const QStringList& keys) {
keysQuery("balance", "balanceSuccess", "balanceFailure");
}
@ -304,18 +312,16 @@ void Ledger::accountSuccess(QNetworkReply* reply) {
QString keyStatus = "ok";
QStringList localPublicKeys = wallet->listPublicKeys();
if (remotePublicKey.isEmpty() || isOverride) {
if (!localPublicKeys.isEmpty()) {
QString key = localPublicKeys.first();
receiveAt(key, key);
if (!localPublicKeys.isEmpty()) { // Let the metaverse know about a local wallet.
receiveAt();
}
} else {
if (localPublicKeys.isEmpty()) {
keyStatus = "preexisting";
} else if (localPublicKeys.first() != remotePublicKey) {
keyStatus = "conflicting";
} else if (locker.isEmpty()) {
QString key = localPublicKeys.first();
receiveAt(key, key);
} else if (locker.isEmpty()) { // Matches metaverse data, but we haven't lockered it yet.
receiveAt();
}
}

View file

@ -26,7 +26,8 @@ class Ledger : public QObject, public Dependency {
public:
void buy(const QString& hfc_key, int cost, const QString& asset_id, const QString& inventory_key, const bool controlled_failure = false);
bool receiveAt(const QString& hfc_key, const QString& signing_key);
bool receiveAt(const QString& hfc_key, const QString& signing_key, const QByteArray& locker);
bool receiveAt();
void balance(const QStringList& keys);
void inventory(const QString& editionFilter, const QString& typeFilter, const QString& titleFilter, const int& page, const int& perPage);
void history(const QStringList& keys, const int& pageNumber, const int& itemsPerPage);

View file

@ -602,7 +602,7 @@ bool Wallet::generateKeyPair() {
// 2. It is maximally private, and we can step back from that later if desired.
// 3. It maximally exercises all the machinery, so we are most likely to surface issues now.
auto ledger = DependencyManager::get<Ledger>();
return ledger->receiveAt(key, key);
return ledger->receiveAt(key, key, getWallet());
}
QStringList Wallet::listPublicKeys() {
@ -741,6 +741,11 @@ QString Wallet::getKeyFilePath() {
bool Wallet::writeWallet(const QString& newPassphrase) {
EC_KEY* keys = readKeys(keyFilePath().toStdString().c_str());
auto ledger = DependencyManager::get<Ledger>();
// Remove any existing locker, because it will be out of date.
if (!_publicKeys.isEmpty() && !ledger->receiveAt(_publicKeys.first(), _publicKeys.first(), QByteArray())) {
return false; // FIXME: receiveAt could fail asynchronously.
}
if (keys) {
// we read successfully, so now write to a new temp file
QString tempFileName = QString("%1.%2").arg(keyFilePath(), QString("temp"));
@ -748,6 +753,7 @@ bool Wallet::writeWallet(const QString& newPassphrase) {
if (!newPassphrase.isEmpty()) {
setPassphrase(newPassphrase);
}
if (writeKeys(tempFileName.toStdString().c_str(), keys)) {
if (writeSecurityImage(_securityImage, tempFileName)) {
// ok, now move the temp file to the correct spot
@ -755,6 +761,11 @@ bool Wallet::writeWallet(const QString& newPassphrase) {
QFile(tempFileName).rename(QString(keyFilePath()));
qCDebug(commerce) << "wallet written successfully";
emit keyFilePathIfExistsResult(getKeyFilePath());
if (!walletIsAuthenticatedWithPassphrase() || !ledger->receiveAt()) {
// FIXME: Should we fail the whole operation?
// Tricky, because we'll need the the key and file from the TEMP location...
qCWarning(commerce) << "Failed to update locker";
}
return true;
} else {
qCDebug(commerce) << "couldn't write security image to temp wallet";