diff --git a/src/cpp/controller/User.cpp b/src/cpp/controller/User.cpp index ad1fd9fc5..389642f71 100644 --- a/src/cpp/controller/User.cpp +++ b/src/cpp/controller/User.cpp @@ -295,7 +295,7 @@ namespace controller { //! \return -1 no matching entry found //! \return -2 if user id is not set or invalid //! \return 0 matching entry found - int User::tryLoadPassphraseUserBackup() + int User::tryLoadPassphraseUserBackup(KeyPairEd25519** createdKeyPair = nullptr) { auto user_model = getModel(); if (user_model->getID() <= 0) return -2; @@ -308,7 +308,11 @@ namespace controller { continue; } auto key_pair = std::unique_ptr(user_backup->createGradidoKeyPair()); + if (key_pair->isTheSame(user_model->getPublicKey())) { + if (createdKeyPair) { + *createdKeyPair = key_pair.get(); + } return 0; } } diff --git a/src/cpp/controller/User.h b/src/cpp/controller/User.h index 5c856780f..e251beb19 100644 --- a/src/cpp/controller/User.h +++ b/src/cpp/controller/User.h @@ -46,7 +46,7 @@ namespace controller { //! \return -1 no matching entry found //! \return -2 user id invalid or net set //! \return 0 matching entry found - int tryLoadPassphraseUserBackup(); + int tryLoadPassphraseUserBackup(KeyPairEd25519** createdKeyPair = nullptr); inline size_t load(const std::string& email) { return getModel()->loadFromDB("email", email); } //! \brief try to load user from db via user_id diff --git a/src/cpp/tasks/SigningTransaction.cpp b/src/cpp/tasks/SigningTransaction.cpp index d7dbd7086..d2fd51a3b 100644 --- a/src/cpp/tasks/SigningTransaction.cpp +++ b/src/cpp/tasks/SigningTransaction.cpp @@ -71,10 +71,20 @@ int SigningTransaction::run() { //auto privKey = mUser->getPrivKey(); //if (!mUser->hasPrivKey()) { auto gradido_key_pair = mNewUser->getGradidoKeyPair(); + if(!gradido_key_pair || !gradido_key_pair->hasPrivateKey()) { - addError(new Error("SigningTransaction", "user cannot decrypt private key")); - sendErrorsAsEmail(); - return -2; + KeyPairEd25519** key_pair_ptr = nullptr; + if (!mNewUser->tryLoadPassphraseUserBackup(key_pair_ptr)) { + if(mNewUser->setGradidoKeyPair(*key_pair_ptr)) + { + mNewUser->getModel()->updatePrivkey(); + } + } + else { + addError(new Error("SigningTransaction", "user cannot decrypt private key")); + sendErrorsAsEmail(); + return -2; + } } // get body bytes model::messages::gradido::Transaction transaction;