Repository: dogecoin/dogecoin
File: src/qt/importkeysdialog.cpp (C++)

Code before:
    if (!key.IsValid()) {
        vchSecret.SetString("");
        ui->privateKeyImportTextMessage->setText(tr("Invalid private key; please check and try again!"));
        return false;
    }

    CPubKey pubkey = key.GetPubKey();
    assert(key.VerifyPubKey(pubkey));
    CKeyID vchAddress = pubkey.GetID();

    pwalletMain->MarkDirty();
    pwalletMain->SetAddressBook(vchAddress, privateKeyLabel.toStdString(), "receive");

    if (pwalletMain->HaveKey(vchAddress)) {
        vchSecret.SetString("");
        ui->privateKeyImportTextMessage->setText(
            tr("Invalid address generated from private key; please check and try again!"
        ));
        return false;
    }

    pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = 1;

    if (!pwalletMain->AddKeyPubKey(key, pubkey)) {
        vchSecret.SetString("");
        ui->privateKeyImportTextMessage->setText(tr("Couldn't add private key; does it already exist?"));
        return false;
    }

    pwalletMain->UpdateTimeFirstKey(1);

    if (rescan) {
        ImportKeyExecutor *executor = new ImportKeyExecutor();
        executor->moveToThread(&thread);
        connect(this, SIGNAL(rescanWallet(CWallet*, CBlockIndex*)), executor, SLOT(rescan(CWallet*, CBlockIndex*)));

        // On stopExecutor signal
        // - quit the Qt event loop in the execution thread
        connect(this, SIGNAL(stopExecutor()), &thread, SLOT(quit()));
        // - queue executor for deletion (in execution thread)
        connect(&thread, SIGNAL(finished()), executor, SLOT(deleteLater()), Qt::DirectConnection);

        // Default implementation of QThread::run() simply spins up an event loop in the thread,
        // which is what we want.
        thread.start();
        ui->privateKeyImportTextMessage->setText(tr("Rescanning..."));
        Q_EMIT rescanWallet(pwalletMain, chainActive.Genesis());
    }

    vchSecret.SetString("");
    return true;

Review comment (refactor):
Are we sure that when a key already exists, we want to react differently than when it doesn't? IIRC this is why RPC doesn't throw an error on duplicate key import, because that could be used to expose which keys are in the wallet? 

Code after:
    if (!key.IsValid()) {
        vchSecret.SetString("");
        ui->privateKeyImportTextMessage->setText(tr("Invalid private key; please check and try again!"));
        return false;
    }

    CPubKey pubkey = key.GetPubKey();
    assert(key.VerifyPubKey(pubkey));
    CKeyID vchAddress = pubkey.GetID();

    pwalletMain->MarkDirty();
    pwalletMain->SetAddressBook(vchAddress, privateKeyLabel.toStdString(), "receive");

    if (pwalletMain->HaveKey(vchAddress)) {
        vchSecret.SetString("");
        ui->privateKeyImportTextMessage->setText(
            tr("Invalid address generated from private key; please check and try again!"
        ));
        return false;
    }

    pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = 1;

    if (!pwalletMain->AddKeyPubKey(key, pubkey)) {
        vchSecret.SetString("");
        ui->privateKeyImportTextMessage->setText(tr("Failed to add private key."));
        return false;
    }

    pwalletMain->UpdateTimeFirstKey(1);

    if (rescan) {
        ImportKeyExecutor *executor = new ImportKeyExecutor();
        executor->moveToThread(&thread);
        connect(this, SIGNAL(rescanWallet(CWallet*, CBlockIndex*)), executor, SLOT(rescan(CWallet*, CBlockIndex*)));

        // On stopExecutor signal
        // - quit the Qt event loop in the execution thread
        connect(this, SIGNAL(stopExecutor()), &thread, SLOT(quit()));
        // - queue executor for deletion (in execution thread)
        connect(&thread, SIGNAL(finished()), executor, SLOT(deleteLater()), Qt::DirectConnection);

        // Default implementation of QThread::run() simply spins up an event loop in the thread,
        // which is what we want.
        thread.start();
        ui->privateKeyImportTextMessage->setText(tr("Rescanning..."));
        Q_EMIT rescanWallet(pwalletMain, chainActive.Genesis());
    }

    vchSecret.SetString("");
    return true;