X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fwallet.cpp;h=780c7f4e37b74d4a2aa81eae9ab59bec00137891;hb=9c9ba366ebc85b0d8eef6d962a9fa84cffe0cae7;hp=5595052f1002d31e3aead64a7984ade3d924cf2d;hpb=0dd25284c30dfdbfeff9a882dd801be8cacd0859;p=novacoin.git diff --git a/src/wallet.cpp b/src/wallet.cpp index 5595052..780c7f4 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -57,6 +57,28 @@ CPubKey CWallet::GenerateNewKey() return key.GetPubKey(); } +CMalleableKeyView CWallet::GenerateNewMalleableKey() +{ + RandAddSeedPerfmon(); + + // Compressed public keys were introduced in version 0.6.0 + SetMinVersion(FEATURE_MALLKEY); + + CMalleableKey mKey; + mKey.MakeNewKeys(); + const CMalleableKeyView &keyView(mKey); + + // Create new metadata + int64_t nCreationTime = GetTime(); + mapMalleableKeyMetadata[keyView] = CKeyMetadata(nCreationTime); + if (!nTimeFirstKey || nCreationTime < nTimeFirstKey) + nTimeFirstKey = nCreationTime; + + if (!AddMalleableKey(mKey)) + throw std::runtime_error("CWallet::GenerateNewMalleableKey() : AddMalleableKey failed"); + return CMalleableKeyView(mKey); +} + bool CWallet::AddKey(const CKey& key) { CPubKey pubkey = key.GetPubKey(); @@ -69,6 +91,18 @@ bool CWallet::AddKey(const CKey& key) return true; } +bool CWallet::AddMalleableKey(const CMalleableKey& mKey) +{ + CMalleableKeyView keyView = CMalleableKeyView(mKey); + if (!CCryptoKeyStore::AddMalleableKey(mKey)) + return false; + if (!fFileBacked) + return true; + if (!IsCrypted()) + return CWalletDB(strWalletFile).WriteMalleableKey(keyView, mKey, mapMalleableKeyMetadata[keyView]); + return true; +} + bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, const vector &vchCryptedSecret) { if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret)) @@ -101,6 +135,15 @@ bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta) return true; } +bool CWallet::LoadMalleableKeyMetadata(const CMalleableKeyView &keyView, const CKeyMetadata &metadata) +{ + if (metadata.nCreateTime && (!nTimeFirstKey || metadata.nCreateTime < nTimeFirstKey)) + nTimeFirstKey = metadata.nCreateTime; + + mapMalleableKeyMetadata[keyView] = metadata; + return true; +} + bool CWallet::AddCScript(const CScript& redeemScript) { if (!CCryptoKeyStore::AddCScript(redeemScript)) @@ -1688,7 +1731,7 @@ bool CWallet::CreateTransaction(CScript scriptPubKey, int64_t nValue, CWalletTx& void CWallet::GetStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& nWeight) { - int64_t nTimeWeight = GetWeight(nTime, (int64_t)GetTime()); + int64_t nTimeWeight = GetWeight(nTime, GetTime()); // If time weight is lower or equal to zero then weight is zero. if (nTimeWeight <= 0) @@ -1895,7 +1938,7 @@ bool CWallet::CreateCoinStake(uint256 &hashTx, uint32_t nOut, uint32_t nGenerati if (setCoins.empty()) return false; - bool fMaxTimeWeight = false; + bool fDontSplitCoins = false; if (GetWeight((int64_t)wtx.nTime, (int64_t)nGenerationTime) == nStakeMaxAge) { // Only one output for old kernel inputs @@ -1938,16 +1981,29 @@ bool CWallet::CreateCoinStake(uint256 &hashTx, uint32_t nOut, uint32_t nGenerati vwtxPrev.push_back(pcoin->first); } - fMaxTimeWeight = true; + fDontSplitCoins = true; } else { - // Split stake input if maximum weight isn't reached yet - txNew.vout.push_back(CTxOut(0, scriptPubKeyOut)); - txNew.vout.push_back(CTxOut(0, scriptPubKeyOut)); + int64_t nSplitThreshold = GetArg("-splitthreshold", nCombineThreshold); if (fDebug && GetBoolArg("-printcoinstake")) - printf("CreateCoinStake : maximum time weight isn't reached, splitting coinstake\n"); + printf("CreateCoinStake : nSplitThreshold=%" PRId64 "\n", nSplitThreshold); + + if (nCredit > nSplitThreshold) + { + // Split stake input if credit is lower than combine threshold and maximum weight isn't reached yet + txNew.vout.push_back(CTxOut(0, scriptPubKeyOut)); + txNew.vout.push_back(CTxOut(0, scriptPubKeyOut)); + + if (fDebug && GetBoolArg("-printcoinstake")) + printf("CreateCoinStake : splitting coinstake\n"); + } + else + { + txNew.vout.push_back(CTxOut(0, scriptPubKeyOut)); + fDontSplitCoins = true; + } } // Calculate coin age reward @@ -1961,7 +2017,7 @@ bool CWallet::CreateCoinStake(uint256 &hashTx, uint32_t nOut, uint32_t nGenerati while (true) { // Set output amount - if (fMaxTimeWeight) + if (fDontSplitCoins) txNew.vout[1].nValue = nCredit - nMinFee; else { @@ -2058,6 +2114,12 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey) string CWallet::SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, bool fAskFee) { + // Check amount + if (nValue <= 0) + return _("Invalid amount"); + if (nValue + nTransactionFee > GetBalance()) + return _("Insufficient funds"); + CReserveKey reservekey(this); int64_t nFeeRequired; @@ -2093,26 +2155,6 @@ string CWallet::SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNe return ""; } - - -string CWallet::SendMoneyToDestination(const CTxDestination& address, int64_t nValue, CWalletTx& wtxNew, bool fAskFee) -{ - // Check amount - if (nValue <= 0) - return _("Invalid amount"); - if (nValue + nTransactionFee > GetBalance()) - return _("Insufficient funds"); - - // Parse Bitcoin address - CScript scriptPubKey; - scriptPubKey.SetDestination(address); - - return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee); -} - - - - DBErrors CWallet::LoadWallet(bool& fFirstRunRet) { if (!fFileBacked) @@ -2738,3 +2780,39 @@ void CWallet::ClearOrphans() for(list::const_iterator it = orphans.begin(); it != orphans.end(); ++it) EraseFromWallet(*it); } + +bool CWallet::ExtractAddress(const CScript& scriptPubKey, std::string& addressRet) +{ + vector vSolutions; + txnouttype whichType; + if (!Solver(scriptPubKey, whichType, vSolutions)) + return false; + + if (whichType == TX_PUBKEY) + { + addressRet = CBitcoinAddress(CPubKey(vSolutions[0]).GetID()).ToString(); + return true; + } + if (whichType == TX_PUBKEY_DROP) + { + // Pay-to-Pubkey-R + CMalleableKeyView view; + if (!CheckOwnership(CPubKey(vSolutions[0]), CPubKey(vSolutions[1]), view)) + return false; + + addressRet = view.GetMalleablePubKey().ToString(); + return true; + } + else if (whichType == TX_PUBKEYHASH) + { + addressRet = CBitcoinAddress(CKeyID(uint160(vSolutions[0]))).ToString(); + return true; + } + else if (whichType == TX_SCRIPTHASH) + { + addressRet = CBitcoinAddress(CScriptID(uint160(vSolutions[0]))).ToString(); + return true; + } + // Multisig txns have more than one address... + return false; +}