Beispiel #1
0
bool CAccount::AddCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, int64_t nKeyChain)
{

    assert(!IsHD());

    if (nKeyChain == KEYCHAIN_EXTERNAL) {
        if (!externalKeyStore.AddCryptedKey(vchPubKey, vchCryptedSecret))
            return false;
    } else {
        if (!internalKeyStore.AddCryptedKey(vchPubKey, vchCryptedSecret))
            return false;
    }

    if (pwalletMain) {
        if (!pwalletMain->fFileBacked)
            return true;
        {
            LOCK(pwalletMain->cs_wallet);
            if (pwalletMain->pwalletdbEncryption)
                return pwalletMain->pwalletdbEncryption->WriteCryptedKey(vchPubKey, vchCryptedSecret, pwalletMain->mapKeyMetadata[vchPubKey.GetID()], getUUID(), nKeyChain);
            else
                return CWalletDB(pwalletMain->strWalletFile).WriteCryptedKey(vchPubKey, vchCryptedSecret, pwalletMain->mapKeyMetadata[vchPubKey.GetID()], getUUID(), nKeyChain);
        }
    } else {
        return true;
    }
    return false;
}
Beispiel #2
0
bool CAccountHD::GetKey(const CKeyID& keyID, CKey& key) const
{
    assert(!m_readOnly);
    assert(IsHD());

    // Witness keys are a special case.
    // They are stored instead of generated on demand (so that they work in encrypted wallets)
    if (IsPoW2Witness())
    {
        if (internalKeyStore.GetKey(keyID, key))
        {
            // But skip it if it is an empty key... (Which can happen by design)
            // Technical: Witness keystore is filled with "public/empty" pairs as the public keys are created and then populated on demand with "public/witness" keys only when they are needed/generated (first use)
            if (key.IsValid())
                return true;
        }
    }

    if (IsLocked())
        return false;

    int64_t nKeyIndex = -1;
    CExtKey privKey;
    if (externalKeyStore.GetKey(keyID, nKeyIndex))
    {
        primaryChainKeyPriv.Derive(privKey, nKeyIndex);
        if(privKey.Neuter().pubkey.GetID() != keyID)
            assert(0);
        key = privKey.key;
        return true;
    }
    if (internalKeyStore.GetKey(keyID, nKeyIndex))
    {
        changeChainKeyPriv.Derive(privKey, nKeyIndex);
        if(privKey.Neuter().pubkey.GetID() != keyID)
            assert(0);
        key = privKey.key;
        return true;
    }
    return false;
}
Beispiel #3
0
bool CAccount::AddCryptedKeyWithChain(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret, int64_t nKeyChain)
{
    //fixme: (Post-2.1) This is essentially dead code now - it has been replaced at the bottom of CWallet::AddKeyPubKey
    //For technical reasons (wallet upgrade)

    //This should never be called on a non-HD wallet
    assert(!IsHD());

    if (nKeyChain == KEYCHAIN_EXTERNAL)
    {
        if (!externalKeyStore.AddCryptedKey(vchPubKey, vchCryptedSecret))
            return false;
    }
    else
    {
        if (!internalKeyStore.AddCryptedKey(vchPubKey, vchCryptedSecret))
            return false;
    }

    // If we don't have a wallet yet (busy during wallet upgrade) - then the below not being called is fine as the wallet does a 'force resave' of all keys at the end of the upgrade.
    if (pactiveWallet)
    {
        {
            LOCK(pactiveWallet->cs_wallet);
            if (pactiveWallet->pwalletdbEncryption)
                return pactiveWallet->pwalletdbEncryption->WriteCryptedKey(vchPubKey, vchCryptedSecret, pactiveWallet->mapKeyMetadata[vchPubKey.GetID()], getUUIDAsString(getUUID()), nKeyChain);
            else
                return CWalletDB(*pactiveWallet->dbw).WriteCryptedKey(vchPubKey, vchCryptedSecret, pactiveWallet->mapKeyMetadata[vchPubKey.GetID()], getUUIDAsString(getUUID()), nKeyChain);
        }
    }
    else
    {
        return true;
    }
    return false;
}